/[drupal]/contributions/sandbox/jjeff/rotomail/rotomail.module
ViewVC logotype

Contents of /contributions/sandbox/jjeff/rotomail/rotomail.module

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


Revision 1.1 - (show annotations) (download) (as text)
Sun Aug 14 01:37:13 2005 UTC (4 years, 3 months ago) by jjeff
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
rotomail.module is a rotating mailing list module.
Rotating mailing lists are lists where each submitted message is sent
to the next person on the list. This is good for distributing help requests
to different tech support people or evenly spreading out contacts for
commission-type business. Project to be created soon...
1 <?php
2
3 // $id
4
5
6 /**
7 * Special fields for forms:
8 * * listname - the name of rotating list to use for this form
9 * * subject - the subject line for the email
10 * * body - the main body of the message
11 * * success - the path to the 'thank you' page
12 * * mail - the sender's email - omit to use mail address from user data
13 * * name - the sender's name - omit to use username
14 *
15 */
16
17 function rotomail_help($section) {
18 switch($section) {
19 case 'admin/modules#description':
20 return t('Submitted forms can send email to the next address on a list of email addresses.');
21 case 'admin/rotomail':
22 return t('<p>Rotomail allows for each consecutive submission of a web form to get sent to the next person on a list. The submissions are "rotated" through the list -- hence the name rotomail. Mailto lists can have as many members as you like. But if you want to use the form, mailing, and logging features of rotomail without sending to a rotating list, you can create a mailto list witn only one contact. Forms submitted to this list will always get sent to the same email address.</p><p>Rotomail relies on PHP-format nodes to provide it\'s forms. These forms can have unlimited fields and all of their data will be stored in the log and sent out as email. Information about how to format these forms can be found in the code of the module itself.</p>');
23 }
24 }
25
26 function rotomail_menu($may_cache) {
27 $items = array();
28 if ($may_cache) {
29 $items[] = array('path' => 'rotomail', 'title' => t('send form mail'), 'access' => user_access('send messages'), 'callback' => 'rotomail_submit_form', 'type' => MENU_CALLBACK);
30 $items[] = array('path' => 'admin/rotomail', 'title' => t('rotomail'), 'access' => (user_access('manage mailto lists')||user_access('view logs')), 'callback'=>'rotomail_list_lists');
31 $items[] = array('path' => 'admin/rotomail/log', 'title' => t('logs'), 'access' => (user_access('view logs')), 'type'=>MENU_CALLBACK, 'callback'=>'rotomail_view_log');
32 $items[] = array('path' => 'admin/rotomail/list', 'title' => t('lists'), 'access' => (user_access('manage mailto lists')), 'type'=>MENU_LOCAL_TASK, 'callback'=>'rotomail_view_list');
33 }
34 else {
35 // Only include this for the create content and edit content pages.
36 if (user_access('manage mailto lists') && strstr($_GET['q'], 'admin/rotomail/list') && strstr($_GET['q'], '/rearrange')) {
37 $head = "<script type=\"text/javascript\" src=\"". drupal_get_path('module', 'rotomail'). "/core.js\"></script>";
38 $head .= "<script type=\"text/javascript\" src=\"". drupal_get_path('module', 'rotomail'). "/events.js\"></script>";
39 $head .= "<script type=\"text/javascript\" src=\"". drupal_get_path('module', 'rotomail'). "/css.js\"></script>";
40 $head .= "<script type=\"text/javascript\" src=\"". drupal_get_path('module', 'rotomail'). "/coordinates.js\"></script>";
41 $head .= "<script type=\"text/javascript\" src=\"". drupal_get_path('module', 'rotomail'). "/drag.js\"></script>";
42 $head .= "<script type=\"text/javascript\" src=\"". drupal_get_path('module', 'rotomail'). "/dragsort.js\"></script>";
43 $head .= "<script type=\"text/javascript\" src=\"". drupal_get_path('module', 'rotomail'). "/cookies.js\"></script>";
44
45 drupal_set_html_head($head);
46 }
47 }
48 return $items;
49 }
50
51 function rotomail_perm() {
52 return array("send messages", "manage mailto lists", "view logs");
53 }
54
55 /**
56 * Need lots of documentation here.
57 *
58 */
59
60 function theme_rotomail_mailform() {
61 // this will handle the form when it's submitted
62 if ($_POST['submit']) {
63 $edit = $_POST['edit'];
64 rotomail_submit_form($edit);
65 }
66 global $user;
67 // Listname is required.
68 // 'Test' is name of list of contacts
69 // Change to whatever you've called your list.
70 $form .= form_hidden('listname', 'Contact Become A Coach');
71
72 // Put form fields, selects, or whatever here -- as many as you want
73 // suffix the field name with '_r' to make it required.
74 $form .= form_textfield(t('Your Name'), 'name', ($edit['name']) ? $edit['name'] : $user->name, 50, 200, NULL, FALSE);
75 $form .= form_textfield(t('Your Email'), 'mail', ($edit['mail']) ? $edit['mail'] : $user->mail, 50, 200, t('Without your email address, we will not be able to respond.'), NULL, TRUE);
76 $form .= form_textfield(t('Subject'), 'subject', $edit['subject'], 50, 200, t('Subject for your message.'), NULL, FALSE);
77 $form .= form_textarea(t('Body'), 'body_r', $edit['body_r'], 50, 8, t('The body of your message (required).'), NULL, TRUE);
78 // for more about form fields see: http://drupaldocs.org/api/head/group/form
79
80 // submit button can have whatever value you want
81 // but don't change the 'submit' part!!
82 $form .= form_submit(t('Send Message'), 'submit');
83 // the following line should probably not be changed
84 $output = form($form, 'post', '');
85 return $output;
86 }
87
88 /**
89 * Handler for submitted forms
90 */
91
92 function rotomail_submit_form(&$edit) {
93 $success = $edit['success'];
94 rotomail_validate_form($edit);
95 if (!form_get_errors()) {
96 rotomail_save_message($edit);
97 rotomail_send_message($edit);
98 // clear the form...
99 $edit = array();
100 // if there's a 'success' value go there
101 // otherwise, just stay on this page...
102 if ($success) {
103 drupal_goto($success);
104 }
105 else {
106 drupal_set_message(t('Your message was sent. Thank you.'));
107 }
108 }
109 }
110
111 function rotomail_validate_form(&$edit) {
112 // maybe we should check for listname here...
113 if (is_array($edit)) {
114 if (!isset($edit['listname'])) {
115 form_set_error('listname', t('Listname value does not exist in the form. Please alert the site administrator.'));
116 }
117 elseif ($edit['listname'] =='') {
118 // if listname is blank, we're to assume that listname is an unselected 'select' list
119 // so we'll return a more subdued error:
120 form_set_error('listname', t('No recipient value is selected.'));
121 }
122 elseif (!rotomail_get_lid($edit['listname'])) {
123 form_set_error('listname', t('The form\'s listname value is not valid. Please alert the site administrator.'));
124
125 }
126 $regex = '/_r$/';
127 foreach($edit as $key => $value) {
128 if (preg_match($regex, $key)) {
129 // if field name ends in '_r'
130 if (!strlen(trim($value))) {
131 form_set_error($key, preg_replace($regex, '', $key).t(' is required.'));
132 }
133 else {
134 // remove '_r' from field names
135 unset($edit[$key]);
136 $edit[preg_replace($regex, '', $key)] = $value;
137 }
138 }
139 }
140 }
141 if (form_get_errors()) {
142 return;
143 }
144 else {
145 global $user;
146 if (!$edit['mail']) {
147 $edit['mail'] = $user->mail;
148 }
149 if (!$edit['name']) {
150 $edit['name'] = $user->name;
151 }
152 unset($edit['success']);
153 }
154 }
155
156 function rotomail_save_message($edit) {
157 $mid = db_next_id('rotomail_log');
158 $lid = rotomail_get_lid($edit['listname']);
159 $cid = rotomail_next_contact($lid);
160 $stamp = time();
161 $glop = serialize($edit);
162 return db_query('INSERT INTO {rotomail_log} (mid, lid, cid, stamp, glop) VALUES (%d, %d, %d, %d, \'%s\')', $mid, $lid, $cid, $stamp, $glop);
163 }
164
165 function rotomail_send_message($edit) {
166 $lid = rotomail_get_lid($edit['listname']);
167 $subject = rotomail_get_list_prefix($lid);
168 $subject .= ($edit['subject']) ? $edit['subject'] : t('Message Submitted From ').variable_get("site_name", "Your Web Site");
169 unset($edit['subject']);
170 if (valid_email_address($edit['mailfrom'])) {
171 $mailfrom = $edit['mailfrom'];
172 unset($edit['mailfrom']);
173 }
174 else {
175 $mailfrom = variable_get("site_mail", ini_get("sendmail_from"));
176 }
177 unset($edit['listname']);
178 foreach ($edit as $key => $value) {
179 $message .= "$key: ";
180 $x = print_r($value, TRUE);
181 // if value is more than 60 characters, put it on a new line with space after
182 $message .= (strlen($x) > 60) ? "\r\n".$x."\r\n\r\n" : $x."\r\n";
183 }
184 $cid = rotomail_next_contact($lid);
185 $to = rotomail_get_contact_mail($cid);
186 rotomail_increment($lid);
187 $headers = "FROM: ".$mailfrom." \r\n";
188 user_mail($to, $subject, $message, $headers);
189 }
190
191 function rotomail_list_lists($op = NULL, $lid = NULL) {
192 switch ($_POST['op']) {
193 case t('Submit List'):
194 if (!isset($_POST['edit']['lid'])) {
195 $_POST['edit']['lid'] = db_next_id('rotomail_lists');
196 }
197 else {
198 db_query('DELETE FROM {rotomail_lists} WHERE lid=%d', $_POST['edit']['lid']);
199 }
200 db_query('INSERT INTO {rotomail_lists} (name, prefix, description, lid) VALUES ("%s", "%s", "%s", %d)', $_POST['edit']['name'], $_POST['edit']['prefix'], $_POST['edit']['description'], $_POST['edit']['lid']);
201 break;
202
203 }
204 if ($_POST['edit']['confirm'] && $lid = $_POST['edit']['lid']) {
205 db_query('DELETE FROM {rotomail_lists} WHERE lid=%d', $_POST['edit']['lid']);
206 drupal_set_message(t('list deleted'));
207 unset($_POST);
208 drupal_goto('admin/rotomail');
209 }
210 switch ($op) {
211 case 'manage':
212 case 'rename':
213 drupal_set_title(t('Edit List Info'));
214 $output .= rotomail_make_list(array('lid'=>$lid));
215 break;
216 case 'delete':
217 $hidden = form_hidden('lid', $lid);
218 $output = theme('confirm', t('Are you sure you want to delete this list?'), 'admin/rotomail', NULL, NULL, NULL, $hidden);
219 break;
220 default:
221 $logacc = user_access('view logs');
222 $editacc = user_access('manage mailto lists');
223 $header[] = t('Rotating Mailto Lists');
224 $header[] = t('#');
225 $header[] = t('Prefix');
226 $header[] = t('Operations');
227 $result = db_query('SELECT * FROM {rotomail_lists} ORDER BY name');
228 $cellstyle = "vertical-align:top";
229 while($list = db_fetch_object($result)) {
230 $rows[$list->lid]['name']['data'] = $list->name;
231 $rows[$list->lid]['name']['data'] .= $list->description ? '<br /><span style="font-size:xx-small">'.$list->description.'</span>' : '';
232 $rows[$list->lid]['name']['style'] = $cellstyle;
233 $rows[$list->lid]['contacts']['data'] = db_result(db_query('SELECT COUNT(*) FROM {rotomail_contacts} WHERE lid = %d', $list->lid));
234 $rows[$list->lid]['contacts']['style'] = $cellstyle;
235 $rows[$list->lid]['prefix']['data'] = $list->prefix;
236 $rows[$list->lid]['prefix']['style'] = $cellstyle;
237 $rows[$list->lid]['operations']['data'] = ($logacc) ? l(t('log'), 'admin/rotomail/log/'.$list->lid).'&nbsp;' : '';
238 $rows[$list->lid]['operations']['data'] .= ($editacc) ? l(t('manage'), 'admin/rotomail/list/'.$list->lid).'&nbsp;'.l(t('edit'), 'admin/rotomail/rename/'.$list->lid).'&nbsp;'.l(t('delete'), 'admin/rotomail/delete/'.$list->lid) : '';
239 $rows[$list->lid]['operations']['style'] = $cellstyle;
240 }
241 $output = theme('table', $header, $rows, array('style'=>'width:100%'));
242 $output .= '<br /><hr />';
243 $output .= t('<h3>Add a List</h3>');
244 $output .= rotomail_make_list();
245 }
246 print theme('page', $output);
247 }
248
249 function rotomail_make_list($edit = NULL) {
250 if (isset($edit['lid'])) {
251 $form .= form_hidden('lid', $edit['lid']);
252 $edit = db_fetch_array(db_query('SELECT * FROM {rotomail_lists} WHERE lid = %d', $edit['lid']));
253 //$edit['name'] = rotomail_get_list_name($edit['lid']);
254 //$edit['prefix'] = rotomail_get_list_prefix($edit['lid']);
255 //$edit['description'] = rotomail_get_list_prefix($edit['lid']);
256 }
257 $form .= form_textfield(t('List Name'), 'name', $edit['name'], 40, 100, NULL, NULL, TRUE);
258 $form .= form_textfield(t('Description'), 'description', $edit['description'], 40, 200, t('Optional list description (only appears in listing in admin area).'));
259 $form .= form_textfield(t('Prefix Subject'), 'prefix', $edit['prefix'], 40, 100, t('Handy for identifying messages sent through rotomailer.<br />Add a trailing space so that message subject doesn\'t butt up against the prefix.<br />Examples: "[Contact Form] ", "Web Message: ", "***Whoop Dee Doo**** ".'));
260 $form .= form_submit(t('Submit List'));
261 $output = form($form, 'post', url('admin/rotomail'));
262 return $output;
263 }
264
265 function rotomail_view_list($lid, $op = NULL, $cid = NULL) {
266 switch ($_POST['op']) {
267 case t('Submit New Order'):
268 var_dump($_POST['edit']);
269 break;
270
271 case t('Submit Contact'):
272 rotomail_save_contact();
273 break;
274
275 case "Confirm":
276 db_query('DELETE FROM {rotomail_contacts} WHERE cid = %d', $_POST['edit']['cid']);
277 drupal_set_message(t("Contact deleted"));
278 drupal_goto("admin/rotomail/list/".$lid);
279 }
280 switch ($op) {
281 case 'rearrange':
282 drupal_set_title(t('Rearrange Mailing Order of ').rotomail_get_list_name($lid));
283 $output = t('<p>You can rearrange this list by dragging the items</p>');
284 $result = rotomail_contacts_result($lid);
285 $form = "<ul id='rearrange'>\n";
286 while($c = db_fetch_object($result)) {
287 $form .= '<li>';
288 if (is_numeric($c->mailoruid)) {
289 $u = user_load(array('uid'=>$c->mailoruid));
290 $form .= $u->name.' ('.$u->mail.')';
291 }
292 else {
293 $form .= $c->mailoruid;
294 }
295 $form .= form_hidden('order][', $c->cid);
296 $form .= "</li>\n";
297 }
298 $form .= "</ul>\n";
299 $form .= form_submit('Submit New Order');
300 $output .= form($form, 'post', url('admin/rotomail/list/'.$lid));
301 print theme('page', $output);
302 break;
303 case 'edit':
304 drupal_set_title(t('Edit Contact'));
305 //$output .= rotomail_make_list(array('lid'=>$lid));
306 break;
307 case 'delete':
308 $hidden .= form_hidden('cid', $cid);
309 $output = theme('confirm', t('Are you sure you want to delete this contact?'), 'admin/rotomail/list/'.$lid, NULL, NULL, NULL, $hidden);
310 break;
311 default:
312 $listname = rotomail_get_list_name($lid);
313 $breadcrumb[] = array('path' => 'admin/rotomail', 'title' => t('rotomail'));
314 $breadcrumb[] = array('path' => 'admin/rotomail/list/'.$lid, 'title' => $listname);
315 //$breadcrumb[] = array('path' => 'stores/'. $account->uid, 'title' => t("%name's store", array('%name' => $account->name)));
316 menu_set_location($breadcrumb);
317
318 drupal_set_title($listname.t(' list'));
319 $header = array(t('next'), t('contact'), t('operations'));
320 $result = rotomail_contacts_result($lid);
321 while($c = db_fetch_object($result)) {
322 $rows[$c->cid]['next'] = (rotomail_next_contact($lid)==$c->cid) ? '<strong>>>></strong>' : '&nbsp;';
323 if (is_numeric($c->mailoruid)) {
324 $u = user_load(array('uid'=>$c->mailoruid));
325 $rows[$c->cid]['contact'] = $u->name.' ('.$u->mail.')';
326 }
327 else {
328 $rows[$c->cid]['contact'] = $c->mailoruid;
329 }
330 $rows[$c->cid]['operations'] = l('edit', 'admin/rotomail/list/'.$lid.'/edit/'.$c->cid). '&nbsp;' . l('delete', 'admin/rotomail/list/'.$lid.'/delete/'.$c->cid);
331 }
332 $output = '<p>'.l(t('log'), 'admin/rotomail/log/'.$lid).'</p>';
333 $output .= theme('table', $header, $rows, array('style'=>'width:100%', 'id'=>'rearrangetable'));
334 //$output .= '<p>'.l(t('rearrange mailing order'), 'admin/rotomail/list/'.$lid.'/rearrange').'</p>';
335 $output .= '<br /><hr />';
336 $output .= t('<h3>Add a Contact</h3>');
337 $output .= rotomail_edit_contact($lid);
338 }
339 print theme('page', $output);
340 }
341
342 function rotomail_save_contact() {
343 if (!isset($_POST['edit']['cid'])) {
344 $_POST['edit']['cid'] = db_next_id('rotomail_contacts');
345 }
346 else {
347 db_query('DELETE FROM {rotomail_contacts} WHERE cid=%d', $_POST['edit']['cid']);
348 }
349 if (!strlen(trim($_POST['edit']['mailoruid']))) {
350 $error = t('Please enter a value.');
351 }
352 if (valid_email_address($_POST['edit']['mailoruid'])) {
353 //it's an email address, but is it one of 'ours'
354 if ($u = user_load(array('mail'=>$_POST['edit']['mailoruid']))) {
355 $_POST['edit']['mailoruid'] = $u->uid;
356 }
357 }
358 else {
359 // it's not an email address. is it a valid user name or uid?
360 if (is_numeric($_POST['edit']['mailoruid'])) {
361 if ($u = user_load(array('uid'=>$_POST['edit']['mailoruid']))) {
362 $_POST['edit']['mailoruid'] = $u->uid;
363 }
364 else {
365 $error = t('Invalid user id. Please try again.');
366 }
367 }
368 else {
369 // looks like a username. Is it one?
370 if ($u = user_load(array('name'=>$_POST['edit']['mailoruid']))) {
371 $_POST['edit']['mailoruid'] = $u->uid;
372 }
373 else {
374 $error = t('Invalid username. Please try again.');
375 }
376 }
377 }
378 if (isset($error)) {
379 drupal_set_message($error);
380 }
381 else {
382 db_query('INSERT INTO {rotomail_contacts} (cid, mailoruid, lid) VALUES (%d, "%s", %d)', $_POST['edit']['cid'], $_POST['edit']['mailoruid'], $_POST['edit']['lid']);
383 drupal_set_message(t('Contact saved.'));
384 }
385 }
386
387 function rotomail_edit_contact($lid, $edit = NULL) {
388 $form .= form_hidden('lid', $lid);
389 if (isset($edit['cid'])) {
390 $form .= form_hidden('cid', $edit['cid']);
391 }
392 $form .= form_textfield(t('Contact'), 'mailoruid', $edit['mailoruid'], 40, 100, t('Username, email address, or user id'), NULL, TRUE);
393 $form .= form_submit(t('Submit Contact'));
394 $output = form($form, 'post', url('admin/rotomail/list/'.$lid));
395 return $output;
396 }
397
398 function rotomail_get_list_name($lid) {
399 if (is_numeric($lid)) {
400 return db_result(db_query('SELECT name FROM {rotomail_lists} WHERE lid = %d', $lid));
401 }
402 }
403
404 function rotomail_get_list_prefix($lid) {
405 if (is_numeric($lid)) {
406 return db_result(db_query('SELECT prefix FROM {rotomail_lists} WHERE lid = %d', $lid));
407 }
408 }
409
410 function rotomail_get_lid($name) {
411 return db_result(db_query('SELECT lid FROM {rotomail_lists} WHERE name LIKE \'%s\'', $name));
412 }
413
414 /**
415 * Returns sql query handle for {rotomail_contacts}
416 * standardizes ORDER BY and other sorting
417 */
418
419 function rotomail_contacts_result($lid) {
420 return db_query('SELECT * FROM {rotomail_contacts} WHERE lid = %d', $lid);
421 }
422
423 /**
424 * Increment the list's pointer and return the new contact id
425 *
426 */
427
428 function rotomail_increment($lid) {
429 $curr = variable_get('rotomail_next_'.$lid, 0);
430 $contacts = rotomail_get_contacts($lid);
431 $next = $curr+1;
432 if ($next+1 > count($contacts)) {
433 $next = 0;
434 }
435 variable_set('rotomail_next_'.$lid, $next);
436 return $contacts[$next];
437 }
438
439 /**
440 * Return the contact id (cid) for the next person on the list
441 *
442 */
443
444 function rotomail_next_contact($lid) {
445 $next = variable_get('rotomail_next_'.$lid, 0);
446 $contacts = rotomail_get_contacts($lid);
447 return $contacts[$next];
448 }
449
450 function rotomail_get_contact_mail($cid) {
451 $mailoruid = db_result(db_query('SELECT mailoruid FROM {rotomail_contacts} WHERE cid = %d', $cid));
452 if (is_numeric($mailoruid)) {
453 $user = user_load(array('uid'=>$mailoruid));
454 $mail = $user->mail;
455 }
456 else {
457 $mail = $mailoruid;
458 }
459 return $mail;
460 }
461
462 /**
463 * Return an array of contact ids for the given list
464 *
465 */
466
467 function rotomail_get_contacts($lid) {
468 // when lists get ordered, this line will need to have ORDER BY:
469 $result = rotomail_contacts_result($lid);
470 while ($c = db_fetch_object($result)) {
471 $contacts[] = $c->cid;
472 }
473 return $contacts;
474 }
475
476 function rotomail_view_log($lid = NULL) {
477 if (!is_numeric($lid)) {
478 drupal_goto('admin/rotomail');
479 }
480 $header = array(t("sent"), t("data"));
481 $result = pager_query("SELECT * FROM {rotomail_log} l INNER JOIN {rotomail_contacts} c ON l.cid = c.cid WHERE l.lid = $lid ORDER BY stamp DESC");
482 while ($r = db_fetch_object($result)) {
483 if (is_numeric($r->mailoruid)) {
484 $user = user_load(array('uid'=>$r->mailoruid));
485 $row[$r->mid]['sent']['data'] = "to: <strong>".$user->name."</strong><br />";
486 }
487 else {
488 $row[$r->mid]['sent']['data'] = "to: <strong>".$r->mailoruid."</strong><br />";
489 }
490 $row[$r->mid]['sent']['data'] .= date('M j, Y',$r->stamp).'<br />'.date('g:ia', $r->stamp);
491 $row[$r->mid]['sent']['valign'] = 'top';
492 $data = unserialize($r->glop);
493 foreach((array)$data as $key=>$value) {
494 $x = print_r($value, TRUE);
495 // if value is more than 60 characters, put it on a new line with space after
496 $message = (strlen($x) > 60) ? "<br />".$x."<br /><br />" : $x."<br />";
497 $row[$r->mid]['message']['data'] .= "<strong>$key:</strong> $message";
498 }
499 $row[$r->mid]['message']['valign'] = 'top';
500 }
501 $output = theme('table', $header, $row, array('style'=>'width:100%', 'cellpadding'=>'5'));
502 $output .= theme('pager');
503 print theme('page', $output);
504 }

  ViewVC Help
Powered by ViewVC 1.1.2