function views_ui_preview

Same name in other branches
  1. 7.x-3.x includes/admin.inc \views_ui_preview()

Page callback for the live preview.

@todo make this use a template

1 string reference to 'views_ui_preview'
views_ui_menu in ./views_ui.module

File

includes/admin.inc, line 378

Code

function views_ui_preview($js, $view) {
    // Take off the items we know so that we can have just the args passed
    // in for later use.
    $func_args = func_get_args();
    array_shift($func_args);
    // $js
    array_shift($func_args);
    // $view
    $display_id = count($func_args) ? array_shift($func_args) : 'default';
    $form_state = array(
        'display_id' => $display_id,
        'view_args' => $func_args ? implode('/', $func_args) : '',
        'rerender' => TRUE,
        'no_redirect' => TRUE,
        'view' => &$view,
        'ajax' => $js,
    );
    $output = drupal_build_form('views_ui_preview_form', $form_state);
    $args = array();
    if (isset($form_state['view_args']) && $form_state['view_args'] !== '') {
        $args = explode('/', $form_state['view_args']);
    }
    $errors = $view->validate();
    if ($errors === TRUE) {
        $view->ajax = $js;
        $view->live_preview = TRUE;
        // Store the current view URL for later use:
        $view->set_display($form_state['display_id']);
        $view->set_arguments($args);
        if ($view->display_handler
            ->get_option('path')) {
            $path = $view->get_url();
        }
        // Make view links come back to preview.
        $ajax = $js ? 'ajax' : 'nojs';
        $view->override_path = "admin/build/views/{$ajax}/preview/" . $view->name . '/' . $form_state['display_id'];
        // also override $_GET['q'] so we get the pager
        $_GET['q'] = $view->override_path;
        if ($form_state['view_args']) {
            $_GET['q'] .= '/' . $form_state['view_args'];
        }
        $preview = $view->preview($form_state['display_id'], $args);
        // Get information from the preview for display.
        if (empty($view->build_info['fail'])) {
            $rows = $view->query
                ->get_preview_info();
            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,
                    )) . ' ' . check_plain($query[0]);
                }
                $rows[] = array(
                    '<strong>' . t('Other queries') . '</strong>',
                    '<pre>' . $queries . '</pre>',
                );
            }
            $rows[] = 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[] = array(
                '<strong>' . t('Path') . '</strong>',
                $path,
            );
            $rows[] = array(
                '<strong>' . t('Query build time') . '</strong>',
                t('@time ms', array(
                    '@time' => intval($view->build_time * 100000) / 100,
                )),
            );
            $rows[] = array(
                '<strong>' . t('Query execute time') . '</strong>',
                t('@time ms', array(
                    '@time' => intval($view->execute_time * 100000) / 100,
                )),
            );
            $rows[] = 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);
            $info = theme('table', array(), $rows);
        }
        else {
            $info = theme('table', array(), 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.');
        $info = '';
    }
    $info = '<div class="views-query-info">' . $info . '</div>';
    if (variable_get('views_ui_query_on_top', FALSE)) {
        $output .= $info . $preview;
    }
    else {
        $output .= $preview . $info;
    }
    if (!$js) {
        views_add_css('views-admin');
        drupal_set_title($view->get_title());
        return $output;
    }
    else {
        views_include('ajax');
        $object = new stdClass();
        if (!empty($view->js_settings)) {
            $object->js = $view->js_settings;
        }
        $object->display = '';
        if ($messages = theme('status_messages')) {
            $object->display = '<div class="views-messages">' . $messages . '</div>';
        }
        $object->display .= $output;
        $object->title = $view->get_title();
        views_ajax_render($object);
    }
}