views_handler_argument_string.inc 9.2 KB

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