public function saveData($aSubscriberData)
 {
     $oSubscriber = SubscriberQuery::create()->findPk($this->iSubscriberId);
     if ($oSubscriber === null) {
         $oSubscriber = new Subscriber();
         $oSubscriber->setCreatedBy(Session::getSession()->getUserId());
         $oSubscriber->setCreatedAt(date('c'));
     }
     $oSubscriber->setPreferredLanguageId($aSubscriberData['preferred_language_id']);
     $oSubscriber->setName($aSubscriberData['name']);
     $oSubscriber->setEmail($aSubscriberData['email']);
     $this->validate($aSubscriberData, $oSubscriber);
     if (!Flash::noErrors()) {
         throw new ValidationException();
     }
     // Subscriptions
     foreach ($oSubscriber->getSubscriberGroupMemberships() as $oSubscriberGroupMembership) {
         $oSubscriberGroupMembership->delete();
     }
     $aSubscriptions = isset($aSubscriberData['subscriber_group_ids']) ? $aSubscriberData['subscriber_group_ids'] : array();
     foreach ($aSubscriptions as $iSubscriberGroupId) {
         $oSubscriberGroupMembership = new SubscriberGroupMembership();
         $oSubscriberGroupMembership->setSubscriberGroupId($iSubscriberGroupId);
         $oSubscriber->addSubscriberGroupMembership($oSubscriberGroupMembership);
     }
     return $oSubscriber->save();
 }
 /** addSubscibers()
  * @param array of email addresses to be added, if they don't exist
  * @param string number of target subscriber group
  * description:
  * • subscribers are added if they don't exist
  * • subscriber_group_membership is added if it does'nt exist
  *
  * @return array of integer received all / actually added
  */
 public function addSubscibers($aSubscribers, $mTargetSubscriberGroup)
 {
     $sSubscribers = '';
     $aTargetSubscriberGroups = is_array($mTargetSubscriberGroup) ? $mTargetSubscriberGroup : array($mTargetSubscriberGroup);
     // If is string the addresses have not been processed and validated by js
     if (is_string($aSubscribers)) {
         // preg_match_all, use
         $sSubscribers = trim($aSubscribers);
         $aSubscribers = array();
         $sSubscribers = preg_replace_callback('/' . Flash::$EMAIL_CHECK_PATTERN . '/', function ($aMatches) use(&$aSubscribers) {
             $aSubscribers[] = $aMatches[0];
             return '';
         }, $sSubscribers);
     }
     $aSubscribers = array_unique($aSubscribers);
     $iCountAll = count($aSubscribers);
     $iMembershipsAdded = 0;
     // Always create temporary groups with all imported subscribers
     $oSubscriberGroup = new SubscriberGroup();
     $oSubscriberGroup->setName(self::GENERATED_PREFIX . date('Ymd-Hs'));
     $oSubscriberGroup->save();
     array_push($aTargetSubscriberGroups, $oSubscriberGroup->getId());
     foreach ($aSubscribers as $sEmail) {
         $oSubscriber = SubscriberQuery::create()->filterByEmail($sEmail)->findOne();
         // Create new if subscriber does not exist and email is correct
         if ($oSubscriber === null) {
             $oSubscriber = new Subscriber();
             $oSubscriber->setEmail($sEmail);
             $oSubscriber->setName($sEmail);
         }
         // Add subscriber_group_membership if it does not exists
         // @todo check change jm > handle multiple groups including generated one
         // Please check meaning and function of counting new subscriptions, only one is counted per subsriber $iMembershipsAdded
         $bHasMemberShip = false;
         foreach ($aTargetSubscriberGroups as $iSubscriberGroupId) {
             if (!$oSubscriber->hasSubscriberGroupMembership($iSubscriberGroupId)) {
                 $bHasMemberShip = true;
                 $oSubscriberGroupMembership = new SubscriberGroupMembership();
                 $oSubscriberGroupMembership->setSubscriberGroupId($iSubscriberGroupId);
                 $oSubscriber->addSubscriberGroupMembership($oSubscriberGroupMembership);
             }
         }
         if ($bHasMemberShip) {
             $iMembershipsAdded++;
         }
         $oSubscriber->save();
     }
     return array('all' => $iCountAll, 'added' => $iMembershipsAdded, 'text' => $sSubscribers);
 }
Пример #3
0
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param PropelPDO $con
  * @return void
  * @throws PropelException
  * @throws Exception
  * @see        BaseObject::setDeleted()
  * @see        BaseObject::isDeleted()
  */
 public function delete(PropelPDO $con = null)
 {
     if ($this->isDeleted()) {
         throw new PropelException("This object has already been deleted.");
     }
     if ($con === null) {
         $con = Propel::getConnection(SubscriberPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
     }
     $con->beginTransaction();
     try {
         $deleteQuery = SubscriberQuery::create()->filterByPrimaryKey($this->getPrimaryKey());
         $ret = $this->preDelete($con);
         // denyable behavior
         if (!(SubscriberPeer::isIgnoringRights() || $this->mayOperate("delete"))) {
             throw new PropelException(new NotPermittedException("delete.by_role", array("role_key" => "subscribers")));
         }
         if ($ret) {
             $deleteQuery->delete($con);
             $this->postDelete($con);
             $con->commit();
             $this->setDeleted(true);
         } else {
             $con->commit();
         }
     } catch (Exception $e) {
         $con->rollBack();
         throw $e;
     }
 }
 /**
  * newsletterUnsubscribe()
  *
  * Description
  * • check if requested url is valid
  * • display opt-out options if request method is get or post is invalid
  * • process unsubscribe action if the request method is post
  * • cleanup subscriber membership and subscriber as fallback
  *
  * @return Template
  */
 private function newsletterUnsubscribe()
 {
     if (!isset($_REQUEST['unsubscribe'])) {
         return $this->constructTemplate('unsubscribe_unknown_error');
     }
     // Process unsubscribe opt_out form if post
     $oSubscriber = SubscriberQuery::create()->filterByEmail($_REQUEST['unsubscribe'])->findOne();
     if (Manager::isPost()) {
         $mOutput = $this->processOptOutSuscriptions($oSubscriber);
         if ($mOutput) {
             return $mOutput;
         }
     }
     // If subscriber does not exist or the required checksum is not correct, return error message
     if (!($oSubscriber && $oSubscriber->getUnsubscribeChecksum() === $_REQUEST['checksum'])) {
         return $this->constructTemplate('unsubscribe_unknown_error');
     }
     SubscriberPeer::ignoreRights(true);
     // Count valid subscriptions [with display_name, not temp or import groups]
     $aSubscriberGroupMemberShips = $oSubscriber->getSubscriberGroupMemberships();
     $aValidSubscriptions = array();
     if (count($aSubscriberGroupMemberShips) > 1) {
         foreach ($aSubscriberGroupMemberShips as $oSubscriberGroupMembership) {
             if ($oSubscriberGroupMembership->getSubscriberGroup()->getDisplayName() == null) {
                 continue;
             }
             $aValidSubscriptions[] = $oSubscriberGroupMembership;
         }
     }
     // Display view with opt_out options if there is more then one valid subscription
     if (count($aValidSubscriptions) > 1) {
         $oTemplate = $this->constructTemplate('unsubscribe_optout_form');
         $oTemplate->replaceIdentifier('checksum', $_REQUEST['checksum']);
         $oTemplate->replaceIdentifier('email', $oSubscriber->getEmail());
         $bIsPostAndAllUnchecked = Manager::isPost() && !isset($_POST['subscriber_group_id']);
         foreach ($aValidSubscriptions as $oSubscriberGroupMemberships) {
             $oCheckboxTemplate = $this->constructTemplate('unsubscribe_optout_checkbox');
             $oCheckboxTemplate->replaceIdentifier('subscriber_group_id', $oSubscriberGroupMemberships->getSubscriberGroupId());
             $oCheckboxTemplate->replaceIdentifier('subscriber_group_name', $oSubscriberGroupMemberships->getSubscriberGroup()->getDisplayName());
             $oCheckboxTemplate->replaceIdentifier('checked', !$bIsPostAndAllUnchecked ? ' checked="checked"' : '', null, Template::NO_HTML_ESCAPE);
             $oTemplate->replaceIdentifierMultiple('subscriber_group_checkbox', $oCheckboxTemplate);
         }
         return $oTemplate;
     }
     // Delete subscriber because there is not a valid subscription (all temp subscriptions are removed too)
     $oSubscriber->delete();
     // Display unsubscribe confirmation international
     return $this->constructTemplate('unsubscribe_confirm');
 }
 public function getCriteria()
 {
     $oQuery = SubscriberQuery::create();
     $sJoinType = is_numeric($this->oDelegateProxy->getSubscriberGroupId()) ? Criteria::INNER_JOIN : Criteria::LEFT_JOIN;
     $oSubscriberGroupMembershipQuery = $oQuery->joinSubscriberGroupMembership(null, $sJoinType)->useQuery('SubscriberGroupMembership');
     if (is_numeric($this->oDelegateProxy->getSubscriberGroupId())) {
         $oSubscriberGroupMembershipQuery->filterBySubscriberGroupId($this->oDelegateProxy->getSubscriberGroupId());
     } else {
         if ($this->oDelegateProxy->getSubscriberGroupId() === CriteriaListWidgetDelegate::SELECT_WITHOUT) {
             $oSubscriberGroupMembershipQuery->filterBySubscriberGroupId(null, Criteria::ISNULL);
         }
     }
     $oSubscriberGroupMembershipQuery->endUse();
     return $oQuery->distinct();
 }
 /**
  * Get the associated Subscriber object
  *
  * @param PropelPDO $con Optional Connection object.
  * @param $doQuery Executes a query to get the object if required
  * @return Subscriber The associated Subscriber object.
  * @throws PropelException
  */
 public function getSubscriber(PropelPDO $con = null, $doQuery = true)
 {
     if ($this->aSubscriber === null && $this->subscriber_id !== null && $doQuery) {
         $this->aSubscriber = SubscriberQuery::create()->findPk($this->subscriber_id, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aSubscriber->addSubscriberGroupMemberships($this);
            */
     }
     return $this->aSubscriber;
 }
 private static function getSubscribersBySubscriberGroupMembership($aSubscriberGroupIds)
 {
     $oQuery = SubscriberQuery::create()->distinct();
     if ($aSubscriberGroupIds !== null) {
         $aSubscriberGroupIds = is_array($aSubscriberGroupIds) ? $aSubscriberGroupIds : array($aSubscriberGroupIds);
         $oQuery->joinSubscriberGroupMembership()->useSubscriberGroupMembershipQuery()->filterBySubscriberGroupId($aSubscriberGroupIds)->filterByOptInHash(null, Criteria::ISNULL)->endUse();
     }
     return $oQuery->orderByEmail()->find();
 }