views_handler_argument_string.inc 8.8 KB

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