Projet

Général

Profil

Paste
Télécharger (11,1 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

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

    
8
/**
9
 * Style plugin to render each item as a row in a table.
10
 *
11
 * @ingroup views_style_plugins
12
 */
13
class views_plugin_style_table extends views_plugin_style {
14

    
15
  /**
16
   * Contains the current active sort column.
17
   * @var string
18
   */
19
  public $active;
20

    
21
  /**
22
   * Contains the current active sort order, either desc or asc.
23
   * @var string
24
   */
25
  public $order;
26

    
27
  /**
28
   * {@inheritdoc}
29
   */
30
  public function option_definition() {
31
    $options = parent::option_definition();
32

    
33
    $options['columns'] = array('default' => array());
34
    $options['default'] = array('default' => '');
35
    $options['info'] = array('default' => array());
36
    $options['override'] = array('default' => TRUE, 'bool' => TRUE);
37
    $options['sticky'] = array('default' => FALSE, 'bool' => TRUE);
38
    $options['order'] = array('default' => 'asc');
39
    $options['caption'] = array('default' => '', 'translatable' => TRUE);
40
    $options['summary'] = array('default' => '', 'translatable' => TRUE);
41
    $options['empty_table'] = array('default' => FALSE, 'bool' => TRUE);
42

    
43
    return $options;
44
  }
45

    
46
  /**
47
   * Determine if we should provide sorting based upon $_GET inputs
48
   *
49
   * @return bool
50
   */
51
  public function build_sort() {
52
    if (!isset($_GET['order']) && ($this->options['default'] == -1 || empty($this->view->field[$this->options['default']]))) {
53
      return TRUE;
54
    }
55

    
56
    // If a sort we don't know anything about gets through, exit gracefully.
57
    if (isset($_GET['order']) && empty($this->view->field[$_GET['order']])) {
58
      return TRUE;
59
    }
60

    
61
    // Let the builder know whether or not we're overriding the default sorts.
62
    return empty($this->options['override']);
63
  }
64

    
65
  /**
66
   * Add our actual sort criteria
67
   */
68
  public function build_sort_post() {
69
    if (!isset($_GET['order'])) {
70
      // check for a 'default' clicksort. If there isn't one, exit gracefully.
71
      if (empty($this->options['default'])) {
72
        return;
73
      }
74
      $sort = $this->options['default'];
75
      if (!empty($this->options['info'][$sort]['default_sort_order'])) {
76
        $this->order = $this->options['info'][$sort]['default_sort_order'];
77
      }
78
      else {
79
        $this->order = !empty($this->options['order']) ? $this->options['order'] : 'asc';
80
      }
81
    }
82
    else {
83
      $sort = $_GET['order'];
84
      // Store the $order for later use.
85
      $this->order = !empty($_GET['sort']) ? strtolower($_GET['sort']) : 'asc';
86
    }
87

    
88
    // If a sort we don't know anything about gets through, exit gracefully.
89
    if (empty($this->view->field[$sort])) {
90
      return;
91
    }
92

    
93
    // Ensure $this->order is valid.
94
    if ($this->order != 'asc' && $this->order != 'desc') {
95
      $this->order = 'asc';
96
    }
97

    
98
    // Store the $sort for later use.
99
    $this->active = $sort;
100

    
101
    // Tell the field to click sort.
102
    $this->view->field[$sort]->click_sort($this->order);
103
  }
104

    
105
  /**
106
   * Normalize a list of columns based upon the fields that are
107
   * available. This compares the fields stored in the style handler
108
   * to the list of fields actually in the view, removing fields that
109
   * have been removed and adding new fields in their own column.
110
   *
111
   * - Each field must be in a column.
112
   * - Each column must be based upon a field, and that field
113
   *   is somewhere in the column.
114
   * - Any fields not currently represented must be added.
115
   * - Columns must be re-ordered to match the fields.
116
   *
117
   * @param array $columns
118
   *   An array of all fields; the key is the id of the field and the
119
   *   value is the id of the column the field should be in.
120
   * @param array $fields
121
   *   The fields to use for the columns. If not provided, they will
122
   *   be requested from the current display. The running render should
123
   *   send the fields through, as they may be different than what the
124
   *   display has listed due to access control or other changes.
125
   *
126
   * @return array
127
   *    An array of all the sanitized columns.
128
   */
129
  public function sanitize_columns($columns, $fields = NULL) {
130
    $sanitized = array();
131
    if ($fields === NULL) {
132
      $fields = $this->display->handler->get_option('fields');
133
    }
134
    // Preconfigure the sanitized array so that the order is retained.
135
    foreach ($fields as $field => $info) {
136
      // Set to itself so that if it isn't touched, it gets column
137
      // status automatically.
138
      $sanitized[$field] = $field;
139
    }
140

    
141
    foreach ($columns as $field => $column) {
142
      // first, make sure the field still exists.
143
      if (!isset($sanitized[$field])) {
144
        continue;
145
      }
146

    
147
      // If the field is the column, mark it so, or the column
148
      // it's set to is a column, that's ok
149
      if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) {
150
        $sanitized[$field] = $column;
151
      }
152
      // Since we set the field to itself initially, ignoring
153
      // the condition is ok; the field will get its column
154
      // status back.
155
    }
156

    
157
    return $sanitized;
158
  }
159

    
160
  /**
161
   * Render the given style.
162
   */
163
  public function options_form(&$form, &$form_state) {
164
    parent::options_form($form, $form_state);
165
    $handlers = $this->display->handler->get_handlers('field');
166
    if (empty($handlers)) {
167
      $form['error_markup'] = array(
168
        '#markup' => '<div class="error messages">' . t('You need at least one field before you can configure your table settings') . '</div>',
169
      );
170
      return;
171
    }
172

    
173
    $form['override'] = array(
174
      '#type' => 'checkbox',
175
      '#title' => t('Override normal sorting if click sorting is used'),
176
      '#default_value' => !empty($this->options['override']),
177
    );
178

    
179
    $form['sticky'] = array(
180
      '#type' => 'checkbox',
181
      '#title' => t('Enable Drupal style "sticky" table headers (Javascript)'),
182
      '#default_value' => !empty($this->options['sticky']),
183
      '#description' => t('(Sticky header effects will not be active for preview below, only on live output.)'),
184
    );
185

    
186
    $form['caption'] = array(
187
      '#type' => 'textfield',
188
      '#title' => t('Short description of table'),
189
      '#description' => t('Include a caption for better accessibility of your table.'),
190
      '#default_value' => $this->options['caption'],
191
      '#maxlength' => 255,
192
    );
193

    
194
    $form['summary'] = array(
195
      '#type' => 'textfield',
196
      '#title' => t('Table summary'),
197
      '#description' => t('This value will be displayed as table-summary attribute in the html. Use this to give a summary of complex tables.'),
198
      '#default_value' => $this->options['summary'],
199
      '#maxlength' => 255,
200
    );
201

    
202
    // Note: views UI registers this theme handler on our behalf. Your module
203
    // will have to register your theme handlers if you do stuff like this.
204
    $form['#theme'] = 'views_ui_style_plugin_table';
205

    
206
    $columns = $this->sanitize_columns($this->options['columns']);
207

    
208
    // Create an array of allowed columns from the data we know.
209
    $field_names = $this->display->handler->get_field_labels();
210

    
211
    if (isset($this->options['default'])) {
212
      $default = $this->options['default'];
213
      if (!isset($columns[$default])) {
214
        $default = -1;
215
      }
216
    }
217
    else {
218
      $default = -1;
219
    }
220

    
221
    foreach ($columns as $field => $column) {
222
      $safe = str_replace(array('][', '_', ' '), '-', $field);
223
      // the $id of the column for dependency checking.
224
      $id = 'edit-style-options-columns-' . $safe;
225

    
226
      $form['columns'][$field] = array(
227
        '#type' => 'select',
228
        '#options' => $field_names,
229
        '#default_value' => $column,
230
      );
231
      if ($handlers[$field]->click_sortable()) {
232
        $form['info'][$field]['sortable'] = array(
233
          '#type' => 'checkbox',
234
          '#default_value' => !empty($this->options['info'][$field]['sortable']),
235
          '#dependency' => array($id => array($field)),
236
        );
237
        $form['info'][$field]['default_sort_order'] = array(
238
          '#type' => 'select',
239
          '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
240
          '#default_value' => !empty($this->options['info'][$field]['default_sort_order']) ? $this->options['info'][$field]['default_sort_order'] : 'asc',
241
          '#dependency_count' => 2,
242
          '#dependency' => array($id => array($field), 'edit-style-options-info-' . $safe . '-sortable' => array(1)),
243
        );
244
        // Provide an ID so we can have such things.
245
        $radio_id = drupal_html_id('edit-default-' . $field);
246
        $form['default'][$field] = array(
247
          '#type' => 'radio',
248
          '#return_value' => $field,
249
          '#parents' => array('style_options', 'default'),
250
          '#id' => $radio_id,
251
          // because 'radio' doesn't fully support '#id' =(
252
          '#attributes' => array('id' => $radio_id),
253
          '#default_value' => $default,
254
          '#dependency' => array($id => array($field)),
255
        );
256
      }
257
      $form['info'][$field]['align'] = array(
258
        '#type' => 'select',
259
        '#default_value' => !empty($this->options['info'][$field]['align']) ? $this->options['info'][$field]['align'] : '',
260
        '#options' => array(
261
          '' => t('None'),
262
          'views-align-left' => t('Left'),
263
          'views-align-center' => t('Center'),
264
          'views-align-right' => t('Right'),
265
          ),
266
        '#dependency' => array($id => array($field)),
267
      );
268
      $form['info'][$field]['separator'] = array(
269
        '#type' => 'textfield',
270
        '#size' => 10,
271
        '#default_value' => isset($this->options['info'][$field]['separator']) ? $this->options['info'][$field]['separator'] : '',
272
        '#dependency' => array($id => array($field)),
273
      );
274
      $form['info'][$field]['empty_column'] = array(
275
        '#type' => 'checkbox',
276
        '#default_value' => isset($this->options['info'][$field]['empty_column']) ? $this->options['info'][$field]['empty_column'] : FALSE,
277
        '#dependency' => array($id => array($field)),
278
      );
279

    
280
      // markup for the field name
281
      $form['info'][$field]['name'] = array(
282
        '#markup' => $field_names[$field],
283
      );
284
    }
285

    
286
    // Provide a radio for no default sort
287
    $form['default'][-1] = array(
288
      '#type' => 'radio',
289
      '#return_value' => -1,
290
      '#parents' => array('style_options', 'default'),
291
      '#id' => 'edit-default-0',
292
      '#default_value' => $default,
293
    );
294

    
295
    $form['empty_table'] = array(
296
      '#type' => 'checkbox',
297
      '#title' => t('Show the empty text in the table'),
298
      '#default_value' => $this->options['empty_table'],
299
      '#description' => t('Per default the table is hidden for an empty view. With this option it is posible to show an empty table with the text in it.'),
300
    );
301

    
302
    $form['description_markup'] = array(
303
      '#markup' => '<div class="description form-item">' . t('Place fields into columns; you may combine multiple fields into the same column. If you do, the separator in the column specified will be used to separate the fields. Check the sortable box to make that column click sortable, and check the default sort radio to determine which column will be sorted by default, if any. You may control column order and field labels in the fields section.') . '</div>',
304
    );
305
  }
306

    
307
  /**
308
   *
309
   */
310
  public function even_empty() {
311
    return parent::even_empty() || !empty($this->options['empty_table']);
312
  }
313
}