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