Project

General

Profile

Paste
Download (43.5 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_argument.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_argument.
6
 */
7

    
8
/**
9
 * @defgroup views_argument_handlers Views argument handlers
10
 * Handlers to tell Views how to contextually filter queries.
11
 * @{
12
 */
13

    
14
/**
15
 * Base class for arguments.
16
 *
17
 * The basic argument works for very simple arguments such as nid and uid.
18
 *
19
 * Definition terms for this handler:
20
 * - name field: The field to use for the name to use in the summary, which is
21
 *   the displayed output. For example, for the node: nid argument, the argument
22
 *   itself is the nid, but node.title is displayed.
23
 * - name table: The table to use for the name, should it not be in the same
24
 *   table as the argument.
25
 * - empty field name: For arguments that can have no value, such as taxonomy
26
 *   which can have "no term", this is the string which will be displayed for
27
 *   this lack of value. Be sure to use t().
28
 * - validate type: A little used string to allow an argument to restrict
29
 *   which validator is available to just one. Use the validator ID. This
30
 *   probably should not be used at all, and may disappear or change.
31
 * - numeric: If set to TRUE this field is numeric and will use %d instead of
32
 *   %s in queries.
33
 *
34
 * @ingroup views_argument_handlers
35
 */
36
class views_handler_argument extends views_handler {
37

    
38
  /**
39
   * @var object
40
   */
41
  public $validator = NULL;
42

    
43
  /**
44
   * @var mixed
45
   */
46
  public $argument = NULL;
47

    
48
  /**
49
   * @var mixed
50
   */
51
  public $value = NULL;
52

    
53
  /**
54
   * The table to use for the name, if not the same table as the argument.
55
   *
56
   * @var string
57
   */
58
  public $name_table;
59

    
60
  /**
61
   * The field to use for the name to use in the summary.
62
   *
63
   * Used as the displayed output. For example, for the node: nid argument, the
64
   * argument itself is the nid, but node.title is displayed.
65
   *
66
   * @var string
67
   */
68
  public $name_field;
69

    
70
  /**
71
   * {@inheritdoc}
72
   */
73
  public function construct() {
74
    parent::construct();
75

    
76
    if (!empty($this->definition['name field'])) {
77
      $this->name_field = $this->definition['name field'];
78
    }
79
    if (!empty($this->definition['name table'])) {
80
      $this->name_table = $this->definition['name table'];
81
    }
82
  }
83

    
84
  /**
85
   * {@inheritdoc}
86
   */
87
  public function init(&$view, &$options) {
88
    parent::init($view, $options);
89

    
90
    // Compatibility: The new UI changed several settings.
91
    if (!empty($options['wildcard']) && !isset($options['exception']['value'])) {
92
      $this->options['exception']['value'] = $options['wildcard'];
93
    }
94
    if (!empty($options['wildcard_substitution']) && !isset($options['exception']['title'])) {
95
      // Enable the checkbox if the title is filled in.
96
      $this->options['exception']['title_enable'] = 1;
97
      $this->options['exception']['title'] = $options['wildcard_substitution'];
98
    }
99

    
100
    if (!isset($options['summary']['format']) && !empty($options['style_plugin'])) {
101
      $this->options['summary']['format'] = $options['style_plugin'];
102
    }
103

    
104
    // Setup default value.
105
    $options['style_options'] = isset($options['style_options']) ? $options['style_options'] : array();
106

    
107
    if (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary asc') {
108
      $this->options['default_action'] = 'summary';
109
      $this->options['summary']['sort_order'] = 'asc';
110
      $this->options['summary']['number_of_records'] = 0;
111
      $this->options['summary_options'] = $options['style_options'];
112
    }
113
    elseif (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary desc') {
114
      $this->options['default_action'] = 'summary';
115
      $this->options['summary']['sort_order'] = 'desc';
116
      $this->options['summary']['number_of_records'] = 0;
117
      $this->options['summary_options'] = $options['style_options'];
118
    }
119
    elseif (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary asc by count') {
120
      $this->options['default_action'] = 'summary';
121
      $this->options['summary']['sort_order'] = 'asc';
122
      $this->options['summary']['number_of_records'] = 1;
123
      $this->options['summary_options'] = $options['style_options'];
124
    }
125
    elseif (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary desc by count') {
126
      $this->options['default_action'] = 'summary';
127
      $this->options['summary']['sort_order'] = 'desc';
128
      $this->options['summary']['number_of_records'] = 1;
129
      $this->options['summary_options'] = $options['style_options'];
130
    }
131

    
132
    if (!empty($options['title']) && !isset($options['title_enable'])) {
133
      $this->options['title_enable'] = 1;
134
    }
135
    if (!empty($options['breadcrumb']) && !isset($options['breadcrumb_enable'])) {
136
      $this->options['breadcrumb_enable'] = 1;
137
    }
138

    
139
    if (!empty($options['validate_type']) && !isset($options['validate']['type'])) {
140
      $this->options['validate']['type'] = $options['validate_type'];
141
      $this->options['specify_validation'] = 1;
142
    }
143
    if (!empty($options['validate_fail']) && !isset($options['validate']['fail'])) {
144
      $this->options['validate']['fail'] = $options['validate_fail'];
145
      $this->options['specify_validation'] = 1;
146
    }
147
  }
148

    
149
  /**
150
   * Give an argument the opportunity to modify the breadcrumb, if it wants.
151
   *
152
   * Only gets called on displays where a breadcrumb is actually used.
153
   *
154
   * The breadcrumb will be in the form of an array, with the keys being
155
   * the path and the value being the already sanitized title of the path.
156
   */
157
  public function set_breadcrumb(&$breadcrumb) {
158
  }
159

    
160
  /**
161
   * Determine if the argument can generate a breadcrumb.
162
   *
163
   * @return bool
164
   *   Indicates whether the argument can generate a breadcrumb.
165
   */
166
  public function uses_breadcrumb() {
167
    $info = $this->default_actions($this->options['default_action']);
168
    return !empty($info['breadcrumb']);
169
  }
170

    
171
  /**
172
   * {@inheritdoc}
173
   */
174
  public function is_exception($arg = NULL) {
175
    if (!isset($arg)) {
176
      $arg = isset($this->argument) ? $this->argument : NULL;
177
    }
178
    return !empty($this->options['exception']['value']) && ($this->options['exception']['value'] === $arg);
179
  }
180

    
181
  /**
182
   * Work out which title to use.
183
   *
184
   * @return string
185
   *   The title string to use.
186
   */
187
  public function exception_title() {
188
    // If title overriding is off for the exception, return the normal title.
189
    if (empty($this->options['exception']['title_enable'])) {
190
      return $this->get_title();
191
    }
192
    return $this->options['exception']['title'];
193
  }
194

    
195
  /**
196
   * Determine if the argument needs a style plugin.
197
   *
198
   * @return bool
199
   *   the argument needs a plugin style.
200
   */
201
  public function needs_style_plugin() {
202
    $info = $this->default_actions($this->options['default_action']);
203
    $validate_info = $this->default_actions($this->options['validate']['fail']);
204
    return !empty($info['style plugin']) || !empty($validate_info['style plugin']);
205
  }
206

    
207
  /**
208
   * {@inheritdoc}
209
   */
210
  public function option_definition() {
211
    $options = parent::option_definition();
212

    
213
    $options['default_action'] = array('default' => 'ignore');
214
    $options['exception'] = array(
215
      'contains' => array(
216
        'value' => array('default' => 'all'),
217
        'title_enable' => array('default' => FALSE, 'bool' => TRUE),
218
        'title' => array('default' => 'All', 'translatable' => TRUE),
219
      ),
220
    );
221
    $options['title_enable'] = array('default' => FALSE, 'bool' => TRUE);
222
    $options['title'] = array('default' => '', 'translatable' => TRUE);
223
    $options['breadcrumb_enable'] = array('default' => FALSE, 'bool' => TRUE);
224
    $options['breadcrumb'] = array('default' => '', 'translatable' => TRUE);
225
    $options['default_argument_type'] = array('default' => 'fixed', 'export' => 'export_plugin');
226
    $options['default_argument_options'] = array('default' => array(), 'export' => FALSE);
227
    $options['default_argument_skip_url'] = array('default' => FALSE, 'bool' => TRUE);
228
    $options['summary_options'] = array('default' => array(), 'export' => FALSE);
229
    $options['summary'] = array(
230
      'contains' => array(
231
        'sort_order' => array('default' => 'asc'),
232
        'number_of_records' => array('default' => 0),
233
        'format' => array('default' => 'default_summary', 'export' => 'export_summary'),
234
      ),
235
    );
236
    $options['specify_validation'] = array('default' => FALSE, 'bool' => TRUE);
237
    $options['validate'] = array(
238
      'contains' => array(
239
        'type' => array('default' => 'none', 'export' => 'export_validation'),
240
        'fail' => array('default' => 'not found'),
241
      ),
242
    );
243
    $options['validate_options'] = array('default' => array(), 'export' => FALSE);
244

    
245
    return $options;
246
  }
247

    
248
  /**
249
   * {@inheritdoc}
250
   */
251
  public function options_form(&$form, &$form_state) {
252
    parent::options_form($form, $form_state);
253

    
254
    $argument_text = $this->view->display_handler->get_argument_text();
255

    
256
    $form['#pre_render'][] = 'views_ui_pre_render_move_argument_options';
257

    
258
    $form['description'] = array(
259
      '#markup' => $argument_text['description'],
260
      '#theme_wrappers' => array('container'),
261
      '#attributes' => array('class' => array('description')),
262
    );
263

    
264
    $form['no_argument'] = array(
265
      '#type' => 'fieldset',
266
      '#title' => $argument_text['filter value not present'],
267
    );
268
    // Everything in the fieldset is floated, so the last element needs to
269
    // clear those floats.
270
    $form['no_argument']['clearfix'] = array(
271
      '#weight' => 1000,
272
      '#markup' => '<div class="clearfix"></div>',
273
    );
274
    $form['default_action'] = array(
275
      '#type' => 'radios',
276
      '#process' => array('views_ui_process_container_radios'),
277
      '#default_value' => $this->options['default_action'],
278
      '#fieldset' => 'no_argument',
279
    );
280

    
281
    $form['exception'] = array(
282
      '#type' => 'fieldset',
283
      '#title' => t('Exceptions'),
284
      '#collapsible' => TRUE,
285
      '#collapsed' => TRUE,
286
      '#fieldset' => 'no_argument',
287
    );
288
    $form['exception']['value'] = array(
289
      '#type' => 'textfield',
290
      '#title' => t('Exception value'),
291
      '#size' => 20,
292
      '#default_value' => $this->options['exception']['value'],
293
      '#description' => t('If this value is received, the filter will be ignored; i.e, "all values"'),
294
    );
295
    $form['exception']['title_enable'] = array(
296
      '#type' => 'checkbox',
297
      '#title' => t('Override title'),
298
      '#default_value' => $this->options['exception']['title_enable'],
299
    );
300
    $form['exception']['title'] = array(
301
      '#type' => 'textfield',
302
      '#title' => t('Override title'),
303
      '#title_display' => 'invisible',
304
      '#size' => 20,
305
      '#default_value' => $this->options['exception']['title'],
306
      '#description' => t('Override the view and other argument titles. Use "%1" for the first argument, "%2" for the second, etc.'),
307
      '#dependency' => array(
308
        'edit-options-exception-title-enable' => array('1'),
309
      ),
310
    );
311

    
312
    $options = array();
313
    $defaults = $this->default_actions();
314
    $validate_options = array();
315
    foreach ($defaults as $id => $info) {
316
      $options[$id] = $info['title'];
317
      if (empty($info['default only'])) {
318
        $validate_options[$id] = $info['title'];
319
      }
320
      if (!empty($info['form method'])) {
321
        $this->{$info['form method']}($form, $form_state);
322
      }
323
    }
324
    $form['default_action']['#options'] = $options;
325

    
326
    $form['argument_present'] = array(
327
      '#type' => 'fieldset',
328
      '#title' => $argument_text['filter value present'],
329
    );
330
    $form['title_enable'] = array(
331
      '#type' => 'checkbox',
332
      '#title' => t('Override title'),
333
      '#default_value' => $this->options['title_enable'],
334
      '#fieldset' => 'argument_present',
335
    );
336
    $form['title'] = array(
337
      '#type' => 'textfield',
338
      '#title' => t('Provide title'),
339
      '#title_display' => 'invisible',
340
      '#default_value' => $this->options['title'],
341
      '#description' => t('Override the view and other argument titles. Use "%1" for the first argument, "%2" for the second, etc.'),
342
      '#dependency' => array(
343
        'edit-options-title-enable' => array('1'),
344
      ),
345
      '#fieldset' => 'argument_present',
346
    );
347

    
348
    $form['breadcrumb_enable'] = array(
349
      '#type' => 'checkbox',
350
      '#title' => t('Override breadcrumb'),
351
      '#default_value' => $this->options['breadcrumb_enable'],
352
      '#fieldset' => 'argument_present',
353
    );
354
    $form['breadcrumb'] = array(
355
      '#type' => 'textfield',
356
      '#title' => t('Provide breadcrumb'),
357
      '#title_display' => 'invisible',
358
      '#default_value' => $this->options['breadcrumb'],
359
      '#description' => t('Enter a breadcrumb name you would like to use. See "Title" for percent substitutions.'),
360
      '#dependency' => array(
361
        'edit-options-breadcrumb-enable' => array('1'),
362
      ),
363
      '#fieldset' => 'argument_present',
364
    );
365

    
366
    $form['specify_validation'] = array(
367
      '#type' => 'checkbox',
368
      '#title' => t('Specify validation criteria'),
369
      '#default_value' => $this->options['specify_validation'],
370
      '#fieldset' => 'argument_present',
371
    );
372

    
373
    $form['validate'] = array(
374
      '#type' => 'container',
375
      '#fieldset' => 'argument_present',
376
    );
377
    // @todo The mockup wanted to use "Validate using" here, but it doesn't
378
    // work well with many options (they'd need to be changed as well)
379
    $form['validate']['type'] = array(
380
      '#type' => 'select',
381
      '#title' => t('Validator'),
382
      '#default_value' => $this->options['validate']['type'],
383
      '#dependency' => array(
384
        'edit-options-specify-validation' => array('1'),
385
      ),
386
    );
387

    
388
    $validate_types = array('none' => t('- Basic validation -'));
389
    $plugins = views_fetch_plugin_data('argument validator');
390
    foreach ($plugins as $id => $info) {
391
      if (!empty($info['no ui'])) {
392
        continue;
393
      }
394

    
395
      $valid = TRUE;
396
      if (!empty($info['type'])) {
397
        $valid = FALSE;
398
        if (empty($this->definition['validate type'])) {
399
          continue;
400
        }
401
        foreach ((array) $info['type'] as $type) {
402
          if ($type == $this->definition['validate type']) {
403
            $valid = TRUE;
404
            break;
405
          }
406
        }
407
      }
408

    
409
      // If we decide this validator is ok, add it to the list.
410
      if ($valid) {
411
        $plugin = $this->get_plugin('argument validator', $id);
412
        if ($plugin) {
413
          if ($plugin->access() || $this->options['validate']['type'] == $id) {
414
            $form['validate']['options'][$id] = array(
415
              '#prefix' => '<div id="edit-options-validate-options-' . $id . '-wrapper">',
416
              '#suffix' => '</div>',
417
              '#type' => 'item',
418
              // Even if the plugin has no options, add the key to the
419
              // form_state. Trick it into checking input to make #process run.
420
              '#input' => TRUE,
421
              '#dependency' => array(
422
                'edit-options-specify-validation' => array('1'),
423
                'edit-options-validate-type' => array($id),
424
              ),
425
              '#dependency_count' => 2,
426
              '#id' => 'edit-options-validate-options-' . $id,
427
            );
428
            $plugin->options_form($form['validate']['options'][$id], $form_state);
429
            $validate_types[$id] = $info['title'];
430
          }
431
        }
432
      }
433
    }
434

    
435
    asort($validate_types);
436
    $form['validate']['type']['#options'] = $validate_types;
437

    
438
    $form['validate']['fail'] = array(
439
      '#type' => 'select',
440
      '#title' => t('Action to take if filter value does not validate'),
441
      '#default_value' => $this->options['validate']['fail'],
442
      '#options' => $validate_options,
443
      '#dependency' => array(
444
        'edit-options-specify-validation' => array('1'),
445
      ),
446
      '#fieldset' => 'argument_present',
447
    );
448
  }
449

    
450
  /**
451
   * {@inheritdoc}
452
   */
453
  public function options_validate(&$form, &$form_state) {
454
    if (empty($form_state['values']['options'])) {
455
      return;
456
    }
457

    
458
    // Let the plugins do validation.
459
    $default_id = $form_state['values']['options']['default_argument_type'];
460
    $plugin = $this->get_plugin('argument default', $default_id);
461
    if ($plugin && isset($form['argument_default'][$default_id]) && isset($form_state['values']['options']['argument_default'][$default_id])) {
462
      $plugin->options_validate($form['argument_default'][$default_id], $form_state, $form_state['values']['options']['argument_default'][$default_id]);
463
    }
464

    
465
    // Validate summary plugin options if one is present.
466
    if (isset($form_state['values']['options']['summary']['format'])) {
467
      $summary_id = $form_state['values']['options']['summary']['format'];
468
      $plugin = $this->get_plugin('style', $summary_id);
469
      if ($plugin) {
470
        $plugin->options_validate($form['summary']['options'][$summary_id], $form_state, $form_state['values']['options']['summary']['options'][$summary_id]);
471
      }
472
    }
473

    
474
    $validate_id = $form_state['values']['options']['validate']['type'];
475
    $plugin = $this->get_plugin('argument validator', $validate_id);
476
    if ($plugin) {
477
      $plugin->options_validate($form['validate']['options'][$default_id], $form_state, $form_state['values']['options']['validate']['options'][$validate_id]);
478
    }
479

    
480
  }
481

    
482
  /**
483
   * {@inheritdoc}
484
   */
485
  public function options_submit(&$form, &$form_state) {
486
    if (empty($form_state['values']['options'])) {
487
      return;
488
    }
489

    
490
    // Let the plugins make submit modifications if necessary.
491
    $default_id = $form_state['values']['options']['default_argument_type'];
492
    $plugin = $this->get_plugin('argument default', $default_id);
493
    if ($plugin) {
494
      $options = &$form_state['values']['options']['argument_default'][$default_id];
495
      $plugin->options_submit($form['argument_default'][$default_id], $form_state, $options);
496
      // Copy the now submitted options to their final resting place so they
497
      // get saved.
498
      $form_state['values']['options']['default_argument_options'] = $options;
499
    }
500

    
501
    // Handle summary plugin options if one is present.
502
    if (isset($form_state['values']['options']['summary']['format'])) {
503
      $summary_id = $form_state['values']['options']['summary']['format'];
504
      $plugin = $this->get_plugin('style', $summary_id);
505
      if ($plugin) {
506
        $options = &$form_state['values']['options']['summary']['options'][$summary_id];
507
        $plugin->options_submit($form['summary']['options'][$summary_id], $form_state, $options);
508
        // Copy the now submitted options to their final resting place so they
509
        // get saved.
510
        $form_state['values']['options']['summary_options'] = $options;
511
      }
512
    }
513

    
514
    $validate_id = $form_state['values']['options']['validate']['type'];
515
    $plugin = $this->get_plugin('argument validator', $validate_id);
516
    if ($plugin) {
517
      $options = &$form_state['values']['options']['validate']['options'][$validate_id];
518
      $plugin->options_submit($form['validate']['options'][$validate_id], $form_state, $options);
519
      // Copy the now submitted options to their final resting place so they
520
      // get saved.
521
      $form_state['values']['options']['validate_options'] = $options;
522
    }
523

    
524
    // Clear out the content of title if it's not enabled.
525
    $options =& $form_state['values']['options'];
526
    if (empty($options['title_enable'])) {
527
      $options['title'] = '';
528
    }
529
  }
530

    
531
  /**
532
   * List of default behaviors for this argument if the argument is not present.
533
   *
534
   * Override this method to provide additional (or fewer) default behaviors.
535
   */
536
  public function default_actions($which = NULL) {
537
    $defaults = array(
538
      'ignore' => array(
539
        'title' => t('Display all results for the specified field'),
540
        'method' => 'default_ignore',
541
        // Generate a breadcrumb to here.
542
        'breadcrumb' => TRUE,
543
      ),
544
      'default' => array(
545
        'title' => t('Provide default value'),
546
        'method' => 'default_default',
547
        'form method' => 'default_argument_form',
548
        'has default argument' => TRUE,
549
        // This can only be used for missing argument, not validation failure.
550
        'default only' => TRUE,
551
        // Generate a breadcrumb to here.
552
        'breadcrumb' => TRUE,
553
      ),
554
      'not found' => array(
555
        'title' => t('Hide view'),
556
        'method' => 'default_not_found',
557
        // This is a hard fail condition.
558
        'hard fail' => TRUE,
559
      ),
560
      'summary' => array(
561
        'title' => t('Display a summary'),
562
        'method' => 'default_summary',
563
        'form method' => 'default_summary_form',
564
        'style plugin' => TRUE,
565
        // Generate a breadcrumb to here.
566
        'breadcrumb' => TRUE,
567
      ),
568
      'empty' => array(
569
        'title' => t('Display contents of "No results found"'),
570
        'method' => 'default_empty',
571
        // Generate a breadcrumb to here.
572
        'breadcrumb' => TRUE,
573
      ),
574
      'access denied' => array(
575
        'title' => t('Display "Access Denied"'),
576
        'method' => 'default_access_denied',
577
        // Generate a breadcrumb to here.
578
        'breadcrumb' => FALSE,
579
      ),
580
    );
581

    
582
    if ($this->view->display_handler->has_path()) {
583
      $defaults['not found']['title'] = t('Show "Page not found"');
584
    }
585

    
586
    if ($which) {
587
      if (!empty($defaults[$which])) {
588
        return $defaults[$which];
589
      }
590
    }
591
    else {
592
      return $defaults;
593
    }
594
  }
595

    
596
  /**
597
   * Provide a form for selecting the default argument.
598
   *
599
   * Used when the default action is set to provide default argument.
600
   */
601
  public function default_argument_form(&$form, &$form_state) {
602
    $plugins = views_fetch_plugin_data('argument default');
603
    $options = array();
604

    
605
    $form['default_argument_skip_url'] = array(
606
      '#type' => 'checkbox',
607
      '#title' => t('Skip default argument for view URL'),
608
      '#default_value' => $this->options['default_argument_skip_url'],
609
      '#description' => t('Select whether to include this default argument when constructing the URL for this view. Skipping default arguments is useful e.g. in the case of feeds.'),
610
    );
611

    
612
    $form['default_argument_type'] = array(
613
      '#prefix' => '<div id="edit-options-default-argument-type-wrapper">',
614
      '#suffix' => '</div>',
615
      '#type' => 'select',
616
      '#id' => 'edit-options-default-argument-type',
617
      '#title' => t('Type'),
618
      '#default_value' => $this->options['default_argument_type'],
619
      '#dependency' => array(
620
        'radio:options[default_action]' => array(
621
          'default',
622
        ),
623
      ),
624
      // Views custom key, moves this element to the appropriate container
625
      // under the radio button.
626
      '#argument_option' => 'default',
627
    );
628

    
629
    foreach ($plugins as $id => $info) {
630
      if (!empty($info['no ui'])) {
631
        continue;
632
      }
633
      $plugin = $this->get_plugin('argument default', $id);
634
      if ($plugin) {
635
        if ($plugin->access() || $this->options['default_argument_type'] == $id) {
636
          $form['argument_default']['#argument_option'] = 'default';
637
          $form['argument_default'][$id] = array(
638
            '#prefix' => '<div id="edit-options-argument-default-options-' . $id . '-wrapper">',
639
            '#suffix' => '</div>',
640
            '#id' => 'edit-options-argument-default-options-' . $id,
641
            '#type' => 'item',
642
            // Even if the plugin has no options add the key to the form_state.
643
            '#input' => TRUE,
644
            '#dependency' => array(
645
              'radio:options[default_action]' => array('default'),
646
              'edit-options-default-argument-type' => array($id),
647
            ),
648
            '#dependency_count' => 2,
649
          );
650
          $options[$id] = $info['title'];
651
          $plugin->options_form($form['argument_default'][$id], $form_state);
652
        }
653
      }
654
    }
655

    
656
    asort($options);
657
    $form['default_argument_type']['#options'] = $options;
658
  }
659

    
660
  /**
661
   * Form for selecting further summary options.
662
   *
663
   * Only used when the default action is set to display one.
664
   */
665
  public function default_summary_form(&$form, &$form_state) {
666
    $style_plugins = views_fetch_plugin_data('style');
667
    $summary_plugins = array();
668
    $format_options = array();
669
    foreach ($style_plugins as $key => $plugin) {
670
      if (isset($plugin['type']) && $plugin['type'] == 'summary') {
671
        $summary_plugins[$key] = $plugin;
672
        $format_options[$key] = $plugin['title'];
673
      }
674
    }
675

    
676
    $form['summary'] = array(
677
      // Views custom key, moves this element to the appropriate container
678
      // under the radio button.
679
      '#argument_option' => 'summary',
680
    );
681
    $form['summary']['sort_order'] = array(
682
      '#type' => 'radios',
683
      '#title' => t('Sort order'),
684
      '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
685
      '#default_value' => $this->options['summary']['sort_order'],
686
      '#dependency' => array('radio:options[default_action]' => array('summary')),
687
    );
688
    $form['summary']['number_of_records'] = array(
689
      '#type' => 'radios',
690
      '#title' => t('Sort by'),
691
      '#default_value' => $this->options['summary']['number_of_records'],
692
      '#options' => array(
693
        0 => $this->get_sort_name(),
694
        1 => t('Number of records'),
695
      ),
696
      '#dependency' => array('radio:options[default_action]' => array('summary')),
697
    );
698

    
699
    $form['summary']['format'] = array(
700
      '#type' => 'radios',
701
      '#title' => t('Format'),
702
      '#options' => $format_options,
703
      '#default_value' => $this->options['summary']['format'],
704
      '#dependency' => array('radio:options[default_action]' => array('summary')),
705
    );
706

    
707
    foreach ($summary_plugins as $id => $info) {
708
      if (empty($info['uses options'])) {
709
        continue;
710
      }
711
      $plugin = $this->get_plugin('style', $id);
712
      if ($plugin) {
713
        $form['summary']['options'][$id] = array(
714
          '#prefix' => '<div id="edit-options-summary-options-' . $id . '-wrapper">',
715
          '#suffix' => '</div>',
716
          '#id' => 'edit-options-summary-options-' . $id,
717
          '#type' => 'item',
718
          // Trick it into checking input to make #process run.
719
          '#input' => TRUE,
720
          '#dependency' => array(
721
            'radio:options[default_action]' => array('summary'),
722
            'radio:options[summary][format]' => array($id),
723
          ),
724
          '#dependency_count' => 2,
725
        );
726
        $options[$id] = $info['title'];
727
        $plugin->options_form($form['summary']['options'][$id], $form_state);
728
      }
729
    }
730
  }
731

    
732
  /**
733
   * Handle the default action, which means our argument wasn't present.
734
   *
735
   * Override this method only with extreme care.
736
   *
737
   * @return bool
738
   *   A boolean value; if TRUE, continue building this view. If FALSE,
739
   *   building the view will be aborted here.
740
   */
741
  public function default_action($info = NULL) {
742
    if (!isset($info)) {
743
      $info = $this->default_actions($this->options['default_action']);
744
    }
745

    
746
    if (!$info) {
747
      return FALSE;
748
    }
749

    
750
    if (!empty($info['method args'])) {
751
      return call_user_func_array(array(&$this, $info['method']), $info['method args']);
752
    }
753
    else {
754
      return $this->{$info['method']}();
755
    }
756
  }
757

    
758
  /**
759
   * How to act if validation fails.
760
   */
761
  public function validate_fail() {
762
    $info = $this->default_actions($this->options['validate']['fail']);
763
    return $this->default_action($info);
764
  }
765

    
766
  /**
767
   * Default action: ignore.
768
   *
769
   * If an argument was expected and was not given, in this case, simply ignore
770
   * the argument entirely.
771
   */
772
  public function default_ignore() {
773
    return TRUE;
774
  }
775

    
776
  /**
777
   * Default action: not found.
778
   *
779
   * If an argument was expected and was not given, in this case, report the
780
   * view as 'not found' or hide it.
781
   */
782
  public function default_not_found() {
783
    // Set a failure condition and let the display manager handle it.
784
    $this->view->build_info['fail'] = TRUE;
785
    return FALSE;
786
  }
787

    
788
  /**
789
   * Default action: access denied.
790
   *
791
   * If an argument was expected and was not given, in this case, report the
792
   * view as 'access denied'.
793
   */
794
  public function default_access_denied() {
795
    $this->view->build_info['denied'] = TRUE;
796
    return FALSE;
797
  }
798

    
799
  /**
800
   * Default action: empty.
801
   *
802
   * If an argument was expected and was not given, in this case, display the
803
   * view's empty text.
804
   */
805
  public function default_empty() {
806
    // We return with no query; this will force the empty text.
807
    $this->view->built = TRUE;
808
    $this->view->executed = TRUE;
809
    $this->view->result = array();
810
    return FALSE;
811
  }
812

    
813
  /**
814
   * This just returns true.
815
   *
816
   * The view argument builder will know where to find the argument from.
817
   *
818
   * @todo Why is this needed?
819
   */
820
  public function default_default() {
821
    return TRUE;
822
  }
823

    
824
  /**
825
   * Determine if the argument is set to provide a default argument.
826
   */
827
  public function has_default_argument() {
828
    $info = $this->default_actions($this->options['default_action']);
829
    return !empty($info['has default argument']);
830
  }
831

    
832
  /**
833
   * Get a default argument, if available.
834
   */
835
  public function get_default_argument() {
836
    $plugin = $this->get_plugin('argument default');
837
    if ($plugin) {
838
      return $plugin->get_argument();
839
    }
840
  }
841

    
842
  /**
843
   * Process the summary arguments for display.
844
   *
845
   * For example, the validation plugin may want to alter an argument for use in
846
   * the URL.
847
   */
848
  public function process_summary_arguments(&$args) {
849
    if ($this->options['validate']['type'] != 'none') {
850
      if (isset($this->validator) || $this->validator = $this->get_plugin('argument validator')) {
851
        $this->validator->process_summary_arguments($args);
852
      }
853
    }
854
  }
855

    
856
  /**
857
   * Default action: summary.
858
   *
859
   * If an argument was expected and was not given, in this case, display a
860
   * summary query.
861
   */
862
  public function default_summary() {
863
    $this->view->build_info['summary'] = TRUE;
864
    $this->view->build_info['summary_level'] = $this->options['id'];
865

    
866
    // Change the display style to the summary style for this argument.
867
    $this->view->plugin_name = $this->options['summary']['format'];
868
    $this->view->style_options = $this->options['summary_options'];
869

    
870
    // Clear out the normal primary field and whatever else may have been added
871
    // and let the summary do the work.
872
    $this->query->clear_fields();
873
    $this->summary_query();
874

    
875
    $by = $this->options['summary']['number_of_records'] ? 'num_records' : NULL;
876
    $this->summary_sort($this->options['summary']['sort_order'], $by);
877

    
878
    // Summaries have their own sorting and fields, so tell the View not
879
    // to build these.
880
    $this->view->build_sort = $this->view->build_fields = FALSE;
881
    return TRUE;
882
  }
883

    
884
  /**
885
   * Build the info for the summary query.
886
   *
887
   * This must:
888
   * - add_groupby: group on this field in order to create summaries.
889
   * - add_field: add a 'num_nodes' field for the count. Usually it will be a
890
   *   count on $view->base_field
891
   * - set_count_field: Reset the count field so we get the right paging.
892
   *
893
   * @return string
894
   *   The alias used to get the number of records (count) for this entry.
895
   */
896
  public function summary_query() {
897
    $this->ensure_my_table();
898
    // Add the field.
899
    $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
900

    
901
    $this->summary_name_field();
902
    return $this->summary_basics();
903
  }
904

    
905
  /**
906
   * Add the name field, which is the field displayed in summary queries.
907
   *
908
   * This is often used when the argument is numeric.
909
   */
910
  public function summary_name_field() {
911
    // Add the 'name' field. For example, if this is a uid argument, the name
912
    // field would be 'name' (i.e, the username).
913
    if (isset($this->name_table)) {
914
      // If the alias is different then we're probably added, not ensured, so
915
      // look up the join and add it instead.
916
      if ($this->table_alias != $this->name_table) {
917
        $j = views_get_table_join($this->name_table, $this->table);
918
        if ($j) {
919
          $join = clone $j;
920
          $join->left_table = $this->table_alias;
921
          $this->name_table_alias = $this->query->add_table($this->name_table, $this->relationship, $join);
922
        }
923
      }
924
      else {
925
        $this->name_table_alias = $this->query->ensure_table($this->name_table, $this->relationship);
926
      }
927
    }
928
    else {
929
      $this->name_table_alias = $this->table_alias;
930
    }
931

    
932
    if (isset($this->name_field)) {
933
      $this->name_alias = $this->query->add_field($this->name_table_alias, $this->name_field);
934
    }
935
    else {
936
      $this->name_alias = $this->base_alias;
937
    }
938
  }
939

    
940
  /**
941
   * Some basic summary behavior.
942
   *
943
   * This doesn't need to be repeated as much as code that goes into
944
   * summary_query().
945
   */
946
  public function summary_basics($count_field = TRUE) {
947
    // Add the number of nodes counter.
948
    $distinct = ($this->view->display_handler->get_option('distinct') && empty($this->query->no_distinct));
949

    
950
    $count_alias = $this->query->add_field($this->query->base_table,
951
      $this->query->base_field, 'num_records',
952
      array(
953
        'count' => TRUE,
954
        'distinct' => $distinct,
955
      ));
956
    $this->query->add_groupby($this->name_alias);
957

    
958
    if ($count_field) {
959
      $this->query->set_count_field($this->table_alias, $this->real_field);
960
    }
961

    
962
    $this->count_alias = $count_alias;
963
  }
964

    
965
  /**
966
   * Sorts the summary based upon the user's selection.
967
   *
968
   * The base variant of this is usually adequte.
969
   *
970
   * @param string $order
971
   *   The order selected in the UI.
972
   * @param string $by
973
   *   Optional alias for this field.
974
   */
975
  public function summary_sort($order, $by = NULL) {
976
    $this->query->add_orderby(NULL, NULL, $order, (!empty($by) ? $by : $this->name_alias));
977
  }
978

    
979
  /**
980
   * Provide the argument to use to link from the summary to the next level.
981
   *
982
   * This will be called once per row of a summary, and used as part of
983
   * $view->get_url().
984
   *
985
   * @param object $data
986
   *   The query results for the row.
987
   */
988
  public function summary_argument($data) {
989
    return $data->{$this->base_alias};
990
  }
991

    
992
  /**
993
   * Provides the name to use for the summary.
994
   *
995
   * By default this is just the name field.
996
   *
997
   * @param object $data
998
   *   The query results for the row.
999
   *
1000
   * @return string
1001
   *   The summary.
1002
   */
1003
  public function summary_name($data) {
1004
    $value = $data->{$this->name_alias};
1005
    if (empty($value) && !empty($this->definition['empty field name'])) {
1006
      $value = $this->definition['empty field name'];
1007
    }
1008
    return check_plain($value);
1009
  }
1010

    
1011
  /**
1012
   * Set up the query for this argument.
1013
   *
1014
   * The argument sent may be found at $this->argument.
1015
   *
1016
   * @param bool $group_by
1017
   *   Whether the query uses a group-by.
1018
   */
1019
  public function query($group_by = FALSE) {
1020
    $this->ensure_my_table();
1021
    $this->query->add_where(0, "$this->table_alias.$this->real_field", $this->argument);
1022
  }
1023

    
1024
  /**
1025
   * Get the title this argument will assign the view, given the argument.
1026
   *
1027
   * This usually needs to be overridden to provide a proper title.
1028
   */
1029
  public function title() {
1030
    return check_plain($this->argument);
1031
  }
1032

    
1033
  /**
1034
   * Called by the view object to get the title.
1035
   *
1036
   * This may be set by a validator so we don't necessarily call through to
1037
   * title().
1038
   */
1039
  public function get_title() {
1040
    if (isset($this->validated_title)) {
1041
      return $this->validated_title;
1042
    }
1043
    else {
1044
      return $this->title();
1045
    }
1046
  }
1047

    
1048
  /**
1049
   * Validate that this argument works. By default, all arguments are valid.
1050
   */
1051
  public function validate_arg($arg) {
1052
    // By using % in URLs, arguments could be validated twice; this eases
1053
    // that pain.
1054
    if (isset($this->argument_validated)) {
1055
      return $this->argument_validated;
1056
    }
1057

    
1058
    if ($this->is_exception($arg)) {
1059
      return $this->argument_validated = TRUE;
1060
    }
1061

    
1062
    if ($this->options['validate']['type'] == 'none') {
1063
      return $this->argument_validated = $this->validate_argument_basic($arg);
1064
    }
1065

    
1066
    $plugin = $this->get_plugin('argument validator');
1067
    if ($plugin) {
1068
      return $this->argument_validated = $plugin->validate_argument($arg);
1069
    }
1070

    
1071
    // If the plugin isn't found, fall back to the basic validation path.
1072
    return $this->argument_validated = $this->validate_argument_basic($arg);
1073
  }
1074

    
1075
  /**
1076
   * Called by the menu system to validate an argument.
1077
   *
1078
   * This checks to see if this is a 'soft fail', which means that if the
1079
   * argument fails to validate, but there is an action to take anyway, then
1080
   * validation cannot actually fail.
1081
   */
1082
  public function validate_argument($arg) {
1083
    $validate_info = $this->default_actions($this->options['validate']['fail']);
1084
    if (empty($validate_info['hard fail'])) {
1085
      return TRUE;
1086
    }
1087

    
1088
    $rc = $this->validate_arg($arg);
1089

    
1090
    // If the validator has changed the validate fail condition to a soft fail,
1091
    // deal with that.
1092
    $validate_info = $this->default_actions($this->options['validate']['fail']);
1093
    if (empty($validate_info['hard fail'])) {
1094
      return TRUE;
1095
    }
1096

    
1097
    return $rc;
1098
  }
1099

    
1100
  /**
1101
   * Provide a basic argument validation.
1102
   *
1103
   * This can be overridden for more complex types; the basic validator only
1104
   * checks to see if the argument is not NULL or is numeric if the definition
1105
   * says it's numeric.
1106
   *
1107
   * @return bool
1108
   *   Whether or not the argument validates.
1109
   */
1110
  public function validate_argument_basic($arg) {
1111
    if (!isset($arg) || $arg === '') {
1112
      return FALSE;
1113
    }
1114

    
1115
    if (!empty($this->definition['numeric']) && !isset($this->options['break_phrase']) && !is_numeric($arg)) {
1116
      return FALSE;
1117
    }
1118

    
1119
    return TRUE;
1120
  }
1121

    
1122
  /**
1123
   * Set the input for this argument.
1124
   *
1125
   * @return bool
1126
   *   TRUE if it successfully validates; FALSE if it does not.
1127
   */
1128
  public function set_argument($arg) {
1129
    $this->argument = $arg;
1130
    return $this->validate_arg($arg);
1131
  }
1132

    
1133
  /**
1134
   * Get the value of this argument.
1135
   *
1136
   * @return string
1137
   *   The value.
1138
   */
1139
  public function get_value() {
1140
    // If we already processed this argument, we're done.
1141
    if (isset($this->argument)) {
1142
      return $this->argument;
1143
    }
1144

    
1145
    // Otherwise, we have to pretend to process ourself to find the value.
1146
    $value = NULL;
1147
    // Find the position of this argument within the view.
1148
    $position = 0;
1149
    foreach ($this->view->argument as $id => $argument) {
1150
      if ($id == $this->options['id']) {
1151
        break;
1152
      }
1153
      $position++;
1154
    }
1155

    
1156
    $arg = isset($this->view->args[$position]) ? $this->view->args[$position] : NULL;
1157
    $this->position = $position;
1158

    
1159
    // Clone ourselves so that we don't break things when we're really
1160
    // processing the arguments.
1161
    $argument = clone $this;
1162
    if (!isset($arg) && $argument->has_default_argument()) {
1163
      $arg = $argument->get_default_argument();
1164
    }
1165
    // Set the argument, which will also validate that the argument can be set.
1166
    if ($argument->set_argument($arg)) {
1167
      $value = $argument->argument;
1168
    }
1169
    unset($argument);
1170
    return $value;
1171
  }
1172

    
1173
  /**
1174
   * Export handler for summary export.
1175
   *
1176
   * Arguments can have styles for the summary view. This special export
1177
   * handler makes sure this works properly.
1178
   *
1179
   * @return string
1180
   *   The export summary.
1181
   */
1182
  public function export_summary($indent, $prefix, $storage, $option, $definition, $parents) {
1183
    $output = '';
1184
    $name = $this->options['summary'][$option];
1185
    $options = $this->options['summary_options'];
1186

    
1187
    $plugin = views_get_plugin('style', $name);
1188
    if ($plugin) {
1189
      $plugin->init($this->view, $this->view->display_handler->display, $options);
1190
      // Write which plugin to use.
1191
      $output .= $indent . $prefix . "['summary']['$option'] = '$name';\n";
1192

    
1193
      // Pass off to the plugin to export itself.
1194
      $output .= $plugin->export_options($indent, $prefix . "['summary_options']");
1195
    }
1196

    
1197
    return $output;
1198
  }
1199

    
1200
  /**
1201
   * Export handler for validation export.
1202
   *
1203
   * Arguments use validation plugins. This special export handler makes sure
1204
   * this works properly.
1205
   *
1206
   * @return string
1207
   *   The validation response.
1208
   */
1209
  public function export_validation($indent, $prefix, $storage, $option, $definition, $parents) {
1210
    $output = '';
1211
    $name = $this->options['validate'][$option];
1212
    $options = $this->options['validate_options'];
1213

    
1214
    $plugin = views_get_plugin('argument validator', $name);
1215
    if ($plugin) {
1216
      $plugin->init($this->view, $this->display, $options);
1217
      // Write which plugin to use.
1218
      $output .= $indent . $prefix . "['validate']['$option'] = '$name';\n";
1219

    
1220
      // Pass off to the plugin to export itself.
1221
      $output .= $plugin->export_options($indent, $prefix . "['validate_options']");
1222
    }
1223

    
1224
    return $output;
1225
  }
1226

    
1227
  /**
1228
   * Generic plugin export handler.
1229
   *
1230
   * Since style and validation plugins have their own export handlers, this
1231
   * one is currently only used for default argument plugins.
1232
   *
1233
   * @return string
1234
   *   Export string.
1235
   */
1236
  public function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
1237
    $output = '';
1238
    if ($option == 'default_argument_type') {
1239
      $type = 'argument default';
1240
      $option_name = 'default_argument_options';
1241
    }
1242

    
1243
    $plugin = $this->get_plugin($type);
1244
    $name = $this->options[$option];
1245

    
1246
    if ($plugin) {
1247
      // Write which plugin to use.
1248
      $output .= $indent . $prefix . "['$option'] = '$name';\n";
1249

    
1250
      // Pass off to the plugin to export itself.
1251
      $output .= $plugin->export_options($indent, $prefix . "['$option_name']");
1252
    }
1253

    
1254
    return $output;
1255
  }
1256

    
1257
  /**
1258
   * Get the display or row plugin, if it exists.
1259
   */
1260
  public function get_plugin($type = 'argument default', $name = NULL) {
1261
    $options = array();
1262
    switch ($type) {
1263
      case 'argument default':
1264
        $plugin_name = $this->options['default_argument_type'];
1265
        $options_name = 'default_argument_options';
1266
        break;
1267

    
1268
      case 'argument validator':
1269
        $plugin_name = $this->options['validate']['type'];
1270
        $options_name = 'validate_options';
1271
        break;
1272

    
1273
      case 'style':
1274
        $plugin_name = $this->options['summary']['format'];
1275
        $options_name = 'summary_options';
1276
        break;
1277
    }
1278

    
1279
    if (!$name) {
1280
      $name = $plugin_name;
1281
    }
1282

    
1283
    // We only fetch the options if we're fetching the plugin actually in use.
1284
    if ($name == $plugin_name) {
1285
      $options = $this->options[$options_name];
1286
    }
1287

    
1288
    $plugin = views_get_plugin($type, $name);
1289
    if ($plugin) {
1290
      // Style plugins expects different parameters as argument related plugins.
1291
      if ($type == 'style') {
1292
        $plugin->init($this->view, $this->view->display_handler->display, $options);
1293
      }
1294
      else {
1295
        $plugin->init($this->view, $this, $options);
1296
      }
1297
      return $plugin;
1298
    }
1299
  }
1300

    
1301
  /**
1302
   * Return a description of how the argument would normally be sorted.
1303
   *
1304
   * Subclasses should override this to specify what the default sort order of
1305
   * their argument is (e.g. alphabetical, numeric, date).
1306
   *
1307
   * @return string
1308
   *   The label for the sorter.
1309
   */
1310
  public function get_sort_name() {
1311
    return t('Default sort', array(), array('context' => 'Sort order'));
1312
  }
1313

    
1314
}
1315

    
1316
/**
1317
 * A special handler to take the place of missing or broken handlers.
1318
 *
1319
 * @ingroup views_argument_handlers
1320
 */
1321
class views_handler_argument_broken extends views_handler_argument {
1322

    
1323
  /**
1324
   * {@inheritdoc}
1325
   */
1326
  public function ui_name($short = FALSE) {
1327
    return t('Broken/missing handler');
1328
  }
1329

    
1330
  /**
1331
   * {@inheritdoc}
1332
   */
1333
  public function ensure_my_table() {
1334
    // No table to ensure!
1335
  }
1336

    
1337
  /**
1338
   * {@inheritdoc}
1339
   */
1340
  public function query($group_by = FALSE) {
1341
    // No query to run.
1342
  }
1343

    
1344
  /**
1345
   * {@inheritdoc}
1346
   */
1347
  public function options_form(&$form, &$form_state) {
1348
    $form['markup'] = array(
1349
      '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
1350
    );
1351
  }
1352

    
1353
  /**
1354
   * {@inheritdoc}
1355
   */
1356
  public function broken() {
1357
    return TRUE;
1358
  }
1359

    
1360
}
1361

    
1362
/**
1363
 * @}
1364
 */