diff --git a/rules/modules/taxonomy.rules.inc b/rules/modules/taxonomy.rules.inc index 4c2dd28..878206b 100644 --- a/rules/modules/taxonomy.rules.inc +++ b/rules/modules/taxonomy.rules.inc @@ -276,5 +276,65 @@ class rules_data_type_taxonomy_vocab extends rules_data_type { } /** + * Implementation of hook_rules_condition_info(). + */ +function taxonomy_rules_condition_info() { + return array( + 'rules_condition_content_has_term' => array( + 'label' => t('Content has term'), + 'help' => t('Evaluates to TRUE if the given content has one of the selected terms.'), + 'module' => 'Taxonomy', + 'arguments' => array( + 'node' => array('type' => 'node', 'label' => t('Content')), + ), + ), + ); +} + +/** + * Condition: Check for selected terms. + */ + +function rules_condition_content_has_term(&$node, $settings) { + + $taxonomy = $node->taxonomy; + + // If vocab is marked as tag, we format it to the proper format. + if (isset($taxonomy['tags']) && count($taxonomy['tags']) > 0 ) { + foreach ($taxonomy['tags'] as $vid => $term) { + $terms_names = explode(', ', $term); + foreach ($terms_names as $term_name) { + // It can return multiple terms with the same name. + $terms_objects = taxonomy_get_term_by_name($term_name); + foreach ($terms_objects as $term_object) { + $tid = $term_object->tid; + $taxonomy[$vid][$tid] = $tid; + } + } + } + // Since we won't use it unset to not bother us. + unset($taxonomy['tags']); + } + + + if (isset($taxonomy) && (count($taxonomy) > 0)) { + $tids = array(); + foreach ($taxonomy as $vocab) { + foreach ($vocab as $term) { + $tid = is_object($term) ? $term->tid : (is_array($term) ? reset($term) : $term); + $tids[$tid] = $tid; + } + } + + foreach ($settings['tids'] as $tid) { + if (isset($tids[$tid])) { + return TRUE; + } + } + } + return FALSE; +} + +/** * @} */ diff --git a/rules/modules/taxonomy.rules_forms.inc b/rules/modules/taxonomy.rules_forms.inc index 8435ed9..065cd25 100644 --- a/rules/modules/taxonomy.rules_forms.inc +++ b/rules/modules/taxonomy.rules_forms.inc @@ -187,5 +187,27 @@ function _rules_action_taxonomy_get_vocab() { } /** + * Condition: Check for terms; Configuration form. + */ +function rules_condition_content_has_term_form($settings, &$form) { + $vocabularies = taxonomy_get_vocabularies(); + $options = array(); + foreach ($vocabularies as $vocabulary) { + $terms = taxonomy_get_tree($vocabulary->vid); + foreach($terms as $term) { + $options[check_plain($vocabulary->name)][$term->tid] = check_plain($term->name); + } + } + $form['settings']['tids'] = array( + '#type' => 'select', + '#title' => t('Taxonomy terms'), + '#options' => $options, + '#multiple' => TRUE, + '#default_value' => isset($settings['tids']) ? $settings['tids'] : array(), + '#required' => TRUE, + ); +} + +/** * @} */