Projet

Général

Profil

Paste
Télécharger (35,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / plugins / views_wizard / views_ui_base_views_wizard.class.php @ a2bb1a14

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides the interface and base class for Views Wizard plugins.
6
 */
7

    
8
/**
9
 * Defines a common interface for Views Wizard plugins.
10
 */
11
interface ViewsWizardInterface {
12
  function __construct($plugin);
13

    
14
  /**
15
   * For AJAX callbacks to build other elements in the "show" form.
16
   */
17
  function build_form($form, &$form_state);
18

    
19
  /**
20
   * Validate form and values.
21
   *
22
   * @return an array of form errors.
23
   */
24
  function validate($form, &$form_state);
25

    
26
  /**
27
   * Create a new View from form values.
28
   *
29
   * @return a view object.
30
   *
31
   * @throws ViewsWizardException in the event of a problem.
32
   */
33
  function create_view($form, &$form_state);
34
}
35

    
36
/**
37
 * A custom exception class for our errors.
38
 */
39
class ViewsWizardException extends Exception {
40
}
41

    
42
/**
43
 * A very generic Views Wizard class - can be constructed for any base table.
44
 */
45
class ViewsUiBaseViewsWizard implements ViewsWizardInterface {
46
  protected $base_table;
47
  protected $entity_type;
48
  protected $entity_info = array();
49
  protected $validated_views = array();
50
  protected $plugin = array();
51
  protected $filter_defaults = array(
52
    'id' => NULL,
53
    'expose' => array('operator' => FALSE),
54
    'group' => 1,
55
  );
56

    
57
  function __construct($plugin) {
58
    $this->base_table = $plugin['base_table'];
59
    $default = $this->filter_defaults;
60

    
61
    if (isset($plugin['filters'])) {
62
      foreach ($plugin['filters'] as $name => $info) {
63
        $default['id'] = $name;
64
        $plugin['filters'][$name] = $info + $default;
65
      }
66
    }
67

    
68
    $this->plugin = $plugin;
69

    
70
    $entities = entity_get_info();
71
    foreach ($entities as $entity_type => $entity_info) {
72
      if (isset($entity_info['base table']) && $this->base_table == $entity_info['base table']) {
73
        $this->entity_info = $entity_info;
74
        $this->entity_type = $entity_type;
75
      }
76
    }
77
  }
78

    
79
  function build_form($form, &$form_state) {
80
    $style_options = views_fetch_plugin_names('style', 'normal', array($this->base_table));
81
    $feed_row_options = views_fetch_plugin_names('row', 'feed', array($this->base_table));
82
    $path_prefix = url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q=');
83

    
84
    // Add filters and sorts which apply to the view as a whole.
85
    $this->build_filters($form, $form_state);
86
    $this->build_sorts($form, $form_state);
87

    
88
    $form['displays']['page'] = array(
89
      '#type' => 'fieldset',
90
      '#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend'),),
91
      '#tree' => TRUE,
92
    );
93
    $form['displays']['page']['create'] = array(
94
      '#title' => t('Create a page'),
95
      '#type' => 'checkbox',
96
      '#attributes' => array('class' => array('strong')),
97
      '#default_value' => TRUE,
98
      '#id' => 'edit-page-create',
99
    );
100

    
101
    // All options for the page display are included in this container so they
102
    // can be hidden en masse when the "Create a page" checkbox is unchecked.
103
    $form['displays']['page']['options'] = array(
104
      '#type' => 'container',
105
      '#attributes' => array('class' => array('options-set'),),
106
      '#dependency' => array(
107
        'edit-page-create' => array(1),
108
      ),
109
      '#pre_render' => array('ctools_dependent_pre_render'),
110
      '#prefix' => '<div><div id="edit-page-wrapper">',
111
      '#suffix' => '</div></div>',
112
      '#parents' => array('page'),
113
    );
114

    
115
    $form['displays']['page']['options']['title'] = array(
116
      '#title' => t('Page title'),
117
      '#type' => 'textfield',
118
    );
119
    $form['displays']['page']['options']['path'] = array(
120
      '#title' => t('Path'),
121
      '#type' => 'textfield',
122
      '#field_prefix' => $path_prefix,
123
    );
124
    $form['displays']['page']['options']['style'] = array(
125
      '#type' => 'fieldset',
126
      '#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
127
    );
128

    
129
    // Create the dropdown for choosing the display format.
130
    $form['displays']['page']['options']['style']['style_plugin'] = array(
131
      '#title' => t('Display format'),
132
      '#help_topic' => 'style',
133
      '#type' => 'select',
134
      '#options' => $style_options,
135
    );
136
    $style_form = &$form['displays']['page']['options']['style'];
137
    $style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array('page', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
138
    // Changing this dropdown updates $form['displays']['page']['options'] via
139
    // AJAX.
140
    views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'page', 'options'));
141

    
142
    $this->build_form_style($form, $form_state, 'page');
143
    $form['displays']['page']['options']['items_per_page'] = array(
144
      '#title' => t('Items to display'),
145
      '#type' => 'textfield',
146
      '#default_value' => '10',
147
      '#size' => 5,
148
      '#element_validate' => array('views_element_validate_integer'),
149
    );
150
    $form['displays']['page']['options']['pager'] = array(
151
      '#title' => t('Use a pager'),
152
      '#type' => 'checkbox',
153
      '#default_value' => TRUE,
154
    );
155
    $form['displays']['page']['options']['link'] = array(
156
      '#title' => t('Create a menu link'),
157
      '#type' => 'checkbox',
158
      '#id' => 'edit-page-link',
159
    );
160
    $form['displays']['page']['options']['link_properties'] = array(
161
      '#type' => 'container',
162
      '#dependency' => array(
163
        'edit-page-link' => array(1),
164
      ),
165
      '#pre_render' => array('ctools_dependent_pre_render'),
166
      '#prefix' => '<div id="edit-page-link-properties-wrapper">',
167
      '#suffix' => '</div>',
168
    );
169
    if (module_exists('menu')) {
170
      $menu_options = menu_get_menus();
171
    }
172
    else {
173
      // These are not yet translated.
174
      $menu_options = menu_list_system_menus();
175
      foreach ($menu_options as $name => $title) {
176
        $menu_options[$name] = t($title);
177
      }
178
    }
179
    $form['displays']['page']['options']['link_properties']['menu_name'] = array(
180
      '#title' => t('Menu'),
181
      '#type' => 'select',
182
      '#options' => $menu_options,
183
    );
184
    $form['displays']['page']['options']['link_properties']['title'] = array(
185
      '#title' => t('Link text'),
186
      '#type' => 'textfield',
187
    );
188
    // Only offer a feed if we have at least one available feed row style.
189
    if ($feed_row_options) {
190
      $form['displays']['page']['options']['feed'] = array(
191
        '#title' => t('Include an RSS feed'),
192
        '#type' => 'checkbox',
193
        '#id' => 'edit-page-feed',
194
      );
195
      $form['displays']['page']['options']['feed_properties'] = array(
196
        '#type' => 'container',
197
        '#dependency' => array(
198
          'edit-page-feed' => array(1),
199
        ),
200
        '#pre_render' => array('ctools_dependent_pre_render'),
201
        '#prefix' => '<div id="edit-page-feed-properties-wrapper">',
202
        '#suffix' => '</div>',
203
      );
204
      $form['displays']['page']['options']['feed_properties']['path'] = array(
205
        '#title' => t('Feed path'),
206
        '#type' => 'textfield',
207
        '#field_prefix' => $path_prefix,
208
      );
209
      // This will almost never be visible.
210
      $form['displays']['page']['options']['feed_properties']['row_plugin'] = array(
211
        '#title' => t('Feed row style'),
212
        '#type' => 'select',
213
        '#options' => $feed_row_options,
214
        '#default_value' => key($feed_row_options),
215
        '#access' => (count($feed_row_options) > 1),
216
        '#dependency' => array(
217
          'edit-page-feed' => array(1),
218
        ),
219
        '#pre_render' => array('ctools_dependent_pre_render'),
220
        '#prefix' => '<div id="edit-page-feed-properties-row-plugin-wrapper">',
221
        '#suffix' => '</div>',
222
      );
223
    }
224

    
225
    $form['displays']['block'] = array(
226
      '#type' => 'fieldset',
227
      '#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend'),),
228
      '#tree' => TRUE,
229
    );
230
    $form['displays']['block']['create'] = array(
231
      '#title' => t('Create a block'),
232
      '#type' => 'checkbox',
233
      '#attributes' => array('class' => array('strong')),
234
      '#id' => 'edit-block-create',
235
    );
236

    
237
    // All options for the block display are included in this container so they
238
    // can be hidden en masse when the "Create a block" checkbox is unchecked.
239
    $form['displays']['block']['options'] = array(
240
      '#type' => 'container',
241
      '#attributes' => array('class' => array('options-set'),),
242
      '#dependency' => array(
243
        'edit-block-create' => array(1),
244
      ),
245
      '#pre_render' => array('ctools_dependent_pre_render'),
246
      '#prefix' => '<div id="edit-block-wrapper">',
247
      '#suffix' => '</div>',
248
      '#parents' => array('block'),
249
    );
250

    
251
    $form['displays']['block']['options']['title'] = array(
252
      '#title' => t('Block title'),
253
      '#type' => 'textfield',
254
    );
255
    $form['displays']['block']['options']['style'] = array(
256
      '#type' => 'fieldset',
257
      '#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
258
    );
259

    
260
    // Create the dropdown for choosing the display format.
261
    $form['displays']['block']['options']['style']['style_plugin'] = array(
262
      '#title' => t('Display format'),
263
      '#help_topic' => 'style',
264
      '#type' => 'select',
265
      '#options' => $style_options,
266
    );
267
    $style_form = &$form['displays']['block']['options']['style'];
268
    $style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array('block', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
269
    // Changing this dropdown updates $form['displays']['block']['options'] via
270
    // AJAX.
271
    views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'block', 'options'));
272

    
273
    $this->build_form_style($form, $form_state, 'block');
274
    $form['displays']['block']['options']['items_per_page'] = array(
275
      '#title' => t('Items per page'),
276
      '#type' => 'textfield',
277
      '#default_value' => '5',
278
      '#size' => 5,
279
      '#element_validate' => array('views_element_validate_integer'),
280
    );
281
    $form['displays']['block']['options']['pager'] = array(
282
      '#title' => t('Use a pager'),
283
      '#type' => 'checkbox',
284
      '#default_value' => FALSE,
285
    );
286

    
287
    return $form;
288
  }
289

    
290
  /**
291
   * Build the part of the form that builds the display format options.
292
   */
293
  protected function build_form_style(&$form, &$form_state, $type) {
294
    $style_form =& $form['displays'][$type]['options']['style'];
295
    $style = $style_form['style_plugin']['#default_value'];
296
    $style_plugin = views_get_plugin('style', $style);
297
    if (isset($style_plugin) && $style_plugin->uses_row_plugin()) {
298
      $options = $this->row_style_options($type);
299
      $style_form['row_plugin'] = array(
300
        '#type' => 'select',
301
        '#title' => t('of'),
302
        '#options' => $options,
303
        '#access' => count($options) > 1,
304
      );
305
      // For the block display, the default value should be "titles (linked)",
306
      // if it's available (since that's the most common use case).
307
      $block_with_linked_titles_available = ($type == 'block' && isset($options['titles_linked']));
308
      $default_value = $block_with_linked_titles_available ? 'titles_linked' : key($options);
309
      $style_form['row_plugin']['#default_value'] = views_ui_get_selected($form_state, array($type, 'style', 'row_plugin'), $default_value, $style_form['row_plugin']);
310
      // Changing this dropdown updates the individual row options via AJAX.
311
      views_ui_add_ajax_trigger($style_form, 'row_plugin', array('displays', $type, 'options', 'style', 'row_options'));
312

    
313
      // This is the region that can be updated by AJAX. The base class doesn't
314
      // add anything here, but child classes can.
315
      $style_form['row_options'] = array(
316
        '#theme_wrappers' => array('container'),
317
      );
318
    }
319
    elseif ($style_plugin->uses_fields()) {
320
      $style_form['row_plugin'] = array('#markup' => '<span>' . t('of fields') . '</span>');
321
    }
322
  }
323

    
324
  /**
325
   * Add possible row style options.
326
   *
327
   * Per default use fields with base field.
328
   */
329
  protected function row_style_options($type) {
330
    $data = views_fetch_data($this->base_table);
331
    // Get all available row plugins by default.
332
    $options = views_fetch_plugin_names('row', 'normal', array($this->base_table));
333
    return $options;
334
  }
335

    
336
  /**
337
   * Build the part of the form that allows the user to select the view's filters.
338
   *
339
   * By default, this adds "of type" and "tagged with" filters (when they are
340
   * available).
341
   */
342
  protected function build_filters(&$form, &$form_state) {
343
    // Find all the fields we are allowed to filter by.
344
    $fields = views_fetch_fields($this->base_table, 'filter');
345

    
346
    $entity_info = $this->entity_info;
347
    // If the current base table support bundles and has more than one (like user).
348
    if (isset($entity_info['bundle keys']) && isset($entity_info['bundles'])) {
349
      // Get all bundles and their human readable names.
350
      $options = array('all' => t('All'));
351
      foreach ($entity_info['bundles'] as $type => $bundle) {
352
        $options[$type] = $bundle['label'];
353
      }
354
      $form['displays']['show']['type'] = array(
355
        '#type' => 'select',
356
        '#title' => t('of type'),
357
        '#options' => $options,
358
      );
359
      $selected_bundle = views_ui_get_selected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']);
360
      $form['displays']['show']['type']['#default_value'] = $selected_bundle;
361
      // Changing this dropdown updates the entire content of $form['displays']
362
      // via AJAX, since each bundle might have entirely different fields
363
      // attached to it, etc.
364
      views_ui_add_ajax_trigger($form['displays']['show'], 'type', array('displays'));
365
    }
366

    
367
    // Check if we are allowed to filter by taxonomy, and if so, add the
368
    // "tagged with" filter to the view.
369
    //
370
    // We construct this filter using taxonomy_index.tid (which limits the
371
    // filtering to a specific vocabulary) rather than taxonomy_term_data.name
372
    // (which matches terms in any vocabulary). This is because it is a more
373
    // commonly-used filter that works better with the autocomplete UI, and
374
    // also to avoid confusion with other vocabularies on the site that may
375
    // have terms with the same name but are not used for free tagging.
376
    //
377
    // The downside is that if there *is* more than one vocabulary on the site
378
    // that is used for free tagging, the wizard will only be able to make the
379
    // "tagged with" filter apply to one of them (see below for the method it
380
    // uses to choose).
381
    if (isset($fields['taxonomy_index.tid'])) {
382
      // Check if this view will be displaying fieldable entities.
383
      if (!empty($entity_info['fieldable'])) {
384
        // Find all "tag-like" taxonomy fields associated with the view's
385
        // entities. If a particular entity type (i.e., bundle) has been
386
        // selected above, then we only search for taxonomy fields associated
387
        // with that bundle. Otherwise, we use all bundles.
388
        $bundles = array_keys($entity_info['bundles']);
389
        // Double check that this is a real bundle before using it (since above
390
        // we added a dummy option 'all' to the bundle list on the form).
391
        if (isset($selected_bundle) && in_array($selected_bundle, $bundles)) {
392
          $bundles = array($selected_bundle);
393
        }
394
        $tag_fields = array();
395
        foreach ($bundles as $bundle) {
396
          foreach (field_info_instances($this->entity_type, $bundle) as $instance) {
397
            // We define "tag-like" taxonomy fields as ones that use the
398
            // "Autocomplete term widget (tagging)" widget.
399
            if ($instance['widget']['type'] == 'taxonomy_autocomplete') {
400
              $tag_fields[] = $instance['field_name'];
401
            }
402
          }
403
        }
404
        $tag_fields = array_unique($tag_fields);
405
        if (!empty($tag_fields)) {
406
          // If there is more than one "tag-like" taxonomy field available to
407
          // the view, we can only make our filter apply to one of them (as
408
          // described above). We choose 'field_tags' if it is available, since
409
          // that is created by the Standard install profile in core and also
410
          // commonly used by contrib modules; thus, it is most likely to be
411
          // associated with the "main" free-tagging vocabulary on the site.
412
          if (in_array('field_tags', $tag_fields)) {
413
            $tag_field_name = 'field_tags';
414
          }
415
          else {
416
            $tag_field_name = reset($tag_fields);
417
          }
418
          // Add the autocomplete textfield to the wizard.
419
          $form['displays']['show']['tagged_with'] = array(
420
            '#type' => 'textfield',
421
            '#title' => t('tagged with'),
422
            '#autocomplete_path' => 'taxonomy/autocomplete/' . $tag_field_name,
423
            '#size' => 30,
424
            '#maxlength' => 1024,
425
            '#field_name' => $tag_field_name,
426
            '#element_validate' => array('views_ui_taxonomy_autocomplete_validate'),
427
          );
428
        }
429
      }
430
    }
431
  }
432

    
433
  /**
434
   * Build the part of the form that allows the user to select the view's sort order.
435
   *
436
   * By default, this adds a "sorted by [date]" filter (when it is available).
437
   */
438
  protected function build_sorts(&$form, &$form_state) {
439
    $sorts = array(
440
      'none' => t('Unsorted'),
441
    );
442
    // Check if we are allowed to sort by creation date.
443
    if (!empty($this->plugin['created_column'])) {
444
      $sorts += array(
445
        $this->plugin['created_column'] . ':DESC' => t('Newest first'),
446
        $this->plugin['created_column'] . ':ASC' => t('Oldest first'),
447
      );
448
    }
449
    if (isset($this->plugin['available_sorts'])) {
450
      $sorts += $this->plugin['available_sorts'];
451
    }
452

    
453
    // If there is no sorts option available continue.
454
    if (!empty($sorts)) {
455
      $form['displays']['show']['sort'] = array(
456
        '#type' => 'select',
457
        '#title' => t('sorted by'),
458
        '#options' => $sorts,
459
        '#default_value' => isset($this->plugin['created_column']) ? $this->plugin['created_column'] . ':DESC' : 'none',
460
      );
461
    }
462
  }
463

    
464
  protected function instantiate_view($form, &$form_state) {
465
    // Build the basic view properties.
466
    $view = views_new_view();
467
    $view->name = $form_state['values']['name'];
468
    $view->human_name = $form_state['values']['human_name'];
469
    $view->description = $form_state['values']['description'];
470
    $view->tag = 'default';
471
    $view->core = VERSION;
472
    $view->base_table = $this->base_table;
473

    
474
    // Build all display options for this view.
475
    $display_options = $this->build_display_options($form, $form_state);
476

    
477
    // Allow the fully built options to be altered. This happens before adding
478
    // the options to the view, so that once they are eventually added we will
479
    // be able to get all the overrides correct.
480
    $this->alter_display_options($display_options, $form, $form_state);
481

    
482
    $this->add_displays($view, $display_options, $form, $form_state);
483

    
484
    return $view;
485
  }
486

    
487
  /**
488
   * Build an array of display options for the view.
489
   *
490
   * @return
491
   *   An array whose keys are the names of each display and whose values are
492
   *   arrays of options for that display.
493
   */
494
  protected function build_display_options($form, $form_state) {
495
    // Display: Master
496
    $display_options['default'] = $this->default_display_options($form, $form_state);
497
    $display_options['default'] += array(
498
      'filters' => array(),
499
      'sorts' => array(),
500
    );
501
    $display_options['default']['filters'] += $this->default_display_filters($form, $form_state);
502
    $display_options['default']['sorts'] += $this->default_display_sorts($form, $form_state);
503

    
504
    // Display: Page
505
    if (!empty($form_state['values']['page']['create'])) {
506
      $display_options['page'] = $this->page_display_options($form, $form_state);
507

    
508
      // Display: Feed (attached to the page)
509
      if (!empty($form_state['values']['page']['feed'])) {
510
        $display_options['feed'] = $this->page_feed_display_options($form, $form_state);
511
      }
512
    }
513

    
514
    // Display: Block
515
    if (!empty($form_state['values']['block']['create'])) {
516
      $display_options['block'] = $this->block_display_options($form, $form_state);
517
    }
518

    
519
    return $display_options;
520
  }
521

    
522
  /**
523
   * Alter the full array of display options before they are added to the view.
524
   */
525
  protected function alter_display_options(&$display_options, $form, $form_state) {
526
    // If any of the displays use jump menus, we want to add fields to the view
527
    // that store the path that will be used in the jump menu. The fields to
528
    // use for this are defined by the plugin.
529
    if (isset($this->plugin['path_field'])) {
530
      $path_field = $this->plugin['path_field'];
531
      $path_fields_added = FALSE;
532
      foreach ($display_options as $display_type => $options) {
533
        if (!empty($options['style_plugin']) && $options['style_plugin'] == 'jump_menu') {
534
          // Regardless of how many displays have jump menus, we only need to
535
          // add a single set of path fields to the view.
536
          if (!$path_fields_added) {
537
            // The plugin might provide supplemental fields that it needs to
538
            // generate the path (for example, node revisions need the node ID
539
            // as well as the revision ID). We need to add these first so they
540
            // are available as replacement patterns in the main path field.
541
            $path_fields = !empty($this->plugin['path_fields_supplemental']) ? $this->plugin['path_fields_supplemental'] : array();
542
            $path_fields[] = &$path_field;
543

    
544
            // Generate a unique ID for each field so we don't overwrite
545
            // existing ones.
546
            foreach ($path_fields as &$field) {
547
              $field['id'] = view::generate_item_id($field['id'], $display_options['default']['fields']);
548
              $display_options['default']['fields'][$field['id']] = $field;
549
            }
550

    
551
            $path_fields_added = TRUE;
552
          }
553

    
554
          // Configure the style plugin to use the path field to generate the
555
          // jump menu path.
556
          $display_options[$display_type]['style_options']['path'] = $path_field['id'];
557
        }
558
      }
559
    }
560

    
561
    // If any of the displays use the table style, take sure that the fields
562
    // always have a labels by unsetting the override.
563
    foreach ($display_options as &$options) {
564
      if ($options['style_plugin'] == 'table') {
565
        foreach ($display_options['default']['fields'] as &$field) {
566
          unset($field['label']);
567
        }
568
      }
569
    }
570
  }
571

    
572
  /**
573
   * Add the array of display options to the view, with appropriate overrides.
574
   */
575
  protected function add_displays($view, $display_options, $form, $form_state) {
576
    // Display: Master
577
    $default_display = $view->new_display('default', 'Master', 'default');
578
    foreach ($display_options['default'] as $option => $value) {
579
      $default_display->set_option($option, $value);
580
    }
581

    
582
    // Display: Page
583
    if (isset($display_options['page'])) {
584
      $display = $view->new_display('page', 'Page', 'page');
585
      // The page display is usually the main one (from the user's point of
586
      // view). Its options should therefore become the overall view defaults,
587
      // so that new displays which are added later automatically inherit them.
588
      $this->set_default_options($display_options['page'], $display, $default_display);
589

    
590
      // Display: Feed (attached to the page)
591
      if (isset($display_options['feed'])) {
592
        $display = $view->new_display('feed', 'Feed', 'feed');
593
        $this->set_override_options($display_options['feed'], $display, $default_display);
594
      }
595
    }
596

    
597
    // Display: Block
598
    if (isset($display_options['block'])) {
599
      $display = $view->new_display('block', 'Block', 'block');
600
      // When there is no page, the block display options should become the
601
      // overall view defaults.
602
      if (!isset($display_options['page'])) {
603
        $this->set_default_options($display_options['block'], $display, $default_display);
604
      }
605
      else {
606
        $this->set_override_options($display_options['block'], $display, $default_display);
607
      }
608
    }
609
  }
610

    
611
  /**
612
   * Most subclasses will need to override this method to provide some fields
613
   * or a different row plugin.
614
   */
615
  protected function default_display_options($form, $form_state) {
616
    $display_options = array();
617
    $display_options['access']['type'] = 'none';
618
    $display_options['cache']['type'] = 'none';
619
    $display_options['query']['type'] = 'views_query';
620
    $display_options['exposed_form']['type'] = 'basic';
621
    $display_options['pager']['type'] = 'full';
622
    $display_options['style_plugin'] = 'default';
623
    $display_options['row_plugin'] = 'fields';
624

    
625
    // Add a least one field so the view validates and the user has already a preview.
626
    // Therefore the basefield could provide 'defaults][field]' in it's base settings.
627
    // If there is nothing like this choose the first field with a field handler.
628
    $data = views_fetch_data($this->base_table);
629
    if (isset($data['table']['base']['defaults']['field'])) {
630
      $field = $data['table']['base']['defaults']['field'];
631
    }
632
    else {
633
      foreach ($data as $field => $field_data) {
634
        if (isset($field_data['field']['handler'])) {
635
          break;
636
        }
637
      }
638
    }
639
    $display_options['fields'][$field] = array(
640
      'table' => $this->base_table,
641
      'field' => $field,
642
      'id' => $field,
643
    );
644

    
645
    return $display_options;
646
  }
647

    
648
  protected function default_display_filters($form, $form_state) {
649
    $filters = array();
650

    
651
    // Add any filters provided by the plugin.
652
    if (isset($this->plugin['filters'])) {
653
      foreach ($this->plugin['filters'] as $name => $info) {
654
        $filters[$name] = $info;
655
      }
656
    }
657

    
658
    // Add any filters specified by the user when filling out the wizard.
659
    $filters = array_merge($filters, $this->default_display_filters_user($form, $form_state));
660

    
661
    return $filters;
662
  }
663

    
664
  protected function default_display_filters_user($form, $form_state) {
665
    $filters = array();
666

    
667
    if (!empty($form_state['values']['show']['type']) && $form_state['values']['show']['type'] != 'all') {
668
      $bundle_key = $this->entity_info['bundle keys']['bundle'];
669
      // Figure out the table where $bundle_key lives. It may not be the same as
670
      // the base table for the view; the taxonomy vocabulary machine_name, for
671
      // example, is stored in taxonomy_vocabulary, not taxonomy_term_data.
672
      $fields = views_fetch_fields($this->base_table, 'filter');
673
      if (isset($fields[$this->base_table . '.' . $bundle_key])) {
674
        $table = $this->base_table;
675
      }
676
      else {
677
        foreach ($fields as $field_name => $value) {
678
          if ($pos = strpos($field_name, '.' . $bundle_key)) {
679
            $table = substr($field_name, 0, $pos);
680
            break;
681
          }
682
        }
683
      }
684
      $table_data = views_fetch_data($table);
685
      // Check whether the bundle key filter handler is or an child of it views_handler_filter_in_operator
686
      // If it's not just use a single value instead of an array.
687
      $handler = $table_data[$bundle_key]['filter']['handler'];
688
      if ($handler == 'views_handler_filter_in_operator' || is_subclass_of($handler, 'views_handler_filter_in_operator')) {
689
        $value = drupal_map_assoc(array($form_state['values']['show']['type']));
690
      }
691
      else {
692
        $value = $form_state['values']['show']['type'];
693
      }
694

    
695
      $filters[$bundle_key] = array(
696
        'id' => $bundle_key,
697
        'table' => $table,
698
        'field' => $bundle_key,
699
        'value' => $value,
700
      );
701
    }
702

    
703
    // @todo: Figure out why this isn't part of node_views_wizard.
704
    if (!empty($form_state['values']['show']['tagged_with']['tids'])) {
705
      $filters['tid'] = array(
706
        'id' => 'tid',
707
        'table' => 'taxonomy_index',
708
        'field' => 'tid',
709
        'value' => $form_state['values']['show']['tagged_with']['tids'],
710
        'vocabulary' => $form_state['values']['show']['tagged_with']['vocabulary'],
711
      );
712
      // If the user entered more than one valid term in the autocomplete
713
      // field, they probably intended both of them to be applied.
714
      if (count($form_state['values']['show']['tagged_with']['tids']) > 1) {
715
        $filters['tid']['operator'] = 'and';
716
        // Sort the terms so the filter will be displayed as it normally would
717
        // on the edit screen.
718
        sort($filters['tid']['value']);
719
      }
720
    }
721

    
722
    return $filters;
723
  }
724

    
725
  protected function default_display_sorts($form, $form_state) {
726
    $sorts = array();
727

    
728
    // Add any sorts provided by the plugin.
729
    if (isset($this->plugin['sorts'])) {
730
      foreach ($this->plugin['sorts'] as $name => $info) {
731
        $sorts[$name] = $info;
732
      }
733
    }
734

    
735
    // Add any sorts specified by the user when filling out the wizard.
736
    $sorts = array_merge($sorts, $this->default_display_sorts_user($form, $form_state));
737

    
738
    return $sorts;
739
  }
740

    
741
  protected function default_display_sorts_user($form, $form_state) {
742
    $sorts = array();
743

    
744
    // Don't add a sort if there is no form value or the user selected none as sort.
745
    if (!empty($form_state['values']['show']['sort']) && $form_state['values']['show']['sort'] != 'none') {
746
      list($column, $sort) = explode(':', $form_state['values']['show']['sort']);
747
      // Column either be a column-name or the table-columnn-ame.
748
      $column = explode('-', $column);
749
      if (count($column) > 1) {
750
        $table = $column[0];
751
        $column = $column[1];
752
      }
753
      else {
754
        $table = $this->base_table;
755
        $column = $column[0];
756
      }
757

    
758
      $sorts[$column] = array(
759
        'id' => $column,
760
        'table' => $table,
761
        'field' => $column,
762
        'order' => $sort,
763
      );
764
    }
765

    
766
    return $sorts;
767
  }
768

    
769
  protected function page_display_options($form, $form_state) {
770
    $display_options = array();
771
    $page = $form_state['values']['page'];
772
    $display_options['title'] = $page['title'];
773
    $display_options['path'] = $page['path'];
774
    $display_options['style_plugin'] = $page['style']['style_plugin'];
775
    // Not every style plugin supports row style plugins.
776
    $display_options['row_plugin'] = isset($page['style']['row_plugin']) ? $page['style']['row_plugin'] : 'fields';
777
    if (empty($page['items_per_page'])) {
778
      $display_options['pager']['type'] = 'none';
779
    }
780
    elseif ($page['pager']) {
781
      $display_options['pager']['type'] = 'full';
782
    }
783
    else {
784
      $display_options['pager']['type'] = 'some';
785
    }
786
    $display_options['pager']['options']['items_per_page'] = $page['items_per_page'];
787
    if (!empty($page['link'])) {
788
      $display_options['menu']['type'] = 'normal';
789
      $display_options['menu']['title'] = $page['link_properties']['title'];
790
      $display_options['menu']['name'] = $page['link_properties']['menu_name'];
791
    }
792
    return $display_options;
793
  }
794

    
795
  protected function block_display_options($form, $form_state) {
796
    $display_options = array();
797
    $block = $form_state['values']['block'];
798
    $display_options['title'] = $block['title'];
799
    $display_options['style_plugin'] = $block['style']['style_plugin'];
800
    $display_options['row_plugin'] = isset($block['style']['row_plugin']) ? $block['style']['row_plugin'] : 'fields';
801
    $display_options['pager']['type'] = $block['pager'] ? 'full' : (empty($block['items_per_page']) ? 'none' : 'some');
802
    $display_options['pager']['options']['items_per_page'] = $block['items_per_page'];
803
    return $display_options;
804
  }
805

    
806
  protected function page_feed_display_options($form, $form_state) {
807
    $display_options = array();
808
    $display_options['pager']['type'] = 'some';
809
    $display_options['style_plugin'] = 'rss';
810
    $display_options['row_plugin'] = $form_state['values']['page']['feed_properties']['row_plugin'];
811
    $display_options['path'] = $form_state['values']['page']['feed_properties']['path'];
812
    $display_options['title'] = $form_state['values']['page']['title'];
813
    $display_options['displays'] = array(
814
      'default' => 'default',
815
      'page' => 'page',
816
    );
817
    return $display_options;
818
  }
819

    
820
  /**
821
   * Sets options for a display and makes them the default options if possible.
822
   *
823
   * This function can be used to set options for a display when it is desired
824
   * that the options also become the defaults for the view whenever possible.
825
   * This should be done for the "primary" display created in the view wizard,
826
   * so that new displays which the user adds later will be similar to this
827
   * one.
828
   *
829
   * @param $options
830
   *   An array whose keys are the name of each option and whose values are the
831
   *   desired values to set.
832
   * @param $display
833
   *   The display which the options will be applied to. The default display
834
   *   will actually be assigned the options (and this display will inherit
835
   *   them) when possible.
836
   * @param $default_display
837
   *   The default display, which will store the options when possible.
838
   */
839
  protected function set_default_options($options, $display, $default_display) {
840
    foreach ($options as $option => $value) {
841
      // If the default display supports this option, set the value there.
842
      // Otherwise, set it on the provided display.
843
      $default_value = $default_display->get_option($option);
844
      if (isset($default_value)) {
845
        $default_display->set_option($option, $value);
846
      }
847
      else {
848
        $display->set_option($option, $value);
849
      }
850
    }
851
  }
852

    
853
  /**
854
   * Sets options for a display, inheriting from the defaults when possible.
855
   *
856
   * This function can be used to set options for a display when it is desired
857
   * that the options inherit from the default display whenever possible. This
858
   * avoids setting too many options as overrides, which will be harder for the
859
   * user to modify later. For example, if $this->set_default_options() was
860
   * previously called on a page display and then this function is called on a
861
   * block display, and if the user entered the same title for both displays in
862
   * the views wizard, then the view will wind up with the title stored as the
863
   * default (with the page and block both inheriting from it).
864
   *
865
   * @param $options
866
   *   An array whose keys are the name of each option and whose values are the
867
   *   desired values.
868
   * @param $display
869
   *   The display which the options will apply to. It will get the options by
870
   *   inheritance from the default display when possible.
871
   * @param $default_display
872
   *   The default display, from which the options will be inherited when
873
   *   possible.
874
   */
875
  protected function set_override_options($options, $display, $default_display) {
876
    foreach ($options as $option => $value) {
877
      // Only override the default value if it is different from the value that
878
      // was provided.
879
      $default_value = $default_display->get_option($option);
880
      if (!isset($default_value)) {
881
        $display->set_option($option, $value);
882
      }
883
      elseif ($default_value !== $value) {
884
        $display->override_option($option, $value);
885
      }
886
    }
887
  }
888

    
889
  protected function retrieve_validated_view($form, $form_state, $unset = TRUE) {
890
    $key = hash('sha256', serialize($form_state['values']));
891
    $view = (isset($this->validated_views[$key]) ? $this->validated_views[$key] : NULL);
892
    if ($unset) {
893
      unset($this->validated_views[$key]);
894
    }
895
    return $view;
896
  }
897

    
898
  protected function set_validated_view($form, $form_state, $view) {
899
    $key = hash('sha256', serialize($form_state['values']));
900
    $this->validated_views[$key] = $view;
901
  }
902

    
903
  /**
904
   * Instantiates a view and validates values.
905
   */
906
  function validate($form, &$form_state) {
907
    $view = $this->instantiate_view($form, $form_state);
908
    $errors = $view->validate();
909
    if (!is_array($errors) || empty($errors)) {
910
      $this->set_validated_view($form, $form_state, $view);
911
      return array();
912
    }
913
    return $errors;
914
  }
915

    
916
  /**
917
   * Create a View from values that have been already submitted to validate().
918
   *
919
   * @throws ViewsWizardException if the values have not been validated.
920
   */
921
 function create_view($form, &$form_state) {
922
   $view = $this->retrieve_validated_view($form, $form_state);
923
   if (empty($view)) {
924
     throw new ViewsWizardException(t('Attempted to create_view with values that have not been validated'));
925
   }
926
   return $view;
927
 }
928

    
929
}