Projet

Général

Profil

Paste
Télécharger (41,6 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / views / handlers / views_handler_argument.inc @ 4543c6c7

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) {
423
      $plugin->options_validate($form['argument_default'][$default_id], $form_state, $form_state['values']['options']['argument_default'][$default_id]);
424
    }
425

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

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

    
439
  }
440

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

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

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

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

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

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

    
527
    if ($this->view->display_handler->has_path()) {
528
      $defaults['not found']['title'] = t('Show "Page not found"');
529
    }
530

    
531
    if ($which) {
532
      if (!empty($defaults[$which])) {
533
        return $defaults[$which];
534
      }
535
    }
536
    else {
537
      return $defaults;
538
    }
539
  }
540

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

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

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

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

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

    
597
    asort($options);
598
    $form['default_argument_type']['#options'] = $options;
599
  }
600

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

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

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

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

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

    
685
    if (!$info) {
686
      return FALSE;
687
    }
688

    
689
    if (!empty($info['method args'])) {
690
      return call_user_func_array(array(&$this, $info['method']), $info['method args']);
691
    }
692
    else {
693
      return $this->{$info['method']}();
694
    }
695
  }
696

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

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

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

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

    
751
  /**
752
   * This just returns true. The view argument builder will know where
753
   * to find the argument from.
754
   */
755
  function default_default() {
756
    return TRUE;
757
  }
758

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

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

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

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

    
801
    // Change the display style to the summary style for this
802
    // argument.
803
    $this->view->plugin_name = $this->options['summary']['format'];
804
    $this->view->style_options = $this->options['summary_options'];
805

    
806
    // Clear out the normal primary field and whatever else may have
807
    // been added and let the summary do the work.
808
    $this->query->clear_fields();
809
    $this->summary_query();
810

    
811
    $by = $this->options['summary']['number_of_records'] ? 'num_records' : NULL;
812
    $this->summary_sort($this->options['summary']['sort_order'], $by);
813

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

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

    
837
    $this->summary_name_field();
838
    return $this->summary_basics();
839
  }
840

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

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

    
868
    if (isset($this->name_field)) {
869
      $this->name_alias = $this->query->add_field($this->name_table_alias, $this->name_field);
870
    }
871
    else {
872
      $this->name_alias = $this->base_alias;
873
    }
874
  }
875

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

    
884
    $count_alias = $this->query->add_field($this->query->base_table, $this->query->base_field, 'num_records',
885
                                           array('count' => TRUE, 'distinct' => $distinct));
886
    $this->query->add_groupby($this->name_alias);
887

    
888
    if ($count_field) {
889
      $this->query->set_count_field($this->table_alias, $this->real_field);
890
    }
891

    
892
    $this->count_alias = $count_alias;
893
  }
894

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

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

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

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

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

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

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

    
975
    if ($this->is_exception($arg)) {
976
      return $this->argument_validated = TRUE;
977
    }
978

    
979
    if ($this->options['validate']['type'] == 'none') {
980
      return $this->argument_validated = $this->validate_argument_basic($arg);
981
    }
982

    
983
    $plugin = $this->get_plugin('argument validator');
984
    if ($plugin) {
985
      return $this->argument_validated = $plugin->validate_argument($arg);
986
    }
987

    
988
    // If the plugin isn't found, fall back to the basic validation path:
989
    return $this->argument_validated = $this->validate_argument_basic($arg);
990
  }
991

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

    
1005
    $rc = $this->validate_arg($arg);
1006

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

    
1014
    return $rc;
1015
  }
1016

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

    
1029
    if (!empty($this->definition['numeric']) && !isset($this->options['break_phrase']) && !is_numeric($arg)) {
1030
      return FALSE;
1031
    }
1032

    
1033
    return TRUE;
1034
  }
1035

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

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

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

    
1066
    $arg = isset($this->view->args[$position]) ? $this->view->args[$position] : NULL;
1067
    $this->position = $position;
1068

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

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

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

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

    
1103
      // Pass off to the plugin to export itself.
1104
      $output .= $plugin->export_options($indent, $prefix . "['summary_options']");
1105
    }
1106

    
1107
    return $output;
1108
  }
1109

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

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

    
1127
      // Pass off to the plugin to export itself.
1128
      $output .= $plugin->export_options($indent, $prefix . "['validate_options']");
1129
    }
1130

    
1131
    return $output;
1132
  }
1133

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

    
1147
    $plugin = $this->get_plugin($type);
1148
    $name = $this->options[$option];
1149

    
1150
    if ($plugin) {
1151
      // Write which plugin to use.
1152
      $output .= $indent . $prefix . "['$option'] = '$name';\n";
1153

    
1154
      // Pass off to the plugin to export itself.
1155
      $output .= $plugin->export_options($indent, $prefix . "['$option_name']");
1156
    }
1157

    
1158
    return $output;
1159
  }
1160

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

    
1180
    if (!$name) {
1181
      $name = $plugin_name;
1182
    }
1183

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

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

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

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

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

    
1232
  /**
1233
   * Determine if the handler is considered 'broken'
1234
   */
1235
  function broken() { return TRUE; }
1236
}
1237

    
1238
/**
1239
 * @}
1240
 */