PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/sites/all/modules/contrib/views/plugins/views_wizard/views_ui_base_views_wizard.class.php

https://bitbucket.org/micahw156/sites_blogrimage
PHP | 929 lines | 620 code | 81 blank | 228 comment | 72 complexity | 1c37eac9183de1bae5c97b34a781711b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file
  4. * Provides the interface and base class for Views Wizard plugins.
  5. */
  6. /**
  7. * Defines a common interface for Views Wizard plugins.
  8. */
  9. interface ViewsWizardInterface {
  10. function __construct($plugin);
  11. /**
  12. * For AJAX callbacks to build other elements in the "show" form.
  13. */
  14. function build_form($form, &$form_state);
  15. /**
  16. * Validate form and values.
  17. *
  18. * @return an array of form errors.
  19. */
  20. function validate($form, &$form_state);
  21. /**
  22. * Create a new View from form values.
  23. *
  24. * @return a view object.
  25. *
  26. * @throws ViewsWizardException in the event of a problem.
  27. */
  28. function create_view($form, &$form_state);
  29. }
  30. /**
  31. * A custom exception class for our errors.
  32. */
  33. class ViewsWizardException extends Exception {
  34. }
  35. /**
  36. * A very generic Views Wizard class - can be constructed for any base table.
  37. */
  38. class ViewsUiBaseViewsWizard implements ViewsWizardInterface {
  39. protected $base_table;
  40. protected $entity_type;
  41. protected $entity_info = array();
  42. protected $validated_views = array();
  43. protected $plugin = array();
  44. protected $filter_defaults = array(
  45. 'id' => NULL,
  46. 'expose' => array('operator' => FALSE),
  47. 'group' => 1,
  48. );
  49. function __construct($plugin) {
  50. $this->base_table = $plugin['base_table'];
  51. $default = $this->filter_defaults;
  52. if (isset($plugin['filters'])) {
  53. foreach ($plugin['filters'] as $name => $info) {
  54. $default['id'] = $name;
  55. $plugin['filters'][$name] = $info + $default;
  56. }
  57. }
  58. $this->plugin = $plugin;
  59. $entities = entity_get_info();
  60. foreach ($entities as $entity_type => $entity_info) {
  61. if (isset($entity_info['base table']) && $this->base_table == $entity_info['base table']) {
  62. $this->entity_info = $entity_info;
  63. $this->entity_type = $entity_type;
  64. }
  65. }
  66. }
  67. function build_form($form, &$form_state) {
  68. $style_options = views_fetch_plugin_names('style', 'normal', array($this->base_table));
  69. $feed_row_options = views_fetch_plugin_names('row', 'feed', array($this->base_table));
  70. $path_prefix = url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q=');
  71. // Add filters and sorts which apply to the view as a whole.
  72. $this->build_filters($form, $form_state);
  73. $this->build_sorts($form, $form_state);
  74. $form['displays']['page'] = array(
  75. '#type' => 'fieldset',
  76. '#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend'),),
  77. '#tree' => TRUE,
  78. );
  79. $form['displays']['page']['create'] = array(
  80. '#title' => t('Create a page'),
  81. '#type' => 'checkbox',
  82. '#attributes' => array('class' => array('strong')),
  83. '#default_value' => TRUE,
  84. '#id' => 'edit-page-create',
  85. );
  86. // All options for the page display are included in this container so they
  87. // can be hidden en masse when the "Create a page" checkbox is unchecked.
  88. $form['displays']['page']['options'] = array(
  89. '#type' => 'container',
  90. '#attributes' => array('class' => array('options-set'),),
  91. '#dependency' => array(
  92. 'edit-page-create' => array(1),
  93. ),
  94. '#pre_render' => array('ctools_dependent_pre_render'),
  95. '#prefix' => '<div><div id="edit-page-wrapper">',
  96. '#suffix' => '</div></div>',
  97. '#parents' => array('page'),
  98. );
  99. $form['displays']['page']['options']['title'] = array(
  100. '#title' => t('Page title'),
  101. '#type' => 'textfield',
  102. );
  103. $form['displays']['page']['options']['path'] = array(
  104. '#title' => t('Path'),
  105. '#type' => 'textfield',
  106. '#field_prefix' => $path_prefix,
  107. );
  108. $form['displays']['page']['options']['style'] = array(
  109. '#type' => 'fieldset',
  110. '#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
  111. );
  112. // Create the dropdown for choosing the display format.
  113. $form['displays']['page']['options']['style']['style_plugin'] = array(
  114. '#title' => t('Display format'),
  115. '#help_topic' => 'style',
  116. '#type' => 'select',
  117. '#options' => $style_options,
  118. );
  119. $style_form = &$form['displays']['page']['options']['style'];
  120. $style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array('page', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
  121. // Changing this dropdown updates $form['displays']['page']['options'] via
  122. // AJAX.
  123. views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'page', 'options'));
  124. $this->build_form_style($form, $form_state, 'page');
  125. $form['displays']['page']['options']['items_per_page'] = array(
  126. '#title' => t('Items to display'),
  127. '#type' => 'textfield',
  128. '#default_value' => '10',
  129. '#size' => 5,
  130. '#element_validate' => array('views_element_validate_integer'),
  131. );
  132. $form['displays']['page']['options']['pager'] = array(
  133. '#title' => t('Use a pager'),
  134. '#type' => 'checkbox',
  135. '#default_value' => TRUE,
  136. );
  137. $form['displays']['page']['options']['link'] = array(
  138. '#title' => t('Create a menu link'),
  139. '#type' => 'checkbox',
  140. '#id' => 'edit-page-link',
  141. );
  142. $form['displays']['page']['options']['link_properties'] = array(
  143. '#type' => 'container',
  144. '#dependency' => array(
  145. 'edit-page-link' => array(1),
  146. ),
  147. '#pre_render' => array('ctools_dependent_pre_render'),
  148. '#prefix' => '<div id="edit-page-link-properties-wrapper">',
  149. '#suffix' => '</div>',
  150. );
  151. if (module_exists('menu')) {
  152. $menu_options = menu_get_menus();
  153. }
  154. else {
  155. // These are not yet translated.
  156. $menu_options = menu_list_system_menus();
  157. foreach ($menu_options as $name => $title) {
  158. $menu_options[$name] = t($title);
  159. }
  160. }
  161. $form['displays']['page']['options']['link_properties']['menu_name'] = array(
  162. '#title' => t('Menu'),
  163. '#type' => 'select',
  164. '#options' => $menu_options,
  165. );
  166. $form['displays']['page']['options']['link_properties']['title'] = array(
  167. '#title' => t('Link text'),
  168. '#type' => 'textfield',
  169. );
  170. // Only offer a feed if we have at least one available feed row style.
  171. if ($feed_row_options) {
  172. $form['displays']['page']['options']['feed'] = array(
  173. '#title' => t('Include an RSS feed'),
  174. '#type' => 'checkbox',
  175. '#id' => 'edit-page-feed',
  176. );
  177. $form['displays']['page']['options']['feed_properties'] = array(
  178. '#type' => 'container',
  179. '#dependency' => array(
  180. 'edit-page-feed' => array(1),
  181. ),
  182. '#pre_render' => array('ctools_dependent_pre_render'),
  183. '#prefix' => '<div id="edit-page-feed-properties-wrapper">',
  184. '#suffix' => '</div>',
  185. );
  186. $form['displays']['page']['options']['feed_properties']['path'] = array(
  187. '#title' => t('Feed path'),
  188. '#type' => 'textfield',
  189. '#field_prefix' => $path_prefix,
  190. );
  191. // This will almost never be visible.
  192. $form['displays']['page']['options']['feed_properties']['row_plugin'] = array(
  193. '#title' => t('Feed row style'),
  194. '#type' => 'select',
  195. '#options' => $feed_row_options,
  196. '#default_value' => key($feed_row_options),
  197. '#access' => (count($feed_row_options) > 1),
  198. '#dependency' => array(
  199. 'edit-page-feed' => array(1),
  200. ),
  201. '#pre_render' => array('ctools_dependent_pre_render'),
  202. '#prefix' => '<div id="edit-page-feed-properties-row-plugin-wrapper">',
  203. '#suffix' => '</div>',
  204. );
  205. }
  206. $form['displays']['block'] = array(
  207. '#type' => 'fieldset',
  208. '#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend'),),
  209. '#tree' => TRUE,
  210. );
  211. $form['displays']['block']['create'] = array(
  212. '#title' => t('Create a block'),
  213. '#type' => 'checkbox',
  214. '#attributes' => array('class' => array('strong')),
  215. '#id' => 'edit-block-create',
  216. );
  217. // All options for the block display are included in this container so they
  218. // can be hidden en masse when the "Create a block" checkbox is unchecked.
  219. $form['displays']['block']['options'] = array(
  220. '#type' => 'container',
  221. '#attributes' => array('class' => array('options-set'),),
  222. '#dependency' => array(
  223. 'edit-block-create' => array(1),
  224. ),
  225. '#pre_render' => array('ctools_dependent_pre_render'),
  226. '#prefix' => '<div id="edit-block-wrapper">',
  227. '#suffix' => '</div>',
  228. '#parents' => array('block'),
  229. );
  230. $form['displays']['block']['options']['title'] = array(
  231. '#title' => t('Block title'),
  232. '#type' => 'textfield',
  233. );
  234. $form['displays']['block']['options']['style'] = array(
  235. '#type' => 'fieldset',
  236. '#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
  237. );
  238. // Create the dropdown for choosing the display format.
  239. $form['displays']['block']['options']['style']['style_plugin'] = array(
  240. '#title' => t('Display format'),
  241. '#help_topic' => 'style',
  242. '#type' => 'select',
  243. '#options' => $style_options,
  244. );
  245. $style_form = &$form['displays']['block']['options']['style'];
  246. $style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array('block', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
  247. // Changing this dropdown updates $form['displays']['block']['options'] via
  248. // AJAX.
  249. views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'block', 'options'));
  250. $this->build_form_style($form, $form_state, 'block');
  251. $form['displays']['block']['options']['items_per_page'] = array(
  252. '#title' => t('Items per page'),
  253. '#type' => 'textfield',
  254. '#default_value' => '5',
  255. '#size' => 5,
  256. '#element_validate' => array('views_element_validate_integer'),
  257. );
  258. $form['displays']['block']['options']['pager'] = array(
  259. '#title' => t('Use a pager'),
  260. '#type' => 'checkbox',
  261. '#default_value' => FALSE,
  262. );
  263. return $form;
  264. }
  265. /**
  266. * Build the part of the form that builds the display format options.
  267. */
  268. protected function build_form_style(&$form, &$form_state, $type) {
  269. $style_form =& $form['displays'][$type]['options']['style'];
  270. $style = $style_form['style_plugin']['#default_value'];
  271. $style_plugin = views_get_plugin('style', $style);
  272. if (isset($style_plugin) && $style_plugin->uses_row_plugin()) {
  273. $options = $this->row_style_options($type);
  274. $style_form['row_plugin'] = array(
  275. '#type' => 'select',
  276. '#title' => t('of'),
  277. '#options' => $options,
  278. '#access' => count($options) > 1,
  279. );
  280. // For the block display, the default value should be "titles (linked)",
  281. // if it's available (since that's the most common use case).
  282. $block_with_linked_titles_available = ($type == 'block' && isset($options['titles_linked']));
  283. $default_value = $block_with_linked_titles_available ? 'titles_linked' : key($options);
  284. $style_form['row_plugin']['#default_value'] = views_ui_get_selected($form_state, array($type, 'style', 'row_plugin'), $default_value, $style_form['row_plugin']);
  285. // Changing this dropdown updates the individual row options via AJAX.
  286. views_ui_add_ajax_trigger($style_form, 'row_plugin', array('displays', $type, 'options', 'style', 'row_options'));
  287. // This is the region that can be updated by AJAX. The base class doesn't
  288. // add anything here, but child classes can.
  289. $style_form['row_options'] = array(
  290. '#theme_wrappers' => array('container'),
  291. );
  292. }
  293. elseif ($style_plugin->uses_fields()) {
  294. $style_form['row_plugin'] = array('#markup' => '<span>' . t('of fields') . '</span>');
  295. }
  296. }
  297. /**
  298. * Add possible row style options.
  299. *
  300. * Per default use fields with base field.
  301. */
  302. protected function row_style_options($type) {
  303. $data = views_fetch_data($this->base_table);
  304. // Get all available row plugins by default.
  305. $options = views_fetch_plugin_names('row', 'normal', array($this->base_table));
  306. return $options;
  307. }
  308. /**
  309. * Build the part of the form that allows the user to select the view's filters.
  310. *
  311. * By default, this adds "of type" and "tagged with" filters (when they are
  312. * available).
  313. */
  314. protected function build_filters(&$form, &$form_state) {
  315. // Find all the fields we are allowed to filter by.
  316. $fields = views_fetch_fields($this->base_table, 'filter');
  317. $entity_info = $this->entity_info;
  318. // If the current base table support bundles and has more than one (like user).
  319. if (isset($entity_info['bundle keys']) && isset($entity_info['bundles'])) {
  320. // Get all bundles and their human readable names.
  321. $options = array('all' => t('All'));
  322. foreach ($entity_info['bundles'] as $type => $bundle) {
  323. $options[$type] = $bundle['label'];
  324. }
  325. $form['displays']['show']['type'] = array(
  326. '#type' => 'select',
  327. '#title' => t('of type'),
  328. '#options' => $options,
  329. );
  330. $selected_bundle = views_ui_get_selected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']);
  331. $form['displays']['show']['type']['#default_value'] = $selected_bundle;
  332. // Changing this dropdown updates the entire content of $form['displays']
  333. // via AJAX, since each bundle might have entirely different fields
  334. // attached to it, etc.
  335. views_ui_add_ajax_trigger($form['displays']['show'], 'type', array('displays'));
  336. }
  337. // Check if we are allowed to filter by taxonomy, and if so, add the
  338. // "tagged with" filter to the view.
  339. //
  340. // We construct this filter using taxonomy_index.tid (which limits the
  341. // filtering to a specific vocabulary) rather than taxonomy_term_data.name
  342. // (which matches terms in any vocabulary). This is because it is a more
  343. // commonly-used filter that works better with the autocomplete UI, and
  344. // also to avoid confusion with other vocabularies on the site that may
  345. // have terms with the same name but are not used for free tagging.
  346. //
  347. // The downside is that if there *is* more than one vocabulary on the site
  348. // that is used for free tagging, the wizard will only be able to make the
  349. // "tagged with" filter apply to one of them (see below for the method it
  350. // uses to choose).
  351. if (isset($fields['taxonomy_index.tid'])) {
  352. // Check if this view will be displaying fieldable entities.
  353. if (!empty($entity_info['fieldable'])) {
  354. // Find all "tag-like" taxonomy fields associated with the view's
  355. // entities. If a particular entity type (i.e., bundle) has been
  356. // selected above, then we only search for taxonomy fields associated
  357. // with that bundle. Otherwise, we use all bundles.
  358. $bundles = array_keys($entity_info['bundles']);
  359. // Double check that this is a real bundle before using it (since above
  360. // we added a dummy option 'all' to the bundle list on the form).
  361. if (isset($selected_bundle) && in_array($selected_bundle, $bundles)) {
  362. $bundles = array($selected_bundle);
  363. }
  364. $tag_fields = array();
  365. foreach ($bundles as $bundle) {
  366. foreach (field_info_instances($this->entity_type, $bundle) as $instance) {
  367. // We define "tag-like" taxonomy fields as ones that use the
  368. // "Autocomplete term widget (tagging)" widget.
  369. if ($instance['widget']['type'] == 'taxonomy_autocomplete') {
  370. $tag_fields[] = $instance['field_name'];
  371. }
  372. }
  373. }
  374. $tag_fields = array_unique($tag_fields);
  375. if (!empty($tag_fields)) {
  376. // If there is more than one "tag-like" taxonomy field available to
  377. // the view, we can only make our filter apply to one of them (as
  378. // described above). We choose 'field_tags' if it is available, since
  379. // that is created by the Standard install profile in core and also
  380. // commonly used by contrib modules; thus, it is most likely to be
  381. // associated with the "main" free-tagging vocabulary on the site.
  382. if (in_array('field_tags', $tag_fields)) {
  383. $tag_field_name = 'field_tags';
  384. }
  385. else {
  386. $tag_field_name = reset($tag_fields);
  387. }
  388. // Add the autocomplete textfield to the wizard.
  389. $form['displays']['show']['tagged_with'] = array(
  390. '#type' => 'textfield',
  391. '#title' => t('tagged with'),
  392. '#autocomplete_path' => 'taxonomy/autocomplete/' . $tag_field_name,
  393. '#size' => 30,
  394. '#maxlength' => 1024,
  395. '#field_name' => $tag_field_name,
  396. '#element_validate' => array('views_ui_taxonomy_autocomplete_validate'),
  397. );
  398. }
  399. }
  400. }
  401. }
  402. /**
  403. * Build the part of the form that allows the user to select the view's sort order.
  404. *
  405. * By default, this adds a "sorted by [date]" filter (when it is available).
  406. */
  407. protected function build_sorts(&$form, &$form_state) {
  408. $sorts = array(
  409. 'none' => t('Unsorted'),
  410. );
  411. // Check if we are allowed to sort by creation date.
  412. if (!empty($this->plugin['created_column'])) {
  413. $sorts += array(
  414. $this->plugin['created_column'] . ':DESC' => t('Newest first'),
  415. $this->plugin['created_column'] . ':ASC' => t('Oldest first'),
  416. );
  417. }
  418. if (isset($this->plugin['available_sorts'])) {
  419. $sorts += $this->plugin['available_sorts'];
  420. }
  421. // If there is no sorts option available continue.
  422. if (!empty($sorts)) {
  423. $form['displays']['show']['sort'] = array(
  424. '#type' => 'select',
  425. '#title' => t('sorted by'),
  426. '#options' => $sorts,
  427. '#default_value' => isset($this->plugin['created_column']) ? $this->plugin['created_column'] . ':DESC' : 'none',
  428. );
  429. }
  430. }
  431. protected function instantiate_view($form, &$form_state) {
  432. // Build the basic view properties.
  433. $view = views_new_view();
  434. $view->name = $form_state['values']['name'];
  435. $view->human_name = $form_state['values']['human_name'];
  436. $view->description = $form_state['values']['description'];
  437. $view->tag = 'default';
  438. $view->core = VERSION;
  439. $view->base_table = $this->base_table;
  440. // Build all display options for this view.
  441. $display_options = $this->build_display_options($form, $form_state);
  442. // Allow the fully built options to be altered. This happens before adding
  443. // the options to the view, so that once they are eventually added we will
  444. // be able to get all the overrides correct.
  445. $this->alter_display_options($display_options, $form, $form_state);
  446. $this->add_displays($view, $display_options, $form, $form_state);
  447. return $view;
  448. }
  449. /**
  450. * Build an array of display options for the view.
  451. *
  452. * @return
  453. * An array whose keys are the names of each display and whose values are
  454. * arrays of options for that display.
  455. */
  456. protected function build_display_options($form, $form_state) {
  457. // Display: Master
  458. $display_options['default'] = $this->default_display_options($form, $form_state);
  459. $display_options['default'] += array(
  460. 'filters' => array(),
  461. 'sorts' => array(),
  462. );
  463. $display_options['default']['filters'] += $this->default_display_filters($form, $form_state);
  464. $display_options['default']['sorts'] += $this->default_display_sorts($form, $form_state);
  465. // Display: Page
  466. if (!empty($form_state['values']['page']['create'])) {
  467. $display_options['page'] = $this->page_display_options($form, $form_state);
  468. // Display: Feed (attached to the page)
  469. if (!empty($form_state['values']['page']['feed'])) {
  470. $display_options['feed'] = $this->page_feed_display_options($form, $form_state);
  471. }
  472. }
  473. // Display: Block
  474. if (!empty($form_state['values']['block']['create'])) {
  475. $display_options['block'] = $this->block_display_options($form, $form_state);
  476. }
  477. return $display_options;
  478. }
  479. /**
  480. * Alter the full array of display options before they are added to the view.
  481. */
  482. protected function alter_display_options(&$display_options, $form, $form_state) {
  483. // If any of the displays use jump menus, we want to add fields to the view
  484. // that store the path that will be used in the jump menu. The fields to
  485. // use for this are defined by the plugin.
  486. if (isset($this->plugin['path_field'])) {
  487. $path_field = $this->plugin['path_field'];
  488. $path_fields_added = FALSE;
  489. foreach ($display_options as $display_type => $options) {
  490. if (!empty($options['style_plugin']) && $options['style_plugin'] == 'jump_menu') {
  491. // Regardless of how many displays have jump menus, we only need to
  492. // add a single set of path fields to the view.
  493. if (!$path_fields_added) {
  494. // The plugin might provide supplemental fields that it needs to
  495. // generate the path (for example, node revisions need the node ID
  496. // as well as the revision ID). We need to add these first so they
  497. // are available as replacement patterns in the main path field.
  498. $path_fields = !empty($this->plugin['path_fields_supplemental']) ? $this->plugin['path_fields_supplemental'] : array();
  499. $path_fields[] = &$path_field;
  500. // Generate a unique ID for each field so we don't overwrite
  501. // existing ones.
  502. foreach ($path_fields as &$field) {
  503. $field['id'] = view::generate_item_id($field['id'], $display_options['default']['fields']);
  504. $display_options['default']['fields'][$field['id']] = $field;
  505. }
  506. $path_fields_added = TRUE;
  507. }
  508. // Configure the style plugin to use the path field to generate the
  509. // jump menu path.
  510. $display_options[$display_type]['style_options']['path'] = $path_field['id'];
  511. }
  512. }
  513. }
  514. // If any of the displays use the table style, take sure that the fields
  515. // always have a labels by unsetting the override.
  516. foreach ($display_options as &$options) {
  517. if ($options['style_plugin'] == 'table') {
  518. foreach ($display_options['default']['fields'] as &$field) {
  519. unset($field['label']);
  520. }
  521. }
  522. }
  523. }
  524. /**
  525. * Add the array of display options to the view, with appropriate overrides.
  526. */
  527. protected function add_displays($view, $display_options, $form, $form_state) {
  528. // Display: Master
  529. $default_display = $view->new_display('default', 'Master', 'default');
  530. foreach ($display_options['default'] as $option => $value) {
  531. $default_display->set_option($option, $value);
  532. }
  533. // Display: Page
  534. if (isset($display_options['page'])) {
  535. $display = $view->new_display('page', 'Page', 'page');
  536. // The page display is usually the main one (from the user's point of
  537. // view). Its options should therefore become the overall view defaults,
  538. // so that new displays which are added later automatically inherit them.
  539. $this->set_default_options($display_options['page'], $display, $default_display);
  540. // Display: Feed (attached to the page)
  541. if (isset($display_options['feed'])) {
  542. $display = $view->new_display('feed', 'Feed', 'feed');
  543. $this->set_override_options($display_options['feed'], $display, $default_display);
  544. }
  545. }
  546. // Display: Block
  547. if (isset($display_options['block'])) {
  548. $display = $view->new_display('block', 'Block', 'block');
  549. // When there is no page, the block display options should become the
  550. // overall view defaults.
  551. if (!isset($display_options['page'])) {
  552. $this->set_default_options($display_options['block'], $display, $default_display);
  553. }
  554. else {
  555. $this->set_override_options($display_options['block'], $display, $default_display);
  556. }
  557. }
  558. }
  559. /**
  560. * Most subclasses will need to override this method to provide some fields
  561. * or a different row plugin.
  562. */
  563. protected function default_display_options($form, $form_state) {
  564. $display_options = array();
  565. $display_options['access']['type'] = 'none';
  566. $display_options['cache']['type'] = 'none';
  567. $display_options['query']['type'] = 'views_query';
  568. $display_options['exposed_form']['type'] = 'basic';
  569. $display_options['pager']['type'] = 'full';
  570. $display_options['style_plugin'] = 'default';
  571. $display_options['row_plugin'] = 'fields';
  572. // Add a least one field so the view validates and the user has already a preview.
  573. // Therefore the basefield could provide 'defaults][field]' in it's base settings.
  574. // If there is nothing like this choose the first field with a field handler.
  575. $data = views_fetch_data($this->base_table);
  576. if (isset($data['table']['base']['defaults']['field'])) {
  577. $field = $data['table']['base']['defaults']['field'];
  578. }
  579. else {
  580. foreach ($data as $field => $field_data) {
  581. if (isset($field_data['field']['handler'])) {
  582. break;
  583. }
  584. }
  585. }
  586. $display_options['fields'][$field] = array(
  587. 'table' => $this->base_table,
  588. 'field' => $field,
  589. 'id' => $field,
  590. );
  591. return $display_options;
  592. }
  593. protected function default_display_filters($form, $form_state) {
  594. $filters = array();
  595. // Add any filters provided by the plugin.
  596. if (isset($this->plugin['filters'])) {
  597. foreach ($this->plugin['filters'] as $name => $info) {
  598. $filters[$name] = $info;
  599. }
  600. }
  601. // Add any filters specified by the user when filling out the wizard.
  602. $filters = array_merge($filters, $this->default_display_filters_user($form, $form_state));
  603. return $filters;
  604. }
  605. protected function default_display_filters_user($form, $form_state) {
  606. $filters = array();
  607. if (!empty($form_state['values']['show']['type']) && $form_state['values']['show']['type'] != 'all') {
  608. $bundle_key = $this->entity_info['bundle keys']['bundle'];
  609. // Figure out the table where $bundle_key lives. It may not be the same as
  610. // the base table for the view; the taxonomy vocabulary machine_name, for
  611. // example, is stored in taxonomy_vocabulary, not taxonomy_term_data.
  612. $fields = views_fetch_fields($this->base_table, 'filter');
  613. if (isset($fields[$this->base_table . '.' . $bundle_key])) {
  614. $table = $this->base_table;
  615. }
  616. else {
  617. foreach ($fields as $field_name => $value) {
  618. if ($pos = strpos($field_name, '.' . $bundle_key)) {
  619. $table = substr($field_name, 0, $pos);
  620. break;
  621. }
  622. }
  623. }
  624. $table_data = views_fetch_data($table);
  625. // Check whether the bundle key filter handler is or an child of it views_handler_filter_in_operator
  626. // If it's not just use a single value instead of an array.
  627. $handler = $table_data[$bundle_key]['filter']['handler'];
  628. if ($handler == 'views_handler_filter_in_operator' || is_subclass_of($handler, 'views_handler_filter_in_operator')) {
  629. $value = drupal_map_assoc(array($form_state['values']['show']['type']));
  630. }
  631. else {
  632. $value = $form_state['values']['show']['type'];
  633. }
  634. $filters[$bundle_key] = array(
  635. 'id' => $bundle_key,
  636. 'table' => $table,
  637. 'field' => $bundle_key,
  638. 'value' => $value,
  639. );
  640. }
  641. // @todo: Figure out why this isn't part of node_views_wizard.
  642. if (!empty($form_state['values']['show']['tagged_with']['tids'])) {
  643. $filters['tid'] = array(
  644. 'id' => 'tid',
  645. 'table' => 'taxonomy_index',
  646. 'field' => 'tid',
  647. 'value' => $form_state['values']['show']['tagged_with']['tids'],
  648. 'vocabulary' => $form_state['values']['show']['tagged_with']['vocabulary'],
  649. );
  650. // If the user entered more than one valid term in the autocomplete
  651. // field, they probably intended both of them to be applied.
  652. if (count($form_state['values']['show']['tagged_with']['tids']) > 1) {
  653. $filters['tid']['operator'] = 'and';
  654. // Sort the terms so the filter will be displayed as it normally would
  655. // on the edit screen.
  656. sort($filters['tid']['value']);
  657. }
  658. }
  659. return $filters;
  660. }
  661. protected function default_display_sorts($form, $form_state) {
  662. $sorts = array();
  663. // Add any sorts provided by the plugin.
  664. if (isset($this->plugin['sorts'])) {
  665. foreach ($this->plugin['sorts'] as $name => $info) {
  666. $sorts[$name] = $info;
  667. }
  668. }
  669. // Add any sorts specified by the user when filling out the wizard.
  670. $sorts = array_merge($sorts, $this->default_display_sorts_user($form, $form_state));
  671. return $sorts;
  672. }
  673. protected function default_display_sorts_user($form, $form_state) {
  674. $sorts = array();
  675. // Don't add a sort if there is no form value or the user selected none as sort.
  676. if (!empty($form_state['values']['show']['sort']) && $form_state['values']['show']['sort'] != 'none') {
  677. list($column, $sort) = explode(':', $form_state['values']['show']['sort']);
  678. // Column either be a column-name or the table-columnn-ame.
  679. $column = explode('-', $column);
  680. if (count($column) > 1) {
  681. $table = $column[0];
  682. $column = $column[1];
  683. }
  684. else {
  685. $table = $this->base_table;
  686. $column = $column[0];
  687. }
  688. $sorts[$column] = array(
  689. 'id' => $column,
  690. 'table' => $table,
  691. 'field' => $column,
  692. 'order' => $sort,
  693. );
  694. }
  695. return $sorts;
  696. }
  697. protected function page_display_options($form, $form_state) {
  698. $display_options = array();
  699. $page = $form_state['values']['page'];
  700. $display_options['title'] = $page['title'];
  701. $display_options['path'] = $page['path'];
  702. $display_options['style_plugin'] = $page['style']['style_plugin'];
  703. // Not every style plugin supports row style plugins.
  704. $display_options['row_plugin'] = isset($page['style']['row_plugin']) ? $page['style']['row_plugin'] : 'fields';
  705. if (empty($page['items_per_page'])) {
  706. $display_options['pager']['type'] = 'none';
  707. }
  708. elseif ($page['pager']) {
  709. $display_options['pager']['type'] = 'full';
  710. }
  711. else {
  712. $display_options['pager']['type'] = 'some';
  713. }
  714. $display_options['pager']['options']['items_per_page'] = $page['items_per_page'];
  715. if (!empty($page['link'])) {
  716. $display_options['menu']['type'] = 'normal';
  717. $display_options['menu']['title'] = $page['link_properties']['title'];
  718. $display_options['menu']['name'] = $page['link_properties']['menu_name'];
  719. }
  720. return $display_options;
  721. }
  722. protected function block_display_options($form, $form_state) {
  723. $display_options = array();
  724. $block = $form_state['values']['block'];
  725. $display_options['title'] = $block['title'];
  726. $display_options['style_plugin'] = $block['style']['style_plugin'];
  727. $display_options['row_plugin'] = isset($block['style']['row_plugin']) ? $block['style']['row_plugin'] : 'fields';
  728. $display_options['pager']['type'] = $block['pager'] ? 'full' : (empty($block['items_per_page']) ? 'none' : 'some');
  729. $display_options['pager']['options']['items_per_page'] = $block['items_per_page'];
  730. return $display_options;
  731. }
  732. protected function page_feed_display_options($form, $form_state) {
  733. $display_options = array();
  734. $display_options['pager']['type'] = 'some';
  735. $display_options['style_plugin'] = 'rss';
  736. $display_options['row_plugin'] = $form_state['values']['page']['feed_properties']['row_plugin'];
  737. $display_options['path'] = $form_state['values']['page']['feed_properties']['path'];
  738. $display_options['title'] = $form_state['values']['page']['title'];
  739. $display_options['displays'] = array(
  740. 'default' => 'default',
  741. 'page' => 'page',
  742. );
  743. return $display_options;
  744. }
  745. /**
  746. * Sets options for a display and makes them the default options if possible.
  747. *
  748. * This function can be used to set options for a display when it is desired
  749. * that the options also become the defaults for the view whenever possible.
  750. * This should be done for the "primary" display created in the view wizard,
  751. * so that new displays which the user adds later will be similar to this
  752. * one.
  753. *
  754. * @param $options
  755. * An array whose keys are the name of each option and whose values are the
  756. * desired values to set.
  757. * @param $display
  758. * The display which the options will be applied to. The default display
  759. * will actually be assigned the options (and this display will inherit
  760. * them) when possible.
  761. * @param $default_display
  762. * The default display, which will store the options when possible.
  763. */
  764. protected function set_default_options($options, $display, $default_display) {
  765. foreach ($options as $option => $value) {
  766. // If the default display supports this option, set the value there.
  767. // Otherwise, set it on the provided display.
  768. $default_value = $default_display->get_option($option);
  769. if (isset($default_value)) {
  770. $default_display->set_option($option, $value);
  771. }
  772. else {
  773. $display->set_option($option, $value);
  774. }
  775. }
  776. }
  777. /**
  778. * Sets options for a display, inheriting from the defaults when possible.
  779. *
  780. * This function can be used to set options for a display when it is desired
  781. * that the options inherit from the default display whenever possible. This
  782. * avoids setting too many options as overrides, which will be harder for the
  783. * user to modify later. For example, if $this->set_default_options() was
  784. * previously called on a page display and then this function is called on a
  785. * block display, and if the user entered the same title for both displays in
  786. * the views wizard, then the view will wind up with the title stored as the
  787. * default (with the page and block both inheriting from it).
  788. *
  789. * @param $options
  790. * An array whose keys are the name of each option and whose values are the
  791. * desired values.
  792. * @param $display
  793. * The display which the options will apply to. It will get the options by
  794. * inheritance from the default display when possible.
  795. * @param $default_display
  796. * The default display, from which the options will be inherited when
  797. * possible.
  798. */
  799. protected function set_override_options($options, $display, $default_display) {
  800. foreach ($options as $option => $value) {
  801. // Only override the default value if it is different from the value that
  802. // was provided.
  803. $default_value = $default_display->get_option($option);
  804. if (!isset($default_value)) {
  805. $display->set_option($option, $value);
  806. }
  807. elseif ($default_value !== $value) {
  808. $display->override_option($option, $value);
  809. }
  810. }
  811. }
  812. protected function retrieve_validated_view($form, $form_state, $unset = TRUE) {
  813. $key = hash('sha256', serialize($form_state['values']));
  814. $view = (isset($this->validated_views[$key]) ? $this->validated_views[$key] : NULL);
  815. if ($unset) {
  816. unset($this->validated_views[$key]);
  817. }
  818. return $view;
  819. }
  820. protected function set_validated_view($form, $form_state, $view) {
  821. $key = hash('sha256', serialize($form_state['values']));
  822. $this->validated_views[$key] = $view;
  823. }
  824. /**
  825. * Instantiates a view and validates values.
  826. */
  827. function validate($form, &$form_state) {
  828. $view = $this->instantiate_view($form, $form_state);
  829. $errors = $view->validate();
  830. if (!is_array($errors) || empty($errors)) {
  831. $this->set_validated_view($form, $form_state, $view);
  832. return array();
  833. }
  834. return $errors;
  835. }
  836. /**
  837. * Create a View from values that have been already submitted to validate().
  838. *
  839. * @throws ViewsWizardException if the values have not been validated.
  840. */
  841. function create_view($form, &$form_state) {
  842. $view = $this->retrieve_validated_view($form, $form_state);
  843. if (empty($view)) {
  844. throw new ViewsWizardException(t('Attempted to create_view with values that have not been validated'));
  845. }
  846. return $view;
  847. }
  848. }