Index: rules/modules/taxonomy.rules.inc =================================================================== --- rules/modules/taxonomy.rules.inc (revision 217) +++ rules/modules/taxonomy.rules.inc (working copy) @@ -57,6 +57,31 @@ } /** + * Implementation of hook_rules_condition_info(). + */ +function taxonomy_rules_condition_info() { + $info = array(); + $info['taxonomy_rules_has_term'] = array( + 'label' => t('Content has term assigned'), + 'arguments' => array( + 'node' => array('type' => 'node', 'label' => t('Content')), + ), + 'eval input' => array('term|term_text'), + 'help' => t('Evaluates to true if node has a given taxonomy term or terms.'), + 'module' => 'Taxonomy', + ); + $info['taxonomy_rules_has_vocab'] = array( + 'label' => t('Content has term in vocab assigned'), + 'arguments' => array( + array('type' => 'node', 'label' => t('Content')), + ), + 'help' => t('Evaluates to true if a node has a taxonomy term that is a member of the given vocabulary'), + 'module' => 'Taxonomy', + ); + return $info; +} + +/** * Implementation of hook_rules_action_info(). */ function taxonomy_rules_action_info() { @@ -157,7 +182,35 @@ return $info; } + /** + * Condition: Node has term + */ +function rules_condition_taxonomy_has_term(&$node, $settings) { + if (isset($settings['term']['term_text'])) { + $terms = explode(',',$settings['term']['term_text']); + } + else { + $terms = array($settings['term']['term_select']); + } + + $node_terms = taxonomy_node_get_terms_by_vocabulary($node,$settings['vid']); + + foreach ($node_terms as $term) { + if (in_array($term->tid,$terms)) { + return TRUE; + } + } + return FALSE; +} + +/** + * Condition: Node has term in vocab + */ +function rules_condition_taxonomy_has_vocab(&$node, $settings) { + return (count(taxonomy_node_get_terms_by_vocabulary($node,$settings['vid'])) > 0); +} +/** * Action: Load a term. */ function rules_action_taxonomy_load_term($settings) { Index: rules/modules/user.rules.inc =================================================================== --- rules/modules/user.rules.inc (revision 217) +++ rules/modules/user.rules.inc (working copy) @@ -99,6 +99,7 @@ ), 'help' => t('Whether the user has the selected role(s).'), 'module' => 'User', + 'eval input' => array('advanced|rid','advanced|role_text'), ), ); } @@ -117,22 +118,39 @@ $roles = $settings['roles']; $operation = $settings['operation']; - switch ($operation) { - case 'OR': - foreach ($roles as $rid) { - if (isset($user->roles[$rid])) { - return TRUE; + if (empty($settings['advanced']['rid']) && empty($settings['advanced']['role_text'])) { + switch ($operation) { + case 'OR': + foreach ($roles as $rid) { + if (isset($user->roles[$rid])) { + return TRUE; + } } - } - return FALSE; - - case 'AND': - foreach ($roles as $rid) { - if (!isset($user->roles[$rid])) { - return FALSE; + return FALSE; + + case 'AND': + foreach ($roles as $rid) { + if (!isset($user->roles[$rid])) { + return FALSE; + } } + return TRUE; + } + } + else { + $rids = explode(',',$settings['advanced']['rid']); + foreach ($rids as $rid) { + if (isset($user->roles[$rid])) { + return TRUE; } - return TRUE; + } + $names = explode(',',$settings['advanced']['role_text']); + foreach ($user->roles as $role) { + if (in_array($role->name, $names)) { + return TRUE; + } + } + return FALSE; } } @@ -141,12 +159,21 @@ */ function user_rules_action_info() { return array( + 'rules_action_create_role' => array( + 'label' => t('Create a new role'), + 'arguments' => array( + 'role_name' => array('type' => 'string', 'label' => t('Role name')), + ), + 'module' => 'User', + 'eval input' => array('role_name'), + ), 'rules_action_user_addrole' => array( 'label' => t('Add user role'), 'arguments' => array( 'user' => array('type' => 'user', 'label' => t('User whos roles should be changed')), ), 'module' => 'User', + 'eval input' => array('advanced|rid','advanced|role_text'), ), 'rules_action_user_removerole' => array( 'label' => t('Remove user role'), @@ -154,6 +181,7 @@ 'user' => array('type' => 'user', 'label' => t('User whos roles should be changed')), ), 'module' => 'User', + 'eval input' => array('advanced|rid','advanced|role_text'), ), 'rules_action_load_user' => array( 'label' => t('Load a user account'), @@ -161,6 +189,7 @@ 'user_loaded' => array('type' => 'user', 'label' => t('Loaded user')), ), 'help' => t('Enter an id or a name of the user to load.'), + 'eval input' => array('username','userid'), 'module' => 'User', ), 'rules_action_user_create' => array( @@ -178,6 +207,24 @@ ); } +/* + * Action: Create a role + */ +function rules_action_create_role($role_name, $settings) +{ + require_once(drupal_get_path('module', 'user') . "/user.admin.inc"); + + $form_id = "user_admin_new_role"; + $form_values = array(); + $form_values["name"] = $role_name; + $form_values["op"] = t('Add role'); + $form_state = array(); + $form_state["values"] = $form_values; + + drupal_execute($form_id, $form_state); + //this should return a role, but we need to add that as an argument type first +} + /** * Action user: adds roles to a particular user */ @@ -210,7 +257,6 @@ return array('user' => $user); } } - /** * Loads a user */ Index: rules/modules/taxonomy.rules_forms.inc =================================================================== --- rules/modules/taxonomy.rules_forms.inc (revision 217) +++ rules/modules/taxonomy.rules_forms.inc (working copy) @@ -9,7 +9,71 @@ * @{ */ + /** + * Condition: Node has vocab configuration form + */ +function taxonomy_rules_has_vocab_form($settings, &$form, $form_state) { + $options = _rules_action_taxonomy_get_vocab(); + $form['settings']['vocab_select'] = array( + '#type' => 'select', + '#title' => t('Vocabulary'), + '#options' => $options, + '#description' => !empty($options) ? t('Select the vocabulary.') : t('There are no existing vocabularies, you should add one.', array('@add-url' => url('/admin/content/taxonomy/add/vocabulary'))), + ); +} +/** + * Condition: Node has term configuration form. + * + * Multistep form, based on rules_action_taxonomy_load_term_form. + */ +function taxonomy_rules_has_term_form($settings, &$form, $form_state) { + $settings += array('vocabulary' => 0); + if (empty($settings['vocabulary'])) { + // Get existing taxonomy vocabularies. + $vocab = $options = array(); + $options = _rules_action_taxonomy_get_vocab(); + $form['settings']['vocabulary'] = array( + '#type' => 'select', + '#title' => t('Vocabulary'), + '#options' => $options, + '#description' => !empty($options) ? t('Select the vocabulary.') : t('There are no existing vocabularies, you should add one.', array('@add-url' => url('/admin/content/taxonomy/add/vocabulary'))), + '#required' => TRUE, + '#disabled' => empty($options), + ); + + // Hide some form elements in the first step. + $form['new']['#access'] = FALSE; + $form['input_help']['#access'] = FALSE; + $form['weight']['#access'] = FALSE; + + // Replace the usual submit handlers with our own handler -- this is from load term but we can keep it!. + $form['submit']['#submit'] = array('rules_action_taxonomy_load_term_form_step_submit'); + $form['submit']['#value'] = t('Continue'); + } + else { + $options = array(); + $vocabulary = taxonomy_vocabulary_load($settings['vocabulary']); + $terms = taxonomy_get_tree($vocabulary->vid); + foreach ($terms as $term) { + $options[$term->tid] = check_plain($term->name); + } + $form['settings']['term'] = array( + '#type' => 'fieldset', + '#title' => t("!vocab's terms", array('!vocab' => $vocabulary->name)), + '#description' => empty($options) ? t('There are no terms in the vocabulary. You can manually enter the names or IDs of the term that should be added or removed from the content. ') : t('Select an existing term or manually enter the names or IDs of the term that should be added or removed from the content.'), + ); + $form['settings']['term']['term_select'] = rules_taxonomy_form($vocabulary->vid, !empty($settings['term']['term_select']) ? $settings['term']['term_select'] : 0); + + $form['settings']['term']['term_text'] = array( + '#type' => 'textarea', + '#title' => t('Select by list of term IDs or names.'), + '#default_value' => !empty($settings['term']['tid_text']) ? $settings['term']['tid_text'] : '', + '#description' => t('Optional: enter a comma separated list of values for the term ID. Matching any term will result in the condition returning TRUE.'), + ); + } +} +/** * Action: Load a term configuration form. * * Multistep form. @@ -66,6 +130,21 @@ } /** +* Taxonomy rule condition configuration form. +*/ +function taxonomy_rules_has_tid_form($settings, &$form, &$form_state) { + $settings = array('tid' => ''); + + // this should be more user friendly, allowing vid then tid selection, indeed this is a proof of the concept + $form['settings']['tid'] = array( + '#type' => 'textfield', + '#title' => t('Taxonomy term'), + '#default_value' => $settings['tid'], + '#description' => t('Select the tid(s) of the term(s) (comma separated numerical tid).'), + ); +} + +/** * Own version of taxonomy_form(), which forces the form to be not multiple. */ function rules_taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') { Index: rules/modules/user.rules_forms.inc =================================================================== --- rules/modules/user.rules_forms.inc (revision 216) +++ rules/modules/user.rules_forms.inc (working copy) @@ -64,8 +64,27 @@ '#title' => t('Select role(s)'), '#options' => $roles, '#default_value' => isset($settings['roles']) ? $settings['roles'] : array(), - '#required' => TRUE, ); + $form['advanced'] = array( + '#title' => t('Advanced'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => empty($settings['advanced']['rid']) && empty($settings['advanced']['role_text']), + '#description' => t('Enter text matching a comma separated list of either role names or a role ids (RID).'), + '#weight' => 1, + ); + $form['advanced']['rid'] = array( + '#type' => 'textarea', + '#title' => t('Match role ID'), + '#default_value' => isset($settings['advanced']['rid']) ? $settings['advanced']['rid'] : '', + '#description' => t('Select role ID(s) by matching an evaluated string'), + ); + $form['advanced']['role_text'] = array( + '#type' => 'textarea', + '#title' => t('Match role name'), + '#default_value' => isset($settings['advanced']['role_text']) ? $settings['advanced']['role_text'] : '', + '#description' => t('Select role name(s) by matching an evaluated string.'), + ); return $form; } @@ -86,6 +105,14 @@ '#description' => t('Id of the user to be loaded.'), ); } +function rules_action_create_role_form($settings, &$form) { + $form['settings']['role_name'] = array( + '#type' => 'textfield', + '#title' => t('Role name'), + '#default_value' => isset($settings['role_name']) ? $settings['role_name'] : '', + '#description' => t('Enter the name of the role to be created. This field can use tokens or PHP evaluation (if enabled).'), + ); +} function rules_action_load_user_validate($form, $form_state) { if (!$form_state['values']['settings']['username'] && !$form_state['values']['settings']['userid']) {