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

Contents of /contributions/modules/tellafriend/tellafriend.module

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


Revision 1.20 - (show annotations) (download) (as text)
Tue Oct 27 22:23:40 2009 UTC (4 weeks, 2 days ago) by thierrygd
Branch: MAIN
CVS Tags: DRUPAL-6--2-10, HEAD
Changes since 1.19: +2 -2 lines
File MIME type: text/x-php
no message
1 <?php
2 // $Id: tellafriend.module,v 1.3.2.5 2008/04/20 21:19:34 thierrygd Exp $
3
4 /**
5 * @file
6 * Enables "send to a friend" functionality
7 */
8
9 ini_set('SMTP', variable_get('tellafriend_server', 'localhost'));
10 // Users are not allowed to send more than x mails/hour:
11 define('TELLAFRIEND_DEFAULT_HOURLY_THRESHOLD', 10);
12 // By default, users are not allowed to include more than y e-mail addresses per request.
13 // This value can be changed via the tell-a-friend settings page.
14 define('TELLAFRIEND_DEFAULT_MAX_EMAIL_ADDRESSES', 10);
15
16 /**
17 * Implementation of hook_help().
18 */
19 function tellafriend_help($path, $arg) {
20 switch ($path) {
21 case 'admin/help#poll':
22 return t("Access page at: tellafriend");
23 case 'admin/modules#description':
24 return t("Allows you to create a Tell-a-Friend page to spread the word about your site.");
25 case 'admin/help#tellafriend':
26 global $base_url;
27 return t("Access <em>Tell a friend</em> page at: !path", array('!path' =>
28 l($base_url . urldecode('/?q=tellafriend'), 'tellafriend')));
29 }
30 }
31
32 /**
33 * Implementation of hook_perm().
34 */
35 function tellafriend_perm() {
36 return array('access tellafriend form', 'administer tellafriend form');
37 }
38
39 /**
40 * Implementation of hook_menu().
41 */
42 function tellafriend_menu() {
43 $items = array();
44
45 $items['admin/settings/tellafriend'] = array(
46 'title' => 'Tell a friend',
47 'description' => 'Control the contents of <em>Tell a friend</em> e-mail message.',
48 'page callback' => 'drupal_get_form',
49 'page arguments' => array('tellafriend_settings'),
50 'access arguments' => array('administer site configuration'),
51 'type' => MENU_NORMAL_ITEM,
52 );
53 $items['tellafriend'] = array(
54 'title' => variable_get('tellafriend_pagetitle', 'Tell a friend'),
55 'page callback' => 'drupal_get_form',
56 'page arguments' => array('tellafriend_page'),
57 'access arguments' => array('access tellafriend form'),
58 'type' => MENU_CALLBACK,
59 );
60 $items['tellafriend_block_form'] = array(
61 'title' => variable_get('tellafriend_blocktitle', 'Tell a friend'),
62 'page callback' => 'tellafriend_block_form',
63 'page arguments' => array('tellafriend_block_form'),
64 'access arguments' => array('access tellafriend block'),
65 'type' => MENU_CALLBACK,
66 );
67
68 return $items;
69 }
70
71 /**
72 * Implementation of hook_settings().
73 */
74 function tellafriend_settings() {
75
76 $form['tellafriend_general'] = array(
77 '#type' => 'fieldset',
78 '#title' => t('General settings'),
79 '#collapsible' => TRUE,
80 '#collapsed' => FALSE,
81 );
82 $form['tellafriend_general']['tellafriend_pagetitle'] = array(
83 '#type' => 'textfield',
84 '#title' => t('Page title'),
85 '#default_value' => variable_get('tellafriend_pagetitle', t('Tell a friend')),
86 '#size' => 70,
87 '#maxlength' => 200,
88 '#description' => t('Title of the <em>Tell a friend</em> page.'),
89 );
90 $form['tellafriend_general']['tellafriend_topmessage'] = array(
91 '#type' => 'textarea',
92 '#title' => t('Top Message'),
93 '#default_value' => variable_get('tellafriend_topmessage', t('Spread the word about !site by telling your friend(s) about it!', array('!site' => variable_get('site_name', 'Drupal')))),
94 '#size' => 70,
95 '#rows' => 5,
96 '#description' => t('A description or information shown above the <em>Tell a friend</em> form.'),
97 );
98 $form['tellafriend_general']['tellafriend_personal_message'] = array(
99 '#type' => 'textarea',
100 '#title' => t('Personal Message'),
101 '#default_value' => variable_get('tellafriend_personal_message', t('I found this great web site, which you should really check out!')),
102 '#size' => 70,
103 '#rows' => 5,
104 '#description' => t('A description or information shown above the <em>Tell a friend</em> form - Personal Message.'),
105 );
106 $form['tellafriend_general']['tellafriend_success'] = array(
107 '#type' => 'textfield',
108 '#title' => t('Success redirect path'),
109 '#default_value' => variable_get('tellafriend_success', variable_get('site_frontpage', 'node')),
110 '#size' => 70,
111 '#maxlength' => 200,
112 '#description' => t('This is where the script will redirect the user after the e-mail is successfully sent.'),
113 );
114 $form['tellafriend_general']['tellafriend_server'] = array(
115 '#type' => 'textfield',
116 '#title' => t('Server name'),
117 '#default_value' => variable_get('tellafriend_server', 'localhost'),
118 '#size' => 70,
119 '#maxlength' => 200,
120 '#description' => t('SMTP server name. Default is <em>localhost</em>.'),
121 );
122 $form['tellafriend_general']['tellafriend_hourly_threshold'] = array(
123 '#type' => 'textfield',
124 '#title' => t('Hourly Threshold'),
125 '#default_value' => variable_get('tellafriend_hourly_threshold', TELLAFRIEND_DEFAULT_HOURLY_THRESHOLD),
126 '#size' => 30,
127 '#maxlength' => 40,
128 '#description' => t('This is the maximum number of tell-a-friend requests per user, per hour.'),
129 );
130 $form['tellafriend_general']['tellafriend_address_limit'] = array(
131 '#type' => 'textfield',
132 '#title' => t('Address Limit'),
133 '#default_value' => variable_get('tellafriend_address_limit', TELLAFRIEND_DEFAULT_MAX_EMAIL_ADDRESSES),
134 '#size' => 30,
135 '#maxlength' => 40,
136 '#description' => t('This is the maximum number of email addresses that can be included per tell-a-friend request.'),
137 );
138
139 $form['tellafriend_block_options'] = array(
140 '#type' => 'fieldset',
141 '#title' => t('Block settings'),
142 '#collapsible' => TRUE,
143 '#collapsed' => FALSE,
144 );
145 $form['tellafriend_block_options']['tellafriend_block_title'] = array(
146 '#type' => 'textfield',
147 '#title' => t('Block Title'),
148 '#default_value' => variable_get('tellafriend_block_title', t("Spread the world...")),
149 '#size' => 70,
150 '#maxlength' => 128,
151 '#description' => t('This will be the title for the tellafriend block (if activated : go to admin/build/block to activate or not the block).'),
152 );
153 $form['tellafriend_block_options']['tellafriend_block_style'] = array(
154 '#type' => 'radios',
155 '#title' => t('Block style'),
156 '#default_value' => variable_get('tellafriend_block_style', 0),
157 '#options' => array(0 => t('Link (Default)'), 1 => t('TextField(s) (option not avalaible yet)')),
158 '#description' => t('Select the visual style of the Block. "Link" will display a link to a page to be used by the user to send invitations, "TextField(s)" will display textfields boxes which user can fill with email(s) directly.'),
159 );
160 $form['tellafriend_block_options']['tellafriend_block_linklabel'] = array(
161 '#type' => 'textfield',
162 '#title' => t('Link label'),
163 '#default_value' => variable_get('tellafriend_block_linklabel', t("Tell a friend...")),
164 '#size' => 70,
165 '#maxlength' => 128,
166 '#description' => t('This will be the link which will appear in the tellafriend block, if "Link" display option is choosed.'),
167 );
168 $form['tellafriend_block_options']['how_many_email_textfields']= array(
169 '#title' => t("How many e-mail textfields?"),
170 '#type' => 'textfield',
171 '#required' => TRUE,
172 '#default_value' => variable_get('how_many_email_textfields', 2),
173 '#description' => t('This is the number of e-mail fields which will be generated on the tellafriend block, if TextField(s) display option is choosed.'),
174 '#size' => 2,
175 '#maxlength' => '2',
176 );
177
178 $form['tellafriend_import'] = array(
179 '#type' => 'fieldset',
180 '#title' => t('Allow import of emails from addressbooks (Yahoo, Gmail, Plaxo, ....)'),
181 '#collapsible' => TRUE,
182 '#collapsed' => FALSE,
183 '#description' => t('Allow import emails from addressbooks (Yahoo, Gmail, Plaxo, ....). (Not avalaible yet)'),
184 );
185 $form['tellafriend_import']['tellafriend_import_yahoo'] = array(
186 '#type' => 'checkbox',
187 '#title' => t('From Yahoo'),
188 '#return_value' => 1,
189 '#default_value' => variable_get('tellafriend_import_yahoo', 0),
190 );
191 $form['tellafriend_import']['tellafriend_import_gmail'] = array(
192 '#type' => 'checkbox',
193 '#title' => t('From Gmail'),
194 '#return_value' => 1,
195 '#default_value' => variable_get('tellafriend_import_gmail', 0),
196 );
197 $form['tellafriend_import']['tellafriend_import_plaxo'] = array(
198 '#type' => 'checkbox',
199 '#title' => t('From Plaxo'),
200 '#return_value' => 1,
201 '#default_value' => variable_get('tellafriend_import_plaxo', 0),
202 );
203 $form['tellafriend_import']['tellafriend_import_linkedin'] = array(
204 '#type' => 'checkbox',
205 '#title' => t('From LinkedIn'),
206 '#return_value' => 1,
207 '#default_value' => variable_get('tellafriend_import_linkedin', 0),
208 );
209 $form['tellafriend_import']['tellafriend_import_facebook'] = array(
210 '#type' => 'checkbox',
211 '#title' => t('From Facebook'),
212 '#return_value' => 1,
213 '#default_value' => variable_get('tellafriend_import_facebook', 0),
214 );
215
216 $form['tellafriend_mail'] = array(
217 '#type' => 'fieldset',
218 '#title' => t('E-mail template'),
219 '#collapsible' => TRUE,
220 '#collapsed' => FALSE,
221 );
222 $form['tellafriend_mail']['tellafriend_fromaddress'] = array(
223 '#type' => 'textfield',
224 '#title' => t('From address'),
225 '#default_value' => variable_get('tellafriend_fromaddress', ''),
226 '#size' => 70,
227 '#maxlength' => 200,
228 '#description' => t("This is the address the e-mail will come from. Site e-mail address is %sitemail. Leave blank to use sender's e-mail address", array('%sitemail' => variable_get('site_mail', ini_get('sendmail_from')))),
229 );
230 $form['tellafriend_mail']['tellafriend_subject'] = array(
231 '#type' => 'textfield',
232 '#title' => t('Email Subject'),
233 '#default_value' => variable_get('tellafriend_subject', t('!sendername has invited you to !sitename!')),
234 '#size' => 70,
235 '#maxlength' => 200,
236 '#description' => t('This is the e-mail subject. Available placeholders: <em>!sitename, !sitelink, !sitemail, !sendername, !sendermail</em>.'),
237 );
238 $form['tellafriend_mail']['tellafriend_message'] = array(
239 '#type' => 'textarea',
240 '#title' => t('Message Text'),
241 '#default_value' => variable_get('tellafriend_message', t('You are invited to check out !sitename at !sitelink')),
242 '#cols' => 70,
243 '#rows' => 5,
244 '#description' => t('This is the text of the sent message. Use plain text only. Available placeholders: <em>!sitename, !sitelink, !sitemail, !sendername, !sendermail</em>.'),
245 );
246 $form['tellafriend_mail']['tellafriend_closing'] = array(
247 '#type' => 'textfield',
248 '#title' => t('Closing'),
249 '#default_value' => variable_get('tellafriend_closing', t('Sincerely,')),
250 '#size' => 70,
251 '#maxlength' => 200,
252 '#description' => t('Love, Sincerely, etc...'),
253 );
254
255 return system_settings_form($form);
256 }
257
258 /**
259 * Implementation of hook_block
260 * - Block lists N upcoming birthdays
261 * - Block lists birthdays in N days
262 *
263 * @param $op the operation that is being requested. This defaults to 'list', which indicates that the method should
264 * return which blocks are available.
265 * @param $delta the specific block to display. This is actually the offset into an array.
266 * @return one of two possibilities. The first is an array of available blocks. The other is an array containing a
267 *
268 */
269 function tellafriend_block($op = 'list', $delta = 0) {
270 // The $op parameter determines what piece of information is being requested.
271 if ($op == 'list') {
272 // If $op is "list", we just need to return a list of block descriptions. This
273 // is used to provide a list of possible blocks to the administrator.
274 $blocks[0]['info'] = t("Tell a friend");
275 return $blocks;
276 }
277 else if ($op == 'save' && $delta == 0) {
278 variable_set('tellafriend_block_title', $edit['tellafriend_block_title']);
279 }
280 else if ($op == 'view') {
281 switch ($delta) {
282 case 0:
283 $block = array();
284 if (user_access('access tellafriend form')) {
285 //drupal_set_message('option = ' . variable_get('tellafriend_block_style'), 'error');
286
287 $block['subject'] = filter_xss_admin(variable_get('tellafriend_block_title', t("Spread the world...")));
288 if (variable_get('tellafriend_block_style', 0) == '0') { //Link
289 $blockContent .= '<ul><li class="leaf">' . l(t(variable_get('tellafriend_block_linklabel', t("Tell a friend..."))), 'tellafriend') . '</li></ul>';
290 $block['content'] = $blockContent;
291 }
292 else { //email TextField(s)
293 $block['content'] = drupal_get_form('tellafriend_block_form');
294 }
295 }
296 }
297 return $block;
298 }
299 }
300
301
302 /**
303 * Generate the tellafriend block form with email TextFields
304 *
305 * @return
306 * A form definition.
307 */
308 function tellafriend_block_form() {
309 global $user;
310
311 $form['tellafriend'] = array(
312 '#value' => t('Recommend @site-name to:', array('@site-name' => variable_get('site_name', t('Drupal')))),
313 );
314
315 for ($i = 0; $i < variable_get('how_many_email_textfields', 2); $i++) {
316 $form['tellafriend_addresses'][] = array(
317 '#type' => 'textfield',
318 '#size' => 20,
319 '#name' => 'to[]',
320 );
321 }
322
323 $form['submit'] = array(
324 '#type' => 'submit',
325 '#value' => t('Tell now'),
326 );
327
328 return $form;
329 }
330
331
332 /**
333 * Generates the form for the page
334 */
335 function tellafriend_page(&$form_state) {
336 global $user;
337 global $base_url;
338 $placeholder_values = array('!sitename' => variable_get('site_name', 'Drupal'),'!sitelink' => $base_url,'!sitemail' => variable_get('site_mail', ini_get('sendmail_from')), '!sendername' => $form_values['tellafriend_name'], '!sendermail' => $form_values['tellafriend_email']);
339
340 $form['#token'] = $user->name . $user->mail;
341 $form['tellafriend_information'] = array('#value' => filter_xss_admin(variable_get('tellafriend_topmessage', t('Spread the word about !site by telling your friend(s) about it!', array('!site' => variable_get('site_name', 'Drupal'))))));
342 $form['tellafriend_name'] = array(
343 '#type' => 'textfield',
344 '#title' => t('Your name'),
345 '#default_value' => ($user->uid ? $user->name : ''),
346 '#size' => 70,
347 '#maxlength' => 255,
348 '#required' => TRUE,
349 '#description' => t('Your name as it will appear in the sent e-mail.'),
350 );
351 $form['tellafriend_email'] = array(
352 '#type' => 'textfield',
353 '#title' => t('Your e-mail address'),
354 '#default_value' => ($user->uid ? $user->mail : ''),
355 '#size' => 70,
356 '#maxlength' => 255,
357 '#required' => TRUE,
358 '#description' => t('Your e-mail address as it will appear in the sent e-mail.'),
359 );
360 $form['tellafriend_addresses'] = array(
361 '#type' => 'textfield',
362 '#title' => t("Your friend's e-mail address(es)"),
363 '#size' => 70,
364 '#maxlength' => 255,
365 '#required' => TRUE,
366 '#description' => t('Separate multiple e-mail addresses with commas.'),
367 );
368 $form['tellafriend_personal'] = array(
369 '#type' => 'textarea',
370 '#title' => t('Your Personal message'),
371 '#default_value' => variable_get('tellafriend_personal_message', t('I found this great web site, which you should really check out!')),
372 '#cols' => 70,
373 '#rows' => 5,
374 '#description' => t('Add your personal message to the e-mail.'),
375 );
376 $form['tellafriend_copy'] = array(
377 '#type' => 'checkbox',
378 '#title' => t('Send yourself a copy.'),
379 );
380 $form['tellafriend_message'] = array('#value' => filter_xss_admin("<strong>".t('Message to be sent').":</strong><br />" . nl2br(t(variable_get('tellafriend_message', 'You are invited to check out !sitename at !sitelink'), $placeholder_values)) . '<br/>'));
381
382 $form['submit'] = array('#type' => 'submit',
383 '#value' => t('Tell now!'),
384 );
385
386 return $form;
387 }
388
389 /**
390 * Implementation of hook_validate().
391 */
392 function tellafriend_page_validate($form, &$form_state) {
393 $tellafriend_personal = $form_state['values']['tellafriend_personal'];
394
395 if (!valid_email_address($form_state['values']['tellafriend_email'])) {
396 form_set_error("tellafriend_email", t('Email address is not valid!'));
397 }
398
399 $addresses = explode(",", $form_state['values']['tellafriend_addresses']);
400
401 if(count($addresses) > variable_get('tellafriend_address_limit', TELLAFRIEND_DEFAULT_MAX_EMAIL_ADDRESSES)) {
402 form_set_error("tellafriend_addresses",
403 t('You have exceeded the maximum number of %number email addresses per request.',
404 array('%number' => variable_get('tellafriend_address_limit', TELLAFRIEND_DEFAULT_MAX_EMAIL_ADDRESSES))));
405 }
406
407 foreach ($addresses as $mail) {
408 $sendaddress = stripslashes(trim($mail));
409
410 if (!valid_email_address($sendaddress)) {
411 $count++;
412 $invalid .= $sendaddress .' ';
413 }
414 if ($invalid != '') {
415 form_set_error("tellafriend_addresses", t(format_plural(count($count), '@invalid is invalid send-to e-mail addresses.', '@invalid are invalid send-to e-mail addresses.'), array('@invalid' => $invalid)));
416 }
417 }
418
419 //Check that flood control has not been surpassed
420 $my_threshold = variable_get('tellafriend_hourly_threshold', TELLAFRIEND_DEFAULT_HOURLY_THRESHOLD);
421 if (!flood_is_allowed('tellafriend', $my_threshold)) {
422 $tellafriend_validated = 1;
423 form_set_error('', t("You can't make more than %number mail requests per hour. Please try again later.", array('%number' => variable_get('tellafriend_hourly_threshold', TELLAFRIEND_DEFAULT_HOURLY_THRESHOLD))));
424 watchdog('mail', '%name has attempted to surpass the flood control for tellafriend.module', array('%name' => $user->name));
425 }
426 }
427
428 /**
429 * Implementation of hook_submit().Process the personal contact page form submission.
430 */
431 function tellafriend_page_submit($form, &$form_state) {
432 global $base_url;
433 global $language;
434
435 $placeholder_values = array('!sitename' => variable_get('site_name', 'Drupal'),'!sitelink' => $base_url,'!sitemail' => variable_get('site_mail', ini_get('sendmail_from')), '!sendername' => $form_state['values']['tellafriend_name'], '!sendermail' => $form_state['values']['tellafriend_email']);
436 $closing = variable_get('tellafriend_closing', 'Sincerely,');
437 //$from = variable_get('tellafriend_fromaddress', $form_state['values']['tellafriend_email']);
438 $from = $form_state['values']['tellafriend_name']. "<".$form_state['values']['tellafriend_email']. ">";
439 $addresses = explode(",", $form_state['values']['tellafriend_addresses']);
440 $subject = stripslashes(t(variable_get('tellafriend_subject', t('!sendername has invited you to !sitename!')), $placeholder_values));
441
442 if(strlen($form_state['values']['tellafriend_personal'])) {
443 $body .= t("\nPersonal Message: ") . $form_state['values']['tellafriend_personal'] . "\n----------------------\n\n";
444 }
445 $body .= t(variable_get('tellafriend_message', t('You are invited to check out !sitename at !sitelink')), $placeholder_values) ."\n$closing \n". $form_state['values']['tellafriend_name'] ." - ". $form_state['values']['tellafriend_email'];
446
447 foreach ($addresses as $key => $mail) {
448 $sendaddress[$key] = stripslashes(trim($mail));
449
450 $account = array(); // Set this as needed
451 $object = array(); // Replace this as needed
452 $context['subject'] = $subject;
453 $context['body'] = $body;
454 $params = array('account' => $account, 'object' => $object, 'context' => $context);
455
456 $params = array(
457 'from' => $from,
458 'subject' => $subject,
459 'body' => $body,
460 );
461
462 drupal_mail('tellafriend', 'tellafriend-page-mail', $sendaddress[$key], $language, $params, $from);
463 }
464 if ($form_state['values']['tellafriend_copy']) {
465 $account = array(); // Set this as needed
466 $object = array(); // Replace this as needed
467 $context['subject'] = $subject;
468 $context['body'] = $body;
469 $params = array('account' => $account, 'object' => $object, 'context' => $context);
470
471 $params = array(
472 'from' => $from,
473 'subject' => $subject,
474 'body' => $body,
475 );
476
477 drupal_mail('tellafriend', 'tellafriend-page-copy', $form_state['values']['tellafriend_email'], $language, $params, $from);
478 }
479
480 flood_register_event('tellafriend');
481
482 $addresses = implode(', ', $sendaddress);
483 watchdog('mail', '%sender sent an e-mail to %recepient using <em>Tell a friend</em> form.', array('%sender' => $form_state['values']['tellafriend_name'], '%recepient' => $addresses));
484 drupal_set_message(t('Thank you! Your message has been sent to: !recepient', array('!recepient' => $addresses)));
485 drupal_goto(variable_get('tellafriend_success', variable_get('site_frontpage', 'node')));
486 }
487
488 //To be done : separate the send action from the function tellafriend_page_submit
489 function tellafriend_send_mail() {
490 }
491
492 /**
493 * Implementation of hook_mail().
494 */
495 function tellafriend_mail($key, &$message, $params) {
496 $message['from'] .= $params['from'];
497 $message['subject'] = $params['subject'];
498 $message['body'] = $params['body'];
499 $message['headers'] = array_merge($message['headers'], isset($params['headers']) ? $params['headers'] : array());
500 $message['headers']['MIME-Version'] = '1.0';
501 $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
502 }
503

  ViewVC Help
Powered by ViewVC 1.1.2