Projet

Général

Profil

Paste
Télécharger (56,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field.inc @ 651307cd

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * @todo.
6
 */
7
8
/**
9
 * @defgroup views_field_handlers Views field handlers
10
 * @{
11
 * Handlers to tell Views how to build and display fields.
12
 *
13
 */
14
15
/**
16
 * Indicator of the render_text() method for rendering a single item.
17
 * (If no render_item() is present).
18
 */
19
define('VIEWS_HANDLER_RENDER_TEXT_PHASE_SINGLE_ITEM', 0);
20
21
/**
22
 * Indicator of the render_text() method for rendering the whole element.
23
 * (if no render_item() method is available).
24
 */
25
define('VIEWS_HANDLER_RENDER_TEXT_PHASE_COMPLETELY', 1);
26
27
/**
28
 * Indicator of the render_text() method for rendering the empty text.
29
 */
30
define('VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY', 2);
31
32
/**
33
 * Base field handler that has no options and renders an unformatted field.
34
 *
35
 * Definition terms:
36
 * - additional fields: An array of fields that should be added to the query
37
 *                      for some purpose. The array is in the form of:
38
 *                      array('identifier' => array('table' => tablename,
39
 *                      'field' => fieldname); as many fields as are necessary
40
 *                      may be in this array.
41
 * - click sortable: If TRUE, this field may be click sorted.
42
 *
43
 * @ingroup views_field_handlers
44
 */
45
class views_handler_field extends views_handler {
46
  var $field_alias = 'unknown';
47
  var $aliases = array();
48
49
  /**
50
   * The field value prior to any rewriting.
51
   *
52
   * @var mixed
53
   */
54
  public $original_value = NULL;
55
56
  /**
57
   * @var array
58
   * Stores additional fields which get's added to the query.
59
   * The generated aliases are stored in $aliases.
60
   */
61
  var $additional_fields = array();
62
63
  /**
64
   * Construct a new field handler.
65
   */
66
  function construct() {
67
    parent::construct();
68
69
    $this->additional_fields = array();
70
    if (!empty($this->definition['additional fields'])) {
71
      $this->additional_fields = $this->definition['additional fields'];
72
    }
73
74
    if (!isset($this->options['exclude'])) {
75
      $this->options['exclude'] = '';
76
    }
77
  }
78
79
  /**
80
   * Determine if this field can allow advanced rendering.
81
   *
82
   * Fields can set this to FALSE if they do not wish to allow
83
   * token based rewriting or link-making.
84
   */
85
  function allow_advanced_render() {
86
    return TRUE;
87
  }
88
89
  function init(&$view, &$options) {
90
    parent::init($view, $options);
91
  }
92
93
  /**
94
   * Called to add the field to a query.
95
   */
96
  function query() {
97
    $this->ensure_my_table();
98
    // Add the field.
99
    $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
100
    $this->field_alias = $this->query->add_field($this->table_alias, $this->real_field, NULL, $params);
101
102
    $this->add_additional_fields();
103
  }
104
105
  /**
106
   * Add 'additional' fields to the query.
107
   *
108
   * @param $fields
109
   * An array of fields. The key is an identifier used to later find the
110
   * field alias used. The value is either a string in which case it's
111
   * assumed to be a field on this handler's table; or it's an array in the
112
   * form of
113
   * @code array('table' => $tablename, 'field' => $fieldname) @endcode
114
   */
115
  function add_additional_fields($fields = NULL) {
116
    if (!isset($fields)) {
117
      // notice check
118
      if (empty($this->additional_fields)) {
119
        return;
120
      }
121
      $fields = $this->additional_fields;
122
    }
123
124
    $group_params = array();
125
    if ($this->options['group_type'] != 'group') {
126
      $group_params = array(
127
        'function' => $this->options['group_type'],
128
      );
129
    }
130
131
    if (!empty($fields) && is_array($fields)) {
132
      foreach ($fields as $identifier => $info) {
133
        if (is_array($info)) {
134
          if (isset($info['table'])) {
135
            $table_alias = $this->query->ensure_table($info['table'], $this->relationship);
136
          }
137
          else {
138
            $table_alias = $this->table_alias;
139
          }
140
141
          if (empty($table_alias)) {
142
            debug(t('Handler @handler tried to add additional_field @identifier but @table could not be added!', array('@handler' => $this->definition['handler'], '@identifier' => $identifier, '@table' => $info['table'])));
143
            $this->aliases[$identifier] = 'broken';
144
            continue;
145
          }
146
147
          $params = array();
148
          if (!empty($info['params'])) {
149
            $params = $info['params'];
150
          }
151
152
          $params += $group_params;
153
          $this->aliases[$identifier] = $this->query->add_field($table_alias, $info['field'], NULL, $params);
154
        }
155
        else {
156
          $this->aliases[$info] = $this->query->add_field($this->table_alias, $info, NULL, $group_params);
157
        }
158
      }
159
    }
160
  }
161
162
  /**
163
   * Called to determine what to tell the clicksorter.
164
   */
165
  function click_sort($order) {
166
    if (isset($this->field_alias)) {
167
      // Since fields should always have themselves already added, just
168
      // add a sort on the field.
169
      $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
170
      $this->query->add_orderby(NULL, NULL, $order, $this->field_alias, $params);
171
    }
172
  }
173
174
  /**
175
   * Determine if this field is click sortable.
176
   */
177
  function click_sortable() {
178
    return !empty($this->definition['click sortable']);
179
  }
180
181
  /**
182
   * Get this field's label.
183
   */
184
  function label() {
185
    if (!isset($this->options['label'])) {
186
      return '';
187
    }
188
    return $this->options['label'];
189
  }
190
191
  /**
192
   * Return an HTML element based upon the field's element type.
193
   */
194
  function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
195
    if ($none_supported) {
196
      if ($this->options['element_type'] === '0') {
197
        return '';
198
      }
199
    }
200
    if ($this->options['element_type']) {
201
      return check_plain($this->options['element_type']);
202
    }
203
204
    if ($default_empty) {
205
      return '';
206
    }
207
208
    if ($inline) {
209
      return 'span';
210
    }
211
212
    if (isset($this->definition['element type'])) {
213
      return $this->definition['element type'];
214
    }
215
216
    return 'span';
217
  }
218
219
  /**
220
   * Return an HTML element for the label based upon the field's element type.
221
   */
222
  function element_label_type($none_supported = FALSE, $default_empty = FALSE) {
223
    if ($none_supported) {
224
      if ($this->options['element_label_type'] === '0') {
225
        return '';
226
      }
227
    }
228
    if ($this->options['element_label_type']) {
229
      return check_plain($this->options['element_label_type']);
230
    }
231
232
    if ($default_empty) {
233
      return '';
234
    }
235
236
    return 'span';
237
  }
238
239
  /**
240
   * Return an HTML element for the wrapper based upon the field's element type.
241
   */
242
  function element_wrapper_type($none_supported = FALSE, $default_empty = FALSE) {
243
    if ($none_supported) {
244
      if ($this->options['element_wrapper_type'] === '0') {
245
        return 0;
246
      }
247
    }
248
    if ($this->options['element_wrapper_type']) {
249
      return check_plain($this->options['element_wrapper_type']);
250
    }
251
252
    if ($default_empty) {
253
      return '';
254
    }
255
256
    return 'div';
257
  }
258
259
  /**
260
   * Provide a list of elements valid for field HTML.
261
   *
262
   * This function can be overridden by fields that want more or fewer
263
   * elements available, though this seems like it would be an incredibly
264
   * rare occurence.
265
   */
266
  function get_elements() {
267
    static $elements = NULL;
268
    if (!isset($elements)) {
269
      $elements = variable_get('views_field_rewrite_elements', array(
270
        '' => t('- Use default -'),
271
        '0' => t('- None -'),
272
        'div' => 'DIV',
273
        'span' => 'SPAN',
274
        'h1' => 'H1',
275
        'h2' => 'H2',
276
        'h3' => 'H3',
277
        'h4' => 'H4',
278
        'h5' => 'H5',
279
        'h6' => 'H6',
280
        'p' => 'P',
281
        'strong' => 'STRONG',
282
        'em' => 'EM',
283
      ));
284
    }
285
286
    return $elements;
287
  }
288
289
  /**
290
   * Return the class of the field.
291
   */
292
  function element_classes($row_index = NULL) {
293
    $classes = explode(' ', $this->options['element_class']);
294
    foreach ($classes as &$class) {
295
      $class = $this->tokenize_value($class, $row_index);
296
      $class = views_clean_css_identifier($class);
297
    }
298
    return implode(' ', $classes);
299
  }
300
301
  /**
302
   * Replace a value with tokens from the last field.
303
   *
304
   * This function actually figures out which field was last and uses its
305
   * tokens so they will all be available.
306
   */
307
  function tokenize_value($value, $row_index = NULL) {
308
    if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) {
309
      $fake_item = array(
310
        'alter_text' => TRUE,
311
        'text' => $value,
312
      );
313
314
      // Use isset() because empty() will trigger on 0 and 0 is
315
      // the first row.
316
      if (isset($row_index) && isset($this->view->style_plugin->render_tokens[$row_index])) {
317
        $tokens = $this->view->style_plugin->render_tokens[$row_index];
318
      }
319
      else {
320
        // Get tokens from the last field.
321
        $last_field = end($this->view->field);
322
        if (isset($last_field->last_tokens)) {
323
          $tokens = $last_field->last_tokens;
324
        }
325
        else {
326
          $tokens = $last_field->get_render_tokens($fake_item);
327
        }
328
      }
329
330
      $value = strip_tags($this->render_altered($fake_item, $tokens));
331
      if (!empty($this->options['alter']['trim_whitespace'])) {
332
        $value = trim($value);
333
      }
334
    }
335
336
    return $value;
337
  }
338
339
  /**
340
   * Return the class of the field's label.
341
   */
342
  function element_label_classes($row_index = NULL) {
343
    $classes = explode(' ', $this->options['element_label_class']);
344
    foreach ($classes as &$class) {
345
      $class = $this->tokenize_value($class, $row_index);
346
      $class = views_clean_css_identifier($class);
347
    }
348
    return implode(' ', $classes);
349
  }
350
351
  /**
352
   * Return the class of the field's wrapper.
353
   */
354
  function element_wrapper_classes($row_index = NULL) {
355
    $classes = explode(' ', $this->options['element_wrapper_class']);
356
    foreach ($classes as &$class) {
357
      $class = $this->tokenize_value($class, $row_index);
358
      $class = views_clean_css_identifier($class);
359
    }
360
    return implode(' ', $classes);
361
  }
362
363
  /**
364
   * Get the value that's supposed to be rendered.
365
   *
366
   * This api exists so that other modules can easy set the values of the field
367
   * without having the need to change the render method as well.
368
   *
369
   * @param $values
370
   *   An object containing all retrieved values.
371
   * @param $field
372
   *   Optional name of the field where the value is stored.
373
   */
374
  function get_value($values, $field = NULL) {
375
    $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
376
    if (isset($values->{$alias})) {
377
      return $values->{$alias};
378
    }
379
  }
380
381
  /**
382
   * Determines if this field will be available as an option to group the result
383
   * by in the style settings.
384
   *
385
   * @return bool
386
   *  TRUE if this field handler is groupable, otherwise FALSE.
387
   */
388
  function use_string_group_by() {
389
    return TRUE;
390
  }
391
392
  function option_definition() {
393
    $options = parent::option_definition();
394
395
    $options['label'] = array('default' => $this->definition['title'], 'translatable' => TRUE);
396
    $options['exclude'] = array('default' => FALSE, 'bool' => TRUE);
397
    $options['alter'] = array(
398
      'contains' => array(
399
        'alter_text' => array('default' => FALSE, 'bool' => TRUE),
400
        'text' => array('default' => '', 'translatable' => TRUE),
401
        'make_link' => array('default' => FALSE, 'bool' => TRUE),
402
        'path' => array('default' => ''),
403
        'absolute' => array('default' => FALSE, 'bool' => TRUE),
404
        'external' => array('default' => FALSE, 'bool' => TRUE),
405
        'replace_spaces' => array('default' => FALSE, 'bool' => TRUE),
406
        'path_case' => array('default' => 'none', 'translatable' => FALSE),
407
        'trim_whitespace' => array('default' => FALSE, 'bool' => TRUE),
408
        'alt' => array('default' => '', 'translatable' => TRUE),
409
        'rel' => array('default' => ''),
410
        'link_class' => array('default' => ''),
411
        'prefix' => array('default' => '', 'translatable' => TRUE),
412
        'suffix' => array('default' => '', 'translatable' => TRUE),
413
        'target' => array('default' => ''),
414
        'nl2br' => array('default' => FALSE, 'bool' => TRUE),
415
        'max_length' => array('default' => ''),
416
        'word_boundary' => array('default' => TRUE, 'bool' => TRUE),
417
        'ellipsis' => array('default' => TRUE, 'bool' => TRUE),
418
        'more_link' => array('default' => FALSE, 'bool' => TRUE),
419
        'more_link_text' => array('default' => '', 'translatable' => TRUE),
420
        'more_link_path' => array('default' => ''),
421
        'strip_tags' => array('default' => FALSE, 'bool' => TRUE),
422
        'trim' => array('default' => FALSE, 'bool' => TRUE),
423
        'preserve_tags' => array('default' => ''),
424
        'html' => array('default' => FALSE, 'bool' => TRUE),
425
      ),
426
    );
427
    $options['element_type'] = array('default' => '');
428
    $options['element_class'] = array('default' => '');
429
430
    $options['element_label_type'] = array('default' => '');
431
    $options['element_label_class'] = array('default' => '');
432
    $options['element_label_colon'] = array('default' => TRUE, 'bool' => TRUE);
433
434
    $options['element_wrapper_type'] = array('default' => '');
435
    $options['element_wrapper_class'] = array('default' => '');
436
437
    $options['element_default_classes'] = array('default' => TRUE, 'bool' => TRUE);
438
439
    $options['empty'] = array('default' => '', 'translatable' => TRUE);
440
    $options['hide_empty'] = array('default' => FALSE, 'bool' => TRUE);
441
    $options['empty_zero'] = array('default' => FALSE, 'bool' => TRUE);
442
    $options['hide_alter_empty'] = array('default' => TRUE, 'bool' => TRUE);
443
444
    return $options;
445
  }
446
447
  /**
448
   * Performs some cleanup tasks on the options array before saving it.
449
   */
450
  function options_submit(&$form, &$form_state) {
451
    $options = &$form_state['values']['options'];
452
    $types = array('element_type', 'element_label_type', 'element_wrapper_type');
453
    $classes = array_combine(array('element_class', 'element_label_class', 'element_wrapper_class'), $types);
454
455
    foreach ($types as $type) {
456
      if (!$options[$type . '_enable']) {
457
        $options[$type] = '';
458
      }
459
    }
460
461
    foreach ($classes as $class => $type) {
462
      if (!$options[$class . '_enable'] || !$options[$type . '_enable']) {
463
        $options[$class] = '';
464
      }
465
    }
466
467
    if (empty($options['custom_label'])) {
468
      $options['label'] = '';
469
      $options['element_label_colon'] = FALSE;
470
    }
471
  }
472
473
  /**
474
   * Default options form that provides the label widget that all fields
475
   * should have.
476
   */
477
  function options_form(&$form, &$form_state) {
478
    parent::options_form($form, $form_state);
479
480
    $label = $this->label();
481
    $form['custom_label'] = array(
482
      '#type' => 'checkbox',
483
      '#title' => t('Create a label'),
484
      '#description' => t('Enable to create a label for this field.'),
485
      '#default_value' => $label !== '',
486
      '#weight' => -103,
487
    );
488
    $form['label'] = array(
489
      '#type' => 'textfield',
490
      '#title' => t('Label'),
491
      '#default_value' => $label,
492
      '#dependency' => array(
493
        'edit-options-custom-label' => array(1),
494
      ),
495
      '#weight' => -102,
496
    );
497
    $form['element_label_colon'] = array(
498
      '#type' => 'checkbox',
499
      '#title' => t('Place a colon after the label'),
500
      '#default_value' => $this->options['element_label_colon'],
501
      '#dependency' => array(
502
        'edit-options-custom-label' => array(1),
503
      ),
504
      '#weight' => -101,
505
    );
506
507
    $form['exclude'] = array(
508
      '#type' => 'checkbox',
509
      '#title' => t('Exclude from display'),
510
      '#default_value' => $this->options['exclude'],
511
      '#description' => t('Enable to load this field as hidden. Often used to group fields, or to use as token in another field.'),
512
      '#weight' => -100,
513
    );
514
515
    $form['style_settings'] = array(
516
      '#type' => 'fieldset',
517
      '#title' => t('Style settings'),
518
      '#collapsible' => TRUE,
519
      '#collapsed' => TRUE,
520
      '#weight' => 99,
521
    );
522
523
    $form['element_type_enable'] = array(
524
      '#type' => 'checkbox',
525
      '#title' => t('Customize field HTML'),
526
      '#default_value' => !empty($this->options['element_type']) || (string) $this->options['element_type'] == '0' || !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
527
      '#fieldset' => 'style_settings',
528
    );
529
    $form['element_type'] = array(
530
      '#title' => t('HTML element'),
531
      '#options' => $this->get_elements(),
532
      '#type' => 'select',
533
      '#default_value' => $this->options['element_type'],
534
      '#description' => t('Choose the HTML element to wrap around this field, e.g. H1, H2, etc.'),
535
      '#dependency' => array(
536
        'edit-options-element-type-enable' => array(1),
537
      ),
538
      '#fieldset' => 'style_settings',
539
    );
540
541
    $form['element_class_enable'] = array(
542
      '#type' => 'checkbox',
543
      '#title' => t('Create a CSS class'),
544
      '#dependency' => array(
545
        'edit-options-element-type-enable' => array(1),
546
      ),
547
      '#default_value' => !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
548
      '#fieldset' => 'style_settings',
549
    );
550
    $form['element_class'] = array(
551
      '#title' => t('CSS class'),
552
      '#description' => t('You may use token substitutions from the rewriting section in this class.'),
553
      '#type' => 'textfield',
554
      '#default_value' => $this->options['element_class'],
555
      '#dependency' => array(
556
        'edit-options-element-class-enable' => array(1),
557
        'edit-options-element-type-enable' => array(1),
558
      ),
559
      '#dependency_count' => 2,
560
      '#fieldset' => 'style_settings',
561
    );
562
563
    $form['element_label_type_enable'] = array(
564
      '#type' => 'checkbox',
565
      '#title' => t('Customize label HTML'),
566
      '#default_value' => !empty($this->options['element_label_type']) || (string) $this->options['element_label_type'] == '0' || !empty($this->options['element_label_class']) || (string) $this->options['element_label_class'] == '0',
567
      '#fieldset' => 'style_settings',
568
    );
569
    $form['element_label_type'] = array(
570
      '#title' => t('Label HTML element'),
571
      '#options' => $this->get_elements(FALSE),
572
      '#type' => 'select',
573
      '#default_value' => $this->options['element_label_type'],
574
      '#description' => t('Choose the HTML element to wrap around this label, e.g. H1, H2, etc.'),
575
      '#dependency' => array(
576
        'edit-options-element-label-type-enable' => array(1),
577
      ),
578
      '#fieldset' => 'style_settings',
579
    );
580
    $form['element_label_class_enable'] = array(
581
      '#type' => 'checkbox',
582
      '#title' => t('Create a CSS class'),
583
      '#dependency' => array(
584
        'edit-options-element-label-type-enable' => array(1)
585
      ),
586
      '#default_value' => !empty($this->options['element_label_class']) || (string) $this->options['element_label_class'] == '0',
587
      '#fieldset' => 'style_settings',
588
    );
589
    $form['element_label_class'] = array(
590
      '#title' => t('CSS class'),
591
      '#description' => t('You may use token substitutions from the rewriting section in this class.'),
592
      '#type' => 'textfield',
593
      '#default_value' => $this->options['element_label_class'],
594
      '#dependency' => array(
595
        'edit-options-element-label-class-enable' => array(1),
596
        'edit-options-element-label-type-enable' => array(1),
597
      ),
598
      '#dependency_count' => 2,
599
      '#fieldset' => 'style_settings',
600
    );
601
602
    $form['element_wrapper_type_enable'] = array(
603
      '#type' => 'checkbox',
604
      '#title' => t('Customize field and label wrapper HTML'),
605
      '#default_value' => !empty($this->options['element_wrapper_type']) || (string) $this->options['element_wrapper_type'] == '0' || !empty($this->options['element_wrapper_class']) || (string) $this->options['element_wrapper_class'] == '0',
606
      '#fieldset' => 'style_settings',
607
    );
608
    $form['element_wrapper_type'] = array(
609
      '#title' => t('Wrapper HTML element'),
610
      '#options' => $this->get_elements(FALSE),
611
      '#type' => 'select',
612
      '#default_value' => $this->options['element_wrapper_type'],
613
      '#description' => t('Choose the HTML element to wrap around this field and label, e.g. H1, H2, etc. This may not be used if the field and label are not rendered together, such as with a table.'),
614
      '#dependency' => array(
615
        'edit-options-element-wrapper-type-enable' => array(1),
616
      ),
617
      '#fieldset' => 'style_settings',
618
    );
619
620
    $form['element_wrapper_class_enable'] = array(
621
      '#type' => 'checkbox',
622
      '#title' => t('Create a CSS class'),
623
      '#dependency' => array(
624
        'edit-options-element-wrapper-type-enable' => array(1),
625
      ),
626
      '#default_value' => !empty($this->options['element_wrapper_class']) || (string) $this->options['element_wrapper_class'] == '0',
627
      '#fieldset' => 'style_settings',
628
    );
629
    $form['element_wrapper_class'] = array(
630
      '#title' => t('CSS class'),
631
      '#description' => t('You may use token substitutions from the rewriting section in this class.'),
632
      '#type' => 'textfield',
633
      '#default_value' => $this->options['element_wrapper_class'],
634
      '#dependency' => array(
635
        'edit-options-element-wrapper-class-enable' => array(1),
636
        'edit-options-element-wrapper-type-enable' => array(1),
637
      ),
638
      '#dependency_count' => 2,
639
      '#fieldset' => 'style_settings',
640
    );
641
642
    $form['element_default_classes'] = array(
643
      '#type' => 'checkbox',
644
      '#title' => t('Add default classes'),
645
      '#default_value' => $this->options['element_default_classes'],
646
      '#description' => t('Use default Views classes to identify the field, field label and field content.'),
647
      '#fieldset' => 'style_settings',
648
    );
649
650
    $form['alter'] = array(
651
      '#title' => t('Rewrite results'),
652
      '#type' => 'fieldset',
653
      '#collapsible' => TRUE,
654
      '#collapsed' => TRUE,
655
      '#weight' => 100,
656
    );
657
658
    if ($this->allow_advanced_render()) {
659
      $form['alter']['#tree'] = TRUE;
660
      $form['alter']['alter_text'] = array(
661
        '#type' => 'checkbox',
662
        '#title' => t('Rewrite the output of this field'),
663
        '#description' => t('Enable to override the output of this field with custom text or replacement tokens.'),
664
        '#default_value' => $this->options['alter']['alter_text'],
665
      );
666
667
      $form['alter']['text'] = array(
668
        '#title' => t('Text'),
669
        '#type' => 'textarea',
670
        '#default_value' => $this->options['alter']['text'],
671
        '#description' => t('The text to display for this field. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
672
        '#dependency' => array(
673
          'edit-options-alter-alter-text' => array(1),
674
        ),
675
      );
676
677
      $form['alter']['make_link'] = array(
678
        '#type' => 'checkbox',
679
        '#title' => t('Output this field as a link'),
680
        '#description' => t('If checked, this field will be made into a link. The destination must be given below.'),
681
        '#default_value' => $this->options['alter']['make_link'],
682
      );
683
      $form['alter']['path'] = array(
684
        '#title' => t('Link path'),
685
        '#type' => 'textfield',
686
        '#default_value' => $this->options['alter']['path'],
687
        '#description' => t('The Drupal path or absolute URL for this link. You may enter data from this view as per the "Replacement patterns" below.'),
688
        '#dependency' => array(
689
          'edit-options-alter-make-link' => array(1),
690
        ),
691
        '#maxlength' => 255,
692
      );
693
      $form['alter']['absolute'] = array(
694
        '#type' => 'checkbox',
695
        '#title' => t('Use absolute path'),
696
        '#default_value' => $this->options['alter']['absolute'],
697
        '#dependency' => array(
698
          'edit-options-alter-make-link' => array(1),
699
        ),
700
      );
701
      $form['alter']['replace_spaces'] = array(
702
        '#type' => 'checkbox',
703
        '#title' => t('Replace spaces with dashes'),
704
        '#default_value' => $this->options['alter']['replace_spaces'],
705
        '#dependency' => array(
706
          'edit-options-alter-make-link' => array(1)
707
        ),
708
      );
709
      $form['alter']['external'] = array(
710
        '#type' => 'checkbox',
711
        '#title' => t('External server URL'),
712
        '#default_value' => $this->options['alter']['external'],
713
        '#description' => t("Links to an external server using a full URL: e.g. 'http://www.example.com' or 'www.example.com'."),
714
        '#dependency' => array(
715
          'edit-options-alter-make-link' => array(1),
716
        ),
717
      );
718
      $form['alter']['path_case'] = array(
719
        '#type' => 'select',
720
        '#title' => t('Transform the case'),
721
        '#description' => t('When printing url paths, how to transform the case of the filter value.'),
722
        '#dependency' => array(
723
          'edit-options-alter-make-link' => array(1),
724
        ),
725
       '#options' => array(
726
          'none' => t('No transform'),
727
          'upper' => t('Upper case'),
728
          'lower' => t('Lower case'),
729
          'ucfirst' => t('Capitalize first letter'),
730
          'ucwords' => t('Capitalize each word'),
731
        ),
732
        '#default_value' => $this->options['alter']['path_case'],
733
      );
734
      $form['alter']['link_class'] = array(
735
        '#title' => t('Link class'),
736
        '#type' => 'textfield',
737
        '#default_value' => $this->options['alter']['link_class'],
738
        '#description' => t('The CSS class to apply to the link.'),
739
        '#dependency' => array(
740
          'edit-options-alter-make-link' => array(1),
741
        ),
742
      );
743
      $form['alter']['alt'] = array(
744
        '#title' => t('Title text'),
745
        '#type' => 'textfield',
746
        '#default_value' => $this->options['alter']['alt'],
747
        '#description' => t('Text to place as "title" text which most browsers display as a tooltip when hovering over the link.'),
748
        '#dependency' => array(
749
          'edit-options-alter-make-link' => array(1),
750
        ),
751
      );
752
      $form['alter']['rel'] = array(
753
        '#title' => t('Rel Text'),
754
        '#type' => 'textfield',
755
        '#default_value' => $this->options['alter']['rel'],
756
        '#description' => t('Include Rel attribute for use in lightbox2 or other javascript utility.'),
757
        '#dependency' => array(
758
          'edit-options-alter-make-link' => array(1),
759
        ),
760
      );
761
      $form['alter']['prefix'] = array(
762
        '#title' => t('Prefix text'),
763
        '#type' => 'textfield',
764
        '#default_value' => $this->options['alter']['prefix'],
765
        '#description' => t('Any text to display before this link. You may include HTML.'),
766
        '#dependency' => array(
767
          'edit-options-alter-make-link' => array(1),
768
        ),
769
      );
770
      $form['alter']['suffix'] = array(
771
        '#title' => t('Suffix text'),
772
        '#type' => 'textfield',
773
        '#default_value' => $this->options['alter']['suffix'],
774
        '#description' => t('Any text to display after this link. You may include HTML.'),
775
        '#dependency' => array(
776
          'edit-options-alter-make-link' => array(1),
777
        ),
778
      );
779
      $form['alter']['target'] = array(
780
        '#title' => t('Target'),
781
        '#type' => 'textfield',
782
        '#default_value' => $this->options['alter']['target'],
783
        '#description' => t("Target of the link, such as _blank, _parent or an iframe's name. This field is rarely used."),
784
        '#dependency' => array(
785
          'edit-options-alter-make-link' => array(1),
786
        ),
787
      );
788
789
790
      // Get a list of the available fields and arguments for token replacement.
791
      $options = array();
792
      foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
793
        $options[t('Fields')]["[$field]"] = $handler->ui_name();
794
        // We only use fields up to (and including) this one.
795
        if ($field == $this->options['id']) {
796
          break;
797
        }
798
      }
799
      $count = 0; // This lets us prepare the key as we want it printed.
800
      foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
801
        $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
802
        $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
803
      }
804
805
      $this->document_self_tokens($options[t('Fields')]);
806
807
      // Default text.
808
      $output = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
809
      // We have some options, so make a list.
810
      if (!empty($options)) {
811
        $output = t('<p>The following tokens are available for this field. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.
812
If you would like to have the characters \'[\' and \']\' please use the html entity codes \'%5B\' or  \'%5D\' or they will get replaced with empty space.</p>');
813
        foreach (array_keys($options) as $type) {
814
          if (!empty($options[$type])) {
815
            $items = array();
816
            foreach ($options[$type] as $key => $value) {
817
              $items[] = $key . ' == ' . check_plain($value);
818
            }
819
            $output .= theme('item_list',
820
              array(
821
                'items' => $items,
822
                'type' => $type
823
              ));
824
          }
825
        }
826
      }
827
      // This construct uses 'hidden' and not markup because process doesn't
828
      // run. It also has an extra div because the dependency wants to hide
829
      // the parent in situations like this, so we need a second div to
830
      // make this work.
831
      $form['alter']['help'] = array(
832
        '#type' => 'fieldset',
833
        '#title' => t('Replacement patterns'),
834
        '#collapsible' => TRUE,
835
        '#collapsed' => TRUE,
836
        '#value' => $output,
837
        '#dependency' => array(
838
          'edit-options-alter-make-link' => array(1),
839
          'edit-options-alter-alter-text' => array(1),
840
          'edit-options-alter-more-link' => array(1),
841
        ),
842
      );
843
844
      $form['alter']['trim'] = array(
845
        '#type' => 'checkbox',
846
        '#title' => t('Trim this field to a maximum length'),
847
        '#description' => t('Enable to trim the field to a maximum length of characters'),
848
        '#default_value' => $this->options['alter']['trim'],
849
      );
850
851
      $form['alter']['max_length'] = array(
852
        '#title' => t('Maximum length'),
853
        '#type' => 'textfield',
854
        '#default_value' => $this->options['alter']['max_length'],
855
        '#description' => t('The maximum number of characters this field can be.'),
856
        '#dependency' => array(
857
          'edit-options-alter-trim' => array(1),
858
        ),
859
      );
860
861
      $form['alter']['word_boundary'] = array(
862
        '#type' => 'checkbox',
863
        '#title' => t('Trim only on a word boundary'),
864
        '#description' => t('If checked, this field be trimmed only on a word boundary. This is guaranteed to be the maximum characters stated or less. If there are no word boundaries this could trim a field to nothing.'),
865
        '#default_value' => $this->options['alter']['word_boundary'],
866
        '#dependency' => array(
867
          'edit-options-alter-trim' => array(1),
868
        ),
869
      );
870
871
      $form['alter']['ellipsis'] = array(
872
        '#type' => 'checkbox',
873
        '#title' => t('Add an ellipsis'),
874
        '#description' => t('If checked, a "..." will be added if a field was trimmed.'),
875
        '#default_value' => $this->options['alter']['ellipsis'],
876
        '#dependency' => array(
877
          'edit-options-alter-trim' => array(1),
878
        ),
879
      );
880
881
      $form['alter']['more_link'] = array(
882
        '#type' => 'checkbox',
883
        '#title' => t('Add a read-more link if output is trimmed.'),
884
        '#description' => t('If checked, a read-more link will be added at the end of the trimmed output'),
885
        '#default_value' => $this->options['alter']['more_link'],
886
        '#dependency' => array(
887
          'edit-options-alter-trim' => array(1),
888
        ),
889
      );
890
891
      $form['alter']['more_link_text'] = array(
892
        '#type' => 'textfield',
893
        '#title' => t('More link text'),
894
        '#default_value' => $this->options['alter']['more_link_text'],
895
        '#description' => t('The text which will be displayed on the more link. You may enter data from this view as per the "Replacement patterns" above.'),
896
        '#dependency_count' => 2,
897
        '#dependency' => array(
898
          'edit-options-alter-trim' => array(1),
899
          'edit-options-alter-more-link' => array(1),
900
        ),
901
      );
902
      $form['alter']['more_link_path'] = array(
903
        '#type' => 'textfield',
904
        '#title' => t('More link path'),
905
        '#default_value' => $this->options['alter']['more_link_path'],
906
        '#description' => t('The path which is used for the more link. You may enter data from this view as per the "Replacement patterns" above.'),
907
        '#dependency_count' => 2,
908
        '#dependency' => array(
909
          'edit-options-alter-trim' => array(1),
910
          'edit-options-alter-more-link' => array(1),
911
        ),
912
      );
913
914
      $form['alter']['html'] = array(
915
        '#type' => 'checkbox',
916
        '#title' => t('Field can contain HTML'),
917
        '#description' => t('If checked, HTML corrector will be run to ensure tags are properly closed after trimming.'),
918
        '#default_value' => $this->options['alter']['html'],
919
        '#dependency' => array(
920
          'edit-options-alter-trim' => array(1),
921
        ),
922
      );
923
924
      $form['alter']['strip_tags'] = array(
925
        '#type' => 'checkbox',
926
        '#title' => t('Strip HTML tags'),
927
        '#description' => t('If checked, all HTML tags will be stripped.'),
928
        '#default_value' => $this->options['alter']['strip_tags'],
929
      );
930
931
      $form['alter']['preserve_tags'] = array(
932
        '#type' => 'textfield',
933
        '#title' => t('Preserve certain tags'),
934
        '#description' => t('List the tags that need to be preserved during the stripping process. example &quot;&lt;p&gt; &lt;br&gt;&quot; which will preserve all p and br elements'),
935
        '#default_value' => $this->options['alter']['preserve_tags'],
936
        '#dependency' => array(
937
          'edit-options-alter-strip-tags' => array(1),
938
        ),
939
      );
940
941
      $form['alter']['trim_whitespace'] = array(
942
        '#type' => 'checkbox',
943
        '#title' => t('Remove whitespace'),
944
        '#description' => t('If checked, all whitespaces at the beginning and the end of the output will be removed.'),
945
        '#default_value' => $this->options['alter']['trim_whitespace'],
946
      );
947
948
      $form['alter']['nl2br'] = array(
949
        '#type' => 'checkbox',
950
        '#title' => t('Convert newlines to HTML &lt;br&gt; tags'),
951
        '#description' => t('If checked, all newlines chars (e.g. \n) are converted into HTML &lt;br&gt; tags.'),
952
        '#default_value' => $this->options['alter']['nl2br'],
953
      );
954
    }
955
956
    $form['empty_field_behavior'] = array(
957
      '#type' => 'fieldset',
958
      '#title' => t('No results behavior'),
959
      '#collapsible' => TRUE,
960
      '#collapsed' => TRUE,
961
      '#weight' => 100,
962
    );
963
964
    $form['empty'] = array(
965
      '#type' => 'textarea',
966
      '#title' => t('No results text'),
967
      '#default_value' => $this->options['empty'],
968
      '#description' => t('Provide text to display if this field contains an empty result. You may include HTML. You may enter data from this view as per the "Replacement patterns" in the "Rewrite Results" section below.'),
969
      '#fieldset' => 'empty_field_behavior',
970
    );
971
972
    $form['empty_zero'] = array(
973
      '#type' => 'checkbox',
974
      '#title' => t('Count the number 0 as empty'),
975
      '#default_value' => $this->options['empty_zero'],
976
      '#description' => t('Enable to display the "no results text" if the field contains the number 0.'),
977
      '#fieldset' => 'empty_field_behavior',
978
    );
979
980
    $form['hide_empty'] = array(
981
      '#type' => 'checkbox',
982
      '#title' => t('Hide if empty'),
983
      '#default_value' => $this->options['hide_empty'],
984
      '#description' => t('Enable to hide this field if it is empty. Note that the field label or rewritten output may still be displayed. To hide labels, check the style or row style settings for empty fields. To hide rewritten content, check the "Hide rewriting if empty" checkbox.'),
985
      '#fieldset' => 'empty_field_behavior',
986
    );
987
988
    $form['hide_alter_empty'] = array(
989
      '#type' => 'checkbox',
990
      '#title' => t('Hide rewriting if empty'),
991
      '#default_value' => $this->options['hide_alter_empty'],
992
      '#description' => t('Do not display rewritten content if this field is empty.'),
993
      '#fieldset' => 'empty_field_behavior',
994
    );
995
  }
996
997
  /**
998
   * Provide extra data to the administration form
999
   */
1000
  function admin_summary() {
1001
    return $this->label();
1002
  }
1003
1004
  /**
1005
   * Run before any fields are rendered.
1006
   *
1007
   * This gives the handlers some time to set up before any handler has
1008
   * been rendered.
1009
   *
1010
   * @param $values
1011
   *   An array of all objects returned from the query.
1012
   */
1013
  function pre_render(&$values) { }
1014
1015
  /**
1016
   * Render the field.
1017
   *
1018
   * @param $values
1019
   *   The values retrieved from the database.
1020
   */
1021
  function render($values) {
1022
    $value = $this->get_value($values);
1023
    return $this->sanitize_value($value);
1024
  }
1025
1026
  /**
1027
   * Render a field using advanced settings.
1028
   *
1029
   * This renders a field normally, then decides if render-as-link and
1030
   * text-replacement rendering is necessary.
1031
   */
1032
  function advanced_render($values) {
1033
    if ($this->allow_advanced_render() && method_exists($this, 'render_item')) {
1034
      $raw_items = $this->get_items($values);
1035
      // If there are no items, set the original value to NULL.
1036
      if (empty($raw_items)) {
1037
        $this->original_value = NULL;
1038
      }
1039
    }
1040
    else {
1041
      $value = $this->render($values);
1042
      if (is_array($value)) {
1043
        $value = drupal_render($value);
1044
      }
1045
      $this->last_render = $value;
1046
      $this->original_value = $value;
1047
    }
1048
1049
    if ($this->allow_advanced_render()) {
1050
      $tokens = NULL;
1051
      if (method_exists($this, 'render_item')) {
1052
        $items = array();
1053
        foreach ($raw_items as $count => $item) {
1054
          $value = $this->render_item($count, $item);
1055
          if (is_array($value)) {
1056
            $value = drupal_render($value);
1057
          }
1058
          $this->last_render = $value;
1059
          $this->original_value = $this->last_render;
1060
1061
          $alter = $item + $this->options['alter'];
1062
          $alter['phase'] = VIEWS_HANDLER_RENDER_TEXT_PHASE_SINGLE_ITEM;
1063
          $items[] = $this->render_text($alter);
1064
        }
1065
1066
        $value = $this->render_items($items);
1067
      }
1068
      else {
1069
        $alter = array('phase' => VIEWS_HANDLER_RENDER_TEXT_PHASE_COMPLETELY) + $this->options['alter'];
1070
        $value = $this->render_text($alter);
1071
      }
1072
1073
      if (is_array($value)) {
1074
        $value = drupal_render($value);
1075
      }
1076
      // This happens here so that render_as_link can get the unaltered value of
1077
      // this field as a token rather than the altered value.
1078
      $this->last_render = $value;
1079
    }
1080
1081
    if (empty($this->last_render)) {
1082
      if ($this->is_value_empty($this->last_render, $this->options['empty_zero'], FALSE)) {
1083
        $alter = $this->options['alter'];
1084
        $alter['alter_text'] = 1;
1085
        $alter['text'] = $this->options['empty'];
1086
        $alter['phase'] = VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY;
1087
        $this->last_render = $this->render_text($alter);
1088
      }
1089
    }
1090
1091
    return $this->last_render;
1092
  }
1093
1094
  /**
1095
   * Checks if a field value is empty.
1096
   *
1097
   * @param $value
1098
   *   The field value.
1099
   * @param bool $empty_zero
1100
   *   Whether or not this field is configured to consider 0 as empty.
1101
   * @param bool $no_skip_empty
1102
   *   Whether or not to use empty() to check the value.
1103
   *
1104
   * @return bool
1105
   * TRUE if the value is considered empty, FALSE otherwise.
1106
   */
1107
  function is_value_empty($value, $empty_zero, $no_skip_empty = TRUE) {
1108
    if (!isset($value)) {
1109
      $empty = TRUE;
1110
    }
1111
    else {
1112
      $empty = ($empty_zero || ($value !== 0 && $value !== '0'));
1113
    }
1114
1115
    if ($no_skip_empty) {
1116
      $empty = empty($value) && $empty;
1117
    }
1118
    return $empty;
1119
  }
1120
1121
  /**
1122
   * Perform an advanced text render for the item.
1123
   *
1124
   * This is separated out as some fields may render lists, and this allows
1125
   * each item to be handled individually.
1126
   */
1127
  function render_text($alter) {
1128
    $value = $this->last_render;
1129
1130
    if (!empty($alter['alter_text']) && $alter['text'] !== '') {
1131
      $tokens = $this->get_render_tokens($alter);
1132
      $value = $this->render_altered($alter, $tokens);
1133
    }
1134
1135
    if (!empty($this->options['alter']['trim_whitespace'])) {
1136
      $value = trim($value);
1137
    }
1138
1139
    // Check if there should be no further rewrite for empty values.
1140
    $no_rewrite_for_empty = $this->options['hide_alter_empty'] && $this->is_value_empty($this->original_value, $this->options['empty_zero']);
1141
1142
    // Check whether the value is empty and return nothing, so the field isn't rendered.
1143
    // First check whether the field should be hidden if the value(hide_alter_empty = TRUE) /the rewrite is empty (hide_alter_empty = FALSE).
1144
    // For numeric values you can specify whether "0"/0 should be empty.
1145
    if ((($this->options['hide_empty'] && empty($value))
1146
        || ($alter['phase'] != VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty))
1147
      && $this->is_value_empty($value, $this->options['empty_zero'], FALSE)) {
1148
      return '';
1149
    }
1150
    // Only in empty phase.
1151
    if ($alter['phase'] == VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty) {
1152
      // If we got here then $alter contains the value of "No results text"
1153
      // and so there is nothing left to do.
1154
      return $value;
1155
    }
1156
1157
    if (!empty($alter['strip_tags'])) {
1158
      $value = strip_tags($value, $alter['preserve_tags']);
1159
    }
1160
1161
    $suffix = '';
1162
    if (!empty($alter['trim']) && !empty($alter['max_length'])) {
1163
      $length = strlen($value);
1164
      $value = $this->render_trim_text($alter, $value);
1165
      if ($this->options['alter']['more_link'] && strlen($value) < $length) {
1166
        $tokens = $this->get_render_tokens($alter);
1167
        $more_link_text = $this->options['alter']['more_link_text'] ? $this->options['alter']['more_link_text'] : t('more');
1168
        $more_link_text = strtr(filter_xss_admin($more_link_text), $tokens);
1169
        $more_link_path = $this->options['alter']['more_link_path'];
1170
        $more_link_path = strip_tags(decode_entities(strtr($more_link_path, $tokens)));
1171
1172
        // Take sure that paths which was runned through url() does work as well.
1173
        $base_path = base_path();
1174
        // Checks whether the path starts with the base_path.
1175
        if (strpos($more_link_path, $base_path) === 0) {
1176
          $more_link_path = drupal_substr($more_link_path, drupal_strlen($base_path));
1177
        }
1178
1179
        $more_link = l($more_link_text, $more_link_path, array('attributes' => array('class' => array('views-more-link'))));
1180
1181
        $suffix .= " " . $more_link;
1182
      }
1183
    }
1184
1185
    if (!empty($alter['nl2br'])) {
1186
      $value = nl2br($value);
1187
    }
1188
    $this->last_render_text = $value;
1189
1190
    if (!empty($alter['make_link']) && !empty($alter['path'])) {
1191
      if (!isset($tokens)) {
1192
       $tokens = $this->get_render_tokens($alter);
1193
      }
1194
      $value = $this->render_as_link($alter, $value, $tokens);
1195
    }
1196
1197
    return $value . $suffix;
1198
  }
1199
1200
  /**
1201
   * Render this field as altered text, from a fieldset set by the user.
1202
   */
1203
  function render_altered($alter, $tokens) {
1204
    // Filter this right away as our substitutions are already sanitized.
1205
    $value = filter_xss_admin($alter['text']);
1206
    $value = strtr($value, $tokens);
1207
1208
    return $value;
1209
  }
1210
1211
  /**
1212
   * Trim the field down to the specified length.
1213
   */
1214
  function render_trim_text($alter, $value) {
1215
    if (!empty($alter['strip_tags'])) {
1216
      // NOTE: It's possible that some external fields might override the
1217
      // element type so if someone from, say, CCK runs into a bug here,
1218
      // this may be why =)
1219
      $this->definition['element type'] = 'span';
1220
    }
1221
    return views_trim_text($alter, $value);
1222
  }
1223
1224
  /**
1225
   * Render this field as a link, with the info from a fieldset set by
1226
   * the user.
1227
   */
1228
  function render_as_link($alter, $text, $tokens) {
1229
    $value = '';
1230
1231
    if (!empty($alter['prefix'])) {
1232
      $value .= filter_xss_admin(strtr($alter['prefix'], $tokens));
1233
    }
1234
1235
    $options = array(
1236
      'html' => TRUE,
1237
      'absolute' => !empty($alter['absolute']) ? TRUE : FALSE,
1238
    );
1239
1240
    // $path will be run through check_url() by l() so we do not need to
1241
    // sanitize it ourselves.
1242
    $path = $alter['path'];
1243
1244
    // strip_tags() removes <front>, so check whether its different to front.
1245
    if ($path != '<front>') {
1246
      // Use strip tags as there should never be HTML in the path.
1247
      // However, we need to preserve special characters like " that
1248
      // were removed by check_plain().
1249
      $path = strip_tags(decode_entities(strtr($path, $tokens)));
1250
1251
      if (!empty($alter['path_case']) && $alter['path_case'] != 'none') {
1252
        $path = $this->case_transform($path, $this->options['alter']['path_case']);
1253
      }
1254
1255
      if (!empty($alter['replace_spaces'])) {
1256
        $path = str_replace(' ', '-', $path);
1257
      }
1258
    }
1259
1260
    // Parse the URL and move any query and fragment parameters out of the path.
1261
    $url = parse_url($path);
1262
1263
    // Seriously malformed URLs may return FALSE or empty arrays.
1264
    if (empty($url)) {
1265
      return $text;
1266
    }
1267
1268
    // If the path is empty do not build a link around the given text and return
1269
    // it as is.
1270
    // http://www.example.com URLs will not have a $url['path'], so check host as well.
1271
    if (empty($url['path']) && empty($url['host']) && empty($url['fragment'])) {
1272
      return $text;
1273
    }
1274
1275
    // If no scheme is provided in the $path, assign the default 'http://'.
1276
    // This allows a url of 'www.example.com' to be converted to 'http://www.example.com'.
1277
    // Only do this on for external URLs.
1278
    if ($alter['external']){
1279
      if (!isset($url['scheme'])) {
1280
        // There is no scheme, add the default 'http://' to the $path.
1281
        $path = "http://$path";
1282
        // Reset the $url array to include the new scheme.
1283
        $url = parse_url($path);
1284
      }
1285
    }
1286
1287
    if (isset($url['query'])) {
1288
      $path = strtr($path, array('?' . $url['query'] => ''));
1289
      $query = drupal_get_query_array($url['query']);
1290
      // Remove query parameters that were assigned a query string replacement
1291
      // token for which there is no value available.
1292
      foreach ($query as $param => $val) {
1293
        if ($val == '%' . $param) {
1294
          unset($query[$param]);
1295
        }
1296
      }
1297
      $options['query'] = $query;
1298
    }
1299
    if (isset($url['fragment'])) {
1300
      $path = strtr($path, array('#' . $url['fragment'] => ''));
1301
      // If the path is empty we want to have a fragment for the current site.
1302
      if ($path == '') {
1303
        $options['external'] = TRUE;
1304
      }
1305
      $options['fragment'] = $url['fragment'];
1306
    }
1307
1308
    $alt = strtr($alter['alt'], $tokens);
1309
    // Set the title attribute of the link only if it improves accessibility
1310
    if ($alt && $alt != $text) {
1311
      $options['attributes']['title'] = decode_entities($alt);
1312
    }
1313
1314
    $class = strtr($alter['link_class'], $tokens);
1315
    if ($class) {
1316
      $options['attributes']['class'] = array($class);
1317
    }
1318
1319
    if (!empty($alter['rel']) && $rel = strtr($alter['rel'], $tokens)) {
1320
      $options['attributes']['rel'] = $rel;
1321
    }
1322
1323
    $target = check_plain(trim(strtr($alter['target'],$tokens)));
1324
    if (!empty($target)) {
1325
      $options['attributes']['target'] = $target;
1326
    }
1327
1328
    // Allow the addition of arbitrary attributes to links. Additional attributes
1329
    // currently can only be altered in preprocessors and not within the UI.
1330
    if (isset($alter['link_attributes']) && is_array($alter['link_attributes'])) {
1331
      foreach ($alter['link_attributes'] as $key => $attribute) {
1332
        if (!isset($options['attributes'][$key])) {
1333
          $options['attributes'][$key] = strtr($attribute, $tokens);
1334
        }
1335
      }
1336
    }
1337
1338
    // If the query and fragment were programatically assigned overwrite any
1339
    // parsed values.
1340
    if (isset($alter['query'])) {
1341
      // Convert the query to a string, perform token replacement, and then
1342
      // convert back to an array form for l().
1343
      $options['query'] = drupal_http_build_query($alter['query']);
1344
      $options['query'] = strtr($options['query'], $tokens);
1345
      $options['query'] = drupal_get_query_array($options['query']);
1346
    }
1347
    if (isset($alter['alias'])) {
1348
      // Alias is a boolean field, so no token.
1349
      $options['alias'] = $alter['alias'];
1350
    }
1351
    if (isset($alter['fragment'])) {
1352
      $options['fragment'] = strtr($alter['fragment'], $tokens);
1353
    }
1354
    if (isset($alter['language'])) {
1355
      $options['language'] = $alter['language'];
1356
    }
1357
1358
    // If the url came from entity_uri(), pass along the required options.
1359
    if (isset($alter['entity'])) {
1360
      $options['entity'] = $alter['entity'];
1361
    }
1362
    if (isset($alter['entity_type'])) {
1363
      $options['entity_type'] = $alter['entity_type'];
1364
    }
1365
1366
    $value .= l($text, $path, $options);
1367
1368
    if (!empty($alter['suffix'])) {
1369
      $value .= filter_xss_admin(strtr($alter['suffix'], $tokens));
1370
    }
1371
1372
    return $value;
1373
  }
1374
1375
  /**
1376
   * Get the 'render' tokens to use for advanced rendering.
1377
   *
1378
   * This runs through all of the fields and arguments that
1379
   * are available and gets their values. This will then be
1380
   * used in one giant str_replace().
1381
   */
1382
  function get_render_tokens($item) {
1383
    $tokens = array();
1384
    if (!empty($this->view->build_info['substitutions'])) {
1385
      $tokens = $this->view->build_info['substitutions'];
1386
    }
1387
    $count = 0;
1388
    foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
1389
      $token = '%' . ++$count;
1390
      if (!isset($tokens[$token])) {
1391
        $tokens[$token] = '';
1392
      }
1393
1394
      // Use strip tags as there should never be HTML in the path.
1395
      // However, we need to preserve special characters like " that
1396
      // were removed by check_plain().
1397
      $tokens['!' . $count] = isset($this->view->args[$count - 1]) ? strip_tags(decode_entities($this->view->args[$count - 1])) : '';
1398
    }
1399
1400
    // Get flattened set of tokens for any array depth in $_GET parameters.
1401
    $tokens += $this->get_token_values_recursive($_GET);
1402
1403
    // Now add replacements for our fields.
1404
    foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
1405
      if (isset($handler->last_render)) {
1406
        $tokens["[$field]"] = $handler->last_render;
1407
      }
1408
      else {
1409
        $tokens["[$field]"] = '';
1410
      }
1411
      if (!empty($item)) {
1412
        $this->add_self_tokens($tokens, $item);
1413
      }
1414
1415
      // We only use fields up to (and including) this one.
1416
      if ($field == $this->options['id']) {
1417
        break;
1418
      }
1419
    }
1420
1421
    // Store the tokens for the row so we can reference them later if necessary.
1422
    $this->view->style_plugin->render_tokens[$this->view->row_index] = $tokens;
1423
    $this->last_tokens = $tokens;
1424
1425
    return $tokens;
1426
  }
1427
1428
  /**
1429
   * Recursive function to add replacements for nested query string parameters.
1430
   *
1431
   * E.g. if you pass in the following array:
1432
   *   array(
1433
   *     'foo' => array(
1434
   *       'a' => 'value',
1435
   *       'b' => 'value',
1436
   *     ),
1437
   *     'bar' => array(
1438
   *       'a' => 'value',
1439
   *       'b' => array(
1440
   *         'c' => value,
1441
   *       ),
1442
   *     ),
1443
   *   );
1444
   *
1445
   * Would yield the following array of tokens:
1446
   *   array(
1447
   *     '%foo_a' => 'value'
1448
   *     '%foo_b' => 'value'
1449
   *     '%bar_a' => 'value'
1450
   *     '%bar_b_c' => 'value'
1451
   *   );
1452
   *
1453
   * @param $array
1454
   *   An array of values.
1455
   *
1456
   * @param $parent_keys
1457
   *   An array of parent keys. This will represent the array depth.
1458
   *
1459
   * @return
1460
   *   An array of available tokens, with nested keys representative of the array structure.
1461
   */
1462
  function get_token_values_recursive(array $array, array $parent_keys = array()) {
1463
    $tokens = array();
1464
1465
    foreach ($array as $param => $val) {
1466
      if (is_array($val)) {
1467
         // Copy parent_keys array, so we don't afect other elements of this iteration.
1468
         $child_parent_keys = $parent_keys;
1469
         $child_parent_keys[] = $param;
1470
         // Get the child tokens.
1471
         $child_tokens = $this->get_token_values_recursive($val, $child_parent_keys);
1472
         // Add them to the current tokens array.
1473
         $tokens += $child_tokens;
1474
      }
1475
      else {
1476
        // Create a token key based on array element structure.
1477
        $token_string = !empty($parent_keys) ? implode('_', $parent_keys) . '_' . $param : $param;
1478
        $tokens['%' . $token_string] = strip_tags(decode_entities($val));
1479
      }
1480
    }
1481
1482
    return $tokens;
1483
  }
1484
1485
  /**
1486
   * Add any special tokens this field might use for itself.
1487
   *
1488
   * This method is intended to be overridden by items that generate
1489
   * fields as a list. For example, the field that displays all terms
1490
   * on a node might have tokens for the tid and the term.
1491
   *
1492
   * By convention, tokens should follow the format of [token-subtoken]
1493
   * where token is the field ID and subtoken is the field. If the
1494
   * field ID is terms, then the tokens might be [terms-tid] and [terms-name].
1495
   */
1496
  function add_self_tokens(&$tokens, $item) { }
1497
1498
  /**
1499
   * Document any special tokens this field might use for itself.
1500
   *
1501
   * @see add_self_tokens()
1502
   */
1503
  function document_self_tokens(&$tokens) { }
1504
1505
  /**
1506
   * Call out to the theme() function, which probably just calls render() but
1507
   * allows sites to override output fairly easily.
1508
   */
1509
  function theme($values) {
1510
    return theme($this->theme_functions(),
1511
      array(
1512
        'view' => $this->view,
1513
        'field' => $this,
1514
        'row' => $values
1515
      ));
1516
  }
1517
1518
  function theme_functions() {
1519
    $themes = array();
1520
    $hook = 'views_view_field';
1521
1522
    $display = $this->view->display[$this->view->current_display];
1523
1524
    if (!empty($display)) {
1525
      $themes[] = $hook . '__' . $this->view->name  . '__' . $display->id . '__' . $this->options['id'];
1526
      $themes[] = $hook . '__' . $this->view->name  . '__' . $display->id;
1527
      $themes[] = $hook . '__' . $display->id . '__' . $this->options['id'];
1528
      $themes[] = $hook . '__' . $display->id;
1529
      if ($display->id != $display->display_plugin) {
1530
        $themes[] = $hook . '__' . $this->view->name  . '__' . $display->display_plugin . '__' . $this->options['id'];
1531
        $themes[] = $hook . '__' . $this->view->name  . '__' . $display->display_plugin;
1532
        $themes[] = $hook . '__' . $display->display_plugin . '__' . $this->options['id'];
1533
        $themes[] = $hook . '__' . $display->display_plugin;
1534
      }
1535
    }
1536
    $themes[] = $hook . '__' . $this->view->name . '__' . $this->options['id'];
1537
    $themes[] = $hook . '__' . $this->view->name;
1538
    $themes[] = $hook . '__' . $this->options['id'];
1539
    $themes[] = $hook;
1540
1541
    return $themes;
1542
  }
1543
1544
  function ui_name($short = FALSE) {
1545
    return $this->get_field(parent::ui_name($short));
1546
  }
1547
}
1548
1549
/**
1550
 * A special handler to take the place of missing or broken handlers.
1551
 *
1552
 * @ingroup views_field_handlers
1553
 */
1554
class views_handler_field_broken extends views_handler_field {
1555
  function ui_name($short = FALSE) {
1556
    return t('Broken/missing handler');
1557
  }
1558
1559
  function ensure_my_table() { /* No table to ensure! */ }
1560
  function query($group_by = FALSE) { /* No query to run */ }
1561
  function options_form(&$form, &$form_state) {
1562
    $form['markup'] = array(
1563
      '#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>',
1564
    );
1565
  }
1566
1567
  /**
1568
   * Determine if the handler is considered 'broken'
1569
   */
1570
  function broken() { return TRUE; }
1571
}
1572
1573
/**
1574
 * Render a numeric value as a size.
1575
 *
1576
 * @ingroup views_field_handlers
1577
 */
1578
class views_handler_field_file_size extends views_handler_field {
1579
  function option_definition() {
1580
    $options = parent::option_definition();
1581
1582
    $options['file_size_display'] = array('default' => 'formatted');
1583
1584
    return $options;
1585
  }
1586
1587
  function options_form(&$form, &$form_state) {
1588
    parent::options_form($form, $form_state);
1589
    $form['file_size_display'] = array(
1590
      '#title' => t('File size display'),
1591
      '#type' => 'select',
1592
      '#options' => array(
1593
        'formatted' => t('Formatted (in KB or MB)'),
1594
        'bytes' => t('Raw bytes'),
1595
      ),
1596
    );
1597
  }
1598
1599
  function render($values) {
1600
    $value = $this->get_value($values);
1601
    if ($value) {
1602
      switch ($this->options['file_size_display']) {
1603
        case 'bytes':
1604
          return $value;
1605
        case 'formatted':
1606
        default:
1607
          return format_size($value);
1608
      }
1609
    }
1610
    else {
1611
      return '';
1612
    }
1613
  }
1614
}
1615
1616
/**
1617
 * A handler to run a field through simple XSS filtering.
1618
 *
1619
 * @ingroup views_field_handlers
1620
 */
1621
class views_handler_field_xss extends views_handler_field {
1622
  function render($values) {
1623
    $value = $this->get_value($values);
1624
    return $this->sanitize_value($value, 'xss');
1625
  }
1626
}
1627
1628
/**
1629
 * @}
1630
 */