name = 'mailjet_sync'; $this->version = '1.6.2'; $this->author = 'Antadis'; $this->tab = 'advertising_marketing'; // $this->module_key = '3a2a1733c24aa77ef5fd5e7ad9a7c77a'; $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6'); $this->need_instance = 0; $this->bootstrap = true; parent::__construct(); $config = Configuration::getMultiple(array('MAILJETSYNC_API_KEY', 'MAILJETSYNC_SECRET_KEY', 'MAILJETSYNC_LIST_ID', 'MAILJETSYNC_LIST_ID_PRO')); if (empty($config['MAILJETSYNC_API_KEY']) || empty($config['MAILJETSYNC_API_KEY'])) $this->warning = $this->l('API key & secret must be configured in order to use this module correctly.'); else if ($config['MAILJETSYNC_LIST_ID'] == 0 && $config['MAILJETSYNC_LIST_ID_PRO'] == 0) $this->warning = $this->l('No list of contacts set for this module.'); $this->displayName = $this->l('MailJet Sync for Prestashop 1.5'); $this->description = $this->l('Synchronize automatically your list of subscribers in Prestashop with your MailJet account, and the reverse too.'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall this module ? All your settings will be lost.'); } public function install() { if (Shop::isFeatureActive()) Shop::setContext(Shop::CONTEXT_ALL); if (!parent::install() || !$this->isCurlEnabled() || !Configuration::updateValue('MAILJETSYNC_EVENT_TOKEN', Tools::strtoupper(Tools::passwdGen(16))) || !Configuration::updateValue('MAILJETSYNC_API_KEY', '') || !Configuration::updateValue('MAILJETSYNC_SECRET_KEY', '') || !Configuration::updateValue('MAILJETSYNC_DELETE_CONTACT', false) || !Configuration::updateValue('MAILJETSYNC_OLD_API', false) || !Configuration::updateValue('MAILJETSYNC_LIST_ID', '0') || !Configuration::updateValue('MAILJETSYNC_LIST_ID_PRO', '0') || !Configuration::updateValue('MAILJETSYNC_LIST_ID_Q', '0') || !Configuration::updateValue('MAILJETSYNC_LIST_ID_H', '0') || !Configuration::updateValue('MAILJETSYNC_LIST_ID_3', '0') || !$this->registerHook('actionCustomerAccountAdd') || !$this->registerHook('actionObjectCustomerUpdateAfter') || !$this->registerHook('actionObjectCustomerDeleteBefore') || !$this->registerHook('displayHeader')) return false; return true; } public function uninstall() { if (!Configuration::deleteByName('MAILJETSYNC_EVENT_TOKEN') || !Configuration::deleteByName('MAILJETSYNC_SECRET_KEY') || !Configuration::deleteByName('MAILJETSYNC_API_KEY') || !Configuration::deleteByName('MAILJETSYNC_OLD_API') || !Configuration::deleteByName('MAILJETSYNC_LIST_ID') || !Configuration::deleteByName('MAILJETSYNC_LIST_ID_PRO') || !Configuration::deleteByName('MAILJETSYNC_LIST_ID_Q') || !Configuration::deleteByName('MAILJETSYNC_LIST_ID_H') || !Configuration::deleteByName('MAILJETSYNC_LIST_ID_3') || !Configuration::deleteByName('MAILJETSYNC_DELETE_CONTACT') || !parent::uninstall()) return false; return true; } private function getClient() { $api_version = (Configuration::get('MAILJETSYNC_OLD_API') == true) ? 1 : 3; $api_key = Configuration::get('MAILJETSYNC_API_KEY'); $api_secret = Configuration::get('MAILJETSYNC_SECRET_KEY'); $api_client = MailjetSyncApiClientFactory::create($api_version, $api_key, $api_secret); return $api_client; } private function testAPI($api_key, $secret_key) { $mj = $this->getClient(); return $mj->testAPI($api_key, $secret_key); } public function getContent() { $output = null; // API version if (Tools::isSubmit('submitVersion')) { $old_api = Tools::getValue('MAILJETSYNC_OLD_API'); if ($old_api === false) $output .= $this->displayError($this->l('Invalid settings')); else { Configuration::updateValue('MAILJETSYNC_OLD_API', $old_api); $output .= $this->displayConfirmation($this->l('Settings updated')); } } // API key if (Tools::isSubmit('submitKey')) { $api_key = Tools::getValue('MAILJETSYNC_API_KEY'); $secret_key = Tools::getValue('MAILJETSYNC_SECRET_KEY'); if (!$api_key || !$secret_key || empty($api_key) || empty($secret_key) || !$this->testAPI($api_key, $secret_key)) $output .= $this->displayError($this->l('Invalid API and/or Secret key')); else { Configuration::updateValue('MAILJETSYNC_API_KEY', $api_key); Configuration::updateValue('MAILJETSYNC_SECRET_KEY', $secret_key); $output .= $this->displayConfirmation($this->l('Settings updated')); } } // List if (Tools::isSubmit('submitList')) { $list_id = Tools::getValue('MAILJETSYNC_LIST_ID'); if (!$list_id || empty($list_id) || $list_id == 0) $output .= $this->displayError($this->l('Invalid list')); else { Configuration::updateValue('MAILJETSYNC_LIST_ID', $list_id); $output .= $this->displayConfirmation($this->l('Settings updated')); } $list_id_pro = Tools::getValue('MAILJETSYNC_LIST_ID_PRO'); if (!$list_id_pro || empty($list_id_pro) || $list_id_pro == 0) $output .= $this->displayError($this->l('Invalid list')); else { Configuration::updateValue('MAILJETSYNC_LIST_ID_PRO', $list_id_pro); $output .= $this->displayConfirmation($this->l('Settings updated')); } $list_id_q = Tools::getValue('MAILJETSYNC_LIST_ID_Q'); if (!$list_id_q || empty($list_id_q) || $list_id_q == 0) $output .= $this->displayError($this->l('Invalid list')); else { Configuration::updateValue('MAILJETSYNC_LIST_ID_Q', $list_id_q); $output .= $this->displayConfirmation($this->l('Settings updated')); } $list_id_h = Tools::getValue('MAILJETSYNC_LIST_ID_H'); if (!$list_id_h || empty($list_id_h) || $list_id_h == 0) $output .= $this->displayError($this->l('Invalid list')); else { Configuration::updateValue('MAILJETSYNC_LIST_ID_H', $list_id_h); $output .= $this->displayConfirmation($this->l('Settings updated')); } $list_id_3 = Tools::getValue('MAILJETSYNC_LIST_ID_3'); if (!$list_id_3 || empty($list_id_3) || $list_id_3 == 0) $output .= $this->displayError($this->l('Invalid list')); else { Configuration::updateValue('MAILJETSYNC_LIST_ID_3', $list_id_3); $output .= $this->displayConfirmation($this->l('Settings updated')); } } // Settings if (Tools::isSubmit('submitSettings')) { $delete_contact = Tools::getValue('MAILJETSYNC_DELETE_CONTACT'); if ($delete_contact === false) $output .= $this->displayError($this->l('Invalid settings')); else { Configuration::updateValue('MAILJETSYNC_DELETE_CONTACT', $delete_contact); $output .= $this->displayConfirmation($this->l('Settings updated')); } } $output .= $this->displayForm(); return $output.$this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl'); } public function displayForm() { // Get default language $default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); $fields_form = array(); // API version $fields_form[]['form'] = array( 'legend' => array( 'title' => $this->l('API version'), ), 'input' => array( array( 'type' => (version_compare(_PS_VERSION_, '1.6') < 0) ? 'radio' : 'switch', 'label' => $this->l('Old version of the API', get_class($this), null, false), 'name' => 'MAILJETSYNC_OLD_API', 'required' => false, 'class' => 't', 'is_bool' => true, 'values' => array( array( 'id' => 'old_api_on', 'value' => 1, 'label' => $this->l('Enabled', get_class($this), null, false) ), array( 'id' => 'old_api_off', 'value' => 0, 'label' => $this->l('Disabled', get_class($this), null, false) ) ), ), ), 'submit' => array( 'title' => $this->l('Save'), 'name' => 'submitVersion', 'class' => 'button' ) ); // API key $fields_form[]['form'] = array( 'legend' => array( 'title' => $this->l('MailJet keys'), ), 'description' => $this->l('You must have a MailJet account. You can create one on their website : ') .'https://www.mailjet.com/signup', 'input' => array( array( 'type' => 'text', 'label' => $this->l('Your MailJet API key'), 'desc' => $this->l('Example: 0123456789abcdef0123456789abcdef'), 'name' => 'MAILJETSYNC_API_KEY', 'size' => 45, 'required' => true ), array( 'type' => 'text', 'label' => $this->l('Your MailJet Secret key'), 'desc' => $this->l('Example: 0123456789abcdef0123456789abcdef'), 'name' => 'MAILJETSYNC_SECRET_KEY', 'size' => 45, 'required' => true ) ), 'submit' => array( 'title' => $this->l('Save'), 'name' => 'submitKey', 'class' => 'button' ) ); // List $lists = $this->getLists(); $fields_form[]['form'] = array( 'legend' => array( 'title' => $this->l('Default list'), ), 'input' => array( array( 'type' => 'select', 'label' => $this->l('MailJet list'), 'name' => 'MAILJETSYNC_LIST_ID', 'options' => array( 'default' => array( 'label' => $this->l('Select a list'), 'value' => 0 ), 'query' => $lists, 'id' => 'id', 'name' => 'name', ), 'required' => true ), array( 'type' => 'select', 'label' => $this->l('MailJet PRO list'), 'name' => 'MAILJETSYNC_LIST_ID_PRO', 'options' => array( 'default' => array( 'label' => $this->l('Select a list'), 'value' => 0 ), 'query' => $lists, 'id' => 'id', 'name' => 'name', ), 'required' => true ), array( 'type' => 'text', 'label' => $this->l('MailJet Quotidien list'), 'name' => 'MAILJETSYNC_LIST_ID_Q', 'required' => true ), array( 'type' => 'text', 'label' => $this->l('MailJet Hebdo list'), 'name' => 'MAILJETSYNC_LIST_ID_H', 'required' => true ), array( 'type' => 'text', 'label' => $this->l('MailJet 3x per week list'), 'name' => 'MAILJETSYNC_LIST_ID_3', 'required' => true ) ), 'submit' => array( 'title' => $this->l('Save'), 'name' => 'submitList', 'class' => 'button' ) ); // Settings $fields_form[]['form'] = array( 'legend' => array( 'title' => $this->l('Settings'), ), 'input' => array( array( 'type' => (version_compare(_PS_VERSION_, '1.6') < 0) ? 'radio' : 'switch', 'label' => $this->l('Delete or unsubscribe', get_class($this), null, false), 'name' => 'MAILJETSYNC_DELETE_CONTACT', 'required' => false, 'class' => 't', 'is_bool' => true, 'values' => array( array( 'id' => 'delete_contact_on', 'value' => 1, 'label' => $this->l('Enabled', get_class($this), null, false) ), array( 'id' => 'delete_contact_off', 'value' => 0, 'label' => $this->l('Disabled', get_class($this), null, false) ) ), ), ), 'submit' => array( 'title' => $this->l('Save'), 'name' => 'submitSettings', 'class' => 'button' ) ); $helper = new HelperForm(); // Module, token and currentIndex $helper->module = $this; $helper->name_controller = $this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; // Language $helper->default_form_language = $default_lang; $helper->allow_employee_form_lang = $default_lang; // Title and toolbar $helper->title = $this->displayName; $helper->show_toolbar = true; // false -> remove toolbar $helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen. $helper->submit_action = 'submit'.$this->name; $helper->toolbar_btn = array(); // Load current value $helper->fields_value['MAILJETSYNC_API_KEY'] = Configuration::get('MAILJETSYNC_API_KEY'); $helper->fields_value['MAILJETSYNC_SECRET_KEY'] = Configuration::get('MAILJETSYNC_SECRET_KEY'); $helper->fields_value['MAILJETSYNC_LIST_ID'] = Configuration::get('MAILJETSYNC_LIST_ID'); $helper->fields_value['MAILJETSYNC_LIST_ID_PRO'] = Configuration::get('MAILJETSYNC_LIST_ID_PRO'); $helper->fields_value['MAILJETSYNC_LIST_ID_Q'] = Configuration::get('MAILJETSYNC_LIST_ID_Q'); $helper->fields_value['MAILJETSYNC_LIST_ID_H'] = Configuration::get('MAILJETSYNC_LIST_ID_H'); $helper->fields_value['MAILJETSYNC_LIST_ID_3'] = Configuration::get('MAILJETSYNC_LIST_ID_3'); $helper->fields_value['MAILJETSYNC_DELETE_CONTACT'] = Configuration::get('MAILJETSYNC_DELETE_CONTACT'); $helper->fields_value['MAILJETSYNC_OLD_API'] = Configuration::get('MAILJETSYNC_OLD_API'); $this->context->smarty->assign('event_url', $this->context->link->getModuleLink($this->name, 'event', array('token' => Configuration::get('MAILJETSYNC_EVENT_TOKEN')))); return $helper->generateForm($fields_form); } private function isConfigured() { return (Configuration::get('MAILJETSYNC_API_KEY') && Configuration::get('MAILJETSYNC_SECRET_KEY')); } private function getLists() { $mj = $this->getClient(); $response = $mj->getLists(); return $response; } private function addContact($email, $isPro = false) { $mj = $this->getClient(); if(!$isPro) { if ($mj->addContactToList($email, Configuration::get('MAILJETSYNC_LIST_ID'))) { return true; } else { return false; } } elseif ($isPro) { if ($mj->addContactToList($email, Configuration::get('MAILJETSYNC_LIST_ID_PRO'))) { return true; } else { return false; } } return false; } private function _addContactWithDetails($customer, $isPro = false) { $email = explode("@", $customer->email); $email = explode(".", $email[1]); $email = $email[0]; $contacts = array( array( "Email" => $customer->email, "Properties" => array( "token" => Customer::getUrlAutologin($customer->id, $customer->email), "prenom" => $customer->firstname, "nom" => $customer->lastname, "fai" => $email ) ) ); $lists = array( array( "ListID" => Configuration::get('MAILJETSYNC_LIST_ID'), "Action" => $isPro ? "remove" : "addforce" ), array( "ListID" => Configuration::get('MAILJETSYNC_LIST_ID_PRO'), "Action" => $isPro ? "addforce" : "remove" ) ); $mj = $this->getClient(); if ($mj->addDetailedContactToList($contacts, $lists)) { return true; } else { return false; } return false; } private function addContactWithDetails($customer, $isPro = false, $frequence_nw = 0) { switch ($frequence_nw) { case '0': $frequence = 'quotidien'; $list = Configuration::get('MAILJETSYNC_LIST_ID_Q'); break; case '1': $frequence = 'hebdomadaire'; $list = Configuration::get('MAILJETSYNC_LIST_ID_H'); break; case '2': $frequence = '3 fois par semaine'; $list = Configuration::get('MAILJETSYNC_LIST_ID_3'); break; default: $frequence = 'quotidien'; $list = Configuration::get('MAILJETSYNC_LIST_ID_Q'); break; } $email = explode("@", $customer->email); $email = explode(".", $email[1]); $email = $email[0]; $contacts = array( array( "Email" => $customer->email, "Properties" => array( "token" => Customer::getUrlAutologin($customer->id, $customer->email), "frequence" => $frequence, "prenom" => $customer->firstname, "nom" => $customer->lastname, "fai" => $email, "group" => $isPro ? 'Pro' : 'Particulier', "date_inscription" => date('Y-m-d\TH:i:s\Z',strtotime($customer->date_add)), "ref_inscription" => (int)strtotime($customer->date_add), "id" => $customer->id, ) ) ); $lists = array( array( "ListID" => Configuration::get('MAILJETSYNC_LIST_ID_Q'), "Action" => ((int) $list !== (int) Configuration::get('MAILJETSYNC_LIST_ID_Q')) ? "remove" : "addforce" ), array( "ListID" => Configuration::get('MAILJETSYNC_LIST_ID_H'), "Action" => ((int) $list !== (int) Configuration::get('MAILJETSYNC_LIST_ID_H')) ? "remove" : "addforce" ), array( "ListID" => Configuration::get('MAILJETSYNC_LIST_ID_3'), "Action" => ((int) $list !== (int) Configuration::get('MAILJETSYNC_LIST_ID_3')) ? "remove" : "addforce" ), array( "ListID" => Configuration::get('MAILJETSYNC_LIST_ID'), "Action" => $isPro ? "remove" : "addforce" ), array( "ListID" => Configuration::get('MAILJETSYNC_LIST_ID_PRO'), "Action" => $isPro ? "addforce" : "remove" ) ); $mj = $this->getClient(); if ($mj->addDetailedContactToList($contacts, $lists)) { return true; } else { return false; } return false; } private function _deleteContact($email, $isPro = false) { $mj = $this->getClient(); if(!$isPro) { if ($mj->deleteContactFromList($email, Configuration::get('MAILJETSYNC_LIST_ID'), Configuration::get('MAILJETSYNC_DELETE_CONTACT'))) { return true; } else { return false; } } elseif ($isPro) { if ($mj->deleteContactFromList($email, Configuration::get('MAILJETSYNC_LIST_ID_PRO'), Configuration::get('MAILJETSYNC_DELETE_CONTACT'))) { return true; } else { return false; } } return false; } private function deleteContact($customer, $remove = 0) { switch ($customer->frequence_nw) { case '0': $list = Configuration::get('MAILJETSYNC_LIST_ID_Q'); break; case '1': $list = Configuration::get('MAILJETSYNC_LIST_ID_H'); break; case '2': $list = Configuration::get('MAILJETSYNC_LIST_ID_3'); break; default: $list = Configuration::get('MAILJETSYNC_LIST_ID_Q'); break; } if (Customer::isGroupPro($customer->id)) { $list2 = Configuration::get('MAILJETSYNC_LIST_ID_PRO'); } else { $list2 = Configuration::get('MAILJETSYNC_LIST_ID'); } $mj = $this->getClient(); if ($mj->deleteContactFromList($customer->email, $list, $remove) && $mj->deleteContactFromList($customer->email, $list2, $remove)) { error_log('ANTADIS DEBUG MAIJET - OK - Delete contact from list Q/H/3 : '.print_r($list, true).' - list Pro/Part : '.print_r($list2, true)); if ($remove == 1) { $mj->deleteContactFromList($customer->email, Configuration::get('MAILJETSYNC_LIST_ID'), $remove); $mj->deleteContactFromList($customer->email, Configuration::get('MAILJETSYNC_LIST_ID_PRO'), $remove); error_log('ANTADIS DEBUG MAIJET - OK - Redondant delete contact from both Pro/part lists'); } return true; } else { error_log('ANTADIS DEBUG MAIJET - FAIL - Delete contact from list Q/H/3 : '.print_r($list, true).' - list Pro/Part : '.print_r($list2, true)); return false; } } public function hookActionCustomerAccountAdd($params) { $new_customer = $params['newCustomer']; if (!Validate::isLoadedObject($new_customer)) return false; $newsletter = $new_customer->newsletter; $email = $new_customer->email; if ($new_customer->newsletter == 0 || !Validate::isEmail($email)) { return true; } //return $this->addContact($new_customer->email, Customer::isGroupPro($customer->id)); return $this->addContactWithDetails($new_customer, Customer::isGroupPro($new_customer->id)); } public function hookActionObjectCustomerUpdateAfter($params) { $customer = $params['object']; if (!Validate::isLoadedObject($customer)) return false; if (!isset($customer->newsletter)) return false; if ($customer->newsletter == 0) { return $this->deleteContact($customer, Configuration::get('MAILJETSYNC_DELETE_CONTACT')); } else { return $this->addContactWithDetails($customer, Customer::isGroupPro($customer->id), $customer->frequence_nw); } } public function hookActionObjectCustomerDeleteBefore($params) { $customer = $params['object']; if (!Validate::isLoadedObject($customer)) return false; return $this->deleteContact($customer, 1); } private function prepareHook() { if (!$this->isConfigured()) { return false; } if (Tools::isSubmit('submitNewsletter')) { $this->newsletterRegistration(); } } public function hookDisplayHeader($params) { return $this->prepareHook(); } public function hookDisplayLeftColumn($params) { return $this->hookDisplayHeader($params); } public function hookDisplayRightColumn($params) { return $this->hookDisplayHeader($params); } public function hookDisplayFooter($params) { return $this->hookDisplayHeader($params); } private function newsletterRegistration() { $email = Tools::getValue('email'); if ($email === false || empty($email) || !Validate::isEmail($email)) return $this->l('Invalid e-mail address'); $c = Customer::getCustomersByEmail($email); $customer = new Customer($c['id_customer']); $action = Tools::getValue('action'); switch ($action) { // Subscription case '0': // return $this->addContact($email, Customer::isGroupPro($customer['id_customer'])); return $this->addContactWithDetails($customer, Customer::isGroupPro((int) $customer->id),$customer->frequence_nw); // Unsubscription case '1': return $this->deleteContact($customer, Customer::isGroupPro((int) $customer->id), Configuration::get('MAILJETSYNC_DELETE_CONTACT')); default: return false; } } /* Check if cUrl extension is enabled */ private function isCurlEnabled() { return function_exists('curl_version'); } }