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

Contents of /contributions/modules/mailbuild/mailbuild.module

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


Revision 1.5 - (show annotations) (download) (as text)
Fri May 16 00:41:29 2008 UTC (18 months, 1 week ago) by dragonwize
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.4: +88 -85 lines
File MIME type: text/x-php
minor changes and testing
1 <?php
2 // $Id: mailbuild.module,v 1.4 2008/05/15 03:13:45 benkant Exp $
3
4 /**
5 * @file
6 * Mailbuild integration.
7 *
8 * Mailbuild is a popular mailing list delivery and subscription management
9 * service. This module defines a block for each configured Mailbuild List.
10 * For Mailbuild information see: http://www.mailbuild.com/
11 *
12 * This block allows your users to subscribe to your list by enter their email
13 * address (which defaults to $user->mail).
14 *
15 * SOAP client functionality is provides via the NuSOAP library. For more
16 * information on NuSOAP see: http://sourceforge.net/projects/nusoap/
17 */
18
19 ///////////////////////////////
20 // DRUPAL HOOKS
21 ///////////////////////////////
22
23 /**
24 * Implementation of hook_help()
25 */
26 function mailbuild_help($path, $arg) {
27 switch ($path) {
28 case 'admin/settings/mailbuild':
29 return '<p>' . t('Mailbuild mailing list subscription service integration.') . '</p>';
30 }
31 }
32
33 /**
34 * Implementation of hook_perm()
35 * @return array An array of valid permissions
36 */
37 function mailbuild_perm() {
38 return array('administer mailbuild');
39 }
40
41 /**
42 * Implementation of hook_menu()
43 */
44 function mailbuild_menu() {
45 $items = array();
46 $items['admin/settings/mailbuild'] = array(
47 'title' => t('Mailbuild'),
48 'access arguments' => array('administer mailbuild'),
49 'page callback' => 'drupal_get_form',
50 'page arguments' => array('mailbuild_admin_settings_form'),
51 'description' => t('Mailbuild integration settings.'),
52 'type' => MENU_NORMAL_ITEM,
53 );
54 $items['admin/settings/mailbuild/delete'] = array(
55 'title' => t('Confirm list removal'),
56 'access arguments' => array('administer mailbuild'),
57 'page callback' => 'drupal_get_form',
58 'page arguments' => array('mailbuild_admin_settings_confirm_delete_form'),
59 'type' => MENU_CALLBACK,
60 );
61 return $items;
62 }
63
64 /**
65 * Implementation of hook_block()
66 */
67 function mailbuild_block($op = 'list', $delta = 0, $edit = array()) {
68 switch ($op) {
69 case 'list':
70 return mailbuild_block_list();
71
72 case 'configure':
73 case 'save':
74 // no additional configuration options
75 break;
76
77 case 'view':
78 default:
79 $list = mailbuild_get_list($delta);
80 $block['subject'] = $list['name'];
81 $block['content'] = theme('mailbuild_block_content', $delta);
82 return $block;
83 }
84 }
85
86 ///////////////////////////////
87 // MODULE FUNCTIONS
88 ///////////////////////////////
89
90 /**
91 * Returns a list of blocks in a form suitable for hook_block() when $op == list:
92 * A block is returned for each Mailbuild list defined in admin/settings/mailbuild
93 */
94 function mailbuild_block_list() {
95 $lists = variable_get('mailbuild_lists', array());
96 foreach ($lists as $list_id => $list) {
97 $blocks[$list_id]['info'] = t('Subscribe to @list_name', array('@list_name' => $list['name']));
98 }
99 return $blocks;
100 }
101
102 /**
103 * Returns block contents in a form suitable for hook_block() when $op == view
104 */
105 function mailbuild_block_view_form(&$form_state, $list_id) {
106 global $user;
107
108 $form['intro_text'] = array(
109 '#type' => 'markup',
110 '#prefix' => '<p>',
111 '#value' => $intro_text,
112 '#suffix' => '</p>',
113 );
114 $form['name'] = array(
115 '#type' => 'textfield',
116 '#title' => t('Name'),
117 '#size' => 18,
118 '#default_value' => $user->name,
119 );
120 $form['email'] = array(
121 '#type' => 'textfield',
122 '#title' => t('Email'),
123 '#size' => 18,
124 '#default_value' => $user->mail,
125 );
126 $form['list_id'] = array(
127 '#type' => 'hidden',
128 '#value' => $list_id,
129 );
130 $form['subscribe'] = array(
131 '#type' => 'submit',
132 '#value' => 'Subscribe',
133 );
134 return $form;
135 }
136
137 /**
138 * Validate the list subscription form
139 */
140 function mailbuild_block_view_form_validate($form, &$form_state) {
141 if (!valid_email_address($form_state['values']['email'])) {
142 form_set_error('email', 'Please specify a valid email address');
143 }
144 if (!strlen($form_state['values']['name'])) {
145 form_set_error('name', 'Please specify a name');
146 }
147 }
148
149 /**
150 * Call the Mailbuild API to add subscriber to the specified list
151 */
152 function mailbuild_block_view_form_submit($form, &$form_state) {
153 $form_state['redirect'] = FALSE;
154 $list = mailbuild_get_list($form_state['values']['list_id']);
155 $params = array(
156 'ApiKey' => variable_get('mailbuild_apikey', ''),
157 'ListID' => $form_state['values']['list_id'],
158 'Email' => $form_state['values']['email'],
159 'Name' => $form_state['values']['name'],
160 );
161 $result = mailbuild_soap_call('AddSubscriber', $params);
162 if ($result['Subscriber.AddResult']['Code'] === '0') {
163 $msg = 'Thank you for subscribing to our list.';
164 if ($list['optin'] == 'double') {
165 $msg .= ' A confirmation link has been sent to your email address.';
166 }
167 drupal_set_message($msg);
168 } else {
169 $error = $result['Subscriber.AddResult']['Message'];
170 if (!strlen($error)) {
171 $error = 'NuSOAP library error';
172 }
173 drupal_set_message('There was an error subscribing to the list: ' . $error);
174 }
175 }
176
177 /**
178 * The actual SOAP call using the NuSOAP library
179 */
180 function mailbuild_soap_call($operation, $params) {
181 if (@include_once(realpath(mailbuild_nusoap_path()))) {
182 $wsdl = variable_get('mailbuild_service_url', '');
183 $client = new nusoap_client($wsdl, TRUE);
184 $result = $client->call($operation, $params);
185 } else {
186 $result = FALSE;
187 }
188 return $result;
189 }
190
191 /**
192 * Get the list data from site variables
193 */
194 function mailbuild_get_list($id) {
195 $lists = variable_get('mailbuild_lists', array());
196 // got an illegal offset error without forcing id to a string
197 return $lists[(string)$id];
198 }
199
200 /**
201 * Returns the path of the NuSOAP library
202 */
203 function mailbuild_nusoap_path() {
204 return variable_get('mailbuild_nusoap_path', drupal_get_path('module', 'mailbuild') .'/nusoap/lib/nusoap.php');
205 }
206
207 /**
208 * Module settings form
209 */
210 function mailbuild_admin_settings_form() {
211 // NuSOAP library location
212 if (file_exists(mailbuild_nusoap_path())) {
213 $status = t('NuSOAP library found.');
214 } else {
215 $status = t('NuSOAP library not found.');
216 }
217
218 $form['nusoap'] = array(
219 '#type' => 'fieldset',
220 '#title' => t('NuSOAP'),
221 '#description' => $status,
222 );
223 $form['nusoap']['nusoap_path'] = array(
224 '#type' => 'textfield',
225 '#title' => t('NuSOAP Path'),
226 '#description' => t('The location where NuSOAP is installed. This should be a relative path to <em>nusoap.php</em>.', array('@NuSOAP' => 'http://sourceforge.net/projects/nusoap/')),
227 '#default_value' => mailbuild_nusoap_path(),
228 '#required' => TRUE,
229 );
230
231 if ($status === t('NuSOAP library found.')) {
232 // display mailbuild settings
233 $form['mailbuild'] = array(
234 '#type' => 'fieldset',
235 '#title' => t('Mailbuild account'),
236 );
237 $form['mailbuild']['service_url'] = array(
238 '#type' => 'textfield',
239 '#title' => t('Service WSDL'),
240 '#description' => t('Your service URL, eg: http://yoursitename.createsend.com/api/api.asmx?wsdl'),
241 '#default_value' => variable_get('mailbuild_service_url', ''),
242 );
243 $form['mailbuild']['apikey'] = array(
244 '#type' => 'textfield',
245 '#title' => t('API Key'),
246 '#description' => t('The API key provided by Mailbuild'),
247 '#default_value' => variable_get('mailbuild_apikey', ''),
248 );
249
250 // current lists
251 $form['lists'] = array(
252 '#type' => 'fieldset',
253 '#title' => t('Current Mailbuild lists'),
254 );
255 $lists = variable_get('mailbuild_lists', array());
256 $header = array('Name', 'List ID', '');
257 foreach ($lists as $list_id => $list) {
258 $rows[] = array($list['name'], $list['id'], l('Delete', 'admin/settings/mailbuild/delete/' . $list_id));
259 }
260 if (!count($rows)) {
261 $rows[] = array(
262 array(
263 'data' => 'No lists are currently configured.',
264 'colspan' => 3,
265 ),
266 );
267 }
268 $form['lists']['current'] = array(
269 '#type' => 'markup',
270 '#prefix' => '<div class="list-table">',
271 '#value' => theme('table', $header, $rows),
272 '#suffix' => '</div>',
273 );
274
275 // new list
276 $form['new_list'] = array(
277 '#type' => 'fieldset',
278 '#title' => t('Add a new list'),
279 );
280 $form['new_list']['new_list_name'] = array(
281 '#type' => 'textfield',
282 '#title' => t('List name'),
283 '#description' => t('Choose a name for this list'),
284 );
285 $form['new_list']['new_list_id'] = array(
286 '#type' => 'textfield',
287 '#title' => t('Mailbuild List ID'),
288 '#description' => t('Enter the List ID provided by Mailbuild'),
289 );
290 $form['new_list']['new_list_optin'] = array(
291 '#type' => 'select',
292 '#title' => t('List opt-in method'),
293 '#description' => t("Single opt-in means new subscribers are added to this list as soon as they complete the subscribe form. Double opt-in means a verification email with a confirmation link will be sent to the subscriber that they must click to validate their address before they're added to this list. Select the opt-in type that matches your list settings on Mailbuild."),
294 '#options' => mailbuild_optin_options(),
295 );
296 }
297
298 $form['submit'] = array(
299 '#type' => 'submit',
300 '#value' => 'Save',
301 );
302 return $form;
303 }
304
305 /**
306 * Opt-in options
307 */
308 function mailbuild_optin_options() {
309 $options = array(
310 'single' => 'Single opt-in (no confirmation required)',
311 'double' => 'Double opt-in (confirmation required)',
312 );
313 return $options;
314 }
315
316 /**
317 * Module settings submit handler.
318 *
319 * Not using system_settings_form() because of the atypical mailbuild_lists system variable
320 */
321 function mailbuild_admin_settings_form_submit($form, &$form_state) {
322 variable_set('mailbuild_service_url', $form_state['values']['service_url']);
323 variable_set('mailbuild_apikey', $form_state['values']['apikey']);
324 variable_set('mailbuild_nusoap_path', $form_state['values']['nusoap_path']);
325
326 if ($form_state['values']['new_list_name'] && $form_state['values']['new_list_id']) {
327 $lists = variable_get('mailbuild_lists', array());
328 $id = $form_state['values']['new_list_id'];
329
330 $lists[$id] = array(
331 'name' => $form_state['values']['new_list_name'],
332 'id' => $id,
333 'optin' => $form_state['values']['new_list_optin'],
334 );
335 variable_set('mailbuild_lists', $lists);
336 }
337 drupal_set_message('Your settings have been saved');
338 return;
339 }
340
341 /**
342 * List delete confirmation form
343 */
344 function mailbuild_admin_settings_confirm_delete_form() {
345 $list = mailbuild_get_list(arg(4));
346 if (is_null($list)) {
347 drupal_goto('admin/settings/mailbuild');
348 return;
349 }
350 $form['message'] = array(
351 '#type' => 'markup',
352 '#prefix' => '<div>',
353 '#value' => 'Are you sure you want to delete the list with key <em>' . $list['name'] . '</em>?',
354 '#suffix' => '</div>',
355 );
356 $form['list_id'] = array(
357 '#type' => 'hidden',
358 '#value' => arg(4),
359 );
360 $form['confirm'] = array(
361 '#type' => 'submit',
362 '#value' => 'Confirm',
363 );
364 $form['cancel'] = array(
365 '#type' => 'submit',
366 '#value' => 'Cancel',
367 '#submit' => array('mailbuild_admin_settings_confirm_delete_form_cancel'),
368 );
369 return $form;
370 }
371
372 /**
373 * Confirm delete submit handler
374 */
375 function mailbuild_admin_settings_confirm_delete_form_submit($form, &$form_state) {
376 $lists = variable_get('mailbuild_lists', array());
377 unset($lists[$form_state['values']['list_id']]);
378 variable_set('mailbuild_lists', $lists);
379 drupal_set_message('List deleted');
380
381 // rebuild the block db table as we've removed the block for this list
382 _block_rehash();
383 $form_state['redirect'] = 'admin/settings/mailbuild';
384 }
385
386 /**
387 * Cancel delete submit handler
388 */
389 function mailbuild_admin_settings_confirm_delete_form_cancel($form, &$form_state) {
390 drupal_set_message('Delete list cancelled');
391 $form_state['redirect'] = 'admin/settings/mailbuild';
392 }
393
394 /**
395 * Implementation of hook_theme()
396 */
397 function mailbuild_theme() {
398 return array(
399 'mailbuild_block_content' => array(
400 'template' => 'mailbuild-block-content',
401 'arguments' => array('list_id' => NULL),
402 ),
403 );
404 }
405
406 /**
407 * template preprocesser
408 * here delta is the block number. we use this to get the list number/information
409 */
410 function template_preprocess_mailbuild_block_content(&$variables) {
411 $list = mailbuild_get_list($variables['list_id']);
412 $variables['list_name'] = check_plain($list['name']);
413 $variables['form'] = drupal_get_form('mailbuild_block_view_form', $variables['list_id']);
414 }

  ViewVC Help
Powered by ViewVC 1.1.2