Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / plugins / views_plugin_style.inc @ 651307cd

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
12
 * can choose to display a collection of fields, node_view() output,
13
 * table output, or any kind of crazy output they want.
14
 *
15
 * Many style plugins can have an optional 'row' plugin, that displays
16
 * a single record. Not all style plugins can utilize this, so it is
17
 * up to the plugin to 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
   * Store all available tokens row rows.
28
   */
29
  var $row_tokens = array();
30

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

    
39
  /**
40
   * Initialize a style plugin.
41
   *
42
   * @param $view
43
   * @param $display
44
   * @param $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
  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
  function destroy() {
69
    parent::destroy();
70

    
71
    if (isset($this->row_plugin)) {
72
      $this->row_plugin->destroy();
73
    }
74
  }
75

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

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

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

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

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

    
127
      if ($this->uses_fields() && $this->view->field) {
128
        $classes = array();
129

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

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

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

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

    
164
      if ($tokens) {
165
        $value = strtr($value, $tokens);
166
      }
167
    }
168

    
169
    return $value;
170
  }
171

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

    
179
  function option_definition() {
180
    $options = parent::option_definition();
181
    $options['grouping'] = array('default' => array());
182
    if ($this->uses_row_class()) {
183
      $options['row_class'] = array('default' => '');
184
      $options['default_row_class'] = array('default' => TRUE, 'bool' => TRUE);
185
      $options['row_class_special'] = array('default' => TRUE, 'bool' => TRUE);
186
    }
187
    $options['uses_fields'] = array('default' => FALSE, 'bool' => TRUE);
188

    
189
    return $options;
190
  }
191

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

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

    
249
    if ($this->uses_row_class()) {
250
      $form['row_class'] = array(
251
        '#title' => t('Row class'),
252
        '#description' => t('The class to provide on each row.'),
253
        '#type' => 'textfield',
254
        '#default_value' => $this->options['row_class'],
255
      );
256

    
257
      if ($this->uses_fields()) {
258
        $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.');
259
      }
260

    
261
      $form['default_row_class'] = array(
262
        '#title' => t('Add views row classes'),
263
        '#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.'),
264
        '#type' => 'checkbox',
265
        '#default_value' => $this->options['default_row_class'],
266
      );
267
      $form['row_class_special'] = array(
268
        '#title' => t('Add striping (odd/even), first/last row classes'),
269
        '#description' => t('Add css classes to the first and last line, as well as odd/even classes for striping.'),
270
        '#type' => 'checkbox',
271
        '#default_value' => $this->options['row_class_special'],
272
      );
273
    }
274

    
275
    if (!$this->uses_fields() || !empty($this->options['uses_fields'])) {
276
      $form['uses_fields'] = array(
277
        '#type' => 'checkbox',
278
        '#title' => t('Force using fields'),
279
        '#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.'),
280
        '#default_value' => $this->options['uses_fields'],
281
      );
282
    }
283
  }
284

    
285
  function options_validate(&$form, &$form_state) {
286
    // Don't run validation on style plugins without the grouping setting.
287
    if (isset($form_state['values']['style_options']['grouping'])) {
288
      // Don't save grouping if no field is specified.
289
      foreach ($form_state['values']['style_options']['grouping'] as $index => $grouping) {
290
        if (empty($grouping['field'])) {
291
          unset($form_state['values']['style_options']['grouping'][$index]);
292
        }
293
      }
294
    }
295
  }
296

    
297
  /**
298
   * Called by the view builder to see if this style handler wants to
299
   * interfere with the sorts. If so it should build; if it returns
300
   * any non-TRUE value, normal sorting will NOT be added to the query.
301
   */
302
  function build_sort() { return TRUE; }
303

    
304
  /**
305
   * Called by the view builder to let the style build a second set of
306
   * sorts that will come after any other sorts in the view.
307
   */
308
  function build_sort_post() { }
309

    
310
  /**
311
   * Allow the style to do stuff before each row is rendered.
312
   *
313
   * @param $result
314
   *   The full array of results from the query.
315
   */
316
  function pre_render($result) {
317
    if (!empty($this->row_plugin)) {
318
      $this->row_plugin->pre_render($result);
319
    }
320
  }
321

    
322
  /**
323
   * Render the display in this style.
324
   */
325
  function render() {
326
    if ($this->uses_row_plugin() && empty($this->row_plugin)) {
327
      debug('views_plugin_style_default: Missing row plugin');
328
      return;
329
    }
330

    
331
    // Group the rows according to the grouping instructions, if specified.
332
    $sets = $this->render_grouping(
333
      $this->view->result,
334
      $this->options['grouping'],
335
      TRUE
336
    );
337

    
338
    return $this->render_grouping_sets($sets);
339
  }
340

    
341
  /**
342
   * Render the grouping sets.
343
   *
344
   * Plugins may override this method if they wish some other way of handling
345
   * grouping.
346
   *
347
   * @param $sets
348
   *   Array containing the grouping sets to render.
349
   * @param $level
350
   *   Integer indicating the hierarchical level of the grouping.
351
   *
352
   * @return string
353
   *   Rendered output of given grouping sets.
354
   */
355
  function render_grouping_sets($sets, $level = 0) {
356
    $output = '';
357
    foreach ($sets as $set) {
358
      $row = reset($set['rows']);
359
      // Render as a grouping set.
360
      if (is_array($row) && isset($row['group'])) {
361
        $output .= theme(views_theme_functions('views_view_grouping', $this->view, $this->display),
362
          array(
363
            'view' => $this->view,
364
            'grouping' => $this->options['grouping'][$level],
365
            'grouping_level' => $level,
366
            'rows' => $set['rows'],
367
            'title' => $set['group'])
368
        );
369
      }
370
      // Render as a record set.
371
      else {
372
        if ($this->uses_row_plugin()) {
373
          foreach ($set['rows'] as $index => $row) {
374
            $this->view->row_index = $index;
375
            $set['rows'][$index] = $this->row_plugin->render($row);
376
          }
377
        }
378

    
379
        $output .= theme($this->theme_functions(),
380
          array(
381
            'view' => $this->view,
382
            'options' => $this->options,
383
            'grouping_level' => $level,
384
            'rows' => $set['rows'],
385
            'title' => $set['group'])
386
        );
387
      }
388
    }
389
    unset($this->view->row_index);
390
    return $output;
391
  }
392

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

    
440
    // Make sure fields are rendered
441
    $this->render_fields($this->view->result);
442
    $sets = array();
443
    if ($groupings) {
444
      foreach ($records as $index => $row) {
445
        // Iterate through configured grouping fields to determine the
446
        // hierarchically positioned set where the current row belongs to.
447
        // While iterating, parent groups, that do not exist yet, are added.
448
        $set = &$sets;
449
        foreach ($groupings as $info) {
450
          $field = $info['field'];
451
          $rendered = isset($info['rendered']) ? $info['rendered'] : $group_rendered;
452
          $rendered_strip = isset($info['rendered_strip']) ? $info['rendered_strip'] : FALSE;
453
          $grouping = '';
454
          $group_content = '';
455
          // Group on the rendered version of the field, not the raw.  That way,
456
          // we can control any special formatting of the grouping field through
457
          // the admin or theme layer or anywhere else we'd like.
458
          if (isset($this->view->field[$field])) {
459
            $group_content = $this->get_field($index, $field);
460
            if ($this->view->field[$field]->options['label']) {
461
              $group_content = $this->view->field[$field]->options['label'] . ': ' . $group_content;
462
            }
463
            if ($rendered) {
464
              $grouping = $group_content;
465
              if ($rendered_strip) {
466
                $group_content = $grouping = strip_tags(htmlspecialchars_decode($group_content));
467
              }
468
            }
469
            else {
470
              $grouping = $this->get_field_value($index, $field);
471
              // Not all field handlers return a scalar value,
472
              // e.g. views_handler_field_field.
473
              if (!is_scalar($grouping)) {
474
                $grouping = md5(serialize($grouping));
475
              }
476
            }
477
          }
478

    
479
          // Create the group if it does not exist yet.
480
          if (empty($set[$grouping])) {
481
            $set[$grouping]['group'] = $group_content;
482
            $set[$grouping]['rows'] = array();
483
          }
484

    
485
          // Move the set reference into the row set of the group we just determined.
486
          $set = &$set[$grouping]['rows'];
487
        }
488
        // Add the row to the hierarchically positioned row set we just determined.
489
        $set[$index] = $row;
490
      }
491
    }
492
    else {
493
      // Create a single group with an empty grouping field.
494
      $sets[''] = array(
495
        'group' => '',
496
        'rows' => $records,
497
      );
498
    }
499

    
500
    // If this parameter isn't explicitly set modify the output to be fully
501
    // backward compatible to code before Views 7.x-3.0-rc2.
502
    // @TODO Remove this as soon as possible e.g. October 2020
503
    if ($group_rendered === NULL) {
504
      $old_style_sets = array();
505
      foreach ($sets as $group) {
506
        $old_style_sets[$group['group']] = $group['rows'];
507
      }
508
      $sets = $old_style_sets;
509
    }
510

    
511
    return $sets;
512
  }
513

    
514
  /**
515
   * Render all of the fields for a given style and store them on the object.
516
   *
517
   * @param $result
518
   *   The result array from $view->result
519
   */
520
  function render_fields($result) {
521
    if (!$this->uses_fields()) {
522
      return;
523
    }
524

    
525
    if (!isset($this->rendered_fields)) {
526
      $this->rendered_fields = array();
527
      $this->view->row_index = 0;
528
      $keys = array_keys($this->view->field);
529

    
530
      // If all fields have a field::access FALSE there might be no fields, so
531
      // there is no reason to execute this code.
532
      if (!empty($keys)) {
533
        foreach ($result as $count => $row) {
534
          $this->view->row_index = $count;
535
          foreach ($keys as $id) {
536
            $this->rendered_fields[$count][$id] = $this->view->field[$id]->theme($row);
537
          }
538

    
539
          $this->row_tokens[$count] = $this->view->field[$id]->get_render_tokens(array());
540
        }
541
      }
542
      unset($this->view->row_index);
543
    }
544

    
545
    return $this->rendered_fields;
546
  }
547

    
548
  /**
549
   * Get a rendered field.
550
   *
551
   * @param $index
552
   *   The index count of the row.
553
   * @param $field
554
   *    The id of the field.
555
   */
556
  function get_field($index, $field) {
557
    if (!isset($this->rendered_fields)) {
558
      $this->render_fields($this->view->result);
559
    }
560

    
561
    if (isset($this->rendered_fields[$index][$field])) {
562
      return $this->rendered_fields[$index][$field];
563
    }
564
  }
565

    
566
  /**
567
  * Get the raw field value.
568
  *
569
  * @param $index
570
  *   The index count of the row.
571
  * @param $field
572
  *    The id of the field.
573
  */
574
  function get_field_value($index, $field) {
575
    $this->view->row_index = $index;
576
    $value = $this->view->field[$field]->get_value($this->view->result[$index]);
577
    unset($this->view->row_index);
578
    return $value;
579
  }
580

    
581
  function validate() {
582
    $errors = parent::validate();
583

    
584
    if ($this->uses_row_plugin()) {
585
      $plugin = $this->display->handler->get_plugin('row');
586
      if (empty($plugin)) {
587
        $errors[] = t('Style @style requires a row style but the row plugin is invalid.', array('@style' => $this->definition['title']));
588
      }
589
      else {
590
        $result = $plugin->validate();
591
        if (!empty($result) && is_array($result)) {
592
          $errors = array_merge($errors, $result);
593
        }
594
      }
595
    }
596
    return $errors;
597
  }
598

    
599
  function query() {
600
    parent::query();
601
    if (isset($this->row_plugin)) {
602
      $this->row_plugin->query();
603
    }
604
  }
605
}
606

    
607
/**
608
 * @}
609
 */