diff --git a/modules/system.eval.inc b/modules/system.eval.inc index a27225f..4ec5a69 100644 --- a/modules/system.eval.inc +++ b/modules/system.eval.inc @@ -98,9 +98,11 @@ function rules_action_mail($to, $subject, $message, $from = NULL, $langcode, $se /** * Action: Send mail to all users of a specific role group(s). */ -function rules_action_mail_to_users_of_role($roles, $subject, $message, $from = NULL, $settings, RulesState $state, RulesPlugin $element) { +function rules_action_mail_to_users_of_role($role_names, $subject, $message, $from = NULL, $settings, RulesState $state, RulesPlugin $element) { $from = !empty($from) ? str_replace(array("\r", "\n"), '', $from) : NULL; + $roles = rules_user_role_name_to_id($role_names); + // All authenticated users, which is everybody. if (in_array(DRUPAL_AUTHENTICATED_RID, $roles)) { $result = db_query('SELECT mail FROM {users} WHERE uid > 0'); diff --git a/modules/system.rules.inc b/modules/system.rules.inc index fcd4f80..05c85e3 100644 --- a/modules/system.rules.inc +++ b/modules/system.rules.inc @@ -219,7 +219,7 @@ function rules_system_action_info() { 'roles' => array( 'type' => 'list', 'label' => t('Roles'), - 'options list' => 'entity_metadata_user_roles', + 'options list' => 'rules_entity_metadata_user_roles', 'description' => t('Select the roles whose users should receive the mail.'), ), 'subject' => array( diff --git a/rules.module b/rules.module index d2ff900..2b34969 100644 --- a/rules.module +++ b/rules.module @@ -1634,3 +1634,46 @@ function rules_entity_metadata_wrapper_all_properties_callback(EntityMetadataWra $property_info['properties'] += $properties; return $property_info; } + +/** + * Implements hook_form_alter(&$form, &$form_state, $form_id). + */ +function rules_form_alter(&$form, &$form_state, $form_id) { + if ($form_id == 'user_admin_role') { + drupal_set_message(t('Please make sure to review the rules with action "Send mail to all users of a role", you will need to re-configure the action if the this role was selected.'), 'warning'); + } +} + +/** + * Helper function. + * return an array of user roles name, with key value are both same as role name. + * @see entity_metadata_user_roles(). + */ +function rules_entity_metadata_user_roles() { + $roles = user_roles(); + unset($roles[DRUPAL_AUTHENTICATED_RID], $roles[DRUPAL_ANONYMOUS_RID]); + + $role_names = array(); + foreach ($roles as $key => $value) { + $role_names[$value] = $value; + } + + return $role_names; +} + +/** + * Helper function. + * change the return of rules_entity_metadata_user_roles() to an array of role ids. + * @see rules_entity_metadata_user_roles() + */ +function rules_user_role_name_to_id($role_names) { + $roles = user_roles(); + $roles = array_flip($roles); + + $role_ids = array(); + foreach ($role_names as $role_name) { + $role_ids[] = $roles[$role_name]; + } + + return $role_ids; +} diff --git a/rules_admin/rules_admin.inc b/rules_admin/rules_admin.inc index e8e9e28..98ac0b2 100644 --- a/rules_admin/rules_admin.inc +++ b/rules_admin/rules_admin.inc @@ -434,3 +434,68 @@ function rules_admin_add_component_submit($form, &$form_state) { drupal_set_message(t('Your changes have been saved.')); $form_state['redirect'] = RulesPluginUI::path($rules_config->name); } + +/** + * Rules update form. + */ +function rules_admin_update($form, &$form_state) { + $form['send_mail_to_role'] = array( + '#type' => 'submit', + '#value' => t("Update action: Send mail to all users of a role Rules"), + '#submit' => array('rules_admin_update_send_mail_to_role'), + ); + + return $form; +} + +/** + * Form submit. + */ +function rules_admin_update_send_mail_to_role($form, &$form_state) { + // Updated rules + $update_send_mail_to_role = variable_get('update_send_mail_to_role', array(0)); + + $query = db_select('rules_config', 'r'); + $query->fields('r'); + $query->condition('r.data', '%"mail_to_users_of_role"%', 'like'); + $query->condition('r.id', $update_send_mail_to_role, 'NOT IN'); + + $result = $query->execute(); + foreach($result as $row) { + /////////////////////// RulesReactionRule + $rules_config= unserialize($row->data); + + /////////////////////// RulesRecursiveElementIterator + $actions = $rules_config->actions(); + $actions_r = $actions->getArrayCopy(); + + $roles = user_roles(); + + /////////////////////// RulesAction + foreach ($actions_r as $action) { + if ($action->label() == 'Send mail to all users of a role') { + $new_roles = array(); + foreach ($action->settings['roles'] as $rid) { + // Check if $rid is real or already role name + if (!empty($roles[$rid])) { + $new_roles[$roles[$rid]] = $roles[$rid]; + } + } + + if (!empty($new_roles)) { + $action->settings['roles'] = $new_roles; + $action->save(); + } + } + } + + drupal_set_message(t('Updated rules: %rule', array('%rule' => $row->label . '(' . $row->id . ')'))); + $update_send_mail_to_role[] = $row->id; + } + + if ($update_send_mail_to_role == variable_get('update_send_mail_to_role', array(0))) { + drupal_set_message(t('No rules updated.')); + } + + variable_set('update_send_mail_to_role', $update_send_mail_to_role); +} diff --git a/rules_admin/rules_admin.module b/rules_admin/rules_admin.module index 8966ded..5f4e5c5 100644 --- a/rules_admin/rules_admin.module +++ b/rules_admin/rules_admin.module @@ -99,6 +99,15 @@ function rules_admin_menu() { 'access arguments' => array('administer rules'), 'file' => 'rules_admin.inc', ); + $items['admin/config/workflow/rules/update'] = array( + 'title' => 'Update', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('rules_admin_update'), + 'access arguments' => array('administer rules'), + 'type' => MENU_LOCAL_TASK, + 'file' => 'rules_admin.inc', + 'weight' => 1, + ); return $items; }