• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

plugins/views_wizard/views_ui_base_views_wizard.class.php

00001 <?php
00002 
00003 interface ViewsWizardInterface {
00004   function __construct($plugin);
00005 
00009   function build_form($form, &$form_state);
00010 
00016   function validate($form, &$form_state);
00017 
00025   function create_view($form, &$form_state);
00026 }
00027 
00031 class ViewsWizardException extends Exception {
00032 }
00033 
00037 class ViewsUiBaseViewsWizard implements ViewsWizardInterface {
00038   protected $base_table;
00039   protected $entity_type;
00040   protected $entity_info = array();
00041   protected $validated_views = array();
00042   protected $plugin = array();
00043   protected $filter_defaults = array(
00044     'id' => NULL,
00045     'expose' => array('operator' => FALSE),
00046     'group' => 1,
00047   );
00048 
00049   function __construct($plugin) {
00050     $this->base_table = $plugin['base_table'];
00051     $default = $this->filter_defaults;
00052 
00053     if (isset($plugin['filters'])) {
00054       foreach ($plugin['filters'] as $name => $info) {
00055         $default['id'] = $name;
00056         $plugin['filters'][$name] = $info + $default;
00057       }
00058     }
00059 
00060     $this->plugin = $plugin;
00061 
00062     $entities = entity_get_info();
00063     foreach ($entities as $entity_type => $entity_info) {
00064       if (isset($entity_info['base table']) && $this->base_table == $entity_info['base table']) {
00065         $this->entity_info = $entity_info;
00066         $this->entity_type = $entity_type;
00067       }
00068     }
00069   }
00070 
00071   function build_form($form, &$form_state) {
00072     $style_options = views_fetch_plugin_names('style', 'normal', array($this->base_table));
00073     $feed_row_options = views_fetch_plugin_names('row', 'feed', array($this->base_table));
00074     $path_prefix = url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q=');
00075 
00076     // Add filters and sorts which apply to the view as a whole.
00077     $this->build_filters($form, $form_state);
00078     $this->build_sorts($form, $form_state);
00079 
00080     $form['displays']['page'] = array(
00081       '#type' => 'fieldset',
00082       '#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend'),),
00083       '#tree' => TRUE,
00084     );
00085     $form['displays']['page']['create'] = array(
00086       '#title' => t('Create a page'),
00087       '#type' => 'checkbox',
00088       '#attributes' => array('class' => array('strong')),
00089       '#default_value' => TRUE,
00090       '#id' => 'edit-page-create',
00091     );
00092 
00093     // All options for the page display are included in this container so they
00094     // can be hidden en masse when the "Create a page" checkbox is unchecked.
00095     $form['displays']['page']['options'] = array(
00096       '#type' => 'container',
00097       '#attributes' => array('class' => array('options-set'),),
00098       '#dependency' => array(
00099         'edit-page-create' => array(1),
00100       ),
00101       '#pre_render' => array('ctools_dependent_pre_render'),
00102       '#prefix' => '<div><div id="edit-page-wrapper">',
00103       '#suffix' => '</div></div>',
00104       '#parents' => array('page'),
00105     );
00106 
00107     $form['displays']['page']['options']['title'] = array(
00108       '#title' => t('Page title'),
00109       '#type' => 'textfield',
00110     );
00111     $form['displays']['page']['options']['path'] = array(
00112       '#title' => t('Path'),
00113       '#type' => 'textfield',
00114       '#field_prefix' => $path_prefix,
00115     );
00116     $form['displays']['page']['options']['style'] = array(
00117       '#type' => 'fieldset',
00118       '#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
00119     );
00120 
00121     // Create the dropdown for choosing the display format.
00122     $form['displays']['page']['options']['style']['style_plugin'] = array(
00123       '#title' => t('Display format'),
00124       '#help_topic' => 'style',
00125       '#type' => 'select',
00126       '#options' => $style_options,
00127     );
00128     $style_form = &$form['displays']['page']['options']['style'];
00129     $style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array('page', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
00130     // Changing this dropdown updates $form['displays']['page']['options'] via
00131     // AJAX.
00132     views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'page', 'options'));
00133 
00134     $this->build_form_style($form, $form_state, 'page');
00135     $form['displays']['page']['options']['items_per_page'] = array(
00136       '#title' => t('Items to display'),
00137       '#type' => 'textfield',
00138       '#default_value' => '10',
00139       '#size' => 5,
00140       '#element_validate' => array('views_element_validate_integer'),
00141     );
00142     $form['displays']['page']['options']['pager'] = array(
00143       '#title' => t('Use a pager'),
00144       '#type' => 'checkbox',
00145       '#default_value' => TRUE,
00146     );
00147     $form['displays']['page']['options']['link'] = array(
00148       '#title' => t('Create a menu link'),
00149       '#type' => 'checkbox',
00150       '#id' => 'edit-page-link',
00151     );
00152     $form['displays']['page']['options']['link_properties'] = array(
00153       '#type' => 'container',
00154       '#dependency' => array(
00155         'edit-page-link' => array(1),
00156       ),
00157       '#pre_render' => array('ctools_dependent_pre_render'),
00158       '#prefix' => '<div id="edit-page-link-properties-wrapper">',
00159       '#suffix' => '</div>',
00160     );
00161     if (module_exists('menu')) {
00162       $menu_options = menu_get_menus();
00163     }
00164     else {
00165       // These are not yet translated.
00166       $menu_options = menu_list_system_menus();
00167       foreach ($menu_options as $name => $title) {
00168         $menu_options[$name] = t($title);
00169       }
00170     }
00171     $form['displays']['page']['options']['link_properties']['menu_name'] = array(
00172       '#title' => t('Menu'),
00173       '#type' => 'select',
00174       '#options' => $menu_options,
00175     );
00176     $form['displays']['page']['options']['link_properties']['title'] = array(
00177       '#title' => t('Link text'),
00178       '#type' => 'textfield',
00179     );
00180     // Only offer a feed if we have at least one available feed row style.
00181     if ($feed_row_options) {
00182       $form['displays']['page']['options']['feed'] = array(
00183         '#title' => t('Include an RSS feed'),
00184         '#type' => 'checkbox',
00185         '#id' => 'edit-page-feed',
00186       );
00187       $form['displays']['page']['options']['feed_properties'] = array(
00188         '#type' => 'container',
00189         '#dependency' => array(
00190           'edit-page-feed' => array(1),
00191         ),
00192         '#pre_render' => array('ctools_dependent_pre_render'),
00193         '#prefix' => '<div id="edit-page-feed-properties-wrapper">',
00194         '#suffix' => '</div>',
00195       );
00196       $form['displays']['page']['options']['feed_properties']['path'] = array(
00197         '#title' => t('Feed path'),
00198         '#type' => 'textfield',
00199         '#field_prefix' => $path_prefix,
00200       );
00201       // This will almost never be visible.
00202       $form['displays']['page']['options']['feed_properties']['row_plugin'] = array(
00203         '#title' => t('Feed row style'),
00204         '#type' => 'select',
00205         '#options' => $feed_row_options,
00206         '#default_value' => key($feed_row_options),
00207         '#access' => (count($feed_row_options) > 1),
00208         '#dependency' => array(
00209           'edit-page-feed' => array(1),
00210         ),
00211         '#pre_render' => array('ctools_dependent_pre_render'),
00212         '#prefix' => '<div id="edit-page-feed-properties-row-plugin-wrapper">',
00213         '#suffix' => '</div>',
00214       );
00215     }
00216 
00217     $form['displays']['block'] = array(
00218       '#type' => 'fieldset',
00219       '#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend'),),
00220       '#tree' => TRUE,
00221     );
00222     $form['displays']['block']['create'] = array(
00223       '#title' => t('Create a block'),
00224       '#type' => 'checkbox',
00225       '#attributes' => array('class' => array('strong')),
00226       '#id' => 'edit-block-create',
00227     );
00228 
00229     // All options for the block display are included in this container so they
00230     // can be hidden en masse when the "Create a block" checkbox is unchecked.
00231     $form['displays']['block']['options'] = array(
00232       '#type' => 'container',
00233       '#attributes' => array('class' => array('options-set'),),
00234       '#dependency' => array(
00235         'edit-block-create' => array(1),
00236       ),
00237       '#pre_render' => array('ctools_dependent_pre_render'),
00238       '#prefix' => '<div id="edit-block-wrapper">',
00239       '#suffix' => '</div>',
00240       '#parents' => array('block'),
00241     );
00242 
00243     $form['displays']['block']['options']['title'] = array(
00244       '#title' => t('Block title'),
00245       '#type' => 'textfield',
00246     );
00247     $form['displays']['block']['options']['style'] = array(
00248       '#type' => 'fieldset',
00249       '#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
00250     );
00251 
00252     // Create the dropdown for choosing the display format.
00253     $form['displays']['block']['options']['style']['style_plugin'] = array(
00254       '#title' => t('Display format'),
00255       '#help_topic' => 'style',
00256       '#type' => 'select',
00257       '#options' => $style_options,
00258     );
00259     $style_form = &$form['displays']['block']['options']['style'];
00260     $style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array('block', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
00261     // Changing this dropdown updates $form['displays']['block']['options'] via
00262     // AJAX.
00263     views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'block', 'options'));
00264 
00265     $this->build_form_style($form, $form_state, 'block');
00266     $form['displays']['block']['options']['items_per_page'] = array(
00267       '#title' => t('Items per page'),
00268       '#type' => 'textfield',
00269       '#default_value' => '5',
00270       '#size' => 5,
00271       '#element_validate' => array('views_element_validate_integer'),
00272     );
00273     $form['displays']['block']['options']['pager'] = array(
00274       '#title' => t('Use a pager'),
00275       '#type' => 'checkbox',
00276       '#default_value' => FALSE,
00277     );
00278 
00279     return $form;
00280   }
00281 
00285   protected function build_form_style(&$form, &$form_state, $type) {
00286     $style_form =& $form['displays'][$type]['options']['style'];
00287     $style = $style_form['style_plugin']['#default_value'];
00288     $style_plugin = views_get_plugin('style', $style);
00289     if (isset($style_plugin) && $style_plugin->uses_row_plugin()) {
00290       $options = $this->row_style_options($type);
00291       $style_form['row_plugin'] = array(
00292         '#type' => 'select',
00293         '#title' => t('of'),
00294         '#options' => $options,
00295         '#access' => count($options) > 1,
00296       );
00297       // For the block display, the default value should be "titles (linked)",
00298       // if it's available (since that's the most common use case).
00299       $block_with_linked_titles_available = ($type == 'block' && isset($options['titles_linked']));
00300       $default_value = $block_with_linked_titles_available ? 'titles_linked' : key($options);
00301       $style_form['row_plugin']['#default_value'] = views_ui_get_selected($form_state, array($type, 'style', 'row_plugin'), $default_value, $style_form['row_plugin']);
00302       // Changing this dropdown updates the individual row options via AJAX.
00303       views_ui_add_ajax_trigger($style_form, 'row_plugin', array('displays', $type, 'options', 'style', 'row_options'));
00304 
00305       // This is the region that can be updated by AJAX. The base class doesn't
00306       // add anything here, but child classes can.
00307       $style_form['row_options'] = array(
00308         '#theme_wrappers' => array('container'),
00309       );
00310     }
00311     elseif ($style_plugin->uses_fields()) {
00312       $style_form['row_plugin'] = array('#markup' => '<span>' . t('of fields') . '</span>');
00313     }
00314   }
00315 
00321   protected function row_style_options($type) {
00322     $data = views_fetch_data($this->base_table);
00323     // Get all available row plugins by default.
00324     $options = views_fetch_plugin_names('row', 'normal', array($this->base_table));
00325     return $options;
00326   }
00327 
00334   protected function build_filters(&$form, &$form_state) {
00335     // Find all the fields we are allowed to filter by.
00336     $fields = views_fetch_fields($this->base_table, 'filter');
00337 
00338     $entity_info = $this->entity_info;
00339     // If the current base table support bundles and has more than one (like user).
00340     if (isset($entity_info['bundle keys']) && isset($entity_info['bundles'])) {
00341       // Get all bundles and their human readable names.
00342       $options = array('all' => t('All'));
00343       foreach ($entity_info['bundles'] as $type => $bundle) {
00344         $options[$type] = $bundle['label'];
00345       }
00346       $form['displays']['show']['type'] = array(
00347         '#type' => 'select',
00348         '#title' => t('of type'),
00349         '#options' => $options,
00350       );
00351       $selected_bundle = views_ui_get_selected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']);
00352       $form['displays']['show']['type']['#default_value'] = $selected_bundle;
00353       // Changing this dropdown updates the entire content of $form['displays']
00354       // via AJAX, since each bundle might have entirely different fields
00355       // attached to it, etc.
00356       views_ui_add_ajax_trigger($form['displays']['show'], 'type', array('displays'));
00357     }
00358 
00359     // Check if we are allowed to filter by taxonomy, and if so, add the
00360     // "tagged with" filter to the view.
00361     //
00362     // We construct this filter using taxonomy_index.tid (which limits the
00363     // filtering to a specific vocabulary) rather than taxonomy_term_data.name
00364     // (which matches terms in any vocabulary). This is because it is a more
00365     // commonly-used filter that works better with the autocomplete UI, and
00366     // also to avoid confusion with other vocabularies on the site that may
00367     // have terms with the same name but are not used for free tagging.
00368     //
00369     // The downside is that if there *is* more than one vocabulary on the site
00370     // that is used for free tagging, the wizard will only be able to make the
00371     // "tagged with" filter apply to one of them (see below for the method it
00372     // uses to choose).
00373     if (isset($fields['taxonomy_index.tid'])) {
00374       // Check if this view will be displaying fieldable entities.
00375       if (!empty($entity_info['fieldable'])) {
00376         // Find all "tag-like" taxonomy fields associated with the view's
00377         // entities. If a particular entity type (i.e., bundle) has been
00378         // selected above, then we only search for taxonomy fields associated
00379         // with that bundle. Otherwise, we use all bundles.
00380         $bundles = array_keys($entity_info['bundles']);
00381         // Double check that this is a real bundle before using it (since above
00382         // we added a dummy option 'all' to the bundle list on the form).
00383         if (isset($selected_bundle) && in_array($selected_bundle, $bundles)) {
00384           $bundles = array($selected_bundle);
00385         }
00386         $tag_fields = array();
00387         foreach ($bundles as $bundle) {
00388           foreach (field_info_instances($this->entity_type, $bundle) as $instance) {
00389             // We define "tag-like" taxonomy fields as ones that use the
00390             // "Autocomplete term widget (tagging)" widget.
00391             if ($instance['widget']['type'] == 'taxonomy_autocomplete') {
00392               $tag_fields[] = $instance['field_name'];
00393             }
00394           }
00395         }
00396         $tag_fields = array_unique($tag_fields);
00397         if (!empty($tag_fields)) {
00398           // If there is more than one "tag-like" taxonomy field available to
00399           // the view, we can only make our filter apply to one of them (as
00400           // described above). We choose 'field_tags' if it is available, since
00401           // that is created by the Standard install profile in core and also
00402           // commonly used by contrib modules; thus, it is most likely to be
00403           // associated with the "main" free-tagging vocabulary on the site.
00404           if (in_array('field_tags', $tag_fields)) {
00405             $tag_field_name = 'field_tags';
00406           }
00407           else {
00408             $tag_field_name = reset($tag_fields);
00409           }
00410           // Add the autocomplete textfield to the wizard.
00411           $form['displays']['show']['tagged_with'] = array(
00412             '#type' => 'textfield',
00413             '#title' => t('tagged with'),
00414             '#autocomplete_path' => 'taxonomy/autocomplete/' . $tag_field_name,
00415             '#size' => 30,
00416             '#maxlength' => 1024,
00417             '#field_name' => $tag_field_name,
00418             '#element_validate' => array('views_ui_taxonomy_autocomplete_validate'),
00419           );
00420         }
00421       }
00422     }
00423   }
00424 
00430   protected function build_sorts(&$form, &$form_state) {
00431     $sorts = array(
00432       'none' => t('Unsorted'),
00433     );
00434     // Check if we are allowed to sort by creation date.
00435     if (!empty($this->plugin['created_column'])) {
00436       $sorts += array(
00437         $this->plugin['created_column'] . ':DESC' => t('Newest first'),
00438         $this->plugin['created_column'] . ':ASC' => t('Oldest first'),
00439       );
00440     }
00441     if (isset($this->plugin['available_sorts'])) {
00442       $sorts += $this->plugin['available_sorts'];
00443     }
00444 
00445     // If there is no sorts option available continue.
00446     if (!empty($sorts)) {
00447       $form['displays']['show']['sort'] = array(
00448         '#type' => 'select',
00449         '#title' => t('sorted by'),
00450         '#options' => $sorts,
00451         '#default_value' => isset($this->plugin['created_column']) ? $this->plugin['created_column'] . ':DESC' : 'none',
00452       );
00453     }
00454   }
00455 
00456   protected function instantiate_view($form, &$form_state) {
00457     // Build the basic view properties.
00458     $view = views_new_view();
00459     $view->name = $form_state['values']['name'];
00460     $view->human_name = $form_state['values']['human_name'];
00461     $view->description = $form_state['values']['description'];
00462     $view->tag = 'default';
00463     $view->core = VERSION;
00464     $view->base_table = $this->base_table;
00465 
00466     // Build all display options for this view.
00467     $display_options = $this->build_display_options($form, $form_state);
00468 
00469     // Allow the fully built options to be altered. This happens before adding
00470     // the options to the view, so that once they are eventually added we will
00471     // be able to get all the overrides correct.
00472     $this->alter_display_options($display_options, $form, $form_state);
00473 
00474     $this->add_displays($view, $display_options, $form, $form_state);
00475 
00476     return $view;
00477   }
00478 
00486   protected function build_display_options($form, $form_state) {
00487     // Display: Master
00488     $display_options['default'] = $this->default_display_options($form, $form_state);
00489     $display_options['default'] += array(
00490       'filters' => array(),
00491       'sorts' => array(),
00492     );
00493     $display_options['default']['filters'] += $this->default_display_filters($form, $form_state);
00494     $display_options['default']['sorts'] += $this->default_display_sorts($form, $form_state);
00495 
00496     // Display: Page
00497     if (!empty($form_state['values']['page']['create'])) {
00498       $display_options['page'] = $this->page_display_options($form, $form_state);
00499 
00500       // Display: Feed (attached to the page)
00501       if (!empty($form_state['values']['page']['feed'])) {
00502         $display_options['feed'] = $this->page_feed_display_options($form, $form_state);
00503       }
00504     }
00505 
00506     // Display: Block
00507     if (!empty($form_state['values']['block']['create'])) {
00508       $display_options['block'] = $this->block_display_options($form, $form_state);
00509     }
00510 
00511     return $display_options;
00512   }
00513 
00517   protected function alter_display_options(&$display_options, $form, $form_state) {
00518     // If any of the displays use jump menus, we want to add fields to the view
00519     // that store the path that will be used in the jump menu. The fields to
00520     // use for this are defined by the plugin.
00521     if (isset($this->plugin['path_field'])) {
00522       $path_field = $this->plugin['path_field'];
00523       $path_fields_added = FALSE;
00524       foreach ($display_options as $display_type => $options) {
00525         if (!empty($options['style_plugin']) && $options['style_plugin'] == 'jump_menu') {
00526           // Regardless of how many displays have jump menus, we only need to
00527           // add a single set of path fields to the view.
00528           if (!$path_fields_added) {
00529             // The plugin might provide supplemental fields that it needs to
00530             // generate the path (for example, node revisions need the node ID
00531             // as well as the revision ID). We need to add these first so they
00532             // are available as replacement patterns in the main path field.
00533             $path_fields = !empty($this->plugin['path_fields_supplemental']) ? $this->plugin['path_fields_supplemental'] : array();
00534             $path_fields[] = &$path_field;
00535 
00536             // Generate a unique ID for each field so we don't overwrite
00537             // existing ones.
00538             foreach ($path_fields as &$field) {
00539               $field['id'] = view::generate_item_id($field['id'], $display_options['default']['fields']);
00540               $display_options['default']['fields'][$field['id']] = $field;
00541             }
00542 
00543             $path_fields_added = TRUE;
00544           }
00545 
00546           // Configure the style plugin to use the path field to generate the
00547           // jump menu path.
00548           $display_options[$display_type]['style_options']['path'] = $path_field['id'];
00549         }
00550       }
00551     }
00552   }
00553 
00557   protected function add_displays($view, $display_options, $form, $form_state) {
00558     // Display: Master
00559     $default_display = $view->new_display('default', 'Master', 'default');
00560     foreach ($display_options['default'] as $option => $value) {
00561       $default_display->set_option($option, $value);
00562     }
00563 
00564     // Display: Page
00565     if (isset($display_options['page'])) {
00566       $display = $view->new_display('page', 'Page', 'page');
00567       // The page display is usually the main one (from the user's point of
00568       // view). Its options should therefore become the overall view defaults,
00569       // so that new displays which are added later automatically inherit them.
00570       $this->set_default_options($display_options['page'], $display, $default_display);
00571 
00572       // Display: Feed (attached to the page)
00573       if (isset($display_options['feed'])) {
00574         $display = $view->new_display('feed', 'Feed', 'feed');
00575         $this->set_override_options($display_options['feed'], $display, $default_display);
00576       }
00577     }
00578 
00579     // Display: Block
00580     if (isset($display_options['block'])) {
00581       $display = $view->new_display('block', 'Block', 'block');
00582       // When there is no page, the block display options should become the
00583       // overall view defaults.
00584       if (!isset($display_options['page'])) {
00585         $this->set_default_options($display_options['block'], $display, $default_display);
00586       }
00587       else {
00588         $this->set_override_options($display_options['block'], $display, $default_display);
00589       }
00590     }
00591   }
00592 
00597   protected function default_display_options($form, $form_state) {
00598     $display_options = array();
00599     $display_options['access']['type'] = 'none';
00600     $display_options['cache']['type'] = 'none';
00601     $display_options['query']['type'] = 'views_query';
00602     $display_options['exposed_form']['type'] = 'basic';
00603     $display_options['pager']['type'] = 'full';
00604     $display_options['style_plugin'] = 'default';
00605     $display_options['row_plugin'] = 'fields';
00606 
00607     // Add a least one field so the view validates and the user has already a preview.
00608     // Therefore the basefield could provide 'defaults][field]' in it's base settings.
00609     // If there is nothing like this choose the first field with a field handler.
00610     $data = views_fetch_data($this->base_table);
00611     if (isset($data['table']['base']['defaults']['field'])) {
00612       $field = $data['table']['base']['defaults']['field'];
00613     }
00614     else {
00615       foreach ($data as $field => $field_data) {
00616         if (isset($field_data['field']['handler'])) {
00617           break;
00618         }
00619       }
00620     }
00621     $display_options['fields'][$field] = array(
00622       'table' => $this->base_table,
00623       'field' => $field,
00624       'id' => $field,
00625     );
00626 
00627     return $display_options;
00628   }
00629 
00630   protected function default_display_filters($form, $form_state) {
00631     $filters = array();
00632 
00633     // Add any filters provided by the plugin.
00634     if (isset($this->plugin['filters'])) {
00635       foreach ($this->plugin['filters'] as $name => $info) {
00636         $filters[$name] = $info;
00637       }
00638     }
00639 
00640     // Add any filters specified by the user when filling out the wizard.
00641     $filters = array_merge($filters, $this->default_display_filters_user($form, $form_state));
00642 
00643     return $filters;
00644   }
00645 
00646   protected function default_display_filters_user($form, $form_state) {
00647     $filters = array();
00648 
00649     if (!empty($form_state['values']['show']['type']) && $form_state['values']['show']['type'] != 'all') {
00650       $bundle_key = $this->entity_info['bundle keys']['bundle'];
00651       // Figure out the table where $bundle_key lives. It may not be the same as
00652       // the base table for the view; the taxonomy vocabulary machine_name, for
00653       // example, is stored in taxonomy_vocabulary, not taxonomy_term_data.
00654       $fields = views_fetch_fields($this->base_table, 'filter');
00655       if (isset($fields[$this->base_table . '.' . $bundle_key])) {
00656         $table = $this->base_table;
00657       }
00658       else {
00659         foreach ($fields as $field_name => $value) {
00660           if ($pos = strpos($field_name, '.' . $bundle_key)) {
00661             $table = substr($field_name, 0, $pos);
00662             break;
00663           }
00664         }
00665       }
00666       $table_data = views_fetch_data($table);
00667       // Check whether the bundle key filter handler is or an child of it views_handler_filter_in_operator
00668       // If it's not just use a single value instead of an array.
00669       $handler = $table_data[$bundle_key]['filter']['handler'];
00670       if ($handler == 'views_handler_filter_in_operator' || is_subclass_of($handler, 'views_handler_filter_in_operator')) {
00671         $value = drupal_map_assoc(array($form_state['values']['show']['type']));
00672       }
00673       else {
00674         $value = $form_state['values']['show']['type'];
00675       }
00676 
00677       $filters[$bundle_key] = array(
00678         'id' => $bundle_key,
00679         'table' => $table,
00680         'field' => $bundle_key,
00681         'value' => $value,
00682       );
00683     }
00684 
00685     // @todo: Figure out why this isn't part of node_views_wizard.
00686     if (!empty($form_state['values']['show']['tagged_with']['tids'])) {
00687       $filters['tid'] = array(
00688         'id' => 'tid',
00689         'table' => 'taxonomy_index',
00690         'field' => 'tid',
00691         'value' => $form_state['values']['show']['tagged_with']['tids'],
00692         'vocabulary' => $form_state['values']['show']['tagged_with']['vocabulary'],
00693       );
00694       // If the user entered more than one valid term in the autocomplete
00695       // field, they probably intended both of them to be applied.
00696       if (count($form_state['values']['show']['tagged_with']['tids']) > 1) {
00697         $filters['tid']['operator'] = 'and';
00698         // Sort the terms so the filter will be displayed as it normally would
00699         // on the edit screen.
00700         sort($filters['tid']['value']);
00701       }
00702     }
00703 
00704     return $filters;
00705   }
00706 
00707   protected function default_display_sorts($form, $form_state) {
00708     $sorts = array();
00709 
00710     // Add any sorts provided by the plugin.
00711     if (isset($this->plugin['sorts'])) {
00712       foreach ($this->plugin['sorts'] as $name => $info) {
00713         $sorts[$name] = $info;
00714       }
00715     }
00716 
00717     // Add any sorts specified by the user when filling out the wizard.
00718     $sorts = array_merge($sorts, $this->default_display_sorts_user($form, $form_state));
00719 
00720     return $sorts;
00721   }
00722 
00723   protected function default_display_sorts_user($form, $form_state) {
00724     $sorts = array();
00725 
00726     // Don't add a sort if there is no form value or the user selected none as sort.
00727     if (!empty($form_state['values']['show']['sort']) && $form_state['values']['show']['sort'] != 'none') {
00728       list($column, $sort) = explode(':', $form_state['values']['show']['sort']);
00729       // Column either be a column-name or the table-columnn-ame.
00730       $column = explode('-', $column);
00731       if (count($column) > 1) {
00732         $table = $column[0];
00733         $column = $column[1];
00734       }
00735       else {
00736         $table = $this->base_table;
00737         $column = $column[0];
00738       }
00739 
00740       $sorts[$column] = array(
00741         'id' => $column,
00742         'table' => $table,
00743         'field' => $column,
00744         'order' => $sort,
00745       );
00746     }
00747 
00748     return $sorts;
00749   }
00750 
00751   protected function page_display_options($form, $form_state) {
00752     $display_options = array();
00753     $page = $form_state['values']['page'];
00754     $display_options['title'] = $page['title'];
00755     $display_options['path'] = $page['path'];
00756     $display_options['style_plugin'] = $page['style']['style_plugin'];
00757     // Not every style plugin supports row style plugins.
00758     $display_options['row_plugin'] = isset($page['style']['row_plugin']) ? $page['style']['row_plugin'] : 'fields';
00759     if (empty($page['items_per_page'])) {
00760       $display_options['pager']['type'] = 'none';
00761     }
00762     elseif ($page['pager']) {
00763       $display_options['pager']['type'] = 'full';
00764     }
00765     else {
00766       $display_options['pager']['type'] = 'some';
00767     }
00768     $display_options['pager']['options']['items_per_page'] = $page['items_per_page'];
00769     if (!empty($page['link'])) {
00770       $display_options['menu']['type'] = 'normal';
00771       $display_options['menu']['title'] = $page['link_properties']['title'];
00772       $display_options['menu']['name'] = $page['link_properties']['menu_name'];
00773     }
00774     return $display_options;
00775   }
00776 
00777   protected function block_display_options($form, $form_state) {
00778     $display_options = array();
00779     $block = $form_state['values']['block'];
00780     $display_options['title'] = $block['title'];
00781     $display_options['style_plugin'] = $block['style']['style_plugin'];
00782     $display_options['row_plugin'] = isset($block['style']['row_plugin']) ? $block['style']['row_plugin'] : 'fields';
00783     $display_options['pager']['type'] = $block['pager'] ? 'full' : (empty($block['items_per_page']) ? 'none' : 'some');
00784     $display_options['pager']['options']['items_per_page'] = $block['items_per_page'];
00785     return $display_options;
00786   }
00787 
00788   protected function page_feed_display_options($form, $form_state) {
00789     $display_options = array();
00790     $display_options['pager']['type'] = 'some';
00791     $display_options['style_plugin'] = 'rss';
00792     $display_options['row_plugin'] = $form_state['values']['page']['feed_properties']['row_plugin'];
00793     $display_options['path'] = $form_state['values']['page']['feed_properties']['path'];
00794     $display_options['title'] = $form_state['values']['page']['title'];
00795     $display_options['displays'] = array(
00796       'default' => 'default',
00797       'page' => 'page',
00798     );
00799     return $display_options;
00800   }
00801 
00821   protected function set_default_options($options, $display, $default_display) {
00822     foreach ($options as $option => $value) {
00823       // If the default display supports this option, set the value there.
00824       // Otherwise, set it on the provided display.
00825       $default_value = $default_display->get_option($option);
00826       if (isset($default_value)) {
00827         $default_display->set_option($option, $value);
00828       }
00829       else {
00830         $display->set_option($option, $value);
00831       }
00832     }
00833   }
00834 
00857   protected function set_override_options($options, $display, $default_display) {
00858     foreach ($options as $option => $value) {
00859       // Only override the default value if it is different from the value that
00860       // was provided.
00861       $default_value = $default_display->get_option($option);
00862       if (!isset($default_value)) {
00863         $display->set_option($option, $value);
00864       }
00865       elseif ($default_value !== $value) {
00866         $display->override_option($option, $value);
00867       }
00868     }
00869   }
00870 
00871   protected function retrieve_validated_view($form, $form_state, $unset = TRUE) {
00872     $key = hash('sha256', serialize($form_state['values']));
00873     $view = (isset($this->validated_views[$key]) ? $this->validated_views[$key] : NULL);
00874     if ($unset) {
00875       unset($this->validated_views[$key]);
00876     }
00877     return $view;
00878   }
00879 
00880   protected function set_validated_view($form, $form_state, $view) {
00881     $key = hash('sha256', serialize($form_state['values']));
00882     $this->validated_views[$key] = $view;
00883   }
00884 
00888   function validate($form, &$form_state) {
00889     $view = $this->instantiate_view($form, $form_state);
00890     $errors = $view->validate();
00891     if (!is_array($errors) || empty($errors)) {
00892       $this->set_validated_view($form, $form_state, $view);
00893       return array();
00894     }
00895     return $errors;
00896   }
00897 
00903  function create_view($form, &$form_state) {
00904    $view = $this->retrieve_validated_view($form, $form_state);
00905    if (empty($view)) {
00906      throw new ViewsWizardException(t('Attempted to create_view with values that have not been validated'));
00907    }
00908    return $view;
00909  }
00910 
00911 }

Generated on Sun Feb 26 2012 12:52:52 for Views by  doxygen 1.7.1