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/edit',
113 'title' => t('edit view'),
114 'callback' => 'views_ui_admin_edit_page',
115 'access' => user_access('administer views'),
116 'type' => MENU_CALLBACK
);
117 $items[] = array('path' => 'admin/views/import',
118 'title' => t('import'),
119 'callback' => 'views_ui_admin_import_page',
120 'access' => user_access('administer views'),
121 'type' => MENU_LOCAL_TASK
);
122 $items[] = array('path' => 'admin/views/export',
123 'title' => t('export view'),
124 'callback' => 'views_ui_admin_export_page',
125 'access' => user_access('administer views'),
126 'type' => MENU_CALLBACK
);
127 $items[] = array('path' => 'admin/views/delete',
128 'title' => t('edit view'),
129 'callback' => 'views_ui_admin_delete_page',
130 'access' => user_access('administer views'),
131 'type' => MENU_CALLBACK
);
132 $items[] = array('path' => 'admin/views/enable',
133 'callback' => 'views_ui_admin_enable_page',
134 'access' => user_access('administer views'),
135 'type' => MENU_CALLBACK
);
136 $items[] = array('path' => 'admin/views/disable',
137 'callback' => 'views_ui_admin_disable_page',
138 'access' => user_access('administer views'),
139 'type' => MENU_CALLBACK
);
146 * hunmonk's module dependency check: see http://drupal.org/node/54463
148 function views_ui_form_alter($form_id, &$form) {
149 if ($form_id == 'system_modules' && !$_POST) {
150 views_ui_system_module_validate($form);
155 * hunmonk's module dependency check: see http://drupal.org/node/54463
157 function views_ui_system_module_validate(&$form) {
158 $module = 'views_ui';
159 $dependencies = array('views');
160 foreach ($dependencies as
$dependency) {
161 if (!in_array($dependency, $form['status']['#default_value'])) {
162 $missing_dependency = TRUE
;
163 $missing_dependency_list[] = $dependency;
166 if (in_array($module, $form['status']['#default_value']) && isset($missing_dependency)) {
167 db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module);
168 $key = array_search($module, $form['status']['#default_value']);
169 unset($form['status']['#default_value'][$key]);
170 drupal_set_message(t('The module %module was deactivated--it requires the following disabled/non-existant modules to function properly: %dependencies', array('%module' => $module, '%dependencies' => implode(', ', $missing_dependency_list))), 'error');
174 // ---------------------------------------------------------------------------
175 // Administrative Pages
178 * This page lists all system views and provides links to edit them.
180 function views_ui_admin_page() {
183 drupal_set_title(t('administer views'));
185 $result = pager_query("SELECT vid, name, description, menu_title, page_title, block_title, url, page, menu, block FROM {view_view} ORDER BY name", $numViews);
187 while ($view = db_fetch_object($result)) {
188 $url = ($view->page ?
l($view->url
, $view->url
) : t('No Page View'));
191 $provides[] = 'Page';
194 $provides[] = 'Block';
197 $provides[] = 'Menu';
199 $items[] = array($view->name
, views_get_title($view, 'menu'), $view->description
, implode(', ', $provides), $url, theme('links', array(l(t('edit'), "admin/views/edit/$view->vid"), l(t('export'), "admin/views/export/$view->vid"), l(t('delete'), "admin/views/delete/$view->vid"))));
203 $output = theme('table', array(t('View'), t('Title'), t('Description'), t('Provides'), t('URL'), t('Actions')), $items, array("cellpadding" => "4"), t('Existing Views'));
204 $output .
= theme('pager', NULL
, $numViews);
207 $output .
= t('<p>No views have currently been defined.</p>');
210 $result = db_query("SELECT name FROM {view_view}");
211 while ($view = db_fetch_object($result)) {
212 $used[$view->name
] = true
;
215 $output .
= t('<p>Below are system default views; if you edit one of these, a view will be created that will override any system use of the view.</p>');
217 $default_views = _views_get_default_views();
219 $views_status = variable_get('views_defaults', array());
221 foreach ($default_views as
$view) {
222 $url = ($view->page ?
l($view->url
, $view->url
) : t('No Page View'));
224 if ($used[$view->name
]) {
225 $status = t('Overridden');
227 else if (isset($views_status[$view->name
])) {
228 if ($views_status[$view->name
] == 'enabled') {
229 $status = t('Enabled');
232 $status = t('Disabled');
235 else if ($view->disabled
) {
236 $status = t('Disabled');
239 $status = t('Enabled');
244 $provides[] = t('Page');
247 $provides[] = t('Block');
250 $provides[] = t('Menu');
253 $links = array(l(t('add'), "admin/views/add/$view->name"));
254 if ($status == 'Enabled') {
255 $links[] = l(t('disable'), "admin/views/disable/$view->name");
257 else if ($status == 'Disabled') {
258 $links[] = l(t('enable'), "admin/views/enable/$view->name");
261 $items[] = array($view->name
, views_get_title($view, 'menu'), $view->description
, implode(', ', $provides), $url, $status, theme('links', $links));
265 $output .
= theme('table', array(t('Default View'), t('Title'), t('Description'), t('Provides'), t('URL'), t('Status'), t('Actions')), $items, array("cellpadding" => "4"), t('Default Views'));
268 $output .
= t('<p>No views have currently been defined.</p>');
275 * Page to enable a disabled default view
277 function views_ui_admin_enable_page($view = '') {
279 $views_status = variable_get('views_defaults', array());
280 $views_status[$view] = 'enabled';
281 variable_set('views_defaults', $views_status);
284 drupal_goto('admin/views');
288 * Page to disable an enabled default view
290 function views_ui_admin_disable_page($view = '') {
292 $views_status = variable_get('views_defaults', array());
293 $views_status[$view] = 'disabled';
294 variable_set('views_defaults', $views_status);
297 drupal_goto('admin/views');
301 * Provide a textarea to paste a view export into.
303 function views_ui_admin_import_page() {
304 $op = $_POST['edit']['name'];
307 return views_ui_admin_add_page();
309 drupal_set_title("Import a View");
310 $form['view'] = array(
311 '#type' => 'textarea',
312 '#title' => t('Import View Code'),
315 '#description' => t('Cut and paste the results of an Export View here.'),
318 $form['submit'] = array(
320 '#value' => t("Submit"),
323 return drupal_get_form('views_import_view', $form);
327 * Handle the submit button on importing a view.
329 function views_import_view_submit($formid, $form) {
334 $tables = array_keys(_views_get_tables());
336 if (!is_array($view->requires
) || !array_diff($view->requires
, $tables)) {
337 _views_sanitize_view($view);
338 drupal_set_title(t('Add a View'));
339 $output = _views_view_form($view, NULL
);
340 print theme('page', $output);
344 drupal_set_message(t("You don't seem to have the following requirements: ") .
implode(', ', array_diff($view->requires
, $tables)));
348 drupal_set_message(t('Unable to get a view out of that.'));
353 * Export a view for cut & paste.
355 function views_ui_admin_export_page($vid = '') {
356 $code = views_create_view_code($vid);
357 $lines = substr_count($code, "\n");
358 $form['code'] = array(
359 '#type' => 'textarea',
360 '#title' => $view->name
,
361 '#default_value' => $code,
363 return drupal_get_form('views_export', $form);
367 * Provide a form to add a view. Allow adding a view from default templates.
369 function views_ui_admin_add_page() {
370 $args = func_get_args();
371 while ($next = array_shift($args)) {
372 $template = ($template ?
"/$next" : $next);
376 if ($op == t('Cancel')) {
377 return drupal_goto('admin/views');
380 $view = _views_get_default_view($template);
382 drupal_set_title(t('Add a View'));
384 return _views_view_form($view, $op);
388 * Provide a form to edit a view.
390 function views_ui_admin_edit_page($vid = '') {
392 if ($op == t('Cancel')) {
393 return drupal_goto('admin/views');
396 if ($op == t('Delete')) {
397 return drupal_goto("admin/views/delete/$vid");
400 if (!($view = _views_load_view($vid))) {
401 return drupal_goto('admin/views');
404 drupal_set_title(t('Edit view %n', array('%n' => $view->name
)));
405 return _views_view_form($view, $op);
409 * Provide a form to confirm deletion of a view.
411 function views_ui_admin_delete_page($vid = '') {
412 $view = _views_load_view($vid);
415 return drupal_goto('admin/views');
418 $form['vid'] = array('#type' => 'value', '#value' => $view->vid
);
419 return confirm_form('views_delete_confirm', $form,
420 t('Are you sure you want to delete %title?', array('%title' => $view->name
)),
421 $_GET['destination'] ?
$_GET['destination'] : 'admin/views',
422 t('This action cannot be undone.'),
423 t('Delete'), t('Cancel')
428 * Handle the submit button to delete a view.
430 function views_delete_confirm_submit($formid, $form) {
431 if ($form['confirm']) {
432 _views_delete_view((object) $form);
434 drupal_goto('admin/views');
439 * Get an empty view with basic defaults.
441 function _views_get_default_view($template = '') {
443 $default_views = _views_get_default_views();
444 if (isset($default_views[$template])) {
445 $view = $default_views[$template];
449 $view = new
stdClass();
450 $view->use_pager
= true
;
451 $view->nodes_per_page
= variable_get('default_nodes_main', 10);
452 $view->page_header_format
= variable_get('filter_default_format', 1);
453 $view->page_footer_format
= variable_get('filter_default_format', 1);
454 $view->page_header_format
= variable_get('filter_default_format', 1);
455 $view->block_header_format
= variable_get('filter_default_format', 1);
456 $view->block_footer_format
= variable_get('filter_default_format', 1);
457 $view->block_header_format
= variable_get('filter_default_format', 1);
461 return _views_check_arrays($view);
464 // ---------------------------------------------------------------------------
465 // Select Box Definitions
467 // These should probably have string array keys that are easier to identify.
470 * Select box entries for argument defaults.
472 function _views_get_arguments_default() {
474 1 => t('Return Page Not Found'),
475 2 => t('Display All Values'),
476 3 => t('Summary, unsorted'),
477 4 => t('Summary, sorted ascending'),
478 5 => t('Summary, sorted descending'),
479 6 => t('Summary, sorted as view'),
484 * Select box entries for sort ordering.
486 function _views_sortorders() {
488 'ASC' => t('Ascending'),
489 'DESC' => t('Descending')
494 * Swap two items in an array.
496 function _views_swap(&$arr, $a, $b) {
503 * Move an item up in an array.
505 function _views_move_up(&$arr, $i) {
506 if ($i <= 0 || $i >= count($arr)) {
507 return; // can't do it.
509 _views_swap($arr, $i - 1, $i);
513 * Move an item down in an array.
515 function _views_move_down(&$arr, $i) {
516 if ($i >= count($arr) - 1 || $i < 0) {
517 return; // can't do it.
519 _views_swap($arr, $i + 1, $i);
523 * Move an item to the front of an array.
525 function _views_move_top(&$arr, $i) {
526 if ($i <= 0 || $i >= count($arr)) {
527 return; // can't do it.
530 for ($x = $i; $x > 0; $x--)
531 $arr[$x] = $arr[$x - 1];
536 * Move an item to the end of an array.
538 function _views_move_bottom(&$arr, $i) {
539 $end = count($arr) - 1;
540 if ($i >= $end || $i < 0) {
541 return; // can't do it.
544 for ($x = $i; $x < $end; $x++)
545 $arr[$x] = $arr[$x + 1];
550 * Figure out which of the many, many buttons on a form were clicked and
553 function _views_check_sub_ops(&$form, &$order, $i) {
555 if ($form['delete']['#value']) {
556 unset($form['delete']['#value']);
558 $order = array_values($order); // reindex
559 $form['delete']['#printed'] = true
;
560 $form['up']['#printed'] = true
;
561 $form['down']['#printed'] = true
;
562 $form['top']['#printed'] = true
;
563 $form['bottom']['#printed'] = true
;
566 else foreach (array('up', 'down', 'top', 'bottom') as
$dir) {
567 if ($form[$dir]['#value']) {
568 unset ($form[$dir]['#value']);
569 $func = "_views_move_$dir";
578 * Figure out if one of the add buttons on a form were clicked, and handle it.
580 function _views_check_ops(&$view, $op, $form) {
581 if ($op == t('Add Filter')) {
582 $view->new_filter
['id'] = $form['filter']['add']['id']['#value'];
585 else if ($op == t('Add Criteria')) {
586 $view->new_sort
['id'] = $form['sort']['add']['id']['#value'];
589 else if ($op == t('Add Argument')) {
590 $view->new_argument
['id'] = $form['argument']['add']['id']['#value'];
593 else if ($op == t('Add Field')) {
594 $fieldbits = explode('.', $form['field']['add']['id']['#value']);
595 $view->new_field
['id'] = $form['field']['add']['id']['#value'];
596 $view->new_field
['tablename'] = $fieldbits[0];
597 $view->new_field
['field'] = $fieldbits[1];
598 $view->new_field
['label'] = $fieldnames[$form['field']['add']['id']['#value']];
599 $view->new_field
['queryname'] = "$fieldbits[0]_$fieldbits[1]";
602 else if ($op == t('Expose Filter')) {
603 $view->new_exposed_filter
['id'] = $form['exposed_filter']['add']['id']['#value'];
609 * Custom form element to do our nice images.
611 function views_elements() {
612 $type['imagebutton'] = array('#input' => TRUE
, '#button_type' => 'submit',);
616 function theme_imagebutton($element) {
617 return '<input type="image" class="form-'.
$element['#button_type'] .
'" name="'.
$element['#name'] .
'" value="'.
check_plain($element['#default_value']) .
'" '.
drupal_attributes($element['#attributes']) .
' src="' .
$element['#image'] .
'" alt="' .
$element['#title'] .
'" title="' .
$element['#title'] .
"\" />\n";
620 function imagebutton_value() {
621 // null function guarantees default_value doesn't get moved to #value.
625 * Display all the guts of a view in a form for editing.
627 function _views_view_form($view, $op = '') {
628 _views_check_arrays($view); // make sure arrays that might be empty get set
630 // Put in all our add buttons, then process them to see if they've been hit.
631 views_ui_add_add_button($form, 'field', _views_get_fields(true
), t('Add Field'));
632 views_ui_add_add_button($form, 'argument', _views_get_arguments(true
), t('Add Argument'));
633 views_ui_add_add_button($form, 'filter', _views_get_filters(true
), t('Add Filter'));
634 views_ui_add_add_button($form, 'sort', _views_get_sorts(true
), t('Add Criteria'));
636 $allbut = _views_check_ops($view, $op, $form);
637 if ($_POST['edit'] && $op != t('Save')) {
638 drupal_set_message(t('You have modified this view; changes will not be recorded until you Save the form.'));
641 $form['exposed_filter'] = array();
642 foreach (array('field', 'argument', 'filter', 'exposed_filter', 'sort') as
$section) {
643 if (views_ui_add_section($form[$section], $view, $section)) {
648 $form['vid'] = array(
650 '#value' => $view->vid
,
652 $form['allbut'] = array(
656 $form['changed'] = array(
658 '#value' => $view->changed
,
661 $form['basic-info'] = array(
662 '#type' => 'fieldset',
663 '#collapsible' => true
,
664 '#collapsed' => ($allbut != NULL
),
665 '#title' => t('Basic Information'),
668 $form['basic-info']['name'] = array(
669 '#type' => 'textfield',
670 '#title' => t('Name'),
671 '#default_value' => $view->name
,
674 '#description' => t('The unique identifier of the view; it is only important for overridden views and views that modules or themes will need to use.'),
678 $form['basic-info']['access'] = array(
679 '#type' => 'checkboxes',
680 '#title' => t('Access'),
681 '#default_value' => $view->access
,
682 '#options' => views_handler_filter_role(),
683 '#description' => t('Only the checked roles will be able to see this view in any form; if no roles are checked, access will not be restricted.'),
686 $form['basic-info']['description'] = array(
687 '#type' => 'textfield',
688 '#title' => t('Description'),
689 '#default_value' => $view->description
,
692 '#description' => t('A description of the view for the admin list.'),
697 $form['page-info'] = array(
698 '#type' => 'fieldset',
699 '#collapsible' => true
,
700 '#collapsed' => ($allbut != NULL
|| !$view->page
),
701 '#title' => t('Page'),
704 $form['page-info']['page'] = array(
705 '#type' => 'checkbox',
706 '#title' => t('Provide Page View'),
707 '#return_value' => 1,
708 '#default_value' => $view->page
,
709 '#description' => t('If checked this view will be provided as a page. If not checked, the fields in this group will be ignored.'),
712 $form['page-info']['url'] = array(
713 '#type' => 'textfield',
714 '#title' => t('URL'),
715 '#default_value' => $view->url
,
718 '#description' => t("Enter the URL to use for this view in the form of 'dir/dir'. Do not begin or end the URL with a /. Example: 'view/tracker'. This is required if providing a page view."),
721 $form['page-info']['page_type'] = array(
723 '#title' => t('View Type'),
724 '#default_value' => $view->page_type
,
725 '#options' => _views_get_style_plugins(true
),
726 '#description' => t('How the nodes should be displayed to the user.'),
729 $form['page-info']['page_title'] = array(
730 '#type' => 'textfield',
731 '#title' => t('Title'),
732 '#default_value' => $view->page_title
,
735 '#description' => t('The title that be shown at the top of the view. May be blank. If your View allows arguments, you may use %1 to substitute argument 1 text, %2 to argument #2, and so on.'),
738 $form['page-info']['use_pager'] = array(
739 '#type' => 'checkbox',
740 '#title' => t('Use Pager'),
741 '#return_value' => 1,
742 '#default_value' => $view->use_pager
,
743 '#description' => t('If checked this query may be multiple pages. If not checked this query will be one page.'),
745 $form['page-info']['breadcrumb_no_home'] = array(
746 '#type' => 'checkbox',
747 '#title' => t('Breadcrumb trail should not include "Home"'),
748 '#return_value' => 1,
749 '#default_value' => $view->breadcrumb_no_home
,
750 '#description' => t('If checked the breadcrumb trail for this page will discard "Home". Usually you will not set this, but this is used for the Front Page View, where it IS Home and should not leave a trail to itself.'),
752 $form['page-info']['nodes_per_page'] = array(
753 '#type' => 'textfield',
754 '#title' => t('Nodes per Page'),
755 '#default_value' => $view->nodes_per_page
,
758 '#description' => t('The number of nodes to display per page. If 0 no nodes will be displayed. If not using a pager, this will be the maximum number of nodes in the list.'),
759 '#attributes' => NULL
,
762 $form['page-info']['page_header_fieldset'] = array(
763 '#type' => 'fieldset',
764 '#collapsible' => TRUE
,
765 '#collapsed' => TRUE
,
766 '#title' => t('Header'),
768 $form['page-info']['page_header_fieldset']['page_header'] = array(
769 '#type' => 'textarea',
770 '#default_value' => $view->page_header
,
773 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
776 $form['page-info']['page_header_fieldset']['page_header_format'] = filter_form($view->page_header_format
, 1, array('page_header_format'));
778 $form['page-info']['page_footer_fieldset'] = array(
779 '#type' => 'fieldset',
780 '#collapsible' => TRUE
,
781 '#collapsed' => TRUE
,
782 '#title' => t('Footer'),
784 $form['page-info']['page_footer_fieldset']['page_footer'] = array(
785 '#type' => 'textarea',
786 '#default_value' => $view->page_footer
,
789 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
792 $form['page-info']['page_footer_fieldset']['page_footer_format'] = filter_form($view->page_footer_format
, 1, array('page_footer_format'));
794 $form['page-info']['page_empty_fieldset'] = array(
795 '#type' => 'fieldset',
796 '#collapsible' => TRUE
,
797 '#collapsed' => TRUE
,
798 '#title' => t('Empty Text'),
800 $form['page-info']['page_empty_fieldset']['page_empty'] = array(
801 '#type' => 'textarea',
802 '#default_value' => $view->page_empty
,
805 '#description' => t('Text to display if a view returns no nodes. Optional.'),
808 $form['page-info']['page_empty_fieldset']['page_empty_format'] = filter_form($view->page_empty_format
, 1, array('page_empty_format'));
810 $form['page-info']['menu-info'] = array(
811 '#type' => 'fieldset',
812 '#collapsible' => TRUE
,
813 '#collapsed' => TRUE
,
814 '#title' => t('Menu'),
817 $form['page-info']['menu-info']['menu'] = array(
818 '#type' => 'checkbox',
819 '#title' => t('Provide Menu'),
820 '#return_value' => 1,
821 '#default_value' => $view->menu
,
822 '#description' => t('If checked this view be given a menu entry in the Drupal menu system. If not checked the data in this group will be ignored.'),
825 $form['page-info']['menu-info']['menu_tab'] = array(
826 '#type' => 'checkbox',
827 '#title' => t('Provide Menu as Tab'),
828 '#return_value' => 1,
829 '#default_value' => $view->menu_tab
,
830 '#description' => t("If checked this view's menu entry will be provided as a tab rather than in the main menu system."),
833 $form['page-info']['menu-info']['menu_tab_default'] = array(
834 '#type' => 'checkbox',
835 '#title' => t('Make Default Menu Tab'),
836 '#return_value' => 1,
837 '#default_value' => $view->menu_tab_default
,
838 '#description' => t("If checked this view's menu entry will be provided as a tab, and will be the default tab for that URL path. For example, if the URL is 'tracker/all' and it is set as the default menu tab, it will be put into the menu as 'tracker' and 'tracker/all' will be the default tab. For tabs to work properly, one tab in the group must be set as the default."),
841 $form['page-info']['menu-info']['menu_tab_weight'] = array(
842 '#type' => 'textfield',
843 '#title' => t('Tab Weight'),
844 '#default_value' => $view->menu_tab_weight
,
846 '#description' => t('If this is a menu tab, select the weight; lower numbers will be further to the left.'),
849 $form['page-info']['menu-info']['menu_title'] = array(
850 '#type' => 'textfield',
851 '#title' => t('Menu Title'),
852 '#default_value' => $view->menu_title
,
855 '#description' => t('Enter the title to use for the menu entry or tab. If blank, the page title will be used.'),
860 $form['block-info'] = array(
861 '#type' => 'fieldset',
862 '#collapsible' => true
,
863 '#collapsed' => ($allbut != NULL
|| !$view->block
),
864 '#title' => t('Block'),
867 $form['block-info']['block'] = array(
868 '#type' => 'checkbox',
869 '#title' => t('Provide Block'),
870 '#return_value' => 1,
871 '#default_value' => $view->block
,
872 '#description' => t('If checked this view will be provided as a block. If checked title may not be blank.'),
875 $form['block-info']['block_type'] = array(
877 '#title' => t('View Type'),
878 '#default_value' => $view->block_type
,
879 '#options' => _views_get_style_plugins(true
),
880 '#description' => t('How the nodes should be displayed to the user.'),
883 $form['block-info']['block_title'] = array(
884 '#type' => 'textfield',
885 '#title' => t('Title'),
886 '#default_value' => $view->block_title
,
889 '#description' => t('The title that will be shown at the top of the block. May be blank.'),
892 $form['block-info']['nodes_per_block'] = array(
893 '#type' => 'textfield',
894 '#title' => t('Nodes per Block'),
895 '#default_value' => $view->nodes_per_block
,
898 '#description' => t('If using a block, the maximum number of items to display in the block. Pagers are not used in blocks.'),
899 '#attributes' => NULL
,
902 $form['block-info']['block_more'] = array(
903 '#type' => 'checkbox',
904 '#title' => t('[More] Link?'),
905 '#return_value' => 1,
906 '#default_value' => $view->block_more
,
907 '#description' => t('If using a view as both a page and a block, display a more link in the block that links to the view URL?'),
910 $form['block-info']['block_header_fieldset'] = array(
911 '#type' => 'fieldset',
912 '#collapsible' => TRUE
,
913 '#collapsed' => TRUE
,
914 '#title' => t('Header'),
916 $form['block-info']['block_header_fieldset']['block_use_page_header'] = array(
917 '#type' => 'checkbox',
918 '#title' => t('Use Page Header'),
919 '#return_value' => 1,
920 '#default_value' => $view->block_use_page_header
,
921 '#description' => t('If checked, use the Page Header for block view instead. If so, you should leave the Block Header blank.'),
924 $form['block-info']['block_header_fieldset']['block_header'] = array(
925 '#type' => 'textarea',
926 '#title' => t('Header'),
927 '#default_value' => $view->block_header
,
930 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
933 $form['block-info']['block_header_fieldset']['block_header_format'] = filter_form($view->block_header_format
, 1, array( 'block_header_format'));
935 $form['block-info']['block_footer_fieldset'] = array(
936 '#type' => 'fieldset',
937 '#collapsible' => TRUE
,
938 '#collapsed' => TRUE
,
939 '#title' => t('Footer'),
941 $form['block-info']['block_footer_fieldset']['block_use_page_footer'] = array(
942 '#type' => 'checkbox',
943 '#title' => t('Use Page Footer'),
944 '#return_value' => 1,
945 '#default_value' => $view->block_use_page_footer
,
946 '#description' => t('If checked, use the page footer for block view instead. If so, you should leave the block footer blank.'),
949 $form['block-info']['block_footer_fieldset']['block_footer'] = array(
950 '#type' => 'textarea',
951 '#title' => t('Footer'),
952 '#default_value' => $view->block_footer
,
955 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
958 $form['block-info']['block_footer_fieldset']['block_footer_format'] = filter_form($view->block_footer_format
, 1, array( 'block_footer_format'));
960 $form['block-info']['block_empty_fieldset'] = array(
961 '#type' => 'fieldset',
962 '#collapsible' => TRUE
,
963 '#collapsed' => TRUE
,
964 '#title' => t('Empty text'),
966 $form['block-info']['block_empty_fieldset']['block_use_page_empty'] = array(
967 '#type' => 'checkbox',
968 '#title' => t('Use Page empty'),
969 '#return_value' => 1,
970 '#default_value' => $view->block_use_page_empty
,
971 '#description' => t('If checked, use the Page Empty Text for block view instead. If so, you should leave the block empty text blank.'),
974 $form['block-info']['block_empty_fieldset']['block_empty'] = array(
975 '#type' => 'textarea',
976 '#title' => t('Empty text'),
977 '#default_value' => $view->block_empty
,
980 '#description' => t('Text to display if a view results in no nodes. Optional.'),
983 $form['block-info']['block_empty_fieldset']['block_empty_format'] = filter_form($view->block_empty_format
, 1, array( 'block_empty_format'));
985 $access = user_access('use PHP for block visibility');
987 $form['view_args_php_fieldset'] = array(
988 '#type' => 'fieldset',
989 '#collapsible' => TRUE
,
990 '#collapsed' => TRUE
,
991 '#title' => t('Argument Handling Code'),
994 $form['view_args_php_fieldset']['view_args_php'] = array(
995 '#type' => 'textarea',
996 '#title' => t('Argument Code'),
997 '#default_value' => $view->view_args_php
,
1000 '#description' => t('Advanced Usage Only: PHP code that returns a custom array of arguments for the view.'),
1003 $form['view_args_php_fieldset']['view_args_php'] = array(
1005 '#value' => $view->view_args_php
,
1009 $form['save'] = array(
1010 '#type' => 'submit',
1011 '#value' => t('Save'),
1014 $form['delete'] = array(
1015 '#type' => 'submit',
1016 '#value' => t('Delete'),
1019 $form['cancel'] = array(
1020 '#type' => 'submit',
1021 '#value' => t('Cancel'),
1024 return drupal_get_form('views_edit_view', $form);
1028 * separately build a piece of the form
1030 function views_ui_build_form(&$form) {
1031 $form = form_builder('views_edit_view', $form);
1035 * Add the 'add' button for a section into the form
1037 function views_ui_add_add_button(&$form, $section, $options, $label) {
1038 $form[$section]['add'] = array('#tree' => true
, '#parents' => array($section, 'add'));
1040 $form[$section]['add']['id'] = array(
1041 '#type' => 'select',
1042 '#options' => $options,
1044 $form[$section]['add']['button'] = array(
1045 '#type' => 'button',
1048 views_ui_build_form($form[$section]['add']);
1052 * Add one of the list sections to the form.
1054 function views_ui_add_section(&$form, &$view, $section) {
1055 // add fields to the form.
1057 $form += array('#tree' => true
, '#parents' => array($section));
1058 $view_section = &$view->$section;
1060 $num_items = count($view_section);
1061 // Populate the form with fields we need to check to track
1062 // changes through the form's life.
1063 $form['count'] = array('#type' => 'hidden', '#default_value' => $num_items);
1065 $form['order'] = array(
1066 '#type' => 'hidden',
1067 '#default_value' => $num_items ?
implode(',', range(0, $num_items - 1)) : '',
1070 // Run the builder to get the value on the form.
1071 views_ui_build_form($form);
1072 $order = $form['order']['#value'] != ''
1073 ?
explode(',', $form['order']['#value'])
1076 // Shortcut because operator precedence gets sticky if using $foo->$bar[]
1077 for ($i = $num_items; $i < $form['count']['#value']; $i++) {
1078 $view_section[] = array();
1081 $new_section = "new_$section";
1082 // instantiate the new field if one was added.
1083 if ($view->$new_section) {
1084 $view_section[] = $view->$new_section;
1085 $order[] = $form['count']['#value'];
1086 $form['count']['#value']++;
1089 $func = "views_ui_add_$section";
1091 foreach ($order as
$key => $i) {
1092 $form[$i] = array('#tree' => true
, '#parents' => array($section, $i));
1094 $form[$i]['id'] = array(
1095 '#type' => 'hidden',
1096 '#default_value' => $view_section[$i]['id'],
1099 views_ui_add_buttons($form[$i]);
1100 views_ui_build_form($form[$i]);
1102 $retval = _views_check_sub_ops($form[$i], $order, $key);
1103 if ($retval !== 'delete') {
1104 $retval2 = $func($form[$i], $view_section[$i], $order, $key, $i, $view);
1107 if ($retval || $retval2) {
1111 $form['order']['#value'] = implode(',', $order);
1117 * Add all the info for a single field into the form.
1119 function views_ui_add_field(&$form, $field, &$order, $key, $i) {
1120 $fields = _views_get_fields();
1121 $fieldname = $form['id']['#value']; // combined table & fieldname
1122 $fieldinfo = $fields[$fieldname]; // laziness + readability
1124 $form['fullname'] = array(
1125 '#type' => 'hidden',
1126 '#default_value' => $field['id'],
1129 $form['name'] = array(
1130 '#type' => 'markup',
1131 '#value' => $fieldinfo['name'], // combined table & fieldname
1133 $form['queryname'] = array(
1134 '#type' => 'hidden',
1135 '#default_value' => $field['queryname'],
1137 $form['tablename'] = array(
1138 '#type' => 'hidden',
1139 '#default_value' => $field['tablename'],
1141 $form['field'] = array(
1142 '#type' => 'hidden',
1143 '#default_value' => $field['field'],
1146 $form['label'] = array(
1147 '#type' => 'textfield',
1148 '#default_value' => $field['label'],
1150 '#maxlength' => 255,
1153 if (is_array($fieldinfo['handler'])) {
1154 $form['handler'] = array(
1155 '#type' => 'select',
1156 '#default_value' => $field['handler'],
1157 '#options' => $fieldinfo['handler'],
1161 if (isset($fieldinfo['option'])) {
1162 $widget = $fieldinfo['option'];
1163 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1164 $widget['#options'] = $widget['#options']('option', $fieldinfo['option']);
1166 $widget['#default_value'] = $field['options'];
1167 $form['options'] = $widget;
1171 if ($fieldinfo['sortable']) {
1172 $form['sortable'] = array(
1173 '#type' => 'select',
1174 '#default_value' => $field['sortable'],
1175 '#options' => array('0' => t('No'), '1' => t('Yes')),
1177 $form['defaultsort'] = array(
1178 '#type' => 'select',
1179 '#default_value' => $field['defaultsort'],
1180 '#options' => array('0' => t('None'), 'ASC' => t('Ascending'), 'DESC' => t('Descending')),
1183 if (isset($fieldinfo['help'])) {
1184 $form['help'] = array(
1185 '#type' => 'markup',
1186 '#value' => '<div class="description">' .
$fieldinfo['help'] .
'</div>',
1192 * Add all the info for a single argument into the form.
1194 function views_ui_add_argument(&$form, $argument, &$order, $key, $i) {
1195 $arguments = _views_get_arguments();
1196 $arg_type = $form['id']['#value'];
1197 $arginfo = $arguments[$arg_type];
1199 $form['type'] = array(
1200 '#type' => 'hidden',
1201 '#default_value' => $argument['id'],
1204 $form['name'] = array(
1205 '#value' => $arginfo['name'],
1208 $form['argdefault'] = array(
1209 '#type' => 'select',
1210 '#default_value' => max(1, intval($argument['argdefault'])),
1211 '#options' => _views_get_arguments_default(),
1213 $form['title'] = array(
1214 '#type' => 'textfield',
1215 '#default_value' => $argument['title'],
1217 '#maxlength' => 255,
1219 if (isset($arginfo['option'])) {
1220 $widget = $arginfo['option'];
1221 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1222 $widget['#options'] = $widget['#options']('option', $arginfo['option']);
1224 $widget['#default_value'] = $argument['options'];
1225 $form['options'] = $widget;
1228 if (isset($arginfo['help'])) {
1229 $form['help'] = array(
1230 '#type' => 'markup',
1231 '#value' => $arginfo['help'],
1237 * Add all the info for a single filter into the form.
1239 function views_ui_add_filter(&$form, $filter, &$order, $key, $i, &$view) {
1240 $filters = _views_get_filters();
1241 $filtername = $form['id']['#value']; // combined table & filtername
1242 $filterinfo = $filters[$filtername];
1244 $form['field'] = array(
1245 '#type' => 'hidden',
1246 '#default_value' => $filter['id'],
1249 $form['name'] = array(
1250 '#type' => 'markup',
1251 '#value' => $filterinfo['name'],
1254 // 'operator' is either an array or a handler
1255 $operator = $filterinfo['operator'];
1256 if (!is_array($operator) && function_exists($filterinfo['operator'])) {
1257 $operator = $filterinfo['operator']('operator', $filterinfo);
1260 $form['operator'] = array(
1261 '#type' => 'select',
1262 '#default_value' => $filter['operator'],
1263 '#options' => $operator,
1265 if (isset($filterinfo['option'])) {
1266 $widget = $filterinfo['option'];
1267 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1268 $widget['#options'] = $widget['#options']('option', $filterinfo['option']);
1270 $widget['#default_value'] = $filter['options'];
1271 $form['options'] = $widget;
1274 $form['value'] = $filterinfo['value'];
1275 if (!is_array($form['value']['#options']) && function_exists($form['value']['#options'])) {
1276 $form['value']['#options'] = $form['value']['#options']('value', $filterinfo);
1278 $form['value']['#default_value'] = $filter['value'];
1280 if (isset($filterinfo['help'])) {
1281 $form['help'] = array(
1282 '#type' => 'markup',
1283 '#value' => $filterinfo['help'],
1287 // This is kind of a pain in the butt -- The only way to know all of
1288 // the filters that have been exposed is to look at the form, but that
1289 // part of the form hasn't been built yet, so I can't use the form_builder
1290 // method. I can't put this after that, either, because then I'd have to
1291 // go back to add one if we see one here. So we cheat heavily with _POST.
1293 if ($_POST['edit']) {
1294 $exposed_order = $_POST['edit']['exposed_filter']['order'];
1295 if ($exposed_order !== '') {
1296 $exposed_order = explode(',', $exposed_order);
1297 foreach ($exposed_order as
$x) {
1298 if ($_POST['edit']['exposed_filter'][$x]['id'] == $filtername) {
1305 else { // do this if there's never been a submit.
1306 $exposed_order = array_keys($view->exposed_filter
);
1307 foreach ($exposed_order as
$x) {
1308 if ($view->exposed_filter
[$x]['id'] == $filtername) {
1315 // I can't find a way to see if this button was pressed other than
1316 // directly checking, because #form_submitted doesn't work on arrays
1318 if ($_POST['edit']['filter'][$i]['expose']) {
1319 $view->new_exposed_filter
['id'] = $filtername;
1323 $form['expose'] = array(
1324 '#type' => 'button',
1325 '#default_value' => t('Expose'),
1326 '#name' => "edit[filter][$i][expose]",
1333 * Add all the info for a single exposed filter into the form.
1335 function views_ui_add_exposed_filter(&$form, $filter, &$order, $key, $i) {
1336 $filters = _views_get_filters();
1337 $filtername = $form['id']['#value']; // combined table & filtername
1338 $filterinfo = $filters[$filtername];
1340 $form['field'] = array(
1341 '#type' => 'hidden',
1342 '#default_value' => $filter['id'],
1345 $form['name'] = array(
1346 '#type' => 'markup',
1347 '#value' => $filterinfo['name'],
1350 $form['label'] = array(
1351 '#type' => 'textfield',
1352 '#default_value' => $filter['label'],
1354 '#maxlength' => 255,
1357 $form['optional'] = array(
1358 '#type' => 'checkbox',
1359 '#default_value' => $filter['optional'],
1362 $form['is_default'] = array(
1363 '#type' => 'checkbox',
1364 '#default_value' => $filter['is_default'],
1367 $form['single'] = array(
1368 '#type' => 'checkbox',
1369 '#default_value' => $filter['single'],
1372 $form['operator'] = array(
1373 '#type' => 'checkbox',
1374 '#default_value' => $filter['operator'],
1379 * Add all the info for a single sort into the form.
1381 function views_ui_add_sort(&$form, $sort, &$order, $key, $i) {
1382 $sorts = _views_get_sorts();
1383 $sortname = $form['id']['#value']; // combined table & sortname
1384 $sortinfo = $sorts[$sortname];
1386 $form['field'] = array(
1387 '#type' => 'hidden',
1388 '#default_value' => $sort['id'],
1391 $form['name'] = array(
1392 '#value' => $sortinfo['name'],
1395 $form['sortorder'] = array(
1396 '#type' => 'select',
1398 '#default_value' => $sort['sortorder'],
1399 '#options' => _views_sortorders(),
1401 // option can be either 'string', 'integer', an array or a handler
1402 // that must return an array.
1403 if (isset($sortinfo['option'])) {
1404 $widget = $sortinfo['option'];
1405 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
1406 $widget['#options'] = $widget['#options']('option', $sortinfo['option']);
1408 $widget['#default_value'] = $sort['options'];
1409 $form['options'] = $widget;
1411 if (isset($sortinfo['help'])) {
1412 $form['help'] = array(
1413 '#type' => 'markup',
1414 '#value' => $sortinfo['help'],
1420 * Add the up, down, top, bottom, delete buttons to a form.
1422 function views_ui_add_buttons(&$form_item) {
1423 $form_item['delete'] = views_ui_add_button('user-trash.png', t('Delete'), t('Delete this item.'));
1424 $form_item['up'] = views_ui_add_button('go-up.png', t('Up'), t('Move this item up.'));
1425 $form_item['down'] = views_ui_add_button('go-down.png', t('Down'), t('Move this item down.'));
1426 $form_item['top'] = views_ui_add_button('go-top.png', t('Top'), t('Move this item to the top.'));
1427 $form_item['bottom'] = views_ui_add_button('go-bottom.png', t('Bottom'), t('Move this item to the bottom.'));
1431 * Add a single button to a form.
1433 function views_ui_add_button($image, $name, $text) {
1434 $module_path = base_path() .
drupal_get_path('module', 'views');
1437 '#type' => 'imagebutton',
1438 '#image' => $module_path .
'/' .
$image,
1440 '#default_value' => $name,
1445 * Display the form built by _views_view_form()
1447 function theme_views_edit_view($form) {
1449 $output .
= form_render($form['basic-info'], false
);
1450 $output .
= form_render($form['page-info'], false
);
1451 $output .
= form_render($form['block-info'], false
);
1453 $allbut = $form['allbut']['#value'];
1455 // form_render wants a reference so use full array notation.
1458 $collapsed = $allbut && $allbut != 'field';
1459 $group = views_ui_render_section($form['field'], array('name', 'label', 'handler', 'options', 'sortable', 'defaultsort'), array(t('Name'), t('Label'), t('Handler'), t('Option'), t('Sortable'), t('Default Sort')), 'fields', $collapsed);
1460 $group .
= views_ui_render_section_add($form['field']['add'], array('id', 'button'), 'Add Field');
1462 $group .
= '<p>'.
t('Fields are only meaningful with List view and Table View; they allow you to choose which fields are presented and in what order.') .
'</p>';
1463 $output .
= theme('fieldset', array('#title' => t('Fields'), '#children' => $group,
1464 '#collapsible' => true
, '#collapsed' => $collapsed,));
1468 $collapsed = $allbut && $allbut != 'argument';
1469 $group = views_ui_render_section($form['argument'], array('name', 'argdefault', 'title', 'options'), array(t('Argument Type'), t('Default'), t('Title'), t('Option')), 'arguments', $collapsed);
1471 $group .
= views_ui_render_section_add($form['argument']['add'], array('id', 'button'), 'Add Argument');
1473 $group .
= form_render($form['view_args_php_fieldset']);
1475 $group .
= '<p>'.
t('Arguments are parsed directly from the URL. They are not necessary to any given view, but allow flexibility.') .
'</p>';
1476 $output .
= theme('fieldset', array('#title' => t('Arguments'), '#children' => $group,
1477 '#collapsible' => true
, '#collapsed' => $collapsed,));
1480 $collapsed = $allbut && $allbut != 'filter';
1481 $group = views_ui_render_section($form['filter'], array('name', 'operator', 'value', 'options', 'expose'), array(t('Field'), t('Operator'), t('Value'), t('Option'), ''), 'filters', $collapsed);
1483 $group .
= views_ui_render_section_add($form['filter']['add'], array('id', 'button'), 'Add Filter');
1485 $group .
= '<p>'.
t('Filters allow you to select a subset of all the nodes to display. All Filters are ANDed together.') .
'</p>';
1486 $output .
= theme('fieldset', array('#title' => t('Filters'), '#children' => $group,
1487 '#collapsible' => true
, '#collapsed' => $collapsed,));
1490 // exposed exposed_filter
1491 $collapsed = $allbut && $allbut != 'exposed_filter';
1492 $group = views_ui_render_section($form['exposed_filter'], array('name', 'label', 'optional', 'is_default', 'single', 'operator'), array(t('Field'), t('Label'), t('Optional'), t('Filter settings Default'), t('Force Single'), t('Lock Operator')), 'exposed_filters', $collapsed);
1495 // $group .= views_ui_render_section_add($form['exposed_filter']['add'], array('id', 'button'), 'Expose Filter');
1497 $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>';
1498 $output .
= theme('fieldset', array('#title' => t('Exposed Filters'), '#children' => $group,
1499 '#collapsible' => true
, '#collapsed' => $collapsed,));
1503 $collapsed = $allbut && $allbut != 'sort';
1504 $group = views_ui_render_section($form['sort'], array('name', 'sortorder', 'options'), array(t('Field'), t('Order'), t('Option')), 'sort criteria', $collapsed);
1506 $group .
= views_ui_render_section_add($form['sort']['add'], array('id', 'button'), 'Add criteria');
1507 $output .
= theme('fieldset', array('#title' => t('Sort Criteria'), '#children' => $group,
1508 '#collapsible' => true
, '#collapsed' => $collapsed,));
1510 $output .
= form_render($form, false
);
1515 * Render one of the dynamic sections on our form.
1517 function views_ui_render_section(&$form, $items, $header, $section, &$collapsed) {
1518 // always do the buttons
1519 $items = array_merge($items, array('delete', 'top', 'up', 'down', 'bottom'));
1520 $num_items = count($items);
1522 $order = ($form['order']['#value'] != ''
1523 ?
explode(',', $form['order']['#value'])
1526 $count = count($order) - 1;
1527 foreach ($order as
$key => $i) {
1529 foreach ($items as
$item) {
1530 if (($key == 0 && ($item == 'up' || $item == 'top')) ||
1531 ($key == $count && ($item == 'down' || $item == 'bottom'))) {
1532 $form[$i][$item]['#printed'] = true
;
1533 $row[] = ' '; // array('data' => ' ', 'width' => 16);
1536 $row[] = form_render($form[$i][$item], false
);
1540 if (isset($form[$i]['help'])) {
1541 $rows[] = array(array('data' => form_render($form[$i]['help']), 'colspan' => $num_items));
1545 $rows[] = array(array('data' => t('This view currently has no %s defined.', array('%s' => $section)), 'colspan' => $num_items));
1549 $header[] = array('data' => t('Ops'), 'colspan' => 5);
1550 return theme('table', $header, $rows) .
"<br />";
1554 * Render the add buttons for one of the dynamic sections on our form.
1556 function views_ui_render_section_add(&$form, $items, $label) {
1557 foreach ($items as
$item) {
1558 $row[] = form_render($form[$item], false
);
1562 $header = array(array('data' => t($label), 'colspan' => count($items)));
1563 return theme('table', $header, $rows) .
"<br />";
1567 * Validate that a view sent via form is OK.
1569 function views_edit_view_validate($form_id, $view, $form) {
1571 if ($op != t('Save')) {
1572 return; // only validate on saving!
1576 $changed = db_result(db_query("SELECT changed FROM {view_view} WHERE vid = %d", $view['vid']));
1577 if ($changed && $view['changed'] != $changed) {
1578 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!'));
1583 if (!$view['name']) {
1584 form_error($form['basic-info']['name'], t('View name is required.'));
1587 // view name must be alphanumeric or underscores, no other punctuation.
1588 if (preg_match('/[^a-zA-Z0-9_]/', $view['name'])) {
1589 form_error($form['basic-info']['name'], t('View name must be alphanumeric or underscores only.'));
1592 // test uniqueness of name
1593 $vid = db_result(db_query("SELECT vid FROM {view_view} WHERE name='%s'", $view['name']));
1594 if ($vid && $vid != $view['vid']) {
1595 form_error($form['basic-info']['name'], t('View name already in use.'));
1598 if ($view['use_block'] && $view['nodes_per_block'] < 1) {
1599 form_set_error($form['block-info']['nodes_per_block'], t('If being used as a block, Nodes Per Block must be positive.'));
1602 // validation based on type:
1604 $plugins = _views_get_style_plugins();
1605 if ($view['page']) {
1606 $validator = $plugins[$view['page_type']]['validate'];
1607 if (function_exists($validator)) {
1608 $validator('page', $view, $form);
1612 if ($view['block']) {
1613 $validator = $plugins[$view['block_type']]['validate'];
1614 if (function_exists($validator)) {
1615 $validator('block', $view, $form);
1619 foreach (array('field', 'argument', 'sort', 'filter') as
$type) {
1620 $function = "_views_get_$type" .
's';
1621 $info = $function();
1622 if (is_array($view->$type)) {
1623 foreach ($view->$type as
$key => $data) {
1624 if (!is_numeric($key)) {
1625 continue; // some non-data data is in here.
1627 $validator = $info[$data['id']]['validate'];
1628 if (function_exists($validator)) {
1629 $validator($data, $view, $form);
1636 function views_ui_reorder(&$view) {
1637 // re-order things as they were ordered on the form.
1638 $order = $view['order'] != '' ?
explode(',', $view['order']) : array();
1639 foreach ($order as
$position => $key) {
1640 $placeholder[] = $view[$key];
1642 $view = $placeholder;
1646 * Handle submit buttons on a view form.
1648 function views_edit_view_submit($form_id, $form) {
1649 $view = (object) $form;
1651 // Transform any data that requires it here.
1652 $view->access
= array_keys(array_filter($view->access
));
1654 // ensure for MYSQL 5 irritation reasons.
1655 $view->nodes_per_page
= intval($view->nodes_per_page
);
1656 $view->nodes_per_block
= intval($view->nodes_per_block
);
1657 $view->menu_tab_weight
= intval($view->menu_tab_weight
);
1659 // re-order things as they were ordered on the form.
1660 foreach (array('field', 'argument', 'filter', 'sort', 'exposed_filter') as
$section) {
1661 views_ui_reorder($view->$section);
1664 _views_save_view($view);
1667 drupal_set_message(t('View successfully saved.'));
1670 drupal_set_message(t('View successfully added.'));
1673 return drupal_goto('admin/views');
1677 * Validate a view with a type: list.
1679 function views_ui_plugin_validate_list($type, $view, $form) {
1680 // list (and table) modes require there to be at least 1 field active.
1681 if (is_array($view['field'])) {
1682 $fields = array_filter(array_keys($view['field']), 'is_numeric');
1685 form_error($form["$type-info"][$type .
'_type'], "List and Table types require at least one field.");
1690 * Validate a view with type: table.
1692 function views_ui_plugin_validate_table($type, $view, $form) {
1693 // pass thru to list cause for now they're the same.
1694 return views_ui_plugin_validate_list($type, $view, $form);
1696 // ---------------------------------------------------------------------------
1697 // View creator tool
1702 function views_tf($val) {
1703 return ($val ?
'TRUE' : 'FALSE');
1707 * This function creates view code for a view.
1709 function views_create_view_code($vid) {
1710 $view = _views_load_view($vid);
1712 return "View '$vid' not found.";
1715 $requires = array();
1717 $output = " \$view = new stdClass();\n";
1718 $output .
= " \$view->name = '" .
db_escape_string($view->name
) .
"';\n";
1719 $output .
= " \$view->description = '" .
db_escape_string($view->description
) .
"';\n";
1720 $output .
= " \$view->access = " .
var_export($view->access
, true
) .
";\n";
1721 if (user_access('use PHP for block visibility')) {
1722 $output .
= " \$view->view_args_php = '" .
db_escape_string($view->view_args_php
) .
"';\n";
1726 $output .
= " \$view->page = " .
views_tf($view->page
) .
";\n";
1727 $output .
= " \$view->page_title = '" .
db_escape_string($view->page_title
) .
"';\n";
1728 $output .
= " \$view->page_header = '" .
db_escape_string($view->page_header
) .
"';\n";
1729 $output .
= " \$view->page_header_format = '$view->page_header_format';\n";
1730 $output .
= " \$view->page_footer = '" .
db_escape_string($view->page_footer
) .
"';\n";
1731 $output .
= " \$view->page_footer_format = '$view->page_footer_format';\n";
1732 $output .
= " \$view->page_empty = '" .
db_escape_string($view->page_empty
) .
"';\n";
1733 $output .
= " \$view->page_empty_format = '$view->page_empty_format';\n";
1734 $output .
= " \$view->page_type = '$view->page_type';\n";
1735 $output .
= " \$view->url = '" .
db_escape_string($view->url
) .
"';\n";
1736 $output .
= " \$view->use_pager = " .
views_tf($view->use_pager
) .
";\n";
1737 $output .
= " \$view->nodes_per_page = '$view->nodes_per_page';\n";
1739 $output .
= " \$view->menu = " .
views_tf($view->menu
) .
";\n";
1740 $output .
= " \$view->menu_title = '" .
db_escape_string($view->menu_title
) .
"';\n";
1741 $output .
= " \$view->menu_tab = " .
views_tf($view->menu_tab
) .
";\n";
1742 $output .
= " \$view->menu_tab_default = " .
views_tf($view->menu_tab_default
) .
";\n";
1743 $output .
= " \$view->menu_weight = '$view->menu_weight';\n";
1747 $output .
= " \$view->block = " .
views_tf($view->block
) .
";\n";
1748 $output .
= " \$view->block_title = '" .
db_escape_string($view->block_title
) .
"';\n";
1749 $output .
= " \$view->block_header = '" .
db_escape_string($view->block_header
) .
"';\n";
1750 $output .
= " \$view->block_header_format = '$view->block_header_format';\n";
1751 $output .
= " \$view->block_footer = '" .
db_escape_string($view->block_footer
) .
"';\n";
1752 $output .
= " \$view->block_footer_format = '$view->block_footer_format';\n";
1753 $output .
= " \$view->block_empty = '" .
db_escape_string($view->block_empty
) .
"';\n";
1754 $output .
= " \$view->block_empty_format = '$view->block_empty_format';\n";
1755 $output .
= " \$view->block_type = '$view->block_type';\n";
1756 $output .
= " \$view->nodes_per_block = '$view->nodes_per_block';\n";
1757 $output .
= " \$view->block_more = '$view->block_more';\n";
1758 $output .
= " \$view->block_use_page_header = " .
views_tf($view->block_use_page_header
) .
";\n";
1759 $output .
= " \$view->block_use_page_footer = " .
views_tf($view->block_use_page_footer
) .
";\n";
1760 $output .
= " \$view->block_use_page_empty = " .
views_tf($view->block_use_page_empty
) .
";\n";
1763 $output .
= " \$view->sort = array (\n";
1764 foreach ($view->sort as
$sort) {
1765 $output .
= " array (\n";
1766 $fieldbits = explode('.', $sort['field']);
1767 $output .
= " 'tablename' => '$fieldbits[0]',\n";
1768 $output .
= " 'field' => '$fieldbits[1]',\n";
1769 $output .
= " 'sortorder' => '$sort[sortorder]',\n";
1770 $output .
= " 'options' => '$sort[options]',\n";
1772 $requires[$fieldbits[0]] = 1;
1776 $output .
= " \$view->argument = array (\n";
1777 foreach ($view->argument as
$argument) {
1778 $output .
= " array (\n";
1779 $output .
= " 'type' => '$argument[type]',\n";
1780 $output .
= " 'argdefault' => '$argument[argdefault]',\n";
1781 $output .
= " 'title' => '$argument[title]',\n";
1782 $output .
= " 'options' => '$argument[options]',\n";
1787 $output .
= " \$view->field = array (\n";
1788 foreach ($view->field as
$field) {
1789 $output .
= " array (\n";
1790 $output .
= " 'tablename' => '$field[tablename]',\n";
1791 $output .
= " 'field' => '$field[field]',\n";
1792 $output .
= " 'label' => '$field[label]',\n";
1793 if ($field['handler']) {
1794 $output .
= " 'handler' => '$field[handler]',\n";
1796 if ($field['sortable']) {
1797 $output .
= " 'sortable' => '$field[sortable]',\n";
1799 if ($field['defaultsort']) {
1800 $output .
= " 'defaultsort' => '$field[defaultsort]',\n";
1802 if ($field['options']) {
1803 $output .
= " 'options' => '$field[options]',\n";
1806 $requires[$field['tablename']] = 1;
1810 $output .
= " \$view->filter = array (\n";
1811 foreach ($view->filter as
$filter) {
1812 $output .
= " array (\n";
1813 $fieldbits = explode('.', $filter['field']);
1814 $output .
= " 'tablename' => '$fieldbits[0]',\n";
1815 $output .
= " 'field' => '$fieldbits[1]',\n";
1816 $output .
= " 'operator' => '$filter[operator]',\n";
1817 $value = var_export($filter['value'], true
);
1818 $output .
= " 'options' => '$filter[options]',\n";
1819 $output .
= " 'value' => $value,\n";
1821 $requires[$fieldbits[0]] = 1;
1825 $output .
= " \$view->exposed_filter = array (\n";
1826 foreach ($view->exposed_filter as
$filter) {
1827 $output .
= " array (\n";
1828 $fieldbits = explode('.', $filter['field']);
1829 $output .
= " 'tablename' => '$fieldbits[0]',\n";
1830 $output .
= " 'field' => '$fieldbits[1]',\n";
1831 $output .
= " 'label' => '$filter[label]',\n";
1832 $output .
= " 'optional' => $filter[optional],\n";
1833 $output .
= " 'is_default' => $filter[is_default],\n";
1834 $output .
= " 'operator' => $filter[operator],\n";
1835 $output .
= " 'single' => $filter[single],\n";
1837 $requires[$fieldbits[0]] = 1;
1840 $output .
= " \$view->requires = array(" .
implode(', ', array_keys($requires)) .
");\n";
1841 $output .
= " \$views[\$view->name] = \$view;\n";
1842 // no breadcrumb for now.