views_handler_argument_string.inc

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

File

handlers/views_handler_argument_string.inc
View source
  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['ignorecase'] = array('default' => FALSE);
  22. $options['limit'] = array('default' => 0);
  23. $options['case'] = array('default' => 'none');
  24. $options['path_case'] = array('default' => 'none');
  25. $options['transform_dash'] = 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 argument, which allows the summary view to act as a glossary.'),
  38. '#default_value' => $this->options['glossary'],
  39. );
  40. $form['ignorecase'] = array(
  41. '#type' => 'checkbox',
  42. '#title' => t('Ignore case'),
  43. '#description' => t('Ignore case allows for doing database searches without case sensitivity. MySQL already works in lower-case mode, so MySQL users should leave this unchecked to improve performance.'),
  44. '#default_value' => $this->options['ignorecase'],
  45. );
  46. $form['limit'] = array(
  47. '#type' => 'textfield',
  48. '#title' => t('Character limit'),
  49. '#description' => t('How many characters of the argument to filter against. If set to 1, all fields starting with the letter in the argument would be matched.'),
  50. '#default_value' => $this->options['limit'],
  51. '#process' => array('views_process_dependency'),
  52. '#dependency' => array('edit-options-glossary' => array(TRUE)),
  53. );
  54. $form['case'] = array(
  55. '#type' => 'select',
  56. '#title' => t('Case'),
  57. '#description' => t('When printing the argument result, how to transform the case.'),
  58. '#options' => array(
  59. 'none' => t('No transform'),
  60. 'upper' => t('Upper case'),
  61. 'lower' => t('Lower case'),
  62. 'ucfirst' => t('Capitalize first letter'),
  63. 'ucwords' => t('Capitalize each word'),
  64. ),
  65. '#default_value' => $this->options['case'],
  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 argument. 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. );
  80. $form['transform_dash'] = array(
  81. '#type' => 'checkbox',
  82. '#title' => t('Transform spaces to dashes in URL'),
  83. '#default_value' => $this->options['transform_dash'],
  84. );
  85. if (!empty($this->definition['many to one'])) {
  86. $form['add_table'] = array(
  87. '#type' => 'checkbox',
  88. '#title' => t('Allow multiple arguments to work together.'),
  89. '#description' => t('If selected, multiple instances of this argument can work together, as though multiple terms were supplied to the same argument. This setting is not compatible with the "Reduce duplicates" setting.'),
  90. '#default_value' => !empty($this->options['add_table']),
  91. );
  92. $form['require_value'] = array(
  93. '#type' => 'checkbox',
  94. '#title' => t('Do not display items with no value in summary'),
  95. '#default_value' => !empty($this->options['require_value']),
  96. );
  97. }
  98. }
  99. /**
  100. * Build the summary query based on a string
  101. */
  102. function summary_query() {
  103. if (empty($this->definition['many to one'])) {
  104. $this->ensure_my_table();
  105. }
  106. else {
  107. $this->table_alias = $this->helper->summary_join();
  108. }
  109. if (empty($this->options['glossary'])) {
  110. // Add the field.
  111. if (empty($this->options['ignorecase'])){
  112. $this->base_alias = $this->name_alias = $this->query->add_field($this->table_alias, $this->real_field);
  113. $this->query->set_count_field($this->table_alias, $this->real_field);
  114. }
  115. else {
  116. $this->base_alias = $this->name_alias = $this->query->add_field($this->table_alias, 'LOWER(' . $this->real_field . ')');
  117. $this->query->set_count_field($this->table_alias, 'LOWER(' . $this->real_field . ')');
  118. }
  119. }
  120. else {
  121. // Add the field.
  122. $formula = $this->get_formula();
  123. if (empty($this->options['ignorecase'])){
  124. $this->base_alias = $this->name_alias = $this->query->add_field(NULL, $formula, $this->field . '_truncated');
  125. $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
  126. }
  127. else {
  128. $this->base_alias = $this->name_alias = $this->query->add_field(NULL, 'LOWER(' . $formula . ')', $this->field . '_truncated');
  129. $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
  130. }
  131. }
  132. return $this->summary_basics(FALSE);
  133. }
  134. /**
  135. * Get the formula for this argument.
  136. *
  137. * $this->ensure_my_table() MUST have been called prior to this.
  138. */
  139. function get_formula() {
  140. return "SUBSTR($this->table_alias.$this->real_field, 1, " . intval($this->options['limit']) . ")";
  141. }
  142. /**
  143. * Build the query based upon the formula
  144. */
  145. function query() {
  146. $argument = $this->argument;
  147. if (!empty($this->options['transform_dash'])) {
  148. $argument = strtr($argument, '-', ' ');
  149. }
  150. if (!empty($this->definition['many to one'])) {
  151. if (!empty($this->options['glossary'])) {
  152. $this->helper->formula = TRUE;
  153. }
  154. $this->value = array($argument);
  155. $this->helper->ensure_my_table();
  156. $this->helper->add_filter();
  157. return;
  158. }
  159. $this->ensure_my_table();
  160. if (empty($this->options['glossary'])) {
  161. $field = "$this->table_alias.$this->real_field";
  162. }
  163. else {
  164. $field = $this->get_formula();
  165. }
  166. if (empty($this->options['ignorecase'])){
  167. $this->query->add_where(0, "$field = '%s'", $argument);
  168. }
  169. else {
  170. $this->query->add_where(0, "LOWER($field) = LOWER('%s')", $argument);
  171. }
  172. }
  173. function summary_argument($data) {
  174. $value = $this->case_transform($data->{$this->base_alias}, 'path_case');
  175. if (!empty($this->options['transform_dash'])) {
  176. $value = strtr($value, ' ', '-');
  177. }
  178. return $value;
  179. }
  180. function case_transform($string, $option) {
  181. global $multibyte;
  182. switch ($this->options[$option]) {
  183. default:
  184. return $string;
  185. case 'upper':
  186. return drupal_strtoupper($string);
  187. case 'lower':
  188. return drupal_strtolower($string);
  189. case 'ucfirst':
  190. return drupal_strtoupper(drupal_substr($string, 0, 1)) . drupal_substr($string, 1);
  191. case 'ucwords':
  192. if ($multibyte == UNICODE_MULTIBYTE) {
  193. return mb_convert_case($string, MB_CASE_TITLE);
  194. } else {
  195. return ucwords($string);
  196. }
  197. }
  198. }
  199. function title() {
  200. $title = $this->case_transform($this->argument, 'case');
  201. if (!empty($this->options['transform_dash'])) {
  202. $title = strtr($title, '-', ' ');
  203. }
  204. return check_plain($title);
  205. }
  206. function summary_name($data) {
  207. return $this->case_transform(parent::summary_name($data), 'case');
  208. }
  209. }