/[drupal]/contributions/modules/privatemsgmm/privatemsgmm.module
ViewVC logotype

Contents of /contributions/modules/privatemsgmm/privatemsgmm.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Thu Mar 12 09:58:13 2009 UTC (8 months, 2 weeks ago) by mcgo
Branch: MAIN
CVS Tags: DRUPAL-5--1-1, HEAD
Changes since 1.6: +103 -65 lines
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 // $Id$
3 /**
4 * @file
5 * The privatemsgmm.module. It allows the roles with the given permission to send
6 * mass mails via the privatemsg module.
7 * @author Mirko Haaser, mcgo@drupalist.de
8 * @link http://www.drupal.org/project/privatemsgmm
9 */
10
11 /**
12 * Implementation of hook_perm().
13 * @see hook_perm()
14 */
15 function privatemsgmm_perm() {
16 return array('send mass mail by role', 'send mass mail by user');
17 }
18
19 function privatemsgmm_menu($may_cache) {
20 global $user;
21 $items = array();
22
23 if ($may_cache) {
24 $access_role = user_access('send mass mail by role');
25 $access_user = user_access('send mass mail by user');
26 $items[] = array(
27 'path' => 'privatemsgmm/newbyrole',
28 'title' => t('Write a new mass mail by role'),
29 'callback' => 'drupal_get_form',
30 'callback arguments' => array('privatemsgmm_newbyrole'),
31 'access' => $access_role,
32 'type' => MENU_CALLBACK,
33 );
34 $items[] = array(
35 'path' => 'privatemsgmm/newbyuser',
36 'title' => t('Write a new mass mail by user'),
37 'callback' => 'drupal_get_form',
38 'callback arguments' => array('privatemsgmm_newbyuser'),
39 'access' => $access_user,
40 'type' => MENU_CALLBACK,
41 );
42 }
43 return $items;
44 }
45
46 /**
47 * Implementation of hook_form_alter().
48 * @see hook_form_alter()
49 */
50 function privatemsgmm_form_alter($form_id, &$form) {
51 if ($form_id == "privatemsg_list_form") {
52 $access_role = user_access('send mass mail by role');
53 $access_user = user_access('send mass mail by user');
54 if ($access_role) {
55 $form['new_massmailbyrole'] = array(
56 '#type' => 'markup',
57 '#value' => l(t('Write a new mass mail (by role)'), 'privatemsgmm/newbyrole'),
58 '#weight' => -3,
59 );
60 if ($access_user) $form[] = array('#type' => 'markup', '#value' => ' - ', '#weight' => -2, );
61 }
62 if ($access_user) {
63 $form['new_massmailbyuser'] = array(
64 '#type' => 'markup',
65 '#value' => l(t('Write a new mass mail (by user)'), 'privatemsgmm/newbyuser'),
66 '#weight' => -1,
67 );
68 }
69 }
70 }
71
72 /**
73 * Menu Callback for the menu item that generates a new privatemsg massmail by role
74 * @return array a valid fapi form
75 */
76 function privatemsgmm_newbyrole() {
77 global $user;
78
79 $roles = db_fetch_object(db_query("SELECT COUNT(rid) AS no FROM {role};"));
80 if ($roles->no <= 1) {
81 // 2 Do: Add setting page to let the user chose mass mailing to authenticated role or not.
82 drupal_set_message(t('There are no user defined roles available. Massmailing is only available to user defined roles.'), 'error');
83 return FALSE;
84 }
85 $form = array('preview' => array());
86
87 $form['recipients'] = array(
88 '#type' => 'checkboxes',
89 '#title' => t('Recipients'),
90 '#options' => _privatemsgmm_roles(),
91 '#required' => TRUE
92 );
93 $form['subject'] = array(
94 '#type' => 'textfield',
95 '#title' => t('Subject'),
96 '#default_value' => $message->subject,
97 '#size' => 50,
98 '#maxlength' => 64,
99 '#required' => TRUE
100 );
101 $form['privatemsgbody'] = array(
102 '#type' => 'textarea',
103 '#title' => t('Message'),
104 '#default_value' => $message->message,
105 '#cols' => 80,
106 '#rows' => 5,
107 '#required' => TRUE
108 );
109 $form[] = filter_form($message->format);
110 $form[] = array(
111 '#type' => 'submit',
112 '#value' => t('Send private mass mail by role')
113 );
114 return $form;
115 }
116
117 /**
118 * Helper Function to generate the form for new mass mails to users.
119 * @return array a valid fapi form.
120 */
121 function privatemsgmm_newbyuser() {
122 global $user;
123
124 $form = array('preview' => array());
125
126 $form['recipients'] = array(
127 '#type' => 'select',
128 '#title' => t('Recipients'),
129 '#multiple' => TRUE,
130 '#options' => _privatemsgmm_users(),
131 '#required' => TRUE
132 );
133 $form['subject'] = array(
134 '#type' => 'textfield',
135 '#title' => t('Subject'),
136 '#default_value' => $message->subject,
137 '#size' => 50,
138 '#maxlength' => 64,
139 '#required' => TRUE
140 );
141 $form['privatemsgbody'] = array(
142 '#type' => 'textarea',
143 '#title' => t('Message'),
144 '#default_value' => $message->message,
145 '#cols' => 80,
146 '#rows' => 5,
147 '#required' => TRUE
148 );
149 $form[] = filter_form($message->format);
150
151 $form[] = array(
152 '#type' => 'submit',
153 '#value' => t('Send private mass mail by user')
154 );
155 return $form;
156 }
157
158 /**
159 * Submit function for the new massmail by role form
160 * @param string $form_id The form_id of the form
161 * @param string $form_values The values of the submitted form
162 */
163 function privatemsgmm_newbyrole_submit($form_id, $form_values) {
164 global $user;
165
166 if ($form_values['op'] == t('Send private mass mail by role')) {
167 // Send to authenticated role
168 if (array_key_exists('2', $form_values['recipients']) && $form_values['recipients'][2] == "2" ) {
169 // Perform database queries to gather online user lists. We use s.timestamp
170 // rather than u.access because it is much faster.
171 $interval = time() - variable_get('user_block_seconds_online', 900);
172 $authenticated_users = db_query('SELECT DISTINCT u.uid, u.name, s.timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= %d AND s.uid > 0 ORDER BY s.timestamp DESC', $interval);
173 while ($userrow = db_fetch_array($authenticated_users)) {
174 $recipient = user_load(array('uid' => $userrow['uid']));
175 if (is_object($recipient)) {
176 $result = privatemsg_send_privatemsg($recipient, $form_values['subject'], $form_values['privatemsgbody'], $form_values['format']);
177 if ($result) $count++;
178 }
179 }
180 }
181
182 if (count($form_values['recipients']) > 0) {
183 $query = "SELECT DISTINCT uid FROM {users_roles} WHERE rid = " . implode(' OR rid = ', array_filter($form_values['recipients']));
184 $res = db_query($query);
185
186 while ($userrow = db_fetch_array($res)) {
187 if ((int)$userrow['uid'] > 0) {
188 $recipient = user_load(array('uid' => $userrow['uid']));
189 if (is_object($recipient)) {
190 $result = privatemsg_send_privatemsg($recipient, $form_values['subject'], $form_values['privatemsgbody'], $form_values['format']);
191 if ($result) $count++;
192 }
193 }
194 }
195 }
196
197 $message = $count > 0 ? t('Message sent to %count', array('%count' => format_plural($count, '1 user', '@count users'))) : t('No recipient found - Message was not sent.');
198 drupal_set_message($message);
199 watchdog('Mass Mail by role ', $message);
200 drupal_goto($user->uid ? 'privatemsg' : '');
201 }
202 }
203
204 /**
205 * Submit function for the new massmail by user form
206 * @param string $form_id The form_id of the form
207 * @param string $form_values The values of the submitted form
208 */
209 function privatemsgmm_newbyuser_submit($form_id, $form_values) {
210 global $user;
211 if ($form_values['op'] == t('Send private mass mail by user')) {
212 $count=0;
213 $rec_users = $form_values['recipients'];
214
215 while ($hash = current($rec_users)) {
216 $recipient = user_load(array('uid' => $hash));
217 if (is_object($recipient)) {
218 $result = privatemsg_send_privatemsg($recipient, $form_values['subject'], $form_values['privatemsgbody'], $form_values['format']);
219 if ($result) $count++;
220 }
221 next($rec_users);
222 }
223
224 $message = $count > 0 ? t('Message sent to %count', array('%count' => format_plural($count, '1 user', '@count users'))) : t('No recipient found - Message was not sent.');
225 drupal_set_message($message);
226 watchdog('Mass Mail by user ', $message);
227 drupal_goto($user->uid ? 'privatemsg' : '');
228 }
229 }
230
231 /**
232 * Helper Function to fill the fapi element with valid selectable values
233 * @return array An indexed array of all possible roles. The roles "authenticated" and "anonymous" are not returned.
234 */
235 function _privatemsgmm_roles() {
236 $roles = array();
237 $roles[2] = t('Online User');
238 $res = db_query('SELECT rid, name FROM {role} WHERE rid > 2 ORDER BY name');
239 while ($row = db_fetch_object($res)) {
240 $roles[$row->rid] = $row->name;
241 }
242 return $roles;
243 }
244
245 /**
246 * Helper Function to fill the fapi element with valid selectable values
247 * @return array An indexed array of all possible roles. The uid 0 (guest) and 1 (superuser) are not returned.
248 */
249 function _privatemsgmm_users() {
250 $users = array();
251 $res = db_query('SELECT uid, name FROM {users} WHERE uid > 1 ORDER BY name');
252 while ($row = db_fetch_object($res)) {
253 $users[$row->uid] = $row->name;
254 }
255 return $users;
256 }
257
258

  ViewVC Help
Powered by ViewVC 1.1.2