forked from minicstudio/minicmailchimp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminicmailchimp.php
465 lines (402 loc) · 13.9 KB
/
minicmailchimp.php
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/*
* minicskeleton - a module template for Prestashop v1.5+
* Copyright (C) 2013 S.C. Minic Studio S.R.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('_PS_VERSION_'))
exit;
require_once 'MCAPI.class.php';
class MinicMailchimp extends Module
{
private $api_key;
private $ssl;
private $module_path;
private $admin_tpl_path;
private $front_tpl_path;
private $hooks_tpl_path;
public function __construct()
{
$this->name = 'minicmailchimp';
$this->tab = 'advertising_marketing';
$this->version = '1.0.0';
$this->author = 'minic studio';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
parent::__construct();
$this->displayName = $this->l('Minic Mailchimp');
$this->description = $this->l('A module to syncronise Mailchimp with Prestashop easilly.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
// Paths
$this->module_path = _PS_MODULE_DIR_.$this->name.'/';
$this->admin_tpl_path = _PS_MODULE_DIR_.$this->name.'/views/templates/admin/';
$this->front_tpl_path = _PS_MODULE_DIR_.$this->name.'/views/templates/front/';
$this->hooks_tpl_path = _PS_MODULE_DIR_.$this->name.'/views/templates/hooks/';
$this->message = array(
'text' => false,
'type' => 'conf'
);
$this->hooks = array('Top', 'LeftColumn', 'RightColumn', 'Footer', 'Home', 'LeftColumnProduct', 'RightColumProduct', 'FooterProduct');
}
/**
* install
*/
public function install()
{
if (!parent::install() ||
!$this->registerHook('displayHome') ||
!$this->registerHook('displayHeader') ||
!$this->registerHook('displayBackOfficeHeader') ||
!$this->registerHook('displayAdminHomeQuickLinks') ||
!Configuration::updateValue(strtoupper($this->name).'_START', 1))
return false;
return true;
}
/**
* uninstall
*/
public function uninstall()
{
if (!parent::uninstall() ||
!Configuration::deleteByName(strtoupper($this->name).'_START') ||
!Configuration::deleteByName('MINIC_MAILCHIMP_SETTINGS') ||
!Configuration::deleteByName('MINIC_MAILCHIMP_FORM'))
return false;
return true;
}
/**
* admin page
*/
public function getContent()
{
$languages = Language::getLanguages(false);
// smarty for admin
$smarty_array = array(
'first_start' => Configuration::get(strtoupper($this->name).'_START'),
'admin_tpl_path' => $this->admin_tpl_path,
'front_tpl_path' => $this->front_tpl_path,
'hooks_tpl_path' => $this->hooks_tpl_path,
'info' => array(
'module' => $this->name,
'name' => Configuration::get('PS_SHOP_NAME'),
'domain' => Configuration::get('PS_SHOP_DOMAIN'),
'email' => Configuration::get('PS_SHOP_EMAIL'),
'version' => $this->version,
'psVersion' => _PS_VERSION_,
'server' => $_SERVER['SERVER_SOFTWARE'],
'php' => phpversion(),
'mysql' => Db::getInstance()->getVersion(),
'theme' => _THEME_NAME_,
'userInfo' => $_SERVER['HTTP_USER_AGENT'],
'today' => date('Y-m-d'),
'module' => $this->name,
'context' => (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') == 0) ? 1 : ($this->context->shop->getTotalShops() != 1) ? $this->context->shop->getContext() : 1,
),
'form_action' => 'index.php?tab=AdminModules&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&tab_module='. $this->tab .'&module_name='.$this->name,
'hooks' => $this->hooks,
'languages' => $languages,
'default_lang' => $this->context->language->id,
'flags' => array(
'title' => $this->displayFlags($languages, $this->context->language->id, 'title¤form', 'title', true),
'form' => $this->displayFlags($languages, $this->context->language->id, 'title¤form', 'form', true)
)
);
// Handling settings
if(Tools::isSubmit('submitMailchimp'))
$this->configureMailchimp();
// Mailchimp lists
$settings = unserialize(Configuration::get('MINIC_MAILCHIMP_SETTINGS'));
if (!$settings){
$this->message['text'] = $this->l('Before you start useing this module you need to configure it below!');
}else{
$this->api_key = $settings['apikey'];
$this->ssl = $settings['ssl'];
$this->getMailchimpLists();
}
// Handling Import
if(Tools::isSubmit('submitImport'))
$this->importCustomers();
// Handling Form
if(Tools::isSubmit('submitForm'))
$this->saveSubscriberForm();
// Smarty for admin
$smarty_array['mailchimp'] = $settings;
$smarty_array['message'] = ($this->message['text']) ? $this->message : false;
$smarty_array['form'] = unserialize(Configuration::get('MINIC_MAILCHIMP_FORM'));
$this->smarty->assign('minic', $smarty_array);
// Change first start
if(Configuration::get(strtoupper($this->name).'_START') == 1)
Configuration::updateValue(strtoupper($this->name).'_START', 0);
return $this->display(__FILE__, 'views/templates/admin/minicmailchimp.tpl');
}
/**
* Save the subscriber form details
*/
public function saveSubscriberForm()
{
$def_lang = $this->context->language->id;
// Get saved form data
$form_settings = unserialize(Configuration::get('MINIC_MAILCHIMP_FORM'));
// Prepear hooks array
$hooks = array(
'old' => ($form_settings) ? $form_settings['hooks'] : false,
'new' => (Tools::isSubmit('hooks')) ? Tools::getValue('hooks') : false,
);
// Get form
$form = ( Tools::isSubmit('form_'.$def_lang)) ? Tools::getValue('form_'.$def_lang) : false;
if(!$hooks['new']){
$this->message = array('text' => $this->l('At least a hook is required to show the form.'), 'type' => 'error');
return;
}
if(!$form){
$this->message = array('text' => $this->l('The form code is required!'), 'type' => 'error');
return;
}
// Unhook from all possible hooks
foreach ($this->hooks as $hook) {
if($this->isRegisteredInHook('display'.$hook))
$this->unregisterHook('display'.$hook);
}
// Hook
if($hooks['new']){ // pointless ?
foreach ($hooks['new'] as $hook) {
$this->registerHook('display'.$hook);
}
}
// Save
$data = array();
$languages = Language::getLanguages(false);
foreach ($languages as $key => $lang) {
$title = ( Tools::isSubmit('title_'.$lang['id_lang'])) ? Tools::getValue('title_'.$lang['id_lang']) : false;
$data[$lang['id_lang']] = array(
'title' => ($title) ? $title : Tools::getValue('title_'.$def_lang),
'form' => ( $form) ? htmlspecialchars($form) : htmlspecialchars(Tools::getValue('form_'.$def_lang)),
);
}
if(Configuration::updateValue('MINIC_MAILCHIMP_FORM', serialize(array('data' => $data, 'hooks' => $hooks['new']))))
$this->message['text'] = $this->l('Saved!');
}
/**
* Import customers into the selected Mailchimp list
*/
public function importCustomers()
{
// Get List id
$list_id = Tools::getValue('list');
if(!Tools::isSubmit('list') || !$list_id){
$this->message = array('text' => $this->l('List is required! Please select one.'), 'type' => 'error');
return;
}
// Get customers type
$all_customers = (Tools::getValue('all-user')) ? true : false;
// Get mailchimp fields
$fields = Tools::getValue('fields');
// Get Customers list
$customers = Customer::getCustomers();
// Creating import array
$list = array();
foreach ($customers as $customer_key => $customer) {
// Get customer data
$customer_details = new Customer($customer['id_customer']);
// Populate customer array
if($all_customers){
$list[$customer_key]['EMAIL'] = $customer_details->email;
if(isset($fields[$list_id])){
foreach ($fields[$list_id] as $key => $field) {
// mailchimp tag = customer field
$list[$customer_key][$key] = $customer_details->$field;
}
}
}else if(!$all_customers && $customer_details->newsletter){
$list[$customer_key]['EMAIL'] = $customer_details->email;
if(isset($fields[$list_id])){
foreach ($fields[$list_id] as $key => $field) {
// mailchimp tag = customer field
$list[$customer_key][$key] = $customer_details->$field;
}
}
}
}
// listBatchSubscribe configuration
$optin = (Tools::getValue('optin')) ? true : false; //send optin emails
$up_exist = (Tools::getValue('update_users')) ? true : false; //update currently subscribed users
$replace_int = true;
// Import customers
$mailchimp = new MCAPI($this->api_key, $this->ssl);
$import = $mailchimp->listBatchSubscribe($list_id, $list, $optin, $up_exist, $replace_int);
// Process response
if ($mailchimp->errorCode){
$this->message = array('text' => $this->l('Mailchimp error code:').' '.$mailchimp->errorCode.'<br />'.$this->l('Milchimp message:').' '.$mailchimp->errorMessage, 'type' => 'error');
return;
} else {
$this->message['text'] = $this->l('Successfull imported:').' <b>'.$import['add_count'].'</b><br />';
$this->message['text'] .= $this->l('Successfull updated:').' <b>'.$import['update_count'].'</b><br />';
if($import['error_count'] > 0){
$this->message['text'] .= $this->l('Error occured:').' <b>'.$import['error_count'].'</b><br />';
foreach ($import['errors'] as $error) {
$this->message['text'] .= '<p style="margin-left: 15px;">';
$this->message['text'] .= $error['email'].' - '.$error['code'].' - '.$error['message'];
$this->message['text'] .= '</p>';
}
$this->message['type'] = 'warn';
}
}
}
/**
* Get all mailchimp list and the fields belongs to them
*/
public function getMailchimpLists()
{
$mailchimp = new MCAPI($this->api_key, $this->ssl);
// Get Mailchimp lists
$list_response = $mailchimp->lists();
if ($mailchimp->errorCode){
$this->message = array('text' => $this->l('Mailchimp error code:').' '.$mailchimp->errorCode.'<br />'.$this->l('Milchimp message:').' '.$mailchimp->errorMessage, 'type' => 'error');
return;
} else {
$lists = '';
foreach ($list_response['data'] as $key => $value) {
$lists[$key] = $value;
// Get list fields
$lists[$key]['fields'] = $mailchimp->listMergeVars($value['id']);
}
$this->context->smarty->assign('mailchimp_list', $lists);
return true;
}
}
/**
* Save Mailchimp settings into PS Configuration (api key and ssl)
*/
public function configureMailchimp()
{
$settings = array();
// Get apikey
if(!Tools::isSubmit('apikey') || !Tools::getValue('apikey')){
$this->message = array('text' => $this->l('API Key is empty!'), 'type' => 'error');
return;
}
// Get ssl
if(!Tools::getValue('ssl') && Tools::getValue('ssl') != 0){
$this->message = array('text' => $this->l('SSL save failed!'), 'type' => 'error');
return;
}
$settings = array(
'apikey' => Tools::getValue('apikey'),
'ssl' => ((int)Tools::getValue('ssl') == 1) ? true : false,
);
Configuration::updateValue('MINIC_MAILCHIMP_SETTINGS', serialize($settings));
$this->message['text'] = $this->l('Saved!');
}
// BACK OFFICE HOOKS
/**
* admin <head> Hook
*/
public function hookDisplayBackOfficeHeader()
{
// Check if module is loaded
if (Tools::getValue('configure') != $this->name)
return false;
// CSS
$this->context->controller->addCSS($this->_path.'views/css/elusive-icons/elusive-webfont.css');
$this->context->controller->addCSS($this->_path.'views/css/admin.css');
$this->context->controller->addCSS($this->_path.'views/css/custom.css');
// JS
$this->context->controller->addJquery();
$this->context->controller->addJS($this->_path.'views/js/admin.js');
$this->context->controller->addJS($this->_path.'views/js/custom.js');
}
/**
* Hook for back office dashboard
*/
public function hookDisplayAdminHomeQuickLinks()
{
$this->context->smarty->assign('minicmailchimp', $this->name);
return $this->display(__FILE__, 'views/templates/hooks/quick_links.tpl');
}
// FRONT OFFICE HOOKS
/**
* <head> Hook
*/
public function hookDisplayHeader()
{
// CSS
$this->context->controller->addCSS($this->_path.'views/css/'.$this->name.'.css');
// JS
$this->context->controller->addJS($this->_path.'views/js/'.$this->name.'.js');
}
/**
* Top of pages hook
*/
public function hookDisplayTop($params)
{
return $this->hookDisplayHome($params, 'top');
}
/**
* Home page hook
*/
public function hookDisplayHome($params, $class = false)
{
$data = unserialize(Configuration::get('MINIC_MAILCHIMP_FORM'));
if(!$data)
return;
$smarty['class'] = ($class) ? $class : 'home';
$smarty['title'] = $data['data'][$this->context->language->id]['title'];
$smarty['form'] = $data['data'][$this->context->language->id]['form'];
$this->smarty->assign('mailchimp_form', $smarty);
return $this->display(__FILE__, 'views/templates/hooks/home.tpl');
}
/**
* Left Column Hook
*/
public function hookDisplayRightColumn($params)
{
return $this->hookDisplayHome($params, 'right');
}
/**
* Right Column Hook
*/
public function hookDisplayLeftColumn($params)
{
return $this->hookDisplayHome($params, 'left');
}
/**
* Footer hook
*/
public function hookDisplayFooter($params)
{
return $this->hookDisplayHome($params, 'footer');
}
/**
* Product page hook
*/
public function hookDisplayLeftColumnProduct($params)
{
return $this->hookDisplayHome($params, 'left-product');
}
/**
* Product page hook
*/
public function hookDisplayRightColumProduct($params)
{
return $this->hookDisplayHome($params, 'right-product');
}
/**
* Product page hook
*/
public function hookDisplayFooterProduct($params)
{
return $this->hookDisplayHome($params, 'footer-product');
}
}