function views_ui_preview

3.x admin.inc views_ui_preview($view, $display_id, $args = array())
2.x admin.inc views_ui_preview($js, $view)

Returns the results of the live preview.

1 call to views_ui_preview()
1 string reference to 'views_ui_preview'

File

includes/admin.inc, line 105
Provides the Views' administrative interface.

Code

function views_ui_preview($view, $display_id, $args = array()) {
  // When this function is invoked as a page callback, each Views argument is
  // passed separately.
  if (!is_array($args)) {
    $args = array_slice(func_get_args(), 2);
  }

  // Save $_GET['q'] so it can be restored before returning from this function.
  $q = $_GET['q'];

  // Determine where the query and performance statistics should be output.
  $show_query = variable_get('views_ui_show_sql_query', FALSE);
  $show_info = variable_get('views_ui_show_preview_information', FALSE);
  $show_location = variable_get('views_ui_show_sql_query_where', 'above');

  $show_stats = variable_get('views_ui_show_performance_statistics', FALSE);
  if ($show_stats) {
    $show_stats = variable_get('views_ui_show_sql_query_where', 'above');
  }

  $combined = $show_query && $show_stats;

  $rows = array('query' => array(), 'statistics' => array());
  $output = '';

  $errors = $view->validate();
  if ($errors === TRUE) {
    $view->ajax = TRUE;
    $view->live_preview = TRUE;
    $view->views_ui_context = TRUE;

    // AJAX happens via $_POST but everything expects exposed data to
    // be in GET. Copy stuff but remove ajax-framework specific keys.
    // If we're clicking on links in a preview, though, we could actually
    // still have some in $_GET, so we use $_REQUEST to ensure we get it all.
    $exposed_input = $_REQUEST;
    foreach (array('view_name', 'view_display_id', 'view_args', 'view_path', 'view_dom_id', 'pager_element', 'view_base_path', 'ajax_html_ids', 'ajax_page_state', 'form_id', 'form_build_id', 'form_token') as $key) {
      if (isset($exposed_input[$key])) {
        unset($exposed_input[$key]);
      }
    }

    $view->set_exposed_input($exposed_input);


    if (!$view->set_display($display_id)) {
      return t('Invalid display id @display', array('@display' => $display_id));
    }

    $view->set_arguments($args);

    // Store the current view URL for later use:
    if ($view->display_handler->get_option('path')) {
      $path = $view->get_url();
    }

    // Make view links come back to preview.
    $view->override_path = 'admin/structure/views/nojs/preview/' . $view->name . '/' . $display_id;

    // Also override $_GET['q'] so we get the pager.
    $original_path = current_path();
    $_GET['q'] = $view->override_path;
    if ($args) {
      $_GET['q'] .= '/' . implode('/', $args);
    }

    // Suppress contextual links of entities within the result set during a
    // Preview.
    // @todo We'll want to add contextual links specific to editing the View, so
    //   the suppression may need to be moved deeper into the Preview pipeline.
    views_ui_contextual_links_suppress_push();
    $preview = $view->preview($display_id, $args);
    views_ui_contextual_links_suppress_pop();

    // Reset variables.
    unset($view->override_path);
    $_GET['q'] = $original_path;

    // Prepare the query information and statistics to show either above or
    // below the view preview.
    if ($show_info || $show_query || $show_stats) {
      // Get information from the preview for display.
      if (!empty($view->build_info['query'])) {
        if ($show_query) {
          $query = $view->build_info['query'];
          // Only the sql default class has a method getArguments.
          $quoted = array();

          if (get_class($view->query) == 'views_plugin_query_default') {
            $quoted = $query->getArguments();
            $connection = Database::getConnection();
            foreach ($quoted as $key => $val) {
              if (is_array($val)) {
                $quoted[$key] = implode(', ', array_map(array($connection, 'quote'), $val));
              }
              else {
                $quoted[$key] = $connection->quote($val);
              }
            }
          }
          $rows['query'][] = array('<strong>' . t('Query') . '</strong>', '<pre>' . check_plain(strtr($query, $quoted)) . '</pre>');
          if (!empty($view->additional_queries)) {
            $queries = '<strong>' . t('These queries were run during view rendering:') . '</strong>';
            foreach ($view->additional_queries as $query) {
              if ($queries) {
                $queries .= "\n";
              }
              $queries .= t('[@time ms]', array('@time' => intval($query[1] * 100000) / 100)) . ' ' . $query[0];
            }

            $rows['query'][] = array('<strong>' . t('Other queries') . '</strong>', '<pre>' . $queries . '</pre>');
          }
        }
        if ($show_info) {
          $rows['query'][] = array('<strong>' . t('Title') . '</strong>', filter_xss_admin($view->get_title()));
          if (isset($path)) {
            $path = l($path, $path);
          }
          else {
            $path = t('This display has no path.');
          }
          $rows['query'][] = array('<strong>' . t('Path') . '</strong>', $path);
        }

        if ($show_stats) {
          $rows['statistics'][] = array('<strong>' . t('Query build time') . '</strong>', t('@time ms', array('@time' => intval($view->build_time * 100000) / 100)));
          $rows['statistics'][] = array('<strong>' . t('Query execute time') . '</strong>', t('@time ms', array('@time' => intval($view->execute_time * 100000) / 100)));
          $rows['statistics'][] = array('<strong>' . t('View render time') . '</strong>', t('@time ms', array('@time' => intval($view->render_time * 100000) / 100)));

        }
        drupal_alter('views_preview_info', $rows, $view);
      }
      else {
        // No query was run. Display that information in place of either the
        // query or the performance statistics, whichever comes first.
        if ($combined || ($show_location === 'above')) {
          $rows['query'] = array(array('<strong>' . t('Query') . '</strong>', t('No query was run')));
        }
        else {
          $rows['statistics'] = array(array('<strong>' . t('Query') . '</strong>', t('No query was run')));
        }
      }
    }
  }
  else {
    foreach ($errors as $error) {
      drupal_set_message($error, 'error');
    }
    $preview = t('Unable to preview due to validation errors.');
  }

  // Assemble the preview, the query info, and the query statistics in the
  // requested order.
  if ($show_location === 'above') {
    if ($combined) {
      $output .= '<div class="views-query-info">' . theme('table', array('rows' => array_merge($rows['query'], $rows['statistics']))) . '</div>';
    }
    else {
      $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['query'])) . '</div>';
    }
  }
  elseif ($show_stats === 'above') {
    $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['statistics'])) . '</div>';
  }

  $output .= $preview;

  if ($show_location === 'below') {
    if ($combined) {
      $output .= '<div class="views-query-info">' . theme('table', array('rows' => array_merge($rows['query'], $rows['statistics']))) . '</div>';
    }
    else {
      $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['query'])) . '</div>';
    }
  }
  elseif ($show_stats === 'below') {
    $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['statistics'])) . '</div>';
  }

  $_GET['q'] = $q;
  return $output;
}