Clone view button
[project/views.git] / views_ui.module
1 <?php
2 // $Id$
3
4 // ---------------------------------------------------------------------------
5 // Drupal Hooks
6
7 /*
8 * Implementation of hook_help()
9 */
10 function views_ui_help($section) {
11 switch ($section) {
12 case 'admin/help#views_ui':
13 return _views_ui_help_add();
14 case 'admin/modules#description':
15 return t('The views module creates customized views of node lists.');
16 case 'admin/views/import':
17 return t('You may import a view by cut-and-pasting the results of an export view. If the import is successful you will be taken to the Add View page with all of the settings of the imported view..');
18 case 'admin/views':
19 return t('This screen shows all of the views that are currently defined in your system. The default views are provided by Views and other modules and are automatically available. If a customized view of the same name exists, it will be used in place of a default view.');
20 }
21 if (!strncmp($section, 'admin/views', 11)) {
22 switch (arg(2)) {
23 case 'add':
24 case 'edit':
25 return t('Please see %s or the views documentation on drupal.org for help here.', array('%s' => l(t('the views help page'), 'admin/help/views_ui')));
26 case 'export':
27 return t('You may cut & paste this view into an import function on another system. The view will only work if all modules required by the view are installed on the target location.');
28 }
29 }
30 }
31
32 /*
33 * Because the add/edit page is kind of complicated.
34 */
35 function _views_ui_help_add() {
36 $output = t('<p>A view retrieves some number of nodes from the database and displays them in a variety of formats.</p>');
37 $output .= t("<h3>View Types</h3>
38 <dl>
39 <dt><em>List View</em></dt><dd>A List View provides the data for each node retrieved in the form of an unordered list. Each item in the Fields section will be displayed; the Title will be displayed as a label. The order the items appear in the Fields list is the order the items will appear in the output. Leaving the title blank will cause the field to appear with no label (which is desirable in lists that just display titles, for example).</dd>
40 <dt><em>Table View</em></dt><dd>A Table View provides the data for each node as one row of a table. The Fields selected in the Fields list will be displayed in the order they are listed. The title column will be shown in the header. If you set the field to 'sortable' then the header will be click-sortable; be careful here as click-sorts will be processed after built-in sort criteria, and built-in sort criteria can easily make click-sorts invalid. If using click-sorts, choose a field to be the default sort; otherwise the first field presented will be the default sort.</dd>
41 <dt><em>Teaser List</em></dt><dd>A Teaser List will simply present the teaser of each node retrieved.</dd>
42 <dt><em>Full Nodes</em></dt><dd>This will show the full content of each node retrieved.</dd>
43 <dt><em>Random Teaser</em></dt><dd>This will show a single random teaser.</dd>
44 <dt><em>Random Node</em></dt><dd>This will show a single random node's full view.</dd>
45 </dl>");
46
47 $output .= t("<h3>Fields</h3>\n");
48 $output .= t("<p>When using List or Table view, it is necessary to choose which fields will be displayed to the user.</p><dl>\n");
49 $fields = _views_get_fields();
50 foreach ($fields as $field) {
51 $output .= "<dt><em>$field[name]</em></dt><dd>$field[help]</dd>\n";
52 }
53 $output .= "</dl>\n";
54
55 $output .= t("<h3>Arguments</h3>\n");
56 $output .= t("<p>Arguments can be passed to the View through the URL, in order to create views that are configurable by the user. This is very useful to create views for taxonomy, or to sort by user. When using arguments, substitution is performed on the title. %1 will represent argument 1, %2 will represent argument 2. Each argument has a title field; this title is used if providing a summary view (which can matter because the argument is missing which could result in confusing phrases such as 'view for')</p><dl>\n");
57 $arguments = _views_get_arguments();
58 foreach ($arguments as $argument) {
59 $output .= "<dt><em>$argument[name]</em></dt><dd>$argument[help]</dd>\n";
60 }
61 $output .= "</dl>\n";
62
63 $output .= t("<h3>Filters</h3>\n");
64 $output .= t("<p>Views may be filtered to restrict the view on a number of criteria.</p><dl>\n");
65 $filters = _views_get_filters();
66 foreach ($filters as $filter) {
67 $output .= "<dt><em>$filter[name]</em></dt><dd>$filter[help]</dd>\n";
68 }
69 $output .= "</dl>\n";
70
71 $output .= t("<h3>Sorting Critera</h3>\n");
72 $output .= t("<p>The result set may be sorted on any of the following criteria.</p><dl>\n");
73 $sorts = _views_get_sorts();
74 foreach ($sorts as $sort) {
75 $output .= "<dt><em>$sort[name]</em></dt><dd>$sort[help]</dd>\n";
76 }
77 $output .= "</dl>\n";
78
79 return $output;
80 }
81
82 /*
83 * Implementation of hook_perm()
84 */
85 function views_ui_perm() {
86 return array('administer views');
87 }
88
89 /*
90 * Implementation of hook_menu()
91 */
92 function views_ui_menu($may_cache) {
93 $items = array();
94
95 if ($may_cache) {
96 $items[] = array('path' => 'admin/views',
97 'title' => t('views'),
98 'callback' => 'views_ui_admin_page',
99 'access' => user_access('administer views'),
100 'type' => MENU_NORMAL_ITEM);
101 $items[] = array('path' => 'admin/views/list',
102 'title' => t('list'),
103 'callback' => 'views_ui_admin_page',
104 'access' => user_access('administer views'),
105 'type' => MENU_DEFAULT_LOCAL_TASK,
106 'weight' => '-1');
107 $items[] = array('path' => 'admin/views/add',
108 'title' => t('add'),
109 'callback' => 'views_ui_admin_add_page',
110 'access' => user_access('administer views'),
111 'type' => MENU_LOCAL_TASK);
112 $items[] = array('path' => 'admin/views/clone',
113 'title' => t('clone'),
114 'callback' => 'views_ui_admin_clone_page',
115 'access' => user_access('administer views'),
116 'type' => MENU_CALLBACK);
117 $items[] = array('path' => 'admin/views/edit',
118 'title' => t('edit view'),
119 'callback' => 'views_ui_admin_edit_page',
120 'access' => user_access('administer views'),
121 'type' => MENU_CALLBACK);
122 $items[] = array('path' => 'admin/views/import',
123 'title' => t('import'),
124 'callback' => 'views_ui_admin_import_page',
125 'access' => user_access('administer views'),
126 'type' => MENU_LOCAL_TASK);
127 $items[] = array('path' => 'admin/views/export',
128 'title' => t('export view'),
129 'callback' => 'views_ui_admin_export_page',
130 'access' => user_access('administer views'),
131 'type' => MENU_CALLBACK);
132 $items[] = array('path' => 'admin/views/delete',
133 'title' => t('edit view'),
134 'callback' => 'views_ui_admin_delete_page',
135 'access' => user_access('administer views'),
136 'type' => MENU_CALLBACK);
137 $items[] = array('path' => 'admin/views/enable',
138 'callback' => 'views_ui_admin_enable_page',
139 'access' => user_access('administer views'),
140 'type' => MENU_CALLBACK);
141 $items[] = array('path' => 'admin/views/disable',
142 'callback' => 'views_ui_admin_disable_page',
143 'access' => user_access('administer views'),
144 'type' => MENU_CALLBACK);
145
146 }
147 return $items;
148 }
149
150 /**
151 * hunmonk's module dependency check: see http://drupal.org/node/54463
152 */
153 function views_ui_form_alter($form_id, &$form) {
154 if ($form_id == 'system_modules' && !$_POST) {
155 views_ui_system_module_validate($form);
156 }
157 }
158
159 /**
160 * hunmonk's module dependency check: see http://drupal.org/node/54463
161 */
162 function views_ui_system_module_validate(&$form) {
163 $module = 'views_ui';
164 $dependencies = array('views');
165 foreach ($dependencies as $dependency) {
166 if (!in_array($dependency, $form['status']['#default_value'])) {
167 $missing_dependency = TRUE;
168 $missing_dependency_list[] = $dependency;
169 }
170 }
171 if (in_array($module, $form['status']['#default_value']) && isset($missing_dependency)) {
172 db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module);
173 $key = array_search($module, $form['status']['#default_value']);
174 unset($form['status']['#default_value'][$key]);
175 drupal_set_message(t('The module %module was deactivated--it requires the following disabled/non-existant modules to function properly: %dependencies', array('%module' => $module, '%dependencies' => implode(', ', $missing_dependency_list))), 'error');
176 }
177 }
178
179 // ---------------------------------------------------------------------------
180 // Administrative Pages
181
182 /*
183 * This page lists all system views and provides links to edit them.
184 */
185 function views_ui_admin_page() {
186 $numViews = 25;
187
188 drupal_set_title(t('administer views'));
189
190 $result = pager_query("SELECT vid, name, description, menu_title, page_title, block_title, url, page, menu, block FROM {view_view} ORDER BY name", $numViews);
191
192 while ($view = db_fetch_object($result)) {
193 $url = ($view->page ? l($view->url, $view->url) : t('No Page View'));
194 $provides = array();
195 if ($view->page) {
196 $provides[] = 'Page';
197 }
198 if ($view->block) {
199 $provides[] = 'Block';
200 }
201 if ($view->menu) {
202 $provides[] = 'Menu';
203 }
204 $items[] = array(
205 $view->name,
206 views_get_title($view, 'menu'),
207 $view->description,
208 implode(', ', $provides),
209 $url,
210 theme('links', array(
211 l(t('edit'), "admin/views/edit/$view->vid"),
212 l(t('export'), "admin/views/export/$view->vid"),
213 l(t('delete'), "admin/views/delete/$view->vid"),
214 l(t('clone'), "admin/views/clone/$view->vid"),
215 ))
216 );
217 }
218
219 if ($items) {
220 $output = theme('table', array(t('View'), t('Title'), t('Description'), t('Provides'), t('URL'), t('Actions')), $items, array("cellpadding" => "4"), t('Existing Views'));
221 $output .= theme('pager', NULL, $numViews);
222 }
223 else {
224 $output .= t('<p>No views have currently been defined.</p>');
225 }
226
227 $result = db_query("SELECT name FROM {view_view}");
228 while ($view = db_fetch_object($result)) {
229 $used[$view->name] = true;
230 }
231
232 $output .= t('<p>Below are system default views; if you edit one of these, a view will be created that will override any system use of the view.</p>');
233 $items = array();
234 $default_views = _views_get_default_views();
235
236 $views_status = variable_get('views_defaults', array());
237
238 foreach ($default_views as $view) {
239 $url = ($view->page ? l($view->url, $view->url) : t('No Page View'));
240
241 if ($used[$view->name]) {
242 $status = t('Overridden');
243 }
244 else if (isset($views_status[$view->name])) {
245 if ($views_status[$view->name] == 'enabled') {
246 $status = t('Enabled');
247 }
248 else {
249 $status = t('Disabled');
250 }
251 }
252 else if ($view->disabled) {
253 $status = t('Disabled');
254 }
255 else {
256 $status = t('Enabled');
257 }
258
259 $provides = array();
260 if ($view->page) {
261 $provides[] = t('Page');
262 }
263 if ($view->block) {
264 $provides[] = t('Block');
265 }
266 if ($view->menu) {
267 $provides[] = t('Menu');
268 }
269
270 $links = array(l(t('add'), "admin/views/add/$view->name"));
271 if ($status == 'Enabled') {
272 $links[] = l(t('disable'), "admin/views/disable/$view->name");
273 }
274 else if ($status == 'Disabled') {
275 $links[] = l(t('enable'), "admin/views/enable/$view->name");
276 }
277
278 $items[] = array($view->name, views_get_title($view, 'menu'), $view->description, implode(', ', $provides), $url, $status, theme('links', $links));
279 }
280
281 if ($items) {
282 $output .= theme('table', array(t('Default View'), t('Title'), t('Description'), t('Provides'), t('URL'), t('Status'), t('Actions')), $items, array("cellpadding" => "4"), t('Default Views'));
283 }
284 else {
285 $output .= t('<p>No views have currently been defined.</p>');
286 }
287
288 return $output;
289 }
290
291 /*
292 * Page to enable a disabled default view
293 */
294 function views_ui_admin_enable_page($view = '') {
295 if ($view) {
296 $views_status = variable_get('views_defaults', array());
297 $views_status[$view] = 'enabled';
298 variable_set('views_defaults', $views_status);
299 menu_rebuild();
300 }
301 drupal_goto('admin/views');
302 }
303
304 /*
305 * Page to disable an enabled default view
306 */
307 function views_ui_admin_disable_page($view = '') {
308 if ($view) {
309 $views_status = variable_get('views_defaults', array());
310 $views_status[$view] = 'disabled';
311 variable_set('views_defaults', $views_status);
312 menu_rebuild();
313 }
314 drupal_goto('admin/views');
315 }
316
317 /*
318 * Provide a textarea to paste a view export into.
319 */
320 function views_ui_admin_import_page() {
321 $op = $_POST['edit']['name'];
322
323 if ($op) {
324 return views_ui_admin_add_page();
325 }
326 drupal_set_title("Import a View");
327 $form['view'] = array(
328 '#type' => 'textarea',
329 '#title' => t('Import View Code'),
330 '#cols' => 60,
331 '#rows' => 6,
332 '#description' => t('Cut and paste the results of an Export View here.'),
333 );
334
335 $form['submit'] = array(
336 '#type' => 'submit',
337 '#value' => t("Submit"),
338 );
339
340 return drupal_get_form('views_import_view', $form);
341 }
342
343 /*
344 * Handle the submit button on importing a view.
345 */
346 function views_import_view_submit($formid, $form) {
347 ob_start();
348 eval($form['view']);
349 ob_end_clean();
350
351 $tables = array_keys(_views_get_tables());
352 if ($view) {
353 if (!is_array($view->requires) || !array_diff($view->requires, $tables)) {
354 _views_sanitize_view($view);
355 drupal_set_title(t('Add a View'));
356 $output = _views_view_form($view, NULL);
357 print theme('page', $output);
358 exit;
359 }
360 else {
361 drupal_set_message(t("You don't seem to have the following requirements: ") . implode(', ', array_diff($view->requires, $tables)));
362 }
363 }
364 else {
365 drupal_set_message(t('Unable to get a view out of that.'));
366 }
367 }
368
369 /*
370 * Export a view for cut & paste.
371 */
372 function views_ui_admin_export_page($vid = '') {
373 $code = views_create_view_code($vid);
374 $lines = substr_count($code, "\n");
375 $form['code'] = array(
376 '#type' => 'textarea',
377 '#title' => $view->name,
378 '#default_value' => $code,
379 '#rows' => $lines);
380 return drupal_get_form('views_export', $form);
381 }
382
383 /*
384 * Provide a form to add a view. Allow adding a view from default templates.
385 */
386 function views_ui_admin_add_page($template) {
387 $op = $_POST['op'];
388 if ($op == t('Cancel')) {
389 return 'admin/views';
390 }
391
392 $view = _views_get_default_view($template);
393
394 drupal_set_title(t('Add a View'));
395
396 return _views_view_form($view, $op);
397 }
398
399 /*
400 * Provide a form to clone a view.
401 */
402 function views_ui_admin_clone_page($viewname) {
403 $op = $_POST['op'];
404 if ($op == t('Cancel')) {
405 return 'admin/views';
406 }
407
408 $view = views_load_view($viewname);
409 if (!$view) {
410 return drupal_not_found();
411 }
412
413 unset($view->vid);
414 drupal_set_title(t('Add a View'));
415
416 return _views_view_form($view, $op);
417 }
418
419 /*
420 * Provide a form to edit a view.
421 */
422 function views_ui_admin_edit_page($vid = '') {
423 $op = $_POST['op'];
424 if ($op == t('Cancel')) {
425 return 'admin/views';
426 }
427
428 if ($op == t('Delete')) {
429 return "admin/views/delete/$vid";
430 }
431
432 if (!($view = _views_load_view($vid))) {
433 return 'admin/views';
434 }
435
436 drupal_set_title(t('Edit view %n', array('%n' => $view->name)));
437 return _views_view_form($view, $op);
438 }
439
440 /*
441 * Provide a form to confirm deletion of a view.
442 */
443 function views_ui_admin_delete_page($vid = '') {
444 $view = _views_load_view($vid);
445
446 if (!$view) {
447 return 'admin/views';
448 }
449
450 $form['vid'] = array('#type' => 'value', '#value' => $view->vid);
451 return confirm_form('views_delete_confirm', $form,
452 t('Are you sure you want to delete %title?', array('%title' => $view->name)),
453 $_GET['destination'] ? $_GET['destination'] : 'admin/views',
454 t('This action cannot be undone.'),
455 t('Delete'), t('Cancel')
456 );
457 }
458
459 /*
460 * Handle the submit button to delete a view.
461 */
462 function views_delete_confirm_submit($formid, $form) {
463 if ($form['confirm']) {
464 _views_delete_view((object) $form);
465 menu_rebuild();
466 drupal_goto('admin/views');
467 }
468 }
469
470 /*
471 * Get an empty view with basic defaults.
472 */
473 function _views_get_default_view($template = '') {
474 if ($template) {
475 $default_views = _views_get_default_views();
476 if (isset($default_views[$template])) {
477 $view = $default_views[$template];
478 }
479 }
480 if (!$view) {
481 $view = new stdClass();
482 $view->use_pager = true;
483 $view->nodes_per_page = variable_get('default_nodes_main', 10);
484 $view->page_header_format = variable_get('filter_default_format', 1);
485 $view->page_footer_format = variable_get('filter_default_format', 1);
486 $view->page_header_format = variable_get('filter_default_format', 1);
487 $view->block_header_format = variable_get('filter_default_format', 1);
488 $view->block_footer_format = variable_get('filter_default_format', 1);
489 $view->block_header_format = variable_get('filter_default_format', 1);
490 $view->vid = 0;
491 }
492
493 return _views_check_arrays($view);
494 }
495
496 // ---------------------------------------------------------------------------
497 // Select Box Definitions
498
499 // These should probably have string array keys that are easier to identify.
500
501 /**
502 * Select box entries for argument defaults.
503 */
504 function _views_get_arguments_default() {
505 return array(
506 1 => t('Return Page Not Found'),
507 2 => t('Display All Values'),
508 3 => t('Summary, unsorted'),
509 4 => t('Summary, sorted ascending'),
510 5 => t('Summary, sorted descending'),
511 6 => t('Summary, sorted as view'),
512 );
513 }
514
515 /**
516 * Select box entries for sort ordering.
517 */
518 function _views_sortorders() {
519 return array(
520 'ASC' => t('Ascending'),
521 'DESC' => t('Descending')
522 );
523 }
524
525 /**
526 * Swap two items in an array.
527 */
528 function _views_swap(&$arr, $a, $b) {
529 $temp = $arr[$a];
530 $arr[$a] = $arr[$b];
531 $arr[$b] = $temp;
532 }
533
534 /**
535 * Move an item up in an array.
536 */
537 function _views_move_up(&$arr, $i) {
538 if ($i <= 0 || $i >= count($arr)) {
539 return; // can't do it.
540 }
541 _views_swap($arr, $i - 1, $i);
542 }
543
544 /**
545 * Move an item down in an array.
546 */
547 function _views_move_down(&$arr, $i) {
548 if ($i >= count($arr) - 1 || $i < 0) {
549 return; // can't do it.
550 }
551 _views_swap($arr, $i + 1, $i);
552 }
553
554 /**
555 * Move an item to the front of an array.
556 */
557 function _views_move_top(&$arr, $i) {
558 if ($i <= 0 || $i >= count($arr)) {
559 return; // can't do it.
560 }
561 $temp = $arr[$i];
562 for ($x = $i; $x > 0; $x--)
563 $arr[$x] = $arr[$x - 1];
564 $arr[0] = $temp;
565 }
566
567 /**
568 * Move an item to the end of an array.
569 */
570 function _views_move_bottom(&$arr, $i) {
571 $end = count($arr) - 1;
572 if ($i >= $end || $i < 0) {
573 return; // can't do it.
574 }
575 $temp = $arr[$i];
576 for ($x = $i; $x < $end; $x++)
577 $arr[$x] = $arr[$x + 1];
578 $arr[$end] = $temp;
579 }
580
581 /**
582 * Figure out which of the many, many buttons on a form were clicked and
583 * handle it.
584 */
585 function _views_check_sub_ops(&$form, &$order, $i) {
586
587 if ($form['delete']['#value']) {
588 unset($form['delete']['#value']);
589 unset($order[$i]);
590 $order = array_values($order); // reindex
591 $form['delete']['#printed'] = true;
592 $form['up']['#printed'] = true;
593 $form['down']['#printed'] = true;
594 $form['top']['#printed'] = true;
595 $form['bottom']['#printed'] = true;
596 return 'delete';
597 }
598 else foreach (array('up', 'down', 'top', 'bottom') as $dir) {
599 if ($form[$dir]['#value']) {
600 unset ($form[$dir]['#value']);
601 $func = "_views_move_$dir";
602 $func($order, $i);
603 return true;
604 }
605 }
606 return false;
607 }
608
609 /**
610 * Figure out if one of the add buttons on a form were clicked, and handle it.
611 */
612 function _views_check_ops(&$view, $op, $form) {
613 if ($op == t('Add Filter')) {
614 $view->new_filter['id'] = $form['filter']['add']['id']['#value'];
615 return 'filter';
616 }
617 else if ($op == t('Add Criteria')) {
618 $view->new_sort['id'] = $form['sort']['add']['id']['#value'];
619 return 'sort';
620 }
621 else if ($op == t('Add Argument')) {
622 $view->new_argument['id'] = $form['argument']['add']['id']['#value'];
623 return 'argument';
624 }
625 else if ($op == t('Add Field')) {
626 $fieldbits = explode('.', $form['field']['add']['id']['#value']);
627 $view->new_field['id'] = $form['field']['add']['id']['#value'];
628 $view->new_field['tablename'] = $fieldbits[0];
629 $view->new_field['field'] = $fieldbits[1];
630 $view->new_field['label'] = $fieldnames[$form['field']['add']['id']['#value']];
631 $view->new_field['queryname'] = "$fieldbits[0]_$fieldbits[1]";
632 return 'field';
633 }
634 else if ($op == t('Expose Filter')) {
635 $view->new_exposed_filter['id'] = $form['exposed_filter']['add']['id']['#value'];
636 return 'filter';
637 }
638 }
639
640 /**
641 * Custom form element to do our nice images.
642 */
643 function views_elements() {
644 $type['imagebutton'] = array('#input' => TRUE, '#button_type' => 'submit',);
645 return $type;
646 }
647
648 function theme_imagebutton($element) {
649 return '<input type="image" class="form-'. $element['#button_type'] .'" name="'. $element['#name'] .'" value="'. check_plain($element['#default_value']) .'" '. drupal_attributes($element['#attributes']) . ' src="' . $element['#image'] . '" alt="' . $element['#title'] . '" title="' . $element['#title'] . "\" />\n";
650 }
651
652 function imagebutton_value() {
653 // null function guarantees default_value doesn't get moved to #value.
654 }
655
656 /**
657 * Display all the guts of a view in a form for editing.
658 */
659 function _views_view_form($view, $op = '') {
660 _views_check_arrays($view); // make sure arrays that might be empty get set
661
662 // Put in all our add buttons, then process them to see if they've been hit.
663 views_ui_add_add_button($form, 'field', _views_get_fields(true), t('Add Field'));
664 views_ui_add_add_button($form, 'argument', _views_get_arguments(true), t('Add Argument'));
665 views_ui_add_add_button($form, 'filter', _views_get_filters(true), t('Add Filter'));
666 views_ui_add_add_button($form, 'sort', _views_get_sorts(true), t('Add Criteria'));
667
668 $allbut = _views_check_ops($view, $op, $form);
669 if ($_POST['edit'] && $op != t('Save')) {
670 drupal_set_message(t('You have modified this view; changes will not be recorded until you Save the form.'));
671 }
672
673 $form['exposed_filter'] = array();
674 foreach (array('field', 'argument', 'filter', 'exposed_filter', 'sort') as $section) {
675 if (views_ui_add_section($form[$section], $view, $section)) {
676 $allbut = $section;
677 }
678 }
679
680 $form['vid'] = array(
681 '#type' => 'value',
682 '#value' => $view->vid,
683 );
684 $form['allbut'] = array(
685 '#type' => 'value',
686 '#value' => $allbut,
687 );
688 $form['changed'] = array(
689 '#type' => 'hidden',
690 '#value' => $view->changed,
691 );
692
693 $form['basic-info'] = array(
694 '#type' => 'fieldset',
695 '#collapsible' => true,
696 '#collapsed' => ($allbut != NULL),
697 '#title' => t('Basic Information'),
698 );
699
700 $form['basic-info']['name'] = array(
701 '#type' => 'textfield',
702 '#title' => t('Name'),
703 '#default_value' => $view->name,
704 '#size' => 20,
705 '#maxlength' => 32,
706 '#description' => t('The unique identifier of the view; it is only important for overridden views and views that modules or themes will need to use. Only alphanumeric and _ allowed here'),
707 '#required' => true,
708 );
709
710 $form['basic-info']['access'] = array(
711 '#type' => 'checkboxes',
712 '#title' => t('Access'),
713 '#default_value' => $view->access,
714 '#options' => views_handler_filter_role(),
715 '#description' => t('Only the checked roles will be able to see this view in any form; if no roles are checked, access will not be restricted.'),
716 );
717
718 $form['basic-info']['description'] = array(
719 '#type' => 'textfield',
720 '#title' => t('Description'),
721 '#default_value' => $view->description,
722 '#size' => 60,
723 '#maxlength' => 255,
724 '#description' => t('A description of the view for the admin list.'),
725 );
726
727 // page info
728
729 $form['page-info'] = array(
730 '#type' => 'fieldset',
731 '#collapsible' => true,
732 '#collapsed' => ($allbut != NULL || !$view->page),
733 '#title' => t('Page'),
734 );
735
736 $form['page-info']['page'] = array(
737 '#type' => 'checkbox',
738 '#title' => t('Provide Page View'),
739 '#return_value' => 1,
740 '#default_value' => $view->page,
741 '#description' => t('If checked this view will be provided as a page. If not checked, the fields in this group will be ignored.'),
742 );
743
744 $form['page-info']['url'] = array(
745 '#type' => 'textfield',
746 '#title' => t('URL'),
747 '#default_value' => $view->url,
748 '#size' => 60,
749 '#maxlength' => 255,
750 '#description' => t("Enter the URL to use for this view in the form of 'dir/dir'. Do not begin or end the URL with a /. Example: 'view/tracker'. This is required if providing a page view. You can also add $arg as a placeholder for arguments passed in the URL, for example 'user/$arg/tracker' or 'view/taxonomy/$arg'."),
751 );
752
753 $form['page-info']['page_type'] = array(
754 '#type' => 'select',
755 '#title' => t('View Type'),
756 '#default_value' => $view->page_type,
757 '#options' => _views_get_style_plugins(true),
758 '#description' => t('How the nodes should be displayed to the user.'),
759 );
760
761 $form['page-info']['page_title'] = array(
762 '#type' => 'textfield',
763 '#title' => t('Title'),
764 '#default_value' => $view->page_title,
765 '#size' => 60,
766 '#maxlength' => 255,
767 '#description' => t('The title that be shown at the top of the view. May be blank. If your View allows arguments, you may use %1 to substitute argument 1 text, %2 to argument #2, and so on.'),
768 );
769
770 $form['page-info']['use_pager'] = array(
771 '#type' => 'checkbox',
772 '#title' => t('Use Pager'),
773 '#return_value' => 1,
774 '#default_value' => $view->use_pager,
775 '#description' => t('If checked this query may be multiple pages. If not checked this query will be one page.'),
776 );
777 $form['page-info']['breadcrumb_no_home'] = array(
778 '#type' => 'checkbox',
779 '#title' => t('Breadcrumb trail should not include "Home"'),
780 '#return_value' => 1,
781 '#default_value' => $view->breadcrumb_no_home,
782 '#description' => t('If checked the breadcrumb trail for this page will discard "Home". Usually you will not set this, but this is used for the Front Page View, where it IS Home and should not leave a trail to itself.'),
783 );
784 $form['page-info']['nodes_per_page'] = array(
785 '#type' => 'textfield',
786 '#title' => t('Nodes per Page'),
787 '#default_value' => $view->nodes_per_page,
788 '#size' => 5,
789 '#maxlength' => 5,
790 '#description' => t('The number of nodes to display per page. If 0 no nodes will be displayed. If not using a pager, this will be the maximum number of nodes in the list.'),
791 '#attributes' => NULL,
792 '#required' => true,
793 );
794 $form['page-info']['page_header_fieldset'] = array(
795 '#type' => 'fieldset',
796 '#collapsible' => TRUE,
797 '#collapsed' => TRUE,
798 '#title' => t('Header'),
799 );
800 $form['page-info']['page_header_fieldset']['page_header'] = array(
801 '#type' => 'textarea',
802 '#default_value' => $view->page_header,
803 '#cols' => 60,
804 '#rows' => 6,
805 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
806 );
807
808 $form['page-info']['page_header_fieldset']['page_header_format'] = filter_form($view->page_header_format, 1, array('page_header_format'));
809
810 $form['page-info']['page_footer_fieldset'] = array(
811 '#type' => 'fieldset',
812 '#collapsible' => TRUE,
813 '#collapsed' => TRUE,
814 '#title' => t('Footer'),
815 );
816 $form['page-info']['page_footer_fieldset']['page_footer'] = array(
817 '#type' => 'textarea',
818 '#default_value' => $view->page_footer,
819 '#cols' => 60,
820 '#rows' => 6,
821 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
822 );
823
824 $form['page-info']['page_footer_fieldset']['page_footer_format'] = filter_form($view->page_footer_format, 1, array('page_footer_format'));
825
826 $form['page-info']['page_empty_fieldset'] = array(
827 '#type' => 'fieldset',
828 '#collapsible' => TRUE,
829 '#collapsed' => TRUE,
830 '#title' => t('Empty Text'),
831 );
832 $form['page-info']['page_empty_fieldset']['page_empty'] = array(
833 '#type' => 'textarea',
834 '#default_value' => $view->page_empty,
835 '#cols' => 60,
836 '#rows' => 6,
837 '#description' => t('Text to display if a view returns no nodes. Optional.'),
838 );
839
840 $form['page-info']['page_empty_fieldset']['page_empty_format'] = filter_form($view->page_empty_format, 1, array('page_empty_format'));
841
842 $form['page-info']['menu-info'] = array(
843 '#type' => 'fieldset',
844 '#collapsible' => TRUE,
845 '#collapsed' => TRUE,
846 '#title' => t('Menu'),
847 );
848
849 $form['page-info']['menu-info']['menu'] = array(
850 '#type' => 'checkbox',
851 '#title' => t('Provide Menu'),
852 '#return_value' => 1,
853 '#default_value' => $view->menu,
854 '#description' => t('If checked this view be given a menu entry in the Drupal menu system. If not checked the data in this group will be ignored.'),
855 );
856
857 $form['page-info']['menu-info']['menu_tab'] = array(
858 '#type' => 'checkbox',
859 '#title' => t('Provide Menu as Tab'),
860 '#return_value' => 1,
861 '#default_value' => $view->menu_tab,
862 '#description' => t("If checked this view's menu entry will be provided as a tab rather than in the main menu system."),
863 );
864
865 $form['page-info']['menu-info']['menu_tab_default'] = array(
866 '#type' => 'checkbox',
867 '#title' => t('Make Default Menu Tab'),
868 '#return_value' => 1,
869 '#default_value' => $view->menu_tab_default,
870 '#description' => t("If checked this view's menu entry will be provided as a tab, and will be the default tab for that URL path. For example, if the URL is 'tracker/all' and it is set as the default menu tab, it will be put into the menu as 'tracker' and 'tracker/all' will be the default tab. For tabs to work properly, one tab in the group must be set as the default."),
871 );
872
873 $form['page-info']['menu-info']['menu_tab_weight'] = array(
874 '#type' => 'textfield',
875 '#title' => t('Tab Weight'),
876 '#default_value' => $view->menu_tab_weight,
877 '#width' => 10,
878 '#description' => t('If this is a menu tab, select the weight; lower numbers will be further to the left.'),
879 );
880
881 $form['page-info']['menu-info']['menu_title'] = array(
882 '#type' => 'textfield',
883 '#title' => t('Menu Title'),
884 '#default_value' => $view->menu_title,
885 '#size' => 60,
886 '#maxlength' => 255,
887 '#description' => t('Enter the title to use for the menu entry or tab. If blank, the page title will be used.'),
888 );
889
890 // block info
891
892 $form['block-info'] = array(
893 '#type' => 'fieldset',
894 '#collapsible' => true,
895 '#collapsed' => ($allbut != NULL || !$view->block),
896 '#title' => t('Block'),
897 );
898
899 $form['block-info']['block'] = array(
900 '#type' => 'checkbox',
901 '#title' => t('Provide Block'),
902 '#return_value' => 1,
903 '#default_value' => $view->block,
904 '#description' => t('If checked this view will be provided as a block. If checked title may not be blank.'),
905 );
906
907 $form['block-info']['block_type'] = array(
908 '#type' => 'select',
909 '#title' => t('View Type'),
910 '#default_value' => $view->block_type,
911 '#options' => _views_get_style_plugins(true),
912 '#description' => t('How the nodes should be displayed to the user.'),
913 );
914
915 $form['block-info']['block_title'] = array(
916 '#type' => 'textfield',
917 '#title' => t('Title'),
918 '#default_value' => $view->block_title,
919 '#size' => 60,
920 '#maxlength' => 255,
921 '#description' => t('The title that will be shown at the top of the block. May be blank.'),
922 );
923
924 $form['block-info']['nodes_per_block'] = array(
925 '#type' => 'textfield',
926 '#title' => t('Nodes per Block'),
927 '#default_value' => $view->nodes_per_block,
928 '#size' => 2,
929 '#maxlength' => 2,
930 '#description' => t('If using a block, the maximum number of items to display in the block. Pagers are not used in blocks.'),
931 '#attributes' => NULL,
932 );
933
934 $form['block-info']['block_more'] = array(
935 '#type' => 'checkbox',
936 '#title' => t('[More] Link?'),
937 '#return_value' => 1,
938 '#default_value' => $view->block_more,
939 '#description' => t('If using a view as both a page and a block, display a more link in the block that links to the view URL?'),
940 );
941
942 $form['block-info']['block_header_fieldset'] = array(
943 '#type' => 'fieldset',
944 '#collapsible' => TRUE,
945 '#collapsed' => TRUE,
946 '#title' => t('Header'),
947 );
948 $form['block-info']['block_header_fieldset']['block_use_page_header'] = array(
949 '#type' => 'checkbox',
950 '#title' => t('Use Page Header'),
951 '#return_value' => 1,
952 '#default_value' => $view->block_use_page_header,
953 '#description' => t('If checked, use the Page Header for block view instead. If so, you should leave the Block Header blank.'),
954 );
955
956 $form['block-info']['block_header_fieldset']['block_header'] = array(
957 '#type' => 'textarea',
958 '#title' => t('Header'),
959 '#default_value' => $view->block_header,
960 '#cols' => 60,
961 '#rows' => 6,
962 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
963 );
964
965 $form['block-info']['block_header_fieldset']['block_header_format'] = filter_form($view->block_header_format, 1, array( 'block_header_format'));
966
967 $form['block-info']['block_footer_fieldset'] = array(
968 '#type' => 'fieldset',
969 '#collapsible' => TRUE,
970 '#collapsed' => TRUE,
971 '#title' => t('Footer'),
972 );
973 $form['block-info']['block_footer_fieldset']['block_use_page_footer'] = array(
974 '#type' => 'checkbox',
975 '#title' => t('Use Page Footer'),
976 '#return_value' => 1,
977 '#default_value' => $view->block_use_page_footer,
978 '#description' => t('If checked, use the page footer for block view instead. If so, you should leave the block footer blank.'),
979 );
980
981 $form['block-info']['block_footer_fieldset']['block_footer'] = array(
982 '#type' => 'textarea',
983 '#title' => t('Footer'),
984 '#default_value' => $view->block_footer,
985 '#cols' => 60,
986 '#rows' => 6,
987 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
988 );
989
990 $form['block-info']['block_footer_fieldset']['block_footer_format'] = filter_form($view->block_footer_format, 1, array( 'block_footer_format'));
991
992 $form['block-info']['block_empty_fieldset'] = array(
993 '#type' => 'fieldset',
994 '#collapsible' => TRUE,
995 '#collapsed' => TRUE,
996 '#title' => t('Empty text'),
997 );
998 $form['block-info']['block_empty_fieldset']['block_use_page_empty'] = array(
999 '#type' => 'checkbox',
1000 '#title' => t('Use Page empty'),
1001 '#return_value' => 1,
1002 '#default_value' => $view->block_use_page_empty,
1003 '#description' => t('If checked, use the Page Empty Text for block view instead. If so, you should leave the block empty text blank.'),
1004 );
1005
1006 $form['block-info']['block_empty_fieldset']['block_empty'] = array(
1007 '#type' => 'textarea',
1008 '#title' => t('Empty text'),
1009 '#default_value' => $view->block_empty,
1010 '#cols' => 60,
1011 '#rows' => 6,
1012 '#description' => t('Text to display if a view results in no nodes. Optional.'),
1013 );
1014
1015 $form['block-info']['block_empty_fieldset']['block_empty_format'] = filter_form($view->block_empty_format, 1, array( 'block_empty_format'));
1016
1017 $access = user_access('use PHP for block visibility');
1018 if ($access) {
1019 $form['view_args_php_fieldset'] = array(
1020 '#type' => 'fieldset',
1021 '#collapsible' => TRUE,
1022 '#collapsed' => empty($view->view_args_php) ? TRUE : FALSE,
1023 '#title' => t('Argument Handling Code'),
1024 );
1025
1026 $form['view_args_php_fieldset']['view_args_php'] = array(
1027 '#type' => 'textarea',
1028 '#title' => t('Argument Code'),
1029 '#default_value' => $view->view_args_php,
1030 '#cols' => 60,
1031 '#rows' => 6,
1032 '#description' => '<p>'. t('Advanced Usage Only: PHP code that returns a custom array of arguments for the view. Should not include &lt;?php ?&gt; delimiters.') .'</p>' .
1033 '<p>'. t('For more information, please see the <a href="%arg">Argument Handling Code documentation</a> in the Drupal handbook.', array('%arg' => 'http://drupal.org/node/70145')) .'</p>',
1034 );
1035 } else {
1036 $form['view_args_php_fieldset']['view_args_php'] = array(
1037 '#type' => 'value',
1038 '#value' => $view->view_args_php,
1039 );
1040 }
1041
1042 $form['save'] = array(
1043 '#type' => 'submit',
1044 '#value' => t('Save'),
1045 );
1046 if ($view->vid) {
1047 $form['delete'] = array(
1048 '#type' => 'submit',
1049 '#value' => t('Delete'),
1050 );
1051 }
1052 $form['cancel'] = array(
1053 '#type' => 'submit',
1054 '#value' => t('Cancel'),
1055 );
1056
1057 return drupal_get_form('views_edit_view', $form);
1058 }
1059
1060 /**
1061 * separately build a piece of the form
1062 */
1063 function views_ui_build_form(&$form) {
1064 $form = form_builder('views_edit_view', $form);
1065 }
1066
1067 /**
1068 * Add the 'add' button for a section into the form
1069 */
1070 function views_ui_add_add_button(&$form, $section, $options, $label) {
1071 $form[$section]['add'] = array('#tree' => true, '#parents' => array($section, 'add'));
1072
1073 $form[$section]['add']['id'] = array(
1074 '#type' => 'select',
1075 '#options' => $options,
1076 );
1077 $form[$section]['add']['button'] = array(
1078 '#type' => 'button',
1079 '#value' => $label,
1080 );
1081 views_ui_build_form($form[$section]['add']);
1082 }
1083
1084 /**
1085 * Add one of the list sections to the form.
1086 */
1087 function views_ui_add_section(&$form, &$view, $section) {
1088 // add fields to the form.
1089
1090 $form += array('#tree' => true, '#parents' => array($section));
1091 $view_section = &$view->$section;
1092
1093 $num_items = count($view_section);
1094 // Populate the form with fields we need to check to track
1095 // changes through the form's life.
1096 $form['count'] = array('#type' => 'hidden', '#default_value' => $num_items);
1097
1098 $form['order'] = array(
1099 '#type' => 'hidden',
1100 '#default_value' => $num_items ? implode(',', range(0, $num_items - 1)) : '',
1101 );
1102
1103 // Run the builder to get the value on the form.
1104 views_ui_build_form($form);
1105 $order = $form['order']['#value'] != ''
1106 ? explode(',', $form['order']['#value'])
1107 : array();
1108
1109 // Shortcut because operator precedence gets sticky if using $foo->$bar[]
1110 for ($i = $num_items; $i < $form['count']['#value']; $i++) {
1111 $view_section[] = array();
1112 }
1113
1114 $new_section = "new_$section";
1115 // instantiate the new field if one was added.
1116 if ($view->$new_section) {
1117 $view_section[] = $view->$new_section;
1118 $order[] = $form['count']['#value'];
1119 $form['count']['#value']++;
1120 }
1121
1122 $func = "views_ui_add_$section";
1123 if ($order) {
1124 foreach ($order as $key => $i) {
1125 $form[$i] = array('#tree' => true, '#parents' => array($section, $i));
1126
1127 $form[$i]['id'] = array(
1128 '#type' => 'hidden',
1129 '#default_value' => $view_section[$i]['id'],
1130 );
1131
1132 views_ui_add_buttons($form[$i]);
1133 views_ui_build_form($form[$i]);
1134
1135 $retval = _views_check_sub_ops($form[$i], $order, $key);
1136 if ($retval !== 'delete') {
1137 $retval2 = $func($form[$i], $view_section[$i], $order, $key, $i, $view);
1138 }
1139
1140 if ($retval || $retval2) {
1141 $allbut = $section;
1142 }
1143 }
1144 $form['order']['#value'] = implode(',', $order);
1145 }
1146 return $allbut;
1147 }
1148
1149 /**
1150 * Add all the info for a single field into the form.
1151 */
1152 function views_ui_add_field(&$form, $field, &$order, $key, $i) {
1153 $fields = _views_get_fields();
1154 $fieldname = $form['id']['#value']; // combined table & fieldname
1155 $fieldinfo = $fields[$fieldname]; // laziness + readability
1156
1157 $form['fullname'] = array(
1158 '#type' => 'hidden',
1159 '#default_value' => $field['id'],
1160 );
1161
1162 $form['name'] = array(
1163 '#type' => 'markup',
1164 '#value' => $fieldinfo['name'], // combined table & fieldname
1165 );
1166 $form['queryname'] = array(
1167 '#type' => 'hidden',
1168 '#default_value' => $field['queryname'],
1169 );
1170 $form['tablename'] = array(
1171 '#type' => 'hidden',
1172 '#default_value' => $field['tablename'],
1173 );
1174 $form['field'] = array(
1175 '#type' => 'hidden',
1176 '#default_value' => $field['field'],
1177 );
1178
1179 $form['label'] = array(
1180 '#type' => 'textfield',
1181 '#default_value' => $field['label'],
1182 '#size' => 15,
1183 '#maxlength' => 255,
1184 );
1185
1186 if (is_array($fieldinfo['handler'])) {
1187 $form['handler'] = array(
1188 '#type' => 'select',
1189 '#default_value' => $field['handler'],
1190 '#options' => $fieldinfo['handler'],
1191 );
1192 }
1193
1194 if (isset($fieldinfo['option'])) {
1195 $widget = $fieldinfo['option'];
1196 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1197 $widget['#options'] = $widget['#options']('option', $fieldinfo['option']);
1198 }
1199 $widget['#default_value'] = $field['options'];
1200 $form['options'] = $widget;
1201 }
1202
1203
1204 if ($fieldinfo['sortable']) {
1205 $form['sortable'] = array(
1206 '#type' => 'select',
1207 '#default_value' => $field['sortable'],
1208 '#options' => array('0' => t('No'), '1' => t('Yes')),
1209 );
1210 $form['defaultsort'] = array(
1211 '#type' => 'select',
1212 '#default_value' => $field['defaultsort'],
1213 '#options' => array('0' => t('None'), 'ASC' => t('Ascending'), 'DESC' => t('Descending')),
1214 );
1215 }
1216 if (isset($fieldinfo['help'])) {
1217 $form['help'] = array(
1218 '#type' => 'markup',
1219 '#value' => '<div class="description">' . $fieldinfo['help'] . '</div>',
1220 );
1221 }
1222 }
1223
1224 /**
1225 * Add all the info for a single argument into the form.
1226 */
1227 function views_ui_add_argument(&$form, $argument, &$order, $key, $i) {
1228 $arguments = _views_get_arguments();
1229 $arg_type = $form['id']['#value'];
1230 $arginfo = $arguments[$arg_type];
1231
1232 $form['type'] = array(
1233 '#type' => 'hidden',
1234 '#default_value' => $argument['id'],
1235 );
1236
1237 $form['name'] = array(
1238 '#value' => $arginfo['name'],
1239 );
1240
1241 $form['argdefault'] = array(
1242 '#type' => 'select',
1243 '#default_value' => max(1, intval($argument['argdefault'])),
1244 '#options' => _views_get_arguments_default(),
1245 );
1246 $form['title'] = array(
1247 '#type' => 'textfield',
1248 '#default_value' => $argument['title'],
1249 '#size' => 10,
1250 '#maxlength' => 255,
1251 );
1252 if (isset($arginfo['option'])) {
1253 $widget = $arginfo['option'];
1254 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1255 $widget['#options'] = $widget['#options']('option', $arginfo['option']);
1256 }
1257 $widget['#default_value'] = $argument['options'];
1258 $form['options'] = $widget;
1259 }
1260
1261 if (isset($arginfo['help'])) {
1262 $form['help'] = array(
1263 '#type' => 'markup',
1264 '#value' => $arginfo['help'],
1265 );
1266 }
1267 }
1268
1269 /**
1270 * Add all the info for a single filter into the form.
1271 */
1272 function views_ui_add_filter(&$form, $filter, &$order, $key, $i, &$view) {
1273 $filters = _views_get_filters();
1274 $filtername = $form['id']['#value']; // combined table & filtername
1275 $filterinfo = $filters[$filtername];
1276
1277 $form['field'] = array(
1278 '#type' => 'hidden',
1279 '#default_value' => $filter['id'],
1280 );
1281
1282 $form['name'] = array(
1283 '#type' => 'markup',
1284 '#value' => $filterinfo['name'],
1285 );
1286
1287 // 'operator' is either an array or a handler
1288 $operator = $filterinfo['operator'];
1289 if (!is_array($operator) && function_exists($filterinfo['operator'])) {
1290 $operator = $filterinfo['operator']('operator', $filterinfo);
1291 }
1292
1293 $form['operator'] = array(
1294 '#type' => 'select',
1295 '#default_value' => $filter['operator'],
1296 '#options' => $operator,
1297 );
1298 if (isset($filterinfo['option'])) {
1299 $widget = $filterinfo['option'];
1300 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1301 $widget['#options'] = $widget['#options']('option', $filterinfo['option']);
1302 }
1303 $widget['#default_value'] = $filter['options'];
1304 $form['options'] = $widget;
1305 }
1306
1307 $form['value'] = $filterinfo['value'];
1308 if (!is_array($form['value']['#options']) && function_exists($form['value']['#options'])) {
1309 $form['value']['#options'] = $form['value']['#options']('value', $filterinfo);
1310 }
1311 $form['value']['#default_value'] = $filter['value'];
1312
1313 if (isset($filterinfo['help'])) {
1314 $form['help'] = array(
1315 '#type' => 'markup',
1316 '#value' => $filterinfo['help'],
1317 );
1318 }
1319
1320 // This is kind of a pain in the butt -- The only way to know all of
1321 // the filters that have been exposed is to look at the form, but that
1322 // part of the form hasn't been built yet, so I can't use the form_builder
1323 // method. I can't put this after that, either, because then I'd have to
1324 // go back to add one if we see one here. So we cheat heavily with _POST.
1325
1326 if ($_POST['edit']) {
1327 $exposed_order = $_POST['edit']['exposed_filter']['order'];
1328 if ($exposed_order !== '') {
1329 $exposed_order = explode(',', $exposed_order);
1330 foreach ($exposed_order as $x) {
1331 if ($_POST['edit']['exposed_filter'][$x]['id'] == $filtername) {
1332 $exposed = true;
1333 break;
1334 }
1335 }
1336 }
1337 }
1338 else { // do this if there's never been a submit.
1339 $exposed_order = array_keys($view->exposed_filter);
1340 foreach ($exposed_order as $x) {
1341 if ($view->exposed_filter[$x]['id'] == $filtername) {
1342 $exposed = true;
1343 break;
1344 }
1345 }
1346 }
1347 if (!$exposed) {
1348 // I can't find a way to see if this button was pressed other than
1349 // directly checking, because #form_submitted doesn't work on arrays
1350
1351 if ($_POST['edit']['filter'][$i]['expose']) {
1352 $view->new_exposed_filter['id'] = $filtername;
1353 return true;
1354 }
1355 else {
1356 $form['expose'] = array(
1357 '#type' => 'button',
1358 '#default_value' => t('Expose'),
1359 '#name' => "edit[filter][$i][expose]",
1360 );
1361 }
1362 }
1363 }
1364
1365 /**
1366 * Add all the info for a single exposed filter into the form.
1367 */
1368 function views_ui_add_exposed_filter(&$form, $filter, &$order, $key, $i) {
1369 $filters = _views_get_filters();
1370 $filtername = $form['id']['#value']; // combined table & filtername
1371 $filterinfo = $filters[$filtername];
1372
1373 $form['field'] = array(
1374 '#type' => 'hidden',
1375 '#default_value' => $filter['id'],
1376 );
1377
1378 $form['name'] = array(
1379 '#type' => 'markup',
1380 '#value' => $filterinfo['name'],
1381 );
1382
1383 $form['label'] = array(
1384 '#type' => 'textfield',
1385 '#default_value' => $filter['label'],
1386 '#size' => 15,
1387 '#maxlength' => 255,
1388 );
1389
1390 $form['optional'] = array(
1391 '#type' => 'checkbox',
1392 '#default_value' => $filter['optional'],
1393 );
1394
1395 $form['is_default'] = array(
1396 '#type' => 'checkbox',
1397 '#default_value' => $filter['is_default'],
1398 );
1399
1400 $form['single'] = array(
1401 '#type' => 'checkbox',
1402 '#default_value' => $filter['single'],
1403 );
1404
1405 $form['operator'] = array(
1406 '#type' => 'checkbox',
1407 '#default_value' => $filter['operator'],
1408 );
1409 }
1410
1411 /**
1412 * Add all the info for a single sort into the form.
1413 */
1414 function views_ui_add_sort(&$form, $sort, &$order, $key, $i) {
1415 $sorts = _views_get_sorts();
1416 $sortname = $form['id']['#value']; // combined table & sortname
1417 $sortinfo = $sorts[$sortname];
1418
1419 $form['field'] = array(
1420 '#type' => 'hidden',
1421 '#default_value' => $sort['id'],
1422 );
1423
1424 $form['name'] = array(
1425 '#value' => $sortinfo['name'],
1426 );
1427
1428 $form['sortorder'] = array(
1429 '#type' => 'select',
1430 '#title' => NULL,
1431 '#default_value' => $sort['sortorder'],
1432 '#options' => _views_sortorders(),
1433 );
1434 // option can be either 'string', 'integer', an array or a handler
1435 // that must return an array.
1436 if (isset($sortinfo['option'])) {
1437 $widget = $sortinfo['option'];
1438 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1439 $widget['#options'] = $widget['#options']('option', $sortinfo['option']);
1440 }
1441 $widget['#default_value'] = $sort['options'];
1442 $form['options'] = $widget;
1443 }
1444 if (isset($sortinfo['help'])) {
1445 $form['help'] = array(
1446 '#type' => 'markup',
1447 '#value' => $sortinfo['help'],
1448 );
1449 }
1450 }
1451
1452 /**
1453 * Add the up, down, top, bottom, delete buttons to a form.
1454 */
1455 function views_ui_add_buttons(&$form_item) {
1456 $form_item['delete'] = views_ui_add_button('user-trash.png', t('Delete'), t('Delete this item.'));
1457 $form_item['up'] = views_ui_add_button('go-up.png', t('Up'), t('Move this item up.'));
1458 $form_item['down'] = views_ui_add_button('go-down.png', t('Down'), t('Move this item down.'));
1459 $form_item['top'] = views_ui_add_button('go-top.png', t('Top'), t('Move this item to the top.'));
1460 $form_item['bottom'] = views_ui_add_button('go-bottom.png', t('Bottom'), t('Move this item to the bottom.'));
1461 }
1462
1463 /**
1464 * Add a single button to a form.
1465 */
1466 function views_ui_add_button($image, $name, $text) {
1467 $module_path = base_path() . drupal_get_path('module', 'views');
1468
1469 return array(
1470 '#type' => 'imagebutton',
1471 '#image' => $module_path . '/' . $image,
1472 '#title' => $text,
1473 '#default_value' => $name,
1474 );
1475 }
1476
1477 /**
1478 * Display the form built by _views_view_form()
1479 */
1480 function theme_views_edit_view($form) {
1481
1482 $output .= form_render($form['basic-info'], false);
1483 $output .= form_render($form['page-info'], false);
1484 $output .= form_render($form['block-info'], false);
1485
1486 $allbut = $form['allbut']['#value'];
1487
1488 // form_render wants a reference so use full array notation.
1489
1490 // fields
1491 $collapsed = $allbut && $allbut != 'field';
1492 $group = views_ui_render_section($form['field'], array('name', 'label', 'handler', 'options', 'sortable', 'defaultsort'), array(t('Name'), t('Label'), t('Handler'), t('Option'), t('Sortable'), t('Default Sort')), 'fields', $collapsed);
1493 $group .= views_ui_render_section_add($form['field']['add'], array('id', 'button'), 'Add Field');
1494
1495 $group .= '<p>'. t('Fields are only meaningful with List view and Table View; they allow you to choose which fields are presented and in what order.') .'</p>';
1496 $output .= theme('fieldset', array('#title' => t('Fields'), '#children' => $group,
1497 '#collapsible' => true, '#collapsed' => $collapsed,));
1498
1499
1500 // arguments
1501 $collapsed = $allbut && $allbut != 'argument';
1502 $group = views_ui_render_section($form['argument'], array('name', 'argdefault', 'title', 'options'), array(t('Argument Type'), t('Default'), t('Title'), t('Option')), 'arguments', $collapsed);
1503
1504 $group .= views_ui_render_section_add($form['argument']['add'], array('id', 'button'), 'Add Argument');
1505
1506 $group .= form_render($form['view_args_php_fieldset']);
1507
1508 $group .= '<p>'. t('Arguments are parsed directly from the URL. They are not necessary to any given view, but allow flexibility.') .'</p>';
1509 $output .= theme('fieldset', array('#title' => t('Arguments'), '#children' => $group,
1510 '#collapsible' => true, '#collapsed' => $collapsed,));
1511
1512 // filter
1513 $collapsed = $allbut && $allbut != 'filter';
1514 $group = views_ui_render_section($form['filter'], array('name', 'operator', 'value', 'options', 'expose'), array(t('Field'), t('Operator'), t('Value'), t('Option'), ''), 'filters', $collapsed);
1515
1516 $group .= views_ui_render_section_add($form['filter']['add'], array('id', 'button'), 'Add Filter');
1517
1518 $group .= '<p>'. t('Filters allow you to select a subset of all the nodes to display. All Filters are ANDed together.') .'</p>';
1519 $output .= theme('fieldset', array('#title' => t('Filters'), '#children' => $group,
1520 '#collapsible' => true, '#collapsed' => $collapsed,));
1521 $group = '';
1522
1523 // exposed exposed_filter
1524 $collapsed = $allbut && $allbut != 'exposed_filter';
1525 $group = views_ui_render_section($form['exposed_filter'], array('name', 'label', 'optional', 'is_default', 'single', 'operator'), array(t('Field'), t('Label'), t('Optional'), t('Filter settings Default'), t('Force Single'), t('Lock Operator')), 'exposed_filters', $collapsed);
1526
1527
1528 // $group .= views_ui_render_section_add($form['exposed_filter']['add'], array('id', 'button'), 'Expose Filter');
1529
1530 $group .= '<p>'. t('Exposed filters will be presented to the viewer. If not set required, then filters will include a "&lt;None&gt;" Value if possible. If set default, filters will default as set here, otherwise filter settings will be ignored. If Lock Operator is set, no operator will be made available to the user.') .'</p>';
1531 $output .= theme('fieldset', array('#title' => t('Exposed Filters'), '#children' => $group,
1532 '#collapsible' => true, '#collapsed' => $collapsed,));
1533 $group = '';
1534
1535 // sort
1536 $collapsed = $allbut && $allbut != 'sort';
1537 $group = views_ui_render_section($form['sort'], array('name', 'sortorder', 'options'), array(t('Field'), t('Order'), t('Option')), 'sort criteria', $collapsed);
1538
1539 $group .= views_ui_render_section_add($form['sort']['add'], array('id', 'button'), 'Add criteria');
1540 $output .= theme('fieldset', array('#title' => t('Sort Criteria'), '#children' => $group,
1541 '#collapsible' => true, '#collapsed' => $collapsed,));
1542
1543 $output .= form_render($form, false);
1544 return $output;
1545 }
1546
1547 /**
1548 * Render one of the dynamic sections on our form.
1549 */
1550 function views_ui_render_section(&$form, $items, $header, $section, &$collapsed) {
1551 // always do the buttons
1552 $items = array_merge($items, array('delete', 'top', 'up', 'down', 'bottom'));
1553 $num_items = count($items);
1554
1555 $order = ($form['order']['#value'] != ''
1556 ? explode(',', $form['order']['#value'])
1557 : array());
1558
1559 $count = count($order) - 1;
1560 foreach ($order as $key => $i) {
1561 $row = array();
1562 foreach ($items as $item) {
1563 if (($key == 0 && ($item == 'up' || $item == 'top')) ||
1564 ($key == $count && ($item == 'down' || $item == 'bottom'))) {
1565 $form[$i][$item]['#printed'] = true;
1566 $row[] = '&nbsp;'; // array('data' => '&nbsp;', 'width' => 16);
1567 }
1568 else {
1569 $row[] = form_render($form[$i][$item], false);
1570 }
1571 }
1572 $rows[] = $row;
1573 if (isset($form[$i]['help'])) {
1574 $rows[] = array(array('data' => form_render($form[$i]['help']), 'colspan' => $num_items));
1575 }
1576 }
1577 if (!$rows) {
1578 $rows[] = array(array('data' => t('This view currently has no %s defined.', array('%s' => $section)), 'colspan' => $num_items));
1579 $collapsed = true;
1580 }
1581
1582 $header[] = array('data' => t('Ops'), 'colspan' => 5);
1583 return theme('table', $header, $rows) . "<br />";
1584 }
1585
1586 /**
1587 * Render the add buttons for one of the dynamic sections on our form.
1588 */
1589 function views_ui_render_section_add(&$form, $items, $label) {
1590 foreach ($items as $item) {
1591 $row[] = form_render($form[$item], false);
1592 }
1593 $rows[] = $row;
1594
1595 $header = array(array('data' => t($label), 'colspan' => count($items)));
1596 return theme('table', $header, $rows) . "<br />";
1597 }
1598
1599 /**
1600 * Validate that a view sent via form is OK.
1601 */
1602 function views_edit_view_validate($form_id, $view, $form) {
1603 $op = $_POST['op'];
1604 if ($op != t('Save')) {
1605 return; // only validate on saving!
1606 }
1607
1608 if ($view['vid']) {
1609 $changed = db_result(db_query("SELECT changed FROM {view_view} WHERE vid = %d", $view['vid']));
1610 if ($changed && $view['changed'] != $changed) {
1611 form_set_error('', t('Another user has modified this view, unable to save. You can get this error by using the Back button to re-edit a view after saving one; if you do this, be sure to Reload before making any changes!'));
1612 return;
1613 }
1614 }
1615
1616 if (!$view['name']) {
1617 form_error($form['basic-info']['name'], t('View name is required.'));
1618 }
1619
1620 // view name must be alphanumeric or underscores, no other punctuation.
1621 if (preg_match('/[^a-zA-Z0-9_]/', $view['name'])) {
1622 form_error($form['basic-info']['name'], t('View name must be alphanumeric or underscores only.'));
1623 }
1624
1625 // test uniqueness of name
1626 $vid = db_result(db_query("SELECT vid FROM {view_view} WHERE name='%s'", $view['name']));
1627 if ($vid && $vid != $view['vid']) {
1628 form_error($form['basic-info']['name'], t('View name already in use.'));
1629 }
1630
1631 if ($view['use_block'] && $view['nodes_per_block'] < 1) {
1632 form_set_error($form['block-info']['nodes_per_block'], t('If being used as a block, Nodes Per Block must be positive.'));
1633 }
1634
1635 // validation based on type:
1636
1637 $plugins = _views_get_style_plugins();
1638 if ($view['page']) {
1639 $validator = $plugins[$view['page_type']]['validate'];
1640 if (function_exists($validator)) {
1641 $validator('page', $view, $form);
1642 }
1643 }
1644
1645 if ($view['block']) {
1646 $validator = $plugins[$view['block_type']]['validate'];
1647 if (function_exists($validator)) {
1648 $validator('block', $view, $form);
1649 }
1650 }
1651
1652 foreach (array('field', 'argument', 'sort', 'filter') as $type) {
1653 $function = "_views_get_$type" . 's';
1654 $info = $function();
1655 if (is_array($view->$type)) {
1656 foreach ($view->$type as $key => $data) {
1657 if (!is_numeric($key)) {
1658 continue; // some non-data data is in here.
1659 }
1660 $validator = $info[$data['id']]['validate'];
1661 if (function_exists($validator)) {
1662 $validator($data, $view, $form);
1663 }
1664 }
1665 }
1666 }
1667 }
1668
1669 function views_ui_reorder(&$view) {
1670 // re-order things as they were ordered on the form.
1671 $order = $view['order'] != '' ? explode(',', $view['order']) : array();
1672 foreach ($order as $position => $key) {
1673 $placeholder[] = $view[$key];
1674 }
1675 $view = $placeholder;
1676 }
1677
1678 /**
1679 * Handle submit buttons on a view form.
1680 */
1681 function views_edit_view_submit($form_id, $form) {
1682 $view = (object) $form;
1683
1684 // Transform any data that requires it here.
1685 $view->access = array_keys(array_filter($view->access));
1686
1687 // ensure for MYSQL 5 irritation reasons.
1688 $view->nodes_per_page = intval($view->nodes_per_page);
1689 $view->nodes_per_block = intval($view->nodes_per_block);
1690 $view->menu_tab_weight = intval($view->menu_tab_weight);
1691
1692 // re-order things as they were ordered on the form.
1693 foreach (array('field', 'argument', 'filter', 'sort', 'exposed_filter') as $section) {
1694 views_ui_reorder($view->$section);
1695 }
1696
1697 _views_save_view($view);
1698 menu_rebuild();
1699 if ($form['vid']) {
1700 drupal_set_message(t('View successfully saved.'));
1701 }
1702 else {
1703 drupal_set_message(t('View successfully added.'));
1704 }
1705
1706 return 'admin/views';
1707 }
1708
1709 /**
1710 * Validate a view with a type: list.
1711 */
1712 function views_ui_plugin_validate_list($type, $view, $form) {
1713 // list (and table) modes require there to be at least 1 field active.
1714 if (is_array($view['field'])) {
1715 $fields = array_filter(array_keys($view['field']), 'is_numeric');
1716 }
1717 if (!$fields) {
1718 form_error($form["$type-info"][$type . '_type'], "List and Table types require at least one field.");
1719 }
1720 }
1721
1722 /**
1723 * Validate a view with type: table.
1724 */
1725 function views_ui_plugin_validate_table($type, $view, $form) {
1726 // pass thru to list cause for now they're the same.
1727 return views_ui_plugin_validate_list($type, $view, $form);
1728 }
1729 // ---------------------------------------------------------------------------
1730 // View creator tool
1731
1732 /*
1733 * Get a true/false.
1734 */
1735 function views_tf($val) {
1736 return ($val ? 'TRUE' : 'FALSE');
1737 }
1738
1739 /*
1740 * This function creates view code for a view.
1741 */
1742 function views_create_view_code($vid) {
1743 $view = _views_load_view($vid);
1744 if (!$view) {
1745 return "View '$vid' not found.";
1746 }
1747
1748 $requires = array();
1749
1750 $output = " \$view = new stdClass();\n";
1751 $output .= " \$view->name = '" . db_escape_string($view->name) . "';\n";
1752 $output .= " \$view->description = '" . db_escape_string($view->description) . "';\n";
1753 $output .= " \$view->access = " . var_export($view->access, true) . ";\n";
1754 if (user_access('use PHP for block visibility')) {
1755 $output .= " \$view->view_args_php = '" . check_plain($view->view_args_php) . "';\n";
1756 }
1757 // page items
1758 if ($view->page) {
1759 $output .= " \$view->page = " . views_tf($view->page) . ";\n";
1760 $output .= " \$view->page_title = '" . db_escape_string($view->page_title) . "';\n";
1761 $output .= " \$view->page_header = '" . db_escape_string($view->page_header) ."';\n";
1762 $output .= " \$view->page_header_format = '$view->page_header_format';\n";
1763 $output .= " \$view->page_footer = '" . db_escape_string($view->page_footer) ."';\n";
1764 $output .= " \$view->page_footer_format = '$view->page_footer_format';\n";
1765 $output .= " \$view->page_empty = '" . db_escape_string($view->page_empty) ."';\n";
1766 $output .= " \$view->page_empty_format = '$view->page_empty_format';\n";
1767 $output .= " \$view->page_type = '$view->page_type';\n";
1768 $output .= " \$view->url = '" . db_escape_string($view->url) . "';\n";
1769 $output .= " \$view->use_pager = " . views_tf($view->use_pager) . ";\n";
1770 $output .= " \$view->nodes_per_page = '$view->nodes_per_page';\n";
1771 if ($view->menu) {
1772 $output .= " \$view->menu = " . views_tf($view->menu) . ";\n";
1773 $output .= " \$view->menu_title = '" . db_escape_string($view->menu_title) . "';\n";
1774 $output .= " \$view->menu_tab = " . views_tf($view->menu_tab) . ";\n";
1775 $output .= " \$view->menu_tab_default = " . views_tf($view->menu_tab_default) . ";\n";
1776 $output .= " \$view->menu_weight = '$view->menu_weight';\n";
1777 }
1778 }
1779 if ($view->block) {
1780 $output .= " \$view->block = " . views_tf($view->block) . ";\n";
1781 $output .= " \$view->block_title = '" . db_escape_string($view->block_title) . "';\n";
1782 $output .= " \$view->block_header = '" . db_escape_string($view->block_header) . "';\n";
1783 $output .= " \$view->block_header_format = '$view->block_header_format';\n";
1784 $output .= " \$view->block_footer = '" . db_escape_string($view->block_footer) ."';\n";
1785 $output .= " \$view->block_footer_format = '$view->block_footer_format';\n";
1786 $output .= " \$view->block_empty = '" . db_escape_string($view->block_empty) ."';\n";
1787 $output .= " \$view->block_empty_format = '$view->block_empty_format';\n";
1788 $output .= " \$view->block_type = '$view->block_type';\n";
1789 $output .= " \$view->nodes_per_block = '$view->nodes_per_block';\n";
1790 $output .= " \$view->block_more = '$view->block_more';\n";
1791 $output .= " \$view->block_use_page_header = " . views_tf($view->block_use_page_header) . ";\n";
1792 $output .= " \$view->block_use_page_footer = " . views_tf($view->block_use_page_footer) . ";\n";
1793 $output .= " \$view->block_use_page_empty = " . views_tf($view->block_use_page_empty) . ";\n";
1794 }
1795
1796 $output .= " \$view->sort = array (\n";
1797 foreach ($view->sort as $sort) {
1798 $output .= " array (\n";
1799 $fieldbits = explode('.', $sort['field']);
1800 $output .= " 'tablename' => '$fieldbits[0]',\n";
1801 $output .= " 'field' => '$fieldbits[1]',\n";
1802 $output .= " 'sortorder' => '$sort[sortorder]',\n";
1803 $output .= " 'options' => '$sort[options]',\n";
1804 $output .= " ),\n";
1805 $requires[$fieldbits[0]] = 1;
1806 }
1807 $output .= " );\n";
1808
1809 $output .= " \$view->argument = array (\n";
1810 foreach ($view->argument as $argument) {
1811 $output .= " array (\n";
1812 $output .= " 'type' => '$argument[type]',\n";
1813 $output .= " 'argdefault' => '$argument[argdefault]',\n";
1814 $output .= " 'title' => '$argument[title]',\n";
1815 $output .= " 'options' => '$argument[options]',\n";
1816 $output .= " ),\n";
1817 }
1818 $output .= " );\n";
1819
1820 $output .= " \$view->field = array (\n";
1821 foreach ($view->field as $field) {
1822 $output .= " array (\n";
1823 $output .= " 'tablename' => '$field[tablename]',\n";
1824 $output .= " 'field' => '$field[field]',\n";
1825 $output .= " 'label' => '$field[label]',\n";
1826 if ($field['handler']) {
1827 $output .= " 'handler' => '$field[handler]',\n";
1828 }
1829 if ($field['sortable']) {
1830 $output .= " 'sortable' => '$field[sortable]',\n";
1831 }
1832 if ($field['defaultsort']) {
1833 $output .= " 'defaultsort' => '$field[defaultsort]',\n";
1834 }
1835 if ($field['options']) {
1836 $output .= " 'options' => '$field[options]',\n";
1837 }
1838 $output .= " ),\n";
1839 $requires[$field['tablename']] = 1;
1840 }
1841 $output .= " );\n";
1842
1843 $output .= " \$view->filter = array (\n";
1844 foreach ($view->filter as $filter) {
1845 $output .= " array (\n";
1846 $fieldbits = explode('.', $filter['field']);
1847 $output .= " 'tablename' => '$fieldbits[0]',\n";
1848 $output .= " 'field' => '$fieldbits[1]',\n";
1849 $output .= " 'operator' => '$filter[operator]',\n";
1850 $value = var_export($filter['value'], true);
1851 $output .= " 'options' => '$filter[options]',\n";
1852 $output .= " 'value' => $value,\n";
1853 $output .= " ),\n";
1854 $requires[$fieldbits[0]] = 1;
1855 }
1856 $output .= " );\n";
1857
1858 $output .= " \$view->exposed_filter = array (\n";
1859 foreach ($view->exposed_filter as $filter) {
1860 $output .= " array (\n";
1861 $fieldbits = explode('.', $filter['field']);
1862 $output .= " 'tablename' => '$fieldbits[0]',\n";
1863 $output .= " 'field' => '$fieldbits[1]',\n";
1864 $output .= " 'label' => '$filter[label]',\n";
1865 $output .= " 'optional' => $filter[optional],\n";
1866 $output .= " 'is_default' => $filter[is_default],\n";
1867 $output .= " 'operator' => $filter[operator],\n";
1868 $output .= " 'single' => $filter[single],\n";
1869 $output .= " ),\n";
1870 $requires[$fieldbits[0]] = 1;
1871 }
1872 $output .= " );\n";
1873 $output .= " \$view->requires = array(" . implode(', ', array_keys($requires)) . ");\n";
1874 $output .= " \$views[\$view->name] = \$view;\n";
1875 // no breadcrumb for now.
1876 return $output;
1877 }
1878
1879