admin.inc

  1. nittany7 modules/contrib/views/includes/admin.inc
  2. cis7 modules/contrib/views/includes/admin.inc
  3. mooc7 modules/contrib/views/includes/admin.inc

Provides the Views' administrative interface.

Functions

Namesort descending Description
template_preprocess_views_ui_display_tab_bucket
template_preprocess_views_ui_display_tab_column
template_preprocess_views_ui_display_tab_setting
theme_views_ui_build_group_filter_form Theme the build group filter form.
theme_views_ui_expose_filter_form Theme the expose filter form.
theme_views_ui_rearrange_filter_form Turn the rearrange form into a proper table
theme_views_ui_rearrange_form Turn the rearrange form into a proper table
theme_views_ui_reorder_displays_form Turn the reorder form into a proper table
theme_views_ui_style_plugin_table Theme the form for the table style plugin
theme_views_ui_view_info Theme function; returns basic administrative information about a view.
views_element_validate_integer Helper form element validator: integer.
views_fetch_base_tables Fetch a list of all base tables available
views_fetch_fields Fetch a list of all fields available for a given base type.
views_ui_add_admin_css Adds standard Views administration CSS to the current page.
views_ui_add_ajax_trigger Converts a form element in the add view wizard to be AJAX-enabled.
views_ui_add_ajax_wrapper After-build function that adds a wrapper to a form region (for AJAX refreshes).
views_ui_add_form Form builder for the "add new view" page.
views_ui_add_form_cancel_submit Cancel the add view form.
views_ui_add_form_save_submit Process the add view form, 'save'.
views_ui_add_form_store_edit_submit Process the add view form, 'continue'.
views_ui_add_form_to_stack Add another form to the stack; clicking 'apply' will go to this form rather than closing the ajax popup.
views_ui_add_item_form Form to add_item items in the views UI.
views_ui_add_item_form_submit Submit handler for adding new item(s) to a view.
views_ui_add_limited_validation Processes a non-JavaScript fallback submit button to limit its validation errors.
views_ui_add_microweights Recursively adds microweights to a render array, similar to what form_builder() does for forms.
views_ui_add_page Page callback to add a new view.
views_ui_add_template_page
views_ui_admin_settings_advanced Form builder for the advanced admin settings page.
views_ui_admin_settings_basic Form builder for the admin display defaults page.
views_ui_ajax_form Generic entry point to handle forms.
views_ui_ajax_forms
views_ui_ajax_update_form Updates a part of the add view form via AJAX.
views_ui_analyze_view_form Form constructor callback to display analysis information on a view
views_ui_analyze_view_form_submit Submit handler for views_ui_analyze_view_form
views_ui_autocomplete_tag Page callback for views tag autocomplete
views_ui_break_lock_confirm Page to delete a view.
views_ui_break_lock_confirm_submit Submit handler to break_lock a view.
views_ui_build_form_state Build up a $form_state object suitable for use with drupal_build_form based on known information about a form.
views_ui_build_form_url Create the URL for one of our standard AJAX forms based upon known information about the form.
views_ui_build_identifier Build a form identifier that we can use to see if one form is the same as another. Since the arguments differ slightly we do a lot of spiffy concatenation here.
views_ui_build_preview
views_ui_check_advanced_help Check to see if the advanced help module is installed, and if not put up a message.
views_ui_config_item_extra_form Form to config_item items in the views UI.
views_ui_config_item_extra_form_submit Submit handler for configing new item(s) to a view.
views_ui_config_item_extra_form_validate Validation handler for configing new item(s) to a view.
views_ui_config_item_form Form to config_item items in the views UI.
views_ui_config_item_form_add_group Add a new group to the exposed filter groups.
views_ui_config_item_form_build_group Override handler for views_ui_edit_display_form
views_ui_config_item_form_expose Override handler for views_ui_edit_display_form
views_ui_config_item_form_remove Submit handler for removing an item from a view
views_ui_config_item_form_rescan Submit hook to clear Drupal's theme registry (thereby triggering a templates rescan).
views_ui_config_item_form_submit Submit handler for configing new item(s) to a view.
views_ui_config_item_form_submit_temporary A submit handler that is used for storing temporary items when using multi-step changes, such as ajax requests.
views_ui_config_item_form_validate Submit handler for configing new item(s) to a view.
views_ui_config_item_group_form Form to config_item items in the views UI.
views_ui_config_item_group_form_submit Submit handler for configing group settings on a view.
views_ui_config_style_form Form to config_style items in the views UI.
views_ui_config_style_form_submit Submit handler for configing new item(s) to a view.
views_ui_config_type_form Form to config items in the views UI.
views_ui_config_type_form_submit Submit handler for type configuration form
views_ui_default_button #process callback for a button; makes implicit form submissions trigger as this button.
views_ui_edit_details_form Form builder to edit details of a view.
views_ui_edit_details_form_submit Submit handler for views_ui_edit_details_form.
views_ui_edit_display_form Form constructor callback to edit display of a view
views_ui_edit_display_form_change_theme Override handler for views_ui_edit_display_form
views_ui_edit_display_form_override Override handler for views_ui_edit_display_form
views_ui_edit_display_form_submit Submit handler for views_ui_edit_display_form
views_ui_edit_display_form_validate Validate handler for views_ui_edit_display_form
views_ui_edit_form Form builder callback for editing a View.
views_ui_edit_form_get_bucket Add information about a section to a display.
views_ui_edit_form_get_build_from_option Build a renderable array representing one option on the edit form.
views_ui_edit_form_submit_add_display Submit handler to add a display to a view.
views_ui_edit_form_submit_delay_destination Submit handler for form buttons that do not complete a form workflow.
views_ui_edit_form_submit_delete_display Submit handler to delete a display from a view.
views_ui_edit_form_submit_disable_display Submit handler to disable display.
views_ui_edit_form_submit_duplicate_display Submit handler to duplicate a display for a view.
views_ui_edit_form_submit_enable_display Submit handler to enable a disabled display.
views_ui_edit_form_submit_preview Submit handler when Preview button is clicked.
views_ui_edit_form_submit_undo_delete_display Submit handler to add a restore a removed display to a view.
views_ui_edit_page Page callback for the Edit View page.
views_ui_edit_page_display Helper function to return the used display_id for the edit page
views_ui_edit_page_display_tabs Adds tabs for navigating across Displays when editing a View.
views_ui_edit_view_form_cancel Submit handler for the edit view form.
views_ui_edit_view_form_delete
views_ui_edit_view_form_submit Submit handler for the edit view form.
views_ui_edit_view_form_validate Validate that a view is complete and whole.
views_ui_field_list List all instances of fields on any views.
views_ui_form_button_was_clicked #process callback for a button; determines if a button is the form's triggering element.
views_ui_get_admin_css Create an array of Views admin CSS for adding or attaching.
views_ui_get_default_ajax_message
views_ui_get_display_label Placeholder function for overriding $display->display_title.
views_ui_get_display_tab Returns a renderable array representing the edit page for one display.
views_ui_get_display_tab_details Helper function to get the display details section of the edit UI.
views_ui_get_form_progress Get the user's current progress through the form stack.
views_ui_get_roles Get a list of roles in the system.
views_ui_get_selected Gets the current value of a #select element, from within a form constructor function.
views_ui_import_page Import a view from cut & paste.
views_ui_import_submit Submit handler for view import.
views_ui_import_validate Validate handler to import a view.
views_ui_nojs_submit Non-Javascript fallback for updating the add view form.
views_ui_plugin_list Lists all plugins and what enabled Views use them.
views_ui_preview Returns the results of the live preview.
views_ui_preview_form Provide the preview formulas and the preview output, too.
views_ui_pre_render_add_fieldset_markup Move form elements into fieldsets for presentation purposes.
views_ui_pre_render_flatten_data Flattens the structure of an element containing the #flatten property.
views_ui_pre_render_move_argument_options Moves argument options into their place.
views_ui_process_container_radios Custom form radios process function.
views_ui_rearrange_filter_form Form to rearrange items in the views UI.
views_ui_rearrange_filter_form_submit Submit handler for rearranging form
views_ui_rearrange_form Form to rearrange items in the views UI.
views_ui_rearrange_form_submit Submit handler for rearranging form
views_ui_regenerate_tab Regenerate the current tab for AJAX updates.
views_ui_remove_display_form_restore Submit handler to add a restore a removed display to a view.
views_ui_render_display_top Render the top of the display so it can be updated during ajax operations.
views_ui_reorder_displays_form Form constructor callback to reorder displays on a view
views_ui_reorder_displays_form_submit Submit handler for rearranging display form
views_ui_show_default_display Controls whether or not the default display should have its own tab on edit.
views_ui_standard_cancel Submit handler for cancel button
views_ui_standard_display_dropdown Add a <select> dropdown for a given section, allowing the user to change whether this info is stored on the default display or on the current display.
views_ui_standard_form_buttons Provide a standard set of Apply/Cancel/OK buttons for the forms. Also provide a hidden op operator because the forms plugin doesn't seem to properly provide which button was clicked.
views_ui_standard_override_values Return the was_defaulted, is_defaulted and revert state of a form.
views_ui_standard_submit Basic submit handler applicable to all 'standard' forms.
views_ui_taxonomy_autocomplete_validate Form element validation handler for a taxonomy autocomplete field.
views_ui_tools_clear_cache Submit hook to clear the views cache.
views_ui_wizard_form_validate Validate the add view form.
_views_position_sort Display position sorting function
_views_sort_types
_views_weight_sort

File

modules/contrib/views/includes/admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides the Views' administrative interface.
  5. */
  6. /**
  7. * Create an array of Views admin CSS for adding or attaching.
  8. *
  9. * This returns an array of arrays. Each array represents a single
  10. * file. The array format is:
  11. * - file: The fully qualified name of the file to send to drupal_add_css
  12. * - options: An array of options to pass to drupal_add_css.
  13. */
  14. function views_ui_get_admin_css() {
  15. $module_path = drupal_get_path('module', 'views_ui');
  16. $list = array();
  17. $list[$module_path . '/css/views-admin.css'] = array();
  18. $list[$module_path . '/css/ie/views-admin.ie7.css'] = array(
  19. 'browsers' => array(
  20. 'IE' => 'lte IE 7',
  21. '!IE' => FALSE
  22. ),
  23. 'preprocess' => FALSE,
  24. );
  25. $list[$module_path . '/css/views-admin.theme.css'] = array();
  26. // Add in any theme specific CSS files we have
  27. $themes = list_themes();
  28. $theme_key = $GLOBALS['theme'];
  29. while ($theme_key) {
  30. // Try to find the admin css file for non-core themes.
  31. if (!in_array($theme_key, array('garland', 'seven', 'bartik'))) {
  32. $theme_path = drupal_get_path('theme', $theme_key);
  33. // First search in the css directory, then in the root folder of the theme.
  34. if (file_exists($theme_path . "/css/views-admin.$theme_key.css")) {
  35. $list[$theme_path . "/css/views-admin.$theme_key.css"] = array(
  36. 'group' => CSS_THEME,
  37. );
  38. }
  39. else if (file_exists($theme_path . "/views-admin.$theme_key.css")) {
  40. $list[$theme_path . "/views-admin.$theme_key.css"] = array(
  41. 'group' => CSS_THEME,
  42. );
  43. }
  44. }
  45. else {
  46. $list[$module_path . "/css/views-admin.$theme_key.css"] = array(
  47. 'group' => CSS_THEME,
  48. );
  49. }
  50. $theme_key = isset($themes[$theme_key]->base_theme) ? $themes[$theme_key]->base_theme : '';
  51. }
  52. // Views contains style overrides for the following modules
  53. $module_list = array('contextual', 'advanced_help', 'ctools');
  54. foreach ($module_list as $module) {
  55. if (module_exists($module)) {
  56. $list[$module_path . '/css/views-admin.' . $module . '.css'] = array();
  57. }
  58. }
  59. return $list;
  60. }
  61. /**
  62. * Adds standard Views administration CSS to the current page.
  63. */
  64. function views_ui_add_admin_css() {
  65. foreach (views_ui_get_admin_css() as $file => $options) {
  66. drupal_add_css($file, $options);
  67. }
  68. }
  69. /**
  70. * Check to see if the advanced help module is installed, and if not put up
  71. * a message.
  72. *
  73. * Only call this function if the user is already in a position for this to
  74. * be useful.
  75. */
  76. function views_ui_check_advanced_help() {
  77. if (!variable_get('views_ui_show_advanced_help_warning', TRUE)) {
  78. return;
  79. }
  80. if (!module_exists('advanced_help')) {
  81. $filename = db_query_range("SELECT filename FROM {system} WHERE type = 'module' AND name = 'advanced_help'", 0, 1)
  82. ->fetchField();
  83. if ($filename && file_exists($filename)) {
  84. drupal_set_message(t('If you <a href="@modules">enable the advanced help module</a>, Views will provide more and better help. <a href="@hide">You can disable this message at the Views settings page.</a>', array('@modules' => url('admin/modules'),'@hide' => url('admin/structure/views/settings'))));
  85. }
  86. else {
  87. drupal_set_message(t('If you install the advanced help module from !href, Views will provide more and better help. <a href="@hide">You can disable this message at the Views settings page.</a>', array('!href' => l('http://drupal.org/project/advanced_help', 'http://drupal.org/project/advanced_help'), '@hide' => url('admin/structure/views/settings'))));
  88. }
  89. }
  90. }
  91. /**
  92. * Returns the results of the live preview.
  93. */
  94. function views_ui_preview($view, $display_id, $args = array()) {
  95. // When this function is invoked as a page callback, each Views argument is
  96. // passed separately.
  97. if (!is_array($args)) {
  98. $args = array_slice(func_get_args(), 2);
  99. }
  100. // Save $_GET['q'] so it can be restored before returning from this function.
  101. $q = $_GET['q'];
  102. // Determine where the query and performance statistics should be output.
  103. $show_query = variable_get('views_ui_show_sql_query', FALSE);
  104. $show_info = variable_get('views_ui_show_preview_information', FALSE);
  105. $show_location = variable_get('views_ui_show_sql_query_where', 'above');
  106. $show_stats = variable_get('views_ui_show_performance_statistics', FALSE);
  107. if ($show_stats) {
  108. $show_stats = variable_get('views_ui_show_sql_query_where', 'above');
  109. }
  110. $combined = $show_query && $show_stats;
  111. $rows = array('query' => array(), 'statistics' => array());
  112. $output = '';
  113. $errors = $view->validate();
  114. if ($errors === TRUE) {
  115. $view->ajax = TRUE;
  116. $view->live_preview = TRUE;
  117. $view->views_ui_context = TRUE;
  118. // AJAX happens via $_POST but everything expects exposed data to
  119. // be in GET. Copy stuff but remove ajax-framework specific keys.
  120. // If we're clicking on links in a preview, though, we could actually
  121. // still have some in $_GET, so we use $_REQUEST to ensure we get it all.
  122. $exposed_input = $_REQUEST;
  123. foreach (array('view_name', 'view_display_id', 'view_args', 'view_path', 'view_dom_id', 'pager_element', 'view_base_path', 'ajax_html_ids', 'ajax_page_state', 'form_id', 'form_build_id', 'form_token') as $key) {
  124. if (isset($exposed_input[$key])) {
  125. unset($exposed_input[$key]);
  126. }
  127. }
  128. $view->set_exposed_input($exposed_input);
  129. if (!$view->set_display($display_id)) {
  130. return t('Invalid display id @display', array('@display' => $display_id));
  131. }
  132. $view->set_arguments($args);
  133. // Store the current view URL for later use:
  134. if ($view->display_handler->get_option('path')) {
  135. $path = $view->get_url();
  136. }
  137. // Make view links come back to preview.
  138. $view->override_path = 'admin/structure/views/nojs/preview/' . $view->name . '/' . $display_id;
  139. // Also override $_GET['q'] so we get the pager.
  140. $original_path = current_path();
  141. $_GET['q'] = $view->override_path;
  142. if ($args) {
  143. $_GET['q'] .= '/' . implode('/', $args);
  144. }
  145. // Suppress contextual links of entities within the result set during a
  146. // Preview.
  147. // @todo We'll want to add contextual links specific to editing the View, so
  148. // the suppression may need to be moved deeper into the Preview pipeline.
  149. views_ui_contextual_links_suppress_push();
  150. $preview = $view->preview($display_id, $args);
  151. views_ui_contextual_links_suppress_pop();
  152. // Reset variables.
  153. unset($view->override_path);
  154. $_GET['q'] = $original_path;
  155. // Prepare the query information and statistics to show either above or
  156. // below the view preview.
  157. if ($show_info || $show_query || $show_stats) {
  158. // Get information from the preview for display.
  159. if (!empty($view->build_info['query'])) {
  160. if ($show_query) {
  161. $query = $view->build_info['query'];
  162. // Only the sql default class has a method getArguments.
  163. $quoted = array();
  164. if (get_class($view->query) == 'views_plugin_query_default') {
  165. $quoted = $query->getArguments();
  166. $connection = Database::getConnection();
  167. foreach ($quoted as $key => $val) {
  168. if (is_array($val)) {
  169. $quoted[$key] = implode(', ', array_map(array($connection, 'quote'), $val));
  170. }
  171. else {
  172. $quoted[$key] = $connection->quote($val);
  173. }
  174. }
  175. }
  176. $rows['query'][] = array('<strong>' . t('Query') . '</strong>', '<pre>' . check_plain(strtr($query, $quoted)) . '</pre>');
  177. if (!empty($view->additional_queries)) {
  178. $queries = '<strong>' . t('These queries were run during view rendering:') . '</strong>';
  179. foreach ($view->additional_queries as $query) {
  180. if ($queries) {
  181. $queries .= "\n";
  182. }
  183. $queries .= t('[@time ms]', array('@time' => intval($query[1] * 100000) / 100)) . ' ' . $query[0];
  184. }
  185. $rows['query'][] = array('<strong>' . t('Other queries') . '</strong>', '<pre>' . $queries . '</pre>');
  186. }
  187. }
  188. if ($show_info) {
  189. $rows['query'][] = array('<strong>' . t('Title') . '</strong>', filter_xss_admin($view->get_title()));
  190. if (isset($path)) {
  191. $path = l($path, $path);
  192. }
  193. else {
  194. $path = t('This display has no path.');
  195. }
  196. $rows['query'][] = array('<strong>' . t('Path') . '</strong>', $path);
  197. }
  198. if ($show_stats) {
  199. $rows['statistics'][] = array('<strong>' . t('Query build time') . '</strong>', t('@time ms', array('@time' => intval($view->build_time * 100000) / 100)));
  200. $rows['statistics'][] = array('<strong>' . t('Query execute time') . '</strong>', t('@time ms', array('@time' => intval($view->execute_time * 100000) / 100)));
  201. $rows['statistics'][] = array('<strong>' . t('View render time') . '</strong>', t('@time ms', array('@time' => intval($view->render_time * 100000) / 100)));
  202. }
  203. drupal_alter('views_preview_info', $rows, $view);
  204. }
  205. else {
  206. // No query was run. Display that information in place of either the
  207. // query or the performance statistics, whichever comes first.
  208. if ($combined || ($show_location === 'above')) {
  209. $rows['query'] = array(array('<strong>' . t('Query') . '</strong>', t('No query was run')));
  210. }
  211. else {
  212. $rows['statistics'] = array(array('<strong>' . t('Query') . '</strong>', t('No query was run')));
  213. }
  214. }
  215. }
  216. }
  217. else {
  218. foreach ($errors as $error) {
  219. drupal_set_message($error, 'error');
  220. }
  221. $preview = t('Unable to preview due to validation errors.');
  222. }
  223. // Assemble the preview, the query info, and the query statistics in the
  224. // requested order.
  225. if ($show_location === 'above') {
  226. if ($combined) {
  227. $output .= '<div class="views-query-info">' . theme('table', array('rows' => array_merge($rows['query'], $rows['statistics']))) . '</div>';
  228. }
  229. else {
  230. $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['query'])) . '</div>';
  231. }
  232. }
  233. elseif ($show_stats === 'above') {
  234. $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['statistics'])) . '</div>';
  235. }
  236. $output .= $preview;
  237. if ($show_location === 'below') {
  238. if ($combined) {
  239. $output .= '<div class="views-query-info">' . theme('table', array('rows' => array_merge($rows['query'], $rows['statistics']))) . '</div>';
  240. }
  241. else {
  242. $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['query'])) . '</div>';
  243. }
  244. }
  245. elseif ($show_stats === 'below') {
  246. $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['statistics'])) . '</div>';
  247. }
  248. $_GET['q'] = $q;
  249. return $output;
  250. }
  251. /**
  252. * Page callback to add a new view.
  253. */
  254. function views_ui_add_page() {
  255. views_ui_add_admin_css();
  256. drupal_set_title(t('Add new view'));
  257. return drupal_get_form('views_ui_add_form');
  258. }
  259. /**
  260. * Form builder for the "add new view" page.
  261. */
  262. function views_ui_add_form($form, &$form_state) {
  263. ctools_include('dependent');
  264. $form['#attached']['js'][] = drupal_get_path('module', 'views_ui') . '/js/views-admin.js';
  265. $form['#attributes']['class'] = array('views-admin');
  266. $form['human_name'] = array(
  267. '#type' => 'textfield',
  268. '#title' => t('View name'),
  269. '#required' => TRUE,
  270. '#size' => 32,
  271. '#default_value' => !empty($form_state['view']) ? $form_state['view']->human_name : '',
  272. '#maxlength' => 255,
  273. );
  274. $form['name'] = array(
  275. '#type' => 'machine_name',
  276. '#maxlength' => 128,
  277. '#machine_name' => array(
  278. 'exists' => 'views_get_view',
  279. 'source' => array('human_name'),
  280. ),
  281. '#description' => t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'),
  282. );
  283. $form['description_enable'] = array(
  284. '#type' => 'checkbox',
  285. '#title' => t('Description'),
  286. );
  287. $form['description'] = array(
  288. '#type' => 'textfield',
  289. '#title' => t('Provide description'),
  290. '#title_display' => 'invisible',
  291. '#size' => 64,
  292. '#default_value' => !empty($form_state['view']) ? $form_state['view']->description : '',
  293. '#dependency' => array(
  294. 'edit-description-enable' => array(1),
  295. ),
  296. );
  297. // Create a wrapper for the entire dynamic portion of the form. Everything
  298. // that can be updated by AJAX goes somewhere inside here. For example, this
  299. // is needed by "Show" dropdown (below); it changes the base table of the
  300. // view and therefore potentially requires all options on the form to be
  301. // dynamically updated.
  302. $form['displays'] = array();
  303. // Create the part of the form that allows the user to select the basic
  304. // properties of what the view will display.
  305. $form['displays']['show'] = array(
  306. '#type' => 'fieldset',
  307. '#tree' => TRUE,
  308. '#attributes' => array('class' => array('container-inline')),
  309. );
  310. // Create the "Show" dropdown, which allows the base table of the view to be
  311. // selected.
  312. $wizard_plugins = views_ui_get_wizards();
  313. $options = array();
  314. foreach ($wizard_plugins as $key => $wizard) {
  315. $options[$key] = $wizard['title'];
  316. }
  317. $form['displays']['show']['wizard_key'] = array(
  318. '#type' => 'select',
  319. '#title' => t('Show'),
  320. '#options' => $options,
  321. );
  322. $show_form = &$form['displays']['show'];
  323. $show_form['wizard_key']['#default_value'] = views_ui_get_selected($form_state, array('show', 'wizard_key'), 'node', $show_form['wizard_key']);
  324. // Changing this dropdown updates the entire content of $form['displays'] via
  325. // AJAX.
  326. views_ui_add_ajax_trigger($show_form, 'wizard_key', array('displays'));
  327. // Build the rest of the form based on the currently selected wizard plugin.
  328. $wizard_key = $show_form['wizard_key']['#default_value'];
  329. $get_instance = $wizard_plugins[$wizard_key]['get_instance'];
  330. $wizard_instance = $get_instance($wizard_plugins[$wizard_key]);
  331. $form = $wizard_instance->build_form($form, $form_state);
  332. $form['save'] = array(
  333. '#type' => 'submit',
  334. '#value' => t('Save & exit'),
  335. '#validate' => array('views_ui_wizard_form_validate'),
  336. '#submit' => array('views_ui_add_form_save_submit'),
  337. );
  338. $form['continue'] = array(
  339. '#type' => 'submit',
  340. '#value' => t('Continue & edit'),
  341. '#validate' => array('views_ui_wizard_form_validate'),
  342. '#submit' => array('views_ui_add_form_store_edit_submit'),
  343. '#process' => array_merge(array('views_ui_default_button'), element_info_property('submit', '#process', array())),
  344. );
  345. $form['cancel'] = array(
  346. '#type' => 'submit',
  347. '#value' => t('Cancel'),
  348. '#submit' => array('views_ui_add_form_cancel_submit'),
  349. '#limit_validation_errors' => array(),
  350. );
  351. return $form;
  352. }
  353. /**
  354. * Helper form element validator: integer.
  355. *
  356. * The problem with this is that the function is private so it's not guaranteed
  357. * that it might not be renamed/changed. In the future field.module or something else
  358. * should provide a public validate function.
  359. *
  360. * @see _element_validate_integer_positive()
  361. */
  362. function views_element_validate_integer($element, &$form_state) {
  363. $value = $element['#value'];
  364. if ($value !== '' && (!is_numeric($value) || intval($value) != $value)) {
  365. form_error($element, t('%name must be a positive integer.', array('%name' => $element['#title'])));
  366. }
  367. }
  368. /**
  369. * Gets the current value of a #select element, from within a form constructor function.
  370. *
  371. * This function is intended for use in highly dynamic forms (in particular the
  372. * add view wizard) which are rebuilt in different ways depending on which
  373. * triggering element (AJAX or otherwise) was most recently fired. For example,
  374. * sometimes it is necessary to decide how to build one dynamic form element
  375. * based on the value of a different dynamic form element that may not have
  376. * even been present on the form the last time it was submitted. This function
  377. * takes care of resolving those conflicts and gives you the proper current
  378. * value of the requested #select element.
  379. *
  380. * By necessity, this function sometimes uses non-validated user input from
  381. * $form_state['input'] in making its determination. Although it performs some
  382. * minor validation of its own, it is not complete. The intention is that the
  383. * return value of this function should only be used to help decide how to
  384. * build the current form the next time it is reloaded, not to be saved as if
  385. * it had gone through the normal, final form validation process. Do NOT use
  386. * the results of this function for any other purpose besides deciding how to
  387. * build the next version of the form.
  388. *
  389. * @param $form_state
  390. * The standard associative array containing the current state of the form.
  391. * @param $parents
  392. * An array of parent keys that point to the part of the submitted form
  393. * values that are expected to contain the element's value (in the case where
  394. * this form element was actually submitted). In a simple case (assuming
  395. * #tree is TRUE throughout the form), if the select element is located in
  396. * $form['wrapper']['select'], so that the submitted form values would
  397. * normally be found in $form_state['values']['wrapper']['select'], you would
  398. * pass array('wrapper', 'select') for this parameter.
  399. * @param $default_value
  400. * The default value to return if the #select element does not currently have
  401. * a proper value set based on the submitted input.
  402. * @param $element
  403. * An array representing the current version of the #select element within
  404. * the form.
  405. *
  406. * @return
  407. * The current value of the #select element. A common use for this is to feed
  408. * it back into $element['#default_value'] so that the form will be rendered
  409. * with the correct value selected.
  410. */
  411. function views_ui_get_selected($form_state, $parents, $default_value, $element) {
  412. // For now, don't trust this to work on anything but a #select element.
  413. if (!isset($element['#type']) || $element['#type'] != 'select' || !isset($element['#options'])) {
  414. return $default_value;
  415. }
  416. // If there is a user-submitted value for this element that matches one of
  417. // the currently available options attached to it, use that. We need to check
  418. // $form_state['input'] rather than $form_state['values'] here because the
  419. // triggering element often has the #limit_validation_errors property set to
  420. // prevent unwanted errors elsewhere on the form. This means that the
  421. // $form_state['values'] array won't be complete. We could make it complete
  422. // by adding each required part of the form to the #limit_validation_errors
  423. // property individually as the form is being built, but this is difficult to
  424. // do for a highly dynamic and extensible form. This method is much simpler.
  425. if (!empty($form_state['input'])) {
  426. $key_exists = NULL;
  427. $submitted = drupal_array_get_nested_value($form_state['input'], $parents, $key_exists);
  428. // Check that the user-submitted value is one of the allowed options before
  429. // returning it. This is not a substitute for actual form validation;
  430. // rather it is necessary because, for example, the same select element
  431. // might have #options A, B, and C under one set of conditions but #options
  432. // D, E, F under a different set of conditions. So the form submission
  433. // might have occurred with option A selected, but when the form is rebuilt
  434. // option A is no longer one of the choices. In that case, we don't want to
  435. // use the value that was submitted anymore but rather fall back to the
  436. // default value.
  437. if ($key_exists && in_array($submitted, array_keys($element['#options']))) {
  438. return $submitted;
  439. }
  440. }
  441. // Fall back on returning the default value if nothing was returned above.
  442. return $default_value;
  443. }
  444. /**
  445. * Converts a form element in the add view wizard to be AJAX-enabled.
  446. *
  447. * This function takes a form element and adds AJAX behaviors to it such that
  448. * changing it triggers another part of the form to update automatically. It
  449. * also adds a submit button to the form that appears next to the triggering
  450. * element and that duplicates its functionality for users who do not have
  451. * JavaScript enabled (the button is automatically hidden for users who do have
  452. * JavaScript).
  453. *
  454. * To use this function, call it directly from your form builder function
  455. * immediately after you have defined the form element that will serve as the
  456. * JavaScript trigger. Calling it elsewhere (such as in hook_form_alter()) may
  457. * mean that the non-JavaScript fallback button does not appear in the correct
  458. * place in the form.
  459. *
  460. * @param $wrapping_element
  461. * The element whose child will server as the AJAX trigger. For example, if
  462. * $form['some_wrapper']['triggering_element'] represents the element which
  463. * will trigger the AJAX behavior, you would pass $form['some_wrapper'] for
  464. * this parameter.
  465. * @param $trigger_key
  466. * The key within the wrapping element that identifies which of its children
  467. * serves as the AJAX trigger. In the above example, you would pass
  468. * 'triggering_element' for this parameter.
  469. * @param $refresh_parents
  470. * An array of parent keys that point to the part of the form that will be
  471. * refreshed by AJAX. For example, if triggering the AJAX behavior should
  472. * cause $form['dynamic_content']['section'] to be refreshed, you would pass
  473. * array('dynamic_content', 'section') for this parameter.
  474. */
  475. function views_ui_add_ajax_trigger(&$wrapping_element, $trigger_key, $refresh_parents) {
  476. $seen_ids = &drupal_static(__FUNCTION__ . ':seen_ids', array());
  477. $seen_buttons = &drupal_static(__FUNCTION__ . ':seen_buttons', array());
  478. // Add the AJAX behavior to the triggering element.
  479. $triggering_element = &$wrapping_element[$trigger_key];
  480. $triggering_element['#ajax']['callback'] = 'views_ui_ajax_update_form';
  481. // We do not use drupal_html_id() to get an ID for the AJAX wrapper, because
  482. // it remembers IDs across AJAX requests (and won't reuse them), but in our
  483. // case we need to use the same ID from request to request so that the
  484. // wrapper can be recognized by the AJAX system and its content can be
  485. // dynamically updated. So instead, we will keep track of duplicate IDs
  486. // (within a single request) on our own, later in this function.
  487. $triggering_element['#ajax']['wrapper'] = 'edit-view-' . implode('-', $refresh_parents) . '-wrapper';
  488. // Add a submit button for users who do not have JavaScript enabled. It
  489. // should be displayed next to the triggering element on the form.
  490. $button_key = $trigger_key . '_trigger_update';
  491. $wrapping_element[$button_key] = array(
  492. '#type' => 'submit',
  493. // Hide this button when JavaScript is enabled.
  494. '#attributes' => array('class' => array('js-hide')),
  495. '#submit' => array('views_ui_nojs_submit'),
  496. // Add a process function to limit this button's validation errors to the
  497. // triggering element only. We have to do this in #process since until the
  498. // form API has added the #parents property to the triggering element for
  499. // us, we don't have any (easy) way to find out where its submitted values
  500. // will eventually appear in $form_state['values'].
  501. '#process' => array_merge(array('views_ui_add_limited_validation'), element_info_property('submit', '#process', array())),
  502. // Add an after-build function that inserts a wrapper around the region of
  503. // the form that needs to be refreshed by AJAX (so that the AJAX system can
  504. // detect and dynamically update it). This is done in #after_build because
  505. // it's a convenient place where we have automatic access to the complete
  506. // form array, but also to minimize the chance that the HTML we add will
  507. // get clobbered by code that runs after we have added it.
  508. '#after_build' => array_merge(element_info_property('submit', '#after_build', array()), array('views_ui_add_ajax_wrapper')),
  509. );
  510. // Copy #weight and #access from the triggering element to the button, so
  511. // that the two elements will be displayed together.
  512. foreach (array('#weight', '#access') as $property) {
  513. if (isset($triggering_element[$property])) {
  514. $wrapping_element[$button_key][$property] = $triggering_element[$property];
  515. }
  516. }
  517. // For easiest integration with the form API and the testing framework, we
  518. // always give the button a unique #value, rather than playing around with
  519. // #name.
  520. $button_title = !empty($triggering_element['#title']) ? $triggering_element['#title'] : $trigger_key;
  521. if (empty($seen_buttons[$button_title])) {
  522. $wrapping_element[$button_key]['#value'] = t('Update "@title" choice', array(
  523. '@title' => $button_title,
  524. ));
  525. $seen_buttons[$button_title] = 1;
  526. }
  527. else {
  528. $wrapping_element[$button_key]['#value'] = t('Update "@title" choice (@number)', array(
  529. '@title' => $button_title,
  530. '@number' => ++$seen_buttons[$button_title],
  531. ));
  532. }
  533. // Attach custom data to the triggering element and submit button, so we can
  534. // use it in both the process function and AJAX callback.
  535. $ajax_data = array(
  536. 'wrapper' => $triggering_element['#ajax']['wrapper'],
  537. 'trigger_key' => $trigger_key,
  538. 'refresh_parents' => $refresh_parents,
  539. // Keep track of duplicate wrappers so we don't add the same wrapper to the
  540. // page more than once.
  541. 'duplicate_wrapper' => !empty($seen_ids[$triggering_element['#ajax']['wrapper']]),
  542. );
  543. $seen_ids[$triggering_element['#ajax']['wrapper']] = TRUE;
  544. $triggering_element['#views_ui_ajax_data'] = $ajax_data;
  545. $wrapping_element[$button_key]['#views_ui_ajax_data'] = $ajax_data;
  546. }
  547. /**
  548. * Processes a non-JavaScript fallback submit button to limit its validation errors.
  549. */
  550. function views_ui_add_limited_validation($element, &$form_state) {
  551. // Retrieve the AJAX triggering element so we can determine its parents. (We
  552. // know it's at the same level of the complete form array as the submit
  553. // button, so all we have to do to find it is swap out the submit button's
  554. // last array parent.)
  555. $array_parents = $element['#array_parents'];
  556. array_pop($array_parents);
  557. $array_parents[] = $element['#views_ui_ajax_data']['trigger_key'];
  558. $ajax_triggering_element = drupal_array_get_nested_value($form_state['complete form'], $array_parents);
  559. // Limit this button's validation to the AJAX triggering element, so it can
  560. // update the form for that change without requiring that the rest of the
  561. // form be filled out properly yet.
  562. $element['#limit_validation_errors'] = array($ajax_triggering_element['#parents']);
  563. // If we are in the process of a form submission and this is the button that
  564. // was clicked, the form API workflow in form_builder() will have already
  565. // copied it to $form_state['triggering_element'] before our #process
  566. // function is run. So we need to make the same modifications in $form_state
  567. // as we did to the element itself, to ensure that #limit_validation_errors
  568. // will actually be set in the correct place.
  569. if (!empty($form_state['triggering_element'])) {
  570. $clicked_button = &$form_state['triggering_element'];
  571. if ($clicked_button['#name'] == $element['#name'] && $clicked_button['#value'] == $element['#value']) {
  572. $clicked_button['#limit_validation_errors'] = $element['#limit_validation_errors'];
  573. }
  574. }
  575. return $element;
  576. }
  577. /**
  578. * After-build function that adds a wrapper to a form region (for AJAX refreshes).
  579. *
  580. * This function inserts a wrapper around the region of the form that needs to
  581. * be refreshed by AJAX, based on information stored in the corresponding
  582. * submit button form element.
  583. */
  584. function views_ui_add_ajax_wrapper($element, &$form_state) {
  585. // Don't add the wrapper <div> if the same one was already inserted on this
  586. // form.
  587. if (empty($element['#views_ui_ajax_data']['duplicate_wrapper'])) {
  588. // Find the region of the complete form that needs to be refreshed by AJAX.
  589. // This was earlier stored in a property on the element.
  590. $complete_form = &$form_state['complete form'];
  591. $refresh_parents = $element['#views_ui_ajax_data']['refresh_parents'];
  592. $refresh_element = drupal_array_get_nested_value($complete_form, $refresh_parents);
  593. // The HTML ID that AJAX expects was also stored in a property on the
  594. // element, so use that information to insert the wrapper <div> here.
  595. $id = $element['#views_ui_ajax_data']['wrapper'];
  596. $refresh_element += array(
  597. '#prefix' => '',
  598. '#suffix' => '',
  599. );
  600. $refresh_element['#prefix'] = '<div id="' . $id . '" class="views-ui-ajax-wrapper">' . $refresh_element['#prefix'];
  601. $refresh_element['#suffix'] .= '</div>';
  602. // Copy the element that needs to be refreshed back into the form, with our
  603. // modifications to it.
  604. drupal_array_set_nested_value($complete_form, $refresh_parents, $refresh_element);
  605. }
  606. return $element;
  607. }
  608. /**
  609. * Updates a part of the add view form via AJAX.
  610. *
  611. * @return
  612. * The part of the form that has changed.
  613. */
  614. function views_ui_ajax_update_form($form, $form_state) {
  615. // The region that needs to be updated was stored in a property of the
  616. // triggering element by views_ui_add_ajax_trigger(), so all we have to do is
  617. // retrieve that here.
  618. return drupal_array_get_nested_value($form, $form_state['triggering_element']['#views_ui_ajax_data']['refresh_parents']);
  619. }
  620. /**
  621. * Non-Javascript fallback for updating the add view form.
  622. */
  623. function views_ui_nojs_submit($form, &$form_state) {
  624. $form_state['rebuild'] = TRUE;
  625. }
  626. /**
  627. * Validate the add view form.
  628. */
  629. function views_ui_wizard_form_validate($form, &$form_state) {
  630. $wizard = views_ui_get_wizard($form_state['values']['show']['wizard_key']);
  631. $form_state['wizard'] = $wizard;
  632. $get_instance = $wizard['get_instance'];
  633. $form_state['wizard_instance'] = $get_instance($wizard);
  634. $errors = $form_state['wizard_instance']->validate($form, $form_state);
  635. foreach ($errors as $name => $message) {
  636. form_set_error($name, $message);
  637. }
  638. }
  639. /**
  640. * Process the add view form, 'save'.
  641. */
  642. function views_ui_add_form_save_submit($form, &$form_state) {
  643. try {
  644. $view = $form_state['wizard_instance']->create_view($form, $form_state);
  645. }
  646. catch (ViewsWizardException $e) {
  647. drupal_set_message($e->getMessage(), 'error');
  648. $form_state['redirect'] = 'admin/structure/views';
  649. }
  650. $view->save();
  651. $form_state['redirect'] = 'admin/structure/views';
  652. if (!empty($view->display['page'])) {
  653. $display = $view->display['page'];
  654. if ($display->handler->has_path()) {
  655. $one_path = $display->handler->get_option('path');
  656. if (strpos($one_path, '%') === FALSE) {
  657. $form_state['redirect'] = $one_path; // PATH TO THE VIEW IF IT HAS ONE
  658. return;
  659. }
  660. }
  661. }
  662. drupal_set_message(t('Your view was saved. You may edit it from the list below.'));
  663. }
  664. /**
  665. * Process the add view form, 'continue'.
  666. */
  667. function views_ui_add_form_store_edit_submit($form, &$form_state) {
  668. try {
  669. $view = $form_state['wizard_instance']->create_view($form, $form_state);
  670. }
  671. catch (ViewsWizardException $e) {
  672. drupal_set_message($e->getMessage(), 'error');
  673. $form_state['redirect'] = 'admin/structure/views';
  674. }
  675. // Just cache it temporarily to edit it.
  676. views_ui_cache_set($view);
  677. // If there is a destination query, ensure we still redirect the user to the
  678. // edit view page, and then redirect the user to the destination.
  679. $destination = array();
  680. if (isset($_GET['destination'])) {
  681. $destination = drupal_get_destination();
  682. unset($_GET['destination']);
  683. }
  684. $form_state['redirect'] = array('admin/structure/views/view/' . $view->name, array('query' => $destination));
  685. }
  686. /**
  687. * Cancel the add view form.
  688. */
  689. function views_ui_add_form_cancel_submit($form, &$form_state) {
  690. $form_state['redirect'] = 'admin/structure/views';
  691. }
  692. /**
  693. * Form element validation handler for a taxonomy autocomplete field.
  694. *
  695. * This allows a taxonomy autocomplete field to be validated outside the
  696. * standard Field API workflow, without passing in a complete field widget.
  697. * Instead, all that is required is that $element['#field_name'] contain the
  698. * name of the taxonomy autocomplete field that is being validated.
  699. *
  700. * This function is currently not used for validation directly, although it
  701. * could be. Instead, it is only used to store the term IDs and vocabulary name
  702. * in the element value, based on the tags that the user typed in.
  703. *
  704. * @see taxonomy_autocomplete_validate()
  705. */
  706. function views_ui_taxonomy_autocomplete_validate($element, &$form_state) {
  707. $value = array();
  708. if ($tags = $element['#value']) {
  709. // Get the machine names of the vocabularies we will search, keyed by the
  710. // vocabulary IDs.
  711. $field = field_info_field($element['#field_name']);
  712. $vocabularies = array();
  713. if (!empty($field['settings']['allowed_values'])) {
  714. foreach ($field['settings']['allowed_values'] as $tree) {
  715. if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
  716. $vocabularies[$vocabulary->vid] = $tree['vocabulary'];
  717. }
  718. }
  719. }
  720. // Store the term ID of each (valid) tag that the user typed.
  721. $typed_terms = drupal_explode_tags($tags);
  722. foreach ($typed_terms as $typed_term) {
  723. if ($terms = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => array_keys($vocabularies)))) {
  724. $term = array_pop($terms);
  725. $value['tids'][] = $term->tid;
  726. }
  727. }
  728. // Store the term IDs along with the name of the vocabulary. Currently
  729. // Views (as well as the Field UI) assumes that there will only be one
  730. // vocabulary, although technically the API allows there to be more than
  731. // one.
  732. if (!empty($value['tids'])) {
  733. $value['tids'] = array_unique($value['tids']);
  734. $value['vocabulary'] = array_pop($vocabularies);
  735. }
  736. }
  737. form_set_value($element, $value, $form_state);
  738. }
  739. /**
  740. * Theme function; returns basic administrative information about a view.
  741. *
  742. * TODO: template + preprocess
  743. */
  744. function theme_views_ui_view_info($variables) {
  745. $view = $variables['view'];
  746. $title = $view->get_human_name();
  747. $displays = _views_ui_get_displays_list($view);
  748. $displays = empty($displays) ? t('None') : format_plural(count($displays), 'Display', 'Displays') . ': ' . '<em>' . implode(', ', $displays) . '</em>';
  749. switch ($view->type) {
  750. case t('Default'):
  751. default:
  752. $type = t('In code');
  753. break;
  754. case t('Normal'):
  755. $type = t('In database');
  756. break;
  757. case t('Overridden'):
  758. $type = t('Database overriding code');
  759. }
  760. $output = '';
  761. $output .= '<div class="views-ui-view-title">' . check_plain($title) . "</div>\n";
  762. $output .= '<div class="views-ui-view-displays">' . $displays . "</div>\n";
  763. $output .= '<div class="views-ui-view-storage">' . $type . "</div>\n";
  764. $output .= '<div class="views-ui-view-base">' . t('Type') . ': ' . check_plain($variables['base']). "</div>\n";
  765. return $output;
  766. }
  767. /**
  768. * Page to delete a view.
  769. */
  770. function views_ui_break_lock_confirm($form, &$form_state, $view) {
  771. $form_state['view'] = &$view;
  772. $form = array();
  773. if (empty($view->locked)) {
  774. $form['message']['#markup'] = t('There is no lock on view %name to break.', array('%name' => $view->name));
  775. return $form;
  776. }
  777. $cancel = 'admin/structure/views/view/' . $view->name . '/edit';
  778. if (!empty($_REQUEST['cancel'])) {
  779. $cancel = $_REQUEST['cancel'];
  780. }
  781. $account = user_load($view->locked->uid);
  782. return confirm_form($form,
  783. t('Are you sure you want to break the lock on view %name?',
  784. array('%name' => $view->name)),
  785. $cancel,
  786. t('By breaking this lock, any unsaved changes made by !user will be lost!', array('!user' => theme('username', array('account' => $account)))),
  787. t('Break lock'),
  788. t('Cancel'));
  789. }
  790. /**
  791. * Submit handler to break_lock a view.
  792. */
  793. function views_ui_break_lock_confirm_submit(&$form, &$form_state) {
  794. ctools_object_cache_clear_all('view', $form_state['view']->name);
  795. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit';
  796. drupal_set_message(t('The lock has been broken and you may now edit this view.'));
  797. }
  798. /**
  799. * Helper function to return the used display_id for the edit page
  800. *
  801. * This function handles access to the display.
  802. */
  803. function views_ui_edit_page_display($view, $display_id) {
  804. // Determine the displays available for editing.
  805. if ($tabs = views_ui_edit_page_display_tabs($view, $display_id)) {
  806. // If a display isn't specified, use the first one.
  807. if (empty($display_id)) {
  808. foreach ($tabs as $id => $tab) {
  809. if (!isset($tab['#access']) || $tab['#access']) {
  810. $display_id = $id;
  811. break;
  812. }
  813. }
  814. }
  815. // If a display is specified, but we don't have access to it, return
  816. // an access denied page.
  817. if ($display_id && (!isset($tabs[$display_id]) || (isset($tabs[$display_id]['#access']) && !$tabs[$display_id]['#access']))) {
  818. return MENU_ACCESS_DENIED;
  819. }
  820. return $display_id;
  821. }
  822. elseif ($display_id) {
  823. return MENU_ACCESS_DENIED;
  824. }
  825. else {
  826. $display_id = NULL;
  827. }
  828. return $display_id;
  829. }
  830. /**
  831. * Page callback for the Edit View page.
  832. */
  833. function views_ui_edit_page($view, $display_id = NULL) {
  834. $display_id = views_ui_edit_page_display($view, $display_id);
  835. if (!in_array($display_id, array(MENU_ACCESS_DENIED, MENU_NOT_FOUND))) {
  836. $build = array();
  837. $build['edit_form'] = drupal_get_form('views_ui_edit_form', $view, $display_id);
  838. $build['preview'] = views_ui_build_preview($view, $display_id, FALSE);
  839. }
  840. else {
  841. $build = $display_id;
  842. }
  843. return $build;
  844. }
  845. function views_ui_build_preview($view, $display_id, $render = TRUE) {
  846. if (isset($_POST['ajax_html_ids'])) {
  847. unset($_POST['ajax_html_ids']);
  848. }
  849. $build = array(
  850. '#theme_wrappers' => array('container'),
  851. '#attributes' => array('id' => 'views-preview-wrapper', 'class' => 'views-admin clearfix'),
  852. );
  853. $form_state = array('build_info' => array('args' => array($view, $display_id)));
  854. $build['controls'] = drupal_build_form('views_ui_preview_form', $form_state);
  855. $args = array();
  856. if (!empty($form_state['values']['view_args'])) {
  857. $args = explode('/', $form_state['values']['view_args']);
  858. }
  859. $build['preview'] = array(
  860. '#theme_wrappers' => array('container'),
  861. '#attributes' => array('id' => 'views-live-preview'),
  862. '#markup' => $render ? views_ui_preview($view->clone_view(), $display_id, $args) : '',
  863. );
  864. return $build;
  865. }
  866. /**
  867. * Form builder callback for editing a View.
  868. *
  869. * @todo Remove as many #prefix/#suffix lines as possible. Use #theme_wrappers
  870. * instead.
  871. *
  872. * @todo Rename to views_ui_edit_view_form(). See that function for the "old"
  873. * version.
  874. *
  875. * @see views_ui_ajax_get_form()
  876. */
  877. function views_ui_edit_form($form, &$form_state, $view, $display_id = NULL) {
  878. // Do not allow the form to be cached, because $form_state['view'] can become
  879. // stale between page requests.
  880. // See views_ui_ajax_get_form() for how this affects #ajax.
  881. // @todo To remove this and allow the form to be cacheable:
  882. // - Change $form_state['view'] to $form_state['temporary']['view'].
  883. // - Add a #process function to initialize $form_state['temporary']['view']
  884. // on cached form submissions.
  885. // - Update ctools_include() to support cached forms, or else use
  886. // form_load_include().
  887. $form_state['no_cache'] = TRUE;
  888. if ($display_id) {
  889. if (!$view->set_display($display_id)) {
  890. $form['#markup'] = t('Invalid display id @display', array('@display' => $display_id));
  891. return $form;
  892. }
  893. $view->fix_missing_relationships();
  894. }
  895. ctools_include('dependent');
  896. $form['#attached']['js'][] = ctools_attach_js('dependent');
  897. $form['#attached']['js'][] = ctools_attach_js('collapsible-div');
  898. $form['#tree'] = TRUE;
  899. // @todo When more functionality is added to this form, cloning here may be
  900. // too soon. But some of what we do with $view later in this function
  901. // results in making it unserializable due to PDO limitations.
  902. $form_state['view'] = clone($view);
  903. $form['#attached']['library'][] = array('system', 'ui.tabs');
  904. $form['#attached']['library'][] = array('system', 'ui.dialog');
  905. $form['#attached']['library'][] = array('system', 'drupal.ajax');
  906. $form['#attached']['library'][] = array('system', 'jquery.form');
  907. // TODO: This should be getting added to the page when an ajax popup calls
  908. // for it, instead of having to add it manually here.
  909. $form['#attached']['js'][] = 'misc/tabledrag.js';
  910. $form['#attached']['css'] = views_ui_get_admin_css();
  911. $module_path = drupal_get_path('module', 'views_ui');
  912. $form['#attached']['js'][] = $module_path . '/js/views-admin.js';
  913. $form['#attached']['js'][] = array(
  914. 'data' => array('views' => array('ajax' => array(
  915. 'id' => '#views-ajax-body',
  916. 'title' => '#views-ajax-title',
  917. 'popup' => '#views-ajax-popup',
  918. 'defaultForm' => views_ui_get_default_ajax_message(),
  919. ))),
  920. 'type' => 'setting',
  921. );
  922. $form += array(
  923. '#prefix' => '',
  924. '#suffix' => '',
  925. );
  926. $form['#prefix'] = $form['#prefix'] . '<div class="views-edit-view views-admin clearfix">';
  927. $form['#suffix'] = '</div>' . $form['#suffix'];
  928. $form['#attributes']['class'] = array('form-edit');
  929. if (isset($view->locked) && is_object($view->locked)) {
  930. $form['locked'] = array(
  931. '#theme_wrappers' => array('container'),
  932. '#attributes' => array('class' => array('view-locked', 'messages', 'warning')),
  933. '#markup' => t('This view is being edited by user !user, and is therefore locked from editing by others. This lock is !age old. Click here to <a href="!break">break this lock</a>.', array('!user' => theme('username', array('account' => user_load($view->locked->uid))), '!age' => format_interval(REQUEST_TIME - $view->locked->updated), '!break' => url('admin/structure/views/view/' . $view->name . '/break-lock'))),
  934. );
  935. }
  936. if (isset($view->vid) && $view->vid == 'new') {
  937. $message = t('* All changes are stored temporarily. Click Save to make your changes permanent. Click Cancel to discard the view.');
  938. }
  939. else {
  940. $message = t('* All changes are stored temporarily. Click Save to make your changes permanent. Click Cancel to discard your changes.');
  941. }
  942. $form['changed'] = array(
  943. '#theme_wrappers' => array('container'),
  944. '#attributes' => array('class' => array('view-changed', 'messages', 'warning')),
  945. '#markup' => $message,
  946. );
  947. if (empty($view->changed)) {
  948. $form['changed']['#attributes']['class'][] = 'js-hide';
  949. }
  950. $form['help_text'] = array(
  951. '#prefix' => '<div>',
  952. '#suffix' => '</div>',
  953. '#markup' => t('Modify the display(s) of your view below or add new displays.'),
  954. );
  955. $form['actions'] = array(
  956. '#type' => 'actions',
  957. '#weight' => 0,
  958. );
  959. if (empty($view->changed)) {
  960. $form['actions']['#attributes'] = array(
  961. 'class' => array(
  962. 'js-hide',
  963. ),
  964. );
  965. }
  966. $form['actions']['save'] = array(
  967. '#type' => 'submit',
  968. '#value' => t('Save'),
  969. // Taken from the "old" UI. @TODO: Review and rename.
  970. '#validate' => array('views_ui_edit_view_form_validate'),
  971. '#submit' => array('views_ui_edit_view_form_submit'),
  972. );
  973. $form['actions']['cancel'] = array(
  974. '#type' => 'submit',
  975. '#value' => t('Cancel'),
  976. '#submit' => array('views_ui_edit_view_form_cancel'),
  977. );
  978. $form['displays'] = array(
  979. '#prefix' => '<h1 class="unit-title clearfix">' . t('Displays') . '</h1>' . "\n" . '<div class="views-displays">',
  980. '#suffix' => '</div>',
  981. );
  982. $form['displays']['top'] = views_ui_render_display_top($view, $display_id);
  983. // The rest requires a display to be selected.
  984. if ($display_id) {
  985. $form_state['display_id'] = $display_id;
  986. // The part of the page where editing will take place.
  987. // This element is the ctools collapsible-div container for the display edit elements.
  988. $form['displays']['settings'] = array(
  989. '#theme_wrappers' => array('container'),
  990. '#attributes' => array(
  991. 'class' => array(
  992. 'views-display-settings',
  993. 'box-margin',
  994. 'ctools-collapsible-container',
  995. ),
  996. ),
  997. '#id' => 'edit-display-settings',
  998. );
  999. $display_title = views_ui_get_display_label($view, $display_id, FALSE);
  1000. // Add a handle for the ctools collapsible-div. The handle is the title of the display
  1001. $form['displays']['settings']['tab_title']['#markup'] = '<h2 id="edit-display-settings-title" class="ctools-collapsible-handle">' . t('@display_title details', array('@display_title' => ucwords($display_title))) . '</h2>';
  1002. // Add a text that the display is disabled.
  1003. if (!empty($view->display[$display_id]->handler)) {
  1004. $enabled = $view->display[$display_id]->handler->get_option('enabled');
  1005. if (empty($enabled)) {
  1006. $form['displays']['settings']['disabled']['#markup'] = t('This display is disabled.');
  1007. }
  1008. }
  1009. // The ctools collapsible-div content
  1010. $form['displays']['settings']['settings_content']= array(
  1011. '#theme_wrappers' => array('container'),
  1012. '#id' => 'edit-display-settings-content',
  1013. '#attributes' => array(
  1014. 'class' => array(
  1015. 'ctools-collapsible-content',
  1016. ),
  1017. ),
  1018. );
  1019. // Add the edit display content
  1020. $form['displays']['settings']['settings_content']['tab_content'] = views_ui_get_display_tab($view, $display_id);
  1021. $form['displays']['settings']['settings_content']['tab_content']['#theme_wrappers'] = array('container');
  1022. $form['displays']['settings']['settings_content']['tab_content']['#attributes'] = array('class' => array('views-display-tab'));
  1023. $form['displays']['settings']['settings_content']['tab_content']['#id'] = 'views-tab-' . $display_id;
  1024. // Mark deleted displays as such.
  1025. if (!empty($view->display[$display_id]->deleted)) {
  1026. $form['displays']['settings']['settings_content']['tab_content']['#attributes']['class'][] = 'views-display-deleted';
  1027. }
  1028. // Mark disabled displays as such.
  1029. if (empty($enabled)) {
  1030. $form['displays']['settings']['settings_content']['tab_content']['#attributes']['class'][] = 'views-display-disabled';
  1031. }
  1032. // The content of the popup dialog.
  1033. $form['ajax-area'] = array(
  1034. '#theme_wrappers' => array('container'),
  1035. '#id' => 'views-ajax-popup',
  1036. );
  1037. $form['ajax-area']['ajax-title'] = array(
  1038. '#markup' => '<h2 id="views-ajax-title"></h2>',
  1039. );
  1040. $form['ajax-area']['ajax-body'] = array(
  1041. '#theme_wrappers' => array('container'),
  1042. '#id' => 'views-ajax-body',
  1043. '#markup' => views_ui_get_default_ajax_message(),
  1044. );
  1045. }
  1046. // If relationships had to be fixed, we want to get that into the cache
  1047. // so that edits work properly, and to try to get the user to save it
  1048. // so that it's not using weird fixed up relationships.
  1049. if (!empty($view->relationships_changed) && empty($_POST)) {
  1050. drupal_set_message(t('This view has been automatically updated to fix missing relationships. While this View should continue to work, you should verify that the automatic updates are correct and save this view.'));
  1051. views_ui_cache_set($view);
  1052. }
  1053. return $form;
  1054. }
  1055. /**
  1056. * Provide the preview formulas and the preview output, too.
  1057. */
  1058. function views_ui_preview_form($form, &$form_state, $view, $display_id = 'default') {
  1059. $form_state['no_cache'] = TRUE;
  1060. $form_state['view'] = $view;
  1061. $form['#attributes'] = array('class' => array('clearfix',));
  1062. // Add a checkbox controlling whether or not this display auto-previews.
  1063. $form['live_preview'] = array(
  1064. '#type' => 'checkbox',
  1065. '#id' => 'edit-displays-live-preview',
  1066. '#title' => t('Auto preview'),
  1067. '#default_value' => variable_get('views_ui_always_live_preview', TRUE),
  1068. );
  1069. // Add the arguments textfield
  1070. $form['view_args'] = array(
  1071. '#type' => 'textfield',
  1072. '#title' => t('Preview with contextual filters:'),
  1073. '#description' => t('Separate contextual filter values with a "/". For example, %example.', array('%example' => '40/12/10')),
  1074. '#id' => 'preview-args',
  1075. // '#attributes' => array('class' => array('ctools-auto-submit')),
  1076. );
  1077. // Add the preview button
  1078. $form['button'] = array(
  1079. '#type' => 'submit',
  1080. '#value' => t('Update preview'),
  1081. '#attributes' => array('class' => array('arguments-preview', 'ctools-auto-submit-click')),
  1082. '#pre_render' => array('ctools_dependent_pre_render'),
  1083. '#prefix' => '<div id="preview-submit-wrapper">',
  1084. '#suffix' => '</div>',
  1085. '#id' => 'preview-submit',
  1086. '#submit' => array('views_ui_edit_form_submit_preview'),
  1087. '#ajax' => array(
  1088. 'path' => 'admin/structure/views/view/' . $view->name . '/preview/' . $display_id . '/ajax',
  1089. 'wrapper' => 'views-preview-wrapper',
  1090. 'event' => 'click',
  1091. 'progress' => array('type' => 'throbber'),
  1092. 'method' => 'replace',
  1093. ),
  1094. // Make ENTER in arguments textfield (and other controls) submit the form
  1095. // as this button, not the Save button.
  1096. // @todo This only works for JS users. To make this work for nojs users,
  1097. // we may need to split Preview into a separate form.
  1098. '#process' => array_merge(array('views_ui_default_button'), element_info_property('submit', '#process', array())),
  1099. );
  1100. $form['#action'] = url('admin/structure/views/view/' . $view->name .'/preview/' . $display_id);
  1101. return $form;
  1102. }
  1103. /**
  1104. * Render the top of the display so it can be updated during ajax operations.
  1105. */
  1106. function views_ui_render_display_top($view, $display_id) {
  1107. $element['#theme_wrappers'] = array('views_container');
  1108. $element['#attributes']['class'] = array('views-display-top', 'clearfix');
  1109. $element['#attributes']['id'] = array('views-display-top');
  1110. // Extra actions for the display
  1111. $element['extra_actions'] = array(
  1112. '#theme' => 'links__ctools_dropbutton',
  1113. '#attributes' => array(
  1114. 'id' => 'views-display-extra-actions',
  1115. 'class' => array(
  1116. 'horizontal', 'right', 'links', 'actions',
  1117. ),
  1118. ),
  1119. '#links' => array(
  1120. 'edit-details' => array(
  1121. 'title' => t('edit view name/description'),
  1122. 'href' => "admin/structure/views/nojs/edit-details/$view->name",
  1123. 'attributes' => array('class' => array('views-ajax-link')),
  1124. ),
  1125. 'analyze' => array(
  1126. 'title' => t('analyze view'),
  1127. 'href' => "admin/structure/views/nojs/analyze/$view->name/$display_id",
  1128. 'attributes' => array('class' => array('views-ajax-link')),
  1129. ),
  1130. 'clone' => array(
  1131. 'title' => t('clone view'),
  1132. 'href' => "admin/structure/views/view/$view->name/clone",
  1133. ),
  1134. 'export' => array(
  1135. 'title' => t('export view'),
  1136. 'href' => "admin/structure/views/view/$view->name/export",
  1137. ),
  1138. 'reorder' => array(
  1139. 'title' => t('reorder displays'),
  1140. 'href' => "admin/structure/views/nojs/reorder-displays/$view->name/$display_id",
  1141. 'attributes' => array('class' => array('views-ajax-link')),
  1142. ),
  1143. ),
  1144. );
  1145. // Let other modules add additional links here.
  1146. drupal_alter('views_ui_display_top_links', $element['extra_actions']['#links'], $view, $display_id);
  1147. if (isset($view->type) && $view->type != t('Default')) {
  1148. if ($view->type == t('Overridden')) {
  1149. $element['extra_actions']['#links']['revert'] = array(
  1150. 'title' => t('revert view'),
  1151. 'href' => "admin/structure/views/view/$view->name/revert",
  1152. 'query' => array('destination' => "admin/structure/views/view/$view->name"),
  1153. );
  1154. }
  1155. else {
  1156. $element['extra_actions']['#links']['delete'] = array(
  1157. 'title' => t('delete view'),
  1158. 'href' => "admin/structure/views/view/$view->name/delete",
  1159. );
  1160. }
  1161. }
  1162. // Determine the displays available for editing.
  1163. if ($tabs = views_ui_edit_page_display_tabs($view, $display_id)) {
  1164. if ($display_id) {
  1165. $tabs[$display_id]['#active'] = TRUE;
  1166. }
  1167. $tabs['#prefix'] = '<h2 class="element-invisible">' . t('Secondary tabs') . '</h2><ul id = "views-display-menu-tabs" class="tabs secondary">';
  1168. $tabs['#suffix'] = '</ul>';
  1169. $element['tabs'] = $tabs;
  1170. }
  1171. // Buttons for adding a new display.
  1172. foreach (views_fetch_plugin_names('display', NULL, array($view->base_table)) as $type => $label) {
  1173. $element['add_display'][$type] = array(
  1174. '#type' => 'submit',
  1175. '#value' => t('Add !display', array('!display' => $label)),
  1176. '#limit_validation_errors' => array(),
  1177. '#submit' => array('views_ui_edit_form_submit_add_display', 'views_ui_edit_form_submit_delay_destination'),
  1178. '#attributes' => array('class' => array('add-display')),
  1179. // Allow JavaScript to remove the 'Add ' prefix from the button label when
  1180. // placing the button in a "Add" dropdown menu.
  1181. '#process' => array_merge(array('views_ui_form_button_was_clicked'), element_info_property('submit', '#process', array())),
  1182. '#values' => array(t('Add !display', array('!display' => $label)), $label),
  1183. );
  1184. }
  1185. return $element;
  1186. }
  1187. function views_ui_get_default_ajax_message() {
  1188. return '<div class="message">' . t("Click on an item to edit that item's details.") . '</div>';
  1189. }
  1190. /**
  1191. * Submit handler to add a display to a view.
  1192. */
  1193. function views_ui_edit_form_submit_add_display($form, &$form_state) {
  1194. $view = $form_state['view'];
  1195. // Create the new display.
  1196. $parents = $form_state['triggering_element']['#parents'];
  1197. $display_type = array_pop($parents);
  1198. $display_id = $view->add_display($display_type);
  1199. views_ui_cache_set($view);
  1200. // Redirect to the new display's edit page.
  1201. $form_state['redirect'] = 'admin/structure/views/view/' . $view->name . '/edit/' . $display_id;
  1202. }
  1203. /**
  1204. * Submit handler to duplicate a display for a view.
  1205. */
  1206. function views_ui_edit_form_submit_duplicate_display($form, &$form_state) {
  1207. $view = $form_state['view'];
  1208. $display_id = $form_state['display_id'];
  1209. // Create the new display.
  1210. $display = $view->display[$display_id];
  1211. $new_display_id = $view->add_display($display->display_plugin);
  1212. $view->display[$new_display_id] = clone $display;
  1213. $view->display[$new_display_id]->id = $new_display_id;
  1214. // By setting the current display the changed marker will appear on the new
  1215. // display.
  1216. $view->current_display = $new_display_id;
  1217. views_ui_cache_set($view);
  1218. // Redirect to the new display's edit page.
  1219. $form_state['redirect'] = 'admin/structure/views/view/' . $view->name . '/edit/' . $new_display_id;
  1220. }
  1221. /**
  1222. * Submit handler to delete a display from a view.
  1223. */
  1224. function views_ui_edit_form_submit_delete_display($form, &$form_state) {
  1225. $view = $form_state['view'];
  1226. $display_id = $form_state['display_id'];
  1227. // Mark the display for deletion.
  1228. $view->display[$display_id]->deleted = TRUE;
  1229. views_ui_cache_set($view);
  1230. // Redirect to the top-level edit page. The first remaining display will
  1231. // become the active display.
  1232. $form_state['redirect'] = 'admin/structure/views/view/' . $view->name;
  1233. }
  1234. /**
  1235. * Submit handler to add a restore a removed display to a view.
  1236. */
  1237. function views_ui_edit_form_submit_undo_delete_display($form, &$form_state) {
  1238. // Create the new display
  1239. $id = $form_state['display_id'];
  1240. $form_state['view']->display[$id]->deleted = FALSE;
  1241. // Store in cache
  1242. views_ui_cache_set($form_state['view']);
  1243. // Redirect to the top-level edit page.
  1244. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit/' . $id;
  1245. }
  1246. /**
  1247. * Submit handler to enable a disabled display.
  1248. */
  1249. function views_ui_edit_form_submit_enable_display($form, &$form_state) {
  1250. $id = $form_state['display_id'];
  1251. // set_option doesn't work because this would might affect upper displays
  1252. $form_state['view']->display[$id]->handler->set_option('enabled', TRUE);
  1253. // Store in cache
  1254. views_ui_cache_set($form_state['view']);
  1255. // Redirect to the top-level edit page.
  1256. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit/' . $id;
  1257. }
  1258. /**
  1259. * Submit handler to disable display.
  1260. */
  1261. function views_ui_edit_form_submit_disable_display($form, &$form_state) {
  1262. $id = $form_state['display_id'];
  1263. $form_state['view']->display[$id]->handler->set_option('enabled', FALSE);
  1264. // Store in cache
  1265. views_ui_cache_set($form_state['view']);
  1266. // Redirect to the top-level edit page.
  1267. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit/' . $id;
  1268. }
  1269. /**
  1270. * Submit handler when Preview button is clicked.
  1271. */
  1272. function views_ui_edit_form_submit_preview($form, &$form_state) {
  1273. // Rebuild the form with a pristine $view object.
  1274. $form_state['build_info']['args'][0] = views_ui_cache_load($form_state['view']->name);
  1275. $form_state['show_preview'] = TRUE;
  1276. $form_state['rebuild'] = TRUE;
  1277. }
  1278. /**
  1279. * Submit handler for form buttons that do not complete a form workflow.
  1280. *
  1281. * The Edit View form is a multistep form workflow, but with state managed by
  1282. * the CTools object cache rather than $form_state['rebuild']. Without this
  1283. * submit handler, buttons that add or remove displays would redirect to the
  1284. * destination parameter (e.g., when the Edit View form is linked to from a
  1285. * contextual link). This handler can be added to buttons whose form submission
  1286. * should not yet redirect to the destination.
  1287. */
  1288. function views_ui_edit_form_submit_delay_destination($form, &$form_state) {
  1289. if (isset($_GET['destination']) && $form_state['redirect'] !== FALSE) {
  1290. if (!isset($form_state['redirect'])) {
  1291. $form_state['redirect'] = $_GET['q'];
  1292. }
  1293. if (is_string($form_state['redirect'])) {
  1294. $form_state['redirect'] = array($form_state['redirect']);
  1295. }
  1296. $options = isset($form_state['redirect'][1]) ? $form_state['redirect'][1] : array();
  1297. if (!isset($options['query']['destination'])) {
  1298. $options['query']['destination'] = $_GET['destination'];
  1299. }
  1300. $form_state['redirect'][1] = $options;
  1301. unset($_GET['destination']);
  1302. }
  1303. }
  1304. /**
  1305. * Adds tabs for navigating across Displays when editing a View.
  1306. *
  1307. * This function can be called from hook_menu_local_tasks_alter() to implement
  1308. * these tabs as secondary local tasks, or it can be called from elsewhere if
  1309. * having them as secondary local tasks isn't desired. The caller is responsible
  1310. * for setting the active tab's #active property to TRUE.
  1311. *
  1312. * @param view $view
  1313. * The view which will be edited.
  1314. * @param $display_id
  1315. * The display_id which is edited on the current request.
  1316. */
  1317. function views_ui_edit_page_display_tabs($view, $display_id = NULL) {
  1318. $tabs = array();
  1319. // Create a tab for each display.
  1320. foreach ($view->display as $id => $display) {
  1321. $tabs[$id] = array(
  1322. '#theme' => 'menu_local_task',
  1323. '#link' => array(
  1324. 'title' => views_ui_get_display_label($view, $id),
  1325. 'href' => 'admin/structure/views/view/' . $view->name . '/edit/' . $id,
  1326. 'localized_options' => array(),
  1327. ),
  1328. );
  1329. if (!empty($display->deleted)) {
  1330. $tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'views-display-deleted-link';
  1331. }
  1332. if (isset($display->display_options['enabled']) && !$display->display_options['enabled']) {
  1333. $tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'views-display-disabled-link';
  1334. }
  1335. }
  1336. // If the default display isn't supposed to be shown, don't display its tab, unless it's the only display.
  1337. if ((!views_ui_show_default_display($view) && $display_id != 'default') && count($tabs) > 1) {
  1338. $tabs['default']['#access'] = FALSE;
  1339. }
  1340. // Mark the display tab as red to show validation errors.
  1341. $view->validate();
  1342. foreach ($view->display as $id => $display) {
  1343. if (!empty($view->display_errors[$id])) {
  1344. // Always show the tab.
  1345. $tabs[$id]['#access'] = TRUE;
  1346. // Add a class to mark the error and a title to make a hover tip.
  1347. $tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'error';
  1348. $tabs[$id]['#link']['localized_options']['attributes']['title'] = t('This display has one or more validation errors; please review it.');
  1349. }
  1350. }
  1351. return $tabs;
  1352. }
  1353. /**
  1354. * Controls whether or not the default display should have its own tab on edit.
  1355. */
  1356. function views_ui_show_default_display($view) {
  1357. // Always show the default display for advanced users who prefer that mode.
  1358. $advanced_mode = variable_get('views_ui_show_master_display', FALSE);
  1359. // For other users, show the default display only if there are no others, and
  1360. // hide it if there's at least one "real" display.
  1361. $additional_displays = (count($view->display) == 1);
  1362. return $advanced_mode || $additional_displays;
  1363. }
  1364. /**
  1365. * Returns a renderable array representing the edit page for one display.
  1366. */
  1367. function views_ui_get_display_tab($view, $display_id) {
  1368. $build = array();
  1369. $display = $view->display[$display_id];
  1370. // If the plugin doesn't exist, display an error message instead of an edit
  1371. // page.
  1372. if (empty($display->handler)) {
  1373. $title = isset($display->display_title) ? $display->display_title : t('Invalid');
  1374. // @TODO: Improved UX for the case where a plugin is missing.
  1375. $build['#markup'] = t("Error: Display @display refers to a plugin named '@plugin', but that plugin is not available.", array('@display' => $display->id, '@plugin' => $display->display_plugin));
  1376. }
  1377. // Build the content of the edit page.
  1378. else {
  1379. $build['details'] = views_ui_get_display_tab_details($view, $display);
  1380. }
  1381. // In AJAX context, views_ui_regenerate_tab() returns this outside of form
  1382. // context, so hook_form_views_ui_edit_form_alter() is insufficient.
  1383. drupal_alter('views_ui_display_tab', $build, $view, $display_id);
  1384. return $build;
  1385. }
  1386. /**
  1387. * Helper function to get the display details section of the edit UI.
  1388. *
  1389. * @param $view
  1390. * @param $display
  1391. *
  1392. * @return array
  1393. * A renderable page build array.
  1394. */
  1395. function views_ui_get_display_tab_details($view, $display) {
  1396. $display_title = views_ui_get_display_label($view, $display->id, FALSE);
  1397. $build = array(
  1398. '#theme_wrappers' => array('container'),
  1399. '#attributes' => array('id' => 'edit-display-settings-details',),
  1400. );
  1401. $plugin = views_fetch_plugin_data('display', $view->display[$display->id]->display_plugin);
  1402. // The following is for display purposes only. We need to determine if there is more than one button and wrap
  1403. // the buttons in a .ctools-dropbutton class if more than one is present. Otherwise, we'll just wrap the
  1404. // actions in the .ctools-button class.
  1405. $isDisplayDeleted = !empty($display->deleted);
  1406. $isDeletable = empty($plugin['no remove']);
  1407. // The master display cannot be cloned.
  1408. $isDefault = $display->id == 'default';
  1409. // @todo: Figure out why get_option doesn't work here.
  1410. $isEnabled = $display->handler->get_option('enabled');
  1411. if (!$isDisplayDeleted && $isDeletable && !$isDefault) {
  1412. $prefix = '<div class="ctools-no-js ctools-button ctools-dropbutton"><div class="ctools-link"><a href="#" class="ctools-twisty ctools-text">open</a></div><div class="ctools-content"><ul class="horizontal right actions">';
  1413. $suffix = '</ul></div></div>';
  1414. $itemElement = 'li';
  1415. }
  1416. else {
  1417. $prefix = '<div class="ctools-button"><div class="ctools-content"><ul class="horizontal right actions">';
  1418. $suffix = '</ul></div></div>';
  1419. $itemElement = 'li';
  1420. }
  1421. if ($display->id != 'default') {
  1422. $build['top']['#theme_wrappers'] = array('container');
  1423. $build['top']['#attributes']['id'] = 'edit-display-settings-top';
  1424. $build['top']['#attributes']['class'] = array('views-ui-display-tab-actions', 'views-ui-display-tab-bucket', 'clearfix');
  1425. // The Delete, Duplicate and Undo Delete buttons.
  1426. $build['top']['actions'] = array(
  1427. '#prefix' => $prefix,
  1428. '#suffix' => $suffix,
  1429. );
  1430. if (!$isDisplayDeleted) {
  1431. if (!$isEnabled) {
  1432. $build['top']['actions']['enable'] = array(
  1433. '#type' => 'submit',
  1434. '#value' => t('enable @display_title', array('@display_title' => $display_title)),
  1435. '#limit_validation_errors' => array(),
  1436. '#submit' => array('views_ui_edit_form_submit_enable_display', 'views_ui_edit_form_submit_delay_destination'),
  1437. '#prefix' => '<' . $itemElement . ' class="enable">',
  1438. "#suffix" => '</' . $itemElement . '>',
  1439. );
  1440. }
  1441. // Add a link to view the page.
  1442. elseif ($display->handler->has_path()) {
  1443. $path = $display->handler->get_path();
  1444. if (strpos($path, '%') === FALSE) {
  1445. $build['top']['actions']['path'] = array(
  1446. '#type' => 'link',
  1447. '#title' => t('view @display', array('@display' => $display->display_title)),
  1448. '#options' => array('alt' => array(t("Go to the real page for this display"))),
  1449. '#href' => $path,
  1450. '#prefix' => '<' . $itemElement . ' class="view">',
  1451. "#suffix" => '</' . $itemElement . '>',
  1452. );
  1453. }
  1454. }
  1455. if (!$isDefault) {
  1456. $build['top']['actions']['duplicate'] = array(
  1457. '#type' => 'submit',
  1458. '#value' => t('clone @display_title', array('@display_title' => $display_title)),
  1459. '#limit_validation_errors' => array(),
  1460. '#submit' => array('views_ui_edit_form_submit_duplicate_display', 'views_ui_edit_form_submit_delay_destination'),
  1461. '#prefix' => '<' . $itemElement . ' class="duplicate">',
  1462. "#suffix" => '</' . $itemElement . '>',
  1463. );
  1464. }
  1465. if ($isDeletable) {
  1466. $build['top']['actions']['delete'] = array(
  1467. '#type' => 'submit',
  1468. '#value' => t('delete @display_title', array('@display_title' => $display_title)),
  1469. '#limit_validation_errors' => array(),
  1470. '#submit' => array('views_ui_edit_form_submit_delete_display', 'views_ui_edit_form_submit_delay_destination'),
  1471. '#prefix' => '<' . $itemElement . ' class="delete">',
  1472. "#suffix" => '</' . $itemElement . '>',
  1473. );
  1474. }
  1475. if ($isEnabled) {
  1476. $build['top']['actions']['disable'] = array(
  1477. '#type' => 'submit',
  1478. '#value' => t('disable @display_title', array('@display_title' => $display_title)),
  1479. '#limit_validation_errors' => array(),
  1480. '#submit' => array('views_ui_edit_form_submit_disable_display', 'views_ui_edit_form_submit_delay_destination'),
  1481. '#prefix' => '<' . $itemElement . ' class="disable">',
  1482. "#suffix" => '</' . $itemElement . '>',
  1483. );
  1484. }
  1485. }
  1486. else {
  1487. $build['top']['actions']['undo_delete'] = array(
  1488. '#type' => 'submit',
  1489. '#value' => t('undo delete of @display_title', array('@display_title' => $display_title)),
  1490. '#limit_validation_errors' => array(),
  1491. '#submit' => array('views_ui_edit_form_submit_undo_delete_display', 'views_ui_edit_form_submit_delay_destination'),
  1492. '#prefix' => '<' . $itemElement . ' class="undo-delete">',
  1493. "#suffix" => '</' . $itemElement . '>',
  1494. );
  1495. }
  1496. // The area above the three columns.
  1497. $build['top']['display_title'] = array(
  1498. '#theme' => 'views_ui_display_tab_setting',
  1499. '#description' => t('Display name'),
  1500. '#link' => $display->handler->option_link(check_plain($display_title), 'display_title'),
  1501. );
  1502. }
  1503. $build['columns'] = array();
  1504. $build['columns']['#theme_wrappers'] = array('container');
  1505. $build['columns']['#attributes'] = array('id' => 'edit-display-settings-main', 'class' => array('clearfix', 'views-display-columns'),);
  1506. $build['columns']['first']['#theme_wrappers'] = array('container');
  1507. $build['columns']['first']['#attributes'] = array('class' => array('views-display-column', 'first'));
  1508. $build['columns']['second']['#theme_wrappers'] = array('container');
  1509. $build['columns']['second']['#attributes'] = array('class' => array('views-display-column', 'second'));
  1510. $build['columns']['second']['settings'] = array();
  1511. $build['columns']['second']['header'] = array();
  1512. $build['columns']['second']['footer'] = array();
  1513. $build['columns']['second']['pager'] = array();
  1514. // The third column buckets are wrapped in a CTools collapsible div
  1515. $build['columns']['third']['#theme_wrappers'] = array('container');
  1516. $build['columns']['third']['#attributes'] = array('class' => array('views-display-column', 'third', 'ctools-collapsible-container', 'ctools-collapsible-remember'));
  1517. // Specify an id that won't change after AJAX requests, so ctools can keep
  1518. // track of the user's preferred collapsible state. Use the same id across
  1519. // different displays of the same view, so changing displays doesn't
  1520. // recollapse the column.
  1521. $build['columns']['third']['#attributes']['id'] = 'views-ui-advanced-column-' . $view->name;
  1522. // Collapse the div by default.
  1523. if (!variable_get('views_ui_show_advanced_column', FALSE)) {
  1524. $build['columns']['third']['#attributes']['class'][] = 'ctools-collapsed';
  1525. }
  1526. $build['columns']['third']['advanced'] = array('#markup' => '<h3 class="ctools-collapsible-handle"><a href="">' . t('Advanced') . '</a></h3>',);
  1527. $build['columns']['third']['collapse']['#theme_wrappers'] = array('container');
  1528. $build['columns']['third']['collapse']['#attributes'] = array('class' => array('ctools-collapsible-content',),);
  1529. // Each option (e.g. title, access, display as grid/table/list) fits into one
  1530. // of several "buckets," or boxes (Format, Fields, Sort, and so on).
  1531. $buckets = array();
  1532. // Fetch options from the display plugin, with a list of buckets they go into.
  1533. $options = array();
  1534. $display->handler->options_summary($buckets, $options);
  1535. // Place each option into its bucket.
  1536. foreach ($options as $id => $option) {
  1537. // Each option self-identifies as belonging in a particular bucket.
  1538. $buckets[$option['category']]['build'][$id] = views_ui_edit_form_get_build_from_option($id, $option, $view, $display);
  1539. }
  1540. // Place each bucket into the proper column.
  1541. foreach ($buckets as $id => $bucket) {
  1542. // Let buckets identify themselves as belonging in a column.
  1543. if (isset($bucket['column']) && isset($build['columns'][$bucket['column']])) {
  1544. $column = $bucket['column'];
  1545. }
  1546. // If a bucket doesn't pick one of our predefined columns to belong to, put
  1547. // it in the last one.
  1548. else {
  1549. $column = 'third';
  1550. }
  1551. if (isset($bucket['build']) && is_array($bucket['build'])) {
  1552. // The third column is a CTools collapsible div, so
  1553. // the structure of the form is a little different for this column
  1554. if ($column === 'third') {
  1555. $build['columns'][$column]['collapse'][$id] = $bucket['build'];
  1556. $build['columns'][$column]['collapse'][$id]['#theme_wrappers'][] = 'views_ui_display_tab_bucket';
  1557. $build['columns'][$column]['collapse'][$id]['#title'] = !empty($bucket['title']) ? $bucket['title'] : '';
  1558. $build['columns'][$column]['collapse'][$id]['#name'] = !empty($bucket['title']) ? $bucket['title'] : $id;
  1559. }
  1560. else {
  1561. $build['columns'][$column][$id] = $bucket['build'];
  1562. $build['columns'][$column][$id]['#theme_wrappers'][] = 'views_ui_display_tab_bucket';
  1563. $build['columns'][$column][$id]['#title'] = !empty($bucket['title']) ? $bucket['title'] : '';
  1564. $build['columns'][$column][$id]['#name'] = !empty($bucket['title']) ? $bucket['title'] : $id;
  1565. }
  1566. }
  1567. }
  1568. $build['columns']['first']['fields'] = views_ui_edit_form_get_bucket('field', $view, $display);
  1569. $build['columns']['first']['filters'] = views_ui_edit_form_get_bucket('filter', $view, $display);
  1570. $build['columns']['first']['sorts'] = views_ui_edit_form_get_bucket('sort', $view, $display);
  1571. $build['columns']['second']['header'] = views_ui_edit_form_get_bucket('header', $view, $display);
  1572. $build['columns']['second']['footer'] = views_ui_edit_form_get_bucket('footer', $view, $display);
  1573. $build['columns']['third']['collapse']['arguments'] = views_ui_edit_form_get_bucket('argument', $view, $display);
  1574. $build['columns']['third']['collapse']['relationships'] = views_ui_edit_form_get_bucket('relationship', $view, $display);
  1575. $build['columns']['third']['collapse']['empty'] = views_ui_edit_form_get_bucket('empty', $view, $display);
  1576. return $build;
  1577. }
  1578. /**
  1579. * Build a renderable array representing one option on the edit form.
  1580. *
  1581. * This function might be more logical as a method on an object, if a suitable
  1582. * object emerges out of refactoring.
  1583. */
  1584. function views_ui_edit_form_get_build_from_option($id, $option, $view, $display) {
  1585. $option_build = array();
  1586. $option_build['#theme'] = 'views_ui_display_tab_setting';
  1587. $option_build['#description'] = $option['title'];
  1588. $option_build['#link'] = $display->handler->option_link($option['value'], $id, '', empty($option['desc']) ? '' : $option['desc']);
  1589. $option_build['#links'] = array();
  1590. if (!empty($option['links']) && is_array($option['links'])) {
  1591. foreach ($option['links'] as $link_id => $link_value) {
  1592. $option_build['#settings_links'][] = $display->handler->option_link($option['setting'], $link_id, 'views-button-configure', $link_value);
  1593. }
  1594. }
  1595. if (!empty($display->handler->options['defaults'][$id])) {
  1596. $display_id = 'default';
  1597. $option_build['#defaulted'] = TRUE;
  1598. }
  1599. else {
  1600. $display_id = $display->id;
  1601. if (!$display->handler->is_default_display()) {
  1602. if ($display->handler->defaultable_sections($id)) {
  1603. $option_build['#overridden'] = TRUE;
  1604. }
  1605. }
  1606. }
  1607. $option_build['#attributes']['class'][] = drupal_clean_css_identifier($display_id . '-' . $id);
  1608. if (!empty($view->changed_sections[$display_id . '-' . $id])) {
  1609. $option_build['#changed'] = TRUE;
  1610. }
  1611. return $option_build;
  1612. }
  1613. function template_preprocess_views_ui_display_tab_setting(&$variables) {
  1614. static $zebra = 0;
  1615. $variables['zebra'] = ($zebra % 2 === 0 ? 'odd' : 'even');
  1616. $zebra++;
  1617. // Put the main link to the left side
  1618. array_unshift($variables['settings_links'], $variables['link']);
  1619. $variables['settings_links'] = implode('<span class="label">&nbsp;|&nbsp;</span>', $variables['settings_links']);
  1620. // Add classes associated with this display tab to the overall list.
  1621. $variables['classes_array'] = array_merge($variables['classes_array'], $variables['class']);
  1622. if (!empty($variables['defaulted'])) {
  1623. $variables['classes_array'][] = 'defaulted';
  1624. }
  1625. if (!empty($variables['overridden'])) {
  1626. $variables['classes_array'][] = 'overridden';
  1627. $variables['attributes_array']['title'][] = t('Overridden');
  1628. }
  1629. // Append a colon to the description, if requested.
  1630. if ($variables['description'] && $variables['description_separator']) {
  1631. $variables['description'] .= t(':');
  1632. }
  1633. }
  1634. function template_preprocess_views_ui_display_tab_bucket(&$variables) {
  1635. $element = $variables['element'];
  1636. $variables['item_help_icon'] = '';
  1637. if (!empty($element['#item_help_icon'])) {
  1638. $variables['item_help_icon'] = render($element['#item_help_icon']);
  1639. }
  1640. if (!empty($element['#name'])) {
  1641. $variables['classes_array'][] = drupal_clean_css_identifier(strtolower($element['#name']));
  1642. }
  1643. if (!empty($element['#overridden'])) {
  1644. $variables['classes_array'][] = 'overridden';
  1645. $variables['attributes_array']['title'][] = t('Overridden');
  1646. }
  1647. $variables['content'] = $element['#children'];
  1648. $variables['title'] = $element['#title'];
  1649. $variables['actions'] = !empty($element['#actions']) ? $element['#actions'] : '';
  1650. }
  1651. function template_preprocess_views_ui_display_tab_column(&$variables) {
  1652. $element = $variables['element'];
  1653. $variables['content'] = $element['#children'];
  1654. $variables['column'] = $element['#column'];
  1655. }
  1656. /**
  1657. * Move form elements into fieldsets for presentation purposes.
  1658. *
  1659. * Many views forms use #tree = TRUE to keep their values in a hierarchy for
  1660. * easier storage. Moving the form elements into fieldsets during form building
  1661. * would break up that hierarchy. Therefore, we wait until the pre_render stage,
  1662. * where any changes we make affect presentation only and aren't reflected in
  1663. * $form_state['values'].
  1664. */
  1665. function views_ui_pre_render_add_fieldset_markup($form) {
  1666. foreach (element_children($form) as $key) {
  1667. $element = $form[$key];
  1668. // In our form builder functions, we added an arbitrary #fieldset property
  1669. // to any element that belongs in a fieldset. If this form element has that
  1670. // property, move it into its fieldset.
  1671. if (isset($element['#fieldset']) && isset($form[$element['#fieldset']])) {
  1672. $form[$element['#fieldset']][$key] = $element;
  1673. // Remove the original element this duplicates.
  1674. unset($form[$key]);
  1675. }
  1676. }
  1677. return $form;
  1678. }
  1679. /**
  1680. * Flattens the structure of an element containing the #flatten property.
  1681. *
  1682. * If a form element has #flatten = TRUE, then all of it's children
  1683. * get moved to the same level as the element itself.
  1684. * So $form['to_be_flattened'][$key] becomes $form[$key], and
  1685. * $form['to_be_flattened'] gets unset.
  1686. */
  1687. function views_ui_pre_render_flatten_data($form) {
  1688. foreach (element_children($form) as $key) {
  1689. $element = $form[$key];
  1690. if (!empty($element['#flatten'])) {
  1691. foreach (element_children($element) as $child_key) {
  1692. $form[$child_key] = $form[$key][$child_key];
  1693. }
  1694. // All done, remove the now-empty parent.
  1695. unset($form[$key]);
  1696. }
  1697. }
  1698. return $form;
  1699. }
  1700. /**
  1701. * Moves argument options into their place.
  1702. *
  1703. * When configuring the default argument behavior, almost each of the radio
  1704. * buttons has its own fieldset shown bellow it when the radio button is
  1705. * clicked. That fieldset is created through a custom form process callback.
  1706. * Each element that has #argument_option defined and pointing to a default
  1707. * behavior gets moved to the appropriate fieldset.
  1708. * So if #argument_option is specified as 'default', the element is moved
  1709. * to the 'default_options' fieldset.
  1710. */
  1711. function views_ui_pre_render_move_argument_options($form) {
  1712. foreach (element_children($form) as $key) {
  1713. $element = $form[$key];
  1714. if (!empty($element['#argument_option'])) {
  1715. $container_name = $element['#argument_option'] . '_options';
  1716. if (isset($form['no_argument']['default_action'][$container_name])) {
  1717. $form['no_argument']['default_action'][$container_name][$key] = $element;
  1718. }
  1719. // Remove the original element this duplicates.
  1720. unset($form[$key]);
  1721. }
  1722. }
  1723. return $form;
  1724. }
  1725. /**
  1726. * Custom form radios process function.
  1727. *
  1728. * Roll out a single radios element to a list of radios,
  1729. * using the options array as index.
  1730. * While doing that, create a container element underneath each option, which
  1731. * contains the settings related to that option.
  1732. *
  1733. * @see form_process_radios()
  1734. */
  1735. function views_ui_process_container_radios($element) {
  1736. if (count($element['#options']) > 0) {
  1737. foreach ($element['#options'] as $key => $choice) {
  1738. $element += array($key => array());
  1739. // Generate the parents as the autogenerator does, so we will have a
  1740. // unique id for each radio button.
  1741. $parents_for_id = array_merge($element['#parents'], array($key));
  1742. $element[$key] += array(
  1743. '#type' => 'radio',
  1744. '#title' => $choice,
  1745. // The key is sanitized in drupal_attributes() during output from the
  1746. // theme function.
  1747. '#return_value' => $key,
  1748. '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : NULL,
  1749. '#attributes' => $element['#attributes'],
  1750. '#parents' => $element['#parents'],
  1751. '#id' => drupal_html_id('edit-' . implode('-', $parents_for_id)),
  1752. '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
  1753. );
  1754. $element[$key . '_options'] = array(
  1755. '#type' => 'container',
  1756. '#attributes' => array('class' => array('views-admin-dependent')),
  1757. );
  1758. }
  1759. }
  1760. return $element;
  1761. }
  1762. /**
  1763. * Import a view from cut & paste.
  1764. */
  1765. function views_ui_import_page($form, &$form_state) {
  1766. $form['name'] = array(
  1767. '#type' => 'textfield',
  1768. '#title' => t('View name'),
  1769. '#description' => t('Enter the name to use for this view if it is different from the source view. Leave blank to use the name of the view.'),
  1770. );
  1771. $form['name_override'] = array(
  1772. '#type' => 'checkbox',
  1773. '#title' => t('Replace an existing view if one exists with the same name'),
  1774. );
  1775. $form['bypass_validation'] = array(
  1776. '#type' => 'checkbox',
  1777. '#title' => t('Bypass view validation'),
  1778. '#description' => t('Bypass the validation of plugins and handlers when importing this view.'),
  1779. );
  1780. $form['view'] = array(
  1781. '#type' => 'textarea',
  1782. '#title' => t('Paste view code here'),
  1783. '#required' => TRUE,
  1784. );
  1785. $form['submit'] = array(
  1786. '#type' => 'submit',
  1787. '#value' => t('Import'),
  1788. '#submit' => array('views_ui_import_submit'),
  1789. '#validate' => array('views_ui_import_validate'),
  1790. );
  1791. return $form;
  1792. }
  1793. /**
  1794. * Validate handler to import a view.
  1795. */
  1796. function views_ui_import_validate($form, &$form_state) {
  1797. $view = '';
  1798. views_include('view');
  1799. // Be forgiving if someone pastes views code that starts with '<?php'.
  1800. if (substr($form_state['values']['view'], 0, 5) == '<?php') {
  1801. $form_state['values']['view'] = substr($form_state['values']['view'], 5);
  1802. }
  1803. ob_start();
  1804. eval($form_state['values']['view']);
  1805. ob_end_clean();
  1806. if (!is_object($view)) {
  1807. return form_error($form['view'], t('Unable to interpret view code.'));
  1808. }
  1809. if (empty($view->api_version) || $view->api_version < 2) {
  1810. form_error($form['view'], t('That view is not compatible with this version of Views.
  1811. If you have a view from views1 you have to go to a drupal6 installation and import it there.'));
  1812. }
  1813. elseif (version_compare($view->api_version, views_api_version(), '>')) {
  1814. form_error($form['view'], t('That view is created for the version @import_version of views, but you only have @api_version', array(
  1815. '@import_version' => $view->api_version,
  1816. '@api_version' => views_api_version())));
  1817. }
  1818. // View name must be alphanumeric or underscores, no other punctuation.
  1819. if (!empty($form_state['values']['name']) && preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
  1820. form_error($form['name'], t('View name must be alphanumeric or underscores only.'));
  1821. }
  1822. if ($form_state['values']['name']) {
  1823. $view->name = $form_state['values']['name'];
  1824. }
  1825. $test = views_get_view($view->name);
  1826. if (!$form_state['values']['name_override']) {
  1827. if ($test && $test->type != t('Default')) {
  1828. form_set_error('', t('A view by that name already exists; please choose a different name'));
  1829. }
  1830. }
  1831. else {
  1832. if ($test->vid) {
  1833. $view->vid = $test->vid;
  1834. }
  1835. }
  1836. // Make sure base table gets set properly if it got moved.
  1837. $view->update();
  1838. $view->init_display();
  1839. $broken = FALSE;
  1840. // Bypass the validation of view pluigns/handlers if option is checked.
  1841. if (!$form_state['values']['bypass_validation']) {
  1842. // Make sure that all plugins and handlers needed by this view actually exist.
  1843. foreach ($view->display as $id => $display) {
  1844. if (empty($display->handler) || !empty($display->handler->broken)) {
  1845. drupal_set_message(t('Display plugin @plugin is not available.', array('@plugin' => $display->display_plugin)), 'error');
  1846. $broken = TRUE;
  1847. continue;
  1848. }
  1849. $plugin = views_get_plugin('style', $display->handler->get_option('style_plugin'));
  1850. if (!$plugin) {
  1851. drupal_set_message(t('Style plugin @plugin is not available.', array('@plugin' => $display->handler->get_option('style_plugin'))), 'error');
  1852. $broken = TRUE;
  1853. }
  1854. elseif ($plugin->uses_row_plugin()) {
  1855. $plugin = views_get_plugin('row', $display->handler->get_option('row_plugin'));
  1856. if (!$plugin) {
  1857. drupal_set_message(t('Row plugin @plugin is not available.', array('@plugin' => $display->handler->get_option('row_plugin'))), 'error');
  1858. $broken = TRUE;
  1859. }
  1860. }
  1861. foreach (views_object_types() as $type => $info) {
  1862. $handlers = $display->handler->get_handlers($type);
  1863. if ($handlers) {
  1864. foreach ($handlers as $id => $handler) {
  1865. if ($handler->broken()) {
  1866. drupal_set_message(t('@type handler @table.@field is not available.', array(
  1867. '@type' => $info['stitle'],
  1868. '@table' => $handler->table,
  1869. '@field' => $handler->field,
  1870. )), 'error');
  1871. $broken = TRUE;
  1872. }
  1873. }
  1874. }
  1875. }
  1876. }
  1877. }
  1878. if ($broken) {
  1879. form_set_error('', t('Unable to import view.'));
  1880. }
  1881. $form_state['view'] = &$view;
  1882. }
  1883. /**
  1884. * Submit handler for view import.
  1885. */
  1886. function views_ui_import_submit($form, &$form_state) {
  1887. // Store in cache and then go to edit.
  1888. views_ui_cache_set($form_state['view']);
  1889. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit';
  1890. }
  1891. /**
  1892. * Validate that a view is complete and whole.
  1893. */
  1894. function views_ui_edit_view_form_validate($form, &$form_state) {
  1895. // Do not validate cancel or delete or revert.
  1896. if (empty($form_state['clicked_button']['#value']) || $form_state['clicked_button']['#value'] != t('Save')) {
  1897. return;
  1898. }
  1899. $errors = $form_state['view']->validate();
  1900. if ($errors !== TRUE) {
  1901. foreach ($errors as $error) {
  1902. form_set_error('', $error);
  1903. }
  1904. }
  1905. }
  1906. /**
  1907. * Submit handler for the edit view form.
  1908. */
  1909. function views_ui_edit_view_form_submit($form, &$form_state) {
  1910. // Go through and remove displayed scheduled for removal.
  1911. foreach ($form_state['view']->display as $id => $display) {
  1912. if (!empty($display->deleted)) {
  1913. unset($form_state['view']->display[$id]);
  1914. }
  1915. }
  1916. // Rename display ids if needed.
  1917. foreach ($form_state['view']->display as $id => $display) {
  1918. if (!empty($display->new_id)) {
  1919. $form_state['view']->display[$id]->id = $display->new_id;
  1920. // Redirect the user to the renamed display to be sure that the page itself exists and doesn't throw errors.
  1921. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit/' . $display->new_id;
  1922. }
  1923. }
  1924. // Direct the user to the right url, if the path of the display has changed.
  1925. if (!empty($_GET['destination'])) {
  1926. $destination = $_GET['destination'];
  1927. // Find out the first display which has a changed path and redirect to this url.
  1928. $old_view = views_get_view($form_state['view']->name);
  1929. foreach ($old_view->display as $id => $display) {
  1930. // Only check for displays with a path.
  1931. if (!isset($display->display_options['path'])) {
  1932. continue;
  1933. }
  1934. $old_path = $display->display_options['path'];
  1935. if (($display->display_plugin == 'page') && ($old_path == $destination) && ($old_path != $form_state['view']->display[$id]->display_options['path'])) {
  1936. $destination = $form_state['view']->display[$id]->display_options['path'];
  1937. unset($_GET['destination']);
  1938. }
  1939. }
  1940. $form_state['redirect'] = $destination;
  1941. }
  1942. $form_state['view']->save();
  1943. drupal_set_message(t('The view %name has been saved.', array('%name' => $form_state['view']->get_human_name())));
  1944. // Remove this view from cache so we can edit it properly.
  1945. ctools_object_cache_clear('view', $form_state['view']->name);
  1946. }
  1947. /**
  1948. * Submit handler for the edit view form.
  1949. */
  1950. function views_ui_edit_view_form_cancel($form, &$form_state) {
  1951. // Remove this view from cache so edits will be lost.
  1952. ctools_object_cache_clear('view', $form_state['view']->name);
  1953. if (empty($form['view']->vid)) {
  1954. // I seem to have to drupal_goto here because I can't get fapi to
  1955. // honor the redirect target. Not sure what I screwed up here.
  1956. drupal_goto('admin/structure/views');
  1957. }
  1958. }
  1959. function views_ui_edit_view_form_delete($form, &$form_state) {
  1960. unset($_REQUEST['destination']);
  1961. // Redirect to the delete confirm page
  1962. $form_state['redirect'] = array('admin/structure/views/view/' . $form_state['view']->name . '/delete', array('query' => drupal_get_destination() + array('cancel' => 'admin/structure/views/view/' . $form_state['view']->name . '/edit')));
  1963. }
  1964. /**
  1965. * Add information about a section to a display.
  1966. */
  1967. function views_ui_edit_form_get_bucket($type, $view, $display) {
  1968. $build = array(
  1969. '#theme_wrappers' => array('views_ui_display_tab_bucket'),
  1970. );
  1971. $types = views_object_types();
  1972. $build['#overridden'] = FALSE;
  1973. $build['#defaulted'] = FALSE;
  1974. if (module_exists('advanced_help')) {
  1975. $build['#item_help_icon'] = array(
  1976. '#theme' => 'advanced_help_topic',
  1977. '#module' => 'views',
  1978. '#topic' => $type,
  1979. );
  1980. }
  1981. $build['#name'] = $build['#title'] = $types[$type]['title'];
  1982. // Different types now have different rearrange forms, so we use this switch
  1983. // to get the right one.
  1984. switch ($type) {
  1985. case 'filter':
  1986. $rearrange_url = "admin/structure/views/nojs/rearrange-$type/$view->name/$display->id/$type";
  1987. $rearrange_text = t('And/Or, Rearrange');
  1988. // TODO: Add another class to have another symbol for filter rearrange.
  1989. $class = 'icon compact rearrange';
  1990. break;
  1991. case 'field':
  1992. // Fetch the style plugin info so we know whether to list fields or not.
  1993. $style_plugin = $display->handler->get_plugin();
  1994. $uses_fields = $style_plugin && $style_plugin->uses_fields();
  1995. if (!$uses_fields) {
  1996. $build['fields'][] = array(
  1997. '#markup' => t('The selected style or row format does not utilize fields.'),
  1998. '#theme_wrappers' => array('views_container'),
  1999. '#attributes' => array('class' => array('views-display-setting')),
  2000. );
  2001. return $build;
  2002. }
  2003. default:
  2004. $rearrange_url = "admin/structure/views/nojs/rearrange/$view->name/$display->id/$type";
  2005. $rearrange_text = t('Rearrange');
  2006. $class = 'icon compact rearrange';
  2007. }
  2008. // Create an array of actions to pass to theme_links
  2009. $actions = array();
  2010. $count_handlers = count($display->handler->get_handlers($type));
  2011. $actions['add'] = array(
  2012. 'title' => t('Add'),
  2013. 'href' => "admin/structure/views/nojs/add-item/$view->name/$display->id/$type",
  2014. 'attributes'=> array('class' => array('icon compact add', 'views-ajax-link'), 'title' => t('Add'), 'id' => 'views-add-' . $type),
  2015. 'html' => TRUE,
  2016. );
  2017. if ($count_handlers > 0) {
  2018. $actions['rearrange'] = array(
  2019. 'title' => $rearrange_text,
  2020. 'href' => $rearrange_url,
  2021. 'attributes' => array('class' => array($class, 'views-ajax-link'), 'title' => t('Rearrange'), 'id' => 'views-rearrange-' . $type),
  2022. 'html' => TRUE,
  2023. );
  2024. }
  2025. // Render the array of links
  2026. $build['#actions'] = theme('links__ctools_dropbutton',
  2027. array(
  2028. 'links' => $actions,
  2029. 'attributes' => array(
  2030. 'class' => array('inline', 'links', 'actions', 'horizontal', 'right')
  2031. ),
  2032. 'class' => array('views-ui-settings-bucket-operations'),
  2033. )
  2034. );
  2035. if (!$display->handler->is_default_display()) {
  2036. if (!$display->handler->is_defaulted($types[$type]['plural'])) {
  2037. $build['#overridden'] = TRUE;
  2038. }
  2039. else {
  2040. $build['#defaulted'] = TRUE;
  2041. }
  2042. }
  2043. // If there's an options form for the bucket, link to it.
  2044. if (!empty($types[$type]['options'])) {
  2045. $build['#title'] = l($build['#title'], "admin/structure/views/nojs/config-type/$view->name/$display->id/$type", array('attributes' => array('class' => array('views-ajax-link'), 'id' => 'views-title-' . $type)));
  2046. }
  2047. static $relationships = NULL;
  2048. if (!isset($relationships)) {
  2049. // Get relationship labels
  2050. $relationships = array();
  2051. // @todo: get_handlers()
  2052. $handlers = $display->handler->get_option('relationships');
  2053. if ($handlers) {
  2054. foreach ($handlers as $id => $info) {
  2055. $handler = $display->handler->get_handler('relationship', $id);
  2056. $relationships[$id] = $handler->label();
  2057. }
  2058. }
  2059. }
  2060. // Filters can now be grouped so we do a little bit extra:
  2061. $groups = array();
  2062. $grouping = FALSE;
  2063. if ($type == 'filter') {
  2064. $group_info = $view->display_handler->get_option('filter_groups');
  2065. // If there is only one group but it is using the "OR" filter, we still
  2066. // treat it as a group for display purposes, since we want to display the
  2067. // "OR" label next to items within the group.
  2068. if (!empty($group_info['groups']) && (count($group_info['groups']) > 1 || current($group_info['groups']) == 'OR')) {
  2069. $grouping = TRUE;
  2070. $groups = array(0 => array());
  2071. }
  2072. }
  2073. $build['fields'] = array();
  2074. foreach ($display->handler->get_option($types[$type]['plural']) as $id => $field) {
  2075. // Build the option link for this handler ("Node: ID = article").
  2076. $build['fields'][$id] = array();
  2077. $build['fields'][$id]['#theme'] = 'views_ui_display_tab_setting';
  2078. $handler = $display->handler->get_handler($type, $id);
  2079. if (empty($handler)) {
  2080. $build['fields'][$id]['#class'][] = 'broken';
  2081. $field_name = t('Broken/missing handler: @table > @field', array('@table' => $field['table'], '@field' => $field['field']));
  2082. $build['fields'][$id]['#link'] = l($field_name, "admin/structure/views/nojs/config-item/$view->name/$display->id/$type/$id", array('attributes' => array('class' => array('views-ajax-link')), 'html' => TRUE));
  2083. continue;
  2084. }
  2085. $field_name = check_plain($handler->ui_name(TRUE));
  2086. if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
  2087. $field_name = '(' . $relationships[$field['relationship']] . ') ' . $field_name;
  2088. }
  2089. $description = filter_xss_admin($handler->admin_summary());
  2090. $link_text = $field_name . (empty($description) ? '' : " ($description)");
  2091. $link_attributes = array('class' => array('views-ajax-link'));
  2092. if (!empty($field['exclude'])) {
  2093. $link_attributes['class'][] = 'views-field-excluded';
  2094. }
  2095. $build['fields'][$id]['#link'] = l($link_text, "admin/structure/views/nojs/config-item/$view->name/$display->id/$type/$id", array('attributes' => $link_attributes, 'html' => TRUE));
  2096. $build['fields'][$id]['#class'][] = drupal_clean_css_identifier($display->id . '-' . $type . '-' . $id);
  2097. if (!empty($view->changed_sections[$display->id . '-' . $type . '-' . $id])) {
  2098. // @TODO: #changed is no longer being used?
  2099. $build['fields'][$id]['#changed'] = TRUE;
  2100. }
  2101. if ($display->handler->use_group_by() && $handler->use_group_by()) {
  2102. $build['fields'][$id]['#settings_links'][] = l('<span class="label">' . t('Aggregation settings') . '</span>', "admin/structure/views/nojs/config-item-group/$view->name/$display->id/$type/$id", array('attributes' => array('class' => 'views-button-configure views-ajax-link', 'title' => t('Aggregation settings')), 'html' => true));
  2103. }
  2104. if ($handler->has_extra_options()) {
  2105. $build['fields'][$id]['#settings_links'][] = l('<span class="label">' . t('Settings') . '</span>', "admin/structure/views/nojs/config-item-extra/$view->name/$display->id/$type/$id", array('attributes' => array('class' => array('views-button-configure', 'views-ajax-link'), 'title' => t('Settings')), 'html' => true));
  2106. }
  2107. if ($grouping) {
  2108. $gid = $handler->options['group'];
  2109. // Show in default group if the group does not exist.
  2110. if (empty($group_info['groups'][$gid])) {
  2111. $gid = 0;
  2112. }
  2113. $groups[$gid][] = $id;
  2114. }
  2115. }
  2116. // If using grouping, re-order fields so that they show up properly in the list.
  2117. if ($type == 'filter' && $grouping) {
  2118. $store = $build['fields'];
  2119. $build['fields'] = array();
  2120. foreach ($groups as $gid => $contents) {
  2121. // Display an operator between each group.
  2122. if (!empty($build['fields'])) {
  2123. $build['fields'][] = array(
  2124. '#theme' => 'views_ui_display_tab_setting',
  2125. '#class' => array('views-group-text'),
  2126. '#link' => ($group_info['operator'] == 'OR' ? t('OR') : t('AND')),
  2127. );
  2128. }
  2129. // Display an operator between each pair of filters within the group.
  2130. $keys = array_keys($contents);
  2131. $last = end($keys);
  2132. foreach ($contents as $key => $pid) {
  2133. if ($key != $last) {
  2134. $store[$pid]['#link'] .= '&nbsp;&nbsp;' . ($group_info['groups'][$gid] == 'OR' ? t('OR') : t('AND'));
  2135. }
  2136. $build['fields'][$pid] = $store[$pid];
  2137. }
  2138. }
  2139. }
  2140. return $build;
  2141. }
  2142. /**
  2143. * Regenerate the current tab for AJAX updates.
  2144. */
  2145. function views_ui_regenerate_tab(&$view, &$output, $display_id) {
  2146. if (!$view->set_display('default')) {
  2147. return;
  2148. }
  2149. // Regenerate the main display area.
  2150. $build = views_ui_get_display_tab($view, $display_id);
  2151. views_ui_add_microweights($build);
  2152. $output[] = ajax_command_html('#views-tab-' . $display_id, drupal_render($build));
  2153. // Regenerate the top area so changes to display names and order will appear.
  2154. $build = views_ui_render_display_top($view, $display_id);
  2155. views_ui_add_microweights($build);
  2156. $output[] = ajax_command_replace('#views-display-top', drupal_render($build));
  2157. }
  2158. /**
  2159. * Recursively adds microweights to a render array, similar to what form_builder() does for forms.
  2160. *
  2161. * @todo Submit a core patch to fix drupal_render() to do this, so that all
  2162. * render arrays automatically preserve array insertion order, as forms do.
  2163. */
  2164. function views_ui_add_microweights(&$build) {
  2165. $count = 0;
  2166. foreach (element_children($build) as $key) {
  2167. if (!isset($build[$key]['#weight'])) {
  2168. $build[$key]['#weight'] = $count/1000;
  2169. }
  2170. views_ui_add_microweights($build[$key]);
  2171. $count++;
  2172. }
  2173. }
  2174. /**
  2175. * Provide a standard set of Apply/Cancel/OK buttons for the forms. Also provide
  2176. * a hidden op operator because the forms plugin doesn't seem to properly
  2177. * provide which button was clicked.
  2178. *
  2179. * TODO: Is the hidden op operator still here somewhere, or is that part of the
  2180. * docblock outdated?
  2181. */
  2182. function views_ui_standard_form_buttons(&$form, &$form_state, $form_id, $name = NULL, $third = NULL, $submit = NULL) {
  2183. $form['buttons'] = array(
  2184. '#prefix' => '<div class="clearfix"><div class="form-buttons">',
  2185. '#suffix' => '</div></div>',
  2186. );
  2187. if (empty($name)) {
  2188. $name = t('Apply');
  2189. $view = $form_state['view'];
  2190. if (!empty($view->stack) && count($view->stack) > 1) {
  2191. $name = t('Apply and continue');
  2192. }
  2193. $names = array(t('Apply'), t('Apply and continue'));
  2194. }
  2195. // Forms that are purely informational set an ok_button flag, so we know not
  2196. // to create an "Apply" button for them.
  2197. if (empty($form_state['ok_button'])) {
  2198. $form['buttons']['submit'] = array(
  2199. '#type' => 'submit',
  2200. '#value' => $name,
  2201. // The regular submit handler ($form_id . '_submit') does not apply if
  2202. // we're updating the default display. It does apply if we're updating
  2203. // the current display. Since we have no way of knowing at this point
  2204. // which display the user wants to update, views_ui_standard_submit will
  2205. // take care of running the regular submit handler as appropriate.
  2206. '#submit' => array('views_ui_standard_submit'),
  2207. );
  2208. // Form API button click detection requires the button's #value to be the
  2209. // same between the form build of the initial page request, and the initial
  2210. // form build of the request processing the form submission. Ideally, the
  2211. // button's #value shouldn't change until the form rebuild step. However,
  2212. // views_ui_ajax_form() implements a different multistep form workflow than
  2213. // the Form API does, and adjusts $view->stack prior to form processing, so
  2214. // we compensate by extending button click detection code to support any of
  2215. // the possible button labels.
  2216. if (isset($names)) {
  2217. $form['buttons']['submit']['#values'] = $names;
  2218. $form['buttons']['submit']['#process'] = array_merge(array('views_ui_form_button_was_clicked'), element_info_property($form['buttons']['submit']['#type'], '#process', array()));
  2219. }
  2220. // If a validation handler exists for the form, assign it to this button.
  2221. if (function_exists($form_id . '_validate')) {
  2222. $form['buttons']['submit']['#validate'][] = $form_id . '_validate';
  2223. }
  2224. }
  2225. // Create a "Cancel" button. For purely informational forms, label it "OK".
  2226. $cancel_submit = function_exists($form_id . '_cancel') ? $form_id . '_cancel' : 'views_ui_standard_cancel';
  2227. $form['buttons']['cancel'] = array(
  2228. '#type' => 'submit',
  2229. '#value' => empty($form_state['ok_button']) ? t('Cancel') : t('Ok'),
  2230. '#submit' => array($cancel_submit),
  2231. '#validate' => array(),
  2232. );
  2233. // Some forms specify a third button, with a name and submit handler.
  2234. if ($third) {
  2235. if (empty($submit)) {
  2236. $submit = 'third';
  2237. }
  2238. $third_submit = function_exists($form_id . '_' . $submit) ? $form_id . '_' . $submit : 'views_ui_standard_cancel';
  2239. $form['buttons'][$submit] = array(
  2240. '#type' => 'submit',
  2241. '#value' => $third,
  2242. '#validate' => array(),
  2243. '#submit' => array($third_submit),
  2244. );
  2245. }
  2246. // Compatibility, to be removed later: // TODO: When is "later"?
  2247. // We used to set these items on the form, but now we want them on the $form_state:
  2248. if (isset($form['#title'])) {
  2249. $form_state['title'] = $form['#title'];
  2250. }
  2251. if (isset($form['#help_topic'])) {
  2252. $form_state['help_topic'] = $form['#help_topic'];
  2253. }
  2254. if (isset($form['#help_module'])) {
  2255. $form_state['help_module'] = $form['#help_module'];
  2256. }
  2257. if (isset($form['#url'])) {
  2258. $form_state['url'] = $form['#url'];
  2259. }
  2260. if (isset($form['#section'])) {
  2261. $form_state['#section'] = $form['#section'];
  2262. }
  2263. // Finally, we never want these cached -- our object cache does that for us.
  2264. $form['#no_cache'] = TRUE;
  2265. // If this isn't an ajaxy form, then we want to set the title.
  2266. if (!empty($form['#title'])) {
  2267. drupal_set_title($form['#title']);
  2268. }
  2269. }
  2270. /**
  2271. * Basic submit handler applicable to all 'standard' forms.
  2272. *
  2273. * This submit handler determines whether the user wants the submitted changes
  2274. * to apply to the default display or to the current display, and dispatches
  2275. * control appropriately.
  2276. */
  2277. function views_ui_standard_submit($form, &$form_state) {
  2278. // Determine whether the values the user entered are intended to apply to
  2279. // the current display or the default display.
  2280. list($was_defaulted, $is_defaulted, $revert) = views_ui_standard_override_values($form, $form_state);
  2281. // Mark the changed section of the view as changed.
  2282. // TODO: Document why we are doing this, and see if we still need it.
  2283. if (!empty($form['#section'])) {
  2284. $form_state['view']->changed_sections[$form['#section']] = TRUE;
  2285. }
  2286. // Based on the user's choice in the display dropdown, determine which display
  2287. // these changes apply to.
  2288. if ($revert) {
  2289. // If it's revert just change the override and return.
  2290. $display = &$form_state['view']->display[$form_state['display_id']];
  2291. $display->handler->options_override($form, $form_state);
  2292. // Don't execute the normal submit handling but still store the changed view into cache.
  2293. views_ui_cache_set($form_state['view']);
  2294. return;
  2295. }
  2296. elseif ($was_defaulted === $is_defaulted) {
  2297. // We're not changing which display these form values apply to.
  2298. // Run the regular submit handler for this form.
  2299. }
  2300. elseif ($was_defaulted && !$is_defaulted) {
  2301. // We were using the default display's values, but we're now overriding
  2302. // the default display and saving values specific to this display.
  2303. $display = &$form_state['view']->display[$form_state['display_id']];
  2304. // options_override toggles the override of this section.
  2305. $display->handler->options_override($form, $form_state);
  2306. $display->handler->options_submit($form, $form_state);
  2307. }
  2308. elseif (!$was_defaulted && $is_defaulted) {
  2309. // We used to have an override for this display, but the user now wants
  2310. // to go back to the default display.
  2311. // Overwrite the default display with the current form values, and make
  2312. // the current display use the new default values.
  2313. $display = &$form_state['view']->display[$form_state['display_id']];
  2314. // options_override toggles the override of this section.
  2315. $display->handler->options_override($form, $form_state);
  2316. $display->handler->options_submit($form, $form_state);
  2317. }
  2318. $submit_handler = $form['#form_id'] . '_submit';
  2319. if (function_exists($submit_handler)) {
  2320. $submit_handler($form, $form_state);
  2321. }
  2322. }
  2323. /**
  2324. * Return the was_defaulted, is_defaulted and revert state of a form.
  2325. */
  2326. function views_ui_standard_override_values($form, $form_state) {
  2327. // Make sure the dropdown exists in the first place.
  2328. if (isset($form_state['values']['override']['dropdown'])) {
  2329. // #default_value is used to determine whether it was the default value or not.
  2330. // So the available options are: $display, 'default' and 'default_revert', not 'defaults'.
  2331. $was_defaulted = ($form['override']['dropdown']['#default_value'] === 'defaults');
  2332. $is_defaulted = ($form_state['values']['override']['dropdown'] === 'default');
  2333. $revert = ($form_state['values']['override']['dropdown'] === 'default_revert');
  2334. if ($was_defaulted !== $is_defaulted && isset($form['#section'])) {
  2335. // We're changing which display these values apply to.
  2336. // Update the #section so it knows what to mark changed.
  2337. $form['#section'] = str_replace('default-', $form_state['display_id'] . '-', $form['#section']);
  2338. }
  2339. }
  2340. else {
  2341. // The user didn't get the dropdown for overriding the default display.
  2342. $was_defaulted = FALSE;
  2343. $is_defaulted = FALSE;
  2344. $revert = FALSE;
  2345. }
  2346. return array($was_defaulted, $is_defaulted, $revert);
  2347. }
  2348. /**
  2349. * Submit handler for cancel button
  2350. */
  2351. function views_ui_standard_cancel($form, &$form_state) {
  2352. if (!empty($form_state['view']->changed) && isset($form_state['view']->form_cache)) {
  2353. unset($form_state['view']->form_cache);
  2354. views_ui_cache_set($form_state['view']);
  2355. }
  2356. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit';
  2357. }
  2358. /**
  2359. * Add a <select> dropdown for a given section, allowing the user to
  2360. * change whether this info is stored on the default display or on
  2361. * the current display.
  2362. */
  2363. function views_ui_standard_display_dropdown(&$form, &$form_state, $section) {
  2364. $view = &$form_state['view'];
  2365. $display_id = $form_state['display_id'];
  2366. $displays = $view->display;
  2367. $current_display = $view->display[$display_id];
  2368. // Add the "2 of 3" progress indicator.
  2369. // @TODO: Move this to a separate function if it's needed on any forms that
  2370. // don't have the display dropdown.
  2371. if ($form_progress = views_ui_get_form_progress($view)) {
  2372. $form['progress']['#markup'] = '<div id="views-progress-indicator">' . t('@current of @total', array('@current' => $form_progress['current'], '@total' => $form_progress['total'])) . '</div>';
  2373. $form['progress']['#weight'] = -1001;
  2374. }
  2375. if ($current_display->handler->is_default_display()) {
  2376. return;
  2377. }
  2378. // Determine whether any other displays have overrides for this section.
  2379. $section_overrides = FALSE;
  2380. $section_defaulted = $current_display->handler->is_defaulted($section);
  2381. foreach ($displays as $id => $display) {
  2382. if ($id === 'default' || $id === $display_id) {
  2383. continue;
  2384. }
  2385. if ($display->handler && !$display->handler->is_defaulted($section)) {
  2386. $section_overrides = TRUE;
  2387. }
  2388. }
  2389. $display_dropdown['default'] = ($section_overrides ? t('All displays (except overridden)') : t('All displays'));
  2390. $display_dropdown[$display_id] = t('This @display_type (override)', array('@display_type' => $current_display->display_plugin));
  2391. // Only display the revert option if we are in a overridden section.
  2392. if (!$section_defaulted) {
  2393. $display_dropdown['default_revert'] = t('Revert to default');
  2394. }
  2395. $form['override'] = array(
  2396. '#prefix' => '<div class="views-override clearfix container-inline">',
  2397. '#suffix' => '</div>',
  2398. '#weight' => -1000,
  2399. '#tree' => TRUE,
  2400. );
  2401. $form['override']['dropdown'] = array(
  2402. '#type' => 'select',
  2403. '#title' => t('For'), // @TODO: Translators may need more context than this.
  2404. '#options' => $display_dropdown,
  2405. );
  2406. if ($current_display->handler->is_defaulted($section)) {
  2407. $form['override']['dropdown']['#default_value'] = 'defaults';
  2408. }
  2409. else {
  2410. $form['override']['dropdown']['#default_value'] = $display_id;
  2411. }
  2412. }
  2413. /**
  2414. * Get the user's current progress through the form stack.
  2415. *
  2416. * @param $view
  2417. * The current view.
  2418. *
  2419. * @return
  2420. * FALSE if the user is not currently in a multiple-form stack. Otherwise,
  2421. * an associative array with the following keys:
  2422. * - current: The number of the current form on the stack.
  2423. * - total: The total number of forms originally on the stack.
  2424. */
  2425. function views_ui_get_form_progress($view) {
  2426. $progress = FALSE;
  2427. if (!empty($view->stack)) {
  2428. $stack = $view->stack;
  2429. // The forms on the stack have integer keys that don't change as the forms
  2430. // are completed, so we can see which ones are still left.
  2431. $keys = array_keys($view->stack);
  2432. // Add 1 to the array keys for the benefit of humans, who start counting
  2433. // from 1 and not 0.
  2434. $current = reset($keys) + 1;
  2435. $total = end($keys) + 1;
  2436. if ($total > 1) {
  2437. $progress = array();
  2438. $progress['current'] = $current;
  2439. $progress['total'] = $total;
  2440. }
  2441. }
  2442. return $progress;
  2443. }
  2444. // --------------------------------------------------------------------------
  2445. // Various subforms for editing the pieces of a view.
  2446. function views_ui_ajax_forms($key = NULL) {
  2447. $forms = array(
  2448. 'display' => array(
  2449. 'form_id' => 'views_ui_edit_display_form',
  2450. 'args' => array('section'),
  2451. ),
  2452. 'remove-display' => array(
  2453. 'form_id' => 'views_ui_remove_display_form',
  2454. 'args' => array(),
  2455. ),
  2456. 'config-type' => array(
  2457. 'form_id' => 'views_ui_config_type_form',
  2458. 'args' => array('type'),
  2459. ),
  2460. 'rearrange' => array(
  2461. 'form_id' => 'views_ui_rearrange_form',
  2462. 'args' => array('type'),
  2463. ),
  2464. 'rearrange-filter' => array(
  2465. 'form_id' => 'views_ui_rearrange_filter_form',
  2466. 'args' => array('type'),
  2467. ),
  2468. 'reorder-displays' => array(
  2469. 'form_id' => 'views_ui_reorder_displays_form',
  2470. 'args' => array(),
  2471. ),
  2472. 'add-item' => array(
  2473. 'form_id' => 'views_ui_add_item_form',
  2474. 'args' => array('type'),
  2475. ),
  2476. 'config-item' => array(
  2477. 'form_id' => 'views_ui_config_item_form',
  2478. 'args' => array('type', 'id'),
  2479. ),
  2480. 'config-item-extra' => array(
  2481. 'form_id' => 'views_ui_config_item_extra_form',
  2482. 'args' => array('type', 'id'),
  2483. ),
  2484. 'config-item-group' => array(
  2485. 'form_id' => 'views_ui_config_item_group_form',
  2486. 'args' => array('type', 'id'),
  2487. ),
  2488. 'config-style' => array(
  2489. 'form_id' => 'views_ui_config_style_form',
  2490. 'args' => array('type', 'id'),
  2491. ),
  2492. 'edit-details' => array(
  2493. 'form_id' => 'views_ui_edit_details_form',
  2494. 'args' => array(),
  2495. ),
  2496. 'analyze' => array(
  2497. 'form_id' => 'views_ui_analyze_view_form',
  2498. 'args' => array(),
  2499. ),
  2500. );
  2501. if ($key) {
  2502. return !empty($forms[$key]) ? $forms[$key] : NULL;
  2503. }
  2504. return $forms;
  2505. }
  2506. /**
  2507. * Build a form identifier that we can use to see if one form
  2508. * is the same as another. Since the arguments differ slightly
  2509. * we do a lot of spiffy concatenation here.
  2510. */
  2511. function views_ui_build_identifier($key, $view, $display_id, $args) {
  2512. $form = views_ui_ajax_forms($key);
  2513. // Automatically remove the single-form cache if it exists and
  2514. // does not match the key.
  2515. $identifier = implode('-', array($key, $view->name, $display_id));
  2516. foreach ($form['args'] as $id) {
  2517. $arg = (!empty($args)) ? array_shift($args) : NULL;
  2518. $identifier .= '-' . $arg;
  2519. }
  2520. return $identifier;
  2521. }
  2522. /**
  2523. * Build up a $form_state object suitable for use with drupal_build_form
  2524. * based on known information about a form.
  2525. */
  2526. function views_ui_build_form_state($js, $key, &$view, $display_id, $args) {
  2527. $form = views_ui_ajax_forms($key);
  2528. // Build up form state
  2529. $form_state = array(
  2530. 'form_key' => $key,
  2531. 'form_id' => $form['form_id'],
  2532. 'view' => &$view,
  2533. 'ajax' => $js,
  2534. 'display_id' => $display_id,
  2535. 'no_redirect' => TRUE,
  2536. );
  2537. foreach ($form['args'] as $id) {
  2538. $form_state[$id] = (!empty($args)) ? array_shift($args) : NULL;
  2539. }
  2540. return $form_state;
  2541. }
  2542. /**
  2543. * Create the URL for one of our standard AJAX forms based upon known
  2544. * information about the form.
  2545. */
  2546. function views_ui_build_form_url($form_state) {
  2547. $form = views_ui_ajax_forms($form_state['form_key']);
  2548. $ajax = empty($form_state['ajax']) ? 'nojs' : 'ajax';
  2549. $name = $form_state['view']->name;
  2550. $url = "admin/structure/views/$ajax/$form_state[form_key]/$name/$form_state[display_id]";
  2551. foreach ($form['args'] as $arg) {
  2552. $url .= '/' . $form_state[$arg];
  2553. }
  2554. return $url;
  2555. }
  2556. /**
  2557. * Add another form to the stack; clicking 'apply' will go to this form
  2558. * rather than closing the ajax popup.
  2559. */
  2560. function views_ui_add_form_to_stack($key, &$view, $display_id, $args, $top = FALSE, $rebuild_keys = FALSE) {
  2561. if (empty($view->stack)) {
  2562. $view->stack = array();
  2563. }
  2564. $stack = array(views_ui_build_identifier($key, $view, $display_id, $args), $key, &$view, $display_id, $args);
  2565. // If we're being asked to add this form to the bottom of the stack, no
  2566. // special logic is required. Our work is equally easy if we were asked to add
  2567. // to the top of the stack, but there's nothing in it yet.
  2568. if (!$top || empty($view->stack)) {
  2569. $view->stack[] = $stack;
  2570. }
  2571. // If we're adding to the top of an existing stack, we have to maintain the
  2572. // existing integer keys, so they can be used for the "2 of 3" progress
  2573. // indicator (which will now read "2 of 4").
  2574. else {
  2575. $keys = array_keys($view->stack);
  2576. $first = current($keys);
  2577. $last = end($keys);
  2578. for ($i = $last; $i >= $first; $i--) {
  2579. if (!isset($view->stack[$i])) {
  2580. continue;
  2581. }
  2582. // Move form number $i to the next position in the stack.
  2583. $view->stack[$i + 1] = $view->stack[$i];
  2584. unset($view->stack[$i]);
  2585. }
  2586. // Now that the previously $first slot is free, move the new form into it.
  2587. $view->stack[$first] = $stack;
  2588. ksort($view->stack);
  2589. // Start the keys from 0 again, if requested.
  2590. if ($rebuild_keys) {
  2591. $view->stack = array_values($view->stack);
  2592. }
  2593. }
  2594. }
  2595. /**
  2596. * Generic entry point to handle forms.
  2597. *
  2598. * We do this for consistency and to make it easy to chain forms
  2599. * together.
  2600. */
  2601. function views_ui_ajax_form($js, $key, $view, $display_id = '') {
  2602. // Reset the cache of IDs. Drupal rather aggressively prevents id duplication
  2603. // but this causes it to remember IDs that are no longer even being used.
  2604. if (isset($_POST['ajax_html_ids'])) {
  2605. unset($_POST['ajax_html_ids']);
  2606. }
  2607. $form = views_ui_ajax_forms($key);
  2608. if (empty($form)) {
  2609. return MENU_NOT_FOUND;
  2610. }
  2611. views_include('ajax');
  2612. $args = func_get_args();
  2613. // Remove the known args
  2614. array_splice($args, 0, 4);
  2615. $form_state = views_ui_build_form_state($js, $key, $view, $display_id, $args);
  2616. // check to see if this is the top form of the stack. If it is, pop
  2617. // it off; if it isn't, the user clicked somewhere else and the stack is
  2618. // now irrelevant.
  2619. if (!empty($view->stack)) {
  2620. $identifier = views_ui_build_identifier($key, $view, $display_id, $args);
  2621. // Retrieve the first form from the stack without changing the integer keys,
  2622. // as they're being used for the "2 of 3" progress indicator.
  2623. reset($view->stack);
  2624. list($key, $top) = each($view->stack);
  2625. unset($view->stack[$key]);
  2626. if (array_shift($top) != $identifier) {
  2627. $view->stack = array();
  2628. }
  2629. }
  2630. // Automatically remove the form cache if it is set and the key does
  2631. // not match. This way navigating away from the form without hitting
  2632. // update will work.
  2633. if (isset($view->form_cache) && $view->form_cache['key'] != $key) {
  2634. unset($view->form_cache);
  2635. }
  2636. // With the below logic, we may end up rendering a form twice (or two forms
  2637. // each sharing the same element ids), potentially resulting in
  2638. // drupal_add_js() being called twice to add the same setting. drupal_get_js()
  2639. // is ok with that, but until ajax_render() is (http://drupal.org/node/208611),
  2640. // reset the drupal_add_js() static before rendering the second time.
  2641. $drupal_add_js_original = drupal_add_js();
  2642. $drupal_add_js = &drupal_static('drupal_add_js');
  2643. $output = views_ajax_form_wrapper($form_state['form_id'], $form_state);
  2644. if ($form_state['submitted'] && empty($form_state['rerender'])) {
  2645. // Sometimes we need to re-generate the form for multi-step type operations.
  2646. $object = NULL;
  2647. if (!empty($view->stack)) {
  2648. $drupal_add_js = $drupal_add_js_original;
  2649. $stack = $view->stack;
  2650. $top = array_shift($stack);
  2651. $top[0] = $js;
  2652. $form_state = call_user_func_array('views_ui_build_form_state', $top);
  2653. $form_state['input'] = array();
  2654. $form_state['url'] = url(views_ui_build_form_url($form_state));
  2655. if (!$js) {
  2656. return drupal_goto(views_ui_build_form_url($form_state));
  2657. }
  2658. $output = views_ajax_form_wrapper($form_state['form_id'], $form_state);
  2659. }
  2660. elseif (!$js) {
  2661. // if nothing on the stack, non-js forms just go back to the main view editor.
  2662. return drupal_goto("admin/structure/views/view/$view->name/edit");
  2663. }
  2664. else {
  2665. $output = array();
  2666. $output[] = views_ajax_command_dismiss_form();
  2667. $output[] = views_ajax_command_show_buttons();
  2668. $output[] = views_ajax_command_trigger_preview();
  2669. if (!empty($form_state['#page_title'])) {
  2670. $output[] = views_ajax_command_replace_title($form_state['#page_title']);
  2671. }
  2672. }
  2673. // If this form was for view-wide changes, there's no need to regenerate
  2674. // the display section of the form.
  2675. if ($display_id !== '') {
  2676. views_ui_regenerate_tab($view, $output, $display_id);
  2677. }
  2678. }
  2679. return $js ? array('#type' => 'ajax', '#commands' => $output) : $output;
  2680. }
  2681. /**
  2682. * Submit handler to add a restore a removed display to a view.
  2683. */
  2684. function views_ui_remove_display_form_restore($form, &$form_state) {
  2685. // Create the new display
  2686. $id = $form_state['display_id'];
  2687. $form_state['view']->display[$id]->deleted = FALSE;
  2688. // Store in cache
  2689. views_ui_cache_set($form_state['view']);
  2690. }
  2691. /**
  2692. * Form constructor callback to display analysis information on a view
  2693. */
  2694. function views_ui_analyze_view_form($form, &$form_state) {
  2695. $view = &$form_state['view'];
  2696. $form['#title'] = t('View analysis');
  2697. $form['#section'] = 'analyze';
  2698. views_include('analyze');
  2699. $messages = views_analyze_view($view);
  2700. $form['analysis'] = array(
  2701. '#prefix' => '<div class="form-item">',
  2702. '#suffix' => '</div>',
  2703. '#markup' => views_analyze_format_result($view, $messages),
  2704. );
  2705. // Inform the standard button function that we want an OK button.
  2706. $form_state['ok_button'] = TRUE;
  2707. views_ui_standard_form_buttons($form, $form_state, 'views_ui_analyze_view_form');
  2708. return $form;
  2709. }
  2710. /**
  2711. * Submit handler for views_ui_analyze_view_form
  2712. */
  2713. function views_ui_analyze_view_form_submit($form, &$form_state) {
  2714. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit';
  2715. }
  2716. /**
  2717. * Form constructor callback to reorder displays on a view
  2718. */
  2719. function views_ui_reorder_displays_form($form, &$form_state) {
  2720. $view = &$form_state['view'];
  2721. $display_id = $form_state['display_id'];
  2722. $form['view'] = array('#type' => 'value', '#value' => $view);
  2723. $form['#tree'] = TRUE;
  2724. $last_display = end($view->display);
  2725. foreach ($view->display as $display) {
  2726. $form[$display->id] = array(
  2727. 'title' => array('#markup' => check_plain($display->display_title)),
  2728. 'weight' => array(
  2729. '#type' => 'weight',
  2730. '#value' => $display->position,
  2731. '#delta' => $last_display->position,
  2732. '#title' => t('Weight for @display', array('@display' => $display->display_title)),
  2733. '#title_display' => 'invisible',
  2734. ),
  2735. '#tree' => TRUE,
  2736. '#display' => $display,
  2737. 'removed' => array(
  2738. '#type' => 'checkbox',
  2739. '#id' => 'display-removed-' . $display->id,
  2740. '#attributes' => array('class' => array('views-remove-checkbox')),
  2741. '#default_value' => isset($display->deleted),
  2742. ),
  2743. );
  2744. if (isset($display->deleted) && $display->deleted) {
  2745. $form[$display->id]['deleted'] = array('#type' => 'value', '#value' => TRUE);
  2746. }
  2747. if ($display->id === 'default') {
  2748. unset($form[$display->id]['weight']);
  2749. unset($form[$display->id]['removed']);
  2750. }
  2751. }
  2752. $form['#title'] = t('Displays Reorder');
  2753. $form['#section'] = 'reorder';
  2754. // Add javascript settings that will be added via $.extend for tabledragging
  2755. $form['#js']['tableDrag']['reorder-displays']['weight'][0] = array(
  2756. 'target' => 'weight',
  2757. 'source' => NULL,
  2758. 'relationship' => 'sibling',
  2759. 'action' => 'order',
  2760. 'hidden' => TRUE,
  2761. 'limit' => 0,
  2762. );
  2763. $form['#action'] = url('admin/structure/views/nojs/reorder-displays/' . $view->name . '/' . $display_id);
  2764. views_ui_standard_form_buttons($form, $form_state, 'views_ui_reorder_displays_form');
  2765. return $form;
  2766. }
  2767. /**
  2768. * Display position sorting function
  2769. */
  2770. function _views_position_sort($display1, $display2) {
  2771. if ($display1->position != $display2->position) {
  2772. return $display1->position < $display2->position ? -1 : 1;
  2773. }
  2774. return 0;
  2775. }
  2776. /**
  2777. * Submit handler for rearranging display form
  2778. */
  2779. function views_ui_reorder_displays_form_submit($form, &$form_state) {
  2780. foreach($form_state['input'] as $display => $info) {
  2781. // add each value that is a field with a weight to our list, but only if
  2782. // it has had its 'removed' checkbox checked.
  2783. if (is_array($info) && isset($info['weight']) && empty($info['removed'])) {
  2784. $order[$display] = $info['weight'];
  2785. }
  2786. }
  2787. // Sort the order array
  2788. asort($order);
  2789. // Fixing up positions
  2790. $position = 2;
  2791. foreach(array_keys($order) as $display) {
  2792. $order[$display] = $position++;
  2793. }
  2794. // Setting up position and removing deleted displays
  2795. $displays = $form_state['view']->display;
  2796. foreach($displays as $display_id => $display) {
  2797. // Don't touch the default !!!
  2798. if ($display_id === 'default') {
  2799. continue;
  2800. }
  2801. if (isset($order[$display_id])) {
  2802. $form_state['view']->display[$display_id]->position = $order[$display_id];
  2803. }
  2804. else {
  2805. $form_state['view']->display[$display_id]->deleted = TRUE;
  2806. }
  2807. }
  2808. // Sorting back the display array as the position is not enough
  2809. uasort($form_state['view']->display, '_views_position_sort');
  2810. // Store in cache
  2811. views_ui_cache_set($form_state['view']);
  2812. $form_state['redirect'] = array('admin/structure/views/view/' . $form_state['view']->name . '/edit', array('fragment' => 'views-tab-default'));
  2813. }
  2814. /**
  2815. * Turn the reorder form into a proper table
  2816. */
  2817. function theme_views_ui_reorder_displays_form($vars) {
  2818. $form = $vars['form'];
  2819. $rows = array();
  2820. foreach (element_children($form) as $key) {
  2821. if (isset($form[$key]['#display'])) {
  2822. $display = &$form[$key];
  2823. $row = array();
  2824. $row[] = drupal_render($display['title']);
  2825. $form[$key]['weight']['#attributes']['class'] = array('weight');
  2826. $row[] = drupal_render($form[$key]['weight']);
  2827. if (isset($display['removed'])) {
  2828. $row[] = drupal_render($form[$key]['removed']) .
  2829. l('<span>' . t('Remove') . '</span>',
  2830. 'javascript:void()',
  2831. array(
  2832. 'attributes' => array(
  2833. 'id' => 'display-remove-link-' . $key,
  2834. 'class' => array('views-button-remove display-remove-link'),
  2835. 'alt' => t('Remove this display'),
  2836. 'title' => t('Remove this display')),
  2837. 'html' => TRUE));
  2838. }
  2839. else {
  2840. $row[] = '';
  2841. }
  2842. $class = array();
  2843. $styles = array();
  2844. if (isset($form[$key]['weight']['#type'])) {
  2845. $class[] = 'draggable';
  2846. }
  2847. if (isset($form[$key]['deleted']['#value']) && $form[$key]['deleted']['#value']) {
  2848. $styles[] = 'display: none;';
  2849. }
  2850. $rows[] = array('data' => $row, 'class' => $class, 'id' => 'display-row-' . $key, 'style' => $styles);
  2851. }
  2852. }
  2853. $header = array(t('Display'), t('Weight'), t('Remove'));
  2854. $output = '';
  2855. drupal_add_tabledrag('reorder-displays', 'order', 'sibling', 'weight');
  2856. $output = drupal_render($form['override']);
  2857. $output .= '<div class="scroll">';
  2858. $output .= theme('table',
  2859. array('header' => $header,
  2860. 'rows' => $rows,
  2861. 'attributes' => array('id' => 'reorder-displays'),
  2862. ));
  2863. $output .= '</div>';
  2864. $output .= drupal_render_children($form);
  2865. return $output;
  2866. }
  2867. /**
  2868. * Form builder to edit details of a view.
  2869. */
  2870. function views_ui_edit_details_form($form, &$form_state) {
  2871. $view = &$form_state['view'];
  2872. $form['#title'] = t('View name and description');
  2873. $form['#section'] = 'details';
  2874. $form['details'] = array(
  2875. '#theme_wrappers' => array('container'),
  2876. '#attributes' => array('class' => array('scroll')),
  2877. );
  2878. $form['details']['human_name'] = array(
  2879. '#type' => 'textfield',
  2880. '#title' => t('Human-readable name'),
  2881. '#description' => t('A descriptive human-readable name for this view. Spaces are allowed'),
  2882. '#default_value' => $view->get_human_name(),
  2883. );
  2884. $form['details']['tag'] = array(
  2885. '#type' => 'textfield',
  2886. '#title' => t('View tag'),
  2887. '#description' => t('Optionally, enter a comma delimited list of tags for this view to use in filtering and sorting views on the administrative page.'),
  2888. '#default_value' => $view->tag,
  2889. '#autocomplete_path' => 'admin/views/ajax/autocomplete/tag',
  2890. );
  2891. $form['details']['description'] = array(
  2892. '#type' => 'textfield',
  2893. '#title' => t('View description'),
  2894. '#description' => t('This description will appear on the Views administrative UI to tell you what the view is about.'),
  2895. '#default_value' => $view->description,
  2896. );
  2897. views_ui_standard_form_buttons($form, $form_state, 'views_ui_edit_details_form');
  2898. return $form;
  2899. }
  2900. /**
  2901. * Submit handler for views_ui_edit_details_form.
  2902. */
  2903. function views_ui_edit_details_form_submit($form, &$form_state) {
  2904. $view = $form_state['view'];
  2905. foreach ($form_state['values'] as $key => $value) {
  2906. // Only save values onto the view if they're actual view properties
  2907. // (as opposed to 'op' or 'form_build_id').
  2908. if (isset($form['details'][$key])) {
  2909. $view->$key = $value;
  2910. }
  2911. }
  2912. $form_state['#page_title'] = views_ui_edit_page_title($view);
  2913. views_ui_cache_set($view);
  2914. }
  2915. /**
  2916. * Form constructor callback to edit display of a view
  2917. */
  2918. function views_ui_edit_display_form($form, &$form_state) {
  2919. $view = &$form_state['view'];
  2920. $display_id = $form_state['display_id'];
  2921. $section = $form_state['section'];
  2922. if (!$view->set_display($display_id)) {
  2923. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  2924. }
  2925. $display = &$view->display[$display_id];
  2926. // Get form from the handler.
  2927. $form['options'] = array(
  2928. '#theme_wrappers' => array('container'),
  2929. '#attributes' => array('class' => array('scroll')),
  2930. );
  2931. $display->handler->options_form($form['options'], $form_state);
  2932. // The handler options form sets $form['#title'], which we need on the entire
  2933. // $form instead of just the ['options'] section.
  2934. $form['#title'] = $form['options']['#title'];
  2935. unset($form['options']['#title']);
  2936. // Move the override dropdown out of the scrollable section of the form.
  2937. if (isset($form['options']['override'])) {
  2938. $form['override'] = $form['options']['override'];
  2939. unset($form['options']['override']);
  2940. }
  2941. $name = NULL;
  2942. if (isset($form_state['update_name'])) {
  2943. $name = $form_state['update_name'];
  2944. }
  2945. views_ui_standard_form_buttons($form, $form_state, 'views_ui_edit_display_form', $name);
  2946. return $form;
  2947. }
  2948. /**
  2949. * Validate handler for views_ui_edit_display_form
  2950. */
  2951. function views_ui_edit_display_form_validate($form, &$form_state) {
  2952. $display = &$form_state['view']->display[$form_state['display_id']];
  2953. $display->handler->options_validate($form['options'], $form_state);
  2954. if (form_get_errors()) {
  2955. $form_state['rerender'] = TRUE;
  2956. }
  2957. }
  2958. /**
  2959. * Submit handler for views_ui_edit_display_form
  2960. */
  2961. function views_ui_edit_display_form_submit($form, &$form_state) {
  2962. $display = &$form_state['view']->display[$form_state['display_id']];
  2963. $display->handler->options_submit($form, $form_state);
  2964. views_ui_cache_set($form_state['view']);
  2965. }
  2966. /**
  2967. * Override handler for views_ui_edit_display_form
  2968. *
  2969. * @TODO: Not currently used. Remove unless we implement an override toggle.
  2970. */
  2971. function views_ui_edit_display_form_override($form, &$form_state) {
  2972. $display = &$form_state['view']->display[$form_state['display_id']];
  2973. $display->handler->options_override($form, $form_state);
  2974. views_ui_cache_set($form_state['view']);
  2975. $form_state['rerender'] = TRUE;
  2976. $form_state['rebuild'] = TRUE;
  2977. }
  2978. /**
  2979. * Form to config items in the views UI.
  2980. */
  2981. function views_ui_config_type_form($form, &$form_state) {
  2982. $view = &$form_state['view'];
  2983. $display_id = $form_state['display_id'];
  2984. $type = $form_state['type'];
  2985. $types = views_object_types();
  2986. if (!$view->set_display($display_id)) {
  2987. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  2988. }
  2989. $display = &$view->display[$display_id];
  2990. $form['#title'] = t('Configure @type', array('@type' => $types[$type]['ltitle']));
  2991. $form['#section'] = $display_id . 'config-item';
  2992. if ($display->handler->defaultable_sections($types[$type]['plural'])) {
  2993. $form_state['section'] = $types[$type]['plural'];
  2994. views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
  2995. }
  2996. if (!empty($types[$type]['options']) && function_exists($types[$type]['options'])) {
  2997. $options = $type . '_options';
  2998. $form[$options] = array('#tree' => TRUE);
  2999. $types[$type]['options']($form, $form_state);
  3000. }
  3001. $name = NULL;
  3002. if (isset($form_state['update_name'])) {
  3003. $name = $form_state['update_name'];
  3004. }
  3005. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_type_form', $name);
  3006. return $form;
  3007. }
  3008. /**
  3009. * Submit handler for type configuration form
  3010. */
  3011. function views_ui_config_type_form_submit($form, &$form_state) {
  3012. $types = views_object_types();
  3013. $display = &$form_state['view']->display[$form_state['display_id']];
  3014. // Store in cache
  3015. views_ui_cache_set($form_state['view']);
  3016. }
  3017. /**
  3018. * Form to rearrange items in the views UI.
  3019. */
  3020. function views_ui_rearrange_form($form, &$form_state) {
  3021. $view = &$form_state['view'];
  3022. $display_id = $form_state['display_id'];
  3023. $type = $form_state['type'];
  3024. $types = views_object_types();
  3025. if (!$view->set_display($display_id)) {
  3026. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  3027. }
  3028. $display = &$view->display[$display_id];
  3029. $form['#title'] = t('Rearrange @type', array('@type' => $types[$type]['ltitle']));
  3030. $form['#section'] = $display_id . 'rearrange-item';
  3031. if ($display->handler->defaultable_sections($types[$type]['plural'])) {
  3032. $form_state['section'] = $types[$type]['plural'];
  3033. views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
  3034. }
  3035. $count = 0;
  3036. // Get relationship labels
  3037. $relationships = array();
  3038. foreach ($display->handler->get_handlers('relationship') as $id => $handler) {
  3039. $relationships[$id] = $handler->label();
  3040. $handlers = $display->handler->get_option('relationships');
  3041. if ($handlers) {
  3042. foreach ($handlers as $id => $info) {
  3043. $handler = $display->handler->get_handler('relationship', $id);
  3044. $relationships[$id] = $handler->label();
  3045. }
  3046. }
  3047. }
  3048. // Filters can now be grouped so we do a little bit extra:
  3049. $groups = array();
  3050. $grouping = FALSE;
  3051. if ($type == 'filter') {
  3052. $group_info = $view->display_handler->get_option('filter_groups');
  3053. if (!empty($group_info['groups']) && count($group_info['groups']) > 1) {
  3054. $grouping = TRUE;
  3055. $groups = array(0 => array());
  3056. }
  3057. }
  3058. foreach ($display->handler->get_option($types[$type]['plural']) as $id => $field) {
  3059. $form['fields'][$id] = array('#tree' => TRUE);
  3060. $form['fields'][$id]['weight'] = array(
  3061. '#type' => 'textfield',
  3062. '#default_value' => ++$count,
  3063. );
  3064. $handler = $display->handler->get_handler($type, $id);
  3065. if ($handler) {
  3066. $name = $handler->ui_name() . ' ' . $handler->admin_summary();
  3067. if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
  3068. $name = '(' . $relationships[$field['relationship']] . ') ' . $name;
  3069. }
  3070. $form['fields'][$id]['name'] = array(
  3071. '#markup' => $name,
  3072. );
  3073. }
  3074. else {
  3075. $form['fields'][$id]['name'] = array('#markup' => t('Broken field @id', array('@id' => $id)));
  3076. }
  3077. $form['fields'][$id]['removed'] = array(
  3078. '#type' => 'checkbox',
  3079. '#id' => 'views-removed-' . $id,
  3080. '#attributes' => array('class' => array('views-remove-checkbox')),
  3081. '#default_value' => 0,
  3082. );
  3083. }
  3084. // Add javascript settings that will be added via $.extend for tabledragging
  3085. $form['#js']['tableDrag']['arrange']['weight'][0] = array(
  3086. 'target' => 'weight',
  3087. 'source' => NULL,
  3088. 'relationship' => 'sibling',
  3089. 'action' => 'order',
  3090. 'hidden' => TRUE,
  3091. 'limit' => 0,
  3092. );
  3093. $name = NULL;
  3094. if (isset($form_state['update_name'])) {
  3095. $name = $form_state['update_name'];
  3096. }
  3097. views_ui_standard_form_buttons($form, $form_state, 'views_ui_rearrange_form');
  3098. return $form;
  3099. }
  3100. /**
  3101. * Turn the rearrange form into a proper table
  3102. */
  3103. function theme_views_ui_rearrange_form($variables) {
  3104. $form = $variables['form'];
  3105. $rows = array();
  3106. foreach (element_children($form['fields']) as $id) {
  3107. if (isset($form['fields'][$id]['name'])) {
  3108. $row = array();
  3109. $row[] = drupal_render($form['fields'][$id]['name']);
  3110. $form['fields'][$id]['weight']['#attributes']['class'] = array('weight');
  3111. $row[] = drupal_render($form['fields'][$id]['weight']);
  3112. $row[] = drupal_render($form['fields'][$id]['removed']) . l('<span>' . t('Remove') . '</span>', 'javascript:void()', array('attributes' => array('id' => 'views-remove-link-' . $id, 'class' => array('views-hidden', 'views-button-remove', 'views-remove-link'), 'alt' => t('Remove this item'), 'title' => t('Remove this item')), 'html' => TRUE));
  3113. $rows[] = array('data' => $row, 'class' => array('draggable'), 'id' => 'views-row-' . $id);
  3114. }
  3115. }
  3116. if (empty($rows)) {
  3117. $rows[] = array(array('data' => t('No fields available.'), 'colspan' => '2'));
  3118. }
  3119. $header = array('', t('Weight'), t('Remove'));
  3120. $output = drupal_render($form['override']);
  3121. $output .= '<div class="scroll">';
  3122. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'arrange')));
  3123. $output .= '</div>';
  3124. $output .= drupal_render_children($form);
  3125. drupal_add_tabledrag('arrange', 'order', 'sibling', 'weight');
  3126. return $output;
  3127. }
  3128. /**
  3129. * Theme the expose filter form.
  3130. */
  3131. function theme_views_ui_expose_filter_form($variables) {
  3132. $form = $variables['form'];
  3133. $more = drupal_render($form['more']);
  3134. $output = drupal_render($form['form_description']);
  3135. $output .= drupal_render($form['expose_button']);
  3136. $output .= drupal_render($form['group_button']);
  3137. if (isset($form['required'])) {
  3138. $output .= drupal_render($form['required']);
  3139. }
  3140. $output .= drupal_render($form['label']);
  3141. $output .= drupal_render($form['description']);
  3142. $output .= drupal_render($form['operator']);
  3143. $output .= drupal_render($form['value']);
  3144. if (isset($form['use_operator'])) {
  3145. $output .= '<div class="views-left-40">';
  3146. $output .= drupal_render($form['use_operator']);
  3147. $output .= '</div>';
  3148. }
  3149. // Only output the right column markup if there's a left column to begin with
  3150. if (!empty($form['operator']['#type'])) {
  3151. $output .= '<div class="views-right-60">';
  3152. $output .= drupal_render_children($form);
  3153. $output .= '</div>';
  3154. }
  3155. else {
  3156. $output .= drupal_render_children($form);
  3157. }
  3158. $output .= $more;
  3159. return $output;
  3160. }
  3161. /**
  3162. * Theme the build group filter form.
  3163. */
  3164. function theme_views_ui_build_group_filter_form($variables) {
  3165. $form = $variables['form'];
  3166. $more = drupal_render($form['more']);
  3167. $output = drupal_render($form['form_description']);
  3168. $output .= drupal_render($form['expose_button']);
  3169. $output .= drupal_render($form['group_button']);
  3170. if (isset($form['required'])) {
  3171. $output .= drupal_render($form['required']);
  3172. }
  3173. $output .= drupal_render($form['operator']);
  3174. $output .= drupal_render($form['value']);
  3175. $output .= '<div class="views-left-40">';
  3176. $output .= drupal_render($form['optional']);
  3177. $output .= drupal_render($form['remember']);
  3178. $output .= '</div>';
  3179. $output .= '<div class="views-right-60">';
  3180. $output .= drupal_render($form['widget']);
  3181. $output .= drupal_render($form['label']);
  3182. $output .= drupal_render($form['description']);
  3183. $output .= '</div>';
  3184. $header = array(
  3185. t('Default'),
  3186. t('Weight'),
  3187. t('Label'),
  3188. t('Operator'),
  3189. t('Value'),
  3190. t('Operations'),
  3191. );
  3192. $form['default_group'] = form_process_radios($form['default_group']);
  3193. $form['default_group_multiple'] = form_process_checkboxes($form['default_group_multiple']);
  3194. $form['default_group']['All']['#title'] = '';
  3195. drupal_render($form['default_group_multiple']['All']); // Don't render
  3196. $rows[] = array(
  3197. drupal_render($form['default_group']['All']),
  3198. '',
  3199. array(
  3200. 'data' => variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('&lt;Any&gt;') : t('- Any -'),
  3201. 'colspan' => 4,
  3202. 'class' => array('class' => 'any-default-radios-row'),
  3203. ),
  3204. );
  3205. foreach (element_children($form['group_items']) as $group_id) {
  3206. $form['group_items'][$group_id]['value']['#title'] = '';
  3207. $data = array(
  3208. 'default' => drupal_render($form['default_group'][$group_id]) . drupal_render($form['default_group_multiple'][$group_id]),
  3209. 'weight' => drupal_render($form['group_items'][$group_id]['weight']),
  3210. 'title' => drupal_render($form['group_items'][$group_id]['title']),
  3211. 'operator' => drupal_render($form['group_items'][$group_id]['operator']),
  3212. 'value' => drupal_render($form['group_items'][$group_id]['value']),
  3213. 'remove' => drupal_render($form['group_items'][$group_id]['remove']) . l('<span>' . t('Remove') . '</span>', 'javascript:void()', array('attributes' => array('id' => 'views-remove-link-' . $group_id, 'class' => array('views-hidden', 'views-button-remove', 'views-groups-remove-link', 'views-remove-link'), 'alt' => t('Remove this item'), 'title' => t('Remove this item')), 'html' => true)),
  3214. );
  3215. $rows[] = array('data' => $data, 'id' => 'views-row-' . $group_id, 'class' => array('draggable'));
  3216. }
  3217. $table = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('views-filter-groups'), 'id' => 'views-filter-groups'))) . drupal_render($form['add_group']);
  3218. drupal_add_tabledrag('views-filter-groups', 'order', 'sibling', 'weight');
  3219. $render_form = drupal_render_children($form);
  3220. return $output . $render_form . $table . $more;
  3221. }
  3222. /**
  3223. * Submit handler for rearranging form
  3224. */
  3225. function views_ui_rearrange_form_submit($form, &$form_state) {
  3226. $types = views_object_types();
  3227. $display = &$form_state['view']->display[$form_state['display_id']];
  3228. $old_fields = $display->handler->get_option($types[$form_state['type']]['plural']);
  3229. $new_fields = $order = array();
  3230. // Make an array with the weights
  3231. foreach ($form_state['values'] as $field => $info) {
  3232. // add each value that is a field with a weight to our list, but only if
  3233. // it has had its 'removed' checkbox checked.
  3234. if (is_array($info) && isset($info['weight']) && empty($info['removed'])) {
  3235. $order[$field] = $info['weight'];
  3236. }
  3237. }
  3238. // Sort the array
  3239. asort($order);
  3240. // Create a new list of fields in the new order.
  3241. foreach (array_keys($order) as $field) {
  3242. $new_fields[$field] = $old_fields[$field];
  3243. }
  3244. $display->handler->set_option($types[$form_state['type']]['plural'], $new_fields);
  3245. // Store in cache
  3246. views_ui_cache_set($form_state['view']);
  3247. }
  3248. /**
  3249. * Form to rearrange items in the views UI.
  3250. */
  3251. function views_ui_rearrange_filter_form($form, &$form_state) {
  3252. $view = &$form_state['view'];
  3253. $display_id = $form_state['display_id'];
  3254. $type = $form_state['type'];
  3255. $types = views_object_types();
  3256. if (!$view->set_display($display_id)) {
  3257. views_ajax_render(t('Invalid display id @display', array('@display' => $display_id)));
  3258. }
  3259. $display = &$view->display[$display_id];
  3260. $form['#title'] = check_plain($display->display_title) . ': ';
  3261. $form['#title'] .= t('Rearrange @type', array('@type' => $types[$type]['ltitle']));
  3262. $form['#section'] = $display_id . 'rearrange-item';
  3263. if ($display->handler->defaultable_sections($types[$type]['plural'])) {
  3264. $form_state['section'] = $types[$type]['plural'];
  3265. views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
  3266. }
  3267. if (!empty($view->form_cache)) {
  3268. $groups = $view->form_cache['groups'];
  3269. $handlers = $view->form_cache['handlers'];
  3270. }
  3271. else {
  3272. $groups = $display->handler->get_option('filter_groups');
  3273. $handlers = $display->handler->get_option($types[$type]['plural']);
  3274. }
  3275. $count = 0;
  3276. // Get relationship labels
  3277. $relationships = array();
  3278. foreach ($display->handler->get_handlers('relationship') as $id => $handler) {
  3279. $relationships[$id] = $handler->label();
  3280. }
  3281. $group_options = array();
  3282. /**
  3283. * Filter groups is an array that contains:
  3284. * array(
  3285. * 'operator' => 'and' || 'or',
  3286. * 'groups' => array(
  3287. * $group_id => 'and' || 'or',
  3288. * ),
  3289. * );
  3290. */
  3291. $grouping = count(array_keys($groups['groups'])) > 1;
  3292. $form['filter_groups']['#tree'] = TRUE;
  3293. $form['filter_groups']['operator'] = array(
  3294. '#type' => 'select',
  3295. '#options' => array (
  3296. 'AND' => t('And'),
  3297. 'OR' => t('Or'),
  3298. ),
  3299. '#default_value' => $groups['operator'],
  3300. '#attributes' => array(
  3301. 'class' => array('warning-on-change'),
  3302. ),
  3303. '#title' => t('Operator to use on all groups'),
  3304. '#description' => t('Either "group 0 AND group 1 AND group 2" or "group 0 OR group 1 OR group 2", etc'),
  3305. '#access' => $grouping,
  3306. );
  3307. $form['remove_groups']['#tree'] = TRUE;
  3308. foreach ($groups['groups'] as $id => $group) {
  3309. $form['filter_groups']['groups'][$id] = array(
  3310. '#title' => t('Operator'),
  3311. '#type' => 'select',
  3312. '#options' => array(
  3313. 'AND' => t('And'),
  3314. 'OR' => t('Or'),
  3315. ),
  3316. '#default_value' => $group,
  3317. '#attributes' => array(
  3318. 'class' => array('warning-on-change'),
  3319. ),
  3320. );
  3321. $form['remove_groups'][$id] = array(); // to prevent a notice
  3322. if ($id != 1) {
  3323. $form['remove_groups'][$id] = array(
  3324. '#type' => 'submit',
  3325. '#value' => t('Remove group @group', array('@group' => $id)),
  3326. '#id' => "views-remove-group-$id",
  3327. '#attributes' => array(
  3328. 'class' => array('views-remove-group'),
  3329. ),
  3330. '#group' => $id,
  3331. );
  3332. }
  3333. $group_options[$id] = $id == 1 ? t('Default group') : t('Group @group', array('@group' => $id));
  3334. $form['#group_renders'][$id] = array();
  3335. }
  3336. $form['#group_options'] = $group_options;
  3337. $form['#groups'] = $groups;
  3338. // We don't use get_handlers() because we want items without handlers to
  3339. // appear and show up as 'broken' so that the user can see them.
  3340. $form['filters'] = array('#tree' => TRUE);
  3341. foreach ($handlers as $id => $field) {
  3342. // If the group does not exist, move the filters to the default group.
  3343. if (empty($field['group']) || empty($groups['groups'][$field['group']])) {
  3344. $field['group'] = 1;
  3345. }
  3346. $handler = $display->handler->get_handler($type, $id);
  3347. if ($grouping && $handler && !$handler->can_group()) {
  3348. $field['group'] = 'ungroupable';
  3349. }
  3350. // If not grouping and the handler is set ungroupable, move it back to
  3351. // the default group to prevent weird errors from having it be in its
  3352. // own group:
  3353. if (!$grouping && $field['group'] == 'ungroupable') {
  3354. $field['group'] = 1;
  3355. }
  3356. // Place this item into the proper group for rendering.
  3357. $form['#group_renders'][$field['group']][] = $id;
  3358. $form['filters'][$id]['weight'] = array(
  3359. '#type' => 'textfield',
  3360. '#default_value' => ++$count,
  3361. '#size' => 8,
  3362. );
  3363. $form['filters'][$id]['group'] = array(
  3364. '#type' => 'select',
  3365. '#options' => $group_options,
  3366. '#default_value' => $field['group'],
  3367. '#attributes' => array(
  3368. 'class' => array('views-region-select', 'views-region-' . $id),
  3369. ),
  3370. '#access' => $field['group'] !== 'ungroupable',
  3371. );
  3372. if ($handler) {
  3373. $name = $handler->ui_name() . ' ' . $handler->admin_summary();
  3374. if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
  3375. $name = '(' . $relationships[$field['relationship']] . ') ' . $name;
  3376. }
  3377. $form['filters'][$id]['name'] = array(
  3378. '#markup' => $name,
  3379. );
  3380. }
  3381. else {
  3382. $form['filters'][$id]['name'] = array('#markup' => t('Broken field @id', array('@id' => $id)));
  3383. }
  3384. $form['filters'][$id]['removed'] = array(
  3385. '#type' => 'checkbox',
  3386. '#id' => 'views-removed-' . $id,
  3387. '#attributes' => array('class' => array('views-remove-checkbox')),
  3388. '#default_value' => 0,
  3389. );
  3390. }
  3391. if (isset($form_state['update_name'])) {
  3392. $name = $form_state['update_name'];
  3393. }
  3394. views_ui_standard_form_buttons($form, $form_state, 'views_ui_rearrange_filter_form');
  3395. $form['buttons']['add_group'] = array(
  3396. '#type' => 'submit',
  3397. '#value' => t('Create new filter group'),
  3398. '#id' => 'views-add-group',
  3399. '#group' => 'add',
  3400. );
  3401. return $form;
  3402. }
  3403. /**
  3404. * Turn the rearrange form into a proper table
  3405. */
  3406. function theme_views_ui_rearrange_filter_form(&$vars) {
  3407. $form = $vars['form'];
  3408. $rows = $ungroupable_rows = array();
  3409. // Enable grouping only if > 1 group.
  3410. $grouping = count(array_keys($form['#group_options'])) > 1;
  3411. foreach ($form['#group_renders'] as $group_id => $contents) {
  3412. // Header row for the group.
  3413. if ($group_id !== 'ungroupable') {
  3414. // Set up tabledrag so that it changes the group dropdown when rows are
  3415. // dragged between groups.
  3416. drupal_add_tabledrag('views-rearrange-filters', 'match', 'sibling', 'views-group-select', 'views-group-select-' . $group_id);
  3417. // Title row, spanning all columns.
  3418. $row = array();
  3419. // Add a cell to the first row, containing the group operator.
  3420. $row[] = array('class' => array('group', 'group-operator', 'container-inline'), 'data' => drupal_render($form['filter_groups']['groups'][$group_id]), 'rowspan' => max(array(2, count($contents) + 1)));
  3421. // Title.
  3422. $row[] = array('class' => array('group', 'group-title'), 'data' => '<span>' . $form['#group_options'][$group_id] . '</span>', 'colspan' => 4);
  3423. $rows[] = array('class' => array('views-group-title'), 'data' => $row, 'id' => 'views-group-title-' . $group_id);
  3424. // Row which will only appear if the group has nothing in it.
  3425. $row = array();
  3426. $class = 'group-' . (count($contents) ? 'populated' : 'empty');
  3427. $instructions = '<span>' . t('No filters have been added.') . '</span> <span class="js-only">' . t('Drag to add filters.') . '</span>';
  3428. // When JavaScript is enabled, the button for removing the group (if it's
  3429. // present) should be hidden, since it will be replaced by a link on the
  3430. // client side.
  3431. if (!empty($form['remove_groups'][$group_id]['#type']) && $form['remove_groups'][$group_id]['#type'] == 'submit') {
  3432. $form['remove_groups'][$group_id]['#attributes']['class'][] = 'js-hide';
  3433. }
  3434. $row[] = array('colspan' => 5, 'data' => $instructions . drupal_render($form['remove_groups'][$group_id]));
  3435. $rows[] = array('class' => array("group-message", "group-$group_id-message", $class), 'data' => $row, 'id' => 'views-group-' . $group_id);
  3436. }
  3437. foreach ($contents as $id) {
  3438. if (isset($form['filters'][$id]['name'])) {
  3439. $row = array();
  3440. $row[] = drupal_render($form['filters'][$id]['name']);
  3441. $form['filters'][$id]['weight']['#attributes']['class'] = array('weight');
  3442. $row[] = drupal_render($form['filters'][$id]['weight']);
  3443. $form['filters'][$id]['group']['#attributes']['class'] = array('views-group-select views-group-select-' . $group_id);
  3444. $row[] = drupal_render($form['filters'][$id]['group']);
  3445. $form['filters'][$id]['removed']['#attributes']['class'][] = 'js-hide';
  3446. $row[] = drupal_render($form['filters'][$id]['removed']) . l('<span>' . t('Remove') . '</span>', 'javascript:void()', array('attributes' => array('id' => 'views-remove-link-' . $id, 'class' => array('views-hidden', 'views-button-remove', 'views-groups-remove-link', 'views-remove-link'), 'alt' => t('Remove this item'), 'title' => t('Remove this item')), 'html' => true));
  3447. $row = array('data' => $row, 'class' => array('draggable'), 'id' => 'views-row-' . $id);
  3448. if ($group_id !== 'ungroupable') {
  3449. $rows[] = $row;
  3450. }
  3451. else {
  3452. $ungroupable_rows[] = $row;
  3453. }
  3454. }
  3455. }
  3456. }
  3457. if (empty($rows)) {
  3458. $rows[] = array(array('data' => t('No fields available.'), 'colspan' => '2'));
  3459. }
  3460. $output = drupal_render($form['override']);
  3461. $output .= '<div class="scroll">';
  3462. if ($grouping) {
  3463. $output .= drupal_render($form['filter_groups']['operator']);
  3464. }
  3465. else {
  3466. $form['filter_groups']['groups'][0]['#title'] = t('Operator');
  3467. $output .= drupal_render($form['filter_groups']['groups'][0]);
  3468. }
  3469. if (!empty($ungroupable_rows)) {
  3470. drupal_add_tabledrag('views-rearrange-filters-ungroupable', 'order', 'sibling', 'weight');
  3471. $header = array(t('Ungroupable filters'), t('Weight'), array('class' => array('views-hide-label'), 'data' => t('Group')), array('class' => array('views-hide-label'), 'data' => t('Remove')));
  3472. $output .= theme('table', array('header' => $header, 'rows' => $ungroupable_rows, 'attributes' => array('id' => 'views-rearrange-filters-ungroupable', 'class' => array('arrange'))));
  3473. }
  3474. // Set up tabledrag so that the weights are changed when rows are dragged.
  3475. drupal_add_tabledrag('views-rearrange-filters', 'order', 'sibling', 'weight');
  3476. $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'views-rearrange-filters', 'class' => array('arrange'))));
  3477. $output .= '</div>';
  3478. // When JavaScript is enabled, the button for adding a new group should be
  3479. // hidden, since it will be replaced by a link on the client side.
  3480. $form['buttons']['add_group']['#attributes']['class'][] = 'js-hide';
  3481. // Render the rest of the form and return.
  3482. $output .= drupal_render_children($form);
  3483. return $output;
  3484. }
  3485. /**
  3486. * Submit handler for rearranging form
  3487. */
  3488. function views_ui_rearrange_filter_form_submit($form, &$form_state) {
  3489. $types = views_object_types();
  3490. $display = &$form_state['view']->display[$form_state['display_id']];
  3491. $remember_groups = array();
  3492. if (!empty($form_state['view']->form_cache)) {
  3493. $old_fields = $form_state['view']->form_cache['handlers'];
  3494. }
  3495. else {
  3496. $old_fields = $display->handler->get_option($types[$form_state['type']]['plural']);
  3497. }
  3498. $count = 0;
  3499. $groups = $form_state['values']['filter_groups'];
  3500. // Whatever button was clicked, re-calculate field information.
  3501. $new_fields = $order = array();
  3502. // Make an array with the weights
  3503. foreach ($form_state['values']['filters'] as $field => $info) {
  3504. // add each value that is a field with a weight to our list, but only if
  3505. // it has had its 'removed' checkbox checked.
  3506. if (is_array($info) && empty($info['removed'])) {
  3507. if (isset($info['weight'])) {
  3508. $order[$field] = $info['weight'];
  3509. }
  3510. if (isset($info['group'])) {
  3511. $old_fields[$field]['group'] = $info['group'];
  3512. $remember_groups[$info['group']][] = $field;
  3513. }
  3514. }
  3515. }
  3516. // Sort the array
  3517. asort($order);
  3518. // Create a new list of fields in the new order.
  3519. foreach (array_keys($order) as $field) {
  3520. $new_fields[$field] = $old_fields[$field];
  3521. }
  3522. // If the #group property is set on the clicked button, that means we are
  3523. // either adding or removing a group, not actually updating the filters.
  3524. if (!empty($form_state['clicked_button']['#group'])) {
  3525. if ($form_state['clicked_button']['#group'] == 'add') {
  3526. // Add a new group
  3527. $groups['groups'][] = 'AND';
  3528. }
  3529. else {
  3530. // Renumber groups above the removed one down.
  3531. foreach (array_keys($groups['groups']) as $group_id) {
  3532. if ($group_id >= $form_state['clicked_button']['#group']) {
  3533. $old_group = $group_id + 1;
  3534. if (isset($groups['groups'][$old_group])) {
  3535. $groups['groups'][$group_id] = $groups['groups'][$old_group];
  3536. if (isset($remember_groups[$old_group])) {
  3537. foreach ($remember_groups[$old_group] as $id) {
  3538. $new_fields[$id]['group'] = $group_id;
  3539. }
  3540. }
  3541. }
  3542. else {
  3543. // If this is the last one, just unset it.
  3544. unset($groups['groups'][$group_id]);
  3545. }
  3546. }
  3547. }
  3548. }
  3549. // Update our cache with values so that cancel still works the way
  3550. // people expect.
  3551. $form_state['view']->form_cache = array(
  3552. 'key' => 'rearrange-filter',
  3553. 'groups' => $groups,
  3554. 'handlers' => $new_fields,
  3555. );
  3556. // Return to this form except on actual Update.
  3557. views_ui_add_form_to_stack('rearrange-filter', $form_state['view'], $form_state['display_id'], array($form_state['type']));
  3558. }
  3559. else {
  3560. // The actual update button was clicked. Remove the empty groups, and
  3561. // renumber them sequentially.
  3562. ksort($remember_groups);
  3563. $groups['groups'] = views_array_key_plus(array_values(array_intersect_key($groups['groups'], $remember_groups)));
  3564. // Change the 'group' key on each field to match. Here, $mapping is an
  3565. // array whose keys are the old group numbers and whose values are the new
  3566. // (sequentially numbered) ones.
  3567. $mapping = array_flip(views_array_key_plus(array_keys($remember_groups)));
  3568. foreach ($new_fields as &$new_field) {
  3569. $new_field['group'] = $mapping[$new_field['group']];
  3570. }
  3571. // Write the changed handler values.
  3572. $display->handler->set_option($types[$form_state['type']]['plural'], $new_fields);
  3573. $display->handler->set_option('filter_groups', $groups);
  3574. if (isset($form_state['view']->form_cache)) {
  3575. unset($form_state['view']->form_cache);
  3576. }
  3577. }
  3578. // Store in cache.
  3579. views_ui_cache_set($form_state['view']);
  3580. }
  3581. /**
  3582. * Form to add_item items in the views UI.
  3583. */
  3584. function views_ui_add_item_form($form, &$form_state) {
  3585. $view = &$form_state['view'];
  3586. $display_id = $form_state['display_id'];
  3587. $type = $form_state['type'];
  3588. $form = array(
  3589. 'options' => array(
  3590. '#theme_wrappers' => array('container'),
  3591. '#attributes' => array('class' => array('scroll')),
  3592. ),
  3593. );
  3594. ctools_add_js('dependent');
  3595. if (!$view->set_display($display_id)) {
  3596. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  3597. }
  3598. $display = &$view->display[$display_id];
  3599. $types = views_object_types();
  3600. $ltitle = $types[$type]['ltitle'];
  3601. $section = $types[$type]['plural'];
  3602. if (!empty($types[$type]['type'])) {
  3603. $type = $types[$type]['type'];
  3604. }
  3605. $form['#title'] = t('Add @type', array('@type' => $ltitle));
  3606. $form['#section'] = $display_id . 'add-item';
  3607. // Add the display override dropdown.
  3608. views_ui_standard_display_dropdown($form, $form_state, $section);
  3609. // Figure out all the base tables allowed based upon what the relationships provide.
  3610. $base_tables = $view->get_base_tables();
  3611. $options = views_fetch_fields(array_keys($base_tables), $type, $display->handler->use_group_by());
  3612. if (!empty($options)) {
  3613. $form['options']['controls'] = array(
  3614. '#theme_wrappers' => array('container'),
  3615. '#id' => 'views-filterable-options-controls',
  3616. '#attributes' => array('class' => array('container-inline')),
  3617. );
  3618. $form['options']['controls']['options_search'] = array(
  3619. '#type' => 'textfield',
  3620. '#title' => t('Search'),
  3621. );
  3622. $groups = array('all' => t('- All -'));
  3623. $form['options']['controls']['group'] = array(
  3624. '#type' => 'select',
  3625. '#title' => t('Filter'),
  3626. '#options' => array(),
  3627. '#attributes' => array('class' => array('ctools-master-dependent')),
  3628. );
  3629. $form['options']['name'] = array(
  3630. '#prefix' => '<div class="views-radio-box form-checkboxes views-filterable-options">',
  3631. '#suffix' => '</div>',
  3632. '#tree' => TRUE,
  3633. '#default_value' => 'all',
  3634. );
  3635. // Group options first to simplify the DOM objects that Views
  3636. // dependent JS will act upon.
  3637. $grouped_options = array();
  3638. foreach ($options as $key => $option) {
  3639. $group = preg_replace('/[^a-z0-9]/', '-', strtolower($option['group']));
  3640. $groups[$group] = $option['group'];
  3641. $grouped_options[$group][$key] = $option;
  3642. if (!empty($option['aliases']) && is_array($option['aliases'])) {
  3643. foreach ($option['aliases'] as $id => $alias) {
  3644. if (empty($alias['base']) || !empty($base_tables[$alias['base']])) {
  3645. $copy = $option;
  3646. $copy['group'] = $alias['group'];
  3647. $copy['title'] = $alias['title'];
  3648. if (isset($alias['help'])) {
  3649. $copy['help'] = $alias['help'];
  3650. }
  3651. $group = preg_replace('/[^a-z0-9]/', '-', strtolower($copy['group']));
  3652. $groups[$group] = $copy['group'];
  3653. $grouped_options[$group][$key . '$' . $id] = $copy;
  3654. }
  3655. }
  3656. }
  3657. }
  3658. foreach ($grouped_options as $group => $group_options) {
  3659. $form['options']['name'][$group . '_start']['#markup'] = '<div class="ctools-dependent-all ctools-dependent-' . $group . '">';
  3660. $zebra = 0;
  3661. foreach ($group_options as $key => $option) {
  3662. $zebra_class = ($zebra % 2) ? 'odd' : 'even';
  3663. $form['options']['name'][$key] = array(
  3664. '#type' => 'checkbox',
  3665. '#title' => t('!group: !field', array('!group' => check_plain($option['group']), '!field' => check_plain($option['title']))),
  3666. '#description' => filter_xss_admin($option['help']),
  3667. '#return_value' => $key,
  3668. '#prefix' => "<div class='$zebra_class filterable-option'>",
  3669. '#suffix' => '</div>',
  3670. );
  3671. $zebra++;
  3672. }
  3673. $form['options']['name'][$group . '_end']['#markup'] = '</div>';
  3674. }
  3675. $form['options']['controls']['group']['#options'] = $groups;
  3676. }
  3677. else {
  3678. $form['options']['markup'] = array(
  3679. '#markup' => '<div class="form-item">' . t('There are no @types available to add.', array('@types' => $ltitle)) . '</div>',
  3680. );
  3681. }
  3682. // Add a div to show the selected items
  3683. $form['selected'] = array(
  3684. '#type' => 'item',
  3685. '#markup' => '<div class="views-selected-options"></div>',
  3686. '#title' => t('Selected') . ':',
  3687. '#theme_wrappers' => array('form_element', 'views_container'),
  3688. '#attributes' => array('class' => array('container-inline', 'views-add-form-selected')),
  3689. );
  3690. ctools_include('dependent');
  3691. views_ui_standard_form_buttons($form, $form_state, 'views_ui_add_item_form', t('Add and configure @types', array('@types' => $ltitle)));
  3692. // Remove the default submit function.
  3693. $form['buttons']['submit']['#submit'] = array_diff($form['buttons']['submit']['#submit'], array('views_ui_standard_submit'));
  3694. $form['buttons']['submit']['#submit'][] = 'views_ui_add_item_form_submit';
  3695. return $form;
  3696. }
  3697. /**
  3698. * Submit handler for adding new item(s) to a view.
  3699. */
  3700. function views_ui_add_item_form_submit($form, &$form_state) {
  3701. $type = $form_state['type'];
  3702. $types = views_object_types();
  3703. $section = $types[$type]['plural'];
  3704. // Handle the override select.
  3705. list($was_defaulted, $is_defaulted) = views_ui_standard_override_values($form, $form_state);
  3706. if ($was_defaulted && !$is_defaulted) {
  3707. // We were using the default display's values, but we're now overriding
  3708. // the default display and saving values specific to this display.
  3709. $display = &$form_state['view']->display[$form_state['display_id']];
  3710. // set_override toggles the override of this section.
  3711. $display->handler->set_override($section);
  3712. }
  3713. elseif (!$was_defaulted && $is_defaulted) {
  3714. // We used to have an override for this display, but the user now wants
  3715. // to go back to the default display.
  3716. // Overwrite the default display with the current form values, and make
  3717. // the current display use the new default values.
  3718. $display = &$form_state['view']->display[$form_state['display_id']];
  3719. // options_override toggles the override of this section.
  3720. $display->handler->set_override($section);
  3721. }
  3722. if (!empty($form_state['values']['name']) && is_array($form_state['values']['name'])) {
  3723. // Loop through each of the items that were checked and add them to the view.
  3724. foreach (array_keys(array_filter($form_state['values']['name'])) as $field) {
  3725. list($table, $field) = explode('.', $field, 2);
  3726. if ($cut = strpos($field, '$')) {
  3727. $field = substr($field, 0, $cut);
  3728. }
  3729. $id = $form_state['view']->add_item($form_state['display_id'], $type, $table, $field);
  3730. // check to see if we have group by settings
  3731. $key = $type;
  3732. // Footer,header and empty text have a different internal handler type(area).
  3733. if (isset($types[$type]['type'])) {
  3734. $key = $types[$type]['type'];
  3735. }
  3736. $handler = views_get_handler($table, $field, $key);
  3737. if ($form_state['view']->display_handler->use_group_by() && $handler->use_group_by()) {
  3738. views_ui_add_form_to_stack('config-item-group', $form_state['view'], $form_state['display_id'], array($type, $id));
  3739. }
  3740. // check to see if this type has settings, if so add the settings form first
  3741. if ($handler && $handler->has_extra_options()) {
  3742. views_ui_add_form_to_stack('config-item-extra', $form_state['view'], $form_state['display_id'], array($type, $id));
  3743. }
  3744. // Then add the form to the stack
  3745. views_ui_add_form_to_stack('config-item', $form_state['view'], $form_state['display_id'], array($type, $id));
  3746. }
  3747. }
  3748. if (isset($form_state['view']->form_cache)) {
  3749. unset($form_state['view']->form_cache);
  3750. }
  3751. // Store in cache
  3752. views_ui_cache_set($form_state['view']);
  3753. }
  3754. /**
  3755. * Override handler for views_ui_edit_display_form
  3756. */
  3757. function views_ui_config_item_form_build_group($form, &$form_state) {
  3758. $item = &$form_state['handler']->options;
  3759. // flip. If the filter was a group, set back to a standard filter.
  3760. $item['is_grouped'] = empty($item['is_grouped']);
  3761. // If necessary, set new defaults:
  3762. if ($item['is_grouped']) {
  3763. $form_state['handler']->build_group_options();
  3764. }
  3765. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  3766. views_ui_add_form_to_stack($form_state['form_key'], $form_state['view'], $form_state['display_id'], array($form_state['type'], $form_state['id']), TRUE, TRUE);
  3767. views_ui_cache_set($form_state['view']);
  3768. $form_state['rerender'] = TRUE;
  3769. $form_state['rebuild'] = TRUE;
  3770. $form_state['force_build_group_options'] = TRUE;
  3771. }
  3772. /**
  3773. * Add a new group to the exposed filter groups.
  3774. */
  3775. function views_ui_config_item_form_add_group($form, &$form_state) {
  3776. $item =& $form_state['handler']->options;
  3777. // Add a new row.
  3778. $item['group_info']['group_items'][] = array();
  3779. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  3780. views_ui_cache_set($form_state['view']);
  3781. $form_state['rerender'] = TRUE;
  3782. $form_state['rebuild'] = TRUE;
  3783. $form_state['force_build_group_options'] = TRUE;
  3784. }
  3785. /**
  3786. * Form to config_item items in the views UI.
  3787. */
  3788. function views_ui_config_item_form($form, &$form_state) {
  3789. $view = &$form_state['view'];
  3790. $display_id = $form_state['display_id'];
  3791. $type = $form_state['type'];
  3792. $id = $form_state['id'];
  3793. $form = array(
  3794. 'options' => array(
  3795. '#tree' => TRUE,
  3796. '#theme_wrappers' => array('container'),
  3797. '#attributes' => array('class' => array('scroll')),
  3798. ),
  3799. );
  3800. if (!$view->set_display($display_id)) {
  3801. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  3802. }
  3803. $item = $view->get_item($display_id, $type, $id);
  3804. if ($item) {
  3805. $handler = $view->display_handler->get_handler($type, $id);
  3806. if (empty($handler)) {
  3807. $form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
  3808. }
  3809. else {
  3810. $types = views_object_types();
  3811. // If this item can come from the default display, show a dropdown
  3812. // that lets the user choose which display the changes should apply to.
  3813. if ($view->display_handler->defaultable_sections($types[$type]['plural'])) {
  3814. $form_state['section'] = $types[$type]['plural'];
  3815. views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
  3816. }
  3817. // A whole bunch of code to figure out what relationships are valid for
  3818. // this item.
  3819. $relationships = $view->display_handler->get_option('relationships');
  3820. $relationship_options = array();
  3821. foreach ($relationships as $relationship) {
  3822. // relationships can't link back to self. But also, due to ordering,
  3823. // relationships can only link to prior relationships.
  3824. if ($type == 'relationship' && $id == $relationship['id']) {
  3825. break;
  3826. }
  3827. $relationship_handler = views_get_handler($relationship['table'], $relationship['field'], 'relationship');
  3828. // ignore invalid/broken relationships.
  3829. if (empty($relationship_handler)) {
  3830. continue;
  3831. }
  3832. // If this relationship is valid for this type, add it to the list.
  3833. $data = views_fetch_data($relationship['table']);
  3834. $base = $data[$relationship['field']]['relationship']['base'];
  3835. $base_fields = views_fetch_fields($base, $form_state['type'], $view->display_handler->use_group_by());
  3836. if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
  3837. $relationship_handler->init($view, $relationship);
  3838. $relationship_options[$relationship['id']] = $relationship_handler->label();
  3839. }
  3840. }
  3841. if (!empty($relationship_options)) {
  3842. // Make sure the existing relationship is even valid. If not, force
  3843. // it to none.
  3844. $base_fields = views_fetch_fields($view->base_table, $form_state['type'], $view->display_handler->use_group_by());
  3845. if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
  3846. $relationship_options = array_merge(array('none' => t('Do not use a relationship')), $relationship_options);
  3847. }
  3848. $rel = empty($item['relationship']) ? 'none' : $item['relationship'];
  3849. if (empty($relationship_options[$rel])) {
  3850. // Pick the first relationship.
  3851. $rel = key($relationship_options);
  3852. // We want this relationship option to get saved even if the user
  3853. // skips submitting the form.
  3854. $view->set_item_option($display_id, $type, $id, 'relationship', $rel);
  3855. $temp_view = $view->clone_view();
  3856. views_ui_cache_set($temp_view);
  3857. }
  3858. $form['options']['relationship'] = array(
  3859. '#type' => 'select',
  3860. '#title' => t('Relationship'),
  3861. '#options' => $relationship_options,
  3862. '#default_value' => $rel,
  3863. '#weight' => -500,
  3864. );
  3865. }
  3866. else {
  3867. $form['options']['relationship'] = array(
  3868. '#type' => 'value',
  3869. '#value' => 'none',
  3870. );
  3871. }
  3872. $form['#title'] = t('Configure @type: @item', array('@type' => $types[$type]['lstitle'], '@item' => $handler->ui_name()));
  3873. if (!empty($handler->definition['help'])) {
  3874. $form['options']['form_description'] = array(
  3875. '#markup' => $handler->definition['help'],
  3876. '#theme_wrappers' => array('container'),
  3877. '#attributes' => array('class' => array('form-item description')),
  3878. '#weight' => -1000,
  3879. );
  3880. }
  3881. $form['#section'] = $display_id . '-' . $type . '-' . $id;
  3882. // Get form from the handler.
  3883. $handler->options_form($form['options'], $form_state);
  3884. $form_state['handler'] = &$handler;
  3885. }
  3886. $name = NULL;
  3887. if (isset($form_state['update_name'])) {
  3888. $name = $form_state['update_name'];
  3889. }
  3890. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_item_form', $name, t('Remove'), 'remove');
  3891. // Only validate the override values, because this values are required for
  3892. // the override selection.
  3893. $form['buttons']['remove']['#limit_validation_errors'] = array(array('override'));
  3894. }
  3895. return $form;
  3896. }
  3897. /**
  3898. * Submit handler for configing new item(s) to a view.
  3899. */
  3900. function views_ui_config_item_form_validate($form, &$form_state) {
  3901. $form_state['handler']->options_validate($form['options'], $form_state);
  3902. if (form_get_errors()) {
  3903. $form_state['rerender'] = TRUE;
  3904. }
  3905. }
  3906. /**
  3907. * A submit handler that is used for storing temporary items when using
  3908. * multi-step changes, such as ajax requests.
  3909. */
  3910. function views_ui_config_item_form_submit_temporary($form, &$form_state) {
  3911. // Run it through the handler's submit function.
  3912. $form_state['handler']->options_submit($form['options'], $form_state);
  3913. $item = $form_state['handler']->options;
  3914. $types = views_object_types();
  3915. // For footer/header $handler_type is area but $type is footer/header.
  3916. // For all other handle types it's the same.
  3917. $handler_type = $type = $form_state['type'];
  3918. if (!empty($types[$type]['type'])) {
  3919. $handler_type = $types[$type]['type'];
  3920. }
  3921. $override = NULL;
  3922. if ($form_state['view']->display_handler->use_group_by() && !empty($item['group_type'])) {
  3923. if (empty($form_state['view']->query)) {
  3924. $form_state['view']->init_query();
  3925. }
  3926. $aggregate = $form_state['view']->query->get_aggregation_info();
  3927. if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
  3928. $override = $aggregate[$item['group_type']]['handler'][$type];
  3929. }
  3930. }
  3931. // Create a new handler and unpack the options from the form onto it. We
  3932. // can use that for storage.
  3933. $handler = views_get_handler($item['table'], $item['field'], $handler_type, $override);
  3934. $handler->init($form_state['view'], $item);
  3935. // Add the incoming options to existing options because items using
  3936. // the extra form may not have everything in the form here.
  3937. $options = $form_state['values']['options'] + $form_state['handler']->options;
  3938. // This unpacks only options that are in the definition, ensuring random
  3939. // extra stuff on the form is not sent through.
  3940. $handler->unpack_options($handler->options, $options, NULL, FALSE);
  3941. // Store the item back on the view
  3942. $form_state['view']->temporary_options[$type][$form_state['id']] = $handler->options;
  3943. // @todo: Figure out whether views_ui_ajax_form is perhaps the better place to fix the issue.
  3944. // views_ui_ajax_form() drops the current form from the stack, even if it's an #ajax.
  3945. // So add the item back to the top of the stack.
  3946. views_ui_add_form_to_stack($form_state['form_key'], $form_state['view'], $form_state['display_id'], array($type, $item['id']), TRUE);
  3947. $form_state['rerender'] = TRUE;
  3948. $form_state['rebuild'] = TRUE;
  3949. // Write to cache
  3950. views_ui_cache_set($form_state['view']);
  3951. }
  3952. /**
  3953. * Submit handler for configing new item(s) to a view.
  3954. */
  3955. function views_ui_config_item_form_submit($form, &$form_state) {
  3956. // Run it through the handler's submit function.
  3957. $form_state['handler']->options_submit($form['options'], $form_state);
  3958. $item = $form_state['handler']->options;
  3959. $types = views_object_types();
  3960. // For footer/header $handler_type is area but $type is footer/header.
  3961. // For all other handle types it's the same.
  3962. $handler_type = $type = $form_state['type'];
  3963. if (!empty($types[$type]['type'])) {
  3964. $handler_type = $types[$type]['type'];
  3965. }
  3966. $override = NULL;
  3967. if ($form_state['view']->display_handler->use_group_by() && !empty($item['group_type'])) {
  3968. if (empty($form_state['view']->query)) {
  3969. $form_state['view']->init_query();
  3970. }
  3971. $aggregate = $form_state['view']->query->get_aggregation_info();
  3972. if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
  3973. $override = $aggregate[$item['group_type']]['handler'][$type];
  3974. }
  3975. }
  3976. // Create a new handler and unpack the options from the form onto it. We
  3977. // can use that for storage.
  3978. $handler = views_get_handler($item['table'], $item['field'], $handler_type, $override);
  3979. $handler->init($form_state['view'], $item);
  3980. // Add the incoming options to existing options because items using
  3981. // the extra form may not have everything in the form here.
  3982. $options = $form_state['values']['options'] + $form_state['handler']->options;
  3983. // This unpacks only options that are in the definition, ensuring random
  3984. // extra stuff on the form is not sent through.
  3985. $handler->unpack_options($handler->options, $options, NULL, FALSE);
  3986. // Store the item back on the view
  3987. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $handler->options);
  3988. // Ensure any temporary options are removed.
  3989. if (isset($form_state['view']->temporary_options[$type][$form_state['id']])) {
  3990. unset($form_state['view']->temporary_options[$type][$form_state['id']]);
  3991. }
  3992. // Write to cache
  3993. views_ui_cache_set($form_state['view']);
  3994. }
  3995. /**
  3996. * Form to config_item items in the views UI.
  3997. */
  3998. function views_ui_config_item_group_form($type, &$form_state) {
  3999. $view = &$form_state['view'];
  4000. $display_id = $form_state['display_id'];
  4001. $type = $form_state['type'];
  4002. $id = $form_state['id'];
  4003. $form = array(
  4004. 'options' => array(
  4005. '#tree' => TRUE,
  4006. '#theme_wrappers' => array('container'),
  4007. '#attributes' => array('class' => array('scroll')),
  4008. ),
  4009. );
  4010. if (!$view->set_display($display_id)) {
  4011. views_ajax_render(t('Invalid display id @display', array('@display' => $display_id)));
  4012. }
  4013. $view->init_query();
  4014. $item = $view->get_item($display_id, $type, $id);
  4015. if ($item) {
  4016. $handler = $view->display_handler->get_handler($type, $id);
  4017. if (empty($handler)) {
  4018. $form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
  4019. }
  4020. else {
  4021. $handler->init($view, $item);
  4022. $types = views_object_types();
  4023. $form['#title'] = t('Configure group settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->ui_name()));
  4024. $handler->groupby_form($form['options'], $form_state);
  4025. $form_state['handler'] = &$handler;
  4026. }
  4027. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_item_group_form');
  4028. }
  4029. return $form;
  4030. }
  4031. /**
  4032. * Submit handler for configing group settings on a view.
  4033. */
  4034. function views_ui_config_item_group_form_submit($form, &$form_state) {
  4035. $item =& $form_state['handler']->options;
  4036. $type = $form_state['type'];
  4037. $id = $form_state['id'];
  4038. $handler = views_get_handler($item['table'], $item['field'], $type);
  4039. $handler->init($form_state['view'], $item);
  4040. $handler->groupby_form_submit($form, $form_state);
  4041. // Store the item back on the view
  4042. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  4043. // Write to cache
  4044. views_ui_cache_set($form_state['view']);
  4045. }
  4046. /**
  4047. * Submit handler for removing an item from a view
  4048. */
  4049. function views_ui_config_item_form_remove($form, &$form_state) {
  4050. // Store the item back on the view
  4051. list($was_defaulted, $is_defaulted) = views_ui_standard_override_values($form, $form_state);
  4052. // If the display selection was changed toggle the override value.
  4053. if ($was_defaulted != $is_defaulted) {
  4054. $display =& $form_state['view']->display[$form_state['display_id']];
  4055. $display->handler->options_override($form, $form_state);
  4056. }
  4057. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], NULL);
  4058. // Write to cache
  4059. views_ui_cache_set($form_state['view']);
  4060. }
  4061. /**
  4062. * Override handler for views_ui_edit_display_form
  4063. */
  4064. function views_ui_config_item_form_expose($form, &$form_state) {
  4065. $item = &$form_state['handler']->options;
  4066. // flip
  4067. $item['exposed'] = empty($item['exposed']);
  4068. // If necessary, set new defaults:
  4069. if ($item['exposed']) {
  4070. $form_state['handler']->expose_options();
  4071. }
  4072. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  4073. views_ui_add_form_to_stack($form_state['form_key'], $form_state['view'], $form_state['display_id'], array($form_state['type'], $form_state['id']), TRUE, TRUE);
  4074. views_ui_cache_set($form_state['view']);
  4075. $form_state['rerender'] = TRUE;
  4076. $form_state['rebuild'] = TRUE;
  4077. $form_state['force_expose_options'] = TRUE;
  4078. }
  4079. /**
  4080. * Form to config_item items in the views UI.
  4081. */
  4082. function views_ui_config_item_extra_form($form, &$form_state) {
  4083. $view = &$form_state['view'];
  4084. $display_id = $form_state['display_id'];
  4085. $type = $form_state['type'];
  4086. $id = $form_state['id'];
  4087. $form = array(
  4088. 'options' => array(
  4089. '#tree' => TRUE,
  4090. '#theme_wrappers' => array('container'),
  4091. '#attributes' => array('class' => array('scroll')),
  4092. ),
  4093. );
  4094. if (!$view->set_display($display_id)) {
  4095. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  4096. }
  4097. $item = $view->get_item($display_id, $type, $id);
  4098. if ($item) {
  4099. $handler = $view->display_handler->get_handler($type, $id);
  4100. if (empty($handler)) {
  4101. $form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
  4102. }
  4103. else {
  4104. $handler->init($view, $item);
  4105. $types = views_object_types();
  4106. $form['#title'] = t('Configure extra settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->ui_name()));
  4107. $form['#section'] = $display_id . '-' . $type . '-' . $id;
  4108. // Get form from the handler.
  4109. $handler->extra_options_form($form['options'], $form_state);
  4110. $form_state['handler'] = &$handler;
  4111. }
  4112. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_item_extra_form');
  4113. }
  4114. return $form;
  4115. }
  4116. /**
  4117. * Validation handler for configing new item(s) to a view.
  4118. */
  4119. function views_ui_config_item_extra_form_validate($form, &$form_state) {
  4120. $form_state['handler']->extra_options_validate($form['options'], $form_state);
  4121. }
  4122. /**
  4123. * Submit handler for configing new item(s) to a view.
  4124. */
  4125. function views_ui_config_item_extra_form_submit($form, &$form_state) {
  4126. // Run it through the handler's submit function.
  4127. $form_state['handler']->extra_options_submit($form['options'], $form_state);
  4128. $item = $form_state['handler']->options;
  4129. // Store the data we're given.
  4130. foreach ($form_state['values']['options'] as $key => $value) {
  4131. $item[$key] = $value;
  4132. }
  4133. // Store the item back on the view
  4134. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  4135. // Write to cache
  4136. views_ui_cache_set($form_state['view']);
  4137. }
  4138. /**
  4139. * Form to config_style items in the views UI.
  4140. */
  4141. function views_ui_config_style_form($form, &$form_state) {
  4142. $view = &$form_state['view'];
  4143. $display_id = $form_state['display_id'];
  4144. $type = $form_state['type'];
  4145. $id = $form_state['id'];
  4146. $form = array(
  4147. 'options' => array(
  4148. '#tree' => TRUE,
  4149. '#theme_wrappers' => array('container'),
  4150. '#attributes' => array('class' => array('scroll')),
  4151. ),
  4152. );
  4153. if (!$view->set_display($display_id)) {
  4154. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  4155. }
  4156. $item = $view->get_item($display_id, $type, $id);
  4157. if ($item) {
  4158. $handler = views_get_handler($item['table'], $item['field'], $type);
  4159. if (empty($handler)) {
  4160. $form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
  4161. }
  4162. else {
  4163. $handler->init($view, $item);
  4164. $types = views_object_types();
  4165. $form['#title'] = t('Configure summary style for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->ui_name()));
  4166. $form['#section'] = $display_id . '-' . $type . '-style-options';
  4167. $plugin = views_get_plugin('style', $handler->options['style_plugin']);
  4168. if ($plugin) {
  4169. $form['style_options'] = array(
  4170. '#tree' => TRUE,
  4171. );
  4172. $plugin->init($view, $view->display[$display_id], $handler->options['style_options']);
  4173. $plugin->options_form($form['style_options'], $form_state);
  4174. }
  4175. $form_state['handler'] = &$handler;
  4176. }
  4177. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_style_form');
  4178. }
  4179. return $form;
  4180. }
  4181. /**
  4182. * Submit handler for configing new item(s) to a view.
  4183. */
  4184. function views_ui_config_style_form_submit($form, &$form_state) {
  4185. // Run it through the handler's submit function.
  4186. $form_state['handler']->options_submit($form['style_options'], $form_state);
  4187. $item = $form_state['handler']->options;
  4188. // Store the data we're given.
  4189. $item['style_options'] = $form_state['values']['style_options'];
  4190. // Store the item back on the view
  4191. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  4192. // Write to cache
  4193. views_ui_cache_set($form_state['view']);
  4194. }
  4195. /**
  4196. * Get a list of roles in the system.
  4197. */
  4198. function views_ui_get_roles() {
  4199. static $roles = NULL;
  4200. if (!isset($roles)) {
  4201. $roles = array();
  4202. $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
  4203. foreach ($result as $obj) {
  4204. $roles[$obj->rid] = $obj->name;
  4205. }
  4206. }
  4207. return $roles;
  4208. }
  4209. /**
  4210. * Form builder for the admin display defaults page.
  4211. */
  4212. function views_ui_admin_settings_basic() {
  4213. $form = array();
  4214. $form['#attached']['css'] = views_ui_get_admin_css();
  4215. $options = array();
  4216. foreach (list_themes() as $name => $theme) {
  4217. if ($theme->status) {
  4218. $options[$name] = $theme->info['name'];
  4219. }
  4220. }
  4221. // This is not currently a fieldset but we may want it to be later,
  4222. // so this will make it easier to change if we do.
  4223. $form['basic'] = array();
  4224. $form['basic']['views_ui_show_listing_filters'] = array(
  4225. '#type' => 'checkbox',
  4226. '#title' => t('Show filters on the list of views'),
  4227. '#default_value' => variable_get('views_ui_show_listing_filters', FALSE),
  4228. );
  4229. $form['basic']['views_ui_show_advanced_help_warning'] = array(
  4230. '#type' => 'checkbox',
  4231. '#title' => t('Show advanced help warning'),
  4232. '#default_value' => variable_get('views_ui_show_advanced_help_warning', TRUE),
  4233. );
  4234. $form['basic']['views_ui_show_master_display'] = array(
  4235. '#type' => 'checkbox',
  4236. '#title' => t('Always show the master display'),
  4237. '#description' => t('Advanced users of views may choose to see the master (i.e. default) display.'),
  4238. '#default_value' => variable_get('views_ui_show_master_display', FALSE),
  4239. );
  4240. $form['basic']['views_ui_show_advanced_column'] = array(
  4241. '#type' => 'checkbox',
  4242. '#title' => t('Always show advanced display settings'),
  4243. '#description' => t('Default to showing advanced display settings, such as relationships and contextual filters.'),
  4244. '#default_value' => variable_get('views_ui_show_advanced_column', FALSE),
  4245. );
  4246. $form['basic']['views_ui_display_embed'] = array(
  4247. '#type' => 'checkbox',
  4248. '#title' => t('Show the embed display in the ui.'),
  4249. '#description' => t('Allow advanced user to use the embed view display. The plugin itself works if it\'s not visible in the ui'),
  4250. '#default_value' => variable_get('views_ui_display_embed', FALSE),
  4251. );
  4252. $form['basic']['views_ui_custom_theme'] = array(
  4253. '#type' => 'select',
  4254. '#title' => t('Custom admin theme for the Views UI'),
  4255. '#options' => array('_default' => t('- Use default -')) + $options,
  4256. '#default_value' => variable_get('views_ui_custom_theme', '_default'),
  4257. '#description' => t('In some cases you might want to select a different admin theme for the Views UI.')
  4258. );
  4259. $form['basic']['views_exposed_filter_any_label'] = array(
  4260. '#type' => 'select',
  4261. '#title' => t('Label for "Any" value on non-required single-select exposed filters'),
  4262. '#options' => array('old_any' => '<Any>', 'new_any' => t('- Any -')),
  4263. '#default_value' => variable_get('views_exposed_filter_any_label', 'new_any'),
  4264. );
  4265. $form['live_preview'] = array(
  4266. '#type' => 'fieldset',
  4267. '#title' => t('Live preview settings'),
  4268. );
  4269. $form['live_preview']['views_ui_always_live_preview'] = array(
  4270. '#type' => 'checkbox',
  4271. '#title' => t('Automatically update preview on changes'),
  4272. '#default_value' => variable_get('views_ui_always_live_preview', TRUE),
  4273. );
  4274. // $form['live_preview']['views_ui_always_live_preview_button'] = array(
  4275. // '#type' => 'checkbox',
  4276. // '#title' => t('Always show the preview button, even when the automatically update option is checked'),
  4277. // '#default_value' => variable_get('views_ui_always_live_preview_button', FALSE),
  4278. // );
  4279. $form['live_preview']['views_ui_show_preview_information'] = array(
  4280. '#type' => 'checkbox',
  4281. '#title' => t('Show information and statistics about the view during live preview'),
  4282. '#default_value' => variable_get('views_ui_show_preview_information', TRUE),
  4283. );
  4284. $form['live_preview']['views_ui_show_sql_query_where'] = array(
  4285. '#type' => 'radios',
  4286. '#options' => array(
  4287. 'above' => t('Above the preview'),
  4288. 'below' => t('Below the preview'),
  4289. ),
  4290. '#id' => 'edit-show-sql',
  4291. '#default_value' => variable_get('views_ui_show_sql_query_where', 'above'),
  4292. '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
  4293. '#prefix' => '<div id="edit-show-sql-wrapper" class="views-dependent">',
  4294. '#suffix' => '</div>',
  4295. );
  4296. $form['live_preview']['views_ui_show_sql_query'] = array(
  4297. '#type' => 'checkbox',
  4298. '#title' => t('Show the SQL query'),
  4299. '#default_value' => variable_get('views_ui_show_sql_query', FALSE),
  4300. '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
  4301. );
  4302. $form['live_preview']['views_ui_show_performance_statistics'] = array(
  4303. '#type' => 'checkbox',
  4304. '#title' => t('Show performance statistics'),
  4305. '#default_value' => variable_get('views_ui_show_performance_statistics', FALSE),
  4306. '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
  4307. );
  4308. $form['live_preview']['views_show_additional_queries'] = array(
  4309. '#type' => 'checkbox',
  4310. '#title' => t('Show other queries run during render during live preview'),
  4311. '#description' => t("Drupal has the potential to run many queries while a view is being rendered. Checking this box will display every query run during view render as part of the live preview."),
  4312. '#default_value' => variable_get('views_show_additional_queries', FALSE),
  4313. '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
  4314. );
  4315. // $form['live_preview']['views_ui_show_performance_statistics_where'] = array(
  4316. return system_settings_form($form);
  4317. }
  4318. /**
  4319. * Form builder for the advanced admin settings page.
  4320. */
  4321. function views_ui_admin_settings_advanced() {
  4322. $form = array();
  4323. $form['#attached']['css'] = views_ui_get_admin_css();
  4324. $form['cache'] = array(
  4325. '#type' => 'fieldset',
  4326. '#title' => t('Caching'),
  4327. );
  4328. $form['cache']['views_skip_cache'] = array(
  4329. '#type' => 'checkbox',
  4330. '#title' => t('Disable views data caching'),
  4331. '#description' => t("Views caches data about tables, modules and views available, to increase performance. By checking this box, Views will skip this cache and always rebuild this data when needed. This can have a serious performance impact on your site."),
  4332. '#default_value' => variable_get('views_skip_cache', FALSE),
  4333. );
  4334. $form['cache']['clear_cache'] = array(
  4335. '#type' => 'submit',
  4336. '#value' => t("Clear Views' cache"),
  4337. '#submit' => array('views_ui_tools_clear_cache'),
  4338. );
  4339. $form['debug'] = array(
  4340. '#type' => 'fieldset',
  4341. '#title' => t('Debugging'),
  4342. );
  4343. $form['debug']['views_sql_signature'] = array(
  4344. '#type' => 'checkbox',
  4345. '#title' => t('Add Views signature to all SQL queries'),
  4346. '#description' => t("All Views-generated queries will include the name of the views and display 'view-name:display-name' as a string at the end of the SELECT clause. This makes identifying Views queries in database server logs simpler, but should only be used when troubleshooting."),
  4347. '#default_value' => variable_get('views_sql_signature', FALSE),
  4348. );
  4349. $form['debug']['views_no_javascript'] = array(
  4350. '#type' => 'checkbox',
  4351. '#title' => t('Disable JavaScript with Views'),
  4352. '#description' => t("If you are having problems with the JavaScript, you can disable it here. The Views UI should degrade and still be usable without javascript; it's just not as good."),
  4353. '#default_value' => variable_get('views_no_javascript', FALSE),
  4354. );
  4355. $form['debug']['views_devel_output'] = array(
  4356. '#type' => 'checkbox',
  4357. '#title' => t('Enable views performance statistics/debug messages via the Devel module'),
  4358. '#description' => t("Check this to enable some Views query and performance statistics/debug messages <em>if Devel is installed</em>."),
  4359. '#default_value' => variable_get('views_devel_output', FALSE),
  4360. );
  4361. $form['locale'] = array(
  4362. '#type' => 'fieldset',
  4363. '#title' => t('Localization'),
  4364. );
  4365. $form['locale']['views_localization_plugin'] = array(
  4366. '#type' => 'radios',
  4367. '#title' => t('Translation method'),
  4368. '#options' => views_fetch_plugin_names('localization', NULL, array()),
  4369. '#default_value' => views_get_localization_plugin(),
  4370. '#description' => t('Select a translation method to use for Views data like header, footer, and empty text.'),
  4371. );
  4372. $regions = array();
  4373. $regions['watchdog'] = t('Watchdog');
  4374. if (module_exists('devel')) {
  4375. $regions['message'] = t('Devel message(dpm)');
  4376. $regions['drupal_debug'] = t('Devel logging (tmp://drupal_debug.txt)');
  4377. }
  4378. $form['debug']['views_devel_region'] = array(
  4379. '#type' => 'select',
  4380. '#title' => t('Page region to output performance statistics/debug messages'),
  4381. '#default_value' => variable_get('views_devel_region', 'footer'),
  4382. '#options' => $regions,
  4383. '#dependency' => array('edit-views-devel-output' => array(1)),
  4384. );
  4385. $options = views_fetch_plugin_names('display_extender');
  4386. if (!empty($options)) {
  4387. $form['extenders'] = array(
  4388. '#type' => 'fieldset',
  4389. );
  4390. ;
  4391. $form['extenders']['views_display_extenders'] = array(
  4392. '#title' => t('Display extenders'),
  4393. '#default_value' => views_get_enabled_display_extenders(),
  4394. '#options' => $options,
  4395. '#type' => 'checkboxes',
  4396. '#description' => t('Select extensions of the views interface.')
  4397. );
  4398. }
  4399. return system_settings_form($form);
  4400. }
  4401. /**
  4402. * Submit hook to clear the views cache.
  4403. */
  4404. function views_ui_tools_clear_cache() {
  4405. views_invalidate_cache();
  4406. drupal_set_message(t('The cache has been cleared.'));
  4407. }
  4408. /**
  4409. * Submit hook to clear Drupal's theme registry (thereby triggering
  4410. * a templates rescan).
  4411. */
  4412. function views_ui_config_item_form_rescan($form, &$form_state) {
  4413. drupal_theme_rebuild();
  4414. // The 'Theme: Information' page is about to be shown again. That page
  4415. // analyzes the output of theme_get_registry(). However, this latter
  4416. // function uses an internal cache (which was initialized before we
  4417. // called drupal_theme_rebuild()) so it won't reflect the
  4418. // current state of our theme registry. The only way to clear that cache
  4419. // is to re-initialize the theme system:
  4420. unset($GLOBALS['theme']);
  4421. drupal_theme_initialize();
  4422. $form_state['rerender'] = TRUE;
  4423. $form_state['rebuild'] = TRUE;
  4424. }
  4425. /**
  4426. * Override handler for views_ui_edit_display_form
  4427. */
  4428. function views_ui_edit_display_form_change_theme($form, &$form_state) {
  4429. // This is just a temporary variable.
  4430. $form_state['view']->theme = $form_state['values']['theme'];
  4431. views_ui_cache_set($form_state['view']);
  4432. $form_state['rerender'] = TRUE;
  4433. $form_state['rebuild'] = TRUE;
  4434. }
  4435. /**
  4436. * Page callback for views tag autocomplete
  4437. */
  4438. function views_ui_autocomplete_tag($string = '') {
  4439. $matches = array();
  4440. // get matches from default views:
  4441. views_include('view');
  4442. $views = views_get_all_views();
  4443. foreach ($views as $view) {
  4444. if (!empty($view->tag) && strpos($view->tag, $string) === 0) {
  4445. $matches[$view->tag] = check_plain($view->tag);
  4446. if (count($matches) >= 10) {
  4447. break;
  4448. }
  4449. }
  4450. }
  4451. drupal_json_output($matches);
  4452. }
  4453. // ------------------------------------------------------------------
  4454. // Get information from the Views data
  4455. function _views_weight_sort($a, $b) {
  4456. if ($a['weight'] != $b['weight']) {
  4457. return $a['weight'] < $b['weight'] ? -1 : 1;
  4458. }
  4459. if ($a['title'] != $b['title']) {
  4460. return $a['title'] < $b['title'] ? -1 : 1;
  4461. }
  4462. return 0;
  4463. }
  4464. /**
  4465. * Fetch a list of all base tables available
  4466. *
  4467. * @return
  4468. * A keyed array of in the form of 'base_table' => 'Description'.
  4469. */
  4470. function views_fetch_base_tables() {
  4471. static $base_tables = array();
  4472. if (empty($base_tables)) {
  4473. $weights = array();
  4474. $tables = array();
  4475. $data = views_fetch_data();
  4476. foreach ($data as $table => $info) {
  4477. if (!empty($info['table']['base'])) {
  4478. $tables[$table] = array(
  4479. 'title' => $info['table']['base']['title'],
  4480. 'description' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
  4481. 'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
  4482. );
  4483. }
  4484. }
  4485. uasort($tables, '_views_weight_sort');
  4486. $base_tables = $tables;
  4487. }
  4488. return $base_tables;
  4489. }
  4490. function _views_sort_types($a, $b) {
  4491. $a_group = drupal_strtolower($a['group']);
  4492. $b_group = drupal_strtolower($b['group']);
  4493. if ($a_group != $b_group) {
  4494. return $a_group < $b_group ? -1 : 1;
  4495. }
  4496. $a_title = drupal_strtolower($a['title']);
  4497. $b_title = drupal_strtolower($b['title']);
  4498. if ($a_title != $b_title) {
  4499. return $a_title < $b_title ? -1 : 1;
  4500. }
  4501. return 0;
  4502. }
  4503. /**
  4504. * Fetch a list of all fields available for a given base type.
  4505. *
  4506. * @param (array|string) $base
  4507. * A list or a single base_table, for example node.
  4508. * @param string $type
  4509. * The handler type, for example field or filter.
  4510. * @param bool $grouping
  4511. * Should the result grouping by its 'group' label.
  4512. *
  4513. * @return array
  4514. * A keyed array of in the form of 'base_table' => 'Description'.
  4515. */
  4516. function views_fetch_fields($base, $type, $grouping = FALSE) {
  4517. static $fields = array();
  4518. if (empty($fields)) {
  4519. $data = views_fetch_data();
  4520. $start = microtime(TRUE);
  4521. // This constructs this ginormous multi dimensional array to
  4522. // collect the important data about fields. In the end,
  4523. // the structure looks a bit like this (using nid as an example)
  4524. // $strings['nid']['filter']['title'] = 'string'.
  4525. //
  4526. // This is constructed this way because the above referenced strings
  4527. // can appear in different places in the actual data structure so that
  4528. // the data doesn't have to be repeated a lot. This essentially lets
  4529. // each field have a cheap kind of inheritance.
  4530. foreach ($data as $table => $table_data) {
  4531. $bases = array();
  4532. $strings = array();
  4533. $skip_bases = array();
  4534. foreach ($table_data as $field => $info) {
  4535. // Collect table data from this table
  4536. if ($field == 'table') {
  4537. // calculate what tables this table can join to.
  4538. if (!empty($info['join'])) {
  4539. $bases = array_keys($info['join']);
  4540. }
  4541. // And it obviously joins to itself.
  4542. $bases[] = $table;
  4543. continue;
  4544. }
  4545. foreach (array('field', 'sort', 'filter', 'argument', 'relationship', 'area') as $key) {
  4546. if (!empty($info[$key])) {
  4547. if ($grouping && !empty($info[$key]['no group by'])) {
  4548. continue;
  4549. }
  4550. if (!empty($info[$key]['skip base'])) {
  4551. foreach ((array) $info[$key]['skip base'] as $base_name) {
  4552. $skip_bases[$field][$key][$base_name] = TRUE;
  4553. }
  4554. }
  4555. elseif (!empty($info['skip base'])) {
  4556. foreach ((array) $info['skip base'] as $base_name) {
  4557. $skip_bases[$field][$key][$base_name] = TRUE;
  4558. }
  4559. }
  4560. // Don't show old fields. The real field will be added right.
  4561. if (isset($info[$key]['moved to'])) {
  4562. continue;
  4563. }
  4564. foreach (array('title', 'group', 'help', 'base', 'aliases') as $string) {
  4565. // First, try the lowest possible level
  4566. if (!empty($info[$key][$string])) {
  4567. $strings[$field][$key][$string] = $info[$key][$string];
  4568. }
  4569. // Then try the field level
  4570. elseif (!empty($info[$string])) {
  4571. $strings[$field][$key][$string] = $info[$string];
  4572. }
  4573. // Finally, try the table level
  4574. elseif (!empty($table_data['table'][$string])) {
  4575. $strings[$field][$key][$string] = $table_data['table'][$string];
  4576. }
  4577. else {
  4578. if ($string != 'base' && $string != 'base') {
  4579. $strings[$field][$key][$string] = t("Error: missing @component", array('@component' => $string));
  4580. }
  4581. }
  4582. }
  4583. }
  4584. }
  4585. }
  4586. foreach ($bases as $base_name) {
  4587. foreach ($strings as $field => $field_strings) {
  4588. foreach ($field_strings as $type_name => $type_strings) {
  4589. if (empty($skip_bases[$field][$type_name][$base_name])) {
  4590. $fields[$base_name][$type_name]["$table.$field"] = $type_strings;
  4591. }
  4592. }
  4593. }
  4594. }
  4595. }
  4596. // vsm('Views UI data build time: ' . (views_microtime() - $start) * 1000 . ' ms');
  4597. }
  4598. // If we have an array of base tables available, go through them
  4599. // all and add them together. Duplicate keys will be lost and that's
  4600. // Just Fine.
  4601. if (is_array($base)) {
  4602. $strings = array();
  4603. foreach ($base as $base_table) {
  4604. if (isset($fields[$base_table][$type])) {
  4605. $strings += $fields[$base_table][$type];
  4606. }
  4607. }
  4608. uasort($strings, '_views_sort_types');
  4609. return $strings;
  4610. }
  4611. if (isset($fields[$base][$type])) {
  4612. uasort($fields[$base][$type], '_views_sort_types');
  4613. return $fields[$base][$type];
  4614. }
  4615. return array();
  4616. }
  4617. /**
  4618. * Theme the form for the table style plugin
  4619. */
  4620. function theme_views_ui_style_plugin_table($variables) {
  4621. $form = $variables['form'];
  4622. $output = drupal_render($form['description_markup']);
  4623. $header = array(
  4624. t('Field'),
  4625. t('Column'),
  4626. t('Align'),
  4627. t('Separator'),
  4628. array(
  4629. 'data' => t('Sortable'),
  4630. 'align' => 'center',
  4631. ),
  4632. array(
  4633. 'data' => t('Default order'),
  4634. 'align' => 'center',
  4635. ),
  4636. array(
  4637. 'data' => t('Default sort'),
  4638. 'align' => 'center',
  4639. ),
  4640. array(
  4641. 'data' => t('Hide empty column'),
  4642. 'align' => 'center',
  4643. ),
  4644. );
  4645. $rows = array();
  4646. foreach (element_children($form['columns']) as $id) {
  4647. $row = array();
  4648. $row[] = check_plain(drupal_render($form['info'][$id]['name']));
  4649. $row[] = drupal_render($form['columns'][$id]);
  4650. $row[] = drupal_render($form['info'][$id]['align']);
  4651. $row[] = drupal_render($form['info'][$id]['separator']);
  4652. if (!empty($form['info'][$id]['sortable'])) {
  4653. $row[] = array(
  4654. 'data' => drupal_render($form['info'][$id]['sortable']),
  4655. 'align' => 'center',
  4656. );
  4657. $row[] = array(
  4658. 'data' => drupal_render($form['info'][$id]['default_sort_order']),
  4659. 'align' => 'center',
  4660. );
  4661. $row[] = array(
  4662. 'data' => drupal_render($form['default'][$id]),
  4663. 'align' => 'center',
  4664. );
  4665. }
  4666. else {
  4667. $row[] = '';
  4668. $row[] = '';
  4669. $row[] = '';
  4670. }
  4671. $row[] = array(
  4672. 'data' => drupal_render($form['info'][$id]['empty_column']),
  4673. 'align' => 'center',
  4674. );
  4675. $rows[] = $row;
  4676. }
  4677. // Add the special 'None' row.
  4678. $rows[] = array(t('None'), '', '', '', '', '', array('align' => 'center', 'data' => drupal_render($form['default'][-1])), '');
  4679. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  4680. $output .= drupal_render_children($form);
  4681. return $output;
  4682. }
  4683. /**
  4684. * Placeholder function for overriding $display->display_title.
  4685. *
  4686. * @todo Remove this function once editing the display title is possible.
  4687. */
  4688. function views_ui_get_display_label($view, $display_id, $check_changed = TRUE) {
  4689. $title = $display_id == 'default' ? t('Master') : $view->display[$display_id]->display_title;
  4690. $title = views_ui_truncate($title, 25);
  4691. if ($check_changed && !empty($view->changed_display[$display_id])) {
  4692. $changed = '*';
  4693. $title = $title . $changed;
  4694. }
  4695. return $title;
  4696. }
  4697. function views_ui_add_template_page() {
  4698. $templates = views_get_all_templates();
  4699. if (empty($templates)) {
  4700. return t('There are no templates available.');
  4701. }
  4702. $header = array(
  4703. t('Name'),
  4704. t('Description'),
  4705. t('Operation'),
  4706. );
  4707. $rows = array();
  4708. foreach ($templates as $name => $template) {
  4709. $rows[] = array(
  4710. array('data' => check_plain($template->get_human_name())),
  4711. array('data' => check_plain($template->description)),
  4712. array('data' => l('add', 'admin/structure/views/template/' . $template->name . '/add')),
  4713. );
  4714. }
  4715. $output = theme('table', array('header' => $header, 'rows' => $rows));
  4716. return $output;
  4717. }
  4718. /**
  4719. * #process callback for a button; determines if a button is the form's triggering element.
  4720. *
  4721. * The Form API has logic to determine the form's triggering element based on
  4722. * the data in $_POST. However, it only checks buttons based on a single #value
  4723. * per button. This function may be added to a button's #process callbacks to
  4724. * extend button click detection to support multiple #values per button. If the
  4725. * data in $_POST matches any value in the button's #values array, then the
  4726. * button is detected as having been clicked. This can be used when the value
  4727. * (label) of the same logical button may be different based on context (e.g.,
  4728. * "Apply" vs. "Apply and continue").
  4729. *
  4730. * @see _form_builder_handle_input_element()
  4731. * @see _form_button_was_clicked()
  4732. */
  4733. function views_ui_form_button_was_clicked($element, &$form_state) {
  4734. $process_input = empty($element['#disabled']) && ($form_state['programmed'] || ($form_state['process_input'] && (!isset($element['#access']) || $element['#access'])));
  4735. if ($process_input && !isset($form_state['triggering_element']) && isset($element['#button_type']) && isset($form_state['input'][$element['#name']]) && isset($element['#values']) && in_array($form_state['input'][$element['#name']], $element['#values'], TRUE)) {
  4736. $form_state['triggering_element'] = $element;
  4737. }
  4738. return $element;
  4739. }
  4740. /**
  4741. * #process callback for a button; makes implicit form submissions trigger as this button.
  4742. *
  4743. * @see Drupal.behaviors.viewsImplicitFormSubmission
  4744. */
  4745. function views_ui_default_button($element, &$form_state, $form) {
  4746. $setting['viewsImplicitFormSubmission'][$form['#id']]['defaultButton'] = $element['#id'];
  4747. $element['#attached']['js'][] = array('type' => 'setting', 'data' => $setting);
  4748. return $element;
  4749. }
  4750. /**
  4751. * List all instances of fields on any views.
  4752. *
  4753. * Therefore it builds up a table of each field which is used in any view.
  4754. *
  4755. * @see field_ui_fields_list()
  4756. */
  4757. function views_ui_field_list() {
  4758. $views = views_get_all_views();
  4759. // Fetch all fieldapi fields which are used in views
  4760. // Therefore search in all views, displays and handler-types.
  4761. $fields = array();
  4762. foreach ($views as $view) {
  4763. foreach ($view->display as $display_id => $display) {
  4764. if ($view->set_display($display_id)) {
  4765. foreach (views_object_types() as $type => $info) {
  4766. foreach ($view->get_items($type, $display_id) as $item) {
  4767. $data = views_fetch_data($item['table']);
  4768. if (isset($data[$item['field']]) && isset($data[$item['field']][$type])
  4769. && $data = $data[$item['field']][$type]) {
  4770. // The final check that we have a fieldapi field now.
  4771. if (isset($data['field_name'])) {
  4772. $fields[$data['field_name']][$view->name] = $view->name;
  4773. }
  4774. }
  4775. }
  4776. }
  4777. }
  4778. }
  4779. }
  4780. $header = array(t('Field name'), t('Used in'));
  4781. $rows = array();
  4782. foreach ($fields as $field_name => $views) {
  4783. $rows[$field_name]['data'][0] = check_plain($field_name);
  4784. foreach ($views as $view) {
  4785. $rows[$field_name]['data'][1][] = l($view, "admin/structure/views/view/$view");
  4786. }
  4787. $rows[$field_name]['data'][1] = implode(', ', $rows[$field_name]['data'][1]);
  4788. }
  4789. // Sort rows by field name.
  4790. ksort($rows);
  4791. $output = array(
  4792. '#theme' => 'table',
  4793. '#header' => $header,
  4794. '#rows' => $rows,
  4795. '#empty' => t('No fields have been used in views yet.'),
  4796. );
  4797. return $output;
  4798. }
  4799. /**
  4800. * Lists all plugins and what enabled Views use them.
  4801. */
  4802. function views_ui_plugin_list() {
  4803. $rows = views_plugin_list();
  4804. foreach ($rows as &$row) {
  4805. // Link each view name to the view itself.
  4806. foreach ($row['views'] as $row_name => $view) {
  4807. $row['views'][$row_name] = l($view, "admin/structure/views/view/$view");
  4808. }
  4809. $row['views'] = implode(', ', $row['views']);
  4810. }
  4811. // Sort rows by field name.
  4812. ksort($rows);
  4813. return array(
  4814. '#theme' => 'table',
  4815. '#header' => array(t('Type'), t('Name'), t('Provided by'), t('Used in')),
  4816. '#rows' => $rows,
  4817. '#empty' => t('There are no enabled views.'),
  4818. );
  4819. }