/**
  *
  * @throws Exception
  * @return int
  */
 public function synchronize()
 {
     if ($masterListId = $this->_getAlreadyCreatedMasterListId()) {
         $segmentSynch = new HooksSynchronizationSegment($this->_getApiOverlay());
         $segmentSynch->deleteList($masterListId);
     }
     $apiOverlay = $this->_getApiOverlay();
     $params = array('method' => 'JSON', 'Name' => self::LIST_NAME);
     $newMailjetList = $apiOverlay->createContactsListP($params);
     if (!$newMailjetList || !isset($newMailjetList->ID)) {
         throw new HooksSynchronizationException('There is a problem with the list\'s creation.');
     }
     $newlyCreatedListId = $newMailjetList->ID;
     if (!is_numeric($newlyCreatedListId)) {
         throw new HooksSynchronizationException('The API response is not correct.');
     }
     // increase the memory limit because the database could contain too many customers
     ini_set('memory_limit', '1028M');
     $allUsers = $this->_getAllActiveCustomers();
     if (count($allUsers) === 0) {
         throw new HooksSynchronizationException('You don\'t have any users in the database.');
     }
     $segmentationObject = new Segmentation();
     $contstToAddCsv = array();
     foreach ($allUsers as $userInfo) {
         $contstToAddCsv[] = array($userInfo['email'], $userInfo['firstname'], $userInfo['lastname']);
     }
     /*
      * Sets related contact meta data like firstname, lastname, etc...
      */
     $this->_getApiOverlay()->setContactMetaData(array(array('Datatype' => 'str', 'Name' => $segmentationObject->ll(48), 'NameSpace' => 'static'), array('Datatype' => 'str', 'Name' => $segmentationObject->ll(49), 'NameSpace' => 'static')));
     $headers = array("email", "firstname", "lastname");
     $contstToAddCsvString = '';
     $contstToAddCsvString .= implode(",", $headers) . "\n";
     foreach ($contstToAddCsv as $contact) {
         $contstToAddCsvString .= implode(",", $contact) . "\n";
     }
     $apiResponse = $apiOverlay->createContacts($contstToAddCsvString, $newlyCreatedListId);
     if (!isset($apiResponse->ID)) {
         $segmentSynch = new HooksSynchronizationSegment($this->_getApiOverlay());
         $segmentSynch->deleteList($newlyCreatedListId);
         throw new HooksSynchronizationException('There is a problem with the creation of the contacts.');
     }
     $batchJobResponse = $apiOverlay->batchJobContacts($newlyCreatedListId, $apiResponse->ID);
     if ($batchJobResponse == false) {
         throw new HooksSynchronizationException('Batchjob problem');
     }
     return $newlyCreatedListId;
 }
include_once _PS_ROOT_DIR_ . '/init.php';
$response = false;
/* $token_ok = Tools::getAdminTokenLite('AdminModules'); */
$token_ok = Tools::getAdminToken('AdminModules');
if (!Tools::getValue('token') && Tools::getValue('token') != $token_ok) {
    die('hack attempt');
}
if (Tools::getValue('idfilter') == 0 && Tools::getValue('action') == 'getQuery') {
    die('You have to save the list first.');
}
include_once _PS_MODULE_DIR_ . 'mailjet/mailjet.php';
/* include_once(_PS_MODULE_DIR_.'mailjet/classes/MailjetAPI.php'); */
include_once _PS_MODULE_DIR_ . 'mailjet/classes/MailJetTemplate.php';
include_once _PS_MODULE_DIR_ . 'mailjet/classes/hooks/synchronization/SynchronizationAbstract.php';
include_once _PS_MODULE_DIR_ . 'mailjet/classes/hooks/synchronization/Segment.php';
if (Tools::getValue('action') == 'getQuery') {
    Configuration::updateValue('MJ_PERCENTAGE_SYNC', 0);
    $obj = new Segmentation();
    $res_contacts = Db::getInstance()->executeS($obj->getQuery($_POST, true, false));
    $api = MailjetTemplate::getApi();
    $synchronization = new HooksSynchronizationSegment(MailjetTemplate::getApi());
    $response = $synchronization->sychronize($res_contacts, Tools::getValue('idfilter'), Tools::getValue('name'));
} else {
    if (Tools::getValue('action') == 'getPercentage') {
        $response = Configuration::get('MJ_PERCENTAGE_SYNC');
    }
}
if ($response === false) {
    $response = 'Error';
}
echo $response;
Example #3
0
 public function deleteFilter($id)
 {
     $deleteFromDb = Db::getInstance()->Execute('DELETE FROM `' . _DB_PREFIX_ . 'mj_condition` WHERE `id_filter` =' . (int) $id) && Db::getInstance()->Execute('DELETE FROM `' . _DB_PREFIX_ . 'mj_filter` WHERE `id_filter` =' . (int) $id);
     if ($deleteFromDb) {
         /* try { */
         $segmentSynchronization = new HooksSynchronizationSegment(MailjetTemplate::getApi());
         $mailjetListId = $this->_getMailjetContactListId($id);
         if ($mailjetListId) {
             $segmentSynchronization->deleteList($mailjetListId);
         }
         /* } catch (Exception $e) { } */
     }
     return (bool) $deleteFromDb;
 }