PageRenderTime 28ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/views/handlers/views_handler_argument_string.inc

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