views_plugin_style_table.inc

  1. 3.x plugins/views_plugin_style_table.inc
  2. 2.x plugins/views_plugin_style_table.inc

Contains the table style plugin.

File

plugins/views_plugin_style_table.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the table style plugin.
  5. */
  6. /**
  7. * Style plugin to render each item as a row in a table.
  8. *
  9. * @ingroup views_style_plugins
  10. */
  11. class views_plugin_style_table extends views_plugin_style {
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['columns'] = array('default' => array());
  15. $options['default'] = array('default' => '');
  16. $options['info'] = array('default' => array());
  17. $options['override'] = array('default' => TRUE);
  18. $options['sticky'] = array('default' => FALSE);
  19. $options['order'] = array('default' => 'asc');
  20. $options['summary'] = array('default' => '');
  21. return $options;
  22. }
  23. /**
  24. * Determine if we should provide sorting based upon $_GET inputs.
  25. */
  26. function build_sort() {
  27. if (!isset($_GET['order']) && ($this->options['default'] == -1 || empty($this->view->field[$this->options['default']]))) {
  28. return TRUE;
  29. }
  30. // If a sort we don't know anything about gets through, exit gracefully.
  31. if (isset($_GET['order']) && empty($this->view->field[$_GET['order']])) {
  32. return TRUE;
  33. }
  34. // Let the builder know whether or not we're overriding the default sorts.
  35. return empty($this->options['override']);
  36. }
  37. /**
  38. * Add our actual sort criteria
  39. */
  40. function build_sort_post() {
  41. if (!isset($_GET['order'])) {
  42. // check for a 'default' clicksort. If there isn't one, exit gracefully.
  43. if (empty($this->options['default'])) {
  44. return;
  45. }
  46. $sort = $this->options['default'];
  47. $this->order = !empty($this->options['order']) ? $this->options['order'] : 'asc';
  48. }
  49. else {
  50. $sort = $_GET['order'];
  51. // Store the $order for later use.
  52. $this->order = !empty($_GET['sort']) ? strtolower($_GET['sort']) : 'asc';
  53. }
  54. // If a sort we don't know anything about gets through, exit gracefully.
  55. if (empty($this->view->field[$sort])) {
  56. return;
  57. }
  58. // Ensure $this->order is valid.
  59. if ($this->order != 'asc' && $this->order != 'desc') {
  60. $this->order = 'asc';
  61. }
  62. // Store the $sort for later use.
  63. $this->active = $sort;
  64. // Tell the field to click sort.
  65. $this->view->field[$sort]->click_sort($this->order);
  66. }
  67. /**
  68. * Normalize a list of columns based upon the fields that are
  69. * available. This compares the fields stored in the style handler
  70. * to the list of fields actually in the view, removing fields that
  71. * have been removed and adding new fields in their own column.
  72. *
  73. * - Each field must be in a column.
  74. * - Each column must be based upon a field, and that field
  75. * is somewhere in the column.
  76. * - Any fields not currently represented must be added.
  77. * - Columns must be re-ordered to match the fields.
  78. *
  79. * @param $columns
  80. * An array of all fields; the key is the id of the field and the
  81. * value is the id of the column the field should be in.
  82. * @param $fields
  83. * The fields to use for the columns. If not provided, they will
  84. * be requested from the current display. The running render should
  85. * send the fields through, as they may be different than what the
  86. * display has listed due to access control or other changes.
  87. */
  88. function sanitize_columns($columns, $fields = NULL) {
  89. $sanitized = array();
  90. if ($fields === NULL) {
  91. $fields = $this->display->handler->get_option('fields');
  92. }
  93. // Preconfigure the sanitized array so that the order is retained.
  94. foreach ($fields as $field => $info) {
  95. // Set to itself so that if it isn't touched, it gets column
  96. // status automatically.
  97. $sanitized[$field] = $field;
  98. }
  99. foreach ($columns as $field => $column) {
  100. // first, make sure the field still exists.
  101. if (!isset($sanitized[$field])) {
  102. continue;
  103. }
  104. // If the field is the column, mark it so, or the column
  105. // it's set to is a column, that's ok
  106. if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) {
  107. $sanitized[$field] = $column;
  108. }
  109. // Since we set the field to itself initially, ignoring
  110. // the condition is ok; the field will get its column
  111. // status back.
  112. }
  113. return $sanitized;
  114. }
  115. /**
  116. * Render the given style.
  117. */
  118. function options_form(&$form, &$form_state) {
  119. parent::options_form($form, $form_state);
  120. $handlers = $this->display->handler->get_handlers('field');
  121. if (empty($handlers)) {
  122. $form['error_markup'] = array(
  123. '#value' => t('You need at least one field before you can configure your table settings'),
  124. '#prefix' => '<div class="error form-item description">',
  125. '#suffix' => '</div>',
  126. );
  127. return;
  128. }
  129. $form['override'] = array(
  130. '#type' => 'checkbox',
  131. '#title' => t('Override normal sorting if click sorting is used'),
  132. '#default_value' => !empty($this->options['override']),
  133. );
  134. $form['sticky'] = array(
  135. '#type' => 'checkbox',
  136. '#title' => t('Enable Drupal style "sticky" table headers (Javascript)'),
  137. '#default_value' => !empty($this->options['sticky']),
  138. '#description' => t('(Sticky header effects will not be active for preview below, only on live output.)'),
  139. );
  140. $form['order'] = array(
  141. '#type' => 'select',
  142. '#title' => t('Default sort order'),
  143. '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
  144. '#default_value' => $this->options['order'],
  145. '#description' => t('If a default sort order is selected, what order should it use by default.'),
  146. );
  147. $form['summary'] = array(
  148. '#type' => 'textfield',
  149. '#title' => t('Table summary'),
  150. '#description' => t('This value will be displayed as table-summary attribute in the html. Set this for better accessiblity of your site.'),
  151. '#default_value' => $this->options['summary'],
  152. );
  153. // Note: views UI registers this theme handler on our behalf. Your module
  154. // will have to register your theme handlers if you do stuff like this.
  155. $form['#theme'] = 'views_ui_style_plugin_table';
  156. $columns = $this->sanitize_columns($this->options['columns']);
  157. // Create an array of allowed columns from the data we know:
  158. $field_names = $this->display->handler->get_field_labels();
  159. if (isset($this->options['default'])) {
  160. $default = $this->options['default'];
  161. if (!isset($columns[$default])) {
  162. $default = -1;
  163. }
  164. }
  165. else {
  166. $default = -1;
  167. }
  168. foreach ($columns as $field => $column) {
  169. $safe = str_replace(array('][', '_', ' '), '-', $field);
  170. // the $id of the column for dependency checking.
  171. $id = 'edit-style-options-columns-' . $safe;
  172. $form['columns'][$field] = array(
  173. '#type' => 'select',
  174. '#options' => $field_names,
  175. '#default_value' => $column,
  176. );
  177. if ($handlers[$field]->click_sortable()) {
  178. $form['info'][$field]['sortable'] = array(
  179. '#type' => 'checkbox',
  180. '#default_value' => !empty($this->options['info'][$field]['sortable']),
  181. '#process' => array('views_process_dependency'),
  182. '#dependency' => array($id => array($field)),
  183. );
  184. // Provide an ID so we can have such things.
  185. $radio_id = form_clean_id('edit-default-' . $field);
  186. $form['default'][$field] = array(
  187. '#type' => 'radio',
  188. '#return_value' => $field,
  189. '#parents' => array('style_options', 'default'),
  190. '#id' => $radio_id,
  191. // because 'radio' doesn't fully support '#id' =(
  192. '#attributes' => array('id' => $radio_id),
  193. '#default_value' => $default,
  194. '#process' => array('views_process_dependency'),
  195. '#dependency' => array($id => array($field)),
  196. );
  197. }
  198. $form['info'][$field]['separator'] = array(
  199. '#type' => 'textfield',
  200. '#size' => 10,
  201. '#default_value' => isset($this->options['info'][$field]['separator']) ? $this->options['info'][$field]['separator'] : '',
  202. '#process' => array('views_process_dependency'),
  203. '#dependency' => array($id => array($field)),
  204. );
  205. // markup for the field name
  206. $form['info'][$field]['name'] = array(
  207. '#value' => $field_names[$field],
  208. );
  209. }
  210. // Provide a radio for no default sort
  211. $form['default'][-1] = array(
  212. '#type' => 'radio',
  213. '#return_value' => -1,
  214. '#parents' => array('style_options', 'default'),
  215. '#id' => 'edit-default-0',
  216. '#default_value' => $default,
  217. );
  218. $form['description_markup'] = array(
  219. '#prefix' => '<div class="description form-item">',
  220. '#suffix' => '</div>',
  221. '#value' => 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.'),
  222. );
  223. }
  224. }