views_plugin_argument_validate_taxonomy_term.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * @file
  4. * Contains the 'taxonomy term' argument validator plugin.
  5. */
  6. /**
  7. * Validate whether an argument is an acceptable node.
  8. */
  9. class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument_validate {
  10. function init(&$view, &$argument, $options) {
  11. parent::init($view, $argument, $options);
  12. // Convert legacy vids option to machine name vocabularies.
  13. if (!empty($this->options['vids'])) {
  14. $vocabularies = taxonomy_get_vocabularies();
  15. foreach ($this->options['vids'] as $vid) {
  16. if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
  17. $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
  18. }
  19. }
  20. }
  21. }
  22. function option_definition() {
  23. $options = parent::option_definition();
  24. $options['vocabularies'] = array('default' => array());
  25. $options['type'] = array('default' => 'tid');
  26. $options['transform'] = array('default' => FALSE, 'bool' => TRUE);
  27. return $options;
  28. }
  29. function options_form(&$form, &$form_state) {
  30. $vocabularies = taxonomy_get_vocabularies();
  31. $options = array();
  32. foreach ($vocabularies as $voc) {
  33. $options[$voc->machine_name] = check_plain($voc->name);
  34. }
  35. $form['vocabularies'] = array(
  36. '#type' => 'checkboxes',
  37. '#prefix' => '<div id="edit-options-validate-argument-vocabulary-wrapper">',
  38. '#suffix' => '</div>',
  39. '#title' => t('Vocabularies'),
  40. '#options' => $options,
  41. '#default_value' => $this->options['vocabularies'],
  42. '#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'),
  43. );
  44. $form['type'] = array(
  45. '#type' => 'select',
  46. '#title' => t('Filter value type'),
  47. '#options' => array(
  48. 'tid' => t('Term ID'),
  49. 'tids' => t('Term IDs separated by , or +'),
  50. 'name' => t('Term name'),
  51. 'convert' => t('Term name converted to Term ID'),
  52. ),
  53. '#default_value' => $this->options['type'],
  54. '#description' => t('Select the form of this filter value; if using term name, it is generally more efficient to convert it to a term ID and use Taxonomy: Term ID rather than Taxonomy: Term Name" as the filter.'),
  55. );
  56. $form['transform'] = array(
  57. '#type' => 'checkbox',
  58. '#title' => t('Transform dashes in URL to spaces in term name filter values'),
  59. '#default_value' => $this->options['transform'],
  60. );
  61. }
  62. function options_submit(&$form, &$form_state, &$options = array()) {
  63. // Filter unselected items so we don't unnecessarily store giant arrays.
  64. $options['vocabularies'] = array_filter($options['vocabularies']);
  65. }
  66. function convert_options(&$options) {
  67. if (!isset($options['vocabularies']) && !empty($this->argument->options['validate_argument_vocabulary'])) {
  68. $options['vocabularies'] = $this->argument->options['validate_argument_vocabulary'];
  69. $options['type'] = $this->argument->options['validate_argument_type'];
  70. $options['transform'] = isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE;
  71. }
  72. }
  73. function validate_argument($argument) {
  74. $vocabularies = array_filter($this->options['vocabularies']);
  75. $type = $this->options['type'];
  76. $transform = $this->options['transform'];
  77. switch ($type) {
  78. case 'tid':
  79. if (!is_numeric($argument)) {
  80. return FALSE;
  81. }
  82. $query = db_select('taxonomy_term_data', 'td');
  83. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  84. $query->fields('td');
  85. $query->fields('tv', array('machine_name'));
  86. $query->condition('td.tid', $argument);
  87. $query->addTag('term_access');
  88. $term = $query->execute()->fetchObject();
  89. if (!$term) {
  90. return FALSE;
  91. }
  92. $this->argument->validated_title = check_plain($term->name);
  93. return empty($vocabularies) || !empty($vocabularies[$term->machine_name]);
  94. case 'tids':
  95. // An empty argument is not a term so doesn't pass.
  96. if (empty($argument)) {
  97. return FALSE;
  98. }
  99. $tids = new stdClass();
  100. $tids->value = $argument;
  101. $tids = views_break_phrase($argument, $tids);
  102. if ($tids->value == array(-1)) {
  103. return FALSE;
  104. }
  105. $test = drupal_map_assoc($tids->value);
  106. $titles = array();
  107. // check, if some tids already verified
  108. static $validated_cache = array();
  109. foreach ($test as $tid) {
  110. if (isset($validated_cache[$tid])) {
  111. if ($validated_cache[$tid] === FALSE) {
  112. return FALSE;
  113. }
  114. else {
  115. $titles[] = $validated_cache[$tid];
  116. unset($test[$tid]);
  117. }
  118. }
  119. }
  120. // if unverified tids left - verify them and cache results
  121. if (count($test)) {
  122. $query = db_select('taxonomy_term_data', 'td');
  123. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  124. $query->fields('td');
  125. $query->fields('tv', array('machine_name'));
  126. $query->condition('td.tid', $test);
  127. $result = $query->execute();
  128. foreach ($result as $term) {
  129. if ($vocabularies && empty($vocabularies[$term->machine_name])) {
  130. $validated_cache[$term->tid] = FALSE;
  131. return FALSE;
  132. }
  133. $titles[] = $validated_cache[$term->tid] = check_plain($term->name);
  134. unset($test[$term->tid]);
  135. }
  136. }
  137. // Remove duplicate titles
  138. $titles = array_unique($titles);
  139. $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);
  140. // If this is not empty, we did not find a tid.
  141. return empty($test);
  142. case 'name':
  143. case 'convert':
  144. $query = db_select('taxonomy_term_data', 'td');
  145. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  146. $query->fields('td');
  147. $query->fields('tv', array('machine_name'));
  148. if (!empty($vocabularies)) {
  149. $query->condition('tv.machine_name', $vocabularies);
  150. }
  151. if ($transform) {
  152. $query->where("replace(td.name, ' ', '-') = :name", array(':name' => $argument));
  153. }
  154. else {
  155. $query->condition('td.name', $argument);
  156. }
  157. $term = $query->execute()->fetchObject();
  158. if ($term && (empty($vocabularies) || !empty($vocabularies[$term->machine_name]))) {
  159. if ($type == 'convert') {
  160. $this->argument->argument = $term->tid;
  161. }
  162. $this->argument->validated_title = check_plain($term->name);
  163. return TRUE;
  164. }
  165. return FALSE;
  166. }
  167. }
  168. function process_summary_arguments(&$args) {
  169. $type = $this->options['type'];
  170. $transform = $this->options['transform'];
  171. $vocabularies = array_filter($this->options['vocabularies']);
  172. if ($type == 'convert') {
  173. $arg_keys = array_flip($args);
  174. $query = db_select('taxonomy_term_data', 'td');
  175. $query->condition('tid', $args);
  176. $query->addField('td', 'tid', 'tid');
  177. if (!empty($vocabularies)) {
  178. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  179. $query->condition('tv.machine_name', $vocabularies);
  180. }
  181. if ($transform) {
  182. $query->addExpression("REPLACE(td.name, ' ', '-')", 'name');
  183. }
  184. else {
  185. $query->addField('td', 'name', 'name');
  186. }
  187. foreach ($query->execute()->fetchAllKeyed() as $tid => $term) {
  188. $args[$arg_keys[$tid]] = $term;
  189. }
  190. }
  191. }
  192. }