commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / views / plugins / export_ui / views_ui.class.php
1 <?php
2
3 /**
4 * @file
5 * Contains the CTools Export UI integration code.
6 *
7 * Note that this is only a partial integration.
8 */
9
10 /**
11 * CTools Export UI class handler for Views UI.
12 */
13 class views_ui extends ctools_export_ui {
14
15 function init($plugin) {
16 // We modify the plugin info here so that we take the defaults and
17 // twiddle, rather than completely override them.
18
19 // Reset the edit path to match what we're really using.
20 $plugin['menu']['items']['edit']['path'] = 'view/%ctools_export_ui/edit';
21 $plugin['menu']['items']['clone']['path'] = 'view/%ctools_export_ui/clone';
22 $plugin['menu']['items']['clone']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
23 $plugin['menu']['items']['export']['path'] = 'view/%ctools_export_ui/export';
24 $plugin['menu']['items']['export']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
25 $plugin['menu']['items']['enable']['path'] = 'view/%ctools_export_ui/enable';
26 $plugin['menu']['items']['disable']['path'] = 'view/%ctools_export_ui/disable';
27 $plugin['menu']['items']['delete']['path'] = 'view/%ctools_export_ui/delete';
28 $plugin['menu']['items']['delete']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
29 $plugin['menu']['items']['revert']['path'] = 'view/%ctools_export_ui/revert';
30 $plugin['menu']['items']['revert']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
31
32 $prefix_count = count(explode('/', $plugin['menu']['menu prefix']));
33 $plugin['menu']['items']['add-template'] = array(
34 'path' => 'template/%/add',
35 'title' => 'Add from template',
36 'page callback' => 'ctools_export_ui_switcher_page',
37 'page arguments' => array($plugin['name'], 'add_template', $prefix_count + 2),
38 'load arguments' => array($plugin['name']),
39 'access callback' => 'ctools_export_ui_task_access',
40 'access arguments' => array($plugin['name'], 'add_template', $prefix_count + 2),
41 'type' => MENU_CALLBACK,
42 );
43
44 return parent::init($plugin);
45 }
46
47 function hook_menu(&$items) {
48 // We are using our own 'edit' still, rather than having edit on this
49 // object (maybe in the future) so unset the edit callbacks:
50
51 // Store this so we can put them back as sometimes they're needed
52 // again laster:
53 $stored_items = $this->plugin['menu']['items'];
54 // We leave these to make sure the operations still exist in the plugin so
55 // that the path finder.
56 unset($this->plugin['menu']['items']['edit']);
57 unset($this->plugin['menu']['items']['add']);
58 unset($this->plugin['menu']['items']['import']);
59 unset($this->plugin['menu']['items']['edit callback']);
60
61 parent::hook_menu($items);
62
63 $this->plugin['menu']['items'] = $stored_items;
64 }
65
66 function load_item($item_name) {
67 return views_ui_cache_load($item_name);
68 }
69
70 function list_form(&$form, &$form_state) {
71 $row_class = 'container-inline';
72 if (!variable_get('views_ui_show_listing_filters', FALSE)) {
73 $row_class .= " element-invisible";
74 }
75
76 views_include('admin');
77
78 parent::list_form($form, $form_state);
79
80 // ctools only has two rows. We want four.
81 // That's why we create our own structure.
82 $form['bottom row']['submit']['#attributes']['class'][] = 'js-hide';
83 $form['first row'] = array(
84 '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-first-row clearfix">',
85 '#suffix' => '</div>',
86 'search' => $form['top row']['search'],
87 'submit' => $form['bottom row']['submit'],
88 'reset' => $form['bottom row']['reset'],
89 );
90 $form['second row'] = array(
91 '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-second-row clearfix">',
92 '#suffix' => '</div>',
93 'storage' => $form['top row']['storage'],
94 'disabled' => $form['top row']['disabled'],
95 );
96 $form['third row'] = array(
97 '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-third-row clearfix element-hidden">',
98 '#suffix' => '</div>',
99 'order' => $form['bottom row']['order'],
100 'sort' => $form['bottom row']['sort'],
101 );
102 unset($form['top row']);
103 unset($form['bottom row']);
104
105 // Modify the look and contents of existing form elements.
106 $form['second row']['storage']['#title'] = '';
107 $form['second row']['storage']['#options'] = array(
108 'all' => t('All storage'),
109 t('Normal') => t('In database'),
110 t('Default') => t('In code'),
111 t('Overridden') => t('Database overriding code'),
112 );
113 $form['second row']['disabled']['#title'] = '';
114 $form['second row']['disabled']['#options']['all'] = t('All status');
115 $form['third row']['sort']['#title'] = '';
116
117 // And finally, add our own.
118 $this->bases = array();
119 foreach (views_fetch_base_tables() as $table => $info) {
120 $this->bases[$table] = $info['title'];
121 }
122
123 $form['second row']['base'] = array(
124 '#type' => 'select',
125 '#options' => array_merge(array('all' => t('All types')), $this->bases),
126 '#default_value' => 'all',
127 '#weight' => -1,
128 );
129
130 $tags = array();
131 if (isset($form_state['object']->items)) {
132 foreach ($form_state['object']->items as $name => $view) {
133 if (!empty($view->tag)) {
134 $view_tags = drupal_explode_tags($view->tag);
135 foreach ($view_tags as $tag) {
136 $tags[$tag] = $tag;
137 }
138 }
139 }
140 }
141 asort($tags);
142
143 $form['second row']['tag'] = array(
144 '#type' => 'select',
145 '#title' => t('Filter'),
146 '#options' => array_merge(array('all' => t('All tags')), array('none' => t('No tags')), $tags),
147 '#default_value' => 'all',
148 '#weight' => -9,
149 );
150
151 $displays = array();
152 foreach (views_fetch_plugin_data('display') as $id => $info) {
153 if (!empty($info['admin'])) {
154 $displays[$id] = $info['admin'];
155 }
156 }
157 asort($displays);
158
159 $form['second row']['display'] = array(
160 '#type' => 'select',
161 '#options' => array_merge(array('all' => t('All displays')), $displays),
162 '#default_value' => 'all',
163 '#weight' => -1,
164 );
165 }
166
167 function list_filter($form_state, $view) {
168 // Don't filter by tags if all is set up.
169 if ($form_state['values']['tag'] != 'all') {
170 // If none is selected check whether the view has a tag.
171 if ($form_state['values']['tag'] == 'none') {
172 return !empty($view->tag);
173 }
174 else {
175 // Check whether the tag can be found in the views tag.
176 return strpos($view->tag, $form_state['values']['tag']) === FALSE;
177 }
178 }
179 if ($form_state['values']['base'] != 'all' && $form_state['values']['base'] != $view->base_table) {
180 return TRUE;
181 }
182
183 return parent::list_filter($form_state, $view);
184 }
185
186 function list_sort_options() {
187 return array(
188 'disabled' => t('Enabled, name'),
189 'name' => t('Name'),
190 'path' => t('Path'),
191 'tag' => t('Tag'),
192 'storage' => t('Storage'),
193 );
194 }
195
196
197 function list_build_row($view, &$form_state, $operations) {
198 if (!empty($view->human_name)) {
199 $title = $view->human_name;
200 }
201 else {
202 $title = $view->get_title();
203 if (empty($title)) {
204 $title = $view->name;
205 }
206 }
207
208 $paths = _views_ui_get_paths($view);
209 $paths = implode(", ", $paths);
210
211 $base = !empty($this->bases[$view->base_table]) ? $this->bases[$view->base_table] : t('Broken');
212
213 $info = theme('views_ui_view_info', array('view' => $view, 'base' => $base));
214
215 // Reorder the operations so that enable is the default action for a templatic views
216 if (!empty($operations['enable'])) {
217 $operations = array('enable' => $operations['enable']) + $operations;
218 }
219
220 // Set up sorting
221 switch ($form_state['values']['order']) {
222 case 'disabled':
223 $this->sorts[$view->name] = strtolower(empty($view->disabled) . $title);
224 break;
225 case 'name':
226 $this->sorts[$view->name] = strtolower($title);
227 break;
228 case 'path':
229 $this->sorts[$view->name] = strtolower($paths);
230 break;
231 case 'tag':
232 $this->sorts[$view->name] = strtolower($view->tag);
233 break;
234 case 'storage':
235 $this->sorts[$view->name] = strtolower($view->type . $title);
236 break;
237 }
238
239 $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
240
241 $this->rows[$view->name] = array(
242 'data' => array(
243 array('data' => $info, 'class' => array('views-ui-name')),
244 array('data' => check_plain($view->description), 'class' => array('views-ui-description')),
245 array('data' => check_plain($view->tag), 'class' => array('views-ui-tag')),
246 array('data' => $paths, 'class' => array('views-ui-path')),
247 array('data' => $ops, 'class' => array('views-ui-operations')),
248 ),
249 'title' => t('Machine name: ') . check_plain($view->name),
250 'class' => array(!empty($view->disabled) ? 'ctools-export-ui-disabled' : 'ctools-export-ui-enabled'),
251 );
252 }
253
254 function list_render(&$form_state) {
255 views_include('admin');
256 views_ui_add_admin_css();
257 if (empty($_REQUEST['js'])) {
258 views_ui_check_advanced_help();
259 }
260 drupal_add_library('system', 'jquery.bbq');
261 views_add_js('views-list');
262
263 $this->active = $form_state['values']['order'];
264 $this->order = $form_state['values']['sort'];
265
266 $query = tablesort_get_query_parameters();
267
268 $header = array(
269 $this->tablesort_link(t('View name'), 'name', 'views-ui-name'),
270 array('data' => t('Description'), 'class' => array('views-ui-description')),
271 $this->tablesort_link(t('Tag'), 'tag', 'views-ui-tag'),
272 $this->tablesort_link(t('Path'), 'path', 'views-ui-path'),
273 array('data' => t('Operations'), 'class' => array('views-ui-operations')),
274 );
275
276 $table = array(
277 'header' => $header,
278 'rows' => $this->rows,
279 'empty' => t('No views match the search criteria.'),
280 'attributes' => array('id' => 'ctools-export-ui-list-items'),
281 );
282 return theme('table', $table);
283 }
284
285 function tablesort_link($label, $field, $class) {
286 $title = t('sort by @s', array('@s' => $label));
287 $initial = 'asc';
288
289 if ($this->active == $field) {
290 $initial = ($this->order == 'asc') ? 'desc' : 'asc';
291 $label .= theme('tablesort_indicator', array('style' => $initial));
292 }
293
294 $query['order'] = $field;
295 $query['sort'] = $initial;
296 $link_options = array(
297 'html' => TRUE,
298 'attributes' => array('title' => $title),
299 'query' => $query,
300 );
301 $link = l($label, $_GET['q'], $link_options);
302 if ($this->active == $field) {
303 $class .= ' active';
304 }
305
306 return array('data' => $link, 'class' => $class);
307 }
308
309 function clone_page($js, $input, $item, $step = NULL) {
310 drupal_set_title($this->get_page_title('clone', $item));
311
312 $name = $item->{$this->plugin['export']['key']};
313
314 $form_state = array(
315 'plugin' => $this->plugin,
316 'object' => &$this,
317 'ajax' => $js,
318 'item' => $item,
319 'op' => 'add',
320 'form type' => 'clone',
321 'original name' => $name,
322 'rerender' => TRUE,
323 'no_redirect' => TRUE,
324 'step' => $step,
325 // Store these in case additional args are needed.
326 'function args' => func_get_args(),
327 );
328
329 $output = drupal_build_form('views_ui_clone_form', $form_state);
330 if (!empty($form_state['executed'])) {
331 $item->name = $form_state['values']['name'];
332 $item->human_name = $form_state['values']['human_name'];
333 $item->vid = NULL;
334 views_ui_cache_set($item);
335
336 drupal_goto(ctools_export_ui_plugin_menu_path($this->plugin, 'edit', $item->name));
337 }
338
339 return $output;
340 }
341
342 function add_template_page($js, $input, $name, $step = NULL) {
343 $templates = views_get_all_templates();
344
345 if (empty($templates[$name])) {
346 return MENU_NOT_FOUND;
347 }
348
349 $template = $templates[$name];
350
351 // The template description probably describes the template, not the
352 // view that will be created from it, but users aren't that likely to
353 // touch it.
354 if (!empty($template->description)) {
355 unset($template->description);
356 }
357
358 $template->is_template = TRUE;
359 $template->type = t('Default');
360
361 $output = $this->clone_page($js, $input, $template, $step);
362 drupal_set_title(t('Create view from template @template', array('@template' => $template->get_human_name())));
363 return $output;
364 }
365
366 function set_item_state($state, $js, $input, $item) {
367 ctools_export_set_object_status($item, $state);
368 menu_rebuild();
369
370 if (!$js) {
371 drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
372 }
373 else {
374 return $this->list_page($js, $input);
375 }
376 }
377
378 function list_page($js, $input) {
379 // Remove filters values from session if filters are hidden.
380 if (!variable_get('views_ui_show_listing_filters', FALSE) && isset($_SESSION['ctools_export_ui'][$this->plugin['name']])) {
381 unset($_SESSION['ctools_export_ui'][$this->plugin['name']]);
382 }
383
384 // wrap output in a div for CSS
385 $output = parent::list_page($js, $input);
386 if (is_string($output)) {
387 $output = '<div id="views-ui-list-page">' . $output . '</div>';
388 }
389 return $output;
390 }
391 }
392
393 /**
394 * Form callback to edit an exportable item using the wizard
395 *
396 * This simply loads the object defined in the plugin and hands it off.
397 */
398 function views_ui_clone_form($form, &$form_state) {
399 $counter = 1;
400
401 if (!isset($form_state['item'])) {
402 $view = views_get_view($form_state['original name']);
403 }
404 else {
405 $view = $form_state['item'];
406 }
407 do {
408 if (empty($form_state['item']->is_template)) {
409 $name = format_plural($counter, 'Clone of', 'Clone @count of') . ' ' . $view->get_human_name();
410 }
411 else {
412 $name = $view->get_human_name();
413 if ($counter > 1) {
414 $name .= ' ' . $counter;
415 }
416 }
417 $counter++;
418 $machine_name = preg_replace('/[^a-z0-9_]+/', '_', drupal_strtolower($name));
419 } while (ctools_export_crud_load($form_state['plugin']['schema'], $machine_name));
420
421 $form['human_name'] = array(
422 '#type' => 'textfield',
423 '#title' => t('View name'),
424 '#default_value' => $name,
425 '#size' => 32,
426 '#maxlength' => 255,
427 );
428
429 $form['name'] = array(
430 '#title' => t('View name'),
431 '#type' => 'machine_name',
432 '#required' => TRUE,
433 '#maxlength' => 128,
434 '#size' => 128,
435 '#machine_name' => array(
436 'exists' => 'ctools_export_ui_edit_name_exists',
437 'source' => array('human_name'),
438 ),
439 );
440
441 $form['submit'] = array(
442 '#type' => 'submit',
443 '#value' => t('Continue'),
444 );
445
446 return $form;
447 }