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

Contents of /contributions/modules/ecard/ecard.module

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


Revision 1.8 - (show annotations) (download) (as text)
Mon Sep 14 07:33:33 2009 UTC (2 months, 2 weeks ago) by karst
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +63 -67 lines
File MIME type: text/x-php
Made the todo list.
Cleaned up comments
Changed ecard_mail
1 <?php
2 // $Id: ecard.module,v 1.7 2009/09/14 06:48:38 karst Exp $
3
4 /**
5 * @file
6 * This module can be used to send any node as an ecard.
7 *
8 * @TODO:
9 * Views support
10 * Panels support
11 * New Thankyou handeling
12 * i18n support
13 * send on cron
14 * move form to a block
15 * Token support
16 * Use drupal_write_record() to write to the DB
17 *
18 */
19
20 /**
21 * Implementation of hook_perm().
22 */
23 function ecard_perm() {
24 return array('view ecards', 'send ecards', 'send own content as ecard');
25 }
26
27 /**
28 * Implementation of hook_menu().
29 */
30 function ecard_menu() {
31 $items=array();
32 $items['admin/settings/ecard'] = array(
33 'title' => 'Ecard settings',
34 'description' => 'Change how ecards behave.',
35 'page callback' => 'drupal_get_form',
36 'page arguments' => array('ecard_admin'),
37 'access arguments' => array('administer site configuration'),
38 );
39 $items['ecard/view'] = array(
40 'page callback' => 'ecard_view',
41 'type' => MENU_CALLBACK,
42 'access arguments' => array('view ecards'),
43 );
44 $items['ecard/thanks'] = array(
45 'page callback' => 'ecard_thanks',
46 'type' => MENU_CALLBACK,
47 'access arguments' => array('view ecards'),
48 );
49 return $items;
50 }
51
52 /**
53 * Used for checking if user has the rights to send ecard.
54 */
55 function ecard_form_access($uid) {
56 return user_access('send ecards') || (user_access('send own content as ecard') && ($GLOBALS['user']->uid == $uid));
57 }
58
59 /**
60 * Define the settings form.
61 */
62
63 function ecard_admin() {
64 $form['ecard_nodetypes'] = array(
65 '#type' => 'checkboxes',
66 '#title' => t('Choose the content types which you want to send as ecard'),
67 '#options' => node_get_types('names'),
68 '#default_value' => variable_get('ecard_nodetypes', array()),
69 '#description' => t('A form to send ecard will append to the content'),
70 );
71 $form['letter'] = array(
72 '#type' => 'fieldset',
73 '#title' => t('Letter customization'),
74 );
75 $form['letter']['ecard_subject'] = array(
76 '#type' => 'textfield',
77 '#title' => t('Subject'),
78 '#default_value' => variable_get('ecard_subject', 'ecard from %site'),
79 '#size' => 70,
80 '#maxlength' => 70,
81 '#description' => t('Customize the subject for ecard'),
82 );
83 $form['letter']['ecard_letter'] = array(
84 '#type' => 'textarea',
85 '#title' => t('Body'),
86 '#default_value' => variable_get('ecard_letter', _ecard_letter()),
87 '#cols' => 70,
88 '#rows' => 5,
89 '#description' => t('This text is the body of the email that the card recipient will see. These are the variables you may use: %site = your site name, %site_url = your site URL, %site_mail = your site email address, %card_url = the URL for the ecard, %sender = sender name, %sender_email = sender email, %recipient = recipient email'),
90 );
91 $form['copy'] = array(
92 '#type' => 'fieldset',
93 '#title' => t('Customization email send with copy of url'),
94 );
95 $form['copy']['ecard_copy_subject'] = array(
96 '#type' => 'textfield',
97 '#title' => t('Subject'),
98 '#default_value' => variable_get('ecard_copy_subject', 'Copy of your ecard'),
99 '#size' => 70,
100 '#maxlenghth' => 70,
101 '#description' => t('Customize email send with copy of card url'),
102 );
103 $form['copy']['ecard_copy'] = array(
104 '#type' => 'textarea',
105 '#title' => t('Body'),
106 '#default_value' => variable_get('ecard_copy', _ecard_copy()),
107 '#cols' => 70,
108 '#rows' => 5,
109 '#description' => t('This text is the body of the email to notify the sender than the card has been picked. These are the variables you may use: %site = your site name, %site_url = your site URL, %site_mail = your site email address, %card_url = the URL for the ecard'),
110 );
111 $form['notify'] = array(
112 '#type' => 'fieldset',
113 '#title' => t('Notification email customization'),
114 );
115 $form['notify']['ecard_notify_subject'] = array(
116 '#type' => 'textfield',
117 '#title' => t('Subject'),
118 '#default_value' => variable_get('ecard_notify_subject', 'Your ecard has been just picked'),
119 '#size' => 70,
120 '#maxlength' => 70,
121 '#description' => t('Customize the subject for ecard'),
122 );
123 $form['notify']['ecard_notify'] = array(
124 '#type' => 'textarea',
125 '#title' => t('Body'),
126 '#default_value' => variable_get('ecard_notify', _ecard_notify()),
127 '#cols' => 70,
128 '#rows' => 5,
129 '#description' => t('This text is the body of the email to notify the sender than the card has been picked. These are the variables you may use: %site = your site name, %site_url = your site URL, %site_mail = your site email address, %card_url = the URL for the ecard, %sender = sender name, %recipient = recipient email'),
130 );
131 $form['misc'] = array(
132 '#type' => 'fieldset',
133 '#title' => t('Miscellaneous settings'),
134 );
135 $form['misc']['ecard_cron'] = array(
136 '#type' => 'textfield',
137 '#title' => t('Days to keep ecards'),
138 '#description' => t('ecards that are aged more than the number of days entered above will automatically be deleted from the database. Use 0 to never delete. Note that this will only be checked when cron.php is run.'),
139 '#size' => '2',
140 '#default_value' => variable_get('ecard_cron', '0'),
141 );
142 $form['misc']['ecard_max_count'] = array(
143 '#type' => 'textfield',
144 '#title' => 'Maximum number of emails allowed to send at a time',
145 '#description' => 'Do not set it to very big values as it may cause spaming',
146 '#size' => '5',
147 '#default_value' => variable_get('ecard_max_count', '100'),
148 );
149 $form['misc']['ecard_site_email'] = array(
150 '#type' => 'textfield',
151 '#title' => 'This email used to send emails',
152 '#description' => 'eg: noreply@yoursitename.com or admin@sitename.com etc',
153 '#default_value' => variable_get('ecard_site_email', 'root@localhost'),
154 );
155 $form['misc']['ecard_hide_send_view'] = array(
156 '#type' => 'checkbox',
157 '#title' => t('Hide the send form when viewing ecard message'),
158 '#default_value' => variable_get('ecard_hide_send_view', 0),
159 );
160 $form['misc']['ecard_require_name'] = array(
161 '#type' => 'checkbox',
162 '#title' => t('Require the user to input a name in the ecard form?'),
163 '#default_value' => variable_get('ecard_require_name', 0),
164 );
165 $form['misc']['ecard_require_message'] = array(
166 '#type' => 'checkbox',
167 '#title' => t('Require the user to input a message in the ecard form?'),
168 '#default_value' => variable_get('ecard_require_message', 0),
169 );
170 $form['misc']['fieldset'] = array(
171 '#type' => 'fieldset',
172 '#title' => t('Fieldset settings'),
173 );
174 $form['misc']['fieldset']['ecard_is_fieldset'] = array(
175 '#type' => 'checkbox',
176 '#title' => t('Put the ecard form in a fieldset?'),
177 '#default_value' => variable_get('ecard_is_fieldset', 1),
178 );
179 $form['misc']['fieldset']['ecard_is_fieldset_collapsible'] = array(
180 '#type' => 'checkbox',
181 '#title' => t('Is the fieldset collapsible?'),
182 '#default_value' => variable_get('ecard_is_fieldset_collapsible', 1),
183 );
184 $form['misc']['fieldset']['ecard_is_fieldset_collapsed'] = array(
185 '#type' => 'checkbox',
186 '#title' => t('Is the fieldset collapsed?'),
187 '#default_value' => variable_get('ecard_is_fieldset_collapsed', 0),
188 );
189 $form['array_filter'] = array('#type' => 'hidden');
190 return system_settings_form($form);
191 }
192
193 /**
194 * Function for making cron job.
195 */
196
197 function ecard_cron() {
198 // Convert cron days into timestamp.
199 $ecard_cron = variable_get('ecard_cron', '0') * 86400;
200 // Delete ecards older than setting.
201 if (!empty($ecard_cron)) {
202 db_query('DELETE FROM {ecard} WHERE send_time < %d', time() - $ecard_cron);
203 }
204 }
205
206 /**
207 * Function for making letter to send.
208 */
209
210 function _ecard_letter() {
211 $output = t(
212 "Hi,
213
214 %sender_name made an ecard for you.
215 At any time you may see your card by clicking this link:
216
217 %card_url
218
219 (if your email client doesn't allow you to click on the site link,
220 then just copy and paste the URL into your browser)
221
222 admin");
223 return $output;
224 }
225
226 /**
227 * Implementation of hook_nodeapi().
228 */
229
230 function ecard_nodeapi(&$node, $op, $teaser, $page) {
231 switch ($op) {
232 case 'view':
233 $types_to_ecard = variable_get('ecard_nodetypes', array());
234 if (!in_array($node->type, $types_to_ecard)) {
235 break;
236 }
237 //Add our form as a content item.
238 if ($teaser == FALSE) {
239 //No form if ecard view is set.
240 if ($node->content['ecard_view'] && variable_get('ecard_hide_send_view', 0)) {
241 return;
242 }
243 $node->content['ecard_form'] = array(
244 '#value' => drupal_get_form('ecard_form', $node),
245 '#weight' => 100,
246 );
247
248 }
249 }
250 }
251
252 /**
253 * Function to define the form.
254 */
255
256 function ecard_form($form_state, &$node) {
257
258 if (!ecard_form_access($node->uid)) {
259 return $form;
260 }
261 //Senders name.
262 $form['name'] = array(
263 '#title' => t('Your name'),
264 '#type' => 'textfield',
265 '#required' => variable_get('ecard_require_name', 0)
266 );
267 //Sending from.
268 $form['from_email'] = array(
269 '#title' => t('Your email'),
270 '#type' => 'textfield',
271 '#required' => TRUE
272 );
273
274 //Dsplay box to type recipients emails.
275 $form['to_email'] = array(
276 '#title' => t('E-mail(s) of recipient(s)'),
277 '#type' => 'textarea',
278 '#rows' => 3,
279 '#default_value' => '',
280 '#description' => t('You may enter multiple emails'),
281 '#required' => TRUE
282 );
283 //Display textarea to type message.
284 $form['message'] = array(
285 '#title' => t('Type your message'),
286 '#type' => 'textarea',
287 '#description' => t('Whatever you type here will be attached to ecard'),
288 '#required' => variable_get('ecard_require_message', 0)
289 );
290 //Filterformat for the text.
291 $form['filter'] = filter_form();
292 //Notification on pickup.
293 $form['notify']=array(
294 '#type' => 'checkbox',
295 '#title' => t('Notify me when the card is picked'),
296 '#default_value' => 0
297 );
298 $form['nid'] = array(
299 '#type' => 'value',
300 '#value' => $node->nid
301 );
302 $form['submit'] = array(
303 '#type' => 'submit',
304 '#value' => t('Send This Card'),
305 '#weight' => 100
306 );
307 if (variable_get('ecard_is_fieldset', 1)) {
308 //Make a fieldset.
309 $form_fieldset['block'] = array(
310 '#type' => 'fieldset',
311 '#title' => t('Send this ecard'),
312 '#collapsible' => variable_get('ecard_is_fieldset_collapsible', 1),
313 '#collapsed' => variable_get('ecard_is_fieldset_collapsed', 0),
314 $form
315 );
316 $form = $form_fieldset;
317 }
318 return $form;
319 }
320
321 /*
322 * Validation function for email syntax.
323 */
324
325 function ecard_validate_emails($form_id, &$form_state) {
326 if (valid_email_address($form_state['values']['from_email']) != TRUE) {
327 form_set_error('from_email', t('Your email address is invalid'));
328 }
329 $valid_emails = $failed_emails = array();
330 $emails = array_unique(split("[,\n\r]", $form_state['values']['to_email']));
331 foreach ($emails as $email) {
332 $email = trim($email);
333 if ($email) {
334 if (valid_email_address($email)) {
335 $valid_emails[] = $email;
336 }
337 else {
338 $failed_emails[] = $email;
339 }
340 }
341 }
342 if (count($failed_emails)) {
343 form_set_error('to_email', t('This email has an error "@email"', array('@email' => $failed_emails[0])));
344 }
345 return $valid_emails;
346 }
347
348 /*
349 * Validation function for the form if call email_import of validate emails on respective steps.
350 */
351
352 function ecard_form_validate($form_id, &$form_state) {
353 $emails = ecard_validate_emails($form_id, $form_state);
354 $count = count($emails);
355 $max_count = variable_get('max_count', 1000);
356 if ($count > $max_count) {
357 form_set_error('to_email', t('You are not allowed to send to more than !count emails , please delete few email address and send the card', array('!count' => $count)));
358 }
359 form_set_value(array('#parents' => array('valid_emails')), $emails, $form_state);
360 }
361
362 /**
363 * Submit handler for ecard_form.
364 */
365
366 function ecard_form_submit($form_id, &$form_state) {
367 unset($random);
368 global $base_url;
369 global $language;
370 $start = 0;
371 $timestamp = time();
372 $send = "y";
373 $notify = "n";
374 // Set the base variable from the clean url value.
375 if (variable_get('clean_url', 0)) {
376 $base = $base_url .'/';
377 }
378 else {
379 $base = $base_url .'/index.php?q=';
380 }
381
382 // Making a random variable.
383 // This loop generate a random and check whether it exist in database if not it will store that random else it will generate another one.
384 while (!$random) {
385 //Add some salt
386 $random = md5(microtime() + rand(0, 100));
387 $result = db_fetch_object(db_query("SELECT random FROM {ecard} WHERE random = '%s'", $random));
388 if ($result) unset($random);
389 }
390 $card_copy_url = $base .'ecard/view/'. $random;
391
392 $sql = 'INSERT INTO {ecard} (
393 random,
394 nid,
395 sender_name,
396 sender_email,
397 recp_mail,
398 message,
399 send_time,
400 send,
401 notify,
402 format
403 ) VALUES (
404 "%s",
405 %d,
406 "%s",
407 "%s",
408 "%s",
409 "%s",
410 %d,
411 "%s",
412 "%s",
413 %d
414 )';
415
416 //Making a copy for you else if you click any other recp link it will mail notification also there is some problem while we produce random.
417 db_query(
418 $sql,
419 $random,
420 $form_state['values']['nid'],
421 $form_state['values']['name'],
422 $form_state['values']['from_email'],
423 $form_state['values']['from_email'],
424 $form_state['values']['message'],
425 $timestamp,
426 $send,
427 $notify,
428 $form_state['values']['format']
429 );
430 $cardid = $random;
431
432 //iterate through each emails and send them and store a random number for each
433 foreach ($form_state['values']['valid_emails'] as $email) {
434 if ($form_state['values']['notify'] == 1) {
435 $notify = "y";
436 $cardid = $random . $start;//append start value to random number to get cardid
437 $start = $start + 1; // making some number in increment order
438 db_query(
439 $sql,
440 $cardid,
441 $form_state['values']['nid'],
442 $form_state['values']['name'],
443 $form_state['values']['from_email'],
444 $email,
445 $form_state['values']['message'],
446 $timestamp,
447 $send,
448 $notify,
449 $form_state['values']['format']
450 );
451 }
452 $params['card_url'] = $base .'ecard/view/'. $cardid;
453 $params['sender'] = $form_state['values']['name'];
454 $params['sendermail'] = $form_state['values']['from_email'];
455 $params['recipient'] = $email;
456 drupal_mail('ecard', 'ecard-mail', $email, $language, $params, $form_state['values']['from_email']);
457 }
458 drupal_set_message(t("Your message has been sent and a copy is stored for your reference.<br />you can view the copy <a href='") . $card_copy_url . t("'>here</a>"));
459 $params['card_copy'] = $card_copy_url;
460 drupal_mail('ecard', 'ecard-copy', $form_state['values']['from_email'], $language, $params, variable_get('site_mail', 'nobody@localhost'));
461 $form_state['redirect'] = 'ecard/thanks';
462 $form_state['nid'] = $node->nid;
463 }
464
465 /**
466 * Function for making copy url letter.
467 */
468
469 function _ecard_copy() {
470 $output = t(
471 "Hi,
472 Here is the copy of your message you send
473 %card_url
474
475 (if your email client doesn't allow you to click on the site link,
476 then just copy and paste the URL into your browser)
477 admin");
478 return $output;
479 }
480
481 /**
482 * Function for making notification letter.
483 */
484
485 function _ecard_notify() {
486 $output = t(
487 "Hi,
488
489 %recipient has picked your card today.
490 Here is the url to your card
491 %card_url
492
493 (if your email client doesn't allow you to click on the site link,
494 then just copy and paste the URL into your browser)
495
496 admin");
497 return $output;
498 }
499
500 /**
501 * Function to view the ecard , it also send an email to the sender to know the card is picked.
502 */
503
504 function ecard_view($arg1 = NULL) {
505 global $base_url;
506 // set the base variable from the clean url value
507 if (variable_get('clean_url', 0)) {
508 $base = $base_url .'/';
509 }
510 else {
511 $base = $base_url .'/index.php?q=';
512 }
513 if (isset($arg1)) {
514
515 $sql = '
516 SELECT
517 random,
518 nid,
519 message,
520 sender_name,
521 sender_email,
522 recp_mail,
523 notify,
524 format
525 FROM {ecard} AS n
526 WHERE n.random = "%s"';
527 $resource = db_query($sql, $arg1);
528 $result = db_fetch_object($resource);
529 if ($result != NULL) {
530 $node = node_load($result->nid);
531 $node->content['ecard_view'] = array(
532 '#value' => theme('ecard_message', check_markup($result->message, $result->format)),
533 '#weight' => 90,
534 );
535
536 $output = node_view($node, FALSE, TRUE, FALSE);
537
538 //Sending email if notify == yes.
539 if ($result->notify == y) {
540 $card_url = $base .'ecard/view/'. $result->random;
541
542 //Making message.
543 $site_email = variable_get('site_email', 'root@localhost');
544 $params['site'] = $site_email;
545 $params['card_url'] = $card_url;
546 $params['sender'] = $result->sender_name;
547 $params['recipient'] = $result->recp_mail;
548 $params['sender_email'] = $result->sender_email;
549 drupal_mail('ecard', 'ecard-view', $params['sender_email'], NULL, $params, $site_email);
550 db_query("UPDATE {ecard} SET notify='n' WHERE random='%s'", $result->random);
551 }
552 }
553 else {
554 return t("Your ecard id is not valid either it may be an error or it expired");
555 }
556 return $output;
557 }
558 else drupal_goto();
559 }
560
561 /**
562 * Implementation hook_mail().
563 */
564
565 function ecard_mail($key, &$message, $params) {
566 global $base_url;
567
568 $variables = array(
569 '%site' => variable_get('site_name', 'drupal'),
570 '%site_url' => $base_url,
571 '%site_mail' => variable_get('site_mail', 'root@localhost'),
572 '%card_url' => $params['card_url'],
573 '%sender' => $params['sender'],
574 '%sender_email' => $params['sendermail'],
575 '%recipient' => $params['recipient'],
576 );
577
578 switch ($key) {
579 case 'ecard-mail':
580 $body = strtr(variable_get('ecard_letter', _ecard_letter()), $variables);
581 $subject = strtr(variable_get('ecard_subject', 'An Ecard from %sender_name'), $variables);
582 break;
583 case 'ecard-copy':
584 $body = strtr(variable_get('ecard_copy', _ecard_copy()), $variables);
585 $subject = strtr(variable_get('ecard_copy_subject', 'Copy of your Ecard'), $variables);
586 break;
587 case 'ecard-view':
588 $body = strtr(variable_get('ecard_notify', _ecard_notify()), $variables);
589 $subject = strtr(variable_get('notify_subject', 'Your card has been picked by %recipient'), $variables);
590 break;
591 }
592
593 $message['subject'] = $subject;
594 $message['body'] = $body;
595 }
596
597 /**
598 * Function for the thankyou page.
599 * @TODO: Let the user setup a message or point to a site.
600 * The message needs token support.
601 */
602
603 function ecard_thanks() {
604 return t('Thanks, and have a nice day.');
605 }
606
607 function ecard_theme() {
608 return array(
609 'ecard_message' => array(
610 'arguments' => array('message'),
611 ),
612 );
613 }
614
615 function theme_ecard_message($message) {
616 return theme(
617 'fieldset',
618 array(
619 '#title' => t('Message'),
620 '#collapsible' => TRUE,
621 '#collapsed' => FALSE,
622 '#value' => $message)
623 );
624 }

  ViewVC Help
Powered by ViewVC 1.1.2