forked from qunabu/drupal-freshmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreshmail.module
158 lines (141 loc) · 4.28 KB
/
freshmail.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
<?php
require_once 'api/class.rest.php';
define('FM_API_KEY', variable_get('freshmail_api_key'));
define('FM_API_SECRET', variable_get('freshmail_api_secret_key'));
define('FM_API_LIST', variable_get('freshmail_list_api_key'));
function freshmail_menu() {
$items['frashmail_api'] = array(
'access callback' => true, // available to all
'page callback' => 'freshmail_api', // defined below
'delivery callback' => 'drupal_json_output'
);
return $items;
}
function freshmail_api() {
$rest = new FmRestAPI();
$rest->setApiKey(FM_API_KEY);
$rest->setApiSecret(FM_API_SECRET);
$data = array(
'email' => isset($_POST['email']) ? $_POST['email'] : variable_get('freshmail_mail', '[email protected]'),
'list' => FM_API_LIST
);
try {
$response = $rest->doRequest('subscriber/add', $data);
return $response;
} catch (Exception $e) {
$errors = array(
1301 => t("Email address is incorrect."),
1302 => t("List Key is wrong or is empty."),
1303 => t("One or more fields are incorrect"),
1304 => t("Subscriber is already subscribed to that list."),
1305 => t("Wrong subscriber status."),
);
$message = $e->getMessage();
if (isset($errors[$e->getCode()])) {
$message = $errors[$e->getCode()];
}
return array(
'error_message' => $message,
'error_code' => $e->getCode(),
'http_code' => $rest->getHttpCode(),
'FM_API_KEY' => FM_API_KEY,
'FM_API_SECRET' => FM_API_SECRET,
'FM_API_LIST' => FM_API_LIST,
'data' => $data
);
}
}
/**
* Implements hook_block_info().
*/
function freshmail_block_info() {
$blocks = array();
$blocks['freshmail_block'] = array(
'info' => t('Freshmail Block'),
);
return $blocks;
}
function freshmail_block_view($delta = '') {
if ($delta == 'freshmail_block') {
drupal_add_js(drupal_get_path('module', 'freshmail') . '/js/freshmail.js');
$variables = array(
'action' => base_path() . 'frashmail_api',
'send' => t("Ok"),
'placeholder' => t("Your email address")
);
$block = array
(
'subject' => t('Newsletter'),
'content' => theme('freshmail_template', $variables),
);
return $block;
}
}
function freshmail_theme_registry_alter(&$theme_registry) {
// add views templates to registry
$path = drupal_get_path('module', 'freshmail') . '/templates';
$templates = drupal_find_theme_templates($theme_registry, '.tpl.php', $path);
foreach ($templates as &$template) {
$template['type'] = 'theme_engine';
}
$theme_registry += $templates;
}
/**
* Implements hook_theme().
*/
function freshmail_theme($existing, $type, $theme, $path) {
return array(
'freshmail_template' => array(
'variables' => array(),
'template' => 'templates/subscribe_form',
),
);
}
/**
* Implements hook_block_configure().
*/
function freshmail_block_configure($delta = '') {
$form = array();
switch ($delta) {
case 'freshmail_block':
$form['freshmail_list_api_key'] = array(
'#type' => 'textfield',
'#title' => t('API Key for newsletter'),
'#size' => 10,
'#default_value' => variable_get('freshmail_list_api_key'),
);
$form['freshmail_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Api Key'),
'#size' => 32,
'#default_value' => variable_get('freshmail_api_key'),
);
$form['freshmail_api_secret_key'] = array(
'#type' => 'textfield',
'#title' => t('Secret Key'),
'#size' => 40,
'#default_value' => variable_get('freshmail_api_secret_key'),
);
$form['freshmail_mail'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#size' => 40,
'#default_value' => variable_get('freshmail_mail', '[email protected]'),
);
break;
}
return $form;
}
/**
* Implements hook_block_save().
*/
function freshmail_block_save($delta = '', $edit = array()) {
switch ($delta) {
case 'freshmail_block':
variable_set('freshmail_list_api_key', $edit['freshmail_list_api_key']);
variable_set('freshmail_api_key', $edit['freshmail_api_key']);
variable_set('freshmail_api_secret_key', $edit['freshmail_api_secret_key']);
variable_set('freshmail_mail', $edit['freshmail_mail']);
break;
}
}