Index: action_email_role.module =================================================================== RCS file: /home/mosherr2/CVS/action_email_role/action_email_role.module,v retrieving revision 1.1.1.1 retrieving revision 1.20 diff -u -p -r1.1.1.1 -r1.20 --- action_email_role.module 11 Jun 2009 15:28:21 -0000 1.1.1.1 +++ action_email_role.module 11 Jun 2009 20:39:17 -0000 1.20 @@ -1,5 +1,5 @@ array( 'description' => t('Send email to all users in specified role(s)'), - 'type' => 'node', + 'type' => 'system', 'configurable' => TRUE, 'hooks' => array( 'nodeapi' => array('view', 'insert', 'update', 'delete'), + 'user' => array('view', 'insert', 'update', 'delete', 'login'), ) ), ); @@ -50,8 +51,9 @@ function action_email_role_send_email_ac $context['message'] = ''; } - // roles checkboxes - $result = db_query('SELECT * FROM {role} WHERE rid NOT IN (1,2)'); + // roles checkboxes - only eleminate the administrator role + // allow authenticated users if you so choose + $result = db_query('SELECT rid, name FROM {role} WHERE rid <> 1'); while ($role = db_fetch_object($result)) { $roles[$role->rid] = $role->name; } @@ -80,7 +82,7 @@ function action_email_role_send_email_ac $form['node_types']['node_types_set'] = array( '#type' => 'radios', - '#title' => t('Perform action for'), + '#title' => t('Perform action for (This must be set to all node types if using in a Trigger for anything but Content).'), '#options' => array( 'all' => t('all node types'), 'selected' => t('selected node types (below)'), @@ -113,7 +115,7 @@ function action_email_role_send_email_ac '#default_value' => $context['message'], '#cols' => '80', '#rows' => '20', - '#description' => t('The message that should be sent. You may include the following variables: %site_name, %uid, %node_url, %node_type, %title, %teaser, %body. Not all variables will be available in all contexts.'), + '#description' => t('The message that should be sent. You may include the following variables: %site_name, %username, %uid, %node_url, %node_type, %title, %teaser, %body. Not all variables will be available in all contexts.'), '#required' => TRUE, ); @@ -121,6 +123,24 @@ function action_email_role_send_email_ac } /** + * validate action_email_role_send_email_action form submissions. + */ +function action_email_role_send_email_action_validate($form, &$form_state) { + // Validate the configuration form. + if ($form_state['values']['node_types_set'] != 'all') { + $number_checked = 0; + foreach ($form_state['values']['node_types_selected'] as $val) { + if ($val) { + $number_checked++; + } + } + if ($number_checked == 0) { + form_set_error('node_types_selected', t('Either select "all node types" or check at least one node type.')); + } + } +} + +/** * Process action_email_role_send_email_action form submissions. */ function action_email_role_send_email_action_submit($form, &$form_state) { @@ -140,27 +160,70 @@ function action_email_role_send_email_ac * Implementation of a configurable Drupal action. Sends an email to specified role(s). */ function action_email_role_send_email_action($object, $context) { - if ($context['node_types_set'] == 'all' || $context['node_types_selected'][$object->type]) { + // grab the global user variable so %username can be used in hook_mail + global $user; + switch ($context['hook']) { + case 'nodeapi': + // Because this is not an action of type 'node' the node + // will not be passed as $object, but it will still be available + // in $context. + $node = $context['node']; + break; + case 'user': + // Because this is not an action of type 'user' the user + // object is not passed as $object, but it will still be available + // in $context. + $account = $context['account']; + if (isset($context['node'])) { + $node = $context['node']; + } + break; + default: + // We are being called directly + $node = $object; + } + /* + * Go ahead if this is not a node being handled, or if it applies to any node + * type, or if it is a node of the type handled by this action + */ + if ($context['hook'] != 'nodeapi' || $context['node_types_set'] == 'all' || $context['node_types_selected'][$node->type]) { + $from = variable_get('site_mail', ini_get('sendmail_from')); - $params['object'] = $object; $params['context'] = $context; foreach ($context['recipient'] as $rid => $rname) { if (!empty($rname)) { - $roles[]=$rid; + $roles[] = $rid; } } + if (isset($node)) { + if (!isset($account)) { + $account = user_load(array('uid' => $node->uid)); + } + } + + if (!isset($account)) { + $account = $user; + } + + $language = user_preferred_language($account); + $params = array('account' => $account, 'object' => $object, 'context' => $context); + + if (isset($node)) { + $params['node'] = $node; + } + $emailed = 0; + // TODO: as I understand, ur.* is insecure. $result = db_query("SELECT ur.*, u.mail, u.name FROM {users_roles} ur LEFT JOIN {users} u ON ur.uid = u.uid WHERE ur.rid IN (%s) AND u.status = 1", implode(',', $roles)); - while ($account = db_fetch_object($result)) { - // @@@ 'en' shouldn't be hardcoded - if (drupal_mail('action_email_role', 'email_roles', $account->mail, 'en', $params, $from)) { - watchdog('action_email_role', 'Sent email to %recipient', array('%recipient' => $account->mail)); + while ($recipient = db_fetch_object($result)) { + if (drupal_mail('action_email_role', 'email_roles', $recipient->mail, $language, $params, $from)) { + watchdog('action_email_role', 'Sent email to %recipient', array('%recipient' => $recipient->mail)); $emailed++; } else { - watchdog('error', 'Unable to send email to %recipient from action_email_role', array('%recipient' => $account->mail)); + watchdog('error', 'Unable to send email to %recipient from action_email_role', array('%recipient' => $recipient->mail)); } } watchdog('action_email_role', "!emailed users emailed successfuly.", array('!emailed' => $emailed)); @@ -171,23 +234,41 @@ function action_email_role_send_email_ac * Implementation of hook_mail(). */ function action_email_role_mail($key, &$message, $params) { - $node = $params['object']; + $account = $params['account']; + $context = $params['context']; - $recipient = $params['context']['recipient']; - $subject = $params['context']['subject']; - $message_body = $params['context']['message']; + // grab mail related variables + $recipient = $context['recipient']; + $subject = $context['subject']; + $message_body = $context['message']; $variables = array( '%site_name' => variable_get('site_name', 'Drupal'), - '%uid' => $node->uid, - '%node_url' => url('node/' . $node->nid, array('absolute' => TRUE)), - '%node_type' => node_get_types('name', $node), - '%title' => check_plain($node->title), - '%teaser' => check_markup($node->teaser), - '%body' => check_markup($node->body) + '%username' => $account->name, ); + // Get uid for new users + if ($context['hook'] == 'user') { + $variables += array( + '%uid' => $account->uid + ); + } + + // Node-based variable translation is only available if we have a node. + if (isset($params['node'])) { + $node = $params['node']; + $variables += array( + '%uid' => $node->uid, + '%node_url' => url('node/'. $node->nid, array('absolute' => TRUE)), + '%node_type' => node_get_types('name', $node), + '%title' => check_plain($node->title), + '%teaser' => check_markup($node->teaser), + '%body' => check_markup($node->body) + ); + } + $subject = strtr($subject, $variables); + $message_body = strtr($message_body, $variables); $message['subject'] = str_replace(array("\r", "\n"), '', $subject); - $message['body'] = strtr($message_body, $variables); + $message['body'][] = drupal_html_to_text($message_body); }