Пример #1
0
 /**
  *
  * @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;
 }
Пример #2
0
 /**
  * 
  * @param array $contacts
  * @param int $existingListId
  * @return string
  */
 private function _update($contacts, $existingListId)
 {
     $segmentationObject = new Segmentation();
     // ** ** Détection du bon Index
     $mail_index = 'Email';
     if ($contacts) {
         $contact_ids = array_keys($contacts[0]);
         foreach ($contact_ids as $k) {
             if (preg_match('/(mail)/', $k)) {
                 $mail_index = $k;
             } else {
                 if ($k == $segmentationObject->ll(48)) {
                     $firstNameIndex = $k;
                 } else {
                     if ($k == $segmentationObject->ll(49)) {
                         $lastNameIndex = $k;
                     }
                 }
             }
         }
     }
     $prestashopContacts = array();
     $contactsToCsv = array();
     foreach ($contacts as $contact) {
         $prestashopContacts[] = $contact[$mail_index];
         if (!empty($contact[$mail_index])) {
             $contactsToCsv[$contact[$mail_index]] = array($contact[$mail_index], $contact[$firstNameIndex], $contact[$lastNameIndex]);
         }
     }
     $this->_gatherCurrentContacts($existingListId);
     $contacstToAdd = array();
     $contacstToRemove = array();
     foreach ($prestashopContacts as $email) {
         if (!in_array($email, $this->_mailjetContacts)) {
             $contacstToAdd[] = $contactsToCsv[$email];
         }
     }
     foreach ($this->_mailjetContacts as $email) {
         if (!in_array($email, $prestashopContacts)) {
             $contacstToRemove[] = $email;
         }
     }
     $response = 'Pending';
     try {
         if (!empty($contacstToAdd)) {
             /*
              * Sets related contact meta data like firstname, lastname, etc...
              */
             $this->_getApiOverlay()->setContactMetaData(array(array('Datatype' => 'str', 'Name' => 'firstname', 'NameSpace' => 'static'), array('Datatype' => 'str', 'Name' => 'lastname', 'NameSpace' => 'static')));
             $headers = array("email", "firstname", "lastname");
             $contstToAddCsv = '';
             $contstToAddCsv .= implode(",", $headers) . "\n";
             foreach ($contactsToCsv as $contact) {
                 $contstToAddCsv .= implode(",", $contact) . "\n";
             }
             $res = $this->_getApiOverlay()->createContacts($contstToAddCsv, $existingListId);
             if (!isset($res->ID)) {
                 throw new HooksSynchronizationException('Create contacts problem');
             }
             $batchJobResponse = $this->_getApiOverlay()->batchJobContacts($existingListId, $res->ID, 'addforce');
             if ($batchJobResponse == false) {
                 throw new HooksSynchronizationException('Batchjob problem');
             }
         }
         if (!empty($contacstToRemove)) {
             $contstToRemoveCsv = implode(' ', $contacstToRemove);
             $res = $this->_getApiOverlay()->createContacts($contstToRemoveCsv, $existingListId);
             if (!isset($res->ID)) {
                 throw new HooksSynchronizationException('Create contacts problem');
             }
             $batchJobResponse = $this->_getApiOverlay()->batchJobContacts($existingListId, $res->ID, 'remove');
             if ($batchJobResponse == false) {
                 throw new HooksSynchronizationException('Batchjob problem');
             }
         }
         $response = 'OK';
     } catch (Exception $e) {
         $response = $e;
     }
     return $response;
 }