Projet

Général

Profil

Paste
Télécharger (20,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / plugins / views_plugin_style.inc @ 5d12d676

1
<?php
2

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

    
8
/**
9
 * @defgroup views_style_plugins Views style plugins
10
 * @{
11
 * Style plugins control how a view is rendered. For example, they can choose to
12
 * display a collection of fields, node_view() output, table output, or any kind
13
 * of crazy output they want.
14
 *
15
 * Many style plugins can have an optional 'row' plugin, that displays a single
16
 * record. Not all style plugins can utilize this, so it is up to the plugin to
17
 * set this up and call through to the row plugin.
18
 *
19
 * @see hook_views_plugins()
20
 */
21

    
22
/**
23
 * Base class to define a style plugin handler.
24
 */
25
class views_plugin_style extends views_plugin {
26

    
27
  /**
28
   * Store all available tokens row rows.
29
   */
30
  public $row_tokens = array();
31

    
32
  /**
33
   * The row plugin, if it's initialized and the style itself supports it.
34
   *
35
   * @var views_plugin_row
36
   */
37
  public $row_plugin;
38

    
39
  /**
40
   * Initialize a style plugin.
41
   *
42
   * @param view $view
43
   * @param object $display
44
   * @param array $options
45
   *   The style options might come externally as the style can be sourced
46
   *   from at least two locations. If it's not included, look on the display.
47
   */
48
  public function init(&$view, &$display, $options = NULL) {
49
    $this->view = &$view;
50
    $this->display = &$display;
51

    
52
    // Overlay incoming options on top of defaults
53
    $this->unpack_options($this->options, isset($options) ? $options : $display->handler->get_option('style_options'));
54

    
55
    if ($this->uses_row_plugin() && $display->handler->get_option('row_plugin')) {
56
      $this->row_plugin = $display->handler->get_plugin('row');
57
    }
58

    
59
    $this->options += array(
60
      'grouping' => array(),
61
    );
62

    
63
    $this->definition += array(
64
      'uses grouping' => TRUE,
65
    );
66
  }
67

    
68
  /**
69
   *
70
   */
71
  public function destroy() {
72
    parent::destroy();
73

    
74
    if (isset($this->row_plugin)) {
75
      $this->row_plugin->destroy();
76
    }
77
  }
78

    
79
  /**
80
   * Return TRUE if this style also uses a row plugin.
81
   */
82
  public function uses_row_plugin() {
83
    return !empty($this->definition['uses row plugin']);
84
  }
85

    
86
  /**
87
   * Return TRUE if this style also uses a row plugin.
88
   */
89
  public function uses_row_class() {
90
    return !empty($this->definition['uses row class']);
91
  }
92

    
93
  /**
94
   * Return TRUE if this style also uses fields.
95
   *
96
   * @return bool
97
   */
98
  public function uses_fields() {
99
    // If we use a row plugin, ask the row plugin. Chances are, we don't
100
    // care, it does.
101
    $row_uses_fields = FALSE;
102
    if ($this->uses_row_plugin() && !empty($this->row_plugin)) {
103
      $row_uses_fields = $this->row_plugin->uses_fields();
104
    }
105
    // Otherwise, check the definition or the option.
106
    return $row_uses_fields || !empty($this->definition['uses fields']) || !empty($this->options['uses_fields']);
107
  }
108

    
109
  /**
110
   * Return TRUE if this style uses tokens.
111
   *
112
   * Used to ensure we don't fetch tokens when not needed for performance.
113
   */
114
  public function uses_tokens() {
115
    if ($this->uses_row_class()) {
116
      $class = $this->options['row_class'];
117
      if (strpos($class, '[') !== FALSE || strpos($class, '!') !== FALSE || strpos($class, '%') !== FALSE) {
118
        return TRUE;
119
      }
120
    }
121
  }
122

    
123
  /**
124
   * Return the token replaced row class for the specified row.
125
   */
126
  public function get_row_class($row_index) {
127
    if ($this->uses_row_class()) {
128
      $class = $this->options['row_class'];
129

    
130
      if ($this->uses_fields() && $this->view->field) {
131
        $classes = array();
132

    
133
        // Explode the value by whitespace, this allows the function to handle
134
        // a single class name and multiple class names that are then tokenized.
135
        foreach (explode(' ', $class) as $token_class) {
136
          $classes = array_merge($classes, explode(' ', strip_tags($this->tokenize_value($token_class, $row_index))));
137
        }
138
      }
139
      else {
140
        $classes = explode(' ', $class);
141
      }
142

    
143
      // Convert whatever the result is to a nice clean class name
144
      foreach ($classes as &$class) {
145
        $class = drupal_clean_css_identifier($class);
146
      }
147
      return implode(' ', $classes);
148
    }
149
  }
150

    
151
  /**
152
   * Take a value and apply token replacement logic to it.
153
   */
154
  public function tokenize_value($value, $row_index) {
155
    if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) {
156
      $fake_item = array(
157
        'alter_text' => TRUE,
158
        'text' => $value,
159
      );
160

    
161
      // Row tokens might be empty, for example for node row style.
162
      $tokens = isset($this->row_tokens[$row_index]) ? $this->row_tokens[$row_index] : array();
163
      if (!empty($this->view->build_info['substitutions'])) {
164
        $tokens += $this->view->build_info['substitutions'];
165
      }
166

    
167
      if ($tokens) {
168
        $value = strtr($value, $tokens);
169
      }
170
    }
171

    
172
    return $value;
173
  }
174

    
175
  /**
176
   * Should the output of the style plugin be rendered even if it's empty.
177
   */
178
  public function even_empty() {
179
    return !empty($this->definition['even empty']);
180
  }
181

    
182
  /**
183
   * {@inheritdoc}
184
   */
185
  public function option_definition() {
186
    $options = parent::option_definition();
187
    $options['grouping'] = array('default' => array());
188
    if ($this->uses_row_class()) {
189
      $options['row_class'] = array('default' => '');
190
      $options['default_row_class'] = array('default' => TRUE, 'bool' => TRUE);
191
      $options['row_class_special'] = array('default' => TRUE, 'bool' => TRUE);
192
    }
193
    $options['uses_fields'] = array('default' => FALSE, 'bool' => TRUE);
194

    
195
    return $options;
196
  }
197

    
198
  /**
199
   * {@inheritdoc}
200
   */
201
  public function options_form(&$form, &$form_state) {
202
    parent::options_form($form, $form_state);
203
    // Only fields-based views can handle grouping. Style plugins can also
204
    // exclude themselves from being groupable by setting their "use grouping"
205
    // definition key to FALSE.
206
    // @todo Document "uses grouping" in docs.php when docs.php is written.
207
    if ($this->uses_fields() && $this->definition['uses grouping']) {
208
      $options = array('' => t('- None -'));
209
      $field_labels = $this->display->handler->get_field_labels(TRUE);
210
      $options += $field_labels;
211
      // If there are no fields, we can't group on them.
212
      if (count($options) > 1) {
213
        // This is for backward compatibility, when there was just a single
214
        // select form.
215
        if (is_string($this->options['grouping'])) {
216
          $grouping = $this->options['grouping'];
217
          $this->options['grouping'] = array();
218
          $this->options['grouping'][0]['field'] = $grouping;
219
        }
220
        if (isset($this->options['group_rendered']) && is_string($this->options['group_rendered'])) {
221
          $this->options['grouping'][0]['rendered'] = $this->options['group_rendered'];
222
          unset($this->options['group_rendered']);
223
        }
224

    
225
        $c = count($this->options['grouping']);
226
        // Add a form for every grouping, plus one.
227
        for ($i = 0; $i <= $c; $i++) {
228
          $grouping = !empty($this->options['grouping'][$i]) ? $this->options['grouping'][$i] : array();
229
          $grouping += array('field' => '', 'rendered' => TRUE, 'rendered_strip' => FALSE);
230
          $form['grouping'][$i]['field'] = array(
231
            '#type' => 'select',
232
            '#title' => t('Grouping field Nr.@number', array('@number' => $i + 1)),
233
            '#options' => $options,
234
            '#default_value' => $grouping['field'],
235
            '#description' => t('You may optionally specify a field by which to group the records. Leave blank to not group.'),
236
          );
237
          $form['grouping'][$i]['rendered'] = array(
238
            '#type' => 'checkbox',
239
            '#title' => t('Use rendered output to group rows'),
240
            '#default_value' => $grouping['rendered'],
241
            '#description' => t('If enabled the rendered output of the grouping field is used to group the rows.'),
242
            '#dependency' => array(
243
              'edit-style-options-grouping-' . $i . '-field' => array_keys($field_labels),
244
            )
245
          );
246
          $form['grouping'][$i]['rendered_strip'] = array(
247
            '#type' => 'checkbox',
248
            '#title' => t('Remove tags from rendered output'),
249
            '#default_value' => $grouping['rendered_strip'],
250
            '#description' => t('Some modules add HTML to the rendered output and prevent the rows from grouping correctly. Stripping the HTML tags should correct this.'),
251
            '#dependency' => array(
252
              'edit-style-options-grouping-' . $i . '-field' => array_keys($field_labels),
253
            )
254
          );
255
        }
256
      }
257
    }
258

    
259
    if ($this->uses_row_class()) {
260
      $form['row_class'] = array(
261
        '#title' => t('Row class'),
262
        '#description' => t('The class to provide on each row.'),
263
        '#type' => 'textfield',
264
        '#default_value' => $this->options['row_class'],
265
      );
266

    
267
      if ($this->uses_fields()) {
268
        $form['row_class']['#description'] .= ' ' . t('You may use field tokens from as per the "Replacement patterns" used in "Rewrite the output of this field" for all fields.');
269
      }
270

    
271
      $form['default_row_class'] = array(
272
        '#title' => t('Add views row classes'),
273
        '#description' => t('Add the default row classes like views-row-1 to the output. You can use this to quickly reduce the amount of markup the view provides by default, at the cost of making it more difficult to apply CSS.'),
274
        '#type' => 'checkbox',
275
        '#default_value' => $this->options['default_row_class'],
276
      );
277
      $form['row_class_special'] = array(
278
        '#title' => t('Add striping (odd/even), first/last row classes'),
279
        '#description' => t('Add css classes to the first and last line, as well as odd/even classes for striping.'),
280
        '#type' => 'checkbox',
281
        '#default_value' => $this->options['row_class_special'],
282
      );
283
    }
284

    
285
    if (!$this->uses_fields() || !empty($this->options['uses_fields'])) {
286
      $form['uses_fields'] = array(
287
        '#type' => 'checkbox',
288
        '#title' => t('Force using fields'),
289
        '#description' => t('If neither the row nor the style plugin supports fields, this field allows to enable them, so you can for example use groupby.'),
290
        '#default_value' => $this->options['uses_fields'],
291
      );
292
    }
293
  }
294

    
295
  /**
296
   * {@inheritdoc}
297
   */
298
  public function options_validate(&$form, &$form_state) {
299
    // Don't run validation on style plugins without the grouping setting.
300
    if (isset($form_state['values']['style_options']['grouping'])) {
301
      // Don't save grouping if no field is specified.
302
      foreach ($form_state['values']['style_options']['grouping'] as $index => $grouping) {
303
        if (empty($grouping['field'])) {
304
          unset($form_state['values']['style_options']['grouping'][$index]);
305
        }
306
      }
307
    }
308
  }
309

    
310
  /**
311
   * Called by the view builder to see if this style handler wants to
312
   * interfere with the sorts. If so it should build; if it returns
313
   * any non-TRUE value, normal sorting will NOT be added to the query.
314
   */
315
  public function build_sort() {
316
    return TRUE;
317
  }
318

    
319
  /**
320
   * Called by the view builder to let the style build a second set of
321
   * sorts that will come after any other sorts in the view.
322
   */
323
  public function build_sort_post() {
324
  }
325

    
326
  /**
327
   * Allow the style to do stuff before each row is rendered.
328
   *
329
   * @param array $result
330
   *   The full array of results from the query.
331
   */
332
  public function pre_render($result) {
333
    if (!empty($this->row_plugin)) {
334
      $this->row_plugin->pre_render($result);
335
    }
336
  }
337

    
338
  /**
339
   * Render the display in this style.
340
   */
341
  public function render() {
342
    if ($this->uses_row_plugin() && empty($this->row_plugin)) {
343
      debug('views_plugin_style_default: Missing row plugin');
344
      return;
345
    }
346

    
347
    // Group the rows according to the grouping instructions, if specified.
348
    $sets = $this->render_grouping(
349
      $this->view->result,
350
      $this->options['grouping'],
351
      TRUE
352
    );
353

    
354
    return $this->render_grouping_sets($sets);
355
  }
356

    
357
  /**
358
   * Render the grouping sets.
359
   *
360
   * Plugins may override this method if they wish some other way of handling
361
   * grouping.
362
   *
363
   * @param array $sets
364
   *   Array containing the grouping sets to render.
365
   * @param int $level
366
   *   Integer indicating the hierarchical level of the grouping.
367
   *
368
   * @return string
369
   *   Rendered output of given grouping sets.
370
   */
371
  public function render_grouping_sets($sets, $level = 0) {
372
    $output = '';
373
    foreach ($sets as $set) {
374
      $row = reset($set['rows']);
375
      // Render as a grouping set.
376
      if (is_array($row) && isset($row['group'])) {
377
        $output .= theme(views_theme_functions('views_view_grouping', $this->view, $this->display),
378
          array(
379
            'view' => $this->view,
380
            'grouping' => $this->options['grouping'][$level],
381
            'grouping_level' => $level,
382
            'rows' => $set['rows'],
383
            'title' => $set['group'])
384
        );
385
      }
386
      // Render as a record set.
387
      else {
388
        if ($this->uses_row_plugin()) {
389
          foreach ($set['rows'] as $index => $row) {
390
            $this->view->row_index = $index;
391
            $set['rows'][$index] = $this->row_plugin->render($row);
392
          }
393
        }
394

    
395
        $output .= theme($this->theme_functions(),
396
          array(
397
            'view' => $this->view,
398
            'options' => $this->options,
399
            'grouping_level' => $level,
400
            'rows' => $set['rows'],
401
            'title' => $set['group'])
402
        );
403
      }
404
    }
405
    unset($this->view->row_index);
406
    return $output;
407
  }
408

    
409
  /**
410
   * Group records as needed for rendering.
411
   *
412
   * @param array $records
413
   *   An array of records from the view to group.
414
   * @param array $groupings
415
   *   An array of grouping instructions on which fields to group. If empty, the
416
   *   result set will be given a single group with an empty string as a label.
417
   * @param bool $group_rendered
418
   *   Boolean value whether to use the rendered or the raw field value for
419
   *   grouping. If set to NULL the return is structured as before
420
   *   Views 7.x-3.0-rc2. After Views 7.x-3.0 this boolean is only used if
421
   *   $groupings is an old-style string or if the rendered option is missing
422
   *   for a grouping instruction.
423
   *
424
   * @return array
425
   *   The grouped record set.
426
   *   A nested set structure is generated if multiple grouping fields are used.
427
   *
428
   *   @code
429
   *   array(
430
   *     'grouping_field_1:grouping_1' => array(
431
   *       'group' => 'grouping_field_1:content_1',
432
   *       'rows' => array(
433
   *         'grouping_field_2:grouping_a' => array(
434
   *           'group' => 'grouping_field_2:content_a',
435
   *           'rows' => array(
436
   *             $row_index_1 => $row_1,
437
   *             $row_index_2 => $row_2,
438
   *             // ...
439
   *           )
440
   *         ),
441
   *       ),
442
   *     ),
443
   *     'grouping_field_1:grouping_2' => array(
444
   *       // ...
445
   *     ),
446
   *   )
447
   *   @endcode
448
   */
449
  public function render_grouping($records, $groupings = array(), $group_rendered = NULL) {
450
    // This is for backward compatibility, when $groupings was a string
451
    // containing the ID of a single field.
452
    if (is_string($groupings)) {
453
      $rendered = $group_rendered === NULL ? TRUE : $group_rendered;
454
      $groupings = array(array('field' => $groupings, 'rendered' => $rendered));
455
    }
456

    
457
    // Make sure fields are rendered
458
    $this->render_fields($this->view->result);
459
    $sets = array();
460
    if ($groupings) {
461
      foreach ($records as $index => $row) {
462
        // Iterate through configured grouping fields to determine the
463
        // hierarchically positioned set where the current row belongs to.
464
        // While iterating, parent groups, that do not exist yet, are added.
465
        $set = &$sets;
466
        foreach ($groupings as $info) {
467
          $field = $info['field'];
468
          $rendered = isset($info['rendered']) ? $info['rendered'] : $group_rendered;
469
          $rendered_strip = isset($info['rendered_strip']) ? $info['rendered_strip'] : FALSE;
470
          $grouping = '';
471
          $group_content = '';
472
          // Group on the rendered version of the field, not the raw.  That way,
473
          // we can control any special formatting of the grouping field through
474
          // the admin or theme layer or anywhere else we'd like.
475
          if (isset($this->view->field[$field])) {
476
            $group_content = $this->get_field($index, $field);
477
            if ($this->view->field[$field]->options['label']) {
478
              $group_content = $this->view->field[$field]->options['label'] . ': ' . $group_content;
479
            }
480
            if ($rendered) {
481
              $grouping = $group_content;
482
              if ($rendered_strip) {
483
                $group_content = $grouping = strip_tags(htmlspecialchars_decode($group_content));
484
              }
485
            }
486
            else {
487
              $grouping = $this->get_field_value($index, $field);
488
              // Not all field handlers return a scalar value,
489
              // e.g. views_handler_field_field.
490
              if (!is_scalar($grouping)) {
491
                $grouping = md5(serialize($grouping));
492
              }
493
            }
494
          }
495

    
496
          // Create the group if it does not exist yet.
497
          if (empty($set[$grouping])) {
498
            $set[$grouping]['group'] = $group_content;
499
            $set[$grouping]['rows'] = array();
500
          }
501

    
502
          // Move the set reference into the row set of the group we just
503
          // determined.
504
          $set = &$set[$grouping]['rows'];
505
        }
506
        // Add the row to the hierarchically positioned row set we just
507
        // determined.
508
        $set[$index] = $row;
509
      }
510
    }
511
    else {
512
      // Create a single group with an empty grouping field.
513
      $sets[''] = array(
514
        'group' => '',
515
        'rows' => $records,
516
      );
517
    }
518

    
519
    // If this parameter isn't explicitly set modify the output to be fully
520
    // backward compatible to code before Views 7.x-3.0-rc2.
521
    // @todo Remove this as soon as possible e.g. October 2020
522
    if ($group_rendered === NULL) {
523
      $old_style_sets = array();
524
      foreach ($sets as $group) {
525
        $old_style_sets[$group['group']] = $group['rows'];
526
      }
527
      $sets = $old_style_sets;
528
    }
529

    
530
    return $sets;
531
  }
532

    
533
  /**
534
   * Render all of the fields for a given style and store them on the object.
535
   *
536
   * @param array $result
537
   *   The result array from $view->result
538
   */
539
  public function render_fields($result) {
540
    if (!$this->uses_fields()) {
541
      return;
542
    }
543

    
544
    if (!isset($this->rendered_fields)) {
545
      $this->rendered_fields = array();
546
      $this->view->row_index = 0;
547
      $keys = array_keys($this->view->field);
548

    
549
      // If all fields have a field::access FALSE there might be no fields, so
550
      // there is no reason to execute this code.
551
      if (!empty($keys)) {
552
        foreach ($result as $count => $row) {
553
          $this->view->row_index = $count;
554
          foreach ($keys as $id) {
555
            $this->rendered_fields[$count][$id] = $this->view->field[$id]->theme($row);
556
          }
557

    
558
          $this->row_tokens[$count] = $this->view->field[$id]->get_render_tokens(array());
559
        }
560
      }
561
      unset($this->view->row_index);
562
    }
563

    
564
    return $this->rendered_fields;
565
  }
566

    
567
  /**
568
   * Get a rendered field.
569
   *
570
   * @param int $index
571
   *   The index count of the row.
572
   * @param string $field
573
   *    The id of the field.
574
   */
575
  public function get_field($index, $field) {
576
    if (!isset($this->rendered_fields)) {
577
      $this->render_fields($this->view->result);
578
    }
579

    
580
    if (isset($this->rendered_fields[$index][$field])) {
581
      return $this->rendered_fields[$index][$field];
582
    }
583
  }
584

    
585
  /**
586
  * Get the raw field value.
587
  *
588
  * @param int $index
589
  *   The index count of the row.
590
  * @param string $field
591
  *    The id of the field.
592
  */
593
  public function get_field_value($index, $field) {
594
    $this->view->row_index = $index;
595
    $value = $this->view->field[$field]->get_value($this->view->result[$index]);
596
    unset($this->view->row_index);
597
    return $value;
598
  }
599

    
600
  /**
601
   * {@inheritdoc}
602
   */
603
  public function validate() {
604
    $errors = parent::validate();
605

    
606
    if ($this->uses_row_plugin()) {
607
      $plugin = $this->display->handler->get_plugin('row');
608
      if (empty($plugin)) {
609
        $errors[] = t('Style @style requires a row style but the row plugin is invalid.', array('@style' => $this->definition['title']));
610
      }
611
      else {
612
        $result = $plugin->validate();
613
        if (!empty($result) && is_array($result)) {
614
          $errors = array_merge($errors, $result);
615
        }
616
      }
617
    }
618
    return $errors;
619
  }
620

    
621
  /**
622
   * {@inheritdoc}
623
   */
624
  public function query() {
625
    parent::query();
626
    if (isset($this->row_plugin)) {
627
      $this->row_plugin->query();
628
    }
629
  }
630

    
631
}
632

    
633
/**
634
 * @}
635
 */