views_handler_argument_string.inc

  1. 3.x handlers/views_handler_argument_string.inc
  2. 2.x handlers/views_handler_argument_string.inc

Definition of views_handler_argument_string.

File

handlers/views_handler_argument_string.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_argument_string.
  5. */
  6. /**
  7. * Basic argument handler to implement string arguments that may have length
  8. * limits.
  9. *
  10. * @ingroup views_argument_handlers
  11. */
  12. class views_handler_argument_string extends views_handler_argument {
  13. function init(&$view, &$options) {
  14. parent::init($view, $options);
  15. if (!empty($this->definition['many to one'])) {
  16. $this->helper = new views_many_to_one_helper($this);
  17. // Ensure defaults for these, during summaries and stuff:
  18. $this->operator = 'or';
  19. $this->value = array();
  20. }
  21. }
  22. function option_definition() {
  23. $options = parent::option_definition();
  24. $options['glossary'] = array('default' => FALSE, 'bool' => TRUE);
  25. $options['limit'] = array('default' => 0);
  26. $options['case'] = array('default' => 'none');
  27. $options['path_case'] = array('default' => 'none');
  28. $options['transform_dash'] = array('default' => FALSE, 'bool' => TRUE);
  29. $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
  30. if (!empty($this->definition['many to one'])) {
  31. $options['add_table'] = array('default' => FALSE, 'bool' => TRUE);
  32. $options['require_value'] = array('default' => FALSE, 'bool' => TRUE);
  33. }
  34. return $options;
  35. }
  36. function options_form(&$form, &$form_state) {
  37. parent::options_form($form, $form_state);
  38. $form['glossary'] = array(
  39. '#type' => 'checkbox',
  40. '#title' => t('Glossary mode'),
  41. '#description' => t('Glossary mode applies a limit to the number of characters used in the filter value, which allows the summary view to act as a glossary.'),
  42. '#default_value' => $this->options['glossary'],
  43. '#fieldset' => 'more',
  44. );
  45. $form['limit'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Character limit'),
  48. '#description' => t('How many characters of the filter value to filter against. If set to 1, all fields starting with the first letter in the filter value would be matched.'),
  49. '#default_value' => $this->options['limit'],
  50. '#dependency' => array('edit-options-glossary' => array(TRUE)),
  51. '#fieldset' => 'more',
  52. );
  53. $form['case'] = array(
  54. '#type' => 'select',
  55. '#title' => t('Case'),
  56. '#description' => t('When printing the title and summary, how to transform the case of the filter value.'),
  57. '#options' => array(
  58. 'none' => t('No transform'),
  59. 'upper' => t('Upper case'),
  60. 'lower' => t('Lower case'),
  61. 'ucfirst' => t('Capitalize first letter'),
  62. 'ucwords' => t('Capitalize each word'),
  63. ),
  64. '#default_value' => $this->options['case'],
  65. '#fieldset' => 'more',
  66. );
  67. $form['path_case'] = array(
  68. '#type' => 'select',
  69. '#title' => t('Case in path'),
  70. '#description' => t('When printing url paths, how to transform the case of the filter value. Do not use this unless with Postgres as it uses case sensitive comparisons.'),
  71. '#options' => array(
  72. 'none' => t('No transform'),
  73. 'upper' => t('Upper case'),
  74. 'lower' => t('Lower case'),
  75. 'ucfirst' => t('Capitalize first letter'),
  76. 'ucwords' => t('Capitalize each word'),
  77. ),
  78. '#default_value' => $this->options['path_case'],
  79. '#fieldset' => 'more',
  80. );
  81. $form['transform_dash'] = array(
  82. '#type' => 'checkbox',
  83. '#title' => t('Transform spaces to dashes in URL'),
  84. '#default_value' => $this->options['transform_dash'],
  85. '#fieldset' => 'more',
  86. );
  87. if (!empty($this->definition['many to one'])) {
  88. $form['add_table'] = array(
  89. '#type' => 'checkbox',
  90. '#title' => t('Allow multiple filter values to work together'),
  91. '#description' => t('If selected, multiple instances of this filter can work together, as though multiple values were supplied to the same filter. This setting is not compatible with the "Reduce duplicates" setting.'),
  92. '#default_value' => !empty($this->options['add_table']),
  93. '#fieldset' => 'more',
  94. );
  95. $form['require_value'] = array(
  96. '#type' => 'checkbox',
  97. '#title' => t('Do not display items with no value in summary'),
  98. '#default_value' => !empty($this->options['require_value']),
  99. '#fieldset' => 'more',
  100. );
  101. }
  102. // allow + for or, , for and
  103. $form['break_phrase'] = array(
  104. '#type' => 'checkbox',
  105. '#title' => t('Allow multiple values'),
  106. '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
  107. '#default_value' => !empty($this->options['break_phrase']),
  108. '#fieldset' => 'more',
  109. );
  110. }
  111. /**
  112. * Build the summary query based on a string
  113. */
  114. function summary_query() {
  115. if (empty($this->definition['many to one'])) {
  116. $this->ensure_my_table();
  117. }
  118. else {
  119. $this->table_alias = $this->helper->summary_join();
  120. }
  121. if (empty($this->options['glossary'])) {
  122. // Add the field.
  123. $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
  124. $this->query->set_count_field($this->table_alias, $this->real_field);
  125. }
  126. else {
  127. // Add the field.
  128. $formula = $this->get_formula();
  129. $this->base_alias = $this->query->add_field(NULL, $formula, $this->field . '_truncated');
  130. $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
  131. }
  132. $this->summary_name_field();
  133. return $this->summary_basics(FALSE);
  134. }
  135. /**
  136. * Get the formula for this argument.
  137. *
  138. * $this->ensure_my_table() MUST have been called prior to this.
  139. */
  140. function get_formula() {
  141. return "SUBSTRING($this->table_alias.$this->real_field, 1, " . intval($this->options['limit']) . ")";
  142. }
  143. /**
  144. * Build the query based upon the formula
  145. */
  146. function query($group_by = FALSE) {
  147. $argument = $this->argument;
  148. if (!empty($this->options['transform_dash'])) {
  149. $argument = strtr($argument, '-', ' ');
  150. }
  151. if (!empty($this->options['break_phrase'])) {
  152. views_break_phrase_string($argument, $this);
  153. }
  154. else {
  155. $this->value = array($argument);
  156. $this->operator = 'or';
  157. }
  158. if (!empty($this->definition['many to one'])) {
  159. if (!empty($this->options['glossary'])) {
  160. $this->helper->formula = TRUE;
  161. }
  162. $this->helper->ensure_my_table();
  163. $this->helper->add_filter();
  164. return;
  165. }
  166. $this->ensure_my_table();
  167. $formula = FALSE;
  168. if (empty($this->options['glossary'])) {
  169. $field = "$this->table_alias.$this->real_field";
  170. }
  171. else {
  172. $formula = TRUE;
  173. $field = $this->get_formula();
  174. }
  175. if (count($this->value) > 1) {
  176. $operator = 'IN';
  177. $argument = $this->value;
  178. }
  179. else {
  180. $operator = '=';
  181. }
  182. if ($formula) {
  183. $placeholder = $this->placeholder();
  184. if ($operator == 'IN') {
  185. $field .= " IN($placeholder)";
  186. }
  187. else {
  188. $field .= ' = ' . $placeholder;
  189. }
  190. $placeholders = array(
  191. $placeholder => $argument,
  192. );
  193. $this->query->add_where_expression(0, $field, $placeholders);
  194. }
  195. else {
  196. $this->query->add_where(0, $field, $argument, $operator);
  197. }
  198. }
  199. function summary_argument($data) {
  200. $value = $this->case_transform($data->{$this->base_alias}, $this->options['path_case']);
  201. if (!empty($this->options['transform_dash'])) {
  202. $value = strtr($value, ' ', '-');
  203. }
  204. return $value;
  205. }
  206. function get_sort_name() {
  207. return t('Alphabetical', array(), array('context' => 'Sort order'));
  208. }
  209. function title() {
  210. $this->argument = $this->case_transform($this->argument, $this->options['case']);
  211. if (!empty($this->options['transform_dash'])) {
  212. $this->argument = strtr($this->argument, '-', ' ');
  213. }
  214. if (!empty($this->options['break_phrase'])) {
  215. views_break_phrase_string($this->argument, $this);
  216. }
  217. else {
  218. $this->value = array($this->argument);
  219. $this->operator = 'or';
  220. }
  221. if (empty($this->value)) {
  222. return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
  223. }
  224. if ($this->value === array(-1)) {
  225. return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
  226. }
  227. return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
  228. }
  229. /**
  230. * Override for specific title lookups.
  231. */
  232. function title_query() {
  233. return drupal_map_assoc($this->value, 'check_plain');
  234. }
  235. function summary_name($data) {
  236. return $this->case_transform(parent::summary_name($data), $this->options['case']);
  237. }
  238. }