views_plugin_style_table.inc 11 KB

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