Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / plugins / views_plugin_style.inc @ 76df55b7

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
      if ($this->uses_fields() && $this->view->field) {
127
        $class = strip_tags($this->tokenize_value($class, $row_index));
128
      }
129

    
130
      $classes = explode(' ', $class);
131
      foreach ($classes as &$class) {
132
        $class = drupal_clean_css_identifier($class);
133
      }
134
      return implode(' ', $classes);
135
    }
136
  }
137

    
138
  /**
139
   * Take a value and apply token replacement logic to it.
140
   */
141
  function tokenize_value($value, $row_index) {
142
    if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) {
143
      $fake_item = array(
144
        'alter_text' => TRUE,
145
        'text' => $value,
146
      );
147

    
148
      // Row tokens might be empty, for example for node row style.
149
      $tokens = isset($this->row_tokens[$row_index]) ? $this->row_tokens[$row_index] : array();
150
      if (!empty($this->view->build_info['substitutions'])) {
151
        $tokens += $this->view->build_info['substitutions'];
152
      }
153

    
154
      if ($tokens) {
155
        $value = strtr($value, $tokens);
156
      }
157
    }
158

    
159
    return $value;
160
  }
161

    
162
  /**
163
   * Should the output of the style plugin be rendered even if it's a empty view.
164
   */
165
  function even_empty() {
166
    return !empty($this->definition['even empty']);
167
  }
168

    
169
  function option_definition() {
170
    $options = parent::option_definition();
171
    $options['grouping'] = array('default' => array());
172
    if ($this->uses_row_class()) {
173
      $options['row_class'] = array('default' => '');
174
      $options['default_row_class'] = array('default' => TRUE, 'bool' => TRUE);
175
      $options['row_class_special'] = array('default' => TRUE, 'bool' => TRUE);
176
    }
177
    $options['uses_fields'] = array('default' => FALSE, 'bool' => TRUE);
178

    
179
    return $options;
180
  }
181

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

    
205
        $c = count($this->options['grouping']);
206
        // Add a form for every grouping, plus one.
207
        for ($i = 0; $i <= $c; $i++) {
208
          $grouping = !empty($this->options['grouping'][$i]) ? $this->options['grouping'][$i] : array();
209
          $grouping += array('field' => '', 'rendered' => TRUE, 'rendered_strip' => FALSE);
210
          $form['grouping'][$i]['field'] = array(
211
            '#type' => 'select',
212
            '#title' => t('Grouping field Nr.@number', array('@number' => $i + 1)),
213
            '#options' => $options,
214
            '#default_value' => $grouping['field'],
215
            '#description' => t('You may optionally specify a field by which to group the records. Leave blank to not group.'),
216
          );
217
          $form['grouping'][$i]['rendered'] = array(
218
            '#type' => 'checkbox',
219
            '#title' => t('Use rendered output to group rows'),
220
            '#default_value' => $grouping['rendered'],
221
            '#description' => t('If enabled the rendered output of the grouping field is used to group the rows.'),
222
            '#dependency' => array(
223
              'edit-style-options-grouping-' . $i . '-field' => array_keys($field_labels),
224
            )
225
          );
226
          $form['grouping'][$i]['rendered_strip'] = array(
227
            '#type' => 'checkbox',
228
            '#title' => t('Remove tags from rendered output'),
229
            '#default_value' => $grouping['rendered_strip'],
230
            '#dependency' => array(
231
              'edit-style-options-grouping-' . $i . '-field' => array_keys($field_labels),
232
            )
233
          );
234
        }
235
      }
236
    }
237

    
238
    if ($this->uses_row_class()) {
239
      $form['row_class'] = array(
240
        '#title' => t('Row class'),
241
        '#description' => t('The class to provide on each row.'),
242
        '#type' => 'textfield',
243
        '#default_value' => $this->options['row_class'],
244
      );
245

    
246
      if ($this->uses_fields()) {
247
        $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.');
248
      }
249

    
250
      $form['default_row_class'] = array(
251
        '#title' => t('Add views row classes'),
252
        '#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.'),
253
        '#type' => 'checkbox',
254
        '#default_value' => $this->options['default_row_class'],
255
      );
256
      $form['row_class_special'] = array(
257
        '#title' => t('Add striping (odd/even), first/last row classes'),
258
        '#description' => t('Add css classes to the first and last line, as well as odd/even classes for striping.'),
259
        '#type' => 'checkbox',
260
        '#default_value' => $this->options['row_class_special'],
261
      );
262
    }
263

    
264
    if (!$this->uses_fields() || !empty($this->options['uses_fields'])) {
265
      $form['uses_fields'] = array(
266
        '#type' => 'checkbox',
267
        '#title' => t('Force using fields'),
268
        '#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.'),
269
        '#default_value' => $this->options['uses_fields'],
270
      );
271
    }
272
  }
273

    
274
  function options_validate(&$form, &$form_state) {
275
    // Don't run validation on style plugins without the grouping setting.
276
    if (isset($form_state['values']['style_options']['grouping'])) {
277
      // Don't save grouping if no field is specified.
278
      foreach ($form_state['values']['style_options']['grouping'] as $index => $grouping) {
279
        if (empty($grouping['field'])) {
280
          unset($form_state['values']['style_options']['grouping'][$index]);
281
        }
282
      }
283
    }
284
  }
285

    
286
  /**
287
   * Called by the view builder to see if this style handler wants to
288
   * interfere with the sorts. If so it should build; if it returns
289
   * any non-TRUE value, normal sorting will NOT be added to the query.
290
   */
291
  function build_sort() { return TRUE; }
292

    
293
  /**
294
   * Called by the view builder to let the style build a second set of
295
   * sorts that will come after any other sorts in the view.
296
   */
297
  function build_sort_post() { }
298

    
299
  /**
300
   * Allow the style to do stuff before each row is rendered.
301
   *
302
   * @param $result
303
   *   The full array of results from the query.
304
   */
305
  function pre_render($result) {
306
    if (!empty($this->row_plugin)) {
307
      $this->row_plugin->pre_render($result);
308
    }
309
  }
310

    
311
  /**
312
   * Render the display in this style.
313
   */
314
  function render() {
315
    if ($this->uses_row_plugin() && empty($this->row_plugin)) {
316
      debug('views_plugin_style_default: Missing row plugin');
317
      return;
318
    }
319

    
320
    // Group the rows according to the grouping instructions, if specified.
321
    $sets = $this->render_grouping(
322
      $this->view->result,
323
      $this->options['grouping'],
324
      TRUE
325
    );
326

    
327
    return $this->render_grouping_sets($sets);
328
  }
329

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

    
368
        $output .= theme($this->theme_functions(),
369
          array(
370
            'view' => $this->view,
371
            'options' => $this->options,
372
            'grouping_level' => $level,
373
            'rows' => $set['rows'],
374
            'title' => $set['group'])
375
        );
376
      }
377
    }
378
    unset($this->view->row_index);
379
    return $output;
380
  }
381

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

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

    
468
          // Create the group if it does not exist yet.
469
          if (empty($set[$grouping])) {
470
            $set[$grouping]['group'] = $group_content;
471
            $set[$grouping]['rows'] = array();
472
          }
473

    
474
          // Move the set reference into the row set of the group we just determined.
475
          $set = &$set[$grouping]['rows'];
476
        }
477
        // Add the row to the hierarchically positioned row set we just determined.
478
        $set[$index] = $row;
479
      }
480
    }
481
    else {
482
      // Create a single group with an empty grouping field.
483
      $sets[''] = array(
484
        'group' => '',
485
        'rows' => $records,
486
      );
487
    }
488

    
489
    // If this parameter isn't explicitely set modify the output to be fully
490
    // backward compatible to code before Views 7.x-3.0-rc2.
491
    // @TODO Remove this as soon as possible e.g. October 2020
492
    if ($group_rendered === NULL) {
493
      $old_style_sets = array();
494
      foreach ($sets as $group) {
495
        $old_style_sets[$group['group']] = $group['rows'];
496
      }
497
      $sets = $old_style_sets;
498
    }
499

    
500
    return $sets;
501
  }
502

    
503
  /**
504
   * Render all of the fields for a given style and store them on the object.
505
   *
506
   * @param $result
507
   *   The result array from $view->result
508
   */
509
  function render_fields($result) {
510
    if (!$this->uses_fields()) {
511
      return;
512
    }
513

    
514
    if (!isset($this->rendered_fields)) {
515
      $this->rendered_fields = array();
516
      $this->view->row_index = 0;
517
      $keys = array_keys($this->view->field);
518

    
519
      // If all fields have a field::access FALSE there might be no fields, so
520
      // there is no reason to execute this code.
521
      if (!empty($keys)) {
522
        foreach ($result as $count => $row) {
523
          $this->view->row_index = $count;
524
          foreach ($keys as $id) {
525
            $this->rendered_fields[$count][$id] = $this->view->field[$id]->theme($row);
526
          }
527

    
528
          $this->row_tokens[$count] = $this->view->field[$id]->get_render_tokens(array());
529
        }
530
      }
531
      unset($this->view->row_index);
532
    }
533

    
534
    return $this->rendered_fields;
535
  }
536

    
537
  /**
538
   * Get a rendered field.
539
   *
540
   * @param $index
541
   *   The index count of the row.
542
   * @param $field
543
   *    The id of the field.
544
   */
545
  function get_field($index, $field) {
546
    if (!isset($this->rendered_fields)) {
547
      $this->render_fields($this->view->result);
548
    }
549

    
550
    if (isset($this->rendered_fields[$index][$field])) {
551
      return $this->rendered_fields[$index][$field];
552
    }
553
  }
554

    
555
  /**
556
  * Get the raw field value.
557
  *
558
  * @param $index
559
  *   The index count of the row.
560
  * @param $field
561
  *    The id of the field.
562
  */
563
  function get_field_value($index, $field) {
564
    $this->view->row_index = $index;
565
    $value = $this->view->field[$field]->get_value($this->view->result[$index]);
566
    unset($this->view->row_index);
567
    return $value;
568
  }
569

    
570
  function validate() {
571
    $errors = parent::validate();
572

    
573
    if ($this->uses_row_plugin()) {
574
      $plugin = $this->display->handler->get_plugin('row');
575
      if (empty($plugin)) {
576
        $errors[] = t('Style @style requires a row style but the row plugin is invalid.', array('@style' => $this->definition['title']));
577
      }
578
      else {
579
        $result = $plugin->validate();
580
        if (!empty($result) && is_array($result)) {
581
          $errors = array_merge($errors, $result);
582
        }
583
      }
584
    }
585
    return $errors;
586
  }
587

    
588
  function query() {
589
    parent::query();
590
    if (isset($this->row_plugin)) {
591
      $this->row_plugin->query();
592
    }
593
  }
594
}
595

    
596
/**
597
 * @}
598
 */