-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendy_subscribe.module
177 lines (161 loc) · 5.11 KB
/
sendy_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
<?php
/**
* @file
* Module file for sendy_subscribe.
*/
/**
* @defgroup sendy_subscribe Sendy Subscribe
*
* This is a simple module that provides a block and a page where a user
* is able to subscribe to configurable mailing lists that reside on an
* instance of Sendy.
*
* Currently, we only have one block which is influenced very heavily by
* the newsletter module. It will have the following pieces:
* - A 'prefix' with some (optional) text that can be placed before the
* subscription form.
* - A form that collects the user's email address and send them to
* the subscription page.
* - A 'suffix' with some (optional) text that can be placed after the
* subscription form.
*/
/**
* Implements hook_block_info().
*/
function sendy_subscribe_block_info() {
$blocks['sendy_subscribe_block'] = array(
'info' => t('Sendy subscription block'),
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function sendy_subscribe_block_configure($delta = '') {
$form = array();
if ($delta == 'sendy_subscribe_block') {
$form['sendy_subscribe_block_prefix'] = array(
'#type' => 'textarea',
'#title' => t('Text to appear before the email address form'),
'#description' => t('This text will appear above the email address form in the subscrib block.',
'#default_value' => variable_get('sendy_subscribe_prefix'),
);
$form['sendy_subscribe_block_suffix'] = array(
'#type' => 'textarea',
'#title' => t('Text to appear after the email address form'),
'#description' => t('This text will appear below the email address form in the subscrib block.',
'#default_value' => variable_get('sendy_subscribe_suffix'),
);
}
return $form;
}
/**
* Implements hook_block_save().
*/
function sendy_subscribe_block_save($delta = '', $edit = array()) {
if ($delta == 'sendy_subscribe_block') {
// Save configuration settings to the database
variable_set('sendy_subscribe_prefix', $edit['sendy_subscribe_prefix']);
variable_set('sendy_subscribe_suffix', $edit['sendy_subscribe_suffix']);
}
return;
}
/**
* Implements hook_block_view().
*/
function sendy_subscribe_block_view($delta = '') {
switch ($delta) {
case 'sendy_subscribe_block':
if ( user_access('subscribe sendy newsletters') ) {
sendy_subscribe_add_js();
module_load_include('inc', 'newsletter', 'includes/newsletter.pages');
$block['subject'] = t('Subscribe to Sendy Mailing Lists');
$block['content'][] = array(
'#prefix' => '<div class="prefix">',
'#markup' => check_plain(variable_get('sendy_subscribe_block_prefix')),
'#suffix' => '</div>',
);
$block['content'][] = drupal_get_form('sendy_subscribe_subscribe_form');
$block['content'][] = array(
'#prefix' => '<div class="suffix">',
'#markup' => check_plain(variable_get('sendy_subscribe_block_suffix')),
'#suffix' => '</div>',
}
else {
$block = array();
}
break;
}
return $block;
}
function sendy_subscribe_add_js() {
}
/**
* Loads up the form that is displayed to the sendy subscribe block
*/
function sendy_subscribe_subscribe_form($form, &$form_state) {
global $user;
$ajax = array(
'callback' => 'sendy_subscribe_subscribe_form_submit',
'wrapper' => 'newsletter-error',
'effect' => 'fade',
'progress' => array(
'type' => 'throbber',
'message' => NULL,
),
'event' => 'click',
);
if (!isset($user->mail) || variable_get('sendy_subscribe_show_email_in_block', FALSE)) {
$form['email'] = array(
'#type' => 'textfield',
'#default_value' => t('[email protected]'),
'#size' => 20,
'#required' => TRUE,
);
}
else {
$form['logged-in'] = array(
'#type' => 'hidden',
'#value' => TRUE,
);
}
$form['newsletter-submit'] = array(
'#type' => 'submit',
'#value' => t('Subscribe'),
'#prefix' => '<div id="newsletter-error"></div><div id="subscribe">',
'#suffix' => '</div>',
'#ajax' => $ajax,
);
return $form;
}
/**
* Callback of sendy subscription block. Degrades when js is off.
* Validates e-mail and sends user to next step.
*/
function sendy_subscribe_subscribe_form_submit($form, &$form_state) {
$is_ajax = isset($form_state['input']['ajax_page_state']);
if (isset($form_state['values']['logged-in'])) {
global $user;
$mail = $user->mail;
}
elseif (!isset($form_state['values']['logged-in'])) {
$mail = ($form_state['values']['email'] != t('[email protected]'))
? $form_state['values']['email']
: '';
}
if (!valid_email_address($mail) || newsletter_is_subscribed($mail)) {
$msg = t("This e-mail doesn't exist or you have already subscribed");
return $is_ajax ? '<div id="newsletter-error">' . $msg . '</div>' : drupal_set_message($msg, 'warning');;
}
$q = array(
'destination' => '',
'email' => $mail,
);
$url = url('sendy/subscribe', array('query' => $q));
if ($is_ajax) {
return "<script type='text/javascript'>
location.href='$url'
</script>";
}
drupal_goto('sendy/subscribe', array('query' => $q));
}