4 // ---------------------------------------------------------------------------
8 * Implementation of hook_help()
10 function views_ui_help($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.');
19 if (!strncmp($section, 'admin/build/views', 11)) {
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')));
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.');
31 * Because the add/edit page is kind of complicated.
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>
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>
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";
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";
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";
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";
82 * Implementation of hook_perm()
84 function views_ui_perm() {
85 return array('administer views');
89 * Implementation of hook_menu()
91 function views_ui_menu($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
,
107 $items[] = array('path' => 'admin/build/views/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
);
152 // ---------------------------------------------------------------------------
153 // Administrative Pages
156 * This page lists all system views and provides links to edit them.
158 function views_ui_admin_page() {
163 drupal_set_title(t('administer views'));
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);
167 while ($view = db_fetch_object($result)) {
168 $url = ($view->page ?
l($view->url
, $view->url
) : t('No Page View'));
171 $provides[] = 'Page';
174 $provides[] = 'Block';
177 $provides[] = 'Menu';
181 views_get_title($view, 'menu'),
183 implode(', ', $provides),
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"),
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);
199 $output .
= t('<p>No views have currently been defined.</p>');
202 $result = db_query("SELECT name FROM {view_view}");
203 while ($view = db_fetch_object($result)) {
204 $used[$view->name
] = true
;
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>');
209 $default_views = _views_get_default_views();
211 $views_status = variable_get('views_defaults', array());
213 foreach ($default_views as
$view) {
214 $url = ($view->page ?
l($view->url
, $view->url
) : t('No Page View'));
216 if ($used[$view->name
]) {
217 $status = t('Overridden');
219 else if (isset($views_status[$view->name
])) {
220 if ($views_status[$view->name
] == 'enabled') {
221 $status = t('Enabled');
224 $status = t('Disabled');
227 else if ($view->disabled
) {
228 $status = t('Disabled');
231 $status = t('Enabled');
236 $provides[] = t('Page');
239 $provides[] = t('Block');
242 $provides[] = t('Menu');
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");
250 else if ($status == t('Disabled')) {
251 $links[] = array('title' => t('Enable'), 'href' => "admin/build/views/enable/$view->name");
254 $items[] = array($view->name
, views_get_title($view, 'menu'), $view->description
, implode(', ', $provides), $url, $status, theme('links', $links));
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'));
261 $output .
= t('<p>No views have currently been defined.</p>');
268 * Page to enable a disabled default view
270 function views_ui_admin_enable_page($view = '') {
274 $views_status = variable_get('views_defaults', array());
275 $views_status[$view] = 'enabled';
276 variable_set('views_defaults', $views_status);
279 drupal_goto('admin/build/views');
283 * Page to disable an enabled default view
285 function views_ui_admin_disable_page($view = '') {
289 $views_status = variable_get('views_defaults', array());
290 $views_status[$view] = 'disabled';
291 variable_set('views_defaults', $views_status);
294 drupal_goto('admin/build/views');
298 * Provide a textarea to paste a view export into.
300 function views_ui_admin_import_page() {
303 if ($_POST['form_id'] == 'views_edit_view') {
304 return views_ui_admin_add_page();
306 drupal_set_title("Import a View");
308 return drupal_get_form('views_ui_admin_import', $form);
311 function views_ui_admin_import() {
312 $form['view'] = array(
313 '#type' => 'textarea',
314 '#title' => t('Import View Code'),
317 '#description' => t('Cut and paste the results of an Export View here.'),
320 $form['submit'] = array(
322 '#value' => t("Submit"),
325 $form['#redirect'] = NULL
;
330 * Handle the submit button on importing a view.
332 function views_ui_admin_import_submit($formid, $form) {
337 $tables = array_keys(_views_get_tables());
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);
347 drupal_set_message(t("You don't seem to have the following requirements: ") .
implode(', ', array_diff($view->requires
, $tables)));
351 drupal_set_message(t('Unable to get a view out of that.'));
356 * Export a view for cut & paste.
358 function views_ui_admin_export_page($vid = '') {
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,
372 * Provide a form to add a view. Allow adding a view from default templates.
374 function views_ui_admin_add_page($template = NULL
) {
378 if ($op == t('Cancel')) {
379 return 'admin/build/views';
382 $view = _views_get_default_view($template);
384 drupal_set_title(t('Add a View'));
386 return drupal_get_form('views_edit_view', $view, $op);
390 * Provide a form to clone a view.
392 function views_ui_admin_clone_page($viewname) {
396 if ($op == t('Cancel')) {
397 return 'admin/build/views';
400 $view = views_load_view($viewname);
402 return drupal_not_found();
406 drupal_set_title(t('Add a View'));
408 return drupal_get_form('views_edit_view', $view, $op);
412 * Provide a form to edit a view.
414 function views_ui_admin_edit_page($vid = '') {
418 if ($op == t('Cancel')) {
419 drupal_goto('admin/build/views');
422 if ($op == t('Delete')) {
423 drupal_goto("admin/build/views/delete/$vid");
426 if (!($view = _views_load_view($vid))) {
427 drupal_goto('admin/build/views');
430 drupal_set_title(t('Edit view %n', array('%n' => $view->name
)));
431 return drupal_get_form('views_edit_view', $view, $op);
435 * Provide a form to confirm deletion of a view.
437 function views_ui_admin_delete_confirm($vid = '') {
438 $view = _views_load_view($vid);
441 return drupal_goto('admin/build/views');
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')
456 * Handle the submit button to delete a view.
458 function views_ui_admin_delete_confirm_submit($form_id, $form) {
459 _views_delete_view((object) $form);
461 drupal_goto('admin/build/views');
465 * Get an empty view with basic defaults.
467 function _views_get_default_view($template = '') {
469 $default_views = _views_get_default_views();
470 if (isset($default_views[$template])) {
471 $view = $default_views[$template];
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);
487 return _views_check_arrays($view);
490 // ---------------------------------------------------------------------------
491 // Select Box Definitions
493 // These should probably have string array keys that are easier to identify.
496 * Select box entries for argument defaults.
498 function _views_get_arguments_default() {
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'),
511 * Select box entries for sort ordering.
513 function _views_sortorders() {
515 'ASC' => t('Ascending'),
516 'DESC' => t('Descending')
521 * Swap two items in an array.
523 function _views_swap(&$arr, $a, $b) {
530 * Move an item up in an array.
532 function _views_move_up(&$arr, $i) {
533 if ($i <= 0 || $i >= count($arr)) {
534 return; // can't do it.
536 _views_swap($arr, $i - 1, $i);
540 * Move an item down in an array.
542 function _views_move_down(&$arr, $i) {
543 if ($i >= count($arr) - 1 || $i < 0) {
544 return; // can't do it.
546 _views_swap($arr, $i + 1, $i);
550 * Move an item to the front of an array.
552 function _views_move_top(&$arr, $i) {
553 if ($i <= 0 || $i >= count($arr)) {
554 return; // can't do it.
557 for ($x = $i; $x > 0; $x--)
558 $arr[$x] = $arr[$x - 1];
563 * Move an item to the end of an array.
565 function _views_move_bottom(&$arr, $i) {
566 $end = count($arr) - 1;
567 if ($i >= $end || $i < 0) {
568 return; // can't do it.
571 for ($x = $i; $x < $end; $x++)
572 $arr[$x] = $arr[$x + 1];
577 * Figure out which of the many, many buttons on a form were clicked and
580 function _views_check_sub_ops(&$form, &$order, $i) {
582 if ($form['delete']['#value']) {
583 unset($form['delete']['#value']);
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
;
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";
605 * Figure out if one of the add buttons on a form were clicked, and handle it.
607 function _views_check_ops(&$view, $op, $form) {
608 if ($op == t('Add Filter')) {
609 $view->new_filter
['id'] = $form['filter']['add']['id']['#value'];
612 else if ($op == t('Add Criteria')) {
613 $view->new_sort
['id'] = $form['sort']['add']['id']['#value'];
616 else if ($op == t('Add Argument')) {
617 $view->new_argument
['id'] = $form['argument']['add']['id']['#value'];
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]";
629 else if ($op == t('Expose Filter')) {
630 $view->new_exposed_filter
['id'] = $form['exposed_filter']['add']['id']['#value'];
636 * Custom form element to do our nice images.
638 function views_elements() {
639 $type['views_imagebutton'] = array('#input' => TRUE
, '#button_type' => 'submit',);
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";
647 function views_imagebutton_value() {
648 // null function guarantees default_value doesn't get moved to #value.
652 * Set up the dynamic #options on a widget
654 function views_ui_setup_widget($widget, $default_value, $argument = NULL
) {
659 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
660 $widget['#options'] = $widget['#options']('option', $argument);
662 if ($widget['#multiple'] && is_array($widget['#options'])) {
663 $widget['#size'] = min(count($widget['#options']), 8);
665 $widget['#default_value'] = $default_value;
670 * Display all the guts of a view in a form for editing.
672 function views_edit_view($view, $op = '') {
673 _views_check_arrays($view); // make sure arrays that might be empty get set
675 // Put in all our add buttons, then process them to see if they've been hit.
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'));
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.'));
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)) {
694 $form['vid'] = array(
696 '#value' => $view->vid
,
698 $form['allbut'] = array(
702 $form['changed'] = array(
704 '#value' => $view->changed
,
707 $form['basic-info'] = array(
708 '#type' => 'fieldset',
709 '#collapsible' => true
,
710 '#collapsed' => ($allbut != NULL
),
711 '#title' => t('Basic Information'),
714 $form['basic-info']['name'] = array(
715 '#type' => 'textfield',
716 '#title' => t('Name'),
717 '#default_value' => $view->name
,
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'),
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.'),
732 $form['basic-info']['description'] = array(
733 '#type' => 'textfield',
734 '#title' => t('Description'),
735 '#default_value' => $view->description
,
738 '#description' => t('A description of the view for the admin list.'),
743 $form['page-info'] = array(
744 '#type' => 'fieldset',
745 '#collapsible' => true
,
746 '#collapsed' => ($allbut != NULL
|| !$view->page
),
747 '#title' => t('Page'),
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.'),
758 $form['page-info']['url'] = array(
759 '#type' => 'textfield',
760 '#title' => t('URL'),
761 '#default_value' => $view->url
,
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\'. Note that any arguments listed here will be required, even if they are listed as optional below. You do not need to list arguments at the end of the path.'),
767 $form['page-info']['page_type'] = array(
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.'),
775 $form['page-info']['page_title'] = array(
776 '#type' => 'textfield',
777 '#title' => t('Title'),
778 '#default_value' => $view->page_title
,
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.'),
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.'),
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.'),
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
),
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
,
807 $form['page-info']['page_header_fieldset'] = array(
808 '#type' => 'fieldset',
809 '#collapsible' => TRUE
,
810 '#collapsed' => TRUE
,
811 '#title' => t('Header'),
813 $form['page-info']['page_header_fieldset']['page_header'] = array(
814 '#type' => 'textarea',
815 '#default_value' => $view->page_header
,
818 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
821 $form['page-info']['page_header_fieldset']['page_header_format'] = filter_form($view->page_header_format
, 1, array('page_header_format'));
823 $form['page-info']['page_footer_fieldset'] = array(
824 '#type' => 'fieldset',
825 '#collapsible' => TRUE
,
826 '#collapsed' => TRUE
,
827 '#title' => t('Footer'),
829 $form['page-info']['page_footer_fieldset']['page_footer'] = array(
830 '#type' => 'textarea',
831 '#default_value' => $view->page_footer
,
834 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
837 $form['page-info']['page_footer_fieldset']['page_footer_format'] = filter_form($view->page_footer_format
, 1, array('page_footer_format'));
839 $form['page-info']['page_empty_fieldset'] = array(
840 '#type' => 'fieldset',
841 '#collapsible' => TRUE
,
842 '#collapsed' => TRUE
,
843 '#title' => t('Empty Text'),
845 $form['page-info']['page_empty_fieldset']['page_empty'] = array(
846 '#type' => 'textarea',
847 '#default_value' => $view->page_empty
,
850 '#description' => t('Text to display if a view returns no nodes. Optional.'),
853 $form['page-info']['page_empty_fieldset']['page_empty_format'] = filter_form($view->page_empty_format
, 1, array('page_empty_format'));
855 $form['page-info']['menu-info'] = array(
856 '#type' => 'fieldset',
857 '#collapsible' => TRUE
,
858 '#collapsed' => TRUE
,
859 '#title' => t('Menu'),
862 $form['page-info']['menu-info']['menu'] = array(
863 '#type' => 'checkbox',
864 '#title' => t('Provide Menu'),
865 '#return_value' => 1,
866 '#default_value' => $view->menu
,
867 '#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.'),
870 $form['page-info']['menu-info']['menu_tab'] = array(
871 '#type' => 'checkbox',
872 '#title' => t('Provide Menu as Tab'),
873 '#return_value' => 1,
874 '#default_value' => $view->menu_tab
,
875 '#description' => t("If checked this view's menu entry will be provided as a tab rather than in the main menu system."),
878 $form['page-info']['menu-info']['menu_tab_default'] = array(
879 '#type' => 'checkbox',
880 '#title' => t('Make Default Menu Tab'),
881 '#return_value' => 1,
882 '#default_value' => $view->menu_tab_default
,
883 '#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."),
886 $form['page-info']['menu-info']['menu_tab_weight'] = array(
887 '#type' => 'textfield',
888 '#title' => t('Tab Weight'),
889 '#default_value' => $view->menu_tab_weight
,
891 '#description' => t('If this is a menu tab, select the weight; lower numbers will be further to the left.'),
894 $form['page-info']['menu-info']['menu_title'] = array(
895 '#type' => 'textfield',
896 '#title' => t('Menu Title'),
897 '#default_value' => $view->menu_title
,
900 '#description' => t('Enter the title to use for the menu entry or tab. If blank, the page title will be used.'),
905 $form['block-info'] = array(
906 '#type' => 'fieldset',
907 '#collapsible' => true
,
908 '#collapsed' => ($allbut != NULL
|| !$view->block
),
909 '#title' => t('Block'),
912 $form['block-info']['block'] = array(
913 '#type' => 'checkbox',
914 '#title' => t('Provide Block'),
915 '#return_value' => 1,
916 '#default_value' => $view->block
,
917 '#description' => t('If checked this view will be provided as a block. If checked title may not be blank.'),
920 $form['block-info']['block_type'] = array(
922 '#title' => t('View Type'),
923 '#default_value' => $view->block_type
,
924 '#options' => _views_get_style_plugins(true
),
925 '#description' => t('How the nodes should be displayed to the user.'),
928 $form['block-info']['block_title'] = array(
929 '#type' => 'textfield',
930 '#title' => t('Title'),
931 '#default_value' => $view->block_title
,
934 '#description' => t('The title that will be shown at the top of the block. May be blank.'),
937 $form['block-info']['nodes_per_block'] = array(
938 '#type' => 'textfield',
939 '#title' => t('Nodes per Block'),
940 '#default_value' => $view->nodes_per_block
,
943 '#description' => t('If using a block, the maximum number of items to display in the block. Pagers are not used in blocks.'),
944 '#attributes' => NULL
,
947 $form['block-info']['block_more'] = array(
948 '#type' => 'checkbox',
949 '#title' => t('[More] Link?'),
950 '#return_value' => 1,
951 '#default_value' => $view->block_more
,
952 '#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?'),
955 $form['block-info']['block_header_fieldset'] = array(
956 '#type' => 'fieldset',
957 '#collapsible' => TRUE
,
958 '#collapsed' => TRUE
,
959 '#title' => t('Header'),
961 $form['block-info']['block_header_fieldset']['block_use_page_header'] = array(
962 '#type' => 'checkbox',
963 '#title' => t('Use Page Header'),
964 '#return_value' => 1,
965 '#default_value' => $view->block_use_page_header
,
966 '#description' => t('If checked, use the Page Header for block view instead. If so, you should leave the Block Header blank.'),
969 $form['block-info']['block_header_fieldset']['block_header'] = array(
970 '#type' => 'textarea',
971 '#title' => t('Header'),
972 '#default_value' => $view->block_header
,
975 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
978 $form['block-info']['block_header_fieldset']['block_header_format'] = filter_form($view->block_header_format
, 1, array( 'block_header_format'));
980 $form['block-info']['block_footer_fieldset'] = array(
981 '#type' => 'fieldset',
982 '#collapsible' => TRUE
,
983 '#collapsed' => TRUE
,
984 '#title' => t('Footer'),
986 $form['block-info']['block_footer_fieldset']['block_use_page_footer'] = array(
987 '#type' => 'checkbox',
988 '#title' => t('Use Page Footer'),
989 '#return_value' => 1,
990 '#default_value' => $view->block_use_page_footer
,
991 '#description' => t('If checked, use the page footer for block view instead. If so, you should leave the block footer blank.'),
994 $form['block-info']['block_footer_fieldset']['block_footer'] = array(
995 '#type' => 'textarea',
996 '#title' => t('Footer'),
997 '#default_value' => $view->block_footer
,
1000 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
1003 $form['block-info']['block_footer_fieldset']['block_footer_format'] = filter_form($view->block_footer_format
, 1, array( 'block_footer_format'));
1005 $form['block-info']['block_empty_fieldset'] = array(
1006 '#type' => 'fieldset',
1007 '#collapsible' => TRUE
,
1008 '#collapsed' => TRUE
,
1009 '#title' => t('Empty text'),
1011 $form['block-info']['block_empty_fieldset']['block_use_page_empty'] = array(
1012 '#type' => 'checkbox',
1013 '#title' => t('Use Page empty'),
1014 '#return_value' => 1,
1015 '#default_value' => $view->block_use_page_empty
,
1016 '#description' => t('If checked, use the Page Empty Text for block view instead. If so, you should leave the block empty text blank.'),
1019 $form['block-info']['block_empty_fieldset']['block_empty'] = array(
1020 '#type' => 'textarea',
1021 '#title' => t('Empty text'),
1022 '#default_value' => $view->block_empty
,
1025 '#description' => t('Text to display if a view results in no nodes. Optional.'),
1028 $form['block-info']['block_empty_fieldset']['block_empty_format'] = filter_form($view->block_empty_format
, 1, array( 'block_empty_format'));
1030 $access = user_access('use PHP for block visibility');
1032 $form['view_args_php_fieldset'] = array(
1033 '#type' => 'fieldset',
1034 '#collapsible' => TRUE
,
1035 '#collapsed' => empty($view->view_args_php
) ? TRUE
: FALSE
,
1036 '#title' => t('Argument Handling Code'),
1039 $form['view_args_php_fieldset']['view_args_php'] = array(
1040 '#type' => 'textarea',
1041 '#title' => t('Argument Code'),
1042 '#default_value' => $view->view_args_php
,
1045 '#description' => '<p>'.
t('Advanced Usage Only: PHP code that returns a custom array of arguments for the view. Should not include <?php ?> delimiters.') .
'</p>' .
1046 '<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>',
1049 $form['view_args_php_fieldset']['view_args_php'] = array(
1051 '#value' => $view->view_args_php
,
1055 $form['save'] = array(
1056 '#type' => 'submit',
1057 '#value' => t('Save'),
1060 $form['delete'] = array(
1061 '#type' => 'submit',
1062 '#value' => t('Delete'),
1065 $form['cancel'] = array(
1066 '#type' => 'submit',
1067 '#value' => t('Cancel'),
1074 * separately build a piece of the form
1076 function views_ui_build_form(&$form) {
1077 // NOT REALLY SURE WHY THIS SORT OF WORKS. IT'S MAGIC THAT CHX POINTED ME TO
1078 // IN THE POLL MODULE.
1079 $form['#post'] = $_POST;
1080 $form = form_builder('views_edit_view', $form);
1084 * Add the 'add' button for a section into the form
1086 function views_ui_add_add_button(&$form, $section, $options, $label) {
1087 $form[$section]['add'] = array('#tree' => true
, '#parents' => array($section, 'add'));
1089 $form[$section]['add']['id'] = array(
1090 '#type' => 'select',
1091 '#options' => $options,
1093 $form[$section]['add']['button'] = array(
1094 '#type' => 'button',
1097 views_ui_build_form($form[$section]['add']);
1101 * Add one of the list sections to the form.
1103 function views_ui_add_section(&$form, &$view, $section) {
1104 // add fields to the form.
1106 $form += array('#tree' => true
, '#parents' => array($section));
1107 $view_section = &$view->$section;
1109 $num_items = count($view_section);
1110 // Populate the form with fields we need to check to track
1111 // changes through the form's life.
1112 $form['count'] = array('#type' => 'hidden', '#default_value' => $num_items);
1114 $form['order'] = array(
1115 '#type' => 'hidden',
1116 '#default_value' => $num_items ?
implode(',', range(0, $num_items - 1)) : '',
1119 // Run the builder to get the value on the form.
1120 views_ui_build_form($form);
1121 $order = $form['order']['#value'] != ''
1122 ?
explode(',', $form['order']['#value'])
1125 // Shortcut because operator precedence gets sticky if using $foo->$bar[]
1126 for ($i = $num_items; $i < $form['count']['#value']; $i++) {
1127 $view_section[] = array();
1130 $new_section = "new_$section";
1131 // instantiate the new field if one was added.
1132 if ($view->$new_section) {
1133 $view_section[] = $view->$new_section;
1134 $order[] = $form['count']['#value'];
1135 $form['count']['#value']++;
1138 $func = "views_ui_add_$section";
1140 foreach ($order as
$key => $i) {
1141 $form[$i] = array('#tree' => true
, '#parents' => array($section, $i));
1143 $form[$i]['id'] = array(
1144 '#type' => 'hidden',
1145 '#default_value' => $view_section[$i]['id'],
1148 views_ui_add_buttons($form[$i]);
1149 views_ui_build_form($form[$i]);
1151 $retval = _views_check_sub_ops($form[$i], $order, $key);
1152 if ($retval !== 'delete') {
1153 $retval2 = $func($form[$i], $view_section[$i], $order, $key, $i, $view);
1156 if ($retval || $retval2) {
1160 $form['order']['#value'] = implode(',', $order);
1166 * Add all the info for a single field into the form.
1168 function views_ui_add_field(&$form, $field, &$order, $key, $i) {
1169 $fields = _views_get_fields();
1170 $fieldname = $form['id']['#value']; // combined table & fieldname
1171 $fieldinfo = $fields[$fieldname]; // laziness + readability
1173 $form['fullname'] = array(
1174 '#type' => 'hidden',
1175 '#default_value' => $field['id'],
1178 $form['name'] = array(
1179 '#type' => 'markup',
1180 '#value' => $fieldinfo['name'], // combined table & fieldname
1182 $form['queryname'] = array(
1183 '#type' => 'hidden',
1184 '#default_value' => $field['queryname'],
1186 $form['tablename'] = array(
1187 '#type' => 'hidden',
1188 '#default_value' => $field['tablename'],
1190 $form['field'] = array(
1191 '#type' => 'hidden',
1192 '#default_value' => $field['field'],
1195 $form['label'] = array(
1196 '#type' => 'textfield',
1197 '#default_value' => $field['label'],
1199 '#maxlength' => 255,
1202 if (is_array($fieldinfo['handler'])) {
1203 $form['handler'] = array(
1204 '#type' => 'select',
1205 '#default_value' => $field['handler'],
1206 '#options' => $fieldinfo['handler'],
1210 if (isset($fieldinfo['option'])) {
1211 $form['options'] = views_ui_setup_widget($fieldinfo['option'], $field['options']);
1215 if ($fieldinfo['sortable']) {
1216 $form['sortable'] = array(
1217 '#type' => 'select',
1218 '#default_value' => $field['sortable'],
1219 '#options' => array('0' => t('No'), '1' => t('Yes')),
1221 $form['defaultsort'] = array(
1222 '#type' => 'select',
1223 '#default_value' => $field['defaultsort'],
1224 '#options' => array('0' => t('None'), 'ASC' => t('Ascending'), 'DESC' => t('Descending')),
1227 if (isset($fieldinfo['help'])) {
1228 $form['help'] = array(
1229 '#type' => 'markup',
1230 '#value' => '<div class="description">' .
$fieldinfo['help'] .
'</div>',
1236 * Add all the info for a single argument into the form.
1238 function views_ui_add_argument(&$form, $argument, &$order, $key, $i) {
1239 $arguments = _views_get_arguments();
1240 $arg_type = $form['id']['#value'];
1241 $arginfo = $arguments[$arg_type];
1243 $form['type'] = array(
1244 '#type' => 'hidden',
1245 '#default_value' => $argument['id'],
1248 $form['name'] = array(
1249 '#value' => $arginfo['name'],
1252 $form['argdefault'] = array(
1253 '#type' => 'select',
1254 '#default_value' => max(1, intval($argument['argdefault'])),
1255 '#options' => _views_get_arguments_default(),
1257 $form['title'] = array(
1258 '#type' => 'textfield',
1259 '#default_value' => $argument['title'],
1261 '#maxlength' => 255,
1263 if (isset($arginfo['option'])) {
1264 $form['options'] = views_ui_setup_widget($arginfo['option'], $argument['options']);
1266 $form['wildcard'] = array(
1267 '#type' => 'textfield',
1268 '#default_value' => $argument['wildcard'],
1272 $form['wildcard_substitution'] = array(
1273 '#type' => 'textfield',
1274 '#default_value' => $argument['wildcard_substitution'],
1278 if (isset($arginfo['help'])) {
1279 $form['help'] = array(
1280 '#type' => 'markup',
1281 '#value' => $arginfo['help'],
1287 * Add all the info for a single filter into the form.
1289 function views_ui_add_filter(&$form, $filter, &$order, $key, $i, &$view) {
1290 $filters = _views_get_filters();
1291 $filtername = $form['id']['#value']; // combined table & filtername
1292 $filterinfo = $filters[$filtername];
1294 $form['field'] = array(
1295 '#type' => 'hidden',
1296 '#default_value' => $filter['id'],
1299 $form['name'] = array(
1300 '#type' => 'markup',
1301 '#value' => $filterinfo['name'],
1304 // 'operator' is either an array or a handler
1305 $operator = $filterinfo['operator'];
1306 if (!is_array($operator) && function_exists($filterinfo['operator'])) {
1307 $operator = $filterinfo['operator']('operator', $filterinfo);
1310 $form['operator'] = array(
1311 '#type' => 'select',
1312 '#default_value' => $filter['operator'],
1313 '#options' => $operator,
1315 if (isset($filterinfo['option'])) {
1316 $form['options'] = views_ui_setup_widget($filterinfo['option'], $filter['options']);
1319 $form['value'] = views_ui_setup_widget($filterinfo['value'], $filter['value'], $filterinfo);
1321 if (isset($filterinfo['help'])) {
1322 $form['help'] = array(
1323 '#type' => 'markup',
1324 '#value' => $filterinfo['help'],
1328 // This is kind of a pain in the butt -- The only way to know all of
1329 // the filters that have been exposed is to look at the form, but that
1330 // part of the form hasn't been built yet, so I can't use the form_builder
1331 // method. I can't put this after that, either, because then I'd have to
1332 // go back to add one if we see one here. So we cheat heavily with _POST.
1334 if ($_POST['edit']) {
1335 $exposed_order = $_POST['edit']['exposed_filter']['order'];
1336 if ($exposed_order !== '') {
1337 $exposed_order = explode(',', $exposed_order);
1338 foreach ($exposed_order as
$x) {
1339 if ($_POST['edit']['exposed_filter'][$x]['id'] == $filtername) {
1346 else { // do this if there's never been a submit.
1347 $exposed_order = array_keys($view->exposed_filter
);
1348 foreach ($exposed_order as
$x) {
1349 if ($view->exposed_filter
[$x]['id'] == $filtername) {
1356 // I can't find a way to see if this button was pressed other than
1357 // directly checking, because #form_submitted doesn't work on arrays
1359 if ($_POST['edit']['filter'][$i]['expose']) {
1360 $view->new_exposed_filter
['id'] = $filtername;
1364 $form['expose'] = array(
1365 '#type' => 'button',
1366 '#default_value' => t('Expose'),
1367 '#name' => "edit[filter][$i][expose]",
1374 * Add all the info for a single exposed filter into the form.
1376 function views_ui_add_exposed_filter(&$form, $filter, &$order, $key, $i) {
1377 $filters = _views_get_filters();
1378 $filtername = $form['id']['#value']; // combined table & filtername
1379 $filterinfo = $filters[$filtername];
1381 $form['field'] = array(
1382 '#type' => 'hidden',
1383 '#default_value' => $filter['id'],
1386 $form['name'] = array(
1387 '#type' => 'markup',
1388 '#value' => $filterinfo['name'],
1391 $form['label'] = array(
1392 '#type' => 'textfield',
1393 '#default_value' => $filter['label'],
1395 '#maxlength' => 255,
1398 $form['optional'] = array(
1399 '#type' => 'checkbox',
1400 '#default_value' => $filter['optional'],
1403 $form['is_default'] = array(
1404 '#type' => 'checkbox',
1405 '#default_value' => $filter['is_default'],
1408 $form['single'] = array(
1409 '#type' => 'checkbox',
1410 '#default_value' => $filter['single'],
1413 $form['operator'] = array(
1414 '#type' => 'checkbox',
1415 '#default_value' => $filter['operator'],
1420 * Add all the info for a single sort into the form.
1422 function views_ui_add_sort(&$form, $sort, &$order, $key, $i) {
1423 $sorts = _views_get_sorts();
1424 $sortname = $form['id']['#value']; // combined table & sortname
1425 $sortinfo = $sorts[$sortname];
1427 $form['field'] = array(
1428 '#type' => 'hidden',
1429 '#default_value' => $sort['id'],
1432 $form['name'] = array(
1433 '#value' => $sortinfo['name'],
1436 $form['sortorder'] = array(
1437 '#type' => 'select',
1439 '#default_value' => $sort['sortorder'],
1440 '#options' => _views_sortorders(),
1442 // option can be either 'string', 'integer', an array or a handler
1443 // that must return an array.
1444 if (isset($sortinfo['option'])) {
1445 $form['options'] = views_ui_setup_widget($sortinfo['option'], $sort['options']);
1447 if (isset($sortinfo['help'])) {
1448 $form['help'] = array(
1449 '#type' => 'markup',
1450 '#value' => $sortinfo['help'],
1456 * Add the up, down, top, bottom, delete buttons to a form.
1458 function views_ui_add_buttons(&$form_item) {
1459 $form_item['delete'] = views_ui_add_button('user-trash.png', t('Delete'), t('Delete this item.'));
1460 $form_item['up'] = views_ui_add_button('go-up.png', t('Up'), t('Move this item up.'));
1461 $form_item['down'] = views_ui_add_button('go-down.png', t('Down'), t('Move this item down.'));
1462 $form_item['top'] = views_ui_add_button('go-top.png', t('Top'), t('Move this item to the top.'));
1463 $form_item['bottom'] = views_ui_add_button('go-bottom.png', t('Bottom'), t('Move this item to the bottom.'));
1467 * Add a single button to a form.
1469 function views_ui_add_button($image, $name, $text) {
1470 $module_path = base_path() .
drupal_get_path('module', 'views');
1473 '#type' => 'views_imagebutton',
1474 '#image' => $module_path .
'/' .
$image,
1476 '#default_value' => $name,
1481 * Display the form built by _views_view_form()
1483 function theme_views_edit_view($form) {
1485 $output .
= drupal_render($form['basic-info'], false
);
1486 $output .
= drupal_render($form['page-info'], false
);
1487 $output .
= drupal_render($form['block-info'], false
);
1489 $allbut = $form['allbut']['#value'];
1491 // drupal_render wants a reference so use full array notation.
1494 $collapsed = $allbut && $allbut != 'field';
1495 $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);
1496 $group .
= views_ui_render_section_add($form['field']['add'], array('id', 'button'), t('Add Field'));
1498 $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>';
1499 $output .
= theme('fieldset', array('#title' => t('Fields'), '#children' => $group,
1500 '#collapsible' => true
, '#collapsed' => $collapsed,));
1504 $collapsed = $allbut && $allbut != 'argument';
1505 $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 $group .
= views_ui_render_section_add($form['argument']['add'], array('id', 'button'), t('Add Argument'));
1509 $group .
= drupal_render($form['view_args_php_fieldset']);
1511 $group .
= '<p>'.
t('Arguments are parsed directly from the URL. They are not necessary to any given view, but allow flexibility.') .
'</p>';
1512 $output .
= theme('fieldset', array('#title' => t('Arguments'), '#children' => $group,
1513 '#collapsible' => true
, '#collapsed' => $collapsed,));
1516 $collapsed = $allbut && $allbut != 'filter';
1517 $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 $group .
= views_ui_render_section_add($form['filter']['add'], array('id', 'button'), t('Add Filter'));
1521 $group .
= '<p>'.
t('Filters allow you to select a subset of all the nodes to display. All Filters are ANDed together.') .
'</p>';
1522 $output .
= theme('fieldset', array('#title' => t('Filters'), '#children' => $group,
1523 '#collapsible' => true
, '#collapsed' => $collapsed,));
1526 // exposed exposed_filter
1527 $collapsed = $allbut && $allbut != 'exposed_filter';
1528 $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);
1531 // $group .= views_ui_render_section_add($form['exposed_filter']['add'], array('id', 'button'), t('Expose Filter'));
1533 $group .
= '<p>'.
t('Exposed filters will be presented to the viewer. If not set required, then filters will include a "<None>" 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>';
1534 $output .
= theme('fieldset', array('#title' => t('Exposed Filters'), '#children' => $group,
1535 '#collapsible' => true
, '#collapsed' => $collapsed,));
1539 $collapsed = $allbut && $allbut != 'sort';
1540 $group = views_ui_render_section($form['sort'], array('name', 'sortorder', 'options'), array(t('Field'), t('Order'), t('Option')), 'sort criteria', $collapsed);
1542 $group .
= views_ui_render_section_add($form['sort']['add'], array('id', 'button'), t('Add criteria'));
1543 $output .
= theme('fieldset', array('#title' => t('Sort Criteria'), '#children' => $group,
1544 '#collapsible' => true
, '#collapsed' => $collapsed,));
1546 $output .
= drupal_render($form, false
);
1551 * Render one of the dynamic sections on our form.
1553 function views_ui_render_section(&$form, $items, $header, $section, &$collapsed) {
1554 // always do the buttons
1555 $items = array_merge($items, array('delete', 'top', 'up', 'down', 'bottom'));
1556 $num_items = count($items);
1558 $order = ($form['order']['#value'] != ''
1559 ?
explode(',', $form['order']['#value'])
1562 $count = count($order) - 1;
1563 foreach ($order as
$key => $i) {
1565 foreach ($items as
$item) {
1566 if (($key == 0 && ($item == 'up' || $item == 'top')) ||
1567 ($key == $count && ($item == 'down' || $item == 'bottom'))) {
1568 $form[$i][$item]['#printed'] = true
;
1569 $row[] = ' '; // array('data' => ' ', 'width' => 16);
1572 $row[] = drupal_render($form[$i][$item], false
);
1576 if (isset($form[$i]['help'])) {
1577 $rows[] = array(array('data' => drupal_render($form[$i]['help']), 'colspan' => $num_items));
1581 $rows[] = array(array('data' => t('This view currently has no %s defined.', array('%s' => $section)), 'colspan' => $num_items));
1585 $header[] = array('data' => t('Ops'), 'colspan' => 5);
1586 return theme('table', $header, $rows) .
"<br />";
1590 * Render the add buttons for one of the dynamic sections on our form.
1592 function views_ui_render_section_add(&$form, $items, $label) {
1593 foreach ($items as
$item) {
1594 $row[] = drupal_render($form[$item], false
);
1598 $header = array(array('data' => $label, 'colspan' => count($items)));
1599 return theme('table', $header, $rows) .
"<br />";
1603 * Validate that a view sent via form is OK.
1605 function views_edit_view_validate($form_id, $view, $form) {
1607 if ($op != t('Save')) {
1608 return; // only validate on saving!
1612 $changed = db_result(db_query("SELECT changed FROM {view_view} WHERE vid = %d", $view['vid']));
1613 if ($changed && $view['changed'] != $changed) {
1614 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!'));
1619 if (!$view['name']) {
1620 form_error($form['basic-info']['name'], t('View name is required.'));
1623 // view name must be alphanumeric or underscores, no other punctuation.
1624 if (preg_match('/[^a-zA-Z0-9_]/', $view['name'])) {
1625 form_error($form['basic-info']['name'], t('View name must be alphanumeric or underscores only.'));
1628 // test uniqueness of name
1629 $vid = db_result(db_query("SELECT vid FROM {view_view} WHERE name='%s'", $view['name']));
1630 if ($vid && $vid != $view['vid']) {
1631 form_error($form['basic-info']['name'], t('View name already in use.'));
1634 if ($view['page']) {
1635 if ($view['use_pager'] && !$view['nodes_per_page']) {
1636 form_error($form['page-info']['nodes_per_page'], t('Nodes per page cannot be 0 if using the pager.'));
1640 if ($view['block'] && $view['nodes_per_block'] < 1) {
1641 form_error($form['block-info']['nodes_per_block'], t('If being used as a block, Nodes Per Block must be positive.'));
1644 // validation based on type:
1646 $plugins = _views_get_style_plugins();
1647 if ($view['page']) {
1648 $validator = $plugins[$view['page_type']]['validate'];
1649 if (function_exists($validator)) {
1650 $validator('page', $view, $form);
1654 if ($view['block']) {
1655 $validator = $plugins[$view['block_type']]['validate'];
1656 if (function_exists($validator)) {
1657 $validator('block', $view, $form);
1661 foreach (array('field', 'argument', 'sort', 'filter') as
$type) {
1662 $function = "_views_get_$type" .
's';
1663 $info = $function();
1664 if (is_array($view->$type)) {
1665 foreach ($view->$type as
$key => $data) {
1666 if (!is_numeric($key)) {
1667 continue; // some non-data data is in here.
1669 $validator = $info[$data['id']]['validate'];
1670 if (function_exists($validator)) {
1671 $validator($data, $view, $form);
1678 function views_ui_reorder(&$view) {
1679 // re-order things as they were ordered on the form.
1680 $order = $view['order'] != '' ?
explode(',', $view['order']) : array();
1681 foreach ($order as
$position => $key) {
1682 $placeholder[] = $view[$key];
1684 $view = $placeholder;
1688 * Handle submit buttons on a view form.
1690 function views_edit_view_submit($form_id, $form) {
1691 $view = (object) $form;
1693 // Transform any data that requires it here.
1694 $view->access
= array_keys(array_filter($view->access
));
1696 // ensure for MYSQL 5 irritation reasons.
1697 $view->nodes_per_page
= intval($view->nodes_per_page
);
1698 $view->nodes_per_block
= intval($view->nodes_per_block
);
1699 $view->menu_tab_weight
= intval($view->menu_tab_weight
);
1701 // re-order things as they were ordered on the form.
1702 foreach (array('field', 'argument', 'filter', 'sort', 'exposed_filter') as
$section) {
1703 views_ui_reorder($view->$section);
1706 _views_save_view($view);
1709 drupal_set_message(t('View successfully saved.'));
1712 drupal_set_message(t('View successfully added.'));
1715 return 'admin/build/views';
1719 * Validate a view with a type: list.
1721 function views_ui_plugin_validate_list($type, $view, $form) {
1722 // list (and table) modes require there to be at least 1 field active.
1723 if (is_array($view['field'])) {
1724 $fields = array_filter(array_keys($view['field']), 'is_numeric');
1727 form_error($form["$type-info"][$type .
'_type'], t('List and Table types require at least one field.'));
1730 if (isset($view['field']['count'])) {
1731 $defaultsort = false
;
1732 for ($i = 0; $i < $view['field']['count']; $i++) {
1733 if ($view['field'][$i]['defaultsort']) {
1735 form_error($form['field'][$i]['defaultsort'], t('You can only set on Default Sort on one field.'));
1738 $defaultsort = true
;
1745 * Validate a view with type: table.
1747 function views_ui_plugin_validate_table($type, $view, $form) {
1748 // pass thru to list cause for now they're the same.
1749 return views_ui_plugin_validate_list($type, $view, $form);
1751 // ---------------------------------------------------------------------------
1752 // View creator tool
1757 function views_tf($val) {
1758 return ($val ?
'TRUE' : 'FALSE');
1762 * This function creates view code for a view.
1764 function views_create_view_code($vid) {
1765 $view = _views_load_view($vid);
1767 return t("View '%vid' not found.", array('%vid' => $vid));
1770 $requires = array();
1772 $output = " \$view = new stdClass();\n";
1773 $output .
= " \$view->name = " .
var_export($view->name
, true
) .
";\n";
1774 $output .
= " \$view->description = " .
var_export($view->description
, true
) .
";\n";
1775 $output .
= " \$view->access = " .
var_export($view->access
, true
) .
";\n";
1776 if (user_access('use PHP for block visibility')) {
1777 $output .
= " \$view->view_args_php = " .
var_export($view->view_args_php
, true
) .
";\n";
1781 $output .
= " \$view->page = " .
views_tf($view->page
) .
";\n";
1782 $output .
= " \$view->page_title = " .
var_export($view->page_title
, true
) .
";\n";
1783 $output .
= " \$view->page_header = " .
var_export($view->page_header
, true
) .
";\n";
1784 $output .
= " \$view->page_header_format = " .
var_export($view->page_header_format
, true
) .
";\n";
1785 $output .
= " \$view->page_footer = " .
var_export($view->page_footer
, true
) .
";\n";
1786 $output .
= " \$view->page_footer_format = " .
var_export($view->page_footer_format
, true
) .
";\n";
1787 $output .
= " \$view->page_empty = " .
var_export($view->page_empty
, true
) .
";\n";
1788 $output .
= " \$view->page_empty_format = " .
var_export($view->page_empty_format
, true
) .
";\n";
1789 $output .
= " \$view->page_type = " .
var_export($view->page_type
, true
) .
";\n";
1790 $output .
= " \$view->url = " .
var_export($view->url
, true
) .
";\n";
1791 $output .
= " \$view->use_pager = " .
views_tf($view->use_pager
) .
";\n";
1792 $output .
= " \$view->nodes_per_page = " .
var_export($view->nodes_per_page
, true
) .
";\n";
1794 $output .
= " \$view->menu = " .
views_tf($view->menu
) .
";\n";
1795 $output .
= " \$view->menu_title = " .
var_export($view->menu_title
, true
) .
";\n";
1796 $output .
= " \$view->menu_tab = " .
views_tf($view->menu_tab
) .
";\n";
1797 $output .
= " \$view->menu_tab_default = " .
views_tf($view->menu_tab_default
) .
";\n";
1798 $output .
= " \$view->menu_tab_weight = " .
var_export($view->menu_tab_weight
, true
) .
";\n";
1802 $output .
= " \$view->block = " .
views_tf($view->block
) .
";\n";
1803 $output .
= " \$view->block_title = " .
var_export($view->block_title
, true
) .
";\n";
1804 $output .
= " \$view->block_header = " .
var_export($view->block_header
, true
) .
";\n";
1805 $output .
= " \$view->block_header_format = " .
var_export($view->block_header_format
, true
) .
";\n";
1806 $output .
= " \$view->block_footer = " .
var_export($view->block_footer
, true
) .
";\n";
1807 $output .
= " \$view->block_footer_format = " .
var_export($view->block_footer_format
, true
) .
";\n";
1808 $output .
= " \$view->block_empty = " .
var_export($view->block_empty
, true
) .
";\n";
1809 $output .
= " \$view->block_empty_format = " .
var_export($view->block_empty_format
, true
) .
";\n";
1810 $output .
= " \$view->block_type = " .
var_export($view->block_type
, true
) .
";\n";
1811 $output .
= " \$view->nodes_per_block = " .
var_export($view->nodes_per_block
, true
) .
";\n";
1812 $output .
= " \$view->block_more = " .
views_tf($view->block_more
) .
";\n";
1813 $output .
= " \$view->block_use_page_header = " .
views_tf($view->block_use_page_header
) .
";\n";
1814 $output .
= " \$view->block_use_page_footer = " .
views_tf($view->block_use_page_footer
) .
";\n";
1815 $output .
= " \$view->block_use_page_empty = " .
views_tf($view->block_use_page_empty
) .
";\n";
1818 $output .
= " \$view->sort = array (\n";
1819 foreach ($view->sort as
$sort) {
1820 $output .
= " array (\n";
1821 $fieldbits = explode('.', $sort['field']);
1822 $output .
= " 'tablename' => " .
var_export($fieldbits[0], true
) .
",\n";
1823 $output .
= " 'field' => " .
var_export($fieldbits[1], true
) .
",\n";
1824 $output .
= " 'sortorder' => " .
var_export($sort['sortorder'], true
) .
",\n";
1825 $output .
= " 'options' => " .
var_export($sort['options'], true
) .
",\n";
1827 $requires[$fieldbits[0]] = 1;
1831 $output .
= " \$view->argument = array (\n";
1832 foreach ($view->argument as
$argument) {
1833 $output .
= " array (\n";
1834 $output .
= " 'type' => " .
var_export($argument['type'], true
) .
",\n";
1835 $output .
= " 'argdefault' => " .
var_export($argument['argdefault'], true
) .
",\n";
1836 $output .
= " 'title' => " .
var_export($argument['title'], true
) .
",\n";
1837 $output .
= " 'options' => " .
var_export($argument['options'], true
) .
",\n";
1838 $output .
= " 'wildcard' => " .
var_export($argument['wildcard'], true
) .
",\n";
1839 $output .
= " 'wildcard_substitution' => " .
var_export($argument['wildcard_substitution'], true
) .
",\n";
1844 $output .
= " \$view->field = array (\n";
1845 foreach ($view->field as
$field) {
1846 $output .
= " array (\n";
1847 $output .
= " 'tablename' => " .
var_export($field['tablename'], true
) .
",\n";
1848 $output .
= " 'field' => " .
var_export($field['field'], true
) .
",\n";
1849 $output .
= " 'label' => " .
var_export($field['label'], true
) .
",\n";
1850 if ($field['handler']) {
1851 $output .
= " 'handler' => " .
var_export($field['handler'], true
) .
",\n";
1853 if ($field['sortable']) {
1854 $output .
= " 'sortable' => " .
var_export($field['sortable'], true
) .
",\n";
1856 if ($field['defaultsort']) {
1857 $output .
= " 'defaultsort' => " .
var_export($field['defaultsort'], true
) .
",\n";
1859 if ($field['options']) {
1860 $output .
= " 'options' => " .
var_export($field['options'], true
) .
",\n";
1863 $requires[$field['tablename']] = 1;
1867 $output .
= " \$view->filter = array (\n";
1868 foreach ($view->filter as
$filter) {
1869 $output .
= " array (\n";
1870 $fieldbits = explode('.', $filter['field']);
1871 $output .
= " 'tablename' => " .
var_export($fieldbits[0], true
) .
",\n";
1872 $output .
= " 'field' => " .
var_export($fieldbits[1], true
) .
",\n";
1873 $output .
= " 'operator' => " .
var_export($filter['operator'], true
) .
",\n";
1874 $output .
= " 'options' => " .
var_export($filter['options'], true
) .
",\n";
1875 $output .
= " 'value' => " .
var_export($filter['value'], true
) .
",\n";
1877 $requires[$fieldbits[0]] = 1;
1881 $output .
= " \$view->exposed_filter = array (\n";
1882 foreach ($view->exposed_filter as
$filter) {
1883 $output .
= " array (\n";
1884 $fieldbits = explode('.', $filter['field']);
1885 $output .
= " 'tablename' => " .
var_export($fieldbits[0], true
) .
",\n";
1886 $output .
= " 'field' => " .
var_export($fieldbits[1], true
) .
",\n";
1887 $output .
= " 'label' => " .
var_export($filter['label'], true
) .
",\n";
1888 $output .
= " 'optional' => " .
var_export($filter['optional'], true
) .
",\n";
1889 $output .
= " 'is_default' => " .
var_export($filter['is_default'], true
) .
",\n";
1890 $output .
= " 'operator' => " .
var_export($filter['operator'], true
) .
",\n";
1891 $output .
= " 'single' => " .
var_export($filter['single'], true
) .
",\n";
1893 $requires[$fieldbits[0]] = 1;
1896 $output .
= " \$view->requires = array(" .
implode(', ', array_keys($requires)) .
");\n";
1897 $output .
= " \$views[\$view->name] = \$view;\n";