/**
  * Processes the subscriber import. Returns an array with failed subscribers.
  *
  * @return	array			A list with failed subscribers.
  * @param	array $csv		The uploaded CSV file.
  * @param	int $groupID	The group ID for which we're importing.
  */
 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;
 }
Exemple #2
0
    /**
     * 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::getDB()->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;
    }
 /**
  * Execute the action
  *
  * @return	void
  */
 public function execute()
 {
     // call parent, this will probably add some general CSS/JS or other required files
     parent::execute();
     // get parameters
     $id = SpoonFilter::getPostValue('id', null, '', 'int');
     // validate
     if ($id == '' || !BackendMailmotorModel::existsMailing($id)) {
         $this->output(self::BAD_REQUEST, null, 'No mailing found.');
     }
     // get mailing record
     $mailing = BackendMailmotorModel::getMailing($id);
     /*
     	mailing was already sent
     	We use a custom status code 900 because we want to do more with JS than triggering an error
     */
     if ($mailing['status'] == 'sent') {
         $this->output(900, null, BL::err('MailingAlreadySent', $this->getModule()));
     }
     // make a regular date out of the send_on timestamp
     $mailing['delivery_date'] = date('Y-m-d H:i:s', $mailing['send_on']);
     // send the mailing
     try {
         // only update the mailing if it was queued
         if ($mailing['status'] == 'queued') {
             BackendMailmotorCMHelper::updateMailing($mailing);
         } else {
             BackendMailmotorCMHelper::sendMailing($mailing);
         }
     } catch (Exception $e) {
         // fetch campaign ID in CM
         $cmId = BackendMailmotorCMHelper::getCampaignMonitorID('campaign', $id);
         // check if the CM ID isn't false
         if ($cmId !== false) {
             // delete the mailing in CM
             BackendMailmotorCMHelper::getCM()->deleteCampaign($cmId);
             // delete the reference
             BackendModel::getDB(true)->delete('mailmotor_campaignmonitor_ids', 'cm_id = ?', $cmId);
         }
         // check what error we have
         switch ($e->getMessage()) {
             case 'HTML Content URL Required':
                 $message = BL::err('HTMLContentURLRequired', $this->getModule());
                 break;
             case 'Payment details required':
                 $message = sprintf(BL::err('PaymentDetailsRequired', $this->getModule()), BackendModel::getModuleSetting($this->getModule(), 'cm_username'));
                 break;
             case 'Duplicate Campaign Name':
                 $message = BL::err('DuplicateCampaignName', $this->getModule());
                 break;
             default:
                 $message = $e->getMessage();
                 break;
         }
         // stop the script and show our error
         $this->output(902, null, $message);
     }
     // set status to 'sent'
     $item['id'] = $id;
     $item['status'] = $mailing['send_on'] > time() ? 'queued' : 'sent';
     // update the mailing record
     BackendMailmotorModel::updateMailing($item);
     // trigger event
     BackendModel::triggerEvent($this->getModule(), 'after_mailing_status_' . $item['status'], array('item' => $item));
     // we made it \o/
     $this->output(self::OK, array('mailing_id' => $item['id']), BL::msg('MailingSent', $this->getModule()));
 }