/**
  * 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');
         }
     }
 }
 /**
  * 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 adresses
             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('statistics_link') . '&url=' . $this->linkURL . '&mailing_id=' . $this->mailing['id'] . '&report=group-added&var=' . urlencode($item['name']) . '&highlight=id-' . $this->mailing['id']);
         }
     }
 }
 /**
  * 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']);
         }
     }
 }