/[drupal]/contributions/modules/simplenews/simplenews.subscription.inc
ViewVC logotype

Contents of /contributions/modules/simplenews/simplenews.subscription.inc

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


Revision 1.13 - (show annotations) (download) (as text)
Thu Jul 30 19:56:47 2009 UTC (3 months, 3 weeks ago) by sutharsan
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--2
Changes since 1.12: +9 -9 lines
File MIME type: text/x-php
#528808 by Sutharsan, joachim: Added Distinguish between 'not subscribed' and 'unsubscribed', API changes.
1 <?php
2 // $Id: simplenews.subscription.inc,v 1.12 2009/03/07 17:14:31 sutharsan Exp $
3
4 /**
5 * @file
6 * (Un)subscription and (un)subscription confirmation
7 *
8 * @ingroup simplenews
9 */
10
11 /**
12 * Menu callback: Generates the subscription form for users.
13 *
14 * @see simplenews_subscription_manager_form_validate()
15 * @see simplenews_subscription_manager_form_submit()
16 */
17 function simplenews_subscription_manager_form(&$form_state, $snid = NULL) {
18 global $user;
19
20 if (isset($snid)) {
21 $account = new stdClass();
22 $account->snid = $snid;
23 $subscription = simplenews_get_subscription($account);
24 }
25 else {
26 $subscription = simplenews_get_subscription($user);
27 }
28
29 // If non-admin is trying to edit someone else's subscription, access denied.
30 if ($user->uid && $user->uid != $subscription->uid && !user_access('administer simplenews subscriptions')) {
31 drupal_access_denied();
32 return;
33 }
34 $form = _simplenews_subscription_manager_form($subscription);
35 $form['#validate'][] = 'simplenews_subscription_manager_form_validate';
36 $form['#submit'][] = 'simplenews_subscription_manager_form_submit';
37 $form['#redirect'] = ''; //Return to home page after (un)subscribe
38
39 return $form;
40 }
41
42 function simplenews_subscription_manager_form_validate($form, &$form_state) {
43 $valid_email = valid_email_address($form_state['values']['mail']);
44 if (!$valid_email) {
45 form_set_error('mail', t('The email address you supplied is not valid.'));
46 }
47 $checked_newsletters = array_filter($form_state['values']['newsletters']);
48 $account = new stdClass();
49 $account->mail = $form_state['values']['mail'];
50 if (!count($checked_newsletters) && !simplenews_get_subscription($account)) {
51 form_set_error('newsletters', t('You must select at least one newsletter.'));
52 }
53 }
54
55 function simplenews_subscription_manager_form_submit($form, &$form_state) {
56 switch ($form_state['values']['op']) {
57 case t('Update'):
58 // We first subscribe, then unsubscribe. This prevents deletion of subscriptions
59 // when unsubscribed from the
60 arsort($form_state['values']['newsletters'], SORT_NUMERIC);
61 foreach ($form_state['values']['newsletters'] as $tid => $checked) {
62 if ($checked) {
63 simplenews_subscribe_user($form_state['values']['mail'], $tid, FALSE, 'website');
64 }
65 else {
66 simplenews_unsubscribe_user($form_state['values']['mail'], $tid, FALSE, 'website');
67 }
68 }
69 drupal_set_message(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state['values']['mail'])));
70 break;
71 case t('Subscribe'):
72 foreach ($form_state['values']['newsletters'] as $tid => $checked) {
73 if ($checked) {
74 simplenews_subscribe_user($form_state['values']['mail'], $tid, TRUE, 'website');
75 }
76 }
77 drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to complete your subscription.'));
78 break;
79 case t('Unsubscribe'):
80 foreach ($form_state['values']['newsletters'] as $tid => $checked) {
81 if ($checked) {
82 simplenews_unsubscribe_user($form_state['values']['mail'], $tid, TRUE, 'website');
83 }
84 }
85 drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to complete the unsubscription process.'));
86 break;
87 }
88 }
89
90 /**
91 * Menu callback: confirm the user's (un)subscription request
92 *
93 * This function is called by clicking the confirm link in the confirmation
94 * email or the unsubscribe link in the footer of the newsletter. It handles
95 * both subscription and unsubscription.
96 *
97 * @see simplenews_confirm_add_form()
98 * @see simplenews_confirm_removal_form()
99 */
100 function simplenews_confirm_subscription($op1 = NULL, $op2 = NULL) {
101 $md5 = drupal_substr($op2, 0, 10);
102 list($snid, $tid) = explode('t', drupal_substr($op2, 10));
103
104 $result = db_query('SELECT snid, mail FROM {simplenews_subscriptions} WHERE snid = %d', $snid);
105 if (!($subs = db_fetch_object($result))) {
106 drupal_not_found();
107 return;
108 }
109
110 if ($md5 == drupal_substr(md5($subs->mail . simplenews_private_key()), 0, 10)) {
111 $newsletter = taxonomy_get_term($tid);
112 if ($op1 == 'remove') {
113 return drupal_get_form('simplenews_confirm_removal_form', $subs->mail, $newsletter);
114 }
115 elseif ($op1 == 'add') {
116 return drupal_get_form('simplenews_confirm_add_form', $subs->mail, $newsletter);
117 }
118 }
119
120 // If md5 didn't match, do a not found.
121 drupal_not_found();
122 return;
123 }
124
125 /**
126 * Generate the confirm subscription form.
127 *
128 * @see simplenews_confirm_add_form_submit()
129 */
130 function simplenews_confirm_add_form(&$form_state, $mail, $newsletter) {
131 $form = array();
132 $form['question'] = array('#value' => '<p>'. t('Are you sure you want to add %user to the %newsletter mailing list?', array('%user' => $mail, '%newsletter' => $newsletter->name)) ."<p>\n");
133 $form['mail'] = array('#type' => 'value', '#value' => $mail);
134 $form['newsletter'] = array('#type' => 'value', '#value' => $newsletter);
135 $form['#redirect'] = '';
136
137 return confirm_form($form,
138 t('Confirm subscription'),
139 '',
140 t('You always have the option of unsubscribing later.'),
141 t('Subscribe'),
142 t('Cancel')
143 );
144 }
145
146 function simplenews_confirm_add_form_submit($form, &$form_state) {
147 simplenews_subscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']->tid, FALSE, 'website');
148 drupal_set_message(t('%user was added to the %newsletter mailing list.', array('%user' => $form_state['values']['mail'], '%newsletter' => $form_state['values']['newsletter']->name)));
149 }
150
151 /**
152 * Generate the confirm unsubscription form.
153 *
154 * @see simplenews_confirm_removal_form_submit()
155 */
156 function simplenews_confirm_removal_form(&$form_state, $mail, $newsletter) {
157 $form = array();
158 $form['question'] = array('#value' => '<p>'. t('Are you sure you want to remove %user from the %newsletter mailing list?', array('%user' => $mail, '%newsletter' => $newsletter->name)) ."<p>\n");
159 $form['mail'] = array('#type' => 'value', '#value' => $mail);
160 $form['newsletter'] = array('#type' => 'value', '#value' => $newsletter);
161 $form['#redirect'] = '';
162
163 return confirm_form($form,
164 t('Confirm unsubscription'),
165 '',
166 t('This action will unsubscribe you from the newsletter mailing list.'),
167 t('Unsubscribe'),
168 t('Cancel')
169 );
170 }
171
172 function simplenews_confirm_removal_form_submit($form, &$form_state) {
173 simplenews_unsubscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']->tid, FALSE, 'website');
174 drupal_set_message(t('%user was unsubscribed from the %newsletter mailing list.', array('%user' => $form_state['values']['mail'], '%newsletter' => $form_state['values']['newsletter']->name)));
175 }
176
177 /**
178 * Menu callback: handle the edit subscription page and a subscription
179 * page for anonymous users.
180 */
181 function simplenews_admin_users_form(&$form_state, $snid = NULL) {
182 $form = simplenews_subscription_manager_form($form_state, $snid);
183 $form['#redirect'] = 'admin/content/simplenews/users';
184 return $form;
185 }

  ViewVC Help
Powered by ViewVC 1.1.2