views_plugin_argument_validate_taxonomy_term.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. $term = taxonomy_term_load($term->tid);
  93. $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
  94. return empty($vocabularies) || !empty($vocabularies[$term->machine_name]);
  95. case 'tids':
  96. // An empty argument is not a term so doesn't pass.
  97. if (empty($argument)) {
  98. return FALSE;
  99. }
  100. $tids = new stdClass();
  101. $tids->value = $argument;
  102. $tids = views_break_phrase($argument, $tids);
  103. if ($tids->value == array(-1)) {
  104. return FALSE;
  105. }
  106. $test = drupal_map_assoc($tids->value);
  107. $titles = array();
  108. // check, if some tids already verified
  109. static $validated_cache = array();
  110. foreach ($test as $tid) {
  111. if (isset($validated_cache[$tid])) {
  112. if ($validated_cache[$tid] === FALSE) {
  113. return FALSE;
  114. }
  115. else {
  116. $titles[] = $validated_cache[$tid];
  117. unset($test[$tid]);
  118. }
  119. }
  120. }
  121. // if unverified tids left - verify them and cache results
  122. if (count($test)) {
  123. $query = db_select('taxonomy_term_data', 'td');
  124. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  125. $query->fields('td');
  126. $query->fields('tv', array('machine_name'));
  127. $query->condition('td.tid', $test);
  128. $result = $query->execute();
  129. foreach ($result as $term) {
  130. if ($vocabularies && empty($vocabularies[$term->machine_name])) {
  131. $validated_cache[$term->tid] = FALSE;
  132. return FALSE;
  133. }
  134. $term = taxonomy_term_load($term->tid);
  135. $titles[] = $validated_cache[$term->tid] = check_plain(entity_label('taxonomy_term', $term));
  136. unset($test[$term->tid]);
  137. }
  138. }
  139. // Remove duplicate titles
  140. $titles = array_unique($titles);
  141. $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);
  142. // If this is not empty, we did not find a tid.
  143. return empty($test);
  144. case 'name':
  145. case 'convert':
  146. $query = db_select('taxonomy_term_data', 'td');
  147. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  148. $query->fields('td');
  149. $query->fields('tv', array('machine_name'));
  150. if (!empty($vocabularies)) {
  151. $query->condition('tv.machine_name', $vocabularies);
  152. }
  153. if ($transform) {
  154. $query->where("replace(td.name, ' ', '-') = :name", array(':name' => $argument));
  155. }
  156. else {
  157. $query->condition('td.name', $argument);
  158. }
  159. $term = $query->execute()->fetchObject();
  160. if ($term && (empty($vocabularies) || !empty($vocabularies[$term->machine_name]))) {
  161. if ($type == 'convert') {
  162. $this->argument->argument = $term->tid;
  163. }
  164. $term = taxonomy_term_load($term->tid);
  165. $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
  166. return TRUE;
  167. }
  168. return FALSE;
  169. }
  170. }
  171. function process_summary_arguments(&$args) {
  172. $type = $this->options['type'];
  173. $transform = $this->options['transform'];
  174. $vocabularies = array_filter($this->options['vocabularies']);
  175. if ($type == 'convert') {
  176. $arg_keys = array_flip($args);
  177. $query = db_select('taxonomy_term_data', 'td');
  178. $query->condition('tid', $args);
  179. $query->addField('td', 'tid', 'tid');
  180. if (!empty($vocabularies)) {
  181. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  182. $query->condition('tv.machine_name', $vocabularies);
  183. }
  184. if ($transform) {
  185. $query->addExpression("REPLACE(td.name, ' ', '-')", 'name');
  186. }
  187. else {
  188. $query->addField('td', 'name', 'name');
  189. }
  190. foreach ($query->execute()->fetchAllKeyed() as $tid => $term) {
  191. $args[$arg_keys[$tid]] = $term;
  192. }
  193. }
  194. }
  195. }