Project

General

Profile

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * @todo.
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,
22
 *               the argument 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
27
 *                     will be displayed for this lack of value. Be sure to use
28
 *                     t().
29
 * - validate type: A little used string to allow an argument to restrict
30
 *                  which validator is available to just one. Use the
31
 *                  validator ID. This probably should not be used at all,
32
 *                  and may disappear or change.
33
 * - numeric: If set to TRUE this field is numeric and will use %d instead of
34
 *            %s in queries.
35
 *
36
 * @ingroup views_argument_handlers
37
 */
38
class views_handler_argument extends views_handler {
39
  var $validator = NULL;
40
  var $argument = NULL;
41
  var $value = NULL;
42

    
43
  /**
44
   * The table to use for the name, should it not be in the same table as the argument.
45
   * @var string
46
   */
47
  var $name_table;
48

    
49
  /**
50
   * The field to use for the name to use in the summary, which is
51
   * the displayed output. For example, for the node: nid argument,
52
   * the argument itself is the nid, but node.title is displayed.
53
   * @var string
54
   */
55
  var $name_field;
56

    
57
  /**
58
   * Constructor
59
   */
60
  function construct() {
61
    parent::construct();
62

    
63
    if (!empty($this->definition['name field'])) {
64
      $this->name_field = $this->definition['name field'];
65
    }
66
    if (!empty($this->definition['name table'])) {
67
      $this->name_table = $this->definition['name table'];
68
    }
69
  }
70

    
71
  function init(&$view, &$options) {
72
    parent::init($view, $options);
73

    
74
    // Compatibility: The new UI changed several settings.
75
    if (!empty($options['wildcard']) && !isset($options['exception']['value'])) {
76
      $this->options['exception']['value'] = $options['wildcard'];
77
    }
78
    if (!empty($options['wildcard_substitution']) && !isset($options['exception']['title'])) {
79
      // Enable the checkbox if the title is filled in.
80
      $this->options['exception']['title_enable'] = 1;
81
      $this->options['exception']['title'] = $options['wildcard_substitution'];
82
    }
83

    
84
    if (!isset($options['summary']['format']) && !empty($options['style_plugin'])) {
85
      $this->options['summary']['format'] = $options['style_plugin'];
86
    }
87

    
88
    // Setup default value.
89
    $options['style_options'] = isset($options['style_options']) ? $options['style_options'] : array();
90

    
91
    if (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary asc') {
92
      $this->options['default_action'] = 'summary';
93
      $this->options['summary']['sort_order'] = 'asc';
94
      $this->options['summary']['number_of_records'] = 0;
95
      $this->options['summary_options'] = $options['style_options'];
96
    }
97
    elseif (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary desc') {
98
      $this->options['default_action'] = 'summary';
99
      $this->options['summary']['sort_order'] = 'desc';
100
      $this->options['summary']['number_of_records'] = 0;
101
      $this->options['summary_options'] = $options['style_options'];
102
    }
103
    elseif (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary asc by count') {
104
      $this->options['default_action'] = 'summary';
105
      $this->options['summary']['sort_order'] = 'asc';
106
      $this->options['summary']['number_of_records'] = 1;
107
      $this->options['summary_options'] = $options['style_options'];
108
    }
109
    elseif (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary desc by count') {
110
      $this->options['default_action'] = 'summary';
111
      $this->options['summary']['sort_order'] = 'desc';
112
      $this->options['summary']['number_of_records'] = 1;
113
      $this->options['summary_options'] = $options['style_options'];
114
    }
115

    
116
    if (!empty($options['title']) && !isset($options['title_enable'])) {
117
      $this->options['title_enable'] = 1;
118
    }
119
    if (!empty($options['breadcrumb']) && !isset($options['breadcrumb_enable'])) {
120
      $this->options['breadcrumb_enable'] = 1;
121
    }
122

    
123
    if (!empty($options['validate_type']) && !isset($options['validate']['type'])) {
124
      $this->options['validate']['type'] = $options['validate_type'];
125
      $this->options['specify_validation'] = 1;
126
    }
127
    if (!empty($options['validate_fail']) && !isset($options['validate']['fail'])) {
128
      $this->options['validate']['fail'] = $options['validate_fail'];
129
      $this->options['specify_validation'] = 1;
130
    }
131
  }
132

    
133
  /**
134
   * Give an argument the opportunity to modify the breadcrumb, if it wants.
135
   * This only gets called on displays where a breadcrumb is actually used.
136
   *
137
   * The breadcrumb will be in the form of an array, with the keys being
138
   * the path and the value being the already sanitized title of the path.
139
   */
140
  function set_breadcrumb(&$breadcrumb) { }
141

    
142
  /**
143
   * Determine if the argument can generate a breadcrumb
144
   *
145
   * @return TRUE/FALSE
146
   */
147
  function uses_breadcrumb() {
148
    $info = $this->default_actions($this->options['default_action']);
149
    return !empty($info['breadcrumb']);
150
  }
151

    
152
  function is_exception($arg = NULL) {
153
    if (!isset($arg)) {
154
      $arg = isset($this->argument) ? $this->argument : NULL;
155
    }
156
    return !empty($this->options['exception']['value']) && $this->options['exception']['value'] === $arg;
157
  }
158

    
159
  function exception_title() {
160
    // If title overriding is off for the exception, return the normal title.
161
    if (empty($this->options['exception']['title_enable'])) {
162
      return $this->get_title();
163
    }
164
    return $this->options['exception']['title'];
165
  }
166

    
167
  /**
168
   * Determine if the argument needs a style plugin.
169
   *
170
   * @return TRUE/FALSE
171
   */
172
  function needs_style_plugin() {
173
    $info = $this->default_actions($this->options['default_action']);
174
    $validate_info = $this->default_actions($this->options['validate']['fail']);
175
    return !empty($info['style plugin']) || !empty($validate_info['style plugin']);
176
  }
177

    
178
  function option_definition() {
179
    $options = parent::option_definition();
180

    
181
    $options['default_action'] = array('default' => 'ignore');
182
    $options['exception'] = array(
183
      'contains' => array(
184
        'value' => array('default' => 'all'),
185
        'title_enable' => array('default' => FALSE, 'bool' => TRUE),
186
        'title' => array('default' => 'All', 'translatable' => TRUE),
187
      ),
188
    );
189
    $options['title_enable'] = array('default' => FALSE, 'bool' => TRUE);
190
    $options['title'] = array('default' => '', 'translatable' => TRUE);
191
    $options['breadcrumb_enable'] = array('default' => FALSE, 'bool' => TRUE);
192
    $options['breadcrumb'] = array('default' => '', 'translatable' => TRUE);
193
    $options['default_argument_type'] = array('default' => 'fixed', 'export' => 'export_plugin');
194
    $options['default_argument_options'] = array('default' => array(), 'export' => FALSE);
195
    $options['default_argument_skip_url'] = array('default' => FALSE, 'bool' => TRUE);
196
    $options['summary_options'] = array('default' => array(), 'export' => FALSE);
197
    $options['summary'] = array(
198
      'contains' => array(
199
        'sort_order' => array('default' => 'asc'),
200
        'number_of_records' => array('default' => 0),
201
        'format' => array('default' => 'default_summary', 'export' => 'export_summary'),
202
      ),
203
    );
204
    $options['specify_validation'] = array('default' => FALSE, 'bool' => TRUE);
205
    $options['validate'] = array(
206
      'contains' => array(
207
        'type' => array('default' => 'none', 'export' => 'export_validation'),
208
        'fail' => array('default' => 'not found'),
209
      ),
210
    );
211
    $options['validate_options'] = array('default' => array(), 'export' => FALSE);
212

    
213
    return $options;
214
  }
215

    
216
  function options_form(&$form, &$form_state) {
217
    parent::options_form($form, $form_state);
218

    
219
    $argument_text = $this->view->display_handler->get_argument_text();
220

    
221
    $form['#pre_render'][] = 'views_ui_pre_render_move_argument_options';
222

    
223
    $form['description'] = array(
224
      '#markup' => $argument_text['description'],
225
      '#theme_wrappers' => array('container'),
226
      '#attributes' => array('class' => array('description')),
227
    );
228

    
229
    $form['no_argument'] = array(
230
      '#type' => 'fieldset',
231
      '#title' => $argument_text['filter value not present'],
232
    );
233
    // Everything in the fieldset is floated, so the last element needs to
234
    // clear those floats.
235
    $form['no_argument']['clearfix'] = array(
236
      '#weight' => 1000,
237
      '#markup' => '<div class="clearfix"></div>',
238
    );
239
    $form['default_action'] = array(
240
      '#type' => 'radios',
241
      '#process' => array('views_ui_process_container_radios'),
242
      '#default_value' => $this->options['default_action'],
243
      '#fieldset' => 'no_argument',
244
    );
245

    
246
    $form['exception'] = array(
247
      '#type' => 'fieldset',
248
      '#title' => t('Exceptions'),
249
      '#collapsible' => TRUE,
250
      '#collapsed' => TRUE,
251
      '#fieldset' => 'no_argument',
252
    );
253
    $form['exception']['value'] = array(
254
      '#type' => 'textfield',
255
      '#title' => t('Exception value'),
256
      '#size' => 20,
257
      '#default_value' => $this->options['exception']['value'],
258
      '#description' => t('If this value is received, the filter will be ignored; i.e, "all values"'),
259
    );
260
    $form['exception']['title_enable'] = array(
261
      '#type' => 'checkbox',
262
      '#title' => t('Override title'),
263
      '#default_value' => $this->options['exception']['title_enable'],
264
    );
265
    $form['exception']['title'] = array(
266
      '#type' => 'textfield',
267
      '#title' => t('Override title'),
268
      '#title_display' => 'invisible',
269
      '#size' => 20,
270
      '#default_value' => $this->options['exception']['title'],
271
      '#description' => t('Override the view and other argument titles. Use "%1" for the first argument, "%2" for the second, etc.'),
272
      '#dependency' => array(
273
        'edit-options-exception-title-enable' => array('1'),
274
      ),
275
    );
276

    
277
    $options = array();
278
    $defaults = $this->default_actions();
279
    $validate_options = array();
280
    foreach ($defaults as $id => $info) {
281
      $options[$id] = $info['title'];
282
      if (empty($info['default only'])) {
283
        $validate_options[$id] = $info['title'];
284
      }
285
      if (!empty($info['form method'])) {
286
        $this->{$info['form method']}($form, $form_state);
287
      }
288
    }
289
    $form['default_action']['#options'] = $options;
290

    
291
    $form['argument_present'] = array(
292
      '#type' => 'fieldset',
293
      '#title' => $argument_text['filter value present'],
294
    );
295
    $form['title_enable'] = array(
296
      '#type' => 'checkbox',
297
      '#title' => t('Override title'),
298
      '#default_value' => $this->options['title_enable'],
299
      '#fieldset' => 'argument_present',
300
    );
301
    $form['title'] = array(
302
      '#type' => 'textfield',
303
      '#title' => t('Provide title'),
304
      '#title_display' => 'invisible',
305
      '#default_value' => $this->options['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-title-enable' => array('1'),
309
      ),
310
      '#fieldset' => 'argument_present',
311
    );
312

    
313
    $form['breadcrumb_enable'] = array(
314
      '#type' => 'checkbox',
315
      '#title' => t('Override breadcrumb'),
316
      '#default_value' => $this->options['breadcrumb_enable'],
317
      '#fieldset' => 'argument_present',
318
    );
319
    $form['breadcrumb'] = array(
320
      '#type' => 'textfield',
321
      '#title' => t('Provide breadcrumb'),
322
      '#title_display' => 'invisible',
323
      '#default_value' => $this->options['breadcrumb'],
324
      '#description' => t('Enter a breadcrumb name you would like to use. See "Title" for percent substitutions.'),
325
      '#dependency' => array(
326
        'edit-options-breadcrumb-enable' => array('1'),
327
      ),
328
      '#fieldset' => 'argument_present',
329
    );
330

    
331
    $form['specify_validation'] = array(
332
      '#type' => 'checkbox',
333
      '#title' => t('Specify validation criteria'),
334
      '#default_value' => $this->options['specify_validation'],
335
      '#fieldset' => 'argument_present',
336
    );
337

    
338
    $form['validate'] = array(
339
      '#type' => 'container',
340
      '#fieldset' => 'argument_present',
341
    );
342
    // @todo The mockup wanted to use "Validate using" here, but it doesn't
343
    // work well with many options (they'd need to be changed as well)
344
    $form['validate']['type'] = array(
345
      '#type' => 'select',
346
      '#title' => t('Validator'),
347
      '#default_value' => $this->options['validate']['type'],
348
      '#dependency' => array(
349
        'edit-options-specify-validation' => array('1'),
350
      ),
351
    );
352

    
353
    $validate_types = array('none' => t('- Basic validation -'));
354
    $plugins = views_fetch_plugin_data('argument validator');
355
    foreach ($plugins as $id => $info) {
356
      if (!empty($info['no ui'])) {
357
        continue;
358
      }
359

    
360
      $valid = TRUE;
361
      if (!empty($info['type'])) {
362
        $valid = FALSE;
363
        if (empty($this->definition['validate type'])) {
364
          continue;
365
        }
366
        foreach ((array) $info['type'] as $type) {
367
          if ($type == $this->definition['validate type']) {
368
            $valid = TRUE;
369
            break;
370
          }
371
        }
372
      }
373

    
374
      // If we decide this validator is ok, add it to the list.
375
      if ($valid) {
376
        $plugin = $this->get_plugin('argument validator', $id);
377
        if ($plugin) {
378
          if ($plugin->access() || $this->options['validate']['type'] == $id) {
379
            $form['validate']['options'][$id] = array(
380
              '#prefix' => '<div id="edit-options-validate-options-' . $id . '-wrapper">',
381
              '#suffix' => '</div>',
382
              '#type' => 'item',
383
              // Even if the plugin has no options add the key to the form_state.
384
              '#input' => TRUE, // trick it into checking input to make #process run
385
              '#dependency' => array(
386
                'edit-options-specify-validation' => array('1'),
387
                'edit-options-validate-type' => array($id),
388
              ),
389
              '#dependency_count' => 2,
390
              '#id' => 'edit-options-validate-options-' . $id,
391
            );
392
            $plugin->options_form($form['validate']['options'][$id], $form_state);
393
            $validate_types[$id] = $info['title'];
394
          }
395
        }
396
      }
397
    }
398

    
399
    asort($validate_types);
400
    $form['validate']['type']['#options'] = $validate_types;
401

    
402
    $form['validate']['fail'] = array(
403
      '#type' => 'select',
404
      '#title' => t('Action to take if filter value does not validate'),
405
      '#default_value' => $this->options['validate']['fail'],
406
      '#options' => $validate_options,
407
      '#dependency' => array(
408
        'edit-options-specify-validation' => array('1'),
409
      ),
410
      '#fieldset' => 'argument_present',
411
    );
412
  }
413

    
414
  function options_validate(&$form, &$form_state) {
415
    if (empty($form_state['values']['options'])) {
416
      return;
417
    }
418

    
419
    // Let the plugins do validation.
420
    $default_id = $form_state['values']['options']['default_argument_type'];
421
    $plugin = $this->get_plugin('argument default', $default_id);
422
    if ($plugin && isset($form['argument_default'][$default_id]) && isset($form_state['values']['options']['argument_default'][$default_id])) {
423
      $plugin->options_validate($form['argument_default'][$default_id], $form_state, $form_state['values']['options']['argument_default'][$default_id]);
424
    }
425

    
426
    // Validate summary plugin options if one is present.
427
    if (isset($form_state['values']['options']['summary']['format'])) {
428
      $summary_id = $form_state['values']['options']['summary']['format'];
429
      $plugin = $this->get_plugin('style', $summary_id);
430
      if ($plugin) {
431
        $plugin->options_validate($form['summary']['options'][$summary_id], $form_state, $form_state['values']['options']['summary']['options'][$summary_id]);
432
      }
433
    }
434

    
435
    $validate_id = $form_state['values']['options']['validate']['type'];
436
    $plugin = $this->get_plugin('argument validator', $validate_id);
437
    if ($plugin) {
438
      $plugin->options_validate($form['validate']['options'][$default_id], $form_state, $form_state['values']['options']['validate']['options'][$validate_id]);
439
    }
440

    
441
  }
442

    
443
  function options_submit(&$form, &$form_state) {
444
    if (empty($form_state['values']['options'])) {
445
      return;
446
    }
447

    
448
    // Let the plugins make submit modifications if necessary.
449
    $default_id = $form_state['values']['options']['default_argument_type'];
450
    $plugin = $this->get_plugin('argument default', $default_id);
451
    if ($plugin) {
452
      $options = &$form_state['values']['options']['argument_default'][$default_id];
453
      $plugin->options_submit($form['argument_default'][$default_id], $form_state, $options);
454
      // Copy the now submitted options to their final resting place so they get saved.
455
      $form_state['values']['options']['default_argument_options'] = $options;
456
    }
457

    
458
    // Handle summary plugin options if one is present.
459
    if (isset($form_state['values']['options']['summary']['format'])) {
460
      $summary_id = $form_state['values']['options']['summary']['format'];
461
      $plugin = $this->get_plugin('style', $summary_id);
462
      if ($plugin) {
463
        $options = &$form_state['values']['options']['summary']['options'][$summary_id];
464
        $plugin->options_submit($form['summary']['options'][$summary_id], $form_state, $options);
465
        // Copy the now submitted options to their final resting place so they get saved.
466
        $form_state['values']['options']['summary_options'] = $options;
467
      }
468
    }
469

    
470
    $validate_id = $form_state['values']['options']['validate']['type'];
471
    $plugin = $this->get_plugin('argument validator', $validate_id);
472
    if ($plugin) {
473
      $options = &$form_state['values']['options']['validate']['options'][$validate_id];
474
      $plugin->options_submit($form['validate']['options'][$validate_id], $form_state, $options);
475
      // Copy the now submitted options to their final resting place so they get saved.
476
      $form_state['values']['options']['validate_options'] = $options;
477
    }
478

    
479
    // Clear out the content of title if it's not enabled.
480
    $options =& $form_state['values']['options'];
481
    if (empty($options['title_enable'])) {
482
      $options['title'] = '';
483
    }
484
  }
485

    
486
  /**
487
   * Provide a list of default behaviors for this argument if the argument
488
   * is not present.
489
   *
490
   * Override this method to provide additional (or fewer) default behaviors.
491
   */
492
  function default_actions($which = NULL) {
493
    $defaults = array(
494
      'ignore' => array(
495
        'title' => t('Display all results for the specified field'),
496
        'method' => 'default_ignore',
497
        'breadcrumb' => TRUE, // generate a breadcrumb to here
498
      ),
499
      'default' => array(
500
        'title' => t('Provide default value'),
501
        'method' => 'default_default',
502
        'form method' => 'default_argument_form',
503
        'has default argument' => TRUE,
504
        'default only' => TRUE, // this can only be used for missing argument, not validation failure
505
        'breadcrumb' => TRUE, // generate a breadcrumb to here
506
      ),
507
      'not found' => array(
508
        'title' => t('Hide view'),
509
        'method' => 'default_not_found',
510
        'hard fail' => TRUE, // This is a hard fail condition
511
      ),
512
      'summary' => array(
513
        'title' => t('Display a summary'),
514
        'method' => 'default_summary',
515
        'form method' => 'default_summary_form',
516
        'style plugin' => TRUE,
517
        'breadcrumb' => TRUE, // generate a breadcrumb to here
518
      ),
519
      'empty' => array(
520
        'title' => t('Display contents of "No results found"'),
521
        'method' => 'default_empty',
522
        'breadcrumb' => TRUE, // generate a breadcrumb to here
523
      ),
524
      'access denied' => array(
525
        'title' => t('Display "Access Denied"'),
526
        'method' => 'default_access_denied',
527
        'breadcrumb' => FALSE, // generate a breadcrumb to here
528
      ),
529
    );
530

    
531
    if ($this->view->display_handler->has_path()) {
532
      $defaults['not found']['title'] = t('Show "Page not found"');
533
    }
534

    
535
    if ($which) {
536
      if (!empty($defaults[$which])) {
537
        return $defaults[$which];
538
      }
539
    }
540
    else {
541
      return $defaults;
542
    }
543
  }
544

    
545
  /**
546
   * Provide a form for selecting the default argument when the
547
   * default action is set to provide default argument.
548
   */
549
  function default_argument_form(&$form, &$form_state) {
550
    $plugins = views_fetch_plugin_data('argument default');
551
    $options = array();
552

    
553
    $form['default_argument_skip_url'] = array(
554
      '#type' => 'checkbox',
555
      '#title' => t('Skip default argument for view URL'),
556
      '#default_value' => $this->options['default_argument_skip_url'],
557
      '#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.')
558
    );
559

    
560
    $form['default_argument_type'] = array(
561
      '#prefix' => '<div id="edit-options-default-argument-type-wrapper">',
562
      '#suffix' => '</div>',
563
      '#type' => 'select',
564
      '#id' => 'edit-options-default-argument-type',
565
      '#title' => t('Type'),
566
      '#default_value' => $this->options['default_argument_type'],
567

    
568
      '#dependency' => array('radio:options[default_action]' => array('default')),
569
      // Views custom key, moves this element to the appropriate container
570
      // under the radio button.
571
      '#argument_option' => 'default',
572
    );
573

    
574
    foreach ($plugins as $id => $info) {
575
      if (!empty($info['no ui'])) {
576
        continue;
577
      }
578
      $plugin = $this->get_plugin('argument default', $id);
579
      if ($plugin) {
580
        if ($plugin->access() || $this->options['default_argument_type'] == $id) {
581
          $form['argument_default']['#argument_option'] = 'default';
582
          $form['argument_default'][$id] = array(
583
            '#prefix' => '<div id="edit-options-argument-default-options-' . $id . '-wrapper">',
584
            '#suffix' => '</div>',
585
            '#id' => 'edit-options-argument-default-options-' . $id,
586
            '#type' => 'item',
587
            // Even if the plugin has no options add the key to the form_state.
588
            '#input' => TRUE,
589
            '#dependency' => array(
590
              'radio:options[default_action]' => array('default'),
591
              'edit-options-default-argument-type' => array($id)
592
            ),
593
            '#dependency_count' => 2,
594
          );
595
          $options[$id] = $info['title'];
596
          $plugin->options_form($form['argument_default'][$id], $form_state);
597
        }
598
      }
599
    }
600

    
601
    asort($options);
602
    $form['default_argument_type']['#options'] = $options;
603
  }
604

    
605
  /**
606
   * Provide a form for selecting further summary options when the
607
   * default action is set to display one.
608
   */
609
  function default_summary_form(&$form, &$form_state) {
610
    $style_plugins = views_fetch_plugin_data('style');
611
    $summary_plugins = array();
612
    $format_options = array();
613
    foreach ($style_plugins as $key => $plugin) {
614
      if (isset($plugin['type']) && $plugin['type'] == 'summary') {
615
        $summary_plugins[$key] = $plugin;
616
        $format_options[$key] = $plugin['title'];
617
      }
618
    }
619

    
620
    $form['summary'] = array(
621
      // Views custom key, moves this element to the appropriate container
622
      // under the radio button.
623
      '#argument_option' => 'summary',
624
    );
625
    $form['summary']['sort_order'] = array(
626
      '#type' => 'radios',
627
      '#title' => t('Sort order'),
628
      '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
629
      '#default_value' => $this->options['summary']['sort_order'],
630
      '#dependency' => array('radio:options[default_action]' => array('summary')),
631
    );
632
    $form['summary']['number_of_records'] = array(
633
      '#type' => 'radios',
634
      '#title' => t('Sort by'),
635
      '#default_value' => $this->options['summary']['number_of_records'],
636
      '#options' => array(
637
        0 => $this->get_sort_name(),
638
        1 => t('Number of records')
639
      ),
640
      '#dependency' => array('radio:options[default_action]' => array('summary')),
641
    );
642

    
643
    $form['summary']['format'] = array(
644
      '#type' => 'radios',
645
      '#title' => t('Format'),
646
      '#options' => $format_options,
647
      '#default_value' => $this->options['summary']['format'],
648
      '#dependency' => array('radio:options[default_action]' => array('summary')),
649
    );
650

    
651
    foreach ($summary_plugins as $id => $info) {
652
      if (empty($info['uses options'])) {
653
        continue;
654
      }
655
      $plugin = $this->get_plugin('style', $id);
656
      if ($plugin) {
657
        $form['summary']['options'][$id] = array(
658
          '#prefix' => '<div id="edit-options-summary-options-' . $id . '-wrapper">',
659
          '#suffix' => '</div>',
660
          '#id' => 'edit-options-summary-options-' . $id,
661
          '#type' => 'item',
662
          '#input' => TRUE, // trick it into checking input to make #process run
663
          '#dependency' => array(
664
            'radio:options[default_action]' => array('summary'),
665
            'radio:options[summary][format]' => array($id),
666
          ),
667
          '#dependency_count' => 2,
668
        );
669
        $options[$id] = $info['title'];
670
        $plugin->options_form($form['summary']['options'][$id], $form_state);
671
      }
672
    }
673
  }
674

    
675
  /**
676
   * Handle the default action, which means our argument wasn't present.
677
   *
678
   * Override this method only with extreme care.
679
   *
680
   * @return
681
   *   A boolean value; if TRUE, continue building this view. If FALSE,
682
   *   building the view will be aborted here.
683
   */
684
  function default_action($info = NULL) {
685
    if (!isset($info)) {
686
      $info = $this->default_actions($this->options['default_action']);
687
    }
688

    
689
    if (!$info) {
690
      return FALSE;
691
    }
692

    
693
    if (!empty($info['method args'])) {
694
      return call_user_func_array(array(&$this, $info['method']), $info['method args']);
695
    }
696
    else {
697
      return $this->{$info['method']}();
698
    }
699
  }
700

    
701
  /**
702
   * How to act if validation failes
703
   */
704
  function validate_fail() {
705
    $info = $this->default_actions($this->options['validate']['fail']);
706
    return $this->default_action($info);
707
  }
708
  /**
709
   * Default action: ignore.
710
   *
711
   * If an argument was expected and was not given, in this case, simply
712
   * ignore the argument entirely.
713
   */
714
  function default_ignore() {
715
    return TRUE;
716
  }
717

    
718
  /**
719
   * Default action: not found.
720
   *
721
   * If an argument was expected and was not given, in this case, report
722
   * the view as 'not found' or hide it.
723
   */
724
  function default_not_found() {
725
    // Set a failure condition and let the display manager handle it.
726
    $this->view->build_info['fail'] = TRUE;
727
    return FALSE;
728
  }
729

    
730
  /**
731
   * Default action: access denied.
732
   *
733
   * If an argument was expected and was not given, in this case, report
734
   * the view as 'access denied'.
735
   */
736
  function default_access_denied() {
737
    $this->view->build_info['denied'] = TRUE;
738
    return FALSE;
739
  }
740

    
741
  /**
742
   * Default action: empty
743
   *
744
   * If an argument was expected and was not given, in this case, display
745
   * the view's empty text
746
   */
747
  function default_empty() {
748
    // We return with no query; this will force the empty text.
749
    $this->view->built = TRUE;
750
    $this->view->executed = TRUE;
751
    $this->view->result = array();
752
    return FALSE;
753
  }
754

    
755
  /**
756
   * This just returns true. The view argument builder will know where
757
   * to find the argument from.
758
   */
759
  function default_default() {
760
    return TRUE;
761
  }
762

    
763
  /**
764
   * Determine if the argument is set to provide a default argument.
765
   */
766
  function has_default_argument() {
767
    $info = $this->default_actions($this->options['default_action']);
768
    return !empty($info['has default argument']);
769
  }
770

    
771
  /**
772
   * Get a default argument, if available.
773
   */
774
  function get_default_argument() {
775
    $plugin = $this->get_plugin('argument default');
776
    if ($plugin) {
777
      return $plugin->get_argument();
778
    }
779
  }
780

    
781
  /**
782
   * Process the summary arguments for display.
783
   *
784
   * For example, the validation plugin may want to alter an argument for use in
785
   * the URL.
786
   */
787
  function process_summary_arguments(&$args) {
788
    if ($this->options['validate']['type'] != 'none') {
789
      if (isset($this->validator) || $this->validator = $this->get_plugin('argument validator')) {
790
        $this->validator->process_summary_arguments($args);
791
      }
792
    }
793
  }
794

    
795
  /**
796
   * Default action: summary.
797
   *
798
   * If an argument was expected and was not given, in this case, display
799
   * a summary query.
800
   */
801
  function default_summary() {
802
    $this->view->build_info['summary'] = TRUE;
803
    $this->view->build_info['summary_level'] = $this->options['id'];
804

    
805
    // Change the display style to the summary style for this
806
    // argument.
807
    $this->view->plugin_name = $this->options['summary']['format'];
808
    $this->view->style_options = $this->options['summary_options'];
809

    
810
    // Clear out the normal primary field and whatever else may have
811
    // been added and let the summary do the work.
812
    $this->query->clear_fields();
813
    $this->summary_query();
814

    
815
    $by = $this->options['summary']['number_of_records'] ? 'num_records' : NULL;
816
    $this->summary_sort($this->options['summary']['sort_order'], $by);
817

    
818
    // Summaries have their own sorting and fields, so tell the View not
819
    // to build these.
820
    $this->view->build_sort = $this->view->build_fields = FALSE;
821
    return TRUE;
822
  }
823

    
824
  /**
825
   * Build the info for the summary query.
826
   *
827
   * This must:
828
   * - add_groupby: group on this field in order to create summaries.
829
   * - add_field: add a 'num_nodes' field for the count. Usually it will
830
   *   be a count on $view->base_field
831
   * - set_count_field: Reset the count field so we get the right paging.
832
   *
833
   * @return
834
   *   The alias used to get the number of records (count) for this entry.
835
   */
836
  function summary_query() {
837
    $this->ensure_my_table();
838
    // Add the field.
839
    $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
840

    
841
    $this->summary_name_field();
842
    return $this->summary_basics();
843
  }
844

    
845
  /**
846
   * Add the name field, which is the field displayed in summary queries.
847
   * This is often used when the argument is numeric.
848
   */
849
  function summary_name_field() {
850
    // Add the 'name' field. For example, if this is a uid argument, the
851
    // name field would be 'name' (i.e, the username).
852

    
853
    if (isset($this->name_table)) {
854
      // if the alias is different then we're probably added, not ensured,
855
      // so look up the join and add it instead.
856
      if ($this->table_alias != $this->name_table) {
857
        $j = views_get_table_join($this->name_table, $this->table);
858
        if ($j) {
859
          $join = clone $j;
860
          $join->left_table = $this->table_alias;
861
          $this->name_table_alias = $this->query->add_table($this->name_table, $this->relationship, $join);
862
        }
863
      }
864
      else {
865
        $this->name_table_alias = $this->query->ensure_table($this->name_table, $this->relationship);
866
      }
867
    }
868
    else {
869
      $this->name_table_alias = $this->table_alias;
870
    }
871

    
872
    if (isset($this->name_field)) {
873
      $this->name_alias = $this->query->add_field($this->name_table_alias, $this->name_field);
874
    }
875
    else {
876
      $this->name_alias = $this->base_alias;
877
    }
878
  }
879

    
880
  /**
881
   * Some basic summary behavior that doesn't need to be repeated as much as
882
   * code that goes into summary_query()
883
   */
884
  function summary_basics($count_field = TRUE) {
885
    // Add the number of nodes counter
886
    $distinct = ($this->view->display_handler->get_option('distinct') && empty($this->query->no_distinct));
887

    
888
    $count_alias = $this->query->add_field($this->query->base_table, $this->query->base_field, 'num_records',
889
                                           array('count' => TRUE, 'distinct' => $distinct));
890
    $this->query->add_groupby($this->name_alias);
891

    
892
    if ($count_field) {
893
      $this->query->set_count_field($this->table_alias, $this->real_field);
894
    }
895

    
896
    $this->count_alias = $count_alias;
897
  }
898

    
899
  /**
900
   * Sorts the summary based upon the user's selection. The base variant of
901
   * this is usually adequte.
902
   *
903
   * @param $order
904
   *   The order selected in the UI.
905
   */
906
  function summary_sort($order, $by = NULL) {
907
    $this->query->add_orderby(NULL, NULL, $order, (!empty($by) ? $by : $this->name_alias));
908
  }
909

    
910
  /**
911
   * Provide the argument to use to link from the summary to the next level;
912
   * this will be called once per row of a summary, and used as part of
913
   * $view->get_url().
914
   *
915
   * @param $data
916
   *   The query results for the row.
917
   */
918
  function summary_argument($data) {
919
    return $data->{$this->base_alias};
920
  }
921

    
922
  /**
923
   * Provides the name to use for the summary. By default this is just
924
   * the name field.
925
   *
926
   * @param $data
927
   *   The query results for the row.
928
   */
929
  function summary_name($data) {
930
    $value = $data->{$this->name_alias};
931
    if (empty($value) && !empty($this->definition['empty field name'])) {
932
      $value = $this->definition['empty field name'];
933
    }
934
    return check_plain($value);
935
  }
936

    
937
  /**
938
   * Set up the query for this argument.
939
   *
940
   * The argument sent may be found at $this->argument.
941
   */
942
  function query($group_by = FALSE) {
943
    $this->ensure_my_table();
944
    $this->query->add_where(0, "$this->table_alias.$this->real_field", $this->argument);
945
  }
946

    
947
  /**
948
   * Get the title this argument will assign the view, given the argument.
949
   *
950
   * This usually needs to be overridden to provide a proper title.
951
   */
952
  function title() {
953
    return check_plain($this->argument);
954
  }
955

    
956
  /**
957
   * Called by the view object to get the title. This may be set by a
958
   * validator so we don't necessarily call through to title().
959
   */
960
  function get_title() {
961
    if (isset($this->validated_title)) {
962
      return $this->validated_title;
963
    }
964
    else {
965
      return $this->title();
966
    }
967
  }
968

    
969
  /**
970
   * Validate that this argument works. By default, all arguments are valid.
971
   */
972
  function validate_arg($arg) {
973
    // By using % in URLs, arguments could be validated twice; this eases
974
    // that pain.
975
    if (isset($this->argument_validated)) {
976
      return $this->argument_validated;
977
    }
978

    
979
    if ($this->is_exception($arg)) {
980
      return $this->argument_validated = TRUE;
981
    }
982

    
983
    if ($this->options['validate']['type'] == 'none') {
984
      return $this->argument_validated = $this->validate_argument_basic($arg);
985
    }
986

    
987
    $plugin = $this->get_plugin('argument validator');
988
    if ($plugin) {
989
      return $this->argument_validated = $plugin->validate_argument($arg);
990
    }
991

    
992
    // If the plugin isn't found, fall back to the basic validation path:
993
    return $this->argument_validated = $this->validate_argument_basic($arg);
994
  }
995

    
996
  /**
997
   * Called by the menu system to validate an argument.
998
   *
999
   * This checks to see if this is a 'soft fail', which means that if the
1000
   * argument fails to validate, but there is an action to take anyway,
1001
   * then validation cannot actually fail.
1002
   */
1003
  function validate_argument($arg) {
1004
    $validate_info = $this->default_actions($this->options['validate']['fail']);
1005
    if (empty($validate_info['hard fail'])) {
1006
      return TRUE;
1007
    }
1008

    
1009
    $rc = $this->validate_arg($arg);
1010

    
1011
    // If the validator has changed the validate fail condition to a
1012
    // soft fail, deal with that:
1013
    $validate_info = $this->default_actions($this->options['validate']['fail']);
1014
    if (empty($validate_info['hard fail'])) {
1015
      return TRUE;
1016
    }
1017

    
1018
    return $rc;
1019
  }
1020

    
1021
  /**
1022
   * Provide a basic argument validation.
1023
   *
1024
   * This can be overridden for more complex types; the basic
1025
   * validator only checks to see if the argument is not NULL
1026
   * or is numeric if the definition says it's numeric.
1027
   */
1028
  function validate_argument_basic($arg) {
1029
    if (!isset($arg) || $arg === '') {
1030
      return FALSE;
1031
    }
1032

    
1033
    if (!empty($this->definition['numeric']) && !isset($this->options['break_phrase']) && !is_numeric($arg)) {
1034
      return FALSE;
1035
    }
1036

    
1037
    return TRUE;
1038
  }
1039

    
1040
  /**
1041
   * Set the input for this argument
1042
   *
1043
   * @return TRUE if it successfully validates; FALSE if it does not.
1044
   */
1045
  function set_argument($arg) {
1046
    $this->argument = $arg;
1047
    return $this->validate_arg($arg);
1048
  }
1049

    
1050
  /**
1051
   * Get the value of this argument.
1052
   */
1053
  function get_value() {
1054
    // If we already processed this argument, we're done.
1055
    if (isset($this->argument)) {
1056
      return $this->argument;
1057
    }
1058

    
1059
    // Otherwise, we have to pretend to process ourself to find the value.
1060
    $value = NULL;
1061
    // Find the position of this argument within the view.
1062
    $position = 0;
1063
    foreach ($this->view->argument as $id => $argument) {
1064
      if ($id == $this->options['id']) {
1065
        break;
1066
      }
1067
      $position++;
1068
    }
1069

    
1070
    $arg = isset($this->view->args[$position]) ? $this->view->args[$position] : NULL;
1071
    $this->position = $position;
1072

    
1073
    // Clone ourselves so that we don't break things when we're really
1074
    // processing the arguments.
1075
    $argument = clone $this;
1076
    if (!isset($arg) && $argument->has_default_argument()) {
1077
      $arg = $argument->get_default_argument();
1078

    
1079
      // remember that this argument was computed, not passed on the URL.
1080
      $this->is_default = TRUE;
1081
    }
1082
    // Set the argument, which will also validate that the argument can be set.
1083
    if ($argument->set_argument($arg)) {
1084
      $value = $argument->argument;
1085
    }
1086
    unset($argument);
1087
    return $value;
1088
  }
1089

    
1090
  /**
1091
   * Export handler for summary export.
1092
   *
1093
   * Arguments can have styles for the summary view. This special export
1094
   * handler makes sure this works properly.
1095
   */
1096
  function export_summary($indent, $prefix, $storage, $option, $definition, $parents) {
1097
    $output = '';
1098
    $name = $this->options['summary'][$option];
1099
    $options = $this->options['summary_options'];
1100

    
1101
    $plugin = views_get_plugin('style', $name);
1102
    if ($plugin) {
1103
      $plugin->init($this->view, $this->view->display_handler->display, $options);
1104
      // Write which plugin to use.
1105
      $output .= $indent . $prefix . "['summary']['$option'] = '$name';\n";
1106

    
1107
      // Pass off to the plugin to export itself.
1108
      $output .= $plugin->export_options($indent, $prefix . "['summary_options']");
1109
    }
1110

    
1111
    return $output;
1112
  }
1113

    
1114
  /**
1115
   * Export handler for validation export.
1116
   *
1117
   * Arguments use validation plugins. This special export handler makes sure
1118
   * this works properly.
1119
   */
1120
  function export_validation($indent, $prefix, $storage, $option, $definition, $parents) {
1121
    $output = '';
1122
    $name = $this->options['validate'][$option];
1123
    $options = $this->options['validate_options'];
1124

    
1125
    $plugin = views_get_plugin('argument validator', $name);
1126
    if ($plugin) {
1127
      $plugin->init($this->view, $this->display, $options);
1128
      // Write which plugin to use.
1129
      $output .= $indent . $prefix . "['validate']['$option'] = '$name';\n";
1130

    
1131
      // Pass off to the plugin to export itself.
1132
      $output .= $plugin->export_options($indent, $prefix . "['validate_options']");
1133
    }
1134

    
1135
    return $output;
1136
  }
1137

    
1138
  /**
1139
   * Generic plugin export handler.
1140
   *
1141
   * Since style and validation plugins have their own export handlers, this
1142
   * one is currently only used for default argument plugins.
1143
   */
1144
  function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
1145
    $output = '';
1146
    if ($option == 'default_argument_type') {
1147
      $type = 'argument default';
1148
      $option_name = 'default_argument_options';
1149
    }
1150

    
1151
    $plugin = $this->get_plugin($type);
1152
    $name = $this->options[$option];
1153

    
1154
    if ($plugin) {
1155
      // Write which plugin to use.
1156
      $output .= $indent . $prefix . "['$option'] = '$name';\n";
1157

    
1158
      // Pass off to the plugin to export itself.
1159
      $output .= $plugin->export_options($indent, $prefix . "['$option_name']");
1160
    }
1161

    
1162
    return $output;
1163
  }
1164

    
1165
  /**
1166
   * Get the display or row plugin, if it exists.
1167
   */
1168
  function get_plugin($type = 'argument default', $name = NULL) {
1169
    $options = array();
1170
    switch ($type) {
1171
      case 'argument default':
1172
        $plugin_name = $this->options['default_argument_type'];
1173
        $options_name = 'default_argument_options';
1174
        break;
1175
      case 'argument validator':
1176
        $plugin_name = $this->options['validate']['type'];
1177
        $options_name = 'validate_options';
1178
        break;
1179
      case 'style':
1180
        $plugin_name = $this->options['summary']['format'];
1181
        $options_name = 'summary_options';
1182
    }
1183

    
1184
    if (!$name) {
1185
      $name = $plugin_name;
1186
    }
1187

    
1188
    // we only fetch the options if we're fetching the plugin actually
1189
    // in use.
1190
    if ($name == $plugin_name) {
1191
      $options = $this->options[$options_name];
1192
    }
1193

    
1194
    $plugin = views_get_plugin($type, $name);
1195
    if ($plugin) {
1196
      // Style plugins expects different parameters as argument related plugins.
1197
      if ($type == 'style') {
1198
        $plugin->init($this->view, $this->view->display_handler->display, $options);
1199
      }
1200
      else {
1201
        $plugin->init($this->view, $this, $options);
1202
      }
1203
      return $plugin;
1204
    }
1205
  }
1206

    
1207
  /**
1208
   * Return a description of how the argument would normally be sorted.
1209
   *
1210
   * Subclasses should override this to specify what the default sort order of
1211
   * their argument is (e.g. alphabetical, numeric, date).
1212
   */
1213
  function get_sort_name() {
1214
    return t('Default sort', array(), array('context' => 'Sort order'));
1215
  }
1216
}
1217

    
1218
/**
1219
 * A special handler to take the place of missing or broken handlers.
1220
 *
1221
 * @ingroup views_argument_handlers
1222
 */
1223
class views_handler_argument_broken extends views_handler_argument {
1224
  function ui_name($short = FALSE) {
1225
    return t('Broken/missing handler');
1226
  }
1227

    
1228
  function ensure_my_table() { /* No table to ensure! */ }
1229
  function query($group_by = FALSE) { /* No query to run */ }
1230
  function options_form(&$form, &$form_state) {
1231
    $form['markup'] = array(
1232
      '#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>',
1233
    );
1234
  }
1235

    
1236
  /**
1237
   * Determine if the handler is considered 'broken'
1238
   */
1239
  function broken() { return TRUE; }
1240
}
1241

    
1242
/**
1243
 * @}
1244
 */