posts - 431,  comments - 344,  trackbacks - 0

          在使用action和workflow時(shí)候,突然想到怎么給一個(gè)角色組里的所以人發(fā)email,然后在主站上找到了一篇文章介紹怎么實(shí)現(xiàn)http://drupal.org/node/48738,添加以后,按照步驟,進(jìn)入action設(shè)置那里添加所要的功能,然后設(shè)置發(fā)送的角色組。這時(shí)候突然想到之前實(shí)現(xiàn)的日歷事件,然后就決定實(shí)現(xiàn)這樣一個(gè)效果,我添加一個(gè)事件,例如添加一個(gè)會(huì)議通知,然后通過email形式發(fā)送到角色組里的成員郵件里,這樣就要去workflow里面創(chuàng)建一個(gè)工作流,添加action,然后把這工作流添加到event 的node type上!這樣就可以了!
          要在action.module里面添加如下的代碼:

          function action_send_email_torolegroup($op, $edit = array(), $node) {
            switch($op) {
              case 'metadata':
                return array(
                  'description' => t('Send Email to a role group'),
                  'type' => t('Email'),
                  'batchable' => false,
                  'configurable' => true,
                );

              case 'do':
                // note this is the user who owns the node, not global $user
                $user = user_load(array('uid' => $node->uid));
                $site_name = variable_get('site_name', 'Drupal');
                $from = "$site_name <" . variable_get('site_mail', ini_get('sendmail_from')) . '>';
                $subject = $edit['subject'];
                $message = $edit['message'];
               
                foreach($edit['recipients'] as $rid => $chose_role) {
                  if ($chose_role) {
                    $recipient_roles[] = $rid;
                  }
                 
                }
               
                $recipient_addresses = array();
               
                if ($recipient_roles[2]) { //email every one! wow. that's why a watchdog is invoked

                  watchdog('action', t('Sent an email to every single registered user. Use with caution.'));
                 
                  $result = db_query('SELECT mail FROM {users} WHERE uid > 0');
                  while($account = db_fetch_object($result)) {
                    $recipient_addresses[] = $account->mail;
                  }
                } else {
                  $roles = implode(',', $recipient_roles);   
                  $result = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN (%s)', $roles);
                  while($account = db_fetch_object($result)) {
                    $recipient_addresses[] = $account->mail;
            
                  }
                }
               
                if (isset($node) && is_object($node)) {
                  $variables = array(
                    '%site_name' => $site_name,
                    '%username' => $user->name,
                    '%uid' => $node->uid,
                    '%node_url' => url('node/' . $node->nid, NULL, NULL, TRUE),
                    '%node_type' => $node->type,
                    '%title' => $node->title,
                    '%teaser' => strip_tags($node->teaser),
                    '%body' => strip_tags($node->body)
                     );

                  $message = strtr($message, $variables);
                }
                foreach ($recipient_addresses as $recipient) {
                  if (user_mail($recipient, $subject, $message, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from" )) {
                    watchdog('action', t('Sent email to %recipient', array('%recipient' => $recipient)));
                  }
                  else {
                    watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $recipient)));
                  }
                }
                break;

              // return an HTML config form for the action
              case 'form':
                // default values for form
                //  if (!isset($edit['recipients'])) $edit['recipients'] = '';
                if (!isset($edit['subject'])) $edit['subject'] = '';
                if (!isset($edit['message'])) $edit['message'] = '';
                $form = array();
                $roles = user_roles();
                unset($roles[1]); // good bye anonymous users!
                //unset($roles[2]); // good bye authenticated users!
                     
                $form['recipients'] = array(
                  '#type' => 'checkboxes',
                  '#title' => t('Recipient Role Groups'),
                  '#default_value' => $edit['recipients'],
                  '#options' => $roles,
                  '#description' => t('Select which roles should receive this email.'),
              );
               
                $form['subject'] = array(
                  '#type' => 'textfield',
                  '#title' => t('Subject'),
                  '#default_value' => $edit['subject'],
                  '#size' => '20',
                  '#maxlength' => '254',
                  '#description' => t('The subject of the message.'),
                );
                $form['message'] = array(
                  '#type' => 'textarea',
                  '#title' => t('Message'),
                  '#default_value' => $edit['message'],
                  '#cols' => '80',
                  '#rows' => '20',
                  '#description' => t('The message that should be sent.  You may include the following variables: %site_name, %username, %node_url, %node_type, %title, %teaser, %body'),
                );
                return $form;

               // validate the HTML form
              case 'validate':
                $errors = array();
               
                $roleselected = false;
                foreach($edit['recipients'] as $rid => $selected) {
                  if ($selected) {
                    $roleselected = true;
                  }
                }
                if (!$roleselected) {
                  $errors['recipients'] = t('Please enter at least one recipient role group');
                }
               
                foreach ($errors as $name => $message) {
                  form_set_error($name, $message);
                }

                return count($errors) == 0;

              // process the HTML form to store configuration
              case 'submit':

                $params = array(
                  'recipients' => $edit['recipients'],
                  'subject'   => $edit['subject'],
                  'message'   => $edit['message']);
                return $params;
            }
          }

          /**
          * Send an e-mail message.
          */
          function user_mail($mail, $subject, $message, $header) {
            if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) {
              include_once variable_get('smtp_library', '');
              return user_mail_wrapper($mail, $subject, $message, $header);
            }
            else {
              return mail(
                check_email_header($mail),
                mime_header_encode(check_email_header($subject)),
                str_replace("\r", '', $message),
                "MIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8; format=flowed\nContent-transfer-encoding: 8Bit\n" . check_email_header($header)
              );
            }
          }

          function check_email_header($val)
          {
             if (strpos($value, "\r") !== false || strpos($value, "\n") !== false)
             {
               die("Why ?? :(");
             }

             return $val;
          }

          posted on 2007-11-21 19:14 周銳 閱讀(266) 評(píng)論(0)  編輯  收藏 所屬分類: PHP
          主站蜘蛛池模板: 恩平市| 许昌县| 禹城市| 谢通门县| 灵武市| 安达市| 津南区| 岐山县| 抚顺市| 临汾市| 龙口市| 平原县| 北海市| 徐闻县| 康定县| 涪陵区| 沙湾县| 阿坝县| 城口县| 徐闻县| 福清市| 桃园市| 亚东县| 鹤山市| 扎赉特旗| 巨野县| 虎林市| 岗巴县| 衡山县| 开鲁县| 宝兴县| 大宁县| 龙南县| 巴林右旗| 南雄市| 天柱县| 合作市| 寿阳县| 邓州市| 岳阳县| 婺源县|