Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / plugins / views_plugin_style.inc @ 4003efde

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 ($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
      $level = isset($set['level']) ? $set['level'] : 0;
376
      // Render as a grouping set.
377
      if (is_array($row) && isset($row['group'])) {
378
        $output .= theme(views_theme_functions('views_view_grouping', $this->view, $this->display),
379
          array(
380
            'view' => $this->view,
381
            'grouping' => $this->options['grouping'][$level],
382
            'grouping_level' => $level,
383
            'rows' => $set['rows'],
384
            'title' => $set['group'])
385
        );
386
      }
387
      // Render as a record set.
388
      else {
389
        if ($this->uses_row_plugin()) {
390
          foreach ($set['rows'] as $index => $row) {
391
            $this->view->row_index = $index;
392
            $set['rows'][$index] = $this->row_plugin->render($row);
393
          }
394
        }
395

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

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

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

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

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

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

    
532
    return $sets;
533
  }
534

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

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

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

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

    
566
    return $this->rendered_fields;
567
  }
568

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

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

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

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

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

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

    
633
}
634

    
635
/**
636
 * @}
637
 */