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