Example #1
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     // is the form submitted?
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // no errors?
         if ($this->frm->isCorrect()) {
             // the total amount of subscribers
             $subscribersTotal = 0;
             // loop all groups
             foreach ($this->externalGroups as $group) {
                 // insert them in our database
                 $groupID = $this->get('database')->insert('mailmotor_groups', array('name' => $group['name'], 'custom_fields' => $group['custom_fields'], 'created_on' => BackendModel::getUTCDate()));
                 // insert the CM ID
                 BackendMailmotorCMHelper::insertCampaignMonitorID('list', $group['id'], $groupID);
                 // continue looping if this group has no subscribers
                 if (empty($group['subscribers'])) {
                     continue;
                 }
                 // add this groups subscribers amount to the total
                 $subscribersTotal += $group['subscribers_amount'];
                 // loop the subscribers for this group, and import them
                 foreach ($group['subscribers'] as $subscriber) {
                     // build new subscriber record
                     $item = array();
                     $item['email'] = $subscriber['email'];
                     $item['source'] = 'import';
                     $item['created_on'] = $subscriber['date'];
                     // add an additional custom field 'name', if it was set in the subscriber record
                     if (!empty($subscriber['name'])) {
                         $subscriber['custom_fields']['Name'] = $subscriber['name'];
                     }
                     // save the subscriber in our database, and subscribe it to this group
                     BackendMailmotorModel::saveAddress($item, $groupID, !empty($subscriber['custom_fields']) ? $subscriber['custom_fields'] : null);
                 }
             }
             // at this point, groups are set
             $this->get('fork.settings')->set($this->getModule(), 'cm_groups_set', true);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_import_groups');
             // redirect to the index
             $this->redirect(BackendModel::createURLForAction('Index', $this->getModule()) . '&report=groups-imported&var[]=' . count($this->externalGroups) . '&var[]=' . $subscribersTotal);
         }
     }
 }
Example #2
0
 /**
  * Processes the subscriber import. Returns an array with failed subscribers.
  *
  * @param array $csv     The uploaded CSV file.
  * @param int   $groupID The group ID for which we're importing.
  * @return array A list with failed subscribers.
  */
 private function processImport($csv, $groupID)
 {
     // the CM list ID for the given group ID
     $listID = BackendMailmotorCMHelper::getCampaignMonitorID('list', $groupID);
     // reserve variables
     $subscribers = array();
     /*
         IMPORTANT NOTE: CM only allows a maximum amount of 100 subscribers for each import. So we have to batch
     */
     foreach ($csv as $key => $record) {
         // no e-mail address set means stop here
         if (empty($record['email'])) {
             continue;
         }
         // build record to insert
         $subscribers[$key] = $this->formatSubscriberCSVRow($record);
     }
     // divide the subscribers into batches of 100
     $batches = array_chunk($subscribers, 100);
     $failed = array();
     $feedback = array();
     $failedSubscribersCSV = array();
     // loop the batches
     foreach ($batches as $key => $batch) {
         // import every 100 subscribers
         $feedback[$key] = BackendMailmotorCMHelper::getCM()->importSubscribers($batch, $listID);
         // if the batch did not contain failed imports, we continue looping
         if (empty($feedback[$key])) {
             continue;
         }
         // merge the feedback results with the full failed set
         $failed = array_merge($failed, $feedback[$key]);
     }
     // now we have to loop all uploaded CSV rows in order to provide a .csv with all failed records.
     foreach ($csv as $row) {
         // the subscriber didn't fail the import, so we proceed to insert him in our database
         if (!in_array($row['email'], $failed)) {
             // build subscriber record
             $subscriber = array();
             $subscriber['email'] = $row['email'];
             $subscriber['source'] = 'import';
             $subscriber['created_on'] = BackendModel::getUTCDate();
             $subscriber['groups'] = $groupID;
             // unset the email (for the custom fields)
             unset($row['email']);
             // save the address in our database, with the assigned custom fields
             BackendMailmotorModel::saveAddress($subscriber, $groupID, $row);
             // continue looping
             continue;
         }
         // subscriber failed in import, so add his record to the fail-csv
         $failedSubscribersCSV[] = $row;
     }
     // return the failed subscribers
     return $failedSubscribersCSV;
 }