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

Contents of /contributions/modules/emailmarketer/emailmarketer.module

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


Revision 1.1 - (show annotations) (download) (as text)
Sun Sep 2 17:26:34 2007 UTC (2 years, 2 months ago) by claudiucristea
Branch: MAIN
CVS Tags: DRUPAL-5--0-1, HEAD
File MIME type: text/x-php
Publish to repository
1 <?php
2 /* $Id$ */
3
4 /**
5 * @file
6 * This module allows visitors to subscribe to Nesox Email Marketer (http://www.nesox.com/) newsletters.
7 */
8
9 // Load some helper functions
10 require_once('emailmarketer.inc');
11
12 define('EMAILMARKETER_TABLE', 'emailmarketer');
13 define('EMAILMARKETER_QUEUE', 'emailmarketer_queue');
14
15 /**
16 * Display help and module information
17 *
18 * @param $section
19 * Which section of the site we're displaying help
20 *
21 * @return
22 * Help text for section
23 */
24 function emailmarketer_help($section = '') {
25 $output = '';
26 switch ($section) {
27 case "admin/help#emailmarketer":
28 $output = '<p>'. t('This module add a form to Drupal that allows visitors subscribe to Nesox Email Marketer newsletters. For more informations about Nesox Email Marketer visit <a href="http://www.nesox.com/">Nesox Website</a>'). '</p>';
29 break;
30 case "admin/settings/emailmarketer":
31 $output = '<p>' . t('Below you can find the Nesox Email Marketer newsletters defined on your site. Visitors can subscribe for each of these lists depending on their roles. Each subscription list can be configured by clicking the link \'configure\'. To add a new subscription point click on <a href="@add">Add Newsletter</a>.', array('@add' => url('admin/settings/emailmarketer'))) . '</p>';
32 }
33 return $output;
34 }
35
36 /**
37 * Valid permissions for this module
38 *
39 * @return array
40 * An array of valid permissions for the person module
41 */
42 function emailmarketer_perm() {
43 return array('administer email marketer');
44 }
45
46 /**
47 * Menu items provided by module
48 *
49 * @return array
50 * An array of menu items
51 */
52 function emailmarketer_menu($may_cache) {
53 global $emailmarketer_lists;
54 $items = array();
55 if (!isset($emailmarketer_lists)) {
56 $emailmarketer_lists = emailmarketer_list(NULL, true);
57 }
58 if ($may_cache) {
59 $items[] = array(
60 'path' => 'admin/settings/emailmarketer',
61 'title' => t('Email Marketer'),
62 'description' => t('Allow visitors to subscribe to Nesox Email Marketer.'),
63 'callback' => 'drupal_get_form',
64 'callback arguments' => array('emailmarketer_admin_overview', $emailmarketer_lists),
65 'access' => user_access('administer email marketer'),
66 'type' => MENU_NORMAL_ITEM,
67 );
68 $items[] = array(
69 'path' => 'admin/settings/emailmarketer/list',
70 'title' => t('List'),
71 'description' => t('View the list of Email Merketer lists'),
72 'callback' => 'emailmarketer_admin_overview',
73 'access' => user_access('administer email marketer'),
74 'type' => MENU_DEFAULT_LOCAL_TASK,
75 );
76 $items[] = array(
77 'path' => 'admin/settings/emailmarketer/setup',
78 'title' => t('Setup'),
79 'callback' => 'drupal_get_form',
80 'callback arguments' => array('emailmarketer_admin_settings'),
81 'type' => MENU_LOCAL_TASK,
82 'weight' => 2,
83 'access' => user_access('administer email marketer'),
84 );
85 $items[] = array(
86 'path' => 'admin/settings/emailmarketer/add',
87 'title' => t('Add Newsletter'),
88 'callback' => 'drupal_get_form',
89 'callback arguments' => array('emailmarketer_newsletter_form'),
90 'type' => MENU_LOCAL_TASK,
91 'weight' => 1,
92 'access' => user_access('administer email marketer'),
93 );
94 $items[] = array(
95 'path' => 'subscribe',
96 'title' => t(variable_get('emailmarketer_title', 'Our Newsletters')),
97 'description' => t('Allow visitors to subscribe to Nesox Email Marketer.'),
98 'callback' => 'emailmarketer_page',
99 'callback arguments' => array($emailmarketer_lists),
100 'access' => user_access('administer email marketer') || count($emailmarketer_lists),
101 'type' => MENU_SUGGESTED_ITEM,
102 );
103 $items[] = array(
104 'path' => 'subscribe/confirm',
105 'title' => t('Confirm your subscription'),
106 'description' => t('You need to confirm your e-mail address.'),
107 'callback' => 'drupal_get_form',
108 'callback arguments' => array('emailmarketer_confirm', $emailmarketer_lists),
109 'access' => user_access('administer email marketer') || count($emailmarketer_lists),
110 'type' => MENU_CALLBACK,
111 );
112 }
113 else {
114 if (arg(0) == 'admin' && arg(1) == 'settings' && arg(2) == 'emailmarketer' && is_numeric(arg(3))) {
115 if (isset($emailmarketer_lists[arg(3)])) {
116 $items[] = array(
117 'path' => 'admin/settings/emailmarketer/'. arg(3),
118 'title' => t("Edit !list [!email]", array('!list' => $emailmarketer_lists[arg(3)]['name'], '!email' => $emailmarketer_lists[arg(3)]['email'])),
119 'callback' => 'drupal_get_form',
120 'callback arguments' => array('emailmarketer_newsletter_form', $emailmarketer_lists[arg(3)]),
121 'type' => MENU_CALLBACK,
122 'access' => user_access('administer email marketer')
123 );
124 }
125 }
126 if (arg(0) == 'admin' && arg(1) == 'settings' && arg(2) == 'emailmarketer' && arg(3) == 'delete' && is_numeric(arg(4))) {
127 if (isset($emailmarketer_lists[arg(4)])) {
128 $items[] = array(
129 'path' => 'admin/settings/emailmarketer/delete/'. arg(4),
130 'title' => t('Delete Email Marketer newsletter'),
131 'callback' => 'drupal_get_form',
132 'callback arguments' => array('emailmarketer_admin_delete', $emailmarketer_lists[arg(4)]),
133 'type' => MENU_CALLBACK,
134 'access' => user_access('administer email marketer')
135 );
136 }
137 }
138 if (arg(0) == 'subscribe' && is_numeric(arg(1))) {
139 $items[] = array(
140 'path' => 'subscribe/'. arg(1),
141 'title' => t("Subscribe to !list", array('!list' => $emailmarketer_lists[arg(1)]['name'], '!email' => $emailmarketer_lists[arg(1)]['email'])),
142 'callback' => 'drupal_get_form',
143 'callback arguments' => array('emailmarketer_subscribe', $emailmarketer_lists[arg(1)]),
144 'type' => MENU_CALLBACK,
145 'access' => (user_access('administer email marketer') || count($emailmarketer_lists[arg(1)])) && isset($emailmarketer_lists[arg(1)]),
146 );
147 }
148 if (arg(0) == 'subscribe' && arg(1) == 'confirm' && preg_match("/[0-9a-z]{32,32}/", arg(2))) {
149 $items[] = array(
150 'path' => 'subscribe/confirm/'. arg(2),
151 'title' => t('Confirmation'),
152 'callback' => 'emailmarketer_confirmation',
153 'callback arguments' => array($emailmarketer_lists),
154 'type' => MENU_CALLBACK,
155 'access' => user_access('administer email marketer') || count($emailmarketer_lists),
156 );
157 }
158 }
159 return $items;
160
161 }
162
163 /**
164 * Form function that allows setting of module general preferences.
165 *
166 * @return array
167 * A Drupal form array
168 */
169 function emailmarketer_admin_settings() {
170 $form['emailmarketer_title'] = array(
171 '#type' => 'textfield',
172 '#title' => t('The title of the Email Marketer subscribe block or page'),
173 '#description' => t('Enter here the text that you want to serve as title in the Email Marketer block or page.'),
174 '#default_value' => variable_get('emailmarketer_title', 'Our Newsletters'),
175 '#maxlength' => 128
176 );
177 $form['message']['emailmarketer_message'] = array(
178 '#type' => 'textarea',
179 '#title' => t('Message displayed on Email Marketer subscribe block and page'),
180 '#description' => t('Enter here the text that you want to appear in the Email Marketer block and page.'),
181 '#default_value' => variable_get('emailmarketer_message', '<p>Select a newsletter to subscribe:</p>')
182 );
183 $form['message']['format'] = filter_form($node->format);
184 return system_settings_form($form);
185 }
186
187 /**
188 * Generate the admin list of available newsletters
189 *
190 * @param $lists
191 * An array of the available newsletters
192 *
193 * @return array
194 * A Drupal form array
195 */
196 function emailmarketer_admin_overview($lists = NULL) {
197 if (!count($lists)) {
198 drupal_set_message(t('There is no Email Marketer newsletter configured. Please add one by filling the form below.'));
199 drupal_goto('admin/settings/emailmarketer/add');
200 }
201 foreach ($lists as $id => $list) {
202 if (empty($list['roles'])) {
203 $roles = array();
204 }
205 else {
206 $roles = explode('|', trim($list['roles'], '|'));
207 }
208 $effective_roles = array();
209 foreach (user_roles() as $rid => $role_name) {
210 if (in_array($rid, $roles)) {
211 $effective_roles[] = $role_name;
212 }
213 }
214
215 $effective_roles = empty($effective_roles) ? t('All roles may subscribe') : implode(', ', $effective_roles);
216
217 $form[$list['email']]['id'] = array('#value' => $id);
218 $form[$list['email']]['name'] = array('#value' => $list['name']);
219 $form[$list['email']]['roles'] = array('#value' => $effective_roles);
220 $form[$list['email']]['configure'] = array('#value' => l(t('configure'), 'admin/settings/emailmarketer/' . $id));
221 $form[$list['email']]['delete'] = array('#value' => l(t('delete'), 'admin/settings/emailmarketer/delete/' . $id));
222 }
223 return $form;
224 }
225
226 /**
227 * Theme function for the admin list of newsletters
228 *
229 * @param $form
230 * A form array
231 *
232 * @return text
233 * The HTML content of the form
234 */
235 function theme_emailmarketer_admin_overview($form) {
236 $rows = array();
237 foreach ($form as $email => $element) {
238 if (isset($element['id']) && is_array($element['id'])) {
239 $rows[] = array(
240 check_plain($element['name']['#value']),
241 check_plain($email),
242 check_plain($element['roles']['#value']),
243 drupal_render($element['configure']),
244 drupal_render($element['delete'])
245 );
246 unset($form[$name]);
247 }
248 }
249 $header = array(t('Name'), t('E-mail'), t('Roles'), array('data' => t('Operations'), 'colspan' => 2));
250 $output = theme('table', $header, $rows);
251 return $output;
252 }
253
254 /**
255 * The form where to add or edit a newsletter
256 *
257 * @param $list
258 * A newsletter record
259 *
260 * @return array
261 * A Drupal form array
262 */
263 function emailmarketer_newsletter_form($list = NULL) {
264 $field_opts = array('0' => t('Not used'), '1' => t('Optional'), '2' => t('Required'));
265 $fields = emailmarketer_fields();
266 if ($list && $list['fields_fixed'] != '') {
267 $list_fields = unserialize($list['fields_fixed']);
268 }
269 else {
270 $list_fields = array();
271 }
272 $form['general'] = array(
273 '#type' => 'fieldset',
274 '#title' => t('General list informations'),
275 '#description' => t('Enter general informations for this Email Marketer newsletter.'),
276 '#collapsible' => TRUE,
277 '#collapsed' => FALSE
278 );
279 $form['general']['email'] = array(
280 '#type' => 'textfield',
281 '#default_value' => $list['email'],
282 '#title' => t('List e-mail'),
283 '#description' => t('Enter the e-mail address of the list.'),
284 '#maxlength' => 255,
285 '#required' => TRUE
286 );
287 $form['general']['name'] = array(
288 '#type' => 'textfield',
289 '#default_value' => $list['name'],
290 '#title' => t('Email Marketer newsletter title'),
291 '#description' => t('Enter a name or title for this Email Marketer list.'),
292 '#required' => TRUE,
293 '#maxlength' => 255
294 );
295 $form['general']['version'] = array(
296 '#type' => 'textfield',
297 '#default_value' => $list['version'] ? $list['version'] : '1.80',
298 '#title' => t('Nesox Email Marketer version'),
299 '#description' => t('Enter the version of Nesox Email Marketer software used to process this subscription requests.'),
300 '#maxlength' => 15
301 );
302 $form['general']['activated'] = array(
303 '#type' => 'select',
304 '#title' => t('Subscription policy'),
305 '#default_value' => $list['activated'] ? $list['activated'] : 'True',
306 '#options' => array('True' => t('Direct subscription'), 'False' => t('Queue subscription')),
307 '#description' => t('Set the subscription policy. If "Queue subscription" is selected then the list administrator approval is required. Otherwise the address is added to the list.')
308 );
309 $form['subscribe'] = array(
310 '#type' => 'fieldset',
311 '#title' => t('Subscribe informations'),
312 '#description' => t('Enter subscribe informations for this Email Marketer newsletter.'),
313 '#collapsible' => TRUE,
314 '#collapsed' => TRUE
315 );
316 $form['subscribe']['email_subscribe'] = array(
317 '#type' => 'textfield',
318 '#default_value' => $list['email_subscribe'],
319 '#title' => t('Subscribe e-mail'),
320 '#description' => t('Enter the e-mail address where the subscribing requests will be delivered. If this field is left blank then the list e-mail will be used for subscribe requests.'),
321 '#maxlength' => 255
322 );
323 $form['subscribe']['subject_subscribe'] = array(
324 '#type' => 'textfield',
325 '#default_value' => $list['subject_subscribe'],
326 '#title' => t('Subscribe e-mail subject'),
327 '#description' => t('Enter the text to be contained in the subject line of the e-mail request for subscribing to this newsletter.'),
328 '#maxlength' => 255
329 );
330 $form['subscribe']['welcome_container'] = array(
331 '#type' => 'fieldset',
332 '#title' => t('Customize the welcome message'),
333 '#description' => '<p >' . t('Customize the way that the welcome message is looking. You can define the Plain Text and the HTML part as well. If both parts are missed then the subscriber will not receivw any welcome message.') . '</p><ul><li>' . t('You can insert in the message Email Marketer fields as they are describet above. The fields will be eclosed in sqare brackets "[" and "]". Make sure that those fields are enabled using this form before use them. Example: [FullName], [LastName], [Gender], ...') . '</li><li>' . t('Additional variables can be used in the welcome message these are: [ListEmail], [ListName], [SubscribeEmail], [UnsubscribeEmail], [SubscribeSubject], [UnsubscribeSubject], [SubscribePolicyMessage], [UnsubscribePolicyMessage], [SiteName], [SiteURL].') . '</li><li>' . t('You must insert the confirmation link in the body of the message. The confirmation link is used to verify the subscriber e-mail. The confirmation link variable is [ConfirmationLink]. Additionally you may include the confirmation code [ConfirmationCode] that can be used as alternative for form validation.') . '</li></ul>',
334 '#collapsible' => TRUE,
335 '#collapsed' => TRUE,
336 );
337 $form['subscribe']['welcome_container']['subscribe_text'] = array(
338 '#type' => 'textarea',
339 '#title' => t('Plain text part'),
340 '#default_value' => $list['subscribe_text'] ? $list['subscribe_text'] : t("\nDear [FullName],\n\nThank you for subscribing to our newsletter: [ListName] [[ListEmail]].\n\nIn order to confirm you subscription you must visit the link:\n\n[ConfirmationLink]\n\nIf you have problems visiting that page, copy the link into your browser. You can also paste the confirmation code provided below into the confirmation form.\n\nConfirmation code: [ConfirmationCode]\nConfirmation form: [ConfirmationForm]\n\nThe confirmation link expires after 2 days. If you don't confirm your subscription within this period you must renew your subscription.\n\nSubscribe policy: [SubscribePolicyMessage]\n\nUnsubscribe policy: [UnsubscribePolicyMessage]\n\nBest regards,\n[SiteName]\n[SiteURL]\n"),
341 '#description' => t('The Plain text part of the e-mail welcome message.'),
342 '#maxlength' => 1024
343 );
344 $form['subscribe']['welcome_container']['subscribe_html'] = array(
345 '#type' => 'textarea',
346 '#title' => t('HTML part'),
347 '#default_value' => $list['subscribe_html'] ? $list['subscribe_html'] : t("<p>Dear [FullName],</p>\n\n<p>Thank you for subscribing to our newsletter: [ListName] [[ListEmail]].</p>\n\n<p>In order to confirm you subscription you must visit the link:</p>\n\n<p>[ConfirmationLink]</p>\n\n<p>If you have problems visiting that page, copy the link into your browser. You can also paste the confirmation code provided below into the confirmation form.</p>\n\n<p>Confirmation code: [ConfirmationCode]<br />\nConfirmation form: [ConfirmationForm]</p>\n\n<p>The confirmation link expires after 2 days. If you don't confirm your subscription within this period you must renew your subscription.</p>\n\n<p>Subscribe policy: [SubscribePolicyMessage]</p>\n\n<p>Unsubscribe policy: [UnsubscribePolicyMessage]</p>\n\n<p>Best regards,<br />\n[SiteName]<br />\n[SiteURL]</p>\n"),
348 '#description' => t('The HTML part of the e-mail welcome message.'),
349 '#maxlength' => 1024
350 );
351 $form['unsubscribe'] = array(
352 '#type' => 'fieldset',
353 '#title' => t('Unsubscribe informations'),
354 '#description' => t('Enter unsubscribe informations for this Email Marketer newsletter.'),
355 '#collapsible' => TRUE,
356 '#collapsed' => TRUE
357 );
358 $form['unsubscribe']['email_unsubscribe'] = array(
359 '#type' => 'textfield',
360 '#default_value' => $list['email_unsubscribe'],
361 '#title' => t('Unsubscribe e-mail'),
362 '#description' => t('Enter the e-mail address where the unsubscribing requests will be delivered. If this field is left blank then the list e-mail will be used for unsubscribe requests.'),
363 '#maxlength' => 255
364 );
365 $form['unsubscribe']['subject_unsubscribe'] = array(
366 '#type' => 'textfield',
367 '#default_value' => $list['subject_unsubscribe'],
368 '#title' => t('Unsubscribe e-mail subject'),
369 '#description' => t('Enter the text to be contained in the subject line of the e-mail request for unsubscribing to this newsletter.'),
370 '#maxlength' => 255
371 );
372 $form['fields_fixed'] = array(
373 '#type' => 'fieldset',
374 '#title' => t('Choose fields'),
375 '#description' => t('Choose which fields will be present in the subscription form.'),
376 '#collapsible' => TRUE,
377 '#collapsed' => TRUE,
378 '#tree' => TRUE,
379 );
380 $form['fields_fixed']['Email'] = array(
381 '#type' => 'select',
382 '#title' => t('Subscription e-mail') . ' [Email]',
383 '#disabled' => TRUE,
384 '#default_value' => '2',
385 '#options' => $field_opts
386 );
387 foreach ($fields as $id => $field) {
388 $form['fields_fixed'][$id] = array(
389 '#type' => 'select',
390 '#title' => $field['#title'] . ' [' . $id . ']',
391 '#options' => $field_opts,
392 '#default_value' => isset($list_fields[$id]) ? $list_fields[$id] : '0'
393 );
394 }
395 $form['add_fields_container'] = array(
396 '#type' => 'fieldset',
397 '#title' => t('Define additional fields'),
398 '#description' => t('Define additional fields that will be submitted to Email Marketer.'),
399 '#collapsible' => TRUE,
400 '#collapsed' => TRUE,
401 );
402 $form['add_fields_container']['add_fields'] = array(
403 '#type' => 'textarea',
404 '#default_value' => empty($list['add_fields']) ? "/* Example:\n\$form['language'] = array(\n '#type' => 'textfield',\n '#title' => 'Subscriber language',\n '#maxlength' => 255\n);\n*/\n" : $list['add_fields'],
405 '#title' => t('Define additional fields'),
406 '#description' => t('You can declare here additional subscription fields using PHP. Additional fields can be declared by filling an associative array called <em>$form</em>. The keys of the array are the Email Marketer field names and the values are form elements defined as in Drupal Forms API. For more informations visit <a href="@url">Drupal Forms API</a>. If you are not familiar with PHP coding and Drupal Forms API do not enter here anything because your application may become unstable.', array('@url' => 'http://api.drupal.org/api/file/developer/topics/forms_api_reference.html/5')),
407 '#tree' => FALSE
408 );
409 $form['roles'] = array(
410 '#type' => 'fieldset',
411 '#title' => t('Roles'),
412 '#collapsible' => TRUE,
413 '#collapsed' => TRUE,
414 '#description' => t('Choose which roles may subscribe to this newsletter. Note that roles with the "administer email marketer" permission can always subscribe to all newsletters.'),
415 '#tree' => TRUE,
416 );
417 if (empty($list['roles']) || !isset($list['roles'])) {
418 $roles = array();
419 }
420 else {
421 $roles = explode('|', trim($list['roles'], '|'));
422 }
423 foreach (user_roles() as $rid => $role_name) {
424 $checked = in_array($rid, $roles);
425 $form['roles'][$rid] = array('#type' => 'checkbox',
426 '#title' => $role_name,
427 '#default_value' => $checked,
428 );
429 }
430 if (isset($list)) {
431 $form['eid'] = array('#type' => 'hidden', '#value' => $list['eid']);
432 }
433 $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
434 return $form;
435 }
436
437 /**
438 * Theme function for the newsletter configuration form
439 *
440 * @param $form
441 * The Drupal form array
442 *
443 * @return text
444 * The HTML representation of the form
445 */
446 function theme_emailmarketer_newsletter_form($form) {
447 $output = drupal_render($form['general']);
448 $output .= drupal_render($form['subscribe']);
449 $output .= drupal_render($form['unsubscribe']);
450 $rows = $row = array();
451 $counter = 1;
452 foreach($form['fields_fixed'] as $id => $element) {
453 if ($id{0} != '#') {
454 $row[] = drupal_render($element);
455 $counter++;
456 if ($counter == 5) {
457 $counter = 1;
458 $rows[] = $row;
459 $row = array();
460 }
461 }
462 }
463 if (!empty($row)) {
464 $rows[] = $row;
465 }
466 $fields = theme('table', array(), $rows);
467 $output .= theme('fieldset', array(
468 '#collapsible' => TRUE,
469 '#collapsed' => TRUE,
470 '#title' => $form['fields_fixed']['#title'],
471 '#description' => $form['fields_fixed']['#description'],
472 '#value' => $fields
473 ));
474 unset($form['fields_fixed']);
475 $output .= drupal_render($form);
476
477 return $output;
478 }
479
480 /**
481 * The validation function for the newsletter configuration form
482 *
483 * @param $form_id
484 * A string with the ID of the form
485 *
486 * @param $form_values
487 * An associative array with the POST or GET variables
488 */
489 function emailmarketer_newsletter_form_validate($form_id, $form_values) {
490 // list email
491 $email = trim($form_values['email']);
492 $result = db_fetch_object(db_query("SELECT email FROM {%s} WHERE email = '%s' AND eid <> %d", EMAILMARKETER_TABLE, $email, (int)$form_values['eid']));
493 if ($result) {
494 form_set_error('email', t('The e-mail of the list must be unique. An e-mail %name already exists.', array('%name' => $email)));
495 }
496 if (!valid_email_address($email)) {
497 form_set_error('email', t('The e-mail of the list is not in a valid format. Please check and fix the e-mail address.'));
498 }
499
500 // subscribe
501 $email = trim($form_values['email_subscribe']);
502 if ($email != '') {
503 $result = db_fetch_object(db_query("SELECT email_subscribe FROM {%s} WHERE email_subscribe = '%s' AND eid <> %d", EMAILMARKETER_TABLE, $email, (int)$form_values['eid']));
504 if ($result) {
505 form_set_error('email_subscribe', t('The e-mail used for subscribe requests must be unique or empty. An e-mail %name already exists.', array('%name' => $email)));
506 }
507 if (!valid_email_address($email)) {
508 form_set_error('email_subscribe', t('The e-mail for subscribe requests is not in a valid format. Please check and fix the e-mail address.'));
509 }
510 }
511
512 // unsubscribe
513 $email = trim($form_values['email_unsubscribe']);
514 if ($email != '') {
515 $result = db_fetch_object(db_query("SELECT email_unsubscribe FROM {%s} WHERE email_unsubscribe = '%s' AND eid <> %d", EMAILMARKETER_TABLE, $email, (int)$form_values['eid']));
516 if ($result) {
517 form_set_error('email_unsubscribe', t('The e-mail used for unsubscribe requests must be unique or empty. An e-mail %name already exists.', array('%name' => $email)));
518 }
519 if (!valid_email_address($email)) {
520 form_set_error('email_unsubscribe', t('The e-mail for unsubscribe requests is not in a valid format. Please check and fix the e-mail address.'));
521 }
522 }
523 }
524
525 /**
526 * Processing the newsletter configuration form
527 *
528 * @param $form_id
529 * A string with the ID of the form
530 *
531 * @param $form_values
532 * An associative array with the POST or GET variables
533 */
534 function emailmarketer_newsletter_form_submit($form_id, $form_values) {
535 $list = isset($form_values['eid']) ? $form_values['eid'] : NULL;
536 $new = !$list;
537
538 $allowed_roles = array();
539 if (isset($form_values['roles'])) {
540 foreach ($form_values['roles'] as $id => $checked) {
541 if ($checked) {
542 $allowed_roles[] = $id;
543 }
544 }
545 }
546 $allowed_roles = empty($allowed_roles) ? '' : '|' . implode('|', $allowed_roles) . '|';
547
548 $fields = serialize($form_values['fields_fixed']);
549
550 if ($new) {
551 db_query("INSERT INTO {%s} (email, version, activated, name, email_subscribe, subject_subscribe, email_unsubscribe, subject_unsubscribe, subscribe_text, subscribe_html, roles, fields_fixed, add_fields) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", EMAILMARKETER_TABLE, $form_values['email'], $form_values['version'], $form_values['activated'], $form_values['name'], $form_values['email_subscribe'], $form_values['subject_subscribe'], $form_values['email_unsubscribe'], $form_values['subject_unsubscribe'], $form_values['subscribe_text'], $form_values['subscribe_html'], (string)$allowed_roles, $fields, $form_values['add_fields']);
552 drupal_set_message(t('The newsletter %email was added.', array('%email' => $form_values['email'])));
553 return 'admin/settings/emailmarketer/';
554 }
555 else {
556 db_query("UPDATE {%s} SET email = '%s', version = '%s', activated='%s', name = '%s', email_subscribe = '%s', subject_subscribe = '%s', email_unsubscribe = '%s', subject_unsubscribe = '%s', subscribe_text = '%s', subscribe_html = '%s', roles = '%s', fields_fixed = '%s', add_fields = '%s' WHERE eid = %d", EMAILMARKETER_TABLE, $form_values['email'], $form_values['version'], $form_values['activated'], $form_values['name'], $form_values['email_subscribe'], $form_values['subject_subscribe'], $form_values['email_unsubscribe'], $form_values['subject_unsubscribe'], $form_values['subscribe_text'], $form_values['subscribe_html'], (string)$allowed_roles, $fields, $form_values['add_fields'], $form_values['eid']);
557 drupal_set_message(t('The newsletter %email was updated.', array('%email' => $form_values['email'])));
558 return 'admin/settings/emailmarketer/'. $list;
559 }
560 }
561
562 /**
563 * Confirm a newsletter deletion
564 *
565 * @param $list
566 * A record with the newsletter being deleted
567 */
568 function emailmarketer_admin_delete($list) {
569 $form['eid'] = array('#type' => 'hidden', '#value' => $list['eid']);
570 $form['email'] = array('#type' => 'hidden', '#value' => $list['email']);
571 return confirm_form($form, t('Are you sure you want to delete the newsletter %list?', array('%list' => $list['email'])), 'admin/settings/emailmarketer', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
572 }
573
574 /**
575 * Processing newsletter deletion
576 *
577 * @param $form_id
578 * A string with the ID of the form
579 *
580 * @param $form_values
581 * An associative array with the POST or GET variables
582 */
583 function emailmarketer_admin_delete_submit($form_id, $form_values) {
584 db_query("DELETE FROM {%s} WHERE eid = %d", EMAILMARKETER_TABLE, $form_values['eid']);
585 db_query("DELETE FROM {%s} WHERE eid = %d", EMAILMARKETER_QUEUE, $form_values['eid']);
586 drupal_set_message(t('Deleted Email Marketer newsletter %list.', array('%list' => $form_values['email'])));
587 }
588
589 /**
590 * Generate block for this module. The block is used for users subscribing.
591 */
592 function emailmarketer_block($op = 'list', $delta = 0, $edit = array()) {
593 global $emailmarketer_lists;
594 switch ($op) {
595 case 'list':
596 $blocks[1]['info'] = 'Email Marketer subscriber';
597 return $blocks;
598 break;
599 case 'view':
600 if (!isset($emailmarketer_lists)) {
601 $emailmarketer_lists = emailmarketer_list(NULL, true);
602 }
603 if (count($emailmarketer_lists)) {
604 foreach ($emailmarketer_lists as $id => $list) {
605 $form['lists'][] = array(
606 'href' => 'subscribe/' . $id,
607 'title' => $list['name']
608 );
609 }
610 $form['message'] = t(variable_get('emailmarketer_message', '<p>Select a newsletter to subscribe:</p>'));
611
612 $block['content'] = theme('emailmarketer_block', $form);
613 $block['subject'] = t(variable_get('emailmarketer_title', 'Our Newsletters'));
614 }
615 return $block;
616 break;
617 }
618 }
619
620 /**
621 * Theme the module block
622 */
623 function theme_emailmarketer_block($form) {
624 $output = '';
625 $output .= $form['message'];
626 $output .= theme('links', $form['lists']);
627 return $output;
628 }
629
630 function emailmarketer_page($lists) {
631 $links = array();
632 foreach ($lists as $id => $list) {
633 $links[] = array(
634 'href' => 'subscribe/' . $id,
635 'title' => $list['name']
636 );
637 }
638 $form['message'] = t(variable_get('emailmarketer_message', '<p>Select a newsletter to subscribe:</p>'));
639 $form['lists'] = $links;
640
641
642 return theme('emailmarketer_page', $form);
643 }
644
645 function theme_emailmarketer_page($form) {
646 $output = '';
647 $output .= $form['message'];
648 $output .= theme('links', $form['lists']);
649 return $output;
650 }
651
652 function emailmarketer_subscribe($list) {
653 global $user;
654 $form['email'] = array(
655 '#type' => 'textfield',
656 '#title' => t('Your e-mail'),
657 '#description' => t('Provide the e-mail address used to subscribe to the newsletter.'),
658 '#default_value' => $user->mail,
659 '#required' => TRUE,
660 '#maxlength' => 255
661 );
662
663 // fixed fields
664 $fields_fixed = unserialize($list['fields_fixed']);
665 $available_fields = emailmarketer_fields();
666 foreach ($fields_fixed as $id => $field) {
667 if ($field != '0') {
668 $form[$id] = $available_fields[$id];
669 if ($field == '2') {
670 $form[$id]['#required'] = TRUE;
671 }
672 }
673 }
674
675 // customized fields
676 eval($list['add_fields']);
677
678 $keys = array_keys($form);
679 unset($keys['email']);
680
681 $form['keys'] = array(
682 '#type' => 'hidden',
683 '#value' => serialize($keys)
684
685 );
686
687 $form['list_name'] = array(
688 '#type' => 'hidden',
689 '#value' => $list['name'],
690 );
691
692 $form['list_email'] = array(
693 '#type' => 'hidden',
694 '#value' => $list['email'],
695 );
696
697 $form['eid'] = array(
698 '#type' => 'hidden',
699 '#value' => $list['eid'],
700 );
701
702 $form['submit'] = array(
703 '#type' => 'submit',
704 '#value' => t('Submit'),
705 '#weight' => 50
706 );
707
708 return $form;
709 }
710
711 function emailmarketer_subscribe_validate($form_id, $form_values) {
712 $email = trim($form_values['email']);
713 if (!valid_email_address($email)) {
714 form_set_error('email', t('The e-mail is not in a valid format. Please check and fix the e-mail address.'));
715 }
716 $result = db_query("SELECT qid FROM {%s} WHERE eid = %d AND email = '%s'", EMAILMARKETER_QUEUE, $form_values['eid'], $email);
717 if (db_num_rows($result) > 0) {
718 form_set_error('email', t('This e-mail address was also subscribed to the newsletter <em>"%list"</em> and the subscription needs user confirmation. You must provide other e-mail address or you have to subscribe to other newsletter.', array('%list' => $form_values['list_name'])));
719 }
720 }
721
722 function emailmarketer_subscribe_submit($form_id, $form_values) {
723 global $user, $base_url;
724 $this_list = emailmarketer_list($form_values['eid']);
725 $this_list = $this_list[$form_values['eid']];
726
727 $qid = md5(serialize($form_values) . serialize($user) . time());
728 $email = $form_values['email'];
729 $stored_fields = array();
730 $all_fields = unserialize($form_values['keys']);
731 foreach ($form_values as $id => $value) {
732 if (in_array($id, $all_fields)) {
733 $stored_fields[$id] = $value;
734 }
735 }
736 $fields = serialize($stored_fields);
737
738 $sender = array('mail' => variable_get('site_mail', ini_get('sendmail_from')), 'name' => variable_get('site_name', 'Drupal'));
739 $recipient = array('mail' => $email, 'name' => $form_values['FullName']);
740
741 $values = array();
742 foreach ($all_fields as $token) {
743 $values['[' . $token . ']'] = $form_values[$token];
744 }
745
746 // make available additional variables
747 $values['[ListEmail]'] = $form_values['list_email'];
748 $values['[ListName]'] = $form_values['list_name'];
749 $values['[SubscribeEmail]'] = $this_list['email_subscribe'];
750 $values['[UnsubscribeEmail]'] = $this_list['email_unsubscribe'];
751 $values['[SubscribeSubject]'] = $this_list['subscribe_subject'];
752 $values['[UnsubscribeSubject]'] = $this_list['unsubscribe_subject'];
753 $values['[SubscribePolicyMessage]'] = ($this_list['activated'] == 'True') ? t('After you confirm your e-mail, you will be directly subscribed to the newsletter. No action is required on the administrators side.') : t('After you confirm your e-mail, you have to wait until the administrators approve your subscription.');
754 $values['[UnsubscribePolicyMessage]'] = t('To unsubscribe send an empty message to @unsubscribe', array('@unsubscribe' => ($this_list['email_unsubscribe'] ? $this_list['email_unsubscribe'] : $form_values['list_email']))) . ' ' . (($this_list['subject_unsubscribe'] != '') ? t('and fill the subject line with "@subj"', array('@subj' => $this_list['subject_unsubscribe'])) : '') . '.';
755 $values['[SiteName]'] = variable_get('site_name', 'Drupal');
756 $values['[SiteURL]'] = $base_url;
757 $values['[ConfirmationLink]'] = url('subscribe/confirm/' . $qid, NULL, NULL, TRUE);
758 $values['[ConfirmationCode]'] = $qid;
759 $values['[ConfirmationForm]'] = url('subscribe/confirm/', NULL, NULL, TRUE);
760
761 $plain = strtr($this_list['subscribe_text'], $values);
762
763 $url = parse_url($base_url);
764 $values['[SiteURL]'] = l($url['host'], $base_url, array(), NULL, NULL, TRUE);
765 $values['[ConfirmationLink]'] = l($values['[ConfirmationLink]'], 'subscribe/confirm/' . $qid);
766 $values['[ConfirmationForm]'] = l($values['[ConfirmationForm]'], 'subscribe/confirm/');
767 $html = strtr($this_list['subscribe_html'], $values);
768
769 if (mimemail(
770 $sender, // sender
771 $recipient, // recipient
772 t('Confirm your subscription to @name [@email]', array('@name' => $form_values['list_name'], '@email' => $form_values['list_email'])), // subject
773 $html, // html
774 false,
775 array(),
776 $plain // plain text
777 )) {
778 db_query("INSERT INTO {%s} (qid, eid, registered, email, subscribe_fields) VALUES ('%s', %d, %d, '%s', '%s')", EMAILMARKETER_QUEUE, $qid, $form_values['eid'], time(), $email, $fields);
779 watchdog('emailmarketer', $email . ' ask for subscription to ' . $form_values['list_email'], WATCHDOG_NOTICE);
780 drupal_set_message(t('Your subscription has been registered. In few moments you will receive a message asking you to confirm your subscription. Please follow the confirmation link included in the e-mail.'));
781 }
782 else {
783 drupal_set_message(t('The subscription process has failed due to an unknown error related to the SMTP system installed on the server. Please contact the system administrator to report the problem.'), 'error');
784 watchdog('emailmarketer', 'SMTP error: ' . $email . ' failed to subscribe to ' . $form_values['list_email'], WATCHDOG_WARNING);
785 }
786 drupal_goto('subscribe');
787 }
788
789 function emailmarketer_confirm($lists) {
790 $form['code'] = array(
791 '#type' => 'textfield',
792 '#title' => t('Enter the confirmation code'),
793 '#description' => t('Paste here the confirmation code sent to you by mail.'),
794 '#maxlength' => 32
795 );
796 $form['submit'] = array(
797 '#type' => 'submit',
798 '#value' => t('Validate'),
799 '#weight' => 50
800 );
801 return $form;
802 }
803
804 function emailmarketer_confirm_validate($form_id, $form_values) {
805 if (!preg_match("/[0-9a-z]{32,32}/", $form_values['code'])) {
806 form_set_error('code', t('The code entered is invalid. Wrong length or invalid characters detected.'));
807 }
808 }
809
810 /**
811 * Processing a confirmation
812 */
813 function emailmarketer_confirm_submit($form_id, $form_values) {
814 drupal_goto('subscribe/confirm/' . $form_values['code']);
815 }
816
817 /**
818 * Confirm a subscription and sent it to Email Marketer
819 *
820 * @param $lists
821 * An array with the newsletters abailable to the current user
822 */
823 function emailmarketer_confirmation($lists) {
824 $qid = arg(2);
825 $result = db_query("SELECT * FROM {%s} WHERE qid = '%s'", EMAILMARKETER_QUEUE, $qid);
826
827 // Check to see if the subscription is still in the queue
828 if (db_num_rows($result) == 0) {
829 drupal_set_message('<p>' . t('Failed to confirm this subscription. Possible reasons:') . '</p><ul><li>' . t('The subscription has expired: Each subscription is queued for validation for 48 hours. If you don\'t confirm your subscription within this period, the subscription expires.') . '</li><li>' . t('The subscription was confirmed previously: The link provided for subscription has already been used by you or other person. The confirmation link or the confirmation code can be used only once.') . '</li><li>' . t('The newsletter was deleted: The newsletter of this subscription was deleted in the meantime by the site administrators.') . '</li><li>' . t('The confirmation code is not valid: You didn\'t handled correctly the confirmation link or the confirmation code provided in the welcome e-mail.') . '</li></ul><p>' . t('If you think that this is a site error please report this to the <a href="@webmaster">site webmaster</a>.</p>', array('@webmaster' => url('user/1/contact'))), 'error');
830 }
831 else {
832 $subscribe = db_fetch_array($result);
833 $list = $lists[$subscribe['eid']];
834 $data = unserialize($subscribe['subscribe_fields']);
835
836 $sender = array('mail' => $subscribe['email'], 'name' => $data['FullName']);
837 $recipient = $list['email_subscribe'] ? $list['email_subscribe'] : $list['email'];
838 $body = <<<EOS
839 [Marketer]
840 version={$list["version"]}
841 activated={$list["activated"]}
842
843 [Data]
844 EOS;
845 $body .= "\n";
846 foreach ($data as $id => $item) {
847 if (!in_array($id, array('email', 'FullName'))) {
848 $body .= $id . '=' . $item . "\n";
849 }
850 }
851 if (mimemail(
852 $sender, // sender
853 $recipient, // recipient
854 $list['subject_subscribe'], // subject
855 '', // html
856 TRUE,
857 array(),
858 $body // plain text
859 )) {
860 $message = t('You have succeded the subscription to the newsletter.') . ' ' . ($list["activated"] == 'True' ? t('No additional action is required.') : t('Your subscription will be complete after administrator approval.'));
861 drupal_set_message($message);
862 watchdog('emailmarketer', $subscribe['email'] . ' was accepted to ' . $list['email'], WATCHDOG_NOTICE);
863
864 }
865 else {
866 drupal_set_message(t('The subscription process has failed due to an unknown error related to the SMTP system installed on the server. Please contact the system administrator to report the problem.'), 'error');
867 watchdog('emailmarketer', 'SMTP error: ' . $subscribe['email'] . ' fail to confirm to ' . $list['email'], WATCHDOG_WARNING);
868 }
869 db_query("DELETE FROM {%s} WHERE qid = '%s'", EMAILMARKETER_QUEUE, $qid);
870 }
871
872 if (count($lists) == 1) {
873 drupal_goto('');
874 }
875 else {
876 drupal_goto('subscribe');
877 }
878 }
879
880 /**
881 * Implementation of hook_cron
882 */
883 function emailmarketer_cron() {
884 $expire = time() - 48 * 3600;
885 db_query("DELETE FROM {%s} WHERE registered < %d", EMAILMARKETER_QUEUE, $expire);
886 }

  ViewVC Help
Powered by ViewVC 1.1.2