• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

plugins/export_ui/views_ui.class.php

Go to the documentation of this file.
00001 <?php
00002 
00013 class views_ui extends ctools_export_ui {
00014 
00015   function init($plugin) {
00016     // We modify the plugin info here so that we take the defaults and
00017     // twiddle, rather than completely override them.
00018 
00019     // Reset the edit path to match what we're really using.
00020     $plugin['menu']['items']['edit']['path'] = 'view/%ctools_export_ui/edit';
00021     $plugin['menu']['items']['clone']['path'] = 'view/%ctools_export_ui/clone';
00022     $plugin['menu']['items']['clone']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
00023     $plugin['menu']['items']['export']['path'] = 'view/%ctools_export_ui/export';
00024     $plugin['menu']['items']['export']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
00025     $plugin['menu']['items']['enable']['path'] = 'view/%ctools_export_ui/enable';
00026     $plugin['menu']['items']['disable']['path'] = 'view/%ctools_export_ui/disable';
00027     $plugin['menu']['items']['delete']['path'] = 'view/%ctools_export_ui/delete';
00028     $plugin['menu']['items']['delete']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
00029     $plugin['menu']['items']['revert']['path'] = 'view/%ctools_export_ui/revert';
00030     $plugin['menu']['items']['revert']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
00031 
00032     $prefix_count = count(explode('/', $plugin['menu']['menu prefix']));
00033     $plugin['menu']['items']['add-template'] = array(
00034       'path' => 'template/%/add',
00035       'title' => 'Add from template',
00036       'page callback' => 'ctools_export_ui_switcher_page',
00037       'page arguments' => array($plugin['name'], 'add_template', $prefix_count + 2),
00038       'load arguments' => array($plugin['name']),
00039       'access callback' => 'ctools_export_ui_task_access',
00040       'access arguments' => array($plugin['name'], 'add_template', $prefix_count + 2),
00041       'type' => MENU_CALLBACK,
00042     );
00043 
00044     return parent::init($plugin);
00045   }
00046 
00047   function hook_menu(&$items) {
00048     // We are using our own 'edit' still, rather than having edit on this
00049     // object (maybe in the future) so unset the edit callbacks:
00050 
00051     // Store this so we can put them back as sometimes they're needed
00052     // again laster:
00053     $stored_items = $this->plugin['menu']['items'];
00054     // We leave these to make sure the operations still exist in the plugin so
00055     // that the path finder.
00056     unset($this->plugin['menu']['items']['edit']);
00057     unset($this->plugin['menu']['items']['add']);
00058     unset($this->plugin['menu']['items']['import']);
00059     unset($this->plugin['menu']['items']['edit callback']);
00060 
00061     parent::hook_menu($items);
00062 
00063     $this->plugin['menu']['items'] = $stored_items;
00064   }
00065 
00066   function load_item($item_name) {
00067     return views_ui_cache_load($item_name);
00068   }
00069 
00070   function list_form(&$form, &$form_state) {
00071     $row_class = 'container-inline';
00072     if (!variable_get('views_ui_show_listing_filters', FALSE)) {
00073       $row_class .= " element-invisible";
00074     }
00075 
00076     views_include('admin');
00077 
00078     parent::list_form($form, $form_state);
00079 
00080     // ctools only has two rows. We want four.
00081     // That's why we create our own structure.
00082     $form['bottom row']['submit']['#attributes']['class'][] = 'js-hide';
00083     $form['first row'] = array(
00084       '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-first-row clearfix">',
00085       '#suffix' => '</div>',
00086       'search' => $form['top row']['search'],
00087       'submit' => $form['bottom row']['submit'],
00088       'reset' => $form['bottom row']['reset'],
00089     );
00090     $form['second row'] = array(
00091       '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-second-row clearfix">',
00092       '#suffix' => '</div>',
00093       'storage' => $form['top row']['storage'],
00094       'disabled' => $form['top row']['disabled'],
00095     );
00096     $form['third row'] = array(
00097       '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-third-row clearfix element-hidden">',
00098       '#suffix' => '</div>',
00099       'order' => $form['bottom row']['order'],
00100       'sort' => $form['bottom row']['sort'],
00101     );
00102     unset($form['top row']);
00103     unset($form['bottom row']);
00104 
00105     // Modify the look and contents of existing form elements.
00106     $form['second row']['storage']['#title'] = '';
00107     $form['second row']['storage']['#options'] = array(
00108       'all' => t('All storage'),
00109       t('Normal') => t('In database'),
00110       t('Default') => t('In code'),
00111       t('Overridden') => t('Database overriding code'),
00112     );
00113     $form['second row']['disabled']['#title'] = '';
00114     $form['second row']['disabled']['#options']['all'] = t('All status');
00115     $form['third row']['sort']['#title'] = '';
00116 
00117     // And finally, add our own.
00118     $this->bases = array();
00119     foreach (views_fetch_base_tables() as $table => $info) {
00120       $this->bases[$table] = $info['title'];
00121     }
00122 
00123     $form['second row']['base'] = array(
00124       '#type' => 'select',
00125       '#options' => array_merge(array('all' => t('All types')), $this->bases),
00126       '#default_value' => 'all',
00127       '#weight' => -1,
00128     );
00129 
00130     $tags = array();
00131     if (isset($form_state['object']->items)) {
00132       foreach ($form_state['object']->items as $name => $view) {
00133         if (!empty($view->tag)) {
00134           $view_tags = drupal_explode_tags($view->tag);
00135           foreach ($view_tags as $tag) {
00136             $tags[$tag] = $tag;
00137           }
00138         }
00139       }
00140     }
00141     asort($tags);
00142 
00143     $form['second row']['tag'] = array(
00144       '#type' => 'select',
00145       '#title' => t('Filter'),
00146       '#options' => array_merge(array('all' => t('All tags')), array('none' => t('No tags')), $tags),
00147       '#default_value' => 'all',
00148       '#weight' => -9,
00149     );
00150 
00151     $displays = array();
00152     foreach (views_fetch_plugin_data('display') as $id => $info) {
00153       if (!empty($info['admin'])) {
00154         $displays[$id] = $info['admin'];
00155       }
00156     }
00157     asort($displays);
00158 
00159     $form['second row']['display'] = array(
00160       '#type' => 'select',
00161       '#options' => array_merge(array('all' => t('All displays')), $displays),
00162       '#default_value' => 'all',
00163       '#weight' => -1,
00164     );
00165   }
00166 
00167   function list_filter($form_state, $view) {
00168     // Don't filter by tags if all is set up.
00169     if ($form_state['values']['tag'] != 'all') {
00170       // If none is selected check whether the view has a tag.
00171       if ($form_state['values']['tag'] == 'none') {
00172         return !empty($view->tag);
00173       }
00174       else {
00175         // Check whether the tag can be found in the views tag.
00176         return strpos($view->tag, $form_state['values']['tag']) === FALSE;
00177       }
00178     }
00179     if ($form_state['values']['base'] != 'all' && $form_state['values']['base'] != $view->base_table) {
00180       return TRUE;
00181     }
00182 
00183     return parent::list_filter($form_state, $view);
00184   }
00185 
00186   function list_sort_options() {
00187     return array(
00188       'disabled' => t('Enabled, name'),
00189       'name' => t('Name'),
00190       'path' => t('Path'),
00191       'tag' => t('Tag'),
00192       'storage' => t('Storage'),
00193     );
00194   }
00195 
00196 
00197   function list_build_row($view, &$form_state, $operations) {
00198     if (!empty($view->human_name)) {
00199       $title = $view->human_name;
00200     }
00201     else {
00202       $title = $view->get_title();
00203       if (empty($title)) {
00204         $title = $view->name;
00205       }
00206     }
00207 
00208     $paths = _views_ui_get_paths($view);
00209     $paths = implode(", ", $paths);
00210 
00211     $base = !empty($this->bases[$view->base_table]) ? $this->bases[$view->base_table] : t('Broken');
00212 
00213     $info = theme('views_ui_view_info', array('view' => $view, 'base' => $base));
00214 
00215     // Reorder the operations so that enable is the default action for a templatic views
00216     if (!empty($operations['enable'])) {
00217       $operations = array('enable' => $operations['enable']) + $operations;
00218     }
00219 
00220     // Set up sorting
00221     switch ($form_state['values']['order']) {
00222       case 'disabled':
00223         $this->sorts[$view->name] = strtolower(empty($view->disabled) . $title);
00224         break;
00225       case 'name':
00226         $this->sorts[$view->name] = strtolower($title);
00227         break;
00228       case 'path':
00229         $this->sorts[$view->name] = strtolower($paths);
00230         break;
00231       case 'tag':
00232         $this->sorts[$view->name] = strtolower($view->tag);
00233         break;
00234       case 'storage':
00235         $this->sorts[$view->name] = strtolower($view->type . $title);
00236         break;
00237     }
00238 
00239     $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
00240 
00241     $this->rows[$view->name] = array(
00242       'data' => array(
00243         array('data' => $info, 'class' => array('views-ui-name')),
00244         array('data' => check_plain($view->description), 'class' => array('views-ui-description')),
00245         array('data' => check_plain($view->tag), 'class' => array('views-ui-tag')),
00246         array('data' => $paths, 'class' => array('views-ui-path')),
00247         array('data' => $ops, 'class' => array('views-ui-operations')),
00248       ),
00249       'title' => t('Machine name: ') . check_plain($view->name),
00250       'class' => array(!empty($view->disabled) ? 'ctools-export-ui-disabled' : 'ctools-export-ui-enabled'),
00251     );
00252   }
00253 
00254   function list_render(&$form_state) {
00255     views_include('admin');
00256     views_ui_add_admin_css();
00257     if (empty($_REQUEST['js'])) {
00258       views_ui_check_advanced_help();
00259     }
00260     drupal_add_library('system', 'jquery.bbq');
00261     views_add_js('views-list');
00262 
00263     $this->active = $form_state['values']['order'];
00264     $this->order = $form_state['values']['sort'];
00265 
00266     $query    = tablesort_get_query_parameters();
00267 
00268     $header = array(
00269       $this->tablesort_link(t('View name'), 'name', 'views-ui-name'),
00270       array('data' => t('Description'), 'class' => array('views-ui-description')),
00271       $this->tablesort_link(t('Tag'), 'tag', 'views-ui-tag'),
00272       $this->tablesort_link(t('Path'), 'path', 'views-ui-path'),
00273       array('data' => t('Operations'), 'class' => array('views-ui-operations')),
00274     );
00275 
00276     $table = array(
00277       'header' => $header,
00278       'rows' => $this->rows,
00279       'empty' => t('No views match the search criteria.'),
00280       'attributes' => array('id' => 'ctools-export-ui-list-items'),
00281     );
00282     return theme('table', $table);
00283   }
00284 
00285   function tablesort_link($label, $field, $class) {
00286     $title = t('sort by @s', array('@s' => $label));
00287     $initial = 'asc';
00288 
00289     if ($this->active == $field) {
00290       $initial = ($this->order == 'asc') ? 'desc' : 'asc';
00291       $label .= theme('tablesort_indicator', array('style' => $initial));
00292     }
00293 
00294     $query['order'] = $field;
00295     $query['sort'] = $initial;
00296     $link_options = array(
00297       'html' => TRUE,
00298       'attributes' => array('title' => $title),
00299       'query' => $query,
00300     );
00301     $link = l($label, $_GET['q'], $link_options);
00302     if ($this->active == $field) {
00303       $class .= ' active';
00304     }
00305 
00306     return array('data' => $link, 'class' => $class);
00307   }
00308 
00309   function clone_page($js, $input, $item, $step = NULL) {
00310     drupal_set_title($this->get_page_title('clone', $item));
00311 
00312     $name = $item->{$this->plugin['export']['key']};
00313 
00314     $form_state = array(
00315       'plugin' => $this->plugin,
00316       'object' => &$this,
00317       'ajax' => $js,
00318       'item' => $item,
00319       'op' => 'add',
00320       'form type' => 'clone',
00321       'original name' => $name,
00322       'rerender' => TRUE,
00323       'no_redirect' => TRUE,
00324       'step' => $step,
00325       // Store these in case additional args are needed.
00326       'function args' => func_get_args(),
00327     );
00328 
00329     $output = drupal_build_form('views_ui_clone_form', $form_state);
00330     if (!empty($form_state['executed'])) {
00331       $item->name = $form_state['values']['name'];
00332       $item->human_name = $form_state['values']['human_name'];
00333       $item->vid = NULL;
00334       views_ui_cache_set($item);
00335 
00336       drupal_goto(ctools_export_ui_plugin_menu_path($this->plugin, 'edit', $item->name));
00337     }
00338 
00339     return $output;
00340   }
00341 
00342   function add_template_page($js, $input, $name, $step = NULL) {
00343     $templates = views_get_all_templates();
00344 
00345     if (empty($templates[$name])) {
00346       return MENU_NOT_FOUND;
00347     }
00348 
00349     $template = $templates[$name];
00350 
00351     // The template description probably describes the template, not the
00352     // view that will be created from it, but users aren't that likely to
00353     // touch it.
00354     if (!empty($template->description)) {
00355       unset($template->description);
00356     }
00357 
00358     $template->is_template = TRUE;
00359     $template->type = t('Default');
00360 
00361     $output = $this->clone_page($js, $input, $template, $step);
00362     drupal_set_title(t('Create view from template @template', array('@template' => $template->get_human_name())));
00363     return $output;
00364   }
00365 
00366   function set_item_state($state, $js, $input, $item) {
00367     ctools_export_set_object_status($item, $state);
00368     menu_rebuild();
00369 
00370     if (!$js) {
00371       drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
00372     }
00373     else {
00374       return $this->list_page($js, $input);
00375     }
00376   }
00377 
00378   function list_page($js, $input) {
00379     // wrap output in a div for CSS
00380     $output = parent::list_page($js, $input);
00381     if (is_string($output)) {
00382       $output = '<div id="views-ui-list-page">' . $output . '</div>';
00383       return $output;
00384     }
00385   }
00386 }
00387 
00393 function views_ui_clone_form($form, &$form_state) {
00394   $counter = 1;
00395 
00396   if (!isset($form_state['item'])) {
00397     $view = views_get_view($form_state['original name']);
00398   }
00399   else {
00400     $view = $form_state['item'];
00401   }
00402   do {
00403     if (empty($form_state['item']->is_template)) {
00404       $name = format_plural($counter, 'Clone of', 'Clone @count of') . ' ' . $view->get_human_name();
00405     }
00406     else {
00407       $name = $view->get_human_name();
00408       if ($counter > 1) {
00409         $name .= ' ' . $counter;
00410       }
00411     }
00412     $counter++;
00413     $machine_name = preg_replace('/[^a-z0-9_]+/', '_', drupal_strtolower($name));
00414   } while (ctools_export_crud_load($form_state['plugin']['schema'], $machine_name));
00415 
00416   $form['human_name'] = array(
00417     '#type' => 'textfield',
00418     '#title' => t('View name'),
00419     '#default_value' => $name,
00420     '#size' => 32,
00421     '#maxlength' => 255,
00422   );
00423 
00424   $form['name'] = array(
00425     '#title' => t('View name'),
00426     '#type' => 'machine_name',
00427     '#required' => TRUE,
00428     '#maxlength' => 128,
00429     '#size' => 128,
00430     '#machine_name' => array(
00431       'exists' => 'ctools_export_ui_edit_name_exists',
00432       'source' => array('human_name'),
00433     ),
00434   );
00435 
00436   $form['submit'] = array(
00437     '#type' => 'submit',
00438     '#value' => t('Continue'),
00439   );
00440 
00441   return $form;
00442 }

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1