commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / views / plugins / views_plugin_style_mapping.inc
1 <?php
2
3 /**
4 * @file
5 * Definition of views_plugin_style_mapping.
6 */
7
8 /**
9 * Allows fields to be mapped to specific use cases.
10 */
11 abstract class views_plugin_style_mapping extends views_plugin_style {
12
13 /**
14 * Builds the list of field mappings.
15 *
16 * @return array
17 * An associative array, keyed by the field name, containing the following
18 * key-value pairs:
19 * - #title: The human-readable label for this field.
20 * - #default_value: The default value for this field. If not provided, an
21 * empty string will be used.
22 * - #description: A description of this field.
23 * - #required: Whether this field is required.
24 * - #filter: (optional) A method on the plugin to filter field options.
25 * - #toggle: (optional) If this select should be toggled by a checkbox.
26 */
27 abstract protected function define_mapping();
28
29 /**
30 * Overrides views_plugin_style::option_definition().
31 */
32 function option_definition() {
33 $options = parent::option_definition();
34
35 // Parse the mapping and add a default for each.
36 foreach ($this->define_mapping() as $key => $value) {
37 $default = !empty($value['#multiple']) ? array() : '';
38 $options['mapping']['contains'][$key] = array(
39 'default' => isset($value['#default_value']) ? $value['#default_value'] : $default,
40 );
41 if (!empty($value['#toggle'])) {
42 $options['mapping']['contains']["toggle_$key"] = array(
43 'default' => FALSE,
44 'bool' => TRUE,
45 );
46 }
47 }
48
49 return $options;
50 }
51
52 /**
53 * Overrides views_plugin_style::options_form().
54 */
55 function options_form(&$form, &$form_state) {
56 parent::options_form($form, $form_state);
57
58 // Get the mapping.
59 $mapping = $this->define_mapping();
60
61 // Restrict the list of defaults to the mapping, in case they have changed.
62 $options = array_intersect_key($this->options['mapping'], $mapping);
63
64 // Get the labels of the fields added to this display.
65 $field_labels = $this->display->handler->get_field_labels();
66
67 // Provide some default values.
68 $defaults = array(
69 '#type' => 'select',
70 '#required' => FALSE,
71 '#multiple' => FALSE,
72 );
73
74 // For each mapping, add a select element to the form.
75 foreach ($options as $key => $value) {
76 // If the field is optional, add a 'None' value to the top of the options.
77 $field_options = array();
78 $required = !empty($mapping[$key]['#required']);
79 if (!$required && empty($mapping[$key]['#multiple'])) {
80 $field_options = array('' => t('- None -'));
81 }
82 $field_options += $field_labels;
83
84 // Optionally filter the available fields.
85 if (isset($mapping[$key]['#filter'])) {
86 $this->view->init_handlers();
87 $this::$mapping[$key]['#filter']($field_options);
88 unset($mapping[$key]['#filter']);
89 }
90
91 // These values must always be set.
92 $overrides = array(
93 '#options' => $field_options,
94 '#default_value' => $options[$key],
95 );
96
97 // Optionally allow the select to be toggleable.
98 if (!empty($mapping[$key]['#toggle'])) {
99 $form['mapping']["toggle_$key"] = array(
100 '#type' => 'checkbox',
101 '#title' => t('Use a custom %field_name', array('%field_name' => strtolower($mapping[$key]['#title']))),
102 '#default_value' => $this->options['mapping']["toggle_$key"],
103 );
104 $overrides['#states']['visible'][':input[name="style_options[mapping][' . "toggle_$key" . ']"]'] = array('checked' => TRUE);
105 }
106
107 $form['mapping'][$key] = $overrides + $mapping[$key] + $defaults;
108 }
109 }
110
111 /**
112 * Overrides views_plugin_style::render().
113 *
114 * Provides the mapping definition as an available variable.
115 */
116 function render() {
117 return theme($this->theme_functions(), array(
118 'view' => $this->view,
119 'options' => $this->options,
120 'rows' => $this->view->result,
121 'mapping' => $this->define_mapping(),
122 ));
123 }
124
125 }