/**
  * Delete addresses
  */
 private function deleteCustomFields()
 {
     // set the group fields by flipping the custom fields array for this group
     $groupFields = array_flip($this->group['custom_fields']);
     // group custom fields found
     if (!empty($groupFields)) {
         // loop the group fields and empty every value
         foreach ($groupFields as &$field) {
             $field = '';
         }
     }
     // loop the fields
     foreach ($this->fields as $field) {
         // check if the passed field is in the group's field list
         if (isset($groupFields[$field])) {
             // delete the custom field in CM
             BackendMailmotorCMHelper::deleteCustomField('[' . $field . ']', $this->group['id']);
             // remove the field from the group's field listing
             unset($groupFields[$field]);
         }
     }
     // update custom fields for this group
     BackendMailmotorModel::updateCustomFields($groupFields, $this->group['id']);
     // redirect
     $this->redirect(BackendModel::createURLForAction('CustomFields') . '&group_id=' . $this->group['id'] . '&report=deleted-custom-fields&var=' . $this->group['name']);
 }
Exemple #2
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();
         // shorten fields
         $txtName = $this->frm->getField('name');
         $rbtDefaultForLanguage = $this->frm->getField('default');
         // validate fields
         if ($txtName->isFilled(BL::err('NameIsRequired'))) {
             // check if the group exists by name
             if (BackendMailmotorModel::existsGroupByName($txtName->getValue())) {
                 $txtName->addError(BL::err('GroupAlreadyExists'));
             }
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             // build item
             $item['name'] = $txtName->getValue();
             $item['created_on'] = BackendModel::getUTCDate('Y-m-d H:i:s');
             $item['language'] = $rbtDefaultForLanguage->getValue() === '0' ? null : $rbtDefaultForLanguage->getValue();
             $item['is_default'] = $rbtDefaultForLanguage->getChecked() ? 'Y' : 'N';
             // insert the item
             $item['id'] = BackendMailmotorCMHelper::insertGroup($item);
             // check if all default groups were set
             BackendMailmotorModel::checkDefaultGroups();
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_add_group', array('item' => $item));
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('Groups') . '&report=added&var=' . urlencode($item['name']) . '&highlight=id-' . $item['id']);
         }
     }
 }
Exemple #3
0
 /**
  * Load the datagrid for statistics
  */
 private function loadStatistics()
 {
     // fetch the latest mailing
     $mailing = BackendMailmotorModel::getSentMailings(1);
     // check if a mailing was found
     if (empty($mailing)) {
         return false;
     }
     // check if a mailing was set
     if (!isset($mailing[0])) {
         return false;
     }
     // show the sent mailings block
     $this->tpl->assign('oSentMailings', true);
     // fetch the statistics for this mailing
     $stats = BackendMailmotorCMHelper::getStatistics($mailing[0]['id'], true);
     // reformat the send date
     $mailing[0]['sent'] = \SpoonDate::getDate('d-m-Y', $mailing[0]['sent']) . ' ' . BL::lbl('At') . ' ' . \SpoonDate::getDate('H:i', $mailing);
     // get results
     $results[] = array('label' => BL::lbl('MailmotorLatestMailing'), 'value' => $mailing[0]['name']);
     $results[] = array('label' => BL::lbl('MailmotorSendDate'), 'value' => $mailing[0]['sent']);
     $results[] = array('label' => BL::lbl('MailmotorSent'), 'value' => $stats['recipients'] . ' (' . $stats['recipients_percentage'] . ')');
     $results[] = array('label' => BL::lbl('MailmotorOpened'), 'value' => $stats['unique_opens'] . ' (' . $stats['unique_opens_percentage'] . ')');
     $results[] = array('label' => BL::lbl('MailmotorClicks'), 'value' => $stats['clicks_total'] . ' (' . $stats['clicks_percentage'] . ')');
     // there are some results
     if (!empty($results)) {
         // get the datagrid
         $dataGrid = new BackendDataGridArray($results);
         // no pagination
         $dataGrid->setPaging(false);
         // parse the datagrid
         $this->tpl->assign('dgMailmotorStatistics', $dataGrid->getContent());
     }
 }
Exemple #4
0
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     // get parameters
     $clientId = \SpoonFilter::getPostValue('client_id', null, '');
     // check input
     if (empty($clientId)) {
         $this->output(self::BAD_REQUEST);
     } else {
         // get basic details for this client
         $client = BackendMailmotorCMHelper::getCM()->getClient($clientId);
         // CM was successfully initialized
         $this->output(self::OK, $client);
     }
 }
Exemple #5
0
 /**
  * Gets all data needed for this page
  */
 private function getData()
 {
     $this->id = $this->getParameter('id', 'int');
     // does the item exist
     if (!BackendMailmotorModel::existsMailing($this->id)) {
         $this->redirect(BackendModel::createURLForAction('Index') . '&error=mailing-does-not-exist');
     }
     // get mailing
     $this->mailing = BackendMailmotorModel::getMailing($this->id);
     // fetch the statistics
     $this->statistics = BackendMailmotorCMHelper::getStatistics($this->id, true);
     // no stats found
     if ($this->statistics === false) {
         $this->redirect(BackendModel::createURLForAction('Index') . '&error=no-statistics-loaded&var=' . str_replace('#', '', $this->mailing['name']));
     }
 }
 /**
  * Gets all data needed for this page
  */
 private function getData()
 {
     // get parameters
     $this->id = $this->getParameter('id', 'int');
     // does the item exist
     if (!BackendMailmotorModel::existsCampaign($this->id)) {
         $this->redirect(BackendModel::createURLForAction('Campaigns') . '&error=campaign-does-not-exist');
     }
     // store mailing
     $this->campaign = BackendMailmotorModel::getCampaign($this->id);
     // fetch the statistics
     $this->statistics = BackendMailmotorCMHelper::getStatisticsByCampaignID($this->id, true);
     // no stats found
     if ($this->statistics === false || empty($this->statistics)) {
         $this->redirect(BackendModel::createURLForAction('Campaigns') . '&error=no-statistics-loaded');
     }
 }
Exemple #7
0
 /**
  * Execute the action
  */
 public function execute()
 {
     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.');
     } else {
         // 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(500, null, BL::err('MailingAlreadySent', $this->getModule()));
         } else {
             // 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 {
                     // send the mailing if it wasn't queued
                     BackendMailmotorCMHelper::sendMailing($mailing);
                 }
             } catch (\Exception $e) {
                 // stop the script and show our error
                 $this->output(500, null, $e->getMessage());
                 return;
             }
             // 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()));
         }
     }
 }
 /**
  * Gets all data needed for this page
  */
 private function getData()
 {
     // get parameters
     $id = $this->getParameter('mailing_id', 'int');
     // does the item exist
     if (!BackendMailmotorModel::existsMailing($id)) {
         $this->redirect(BackendModel::createURLForAction('Index') . '&error=mailing-does-not-exist');
     }
     // fetch the mailing
     $this->mailing = BackendMailmotorModel::getMailing($id);
     // fetch the bounces
     $this->bounces = BackendMailmotorCMHelper::getBounces($this->mailing['id']);
     // does the item exist
     if (empty($this->bounces)) {
         $this->redirect(BackendModel::createURLForAction('Statistics') . '&id=' . $this->mailing['id'] . '&error=no-bounces');
     }
 }
Exemple #9
0
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     // action to execute
     $action = \SpoonFilter::getGetValue('action', array('delete'), 'delete');
     // no id's provided
     if (!isset($_GET['id'])) {
         $this->redirect(BackendModel::createURLForAction('Groups') . '&error=no-selection');
     } else {
         // redefine id's
         $ids = (array) $_GET['id'];
         // delete comment(s)
         if ($action == 'delete') {
             BackendMailmotorCMHelper::deleteGroups($ids);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_delete_groups', array('ids' => $ids));
         }
     }
     // redirect
     $this->redirect(BackendModel::createURLForAction('Groups') . '&report=delete-groups');
 }
 /**
  * Delete addresses
  */
 private function deleteAddresses()
 {
     // no group set
     if ($this->groupId == '') {
         $this->groupId = null;
     }
     // get all groups
     $groupIds = BackendMailmotorModel::getGroupIDs();
     // loop the emails
     foreach ($this->emails as $email) {
         // the group ID is not set
         if ($this->groupId == null) {
             // if no groups were set, break here
             if (empty($groupIds)) {
                 break;
             }
             // loop the group IDs
             foreach ($groupIds as $groupId) {
                 // try to unsubscribe this address
                 try {
                     BackendMailmotorCMHelper::unsubscribe($email, $groupId);
                 } catch (\Exception $e) {
                     // do nothing
                 }
             }
             // delete all addresses
             BackendMailmotorModel::deleteAddresses($email);
         } else {
             // group ID was set, unsubscribe the address for this group
             BackendMailmotorCMHelper::unsubscribe($email, $this->groupId);
         }
     }
     // trigger event
     BackendModel::triggerEvent($this->getModule(), 'after_delete_addresses');
     // redirect
     $this->redirect(BackendModel::createURLForAction('Addresses') . '&report=delete-addresses' . (!empty($this->groupId) ? '&group_id=' . $this->groupId : ''));
 }
Exemple #11
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::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;
 }
 /**
  * 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();
         // shorten fields
         $txtGroup = $this->frm->getField('group');
         // validate fields
         if ($txtGroup->isFilled(BL::err('NameIsRequired'))) {
             if (BackendMailmotorModel::existsGroupByName($txtGroup->getValue())) {
                 $txtGroup->addError(BL::err('GroupAlreadyExists'));
             }
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             // build item
             $item['name'] = $txtGroup->getValue();
             $item['created_on'] = BackendModel::getUTCDate('Y-m-d H:i:s');
             // update the item
             $item['id'] = BackendMailmotorCMHelper::insertGroup($item);
             // loop the addresses
             foreach ($this->statistics['clicked_links_by'][$this->linkURL] as $clicker) {
                 // subscribe the user to the created group
                 BackendMailmotorCMHelper::subscribe($clicker['email'], $item['id']);
             }
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('StatisticsLink') . '&url=' . $this->linkURL . '&mailing_id=' . $this->mailing['id'] . '&report=group-added&var=' . urlencode($item['name']) . '&highlight=id-' . $this->mailing['id']);
         }
     }
 }
Exemple #13
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;
 }
Exemple #14
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();
         // validate fields
         $this->frm->getField('email')->isFilled(BL::err('EmailIsRequired'));
         // get addresses
         $addresses = (array) explode(',', $this->frm->getField('email')->getValue());
         // loop addresses
         foreach ($addresses as $email) {
             // validate email
             if (!\SpoonFilter::isEmail(trim($email))) {
                 // add error if needed
                 $this->frm->getField('email')->addError(BL::err('ContainsInvalidEmail'));
                 // stop looking
                 break;
             }
         }
         $this->frm->getField('groups')->isFilled(BL::err('ChooseAtLeastOneGroup'));
         // no errors?
         if ($this->frm->isCorrect()) {
             // build item
             $item = $this->frm->getValues();
             $item['source'] = BL::lbl('Manual');
             $item['created_on'] = BackendModel::getUTCDate('Y-m-d H:i:s');
             // loop the groups
             foreach ($item['groups'] as $group) {
                 foreach ($addresses as $email) {
                     BackendMailmotorCMHelper::subscribe(trim($email), $group);
                 }
             }
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_add_address', array('item' => $item));
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('Addresses') . (!empty($this->groupId) ? '&group_id=' . $this->groupId : '') . '&report=added');
         }
     }
 }
Exemple #15
0
 /**
  * Validate the form for step 4
  */
 private function validateFormForStep4()
 {
     // is the form submitted?
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // shorten fields
         $txtEmail = $this->frm->getField('email');
         $txtSendOnDate = $this->frm->getField('send_on_date');
         $txtSendOnTime = $this->frm->getField('send_on_time');
         // validation
         if ($txtEmail->isFilled(BL::err('FieldIsRequired'))) {
             $txtEmail->isEmail(BL::err('EmailIsInvalid'));
         }
         $txtSendOnDate->isValid(BL::err('DateIsInvalid'));
         $txtSendOnTime->isValid(BL::err('TimeIsInvalid'));
         // no errors?
         if ($this->frm->isCorrect()) {
             /*
                 the actual sending of a mailing happens in ajax/send_mailing.php
                 This, however, is the point where a preview is sent to a specific address.
             */
             BackendMailmotorCMHelper::sendPreviewMailing($this->id, $txtEmail->getValue());
             // build URL
             $url = BackendModel::createURLForAction('Edit') . '&id=' . $this->id . '&step=4';
             // send the preview
             $this->redirect($url . '&report=preview-sent&var=' . $txtEmail->getValue());
         }
     }
 }
Exemple #16
0
 /**
  * Updates a client record.
  *
  * @param array $record The client record to update.
  * @return mixed
  */
 private function updateClient($record)
 {
     // get the account settings
     $url = $this->get('fork.settings')->get($this->getModule(), 'cm_url');
     $username = $this->get('fork.settings')->get($this->getModule(), 'cm_username');
     $password = $this->get('fork.settings')->get($this->getModule(), 'cm_password');
     // try and update the client info
     try {
         // fetch complete list of timezones as pairs
         $timezones = BackendMailmotorCMHelper::getTimezonesAsPairs();
         // init CampaignMonitor object
         $cm = new \CampaignMonitor($url, $username, $password, 10, $this->clientID);
         // update the client
         $cm->updateClientBasics($record['company_name'], $record['country'], $timezones[$record['timezone']]);
     } catch (\Exception $e) {
         // add an error to the email field
         $this->redirect(BackendModel::createURLForAction('Settings') . '&error=campaign-monitor-error&var=' . $e->getMessage() . '#tabSettingsClient');
     }
 }
Exemple #17
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();
         // shorten fields
         $txtName = $this->frm->getField('name');
         // validate fields
         if ($txtName->isFilled(BL::err('NameIsRequired'))) {
             if (in_array($txtName->getValue(), $this->group['custom_fields'])) {
                 $txtName->addError(BL::err('CustomFieldExists'));
             }
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             try {
                 // add the new item to the custom fields list
                 $this->group['custom_fields'][] = $txtName->getValue();
                 // set the group fields by flipping the custom fields array for this group
                 $groupFields = array_flip($this->group['custom_fields']);
                 // group custom fields found
                 if (!empty($groupFields)) {
                     // loop the group fields and empty every value
                     foreach ($groupFields as &$field) {
                         $field = '';
                     }
                 }
                 // addresses found and custom field delete with CM
                 BackendMailmotorCMHelper::createCustomField($txtName->getValue(), $this->group['id']);
                 // update custom fields for this group
                 BackendMailmotorModel::updateCustomFields($groupFields, $this->group['id']);
             } catch (\Exception $e) {
                 // redirect with a custom error
                 $this->redirect(BackendModel::createURLForAction('CustomFields') . '&group_id=' . $this->group['id'] . '&error=campaign-monitor-error&var=' . urlencode($e->getMessage()));
             }
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('CustomFields') . '&group_id=' . $this->group['id'] . '&report=added&var=' . urlencode($txtName->getValue()) . '&highlight=id-' . $this->group['id']);
         }
     }
 }
Exemple #18
0
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     // get parameters
     $mailingId = \SpoonFilter::getPostValue('mailing_id', null, '', 'int');
     $subject = \SpoonFilter::getPostValue('subject', null, '');
     $contentHTML = urldecode(\SpoonFilter::getPostValue('content_html', null, ''));
     $contentPlain = \SpoonFilter::getPostValue('content_plain', null, '');
     // validate mailing ID
     if ($mailingId == '') {
         $this->output(self::BAD_REQUEST, null, 'No mailing ID provided');
     } else {
         // get mailing record
         $this->mailing = BackendMailmotorModel::getMailing($mailingId);
         // check if record is empty
         if (empty($this->mailing)) {
             $this->output(self::BAD_REQUEST, null, BL::err('MailingDoesNotExist', $this->getModule()));
         } else {
             // validate subject
             if ($subject == '') {
                 $this->output(500, array('element' => 'subject', 'element_error' => BL::err('NoSubject', $this->getModule())), BL::err('FormError'));
             } else {
                 // set plain content
                 $contentPlain = empty($contentPlain) ? \SpoonFilter::stripHTML($contentHTML) : $contentPlain;
                 // add unsubscribe link
                 if (mb_strpos($contentPlain, '[unsubscribe]') === false) {
                     $contentPlain .= PHP_EOL . '[unsubscribe]';
                 }
                 // build data
                 $item['id'] = $this->mailing['id'];
                 $item['subject'] = $subject;
                 $item['content_plain'] = $contentPlain;
                 $item['content_html'] = $contentHTML;
                 $item['edited_on'] = date('Y-m-d H:i:s');
                 // update mailing in our database
                 BackendMailmotorModel::updateMailing($item);
                 /*
                     we should insert the draft into campaignmonitor here,
                     so we can use sendCampaignPreview in step 4.
                 */
                 $item['groups'] = $this->mailing['groups'];
                 $item['name'] = $this->mailing['name'];
                 $item['from_name'] = $this->mailing['from_name'];
                 $item['from_email'] = $this->mailing['from_email'];
                 $item['reply_to_email'] = $this->mailing['reply_to_email'];
                 try {
                     BackendMailmotorCMHelper::saveMailingDraft($item);
                 } catch (Exception $e) {
                     // CM did not receive a valid URL
                     if (strpos($e->getMessage(), 'HTML Content URL Required')) {
                         $message = BL::err('HTMLContentURLRequired', $this->getModule());
                     } elseif (strpos($e->getMessage(), 'Payment details required')) {
                         // no payment details were set for the CM client yet
                         $error = BL::err('PaymentDetailsRequired', $this->getModule());
                         $cmUsername = $this->get('fork.settings')->get($this->getModule(), 'cm_username');
                         $message = sprintf($error, $cmUsername);
                     } elseif (strpos($e->getMessage(), 'Duplicate Campaign Name')) {
                         // the campaign name already exists in CM
                         $message = BL::err('DuplicateCampaignName', $this->getModule());
                     } else {
                         // we received an unknown error
                         $message = $e->getMessage();
                     }
                     // stop the script and show our error
                     $this->output(500, null, $message);
                     return;
                 }
                 // trigger event
                 BackendModel::triggerEvent($this->getModule(), 'after_edit_mailing_step3', array('item' => $item));
                 // output
                 $this->output(self::OK, array('mailing_id' => $mailingId), BL::msg('MailingEdited', $this->getModule()));
                 return;
             }
         }
         // error
         $this->output(500, null, $message);
         return;
     }
 }
Exemple #19
0
 /**
  * Checks if any groups are made yet. Depending on the client that is linked to Fork, it will
  * create default groups if none were found in CampaignMonitor. If they were, the user is
  * presented with an overview to import all groups and their subscribers in Fork.
  */
 private function checkForGroups()
 {
     // groups are already set
     if ($this->get('fork.settings')->get('Mailmotor', 'cm_groups_set')) {
         return false;
     }
     // no CM data found
     if (!BackendMailmotorCMHelper::checkAccount()) {
         return false;
     }
     // check if there are external groups present in CampaignMonitor
     if ($this->checkForExternalGroups()) {
         $this->redirect(BackendModel::createURLForAction('ImportGroups', 'Mailmotor'));
     }
     // fetch the default groups, language abbreviation is the array key
     $groups = BackendMailmotorModel::getDefaultGroups();
     // loop languages
     foreach (BL::getActiveLanguages() as $language) {
         // this language does not have a default group set
         if (!isset($groups[$language])) {
             // set group record
             $group['name'] = 'Website (' . strtoupper($language) . ')';
             $group['language'] = $language;
             $group['is_default'] = 'Y';
             $group['created_on'] = date('Y-m-d H:i:s');
             try {
                 // insert the group in CampaignMonitor
                 BackendMailmotorCMHelper::insertGroup($group);
             } catch (\CampaignMonitorException $e) {
                 // ignore
             }
         }
     }
     // we have groups set, and default groups chosen
     $this->get('fork.settings')->set('Mailmotor', 'cm_groups_set', true);
     $this->get('fork.settings')->set('Mailmotor', 'cm_groups_defaults_set', true);
 }
Exemple #20
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();
         // get subscriptions
         if (!empty($this->subscriptions)) {
             $ddmGroups = $this->frm->getField('subscriptions');
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             // build item
             $item['email'] = $this->email;
             $item['groups'] = isset($_POST['groups']) ? $_POST['groups'] : array();
             // loop the fields
             foreach ($this->customFields as $field) {
                 // shorten the field
                 $txtField = $field['formElements']['txtField'];
                 // add the value to the custom fields to store
                 $this->record['custom_fields'][$this->group['id']][$field['label']] = $txtField->getValue();
             }
             /*
              * This is, in fact, an unsubscribe of the subscriber's
              * current groups, and a re-subscribe of the groups he
              * requested. This is done because the CM API supports no
              * updateSubscriber function, and it overwrites the values of
              * custom fields if you do an update for 1 list and don't
              * provide values for another.
              *
              * NOTE: A user will still be in the suppression list if he is
              * resubscribed, but he will receive e-mails.
              * (see: http://www.campaignmonitor.com/forums/viewtopic.php?id=1761)
              */
             // the groups the user is currently subscribed to
             if (!empty($this->record['groups'])) {
                 // loop the groups
                 foreach ($this->record['groups'] as $group) {
                     // Check if this group is in the allowed list. If it is, it should not be unsubscribed
                     if (!empty($item['groups']) && in_array($group, $item['groups'])) {
                         continue;
                     }
                     // unsubscribe the user
                     BackendMailmotorCMHelper::unsubscribe($this->email, $group);
                 }
             }
             // the groups the user wants to keep
             if (!empty($item['groups'])) {
                 // loop the groups
                 foreach ($item['groups'] as $group) {
                     // continue looping if this group has no custom fields
                     $customFields = !empty($this->record['custom_fields'][$group]) ? $this->record['custom_fields'][$group] : null;
                     // resubscribe for this group
                     BackendMailmotorCMHelper::subscribe($this->email, $group, $customFields);
                 }
             }
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_edit_address', array('item' => $this->record));
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('Addresses') . (!empty($this->subscriptions) ? '&group_id=' . $ddmGroups->getValue() : '') . '&report=edited&var=' . urlencode($item['email']) . '&highlight=email-' . $item['email']);
         }
     }
 }
Exemple #21
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);
         }
     }
 }