views_plugin_argument_validate_taxonomy_term.inc

  1. 3.x modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc
  2. 2.x modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc

Contains the 'taxonomy term' argument validator plugin.

File

modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc
View source
  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 validate_form(&$form, &$form_state) {
  11. $vocabularies = taxonomy_get_vocabularies();
  12. $options = array();
  13. foreach ($vocabularies as $voc) {
  14. $options[$voc->vid] = check_plain($voc->name);
  15. }
  16. $form['validate_argument_vocabulary'] = array(
  17. '#type' => 'checkboxes',
  18. '#prefix' => '<div id="edit-options-validate-argument-vocabulary-wrapper">',
  19. '#suffix' => '</div>',
  20. '#title' => t('Vocabularies'),
  21. '#options' => $options,
  22. '#default_value' => isset($this->argument->options['validate_argument_vocabulary']) ? $this->argument->options['validate_argument_vocabulary'] : array(),
  23. '#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'),
  24. '#process' => array('expand_checkboxes', 'views_process_dependency'),
  25. '#dependency' => array('edit-options-validate-type' => array($this->id)),
  26. );
  27. $form['validate_argument_type'] = array(
  28. '#type' => 'select',
  29. '#title' => t('Argument type'),
  30. '#options' => array(
  31. 'tid' => t('Term ID'),
  32. 'tids' => t('Term IDs separated by , or +'),
  33. 'name' => t('Term name or synonym'),
  34. 'convert' => t('Term name/synonym converted to Term ID'),
  35. ),
  36. '#default_value' => isset($this->argument->options['validate_argument_type']) ? $this->argument->options['validate_argument_type'] : 'tid',
  37. '#description' => t('Select the form of this argument; 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 an argument.'),
  38. '#process' => array('views_process_dependency'),
  39. '#dependency' => array('edit-options-validate-type' => array($this->id)),
  40. );
  41. $form['validate_argument_transform'] = array(
  42. '#type' => 'checkbox',
  43. '#title' => t('Transform dashes in URL to spaces in term name arguments'),
  44. '#default_value' => isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE,
  45. '#process' => array('views_process_dependency'),
  46. '#dependency' => array('edit-options-validate-argument-type' => array('convert', 'name')),
  47. );
  48. }
  49. function validate_argument($argument) {
  50. $vids = isset($this->argument->options['validate_argument_vocabulary']) ? array_filter($this->argument->options['validate_argument_vocabulary']) : array();
  51. $type = isset($this->argument->options['validate_argument_type']) ? $this->argument->options['validate_argument_type'] : 'tid';
  52. $transform = isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE;
  53. switch ($type) {
  54. case 'tid':
  55. if (!is_numeric($argument)) {
  56. return FALSE;
  57. }
  58. $result = db_fetch_object(db_query("SELECT * FROM {term_data} WHERE tid = %d", $argument));
  59. if (!$result) {
  60. return FALSE;
  61. }
  62. return empty($vids) || !empty($vids[$result->vid]);
  63. case 'tids':
  64. // An empty argument is not a term so doesn't pass.
  65. if (empty($argument)) {
  66. return FALSE;
  67. }
  68. $tids = new stdClass();
  69. $tids->value = $argument;
  70. $tids = views_break_phrase($argument, $tids);
  71. if ($tids->value == array(-1)) {
  72. return FALSE;
  73. }
  74. $test = drupal_map_assoc($tids->value);
  75. $titles = array();
  76. // check, if some tids already verified
  77. static $validated_cache = array();
  78. foreach ($test as $tid) {
  79. if (isset($validated_cache[$tid])) {
  80. if ($validated_cache[$tid] === FALSE) {
  81. return FALSE;
  82. }
  83. else {
  84. $titles[] = $validated_cache[$tid];
  85. unset($test[$tid]);
  86. }
  87. }
  88. }
  89. // if unverified tids left - verify them and cache results
  90. if (count($test)) {
  91. $placeholders = implode(', ', array_fill(0, count($test), '%d'));
  92. $result = db_query("SELECT * FROM {term_data} WHERE tid IN ($placeholders)", $test);
  93. while ($term = db_fetch_object($result)) {
  94. if ($vids && empty($vids[$term->vid])) {
  95. $validated_cache[$term->tid] = FALSE;
  96. return FALSE;
  97. }
  98. $titles[] = $validated_cache[$term->tid] = check_plain($term->name);
  99. unset($test[$term->tid]);
  100. }
  101. }
  102. // Remove duplicate titles
  103. $titles = array_unique($titles);
  104. $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);
  105. // If this is not empty, we did not find a tid.
  106. return empty($test);
  107. case 'name':
  108. case 'convert':
  109. $and = '';
  110. if (!empty($vids)) {
  111. $and = " AND td.vid IN(" . implode(', ', $vids) . ')';
  112. }
  113. if ($transform) {
  114. $result = db_fetch_object(db_query("SELECT td.* FROM {term_data} td LEFT JOIN {term_synonym} ts ON ts.tid = td.tid WHERE (replace(td.name, ' ', '-') = '%s' OR replace(ts.name, ' ', '-') = '%s')$and", $argument, $argument));
  115. }
  116. else {
  117. $result = db_fetch_object(db_query("SELECT td.* FROM {term_data} td LEFT JOIN {term_synonym} ts ON ts.tid = td.tid WHERE (td.name = '%s' OR ts.name = '%s')$and", $argument, $argument));
  118. }
  119. if (!$result) {
  120. return FALSE;
  121. }
  122. if ($type == 'convert') {
  123. $this->argument->argument = $result->tid;
  124. }
  125. $this->argument->validated_title = check_plain($result->name);
  126. return TRUE;
  127. }
  128. }
  129. }