-
Notifications
You must be signed in to change notification settings - Fork 0
/
phplist_subscribe.module
executable file
·248 lines (224 loc) · 7.02 KB
/
phplist_subscribe.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* @file
* Provides a block for subscribing to PHPList lists.
*
* @todo: Add a page for managing subscriptions.
*/
/* Defines constants. */
// PHPList database.
// @todo: Allow users to set in settings form.
define('PHPLIST_DBNAME', 'phplist');
// Error reporting.
define('PHPLIST_LIST_RETURN_CODE_SUCCESS', 0);
define('PHPLIST_LIST_RETURN_CODE_ERROR', 1);
define('PHPLIST_LIST_RETURN_CODE_ALREADY_SUBSCRIBED', 2);
// ID of list to which to subscribe
define('PHPLIST_LIST_LIST_ID', 2);
// Message texts for user history table.
define('TEXT_SUMMARY', 'Subscription');
define('TEXT_DETAIL', 'Subscribe page: 1 List Membership: * ChristianVolunteering Marketing');
// Site homepage URL.
define('SITE_HOMEPAGE', 'home');
// Set the module in debug mode.
// Switch to FALSE to stop printing error messages.
define('PHPLIST_MODULE_IN_DEBUG', TRUE);
/**
* Implements hook_init().
*/
function phplist_subscribe_init() {
// add a custom JS file to change the destination of the form on tm homepage
if(arg(0) == SITE_HOMEPAGE) {
drupal_add_js(drupal_get_path('module', 'phplist_subscribe') . '/phplist_subscribe.js', array('type' => 'file', 'scope' => 'footer', 'every_page' => FALSE, 'cache' => FALSE, 'preprocess' => FALSE));
}
}
/**
* Implements hook_block_info().
*/
function phplist_subscribe_block_info() {
$blocks[0] = array(
'info' => t('PHPList Subscribe'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function phplist_subscribe_block_view($delta = 0) {
switch ($delta) {
case 0:
$block = array(
'subject' => t('Subscribe to Our Mailing List'),
'content' => drupal_get_form('phplist_subscribe_email_form'),
);
break;
}
return $block;
}
/**
* FormAPI form for subscribing to PHPList list.
*/
function phplist_subscribe_email_form($form, &$form_state) {
$form = array(
'email' => array(
'#type' => 'textfield',
'#title' => t('Email Address'),
'#required' => 1,
'#size' => 30,
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Subscribe'),
),
'#validate' => array('phplist_subscribe_email_form_validate'),
'#submit' => array('phplist_subscribe_email_form_submit'),
);
return $form;
}
/**
* Validate handler for subscribing to PHPList list.
*/
function phplist_subscribe_email_form_validate($form, &$form_state) {
if (user_validate_mail($form_state['values']['email'])) {
form_set_error('email', t('Your email address is invalid.'));
}
}
/**
* Submit handler for subscribing to PHPList list.
*/
function phplist_subscribe_email_form_submit($form, &$form_state) {
// Subscribe to list.
$response = phplist_subscribe_to_list($form_state['values']['email'], PHPLIST_LIST_LIST_ID);
// Handle any user-facing errors.
_phplist_handle_response($response, PHPLIST_LIST_LIST_ID);
}
/**
* Subscribes a user to a mailing list.
*
* Call in submit handler
* phplist_subscribe_to_list($form_state['values']['email'], MAILING_LIST_LIST_ID)
*
* @param string $email
* @return string
*/
function phplist_subscribe_to_list($email, $list_id) {
// Is this email already in our system?
$found_email = FALSE;
db_set_active(PHPLIST_DBNAME);
$result = db_query('select u.id from phplist_user_user u where u.email = :email',array(':email' => $email));
if($result->rowCount() > 0) {
// Yes, we already have the email in our db
$found_email = TRUE;
$userid = $record->fetchField();
// Is the email already subscribed to the mailing list?
$result = db_query('select userid from phplist_listuser where listid = :listid and userid = :userid', array(':listid' => $list_id, ':userid' => $userid));
if($result->rowCount() > 0) {
// Yes, the email is already subscribed
return PHPLIST_LIST_RETURN_CODE_ALREADY_SUBSCRIBED;
}
}
// The following inserts may fail, so wrap in a transaction for rollback.
$transaction = db_transaction();
try {
if(!$found_email) {
// The email was not already in our db, so we need to insert it.
$result = db_insert('phplist_user_user')
->fields(array(
'email' => $email,
'entered' => date("Y-m-d H:i:s"),
'modified' => date("Y-m-d H:i:s"),
'uniqid' => uniqid(),
'htmlemail' => 1,
'subscribepage' => 1
))
->execute();
// Get the user id for the newly inserted user
$userid = db_query("select id from phplist_user_user where email = :email", array(':email' => $email))->fetchField();
}
// Insert subscription.
$result = db_insert('phplist_listuser')
->fields(array(
'userid' => $userid,
'listid' => $list_id,
'entered' => date("Y-m-d H:i:s"),
'modified' => date("Y-m-d H:i:s")
))
->execute();
// Insert user history.
$result = db_insert('phplist_user_user_history')
->fields(array(
'userid' => $userid,
'ip' => $_SERVER['REMOTE_ADDR'],
'date' => date("Y-m-d H:i:s"),
'summary' => TEXT_SUMMARY,
'detail' => TEXT_DETAIL,
'systeminfo' => _phplist_get_sysinfo()
))
->execute();
}
catch(Exception $e) {
$transaction->rollback();
db_set_active();
_phplist_debug('Exception ' . $e::toString);
}
db_set_active();
return PHPLIST_LIST_RETURN_CODE_SUCCESS;
}
/**
* Helper function: Handles the response from the subscription.
*
* @return void
*/
function _phplist_handle_response($response, $list_id) {
$listname = _phplist_get_listname($list_id);
if(empty($listname)) {
$listname = 'default';
}
switch($response) {
case PHPLIST_LIST_RETURN_CODE_SUCCESS:
drupal_set_message(t('You have been subscribed to the %listname list.',
array('%listname' => $listname)));
break;
case PHPLIST_LIST_RETURN_CODE_ALREADY_SUBSCRIBED:
drupal_set_message(t('You are already subscribed to the %listname list.',
array('%listname' => $listname)));
break;
case PHPLIST_LIST_RETURN_CODE_ERROR:
default:
drupal_set_message(t('An error occurred. You have not been subscribed to the %listname list.',
array('%listname' => $listname)));
}
return;
}
/**
* Helper function: Gets the name of a PHPList list by id.
*/
function _phplist_get_listname($list_id) {
db_set_active(PHPLIST_DBNAME);
$listname = html_entity_decode(db_query('select name from phplist_list where id = :list_id', array(':list_id' => $list_id))->fetchField());
db_set_active();
return $listname;
}
/**
* Utility function: Returns a string with various information about the user's browser.
*
* @return string
*/
function _phplist_get_sysinfo() {
$attributes = array('HTTP_USER_AGENT', 'HTTP_REFERER', 'REMOTE_ADDR');
$sysinfo = '';
foreach($attributes as $attr) {
if(strlen($s) > 0) $s .= ' ';
$sysinfo .= ($attr . ' = ' . $_SERVER[$attr]);
}
return $sysinfo;
}
/**
* Utility function: Used for debugging the subscription function.
*
*/
function _phplist_debug($value, $label = 'error') {
if(module_exists('devel') && PHPLIST_MODULE_IN_DEBUG == TRUE) {
dpm($value, $label);
}
}