/** * 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; }
/** * Get all data for a given mailing * * @param int $id The id of the mailing. * @return array */ public static function getMailing($id) { // get record and return it $record = (array) BackendModel::getContainer()->get('database')->getRecord('SELECT mm.*, UNIX_TIMESTAMP(mm.send_on) AS send_on FROM mailmotor_mailings AS mm WHERE mm.id = ?', array((int) $id)); // stop here if record is empty if (empty($record)) { return array(); } // get groups for this mailing ID $record['groups'] = self::getGroupIDsByMailingID($id); $record['recipients'] = self::getAddressesByGroupID($record['groups']); // fetch CM id for this mailing $record['cm_id'] = BackendMailmotorCMHelper::getCampaignMonitorID('campaign', $record['id']); // return the record return $record; }