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/modules#description':
15 return t('The views module creates customized views of node lists.');
16 case
'admin/views/import':
17 return t('You may import a view by cut-and-pasting the results of an export view. If the import is successful you will be taken to the Add View page with all of the settings of the imported view..');
19 return t('This screen shows all of the views that are currently defined in your system. The default views are provided by Views and other modules and are automatically available. If a customized view of the same name exists, it will be used in place of a default view.');
21 if (!strncmp($section, 'admin/views', 11)) {
25 return t('Please see %s or the views documentation on drupal.org for help here.', array('%s' => l(t('the views help page'), 'admin/help/views_ui')));
27 return t('You may cut & paste this view into an import function on another system. The view will only work if all modules required by the view are installed on the target location.');
33 * Because the add/edit page is kind of complicated.
35 function _views_ui_help_add() {
36 $output = t('<p>A view retrieves some number of nodes from the database and displays them in a variety of formats.</p>');
37 $output .
= t("<h3>View Types</h3>
39 <dt><em>List View</em></dt><dd>A List View provides the data for each node retrieved in the form of an unordered list. Each item in the Fields section will be displayed; the Title will be displayed as a label. The order the items appear in the Fields list is the order the items will appear in the output. Leaving the title blank will cause the field to appear with no label (which is desirable in lists that just display titles, for example).</dd>
40 <dt><em>Table View</em></dt><dd>A Table View provides the data for each node as one row of a table. The Fields selected in the Fields list will be displayed in the order they are listed. The title column will be shown in the header. If you set the field to 'sortable' then the header will be click-sortable; be careful here as click-sorts will be processed after built-in sort criteria, and built-in sort criteria can easily make click-sorts invalid. If using click-sorts, choose a field to be the default sort; otherwise the first field presented will be the default sort.</dd>
41 <dt><em>Teaser List</em></dt><dd>A Teaser List will simply present the teaser of each node retrieved.</dd>
42 <dt><em>Full Nodes</em></dt><dd>This will show the full content of each node retrieved.</dd>
43 <dt><em>Random Teaser</em></dt><dd>This will show a single random teaser.</dd>
44 <dt><em>Random Node</em></dt><dd>This will show a single random node's full view.</dd>
47 $output .
= t("<h3>Fields</h3>\n");
48 $output .
= t("<p>When using List or Table view, it is necessary to choose which fields will be displayed to the user.</p><dl>\n");
49 $fields = _views_get_fields();
50 foreach ($fields as
$field) {
51 $output .
= "<dt><em>$field[name]</em></dt><dd>$field[help]</dd>\n";
55 $output .
= t("<h3>Arguments</h3>\n");
56 $output .
= t("<p>Arguments can be passed to the View through the URL, in order to create views that are configurable by the user. This is very useful to create views for taxonomy, or to sort by user. When using arguments, substitution is performed on the title. %1 will represent argument 1, %2 will represent argument 2. Each argument has a title field; this title is used if providing a summary view (which can matter because the argument is missing which could result in confusing phrases such as 'view for')</p><dl>\n");
57 $arguments = _views_get_arguments();
58 foreach ($arguments as
$argument) {
59 $output .
= "<dt><em>$argument[name]</em></dt><dd>$argument[help]</dd>\n";
63 $output .
= t("<h3>Filters</h3>\n");
64 $output .
= t("<p>Views may be filtered to restrict the view on a number of criteria.</p><dl>\n");
65 $filters = _views_get_filters();
66 foreach ($filters as
$filter) {
67 $output .
= "<dt><em>$filter[name]</em></dt><dd>$filter[help]</dd>\n";
71 $output .
= t("<h3>Sorting Critera</h3>\n");
72 $output .
= t("<p>The result set may be sorted on any of the following criteria.</p><dl>\n");
73 $sorts = _views_get_sorts();
74 foreach ($sorts as
$sort) {
75 $output .
= "<dt><em>$sort[name]</em></dt><dd>$sort[help]</dd>\n";
83 * Implementation of hook_perm()
85 function views_ui_perm() {
86 return array('administer views');
90 * Implementation of hook_menu()
92 function views_ui_menu($may_cache) {
96 $items[] = array('path' => 'admin/views',
97 'title' => t('views'),
98 'callback' => 'views_ui_admin_page',
99 'access' => user_access('administer views'),
100 'type' => MENU_NORMAL_ITEM
);
101 $items[] = array('path' => 'admin/views/list',
102 'title' => t('list'),
103 'callback' => 'views_ui_admin_page',
104 'access' => user_access('administer views'),
105 'type' => MENU_DEFAULT_LOCAL_TASK
,
107 $items[] = array('path' => 'admin/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/views/clone',
113 'title' => t('clone'),
114 'callback' => 'views_ui_admin_clone_page',
115 'access' => user_access('administer views'),
116 'type' => MENU_CALLBACK
);
117 $items[] = array('path' => 'admin/views/edit',
118 'title' => t('edit view'),
119 'callback' => 'views_ui_admin_edit_page',
120 'access' => user_access('administer views'),
121 'type' => MENU_CALLBACK
);
122 $items[] = array('path' => 'admin/views/import',
123 'title' => t('import'),
124 'callback' => 'views_ui_admin_import_page',
125 'access' => user_access('administer views'),
126 'type' => MENU_LOCAL_TASK
);
127 $items[] = array('path' => 'admin/views/export',
128 'title' => t('export view'),
129 'callback' => 'views_ui_admin_export_page',
130 'access' => user_access('administer views'),
131 'type' => MENU_CALLBACK
);
132 $items[] = array('path' => 'admin/views/delete',
133 'title' => t('edit view'),
134 'callback' => 'views_ui_admin_delete_page',
135 'access' => user_access('administer views'),
136 'type' => MENU_CALLBACK
);
137 $items[] = array('path' => 'admin/views/enable',
138 'callback' => 'views_ui_admin_enable_page',
139 'access' => user_access('administer views'),
140 'type' => MENU_CALLBACK
);
141 $items[] = array('path' => 'admin/views/disable',
142 'callback' => 'views_ui_admin_disable_page',
143 'access' => user_access('administer views'),
144 'type' => MENU_CALLBACK
);
151 * hunmonk's module dependency check: see http://drupal.org/node/54463
153 function views_ui_form_alter($form_id, &$form) {
154 if ($form_id == 'system_modules' && !$_POST) {
155 views_ui_system_module_validate($form);
160 * hunmonk's module dependency check: see http://drupal.org/node/54463
162 function views_ui_system_module_validate(&$form) {
163 $module = 'views_ui';
164 $dependencies = array('views');
165 foreach ($dependencies as
$dependency) {
166 if (!in_array($dependency, $form['status']['#default_value'])) {
167 $missing_dependency = TRUE
;
168 $missing_dependency_list[] = $dependency;
171 if (in_array($module, $form['status']['#default_value']) && isset($missing_dependency)) {
172 db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module);
173 $key = array_search($module, $form['status']['#default_value']);
174 unset($form['status']['#default_value'][$key]);
175 drupal_set_message(t('The module %module was deactivated--it requires the following disabled/non-existant modules to function properly: %dependencies', array('%module' => $module, '%dependencies' => implode(', ', $missing_dependency_list))), 'error');
179 // ---------------------------------------------------------------------------
180 // Administrative Pages
183 * This page lists all system views and provides links to edit them.
185 function views_ui_admin_page() {
188 drupal_set_title(t('administer views'));
190 $result = pager_query("SELECT vid, name, description, menu_title, page_title, block_title, url, page, menu, block FROM {view_view} ORDER BY name", $numViews);
192 while ($view = db_fetch_object($result)) {
193 $url = ($view->page ?
l($view->url
, $view->url
) : t('No Page View'));
196 $provides[] = 'Page';
199 $provides[] = 'Block';
202 $provides[] = 'Menu';
206 views_get_title($view, 'menu'),
208 implode(', ', $provides),
210 theme('links', array(
211 l(t('edit'), "admin/views/edit/$view->vid"),
212 l(t('export'), "admin/views/export/$view->vid"),
213 l(t('delete'), "admin/views/delete/$view->vid"),
214 l(t('clone'), "admin/views/clone/$view->vid"),
220 $output = theme('table', array(t('View'), t('Title'), t('Description'), t('Provides'), t('URL'), t('Actions')), $items, array("cellpadding" => "4"), t('Existing Views'));
221 $output .
= theme('pager', NULL
, $numViews);
224 $output .
= t('<p>No views have currently been defined.</p>');
227 $result = db_query("SELECT name FROM {view_view}");
228 while ($view = db_fetch_object($result)) {
229 $used[$view->name
] = true
;
232 $output .
= t('<p>Below are system default views; if you edit one of these, a view will be created that will override any system use of the view.</p>');
234 $default_views = _views_get_default_views();
236 $views_status = variable_get('views_defaults', array());
238 foreach ($default_views as
$view) {
239 $url = ($view->page ?
l($view->url
, $view->url
) : t('No Page View'));
241 if ($used[$view->name
]) {
242 $status = t('Overridden');
244 else if (isset($views_status[$view->name
])) {
245 if ($views_status[$view->name
] == 'enabled') {
246 $status = t('Enabled');
249 $status = t('Disabled');
252 else if ($view->disabled
) {
253 $status = t('Disabled');
256 $status = t('Enabled');
261 $provides[] = t('Page');
264 $provides[] = t('Block');
267 $provides[] = t('Menu');
270 $links = array(l(t('add'), "admin/views/add/$view->name"));
271 if ($status == 'Enabled') {
272 $links[] = l(t('disable'), "admin/views/disable/$view->name");
274 else if ($status == 'Disabled') {
275 $links[] = l(t('enable'), "admin/views/enable/$view->name");
278 $items[] = array($view->name
, views_get_title($view, 'menu'), $view->description
, implode(', ', $provides), $url, $status, theme('links', $links));
282 $output .
= theme('table', array(t('Default View'), t('Title'), t('Description'), t('Provides'), t('URL'), t('Status'), t('Actions')), $items, array("cellpadding" => "4"), t('Default Views'));
285 $output .
= t('<p>No views have currently been defined.</p>');
292 * Page to enable a disabled default view
294 function views_ui_admin_enable_page($view = '') {
296 $views_status = variable_get('views_defaults', array());
297 $views_status[$view] = 'enabled';
298 variable_set('views_defaults', $views_status);
301 drupal_goto('admin/views');
305 * Page to disable an enabled default view
307 function views_ui_admin_disable_page($view = '') {
309 $views_status = variable_get('views_defaults', array());
310 $views_status[$view] = 'disabled';
311 variable_set('views_defaults', $views_status);
314 drupal_goto('admin/views');
318 * Provide a textarea to paste a view export into.
320 function views_ui_admin_import_page() {
321 $op = $_POST['edit']['name'];
324 return views_ui_admin_add_page();
326 drupal_set_title("Import a View");
327 $form['view'] = array(
328 '#type' => 'textarea',
329 '#title' => t('Import View Code'),
332 '#description' => t('Cut and paste the results of an Export View here.'),
335 $form['submit'] = array(
337 '#value' => t("Submit"),
340 return drupal_get_form('views_import_view', $form);
344 * Handle the submit button on importing a view.
346 function views_import_view_submit($formid, $form) {
351 $tables = array_keys(_views_get_tables());
353 if (!is_array($view->requires
) || !array_diff($view->requires
, $tables)) {
354 _views_sanitize_view($view);
355 drupal_set_title(t('Add a View'));
356 $output = _views_view_form($view, NULL
);
357 print theme('page', $output);
361 drupal_set_message(t("You don't seem to have the following requirements: ") .
implode(', ', array_diff($view->requires
, $tables)));
365 drupal_set_message(t('Unable to get a view out of that.'));
370 * Export a view for cut & paste.
372 function views_ui_admin_export_page($vid = '') {
373 $code = views_create_view_code($vid);
374 $lines = substr_count($code, "\n");
375 $form['code'] = array(
376 '#type' => 'textarea',
377 '#title' => $view->name
,
378 '#default_value' => $code,
380 return drupal_get_form('views_export', $form);
384 * Provide a form to add a view. Allow adding a view from default templates.
386 function views_ui_admin_add_page($template) {
388 if ($op == t('Cancel')) {
389 return 'admin/views';
392 $view = _views_get_default_view($template);
394 drupal_set_title(t('Add a View'));
396 return _views_view_form($view, $op);
400 * Provide a form to clone a view.
402 function views_ui_admin_clone_page($viewname) {
404 if ($op == t('Cancel')) {
405 return 'admin/views';
408 $view = views_load_view($viewname);
410 return drupal_not_found();
414 drupal_set_title(t('Add a View'));
416 return _views_view_form($view, $op);
420 * Provide a form to edit a view.
422 function views_ui_admin_edit_page($vid = '') {
424 if ($op == t('Cancel')) {
425 return 'admin/views';
428 if ($op == t('Delete')) {
429 return "admin/views/delete/$vid";
432 if (!($view = _views_load_view($vid))) {
433 return 'admin/views';
436 drupal_set_title(t('Edit view %n', array('%n' => $view->name
)));
437 return _views_view_form($view, $op);
441 * Provide a form to confirm deletion of a view.
443 function views_ui_admin_delete_page($vid = '') {
444 $view = _views_load_view($vid);
447 return 'admin/views';
450 $form['vid'] = array('#type' => 'value', '#value' => $view->vid
);
451 return confirm_form('views_delete_confirm', $form,
452 t('Are you sure you want to delete %title?', array('%title' => $view->name
)),
453 $_GET['destination'] ?
$_GET['destination'] : 'admin/views',
454 t('This action cannot be undone.'),
455 t('Delete'), t('Cancel')
460 * Handle the submit button to delete a view.
462 function views_delete_confirm_submit($formid, $form) {
463 if ($form['confirm']) {
464 _views_delete_view((object) $form);
466 drupal_goto('admin/views');
471 * Get an empty view with basic defaults.
473 function _views_get_default_view($template = '') {
475 $default_views = _views_get_default_views();
476 if (isset($default_views[$template])) {
477 $view = $default_views[$template];
481 $view = new
stdClass();
482 $view->use_pager
= true
;
483 $view->nodes_per_page
= variable_get('default_nodes_main', 10);
484 $view->page_header_format
= variable_get('filter_default_format', 1);
485 $view->page_footer_format
= variable_get('filter_default_format', 1);
486 $view->page_header_format
= variable_get('filter_default_format', 1);
487 $view->block_header_format
= variable_get('filter_default_format', 1);
488 $view->block_footer_format
= variable_get('filter_default_format', 1);
489 $view->block_header_format
= variable_get('filter_default_format', 1);
493 return _views_check_arrays($view);
496 // ---------------------------------------------------------------------------
497 // Select Box Definitions
499 // These should probably have string array keys that are easier to identify.
502 * Select box entries for argument defaults.
504 function _views_get_arguments_default() {
506 1 => t('Return Page Not Found'),
507 2 => t('Display All Values'),
508 3 => t('Summary, unsorted'),
509 4 => t('Summary, sorted ascending'),
510 5 => t('Summary, sorted descending'),
511 6 => t('Summary, sorted as view'),
516 * Select box entries for sort ordering.
518 function _views_sortorders() {
520 'ASC' => t('Ascending'),
521 'DESC' => t('Descending')
526 * Swap two items in an array.
528 function _views_swap(&$arr, $a, $b) {
535 * Move an item up in an array.
537 function _views_move_up(&$arr, $i) {
538 if ($i <= 0 || $i >= count($arr)) {
539 return; // can't do it.
541 _views_swap($arr, $i - 1, $i);
545 * Move an item down in an array.
547 function _views_move_down(&$arr, $i) {
548 if ($i >= count($arr) - 1 || $i < 0) {
549 return; // can't do it.
551 _views_swap($arr, $i + 1, $i);
555 * Move an item to the front of an array.
557 function _views_move_top(&$arr, $i) {
558 if ($i <= 0 || $i >= count($arr)) {
559 return; // can't do it.
562 for ($x = $i; $x > 0; $x--)
563 $arr[$x] = $arr[$x - 1];
568 * Move an item to the end of an array.
570 function _views_move_bottom(&$arr, $i) {
571 $end = count($arr) - 1;
572 if ($i >= $end || $i < 0) {
573 return; // can't do it.
576 for ($x = $i; $x < $end; $x++)
577 $arr[$x] = $arr[$x + 1];
582 * Figure out which of the many, many buttons on a form were clicked and
585 function _views_check_sub_ops(&$form, &$order, $i) {
587 if ($form['delete']['#value']) {
588 unset($form['delete']['#value']);
590 $order = array_values($order); // reindex
591 $form['delete']['#printed'] = true
;
592 $form['up']['#printed'] = true
;
593 $form['down']['#printed'] = true
;
594 $form['top']['#printed'] = true
;
595 $form['bottom']['#printed'] = true
;
598 else foreach (array('up', 'down', 'top', 'bottom') as
$dir) {
599 if ($form[$dir]['#value']) {
600 unset ($form[$dir]['#value']);
601 $func = "_views_move_$dir";
610 * Figure out if one of the add buttons on a form were clicked, and handle it.
612 function _views_check_ops(&$view, $op, $form) {
613 if ($op == t('Add Filter')) {
614 $view->new_filter
['id'] = $form['filter']['add']['id']['#value'];
617 else if ($op == t('Add Criteria')) {
618 $view->new_sort
['id'] = $form['sort']['add']['id']['#value'];
621 else if ($op == t('Add Argument')) {
622 $view->new_argument
['id'] = $form['argument']['add']['id']['#value'];
625 else if ($op == t('Add Field')) {
626 $fieldbits = explode('.', $form['field']['add']['id']['#value']);
627 $view->new_field
['id'] = $form['field']['add']['id']['#value'];
628 $view->new_field
['tablename'] = $fieldbits[0];
629 $view->new_field
['field'] = $fieldbits[1];
630 $view->new_field
['label'] = $fieldnames[$form['field']['add']['id']['#value']];
631 $view->new_field
['queryname'] = "$fieldbits[0]_$fieldbits[1]";
634 else if ($op == t('Expose Filter')) {
635 $view->new_exposed_filter
['id'] = $form['exposed_filter']['add']['id']['#value'];
641 * Custom form element to do our nice images.
643 function views_elements() {
644 $type['imagebutton'] = array('#input' => TRUE
, '#button_type' => 'submit',);
648 function theme_imagebutton($element) {
649 return '<input type="image" class="form-'.
$element['#button_type'] .
'" name="'.
$element['#name'] .
'" value="'.
check_plain($element['#default_value']) .
'" '.
drupal_attributes($element['#attributes']) .
' src="' .
$element['#image'] .
'" alt="' .
$element['#title'] .
'" title="' .
$element['#title'] .
"\" />\n";
652 function imagebutton_value() {
653 // null function guarantees default_value doesn't get moved to #value.
657 * Display all the guts of a view in a form for editing.
659 function _views_view_form($view, $op = '') {
660 _views_check_arrays($view); // make sure arrays that might be empty get set
662 // Put in all our add buttons, then process them to see if they've been hit.
663 views_ui_add_add_button($form, 'field', _views_get_fields(true
), t('Add Field'));
664 views_ui_add_add_button($form, 'argument', _views_get_arguments(true
), t('Add Argument'));
665 views_ui_add_add_button($form, 'filter', _views_get_filters(true
), t('Add Filter'));
666 views_ui_add_add_button($form, 'sort', _views_get_sorts(true
), t('Add Criteria'));
668 $allbut = _views_check_ops($view, $op, $form);
669 if ($_POST['edit'] && $op != t('Save')) {
670 drupal_set_message(t('You have modified this view; changes will not be recorded until you Save the form.'));
673 $form['exposed_filter'] = array();
674 foreach (array('field', 'argument', 'filter', 'exposed_filter', 'sort') as
$section) {
675 if (views_ui_add_section($form[$section], $view, $section)) {
680 $form['vid'] = array(
682 '#value' => $view->vid
,
684 $form['allbut'] = array(
688 $form['changed'] = array(
690 '#value' => $view->changed
,
693 $form['basic-info'] = array(
694 '#type' => 'fieldset',
695 '#collapsible' => true
,
696 '#collapsed' => ($allbut != NULL
),
697 '#title' => t('Basic Information'),
700 $form['basic-info']['name'] = array(
701 '#type' => 'textfield',
702 '#title' => t('Name'),
703 '#default_value' => $view->name
,
706 '#description' => t('The unique identifier of the view; it is only important for overridden views and views that modules or themes will need to use. Only alphanumeric and _ allowed here'),
710 $form['basic-info']['access'] = array(
711 '#type' => 'checkboxes',
712 '#title' => t('Access'),
713 '#default_value' => $view->access
,
714 '#options' => views_handler_filter_role(),
715 '#description' => t('Only the checked roles will be able to see this view in any form; if no roles are checked, access will not be restricted.'),
718 $form['basic-info']['description'] = array(
719 '#type' => 'textfield',
720 '#title' => t('Description'),
721 '#default_value' => $view->description
,
724 '#description' => t('A description of the view for the admin list.'),
729 $form['page-info'] = array(
730 '#type' => 'fieldset',
731 '#collapsible' => true
,
732 '#collapsed' => ($allbut != NULL
|| !$view->page
),
733 '#title' => t('Page'),
736 $form['page-info']['page'] = array(
737 '#type' => 'checkbox',
738 '#title' => t('Provide Page View'),
739 '#return_value' => 1,
740 '#default_value' => $view->page
,
741 '#description' => t('If checked this view will be provided as a page. If not checked, the fields in this group will be ignored.'),
744 $form['page-info']['url'] = array(
745 '#type' => 'textfield',
746 '#title' => t('URL'),
747 '#default_value' => $view->url
,
750 '#description' => t("Enter the URL to use for this view in the form of 'dir/dir'. Do not begin or end the URL with a /. Example: 'view/tracker'. This is required if providing a page view. You can also add $arg as a placeholder for arguments passed in the URL, for example 'user/$arg/tracker' or 'view/taxonomy/$arg'."),
753 $form['page-info']['page_type'] = array(
755 '#title' => t('View Type'),
756 '#default_value' => $view->page_type
,
757 '#options' => _views_get_style_plugins(true
),
758 '#description' => t('How the nodes should be displayed to the user.'),
761 $form['page-info']['page_title'] = array(
762 '#type' => 'textfield',
763 '#title' => t('Title'),
764 '#default_value' => $view->page_title
,
767 '#description' => t('The title that be shown at the top of the view. May be blank. If your View allows arguments, you may use %1 to substitute argument 1 text, %2 to argument #2, and so on.'),
770 $form['page-info']['use_pager'] = array(
771 '#type' => 'checkbox',
772 '#title' => t('Use Pager'),
773 '#return_value' => 1,
774 '#default_value' => $view->use_pager
,
775 '#description' => t('If checked this query may be multiple pages. If not checked this query will be one page.'),
777 $form['page-info']['breadcrumb_no_home'] = array(
778 '#type' => 'checkbox',
779 '#title' => t('Breadcrumb trail should not include "Home"'),
780 '#return_value' => 1,
781 '#default_value' => $view->breadcrumb_no_home
,
782 '#description' => t('If checked the breadcrumb trail for this page will discard "Home". Usually you will not set this, but this is used for the Front Page View, where it IS Home and should not leave a trail to itself.'),
784 $form['page-info']['nodes_per_page'] = array(
785 '#type' => 'textfield',
786 '#title' => t('Nodes per Page'),
787 '#default_value' => $view->nodes_per_page
,
790 '#description' => t('The number of nodes to display per page. If 0 no nodes will be displayed. If not using a pager, this will be the maximum number of nodes in the list.'),
791 '#attributes' => NULL
,
794 $form['page-info']['page_header_fieldset'] = array(
795 '#type' => 'fieldset',
796 '#collapsible' => TRUE
,
797 '#collapsed' => TRUE
,
798 '#title' => t('Header'),
800 $form['page-info']['page_header_fieldset']['page_header'] = array(
801 '#type' => 'textarea',
802 '#default_value' => $view->page_header
,
805 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
808 $form['page-info']['page_header_fieldset']['page_header_format'] = filter_form($view->page_header_format
, 1, array('page_header_format'));
810 $form['page-info']['page_footer_fieldset'] = array(
811 '#type' => 'fieldset',
812 '#collapsible' => TRUE
,
813 '#collapsed' => TRUE
,
814 '#title' => t('Footer'),
816 $form['page-info']['page_footer_fieldset']['page_footer'] = array(
817 '#type' => 'textarea',
818 '#default_value' => $view->page_footer
,
821 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
824 $form['page-info']['page_footer_fieldset']['page_footer_format'] = filter_form($view->page_footer_format
, 1, array('page_footer_format'));
826 $form['page-info']['page_empty_fieldset'] = array(
827 '#type' => 'fieldset',
828 '#collapsible' => TRUE
,
829 '#collapsed' => TRUE
,
830 '#title' => t('Empty Text'),
832 $form['page-info']['page_empty_fieldset']['page_empty'] = array(
833 '#type' => 'textarea',
834 '#default_value' => $view->page_empty
,
837 '#description' => t('Text to display if a view returns no nodes. Optional.'),
840 $form['page-info']['page_empty_fieldset']['page_empty_format'] = filter_form($view->page_empty_format
, 1, array('page_empty_format'));
842 $form['page-info']['menu-info'] = array(
843 '#type' => 'fieldset',
844 '#collapsible' => TRUE
,
845 '#collapsed' => TRUE
,
846 '#title' => t('Menu'),
849 $form['page-info']['menu-info']['menu'] = array(
850 '#type' => 'checkbox',
851 '#title' => t('Provide Menu'),
852 '#return_value' => 1,
853 '#default_value' => $view->menu
,
854 '#description' => t('If checked this view be given a menu entry in the Drupal menu system. If not checked the data in this group will be ignored.'),
857 $form['page-info']['menu-info']['menu_tab'] = array(
858 '#type' => 'checkbox',
859 '#title' => t('Provide Menu as Tab'),
860 '#return_value' => 1,
861 '#default_value' => $view->menu_tab
,
862 '#description' => t("If checked this view's menu entry will be provided as a tab rather than in the main menu system."),
865 $form['page-info']['menu-info']['menu_tab_default'] = array(
866 '#type' => 'checkbox',
867 '#title' => t('Make Default Menu Tab'),
868 '#return_value' => 1,
869 '#default_value' => $view->menu_tab_default
,
870 '#description' => t("If checked this view's menu entry will be provided as a tab, and will be the default tab for that URL path. For example, if the URL is 'tracker/all' and it is set as the default menu tab, it will be put into the menu as 'tracker' and 'tracker/all' will be the default tab. For tabs to work properly, one tab in the group must be set as the default."),
873 $form['page-info']['menu-info']['menu_tab_weight'] = array(
874 '#type' => 'textfield',
875 '#title' => t('Tab Weight'),
876 '#default_value' => $view->menu_tab_weight
,
878 '#description' => t('If this is a menu tab, select the weight; lower numbers will be further to the left.'),
881 $form['page-info']['menu-info']['menu_title'] = array(
882 '#type' => 'textfield',
883 '#title' => t('Menu Title'),
884 '#default_value' => $view->menu_title
,
887 '#description' => t('Enter the title to use for the menu entry or tab. If blank, the page title will be used.'),
892 $form['block-info'] = array(
893 '#type' => 'fieldset',
894 '#collapsible' => true
,
895 '#collapsed' => ($allbut != NULL
|| !$view->block
),
896 '#title' => t('Block'),
899 $form['block-info']['block'] = array(
900 '#type' => 'checkbox',
901 '#title' => t('Provide Block'),
902 '#return_value' => 1,
903 '#default_value' => $view->block
,
904 '#description' => t('If checked this view will be provided as a block. If checked title may not be blank.'),
907 $form['block-info']['block_type'] = array(
909 '#title' => t('View Type'),
910 '#default_value' => $view->block_type
,
911 '#options' => _views_get_style_plugins(true
),
912 '#description' => t('How the nodes should be displayed to the user.'),
915 $form['block-info']['block_title'] = array(
916 '#type' => 'textfield',
917 '#title' => t('Title'),
918 '#default_value' => $view->block_title
,
921 '#description' => t('The title that will be shown at the top of the block. May be blank.'),
924 $form['block-info']['nodes_per_block'] = array(
925 '#type' => 'textfield',
926 '#title' => t('Nodes per Block'),
927 '#default_value' => $view->nodes_per_block
,
930 '#description' => t('If using a block, the maximum number of items to display in the block. Pagers are not used in blocks.'),
931 '#attributes' => NULL
,
934 $form['block-info']['block_more'] = array(
935 '#type' => 'checkbox',
936 '#title' => t('[More] Link?'),
937 '#return_value' => 1,
938 '#default_value' => $view->block_more
,
939 '#description' => t('If using a view as both a page and a block, display a more link in the block that links to the view URL?'),
942 $form['block-info']['block_header_fieldset'] = array(
943 '#type' => 'fieldset',
944 '#collapsible' => TRUE
,
945 '#collapsed' => TRUE
,
946 '#title' => t('Header'),
948 $form['block-info']['block_header_fieldset']['block_use_page_header'] = array(
949 '#type' => 'checkbox',
950 '#title' => t('Use Page Header'),
951 '#return_value' => 1,
952 '#default_value' => $view->block_use_page_header
,
953 '#description' => t('If checked, use the Page Header for block view instead. If so, you should leave the Block Header blank.'),
956 $form['block-info']['block_header_fieldset']['block_header'] = array(
957 '#type' => 'textarea',
958 '#title' => t('Header'),
959 '#default_value' => $view->block_header
,
962 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
965 $form['block-info']['block_header_fieldset']['block_header_format'] = filter_form($view->block_header_format
, 1, array( 'block_header_format'));
967 $form['block-info']['block_footer_fieldset'] = array(
968 '#type' => 'fieldset',
969 '#collapsible' => TRUE
,
970 '#collapsed' => TRUE
,
971 '#title' => t('Footer'),
973 $form['block-info']['block_footer_fieldset']['block_use_page_footer'] = array(
974 '#type' => 'checkbox',
975 '#title' => t('Use Page Footer'),
976 '#return_value' => 1,
977 '#default_value' => $view->block_use_page_footer
,
978 '#description' => t('If checked, use the page footer for block view instead. If so, you should leave the block footer blank.'),
981 $form['block-info']['block_footer_fieldset']['block_footer'] = array(
982 '#type' => 'textarea',
983 '#title' => t('Footer'),
984 '#default_value' => $view->block_footer
,
987 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
990 $form['block-info']['block_footer_fieldset']['block_footer_format'] = filter_form($view->block_footer_format
, 1, array( 'block_footer_format'));
992 $form['block-info']['block_empty_fieldset'] = array(
993 '#type' => 'fieldset',
994 '#collapsible' => TRUE
,
995 '#collapsed' => TRUE
,
996 '#title' => t('Empty text'),
998 $form['block-info']['block_empty_fieldset']['block_use_page_empty'] = array(
999 '#type' => 'checkbox',
1000 '#title' => t('Use Page empty'),
1001 '#return_value' => 1,
1002 '#default_value' => $view->block_use_page_empty
,
1003 '#description' => t('If checked, use the Page Empty Text for block view instead. If so, you should leave the block empty text blank.'),
1006 $form['block-info']['block_empty_fieldset']['block_empty'] = array(
1007 '#type' => 'textarea',
1008 '#title' => t('Empty text'),
1009 '#default_value' => $view->block_empty
,
1012 '#description' => t('Text to display if a view results in no nodes. Optional.'),
1015 $form['block-info']['block_empty_fieldset']['block_empty_format'] = filter_form($view->block_empty_format
, 1, array( 'block_empty_format'));
1017 $access = user_access('use PHP for block visibility');
1019 $form['view_args_php_fieldset'] = array(
1020 '#type' => 'fieldset',
1021 '#collapsible' => TRUE
,
1022 '#collapsed' => empty($view->view_args_php
) ? TRUE
: FALSE
,
1023 '#title' => t('Argument Handling Code'),
1026 $form['view_args_php_fieldset']['view_args_php'] = array(
1027 '#type' => 'textarea',
1028 '#title' => t('Argument Code'),
1029 '#default_value' => $view->view_args_php
,
1032 '#description' => '<p>'.
t('Advanced Usage Only: PHP code that returns a custom array of arguments for the view. Should not include <?php ?> delimiters.') .
'</p>' .
1033 '<p>'.
t('For more information, please see the <a href="%arg">Argument Handling Code documentation</a> in the Drupal handbook.', array('%arg' => 'http://drupal.org/node/70145')) .
'</p>',
1036 $form['view_args_php_fieldset']['view_args_php'] = array(
1038 '#value' => $view->view_args_php
,
1042 $form['save'] = array(
1043 '#type' => 'submit',
1044 '#value' => t('Save'),
1047 $form['delete'] = array(
1048 '#type' => 'submit',
1049 '#value' => t('Delete'),
1052 $form['cancel'] = array(
1053 '#type' => 'submit',
1054 '#value' => t('Cancel'),
1057 return drupal_get_form('views_edit_view', $form);
1061 * separately build a piece of the form
1063 function views_ui_build_form(&$form) {
1064 $form = form_builder('views_edit_view', $form);
1068 * Add the 'add' button for a section into the form
1070 function views_ui_add_add_button(&$form, $section, $options, $label) {
1071 $form[$section]['add'] = array('#tree' => true
, '#parents' => array($section, 'add'));
1073 $form[$section]['add']['id'] = array(
1074 '#type' => 'select',
1075 '#options' => $options,
1077 $form[$section]['add']['button'] = array(
1078 '#type' => 'button',
1081 views_ui_build_form($form[$section]['add']);
1085 * Add one of the list sections to the form.
1087 function views_ui_add_section(&$form, &$view, $section) {
1088 // add fields to the form.
1090 $form += array('#tree' => true
, '#parents' => array($section));
1091 $view_section = &$view->$section;
1093 $num_items = count($view_section);
1094 // Populate the form with fields we need to check to track
1095 // changes through the form's life.
1096 $form['count'] = array('#type' => 'hidden', '#default_value' => $num_items);
1098 $form['order'] = array(
1099 '#type' => 'hidden',
1100 '#default_value' => $num_items ?
implode(',', range(0, $num_items - 1)) : '',
1103 // Run the builder to get the value on the form.
1104 views_ui_build_form($form);
1105 $order = $form['order']['#value'] != ''
1106 ?
explode(',', $form['order']['#value'])
1109 // Shortcut because operator precedence gets sticky if using $foo->$bar[]
1110 for ($i = $num_items; $i < $form['count']['#value']; $i++) {
1111 $view_section[] = array();
1114 $new_section = "new_$section";
1115 // instantiate the new field if one was added.
1116 if ($view->$new_section) {
1117 $view_section[] = $view->$new_section;
1118 $order[] = $form['count']['#value'];
1119 $form['count']['#value']++;
1122 $func = "views_ui_add_$section";
1124 foreach ($order as
$key => $i) {
1125 $form[$i] = array('#tree' => true
, '#parents' => array($section, $i));
1127 $form[$i]['id'] = array(
1128 '#type' => 'hidden',
1129 '#default_value' => $view_section[$i]['id'],
1132 views_ui_add_buttons($form[$i]);
1133 views_ui_build_form($form[$i]);
1135 $retval = _views_check_sub_ops($form[$i], $order, $key);
1136 if ($retval !== 'delete') {
1137 $retval2 = $func($form[$i], $view_section[$i], $order, $key, $i, $view);
1140 if ($retval || $retval2) {
1144 $form['order']['#value'] = implode(',', $order);
1150 * Add all the info for a single field into the form.
1152 function views_ui_add_field(&$form, $field, &$order, $key, $i) {
1153 $fields = _views_get_fields();
1154 $fieldname = $form['id']['#value']; // combined table & fieldname
1155 $fieldinfo = $fields[$fieldname]; // laziness + readability
1157 $form['fullname'] = array(
1158 '#type' => 'hidden',
1159 '#default_value' => $field['id'],
1162 $form['name'] = array(
1163 '#type' => 'markup',
1164 '#value' => $fieldinfo['name'], // combined table & fieldname
1166 $form['queryname'] = array(
1167 '#type' => 'hidden',
1168 '#default_value' => $field['queryname'],
1170 $form['tablename'] = array(
1171 '#type' => 'hidden',
1172 '#default_value' => $field['tablename'],
1174 $form['field'] = array(
1175 '#type' => 'hidden',
1176 '#default_value' => $field['field'],
1179 $form['label'] = array(
1180 '#type' => 'textfield',
1181 '#default_value' => $field['label'],
1183 '#maxlength' => 255,
1186 if (is_array($fieldinfo['handler'])) {
1187 $form['handler'] = array(
1188 '#type' => 'select',
1189 '#default_value' => $field['handler'],
1190 '#options' => $fieldinfo['handler'],
1194 if (isset($fieldinfo['option'])) {
1195 $widget = $fieldinfo['option'];
1196 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1197 $widget['#options'] = $widget['#options']('option', $fieldinfo['option']);
1199 $widget['#default_value'] = $field['options'];
1200 $form['options'] = $widget;
1204 if ($fieldinfo['sortable']) {
1205 $form['sortable'] = array(
1206 '#type' => 'select',
1207 '#default_value' => $field['sortable'],
1208 '#options' => array('0' => t('No'), '1' => t('Yes')),
1210 $form['defaultsort'] = array(
1211 '#type' => 'select',
1212 '#default_value' => $field['defaultsort'],
1213 '#options' => array('0' => t('None'), 'ASC' => t('Ascending'), 'DESC' => t('Descending')),
1216 if (isset($fieldinfo['help'])) {
1217 $form['help'] = array(
1218 '#type' => 'markup',
1219 '#value' => '<div class="description">' .
$fieldinfo['help'] .
'</div>',
1225 * Add all the info for a single argument into the form.
1227 function views_ui_add_argument(&$form, $argument, &$order, $key, $i) {
1228 $arguments = _views_get_arguments();
1229 $arg_type = $form['id']['#value'];
1230 $arginfo = $arguments[$arg_type];
1232 $form['type'] = array(
1233 '#type' => 'hidden',
1234 '#default_value' => $argument['id'],
1237 $form['name'] = array(
1238 '#value' => $arginfo['name'],
1241 $form['argdefault'] = array(
1242 '#type' => 'select',
1243 '#default_value' => max(1, intval($argument['argdefault'])),
1244 '#options' => _views_get_arguments_default(),
1246 $form['title'] = array(
1247 '#type' => 'textfield',
1248 '#default_value' => $argument['title'],
1250 '#maxlength' => 255,
1252 if (isset($arginfo['option'])) {
1253 $widget = $arginfo['option'];
1254 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1255 $widget['#options'] = $widget['#options']('option', $arginfo['option']);
1257 $widget['#default_value'] = $argument['options'];
1258 $form['options'] = $widget;
1261 if (isset($arginfo['help'])) {
1262 $form['help'] = array(
1263 '#type' => 'markup',
1264 '#value' => $arginfo['help'],
1270 * Add all the info for a single filter into the form.
1272 function views_ui_add_filter(&$form, $filter, &$order, $key, $i, &$view) {
1273 $filters = _views_get_filters();
1274 $filtername = $form['id']['#value']; // combined table & filtername
1275 $filterinfo = $filters[$filtername];
1277 $form['field'] = array(
1278 '#type' => 'hidden',
1279 '#default_value' => $filter['id'],
1282 $form['name'] = array(
1283 '#type' => 'markup',
1284 '#value' => $filterinfo['name'],
1287 // 'operator' is either an array or a handler
1288 $operator = $filterinfo['operator'];
1289 if (!is_array($operator) && function_exists($filterinfo['operator'])) {
1290 $operator = $filterinfo['operator']('operator', $filterinfo);
1293 $form['operator'] = array(
1294 '#type' => 'select',
1295 '#default_value' => $filter['operator'],
1296 '#options' => $operator,
1298 if (isset($filterinfo['option'])) {
1299 $widget = $filterinfo['option'];
1300 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1301 $widget['#options'] = $widget['#options']('option', $filterinfo['option']);
1303 $widget['#default_value'] = $filter['options'];
1304 $form['options'] = $widget;
1307 $form['value'] = $filterinfo['value'];
1308 if (!is_array($form['value']['#options']) && function_exists($form['value']['#options'])) {
1309 $form['value']['#options'] = $form['value']['#options']('value', $filterinfo);
1311 $form['value']['#default_value'] = $filter['value'];
1313 if (isset($filterinfo['help'])) {
1314 $form['help'] = array(
1315 '#type' => 'markup',
1316 '#value' => $filterinfo['help'],
1320 // This is kind of a pain in the butt -- The only way to know all of
1321 // the filters that have been exposed is to look at the form, but that
1322 // part of the form hasn't been built yet, so I can't use the form_builder
1323 // method. I can't put this after that, either, because then I'd have to
1324 // go back to add one if we see one here. So we cheat heavily with _POST.
1326 if ($_POST['edit']) {
1327 $exposed_order = $_POST['edit']['exposed_filter']['order'];
1328 if ($exposed_order !== '') {
1329 $exposed_order = explode(',', $exposed_order);
1330 foreach ($exposed_order as
$x) {
1331 if ($_POST['edit']['exposed_filter'][$x]['id'] == $filtername) {
1338 else { // do this if there's never been a submit.
1339 $exposed_order = array_keys($view->exposed_filter
);
1340 foreach ($exposed_order as
$x) {
1341 if ($view->exposed_filter
[$x]['id'] == $filtername) {
1348 // I can't find a way to see if this button was pressed other than
1349 // directly checking, because #form_submitted doesn't work on arrays
1351 if ($_POST['edit']['filter'][$i]['expose']) {
1352 $view->new_exposed_filter
['id'] = $filtername;
1356 $form['expose'] = array(
1357 '#type' => 'button',
1358 '#default_value' => t('Expose'),
1359 '#name' => "edit[filter][$i][expose]",
1366 * Add all the info for a single exposed filter into the form.
1368 function views_ui_add_exposed_filter(&$form, $filter, &$order, $key, $i) {
1369 $filters = _views_get_filters();
1370 $filtername = $form['id']['#value']; // combined table & filtername
1371 $filterinfo = $filters[$filtername];
1373 $form['field'] = array(
1374 '#type' => 'hidden',
1375 '#default_value' => $filter['id'],
1378 $form['name'] = array(
1379 '#type' => 'markup',
1380 '#value' => $filterinfo['name'],
1383 $form['label'] = array(
1384 '#type' => 'textfield',
1385 '#default_value' => $filter['label'],
1387 '#maxlength' => 255,
1390 $form['optional'] = array(
1391 '#type' => 'checkbox',
1392 '#default_value' => $filter['optional'],
1395 $form['is_default'] = array(
1396 '#type' => 'checkbox',
1397 '#default_value' => $filter['is_default'],
1400 $form['single'] = array(
1401 '#type' => 'checkbox',
1402 '#default_value' => $filter['single'],
1405 $form['operator'] = array(
1406 '#type' => 'checkbox',
1407 '#default_value' => $filter['operator'],
1412 * Add all the info for a single sort into the form.
1414 function views_ui_add_sort(&$form, $sort, &$order, $key, $i) {
1415 $sorts = _views_get_sorts();
1416 $sortname = $form['id']['#value']; // combined table & sortname
1417 $sortinfo = $sorts[$sortname];
1419 $form['field'] = array(
1420 '#type' => 'hidden',
1421 '#default_value' => $sort['id'],
1424 $form['name'] = array(
1425 '#value' => $sortinfo['name'],
1428 $form['sortorder'] = array(
1429 '#type' => 'select',
1431 '#default_value' => $sort['sortorder'],
1432 '#options' => _views_sortorders(),
1434 // option can be either 'string', 'integer', an array or a handler
1435 // that must return an array.
1436 if (isset($sortinfo['option'])) {
1437 $widget = $sortinfo['option'];
1438 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1439 $widget['#options'] = $widget['#options']('option', $sortinfo['option']);
1441 $widget['#default_value'] = $sort['options'];
1442 $form['options'] = $widget;
1444 if (isset($sortinfo['help'])) {
1445 $form['help'] = array(
1446 '#type' => 'markup',
1447 '#value' => $sortinfo['help'],
1453 * Add the up, down, top, bottom, delete buttons to a form.
1455 function views_ui_add_buttons(&$form_item) {
1456 $form_item['delete'] = views_ui_add_button('user-trash.png', t('Delete'), t('Delete this item.'));
1457 $form_item['up'] = views_ui_add_button('go-up.png', t('Up'), t('Move this item up.'));
1458 $form_item['down'] = views_ui_add_button('go-down.png', t('Down'), t('Move this item down.'));
1459 $form_item['top'] = views_ui_add_button('go-top.png', t('Top'), t('Move this item to the top.'));
1460 $form_item['bottom'] = views_ui_add_button('go-bottom.png', t('Bottom'), t('Move this item to the bottom.'));
1464 * Add a single button to a form.
1466 function views_ui_add_button($image, $name, $text) {
1467 $module_path = base_path() .
drupal_get_path('module', 'views');
1470 '#type' => 'imagebutton',
1471 '#image' => $module_path .
'/' .
$image,
1473 '#default_value' => $name,
1478 * Display the form built by _views_view_form()
1480 function theme_views_edit_view($form) {
1482 $output .
= form_render($form['basic-info'], false
);
1483 $output .
= form_render($form['page-info'], false
);
1484 $output .
= form_render($form['block-info'], false
);
1486 $allbut = $form['allbut']['#value'];
1488 // form_render wants a reference so use full array notation.
1491 $collapsed = $allbut && $allbut != 'field';
1492 $group = views_ui_render_section($form['field'], array('name', 'label', 'handler', 'options', 'sortable', 'defaultsort'), array(t('Name'), t('Label'), t('Handler'), t('Option'), t('Sortable'), t('Default Sort')), 'fields', $collapsed);
1493 $group .
= views_ui_render_section_add($form['field']['add'], array('id', 'button'), 'Add Field');
1495 $group .
= '<p>'.
t('Fields are only meaningful with List view and Table View; they allow you to choose which fields are presented and in what order.') .
'</p>';
1496 $output .
= theme('fieldset', array('#title' => t('Fields'), '#children' => $group,
1497 '#collapsible' => true
, '#collapsed' => $collapsed,));
1501 $collapsed = $allbut && $allbut != 'argument';
1502 $group = views_ui_render_section($form['argument'], array('name', 'argdefault', 'title', 'options'), array(t('Argument Type'), t('Default'), t('Title'), t('Option')), 'arguments', $collapsed);
1504 $group .
= views_ui_render_section_add($form['argument']['add'], array('id', 'button'), 'Add Argument');
1506 $group .
= form_render($form['view_args_php_fieldset']);
1508 $group .
= '<p>'.
t('Arguments are parsed directly from the URL. They are not necessary to any given view, but allow flexibility.') .
'</p>';
1509 $output .
= theme('fieldset', array('#title' => t('Arguments'), '#children' => $group,
1510 '#collapsible' => true
, '#collapsed' => $collapsed,));
1513 $collapsed = $allbut && $allbut != 'filter';
1514 $group = views_ui_render_section($form['filter'], array('name', 'operator', 'value', 'options', 'expose'), array(t('Field'), t('Operator'), t('Value'), t('Option'), ''), 'filters', $collapsed);
1516 $group .
= views_ui_render_section_add($form['filter']['add'], array('id', 'button'), 'Add Filter');
1518 $group .
= '<p>'.
t('Filters allow you to select a subset of all the nodes to display. All Filters are ANDed together.') .
'</p>';
1519 $output .
= theme('fieldset', array('#title' => t('Filters'), '#children' => $group,
1520 '#collapsible' => true
, '#collapsed' => $collapsed,));
1523 // exposed exposed_filter
1524 $collapsed = $allbut && $allbut != 'exposed_filter';
1525 $group = views_ui_render_section($form['exposed_filter'], array('name', 'label', 'optional', 'is_default', 'single', 'operator'), array(t('Field'), t('Label'), t('Optional'), t('Filter settings Default'), t('Force Single'), t('Lock Operator')), 'exposed_filters', $collapsed);
1528 // $group .= views_ui_render_section_add($form['exposed_filter']['add'], array('id', 'button'), 'Expose Filter');
1530 $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>';
1531 $output .
= theme('fieldset', array('#title' => t('Exposed Filters'), '#children' => $group,
1532 '#collapsible' => true
, '#collapsed' => $collapsed,));
1536 $collapsed = $allbut && $allbut != 'sort';
1537 $group = views_ui_render_section($form['sort'], array('name', 'sortorder', 'options'), array(t('Field'), t('Order'), t('Option')), 'sort criteria', $collapsed);
1539 $group .
= views_ui_render_section_add($form['sort']['add'], array('id', 'button'), 'Add criteria');
1540 $output .
= theme('fieldset', array('#title' => t('Sort Criteria'), '#children' => $group,
1541 '#collapsible' => true
, '#collapsed' => $collapsed,));
1543 $output .
= form_render($form, false
);
1548 * Render one of the dynamic sections on our form.
1550 function views_ui_render_section(&$form, $items, $header, $section, &$collapsed) {
1551 // always do the buttons
1552 $items = array_merge($items, array('delete', 'top', 'up', 'down', 'bottom'));
1553 $num_items = count($items);
1555 $order = ($form['order']['#value'] != ''
1556 ?
explode(',', $form['order']['#value'])
1559 $count = count($order) - 1;
1560 foreach ($order as
$key => $i) {
1562 foreach ($items as
$item) {
1563 if (($key == 0 && ($item == 'up' || $item == 'top')) ||
1564 ($key == $count && ($item == 'down' || $item == 'bottom'))) {
1565 $form[$i][$item]['#printed'] = true
;
1566 $row[] = ' '; // array('data' => ' ', 'width' => 16);
1569 $row[] = form_render($form[$i][$item], false
);
1573 if (isset($form[$i]['help'])) {
1574 $rows[] = array(array('data' => form_render($form[$i]['help']), 'colspan' => $num_items));
1578 $rows[] = array(array('data' => t('This view currently has no %s defined.', array('%s' => $section)), 'colspan' => $num_items));
1582 $header[] = array('data' => t('Ops'), 'colspan' => 5);
1583 return theme('table', $header, $rows) .
"<br />";
1587 * Render the add buttons for one of the dynamic sections on our form.
1589 function views_ui_render_section_add(&$form, $items, $label) {
1590 foreach ($items as
$item) {
1591 $row[] = form_render($form[$item], false
);
1595 $header = array(array('data' => t($label), 'colspan' => count($items)));
1596 return theme('table', $header, $rows) .
"<br />";
1600 * Validate that a view sent via form is OK.
1602 function views_edit_view_validate($form_id, $view, $form) {
1604 if ($op != t('Save')) {
1605 return; // only validate on saving!
1609 $changed = db_result(db_query("SELECT changed FROM {view_view} WHERE vid = %d", $view['vid']));
1610 if ($changed && $view['changed'] != $changed) {
1611 form_set_error('', t('Another user has modified this view, unable to save. You can get this error by using the Back button to re-edit a view after saving one; if you do this, be sure to Reload before making any changes!'));
1616 if (!$view['name']) {
1617 form_error($form['basic-info']['name'], t('View name is required.'));
1620 // view name must be alphanumeric or underscores, no other punctuation.
1621 if (preg_match('/[^a-zA-Z0-9_]/', $view['name'])) {
1622 form_error($form['basic-info']['name'], t('View name must be alphanumeric or underscores only.'));
1625 // test uniqueness of name
1626 $vid = db_result(db_query("SELECT vid FROM {view_view} WHERE name='%s'", $view['name']));
1627 if ($vid && $vid != $view['vid']) {
1628 form_error($form['basic-info']['name'], t('View name already in use.'));
1631 if ($view['use_block'] && $view['nodes_per_block'] < 1) {
1632 form_set_error($form['block-info']['nodes_per_block'], t('If being used as a block, Nodes Per Block must be positive.'));
1635 // validation based on type:
1637 $plugins = _views_get_style_plugins();
1638 if ($view['page']) {
1639 $validator = $plugins[$view['page_type']]['validate'];
1640 if (function_exists($validator)) {
1641 $validator('page', $view, $form);
1645 if ($view['block']) {
1646 $validator = $plugins[$view['block_type']]['validate'];
1647 if (function_exists($validator)) {
1648 $validator('block', $view, $form);
1652 foreach (array('field', 'argument', 'sort', 'filter') as
$type) {
1653 $function = "_views_get_$type" .
's';
1654 $info = $function();
1655 if (is_array($view->$type)) {
1656 foreach ($view->$type as
$key => $data) {
1657 if (!is_numeric($key)) {
1658 continue; // some non-data data is in here.
1660 $validator = $info[$data['id']]['validate'];
1661 if (function_exists($validator)) {
1662 $validator($data, $view, $form);
1669 function views_ui_reorder(&$view) {
1670 // re-order things as they were ordered on the form.
1671 $order = $view['order'] != '' ?
explode(',', $view['order']) : array();
1672 foreach ($order as
$position => $key) {
1673 $placeholder[] = $view[$key];
1675 $view = $placeholder;
1679 * Handle submit buttons on a view form.
1681 function views_edit_view_submit($form_id, $form) {
1682 $view = (object) $form;
1684 // Transform any data that requires it here.
1685 $view->access
= array_keys(array_filter($view->access
));
1687 // ensure for MYSQL 5 irritation reasons.
1688 $view->nodes_per_page
= intval($view->nodes_per_page
);
1689 $view->nodes_per_block
= intval($view->nodes_per_block
);
1690 $view->menu_tab_weight
= intval($view->menu_tab_weight
);
1692 // re-order things as they were ordered on the form.
1693 foreach (array('field', 'argument', 'filter', 'sort', 'exposed_filter') as
$section) {
1694 views_ui_reorder($view->$section);
1697 _views_save_view($view);
1700 drupal_set_message(t('View successfully saved.'));
1703 drupal_set_message(t('View successfully added.'));
1706 return 'admin/views';
1710 * Validate a view with a type: list.
1712 function views_ui_plugin_validate_list($type, $view, $form) {
1713 // list (and table) modes require there to be at least 1 field active.
1714 if (is_array($view['field'])) {
1715 $fields = array_filter(array_keys($view['field']), 'is_numeric');
1718 form_error($form["$type-info"][$type .
'_type'], "List and Table types require at least one field.");
1723 * Validate a view with type: table.
1725 function views_ui_plugin_validate_table($type, $view, $form) {
1726 // pass thru to list cause for now they're the same.
1727 return views_ui_plugin_validate_list($type, $view, $form);
1729 // ---------------------------------------------------------------------------
1730 // View creator tool
1735 function views_tf($val) {
1736 return ($val ?
'TRUE' : 'FALSE');
1740 * This function creates view code for a view.
1742 function views_create_view_code($vid) {
1743 $view = _views_load_view($vid);
1745 return "View '$vid' not found.";
1748 $requires = array();
1750 $output = " \$view = new stdClass();\n";
1751 $output .
= " \$view->name = '" .
db_escape_string($view->name
) .
"';\n";
1752 $output .
= " \$view->description = '" .
db_escape_string($view->description
) .
"';\n";
1753 $output .
= " \$view->access = " .
var_export($view->access
, true
) .
";\n";
1754 if (user_access('use PHP for block visibility')) {
1755 $output .
= " \$view->view_args_php = '" .
check_plain($view->view_args_php
) .
"';\n";
1759 $output .
= " \$view->page = " .
views_tf($view->page
) .
";\n";
1760 $output .
= " \$view->page_title = '" .
db_escape_string($view->page_title
) .
"';\n";
1761 $output .
= " \$view->page_header = '" .
db_escape_string($view->page_header
) .
"';\n";
1762 $output .
= " \$view->page_header_format = '$view->page_header_format';\n";
1763 $output .
= " \$view->page_footer = '" .
db_escape_string($view->page_footer
) .
"';\n";
1764 $output .
= " \$view->page_footer_format = '$view->page_footer_format';\n";
1765 $output .
= " \$view->page_empty = '" .
db_escape_string($view->page_empty
) .
"';\n";
1766 $output .
= " \$view->page_empty_format = '$view->page_empty_format';\n";
1767 $output .
= " \$view->page_type = '$view->page_type';\n";
1768 $output .
= " \$view->url = '" .
db_escape_string($view->url
) .
"';\n";
1769 $output .
= " \$view->use_pager = " .
views_tf($view->use_pager
) .
";\n";
1770 $output .
= " \$view->nodes_per_page = '$view->nodes_per_page';\n";
1772 $output .
= " \$view->menu = " .
views_tf($view->menu
) .
";\n";
1773 $output .
= " \$view->menu_title = '" .
db_escape_string($view->menu_title
) .
"';\n";
1774 $output .
= " \$view->menu_tab = " .
views_tf($view->menu_tab
) .
";\n";
1775 $output .
= " \$view->menu_tab_default = " .
views_tf($view->menu_tab_default
) .
";\n";
1776 $output .
= " \$view->menu_weight = '$view->menu_weight';\n";
1780 $output .
= " \$view->block = " .
views_tf($view->block
) .
";\n";
1781 $output .
= " \$view->block_title = '" .
db_escape_string($view->block_title
) .
"';\n";
1782 $output .
= " \$view->block_header = '" .
db_escape_string($view->block_header
) .
"';\n";
1783 $output .
= " \$view->block_header_format = '$view->block_header_format';\n";
1784 $output .
= " \$view->block_footer = '" .
db_escape_string($view->block_footer
) .
"';\n";
1785 $output .
= " \$view->block_footer_format = '$view->block_footer_format';\n";
1786 $output .
= " \$view->block_empty = '" .
db_escape_string($view->block_empty
) .
"';\n";
1787 $output .
= " \$view->block_empty_format = '$view->block_empty_format';\n";
1788 $output .
= " \$view->block_type = '$view->block_type';\n";
1789 $output .
= " \$view->nodes_per_block = '$view->nodes_per_block';\n";
1790 $output .
= " \$view->block_more = '$view->block_more';\n";
1791 $output .
= " \$view->block_use_page_header = " .
views_tf($view->block_use_page_header
) .
";\n";
1792 $output .
= " \$view->block_use_page_footer = " .
views_tf($view->block_use_page_footer
) .
";\n";
1793 $output .
= " \$view->block_use_page_empty = " .
views_tf($view->block_use_page_empty
) .
";\n";
1796 $output .
= " \$view->sort = array (\n";
1797 foreach ($view->sort as
$sort) {
1798 $output .
= " array (\n";
1799 $fieldbits = explode('.', $sort['field']);
1800 $output .
= " 'tablename' => '$fieldbits[0]',\n";
1801 $output .
= " 'field' => '$fieldbits[1]',\n";
1802 $output .
= " 'sortorder' => '$sort[sortorder]',\n";
1803 $output .
= " 'options' => '$sort[options]',\n";
1805 $requires[$fieldbits[0]] = 1;
1809 $output .
= " \$view->argument = array (\n";
1810 foreach ($view->argument as
$argument) {
1811 $output .
= " array (\n";
1812 $output .
= " 'type' => '$argument[type]',\n";
1813 $output .
= " 'argdefault' => '$argument[argdefault]',\n";
1814 $output .
= " 'title' => '$argument[title]',\n";
1815 $output .
= " 'options' => '$argument[options]',\n";
1820 $output .
= " \$view->field = array (\n";
1821 foreach ($view->field as
$field) {
1822 $output .
= " array (\n";
1823 $output .
= " 'tablename' => '$field[tablename]',\n";
1824 $output .
= " 'field' => '$field[field]',\n";
1825 $output .
= " 'label' => '$field[label]',\n";
1826 if ($field['handler']) {
1827 $output .
= " 'handler' => '$field[handler]',\n";
1829 if ($field['sortable']) {
1830 $output .
= " 'sortable' => '$field[sortable]',\n";
1832 if ($field['defaultsort']) {
1833 $output .
= " 'defaultsort' => '$field[defaultsort]',\n";
1835 if ($field['options']) {
1836 $output .
= " 'options' => '$field[options]',\n";
1839 $requires[$field['tablename']] = 1;
1843 $output .
= " \$view->filter = array (\n";
1844 foreach ($view->filter as
$filter) {
1845 $output .
= " array (\n";
1846 $fieldbits = explode('.', $filter['field']);
1847 $output .
= " 'tablename' => '$fieldbits[0]',\n";
1848 $output .
= " 'field' => '$fieldbits[1]',\n";
1849 $output .
= " 'operator' => '$filter[operator]',\n";
1850 $value = var_export($filter['value'], true
);
1851 $output .
= " 'options' => '$filter[options]',\n";
1852 $output .
= " 'value' => $value,\n";
1854 $requires[$fieldbits[0]] = 1;
1858 $output .
= " \$view->exposed_filter = array (\n";
1859 foreach ($view->exposed_filter as
$filter) {
1860 $output .
= " array (\n";
1861 $fieldbits = explode('.', $filter['field']);
1862 $output .
= " 'tablename' => '$fieldbits[0]',\n";
1863 $output .
= " 'field' => '$fieldbits[1]',\n";
1864 $output .
= " 'label' => '$filter[label]',\n";
1865 $output .
= " 'optional' => $filter[optional],\n";
1866 $output .
= " 'is_default' => $filter[is_default],\n";
1867 $output .
= " 'operator' => $filter[operator],\n";
1868 $output .
= " 'single' => $filter[single],\n";
1870 $requires[$fieldbits[0]] = 1;
1873 $output .
= " \$view->requires = array(" .
implode(', ', array_keys($requires)) .
");\n";
1874 $output .
= " \$views[\$view->name] = \$view;\n";
1875 // no breadcrumb for now.