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