/** * * @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; }
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; }