* @license PHP License * @package WB */ /** * Load classes */ WBClass::load('WBDatasource_Newsletter' , 'WBDatasource_Callback' ); /** * Wombat Newsletter System Receiver * * @version 1.5.4 * @package WB */ class WBDatasource_Newsletter_Receiver extends WBDatasource_Newsletter implements Iterator, Countable, WBDatasource_Callback { /** * Current Language * @var string */ private $lang = 'de'; /** * Check for proper language * @var bool */ private $useLang = true; /** * Verify check flag * @var bool */ private $useChecked = true; /** * Check for proper approved flag (double-opt-in) * @var bool */ private $useApproved = true; /** * Verify subscription timestamp * @var bool */ private $checkSubscriptionTimestamp = true; /** * List of Subscribers * @var array */ private $subscribers = array(); /** * Current Pager * @var WBDatasource_Pager */ private $pager; /** * @var array */ private $topic = array(); /** * @var array */ private $pagerList = array(); /** * @var array */ private $pagerInfo = array(); /** * Newsletter Issue * @var WBDatasource_Newsletter_Issue */ private $issue; /** * Tell Which Newsletter Issue 2 Mail * * Set issue object * @param WBDatasource_Newsletter_Issue */ public function setIssue(WBDatasource_Newsletter_Issue $issue) { $this->issue = $issue; } /** * Set Language Code * * @param string $lang */ public function setLang($lang) { $this->lang = $lang; $this->resetPager(); } /** * Use Language to Select Subscriber * * @param bool */ public function useLang($use = true) { $this->useLang = $use; $this->resetPager(); } /** * Use Language to Select Subscriber * * @param bool */ public function checkSubscriptionTimestamp($check = true) { $this->checkSubscriptionTimestamp = $check; $this->resetPager(); } /** * Use Approved Flag to Select Subscriber * * Implement Double-Opt-In * @param bool */ public function useApproved($use = true) { $this->useApproved = $use; $this->resetPager(); } /** * Use Checked Flag to Select Subscriber * * Implement Double-Opt-In * @param bool */ public function useChecked($use = true) { $this->useChecked = $use; $this->resetPager(); } public function setSubscribers($sub) { $this->subscribers = $sub; $this->resetPager(); } public function useTopic($list) { $this->topic = $list; $this->resetPager(); } /** * */ public function setSourceNamespaces() { } /** * Mark Newsletter Issue as Send to Subscriber * * @param string newsletter issue id * @param string subscriber id * @param string mark status */ public function markSent($issueId, $subId = null, $status = WBDatasource_Newsletter::SEND_STATUS_SENT) { if (empty($subId)) { if (!$this->valid()) { return; } $sub = $this->current(); $subId = $sub[$this->table->getIdentifier(WBDatasource_Newsletter::TABLE_SUBSCRIBER)]; } $clause = array(); $pIss = $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_NEWSLETTERISSUE); if (!is_array($pIss)) { $pIss = array($pIss); } if (!is_array($issueId)) { $issueId = explode('-', $issueId); } if (count($issueId) != count($pIss)) { $e = array( 'msg' => sprintf('Primary key of issue seems to be currupt. Expected %d parts, got %d parts', count($pIss), count($issueId)), 'code' => 1, 'class' => __CLASS__ ); throw WBClass::create('WBException_Argument', $e); } $old = $this->doGetSent($issueId, $subId, $clause); if (empty($old)) { $save = array( $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_SUBSCRIBER, true) => $subId, 'status' => $status ); foreach ($pIss as $i => $p) { $save[$p] = $issueId[$i]; } $this->table->save(WBDatasource_Newsletter::TABLE_SUBSCRIBERSENT, '__new', $save); return; } $save = array( 'status' => $status ); $this->table->save(WBDatasource_Newsletter::TABLE_SUBSCRIBERSENT, null, $save, $clause); } /** * Get Subscriber Send Status * * @param string newsletter id * @param string subscriber id, optional * @return array */ public function getSent($issueId, $subId = null) { if (empty($subId)) { if (!$this->valid()) { return; } $sub = $this->current(); $subId = $sub[$this->table->getIdentifier(WBDatasource_Newsletter::TABLE_SUBSCRIBER)]; } if (!is_array($issueId)) { $issueId = explode('-', $issueId); } $clause = array(); return $this->doGetSent($issueId, $subId, $clause); } /** * Load Subscriber's Send Status * * @param string newsletter id * @param string subscriber id, optional * @param array clause * @return array */ private function doGetSent($issueId, $subId, &$clause) { $pIss = $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_NEWSLETTERISSUE); if (!is_array($pIss)) { $pIss = array($pIss); } $clause = array(); $clause[] = array( 'field' => $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_SUBSCRIBER), 'value' => $subId ); foreach ($pIss as $i => $p) { $clause[] = array( 'field' => $p, 'value' => $issueId[$i] ); } $old = $this->table->get(WBDatasource_Newsletter::TABLE_SUBSCRIBERSENT, null, null, $clause); if (1 > count($old)) { return array(); } return $old[0]; } /** * Countable Interface * * Count number of selected recipients * @return integet */ public function count() { $this->startPager(); if (empty($this->pagerInfo)) { $this->pagerInfo = $this->pager->browse('__first'); } return $this->pagerInfo['total']; } /** * Flush Pager Information * * @see startPager() */ private function resetPager() { $this->pager = null; $this->pagerInfo = array(); $this->pagerList = array(); } /** * Begin waling Through All Recipients * * @see WBDatasource_Pager */ private function startPager() { if (!empty($this->pager)) { return; } $pSub = $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_SUBSCRIBER); $clause = array(); if ($this->useLang) { $clause[] = array( 'field' => 'lang', 'value' => $this->lang ); } if (!empty($this->subscribers)) { $clause[] = array( 'field' => $pSub, 'relation' => 'in', 'value' => $this->subscribers ); } // double opt in if ($this->useApproved) { $clause[] = array( 'field' => 'approved', 'value' => 1 ); } // check flag if ($this->useChecked) { $clause[] = array( 'field' => 'checked', 'value' => 1 ); } $clause[] = array( 'field' => 'enabled', 'value' => 1 ); $clause[] = array( 'field' => 'subscribed', 'value' => 1 ); $clause[] = array( 'field' => 'blacklisted', 'value' => 0 ); // subscription timestamp if ($this->checkSubscriptionTimestamp) { $clause[] = array( 'field' => 'subscribedtimestamp', 'relation' => 'gt', 'value' => '1970-01-01 00:00:00' ); } $options = array( 'callback' => $this, 'limit' => 100, 'groupby' => array( 'field' => $pSub ), 'distinct' => $pSub ); $pMan = $this->table->getIdentifier(WBDatasource::TABLE_MANDATOR, true); // subscriber listed for mandator if (!empty($this->issue)) { if (empty($options['join'])) { $options['join'] = array(); } $options['join'][] = array( 'table' => WBDatasource_Newsletter::TABLE_SUBSCRIBERMANDATOR, 'using' => $pSub ); $clause[] = array( 'table' => WBDatasource_Newsletter::TABLE_SUBSCRIBERMANDATOR, 'field' => $pMan, 'value' => $this->issue->get($pMan) ); } // subscriber enrolled for topics if (!empty($this->topic)) { if (empty($options['join'])) { $options['join'] = array(); } $options['join'][] = array( 'table' => WBDatasource_Newsletter::TABLE_NEWSLETTERSUBSCRIBERTOPIC, 'using' => $pSub ); $pTop = $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_NEWSLETTERTOPIC, true); $topic = array(); foreach ($this->topic as $t) { $topic[] = $t[$pTop]; } $clause[] = array( 'table' => WBDatasource_Newsletter::TABLE_NEWSLETTERSUBSCRIBERTOPIC, 'field' => $pMan, 'value' => $this->topic[0][$pMan] ); $clause[] = array( 'table' => WBDatasource_Newsletter::TABLE_NEWSLETTERSUBSCRIBERTOPIC, 'field' => $pTop, 'value' => $topic, 'relation' => 'in' ); } $cs = md5(__CLASS__ . serialize($clause)); $this->pager = $this->table->getPager($cs, WBDatasource_Newsletter::TABLE_SUBSCRIBER, null, $clause, $options); $this->pager->setCountGroup(array()); } /** * Iterator Interface: rewind */ public function rewind() { $this->startPager(); $this->pagerInfo = $this->pager->browse('__first'); $this->pagerInfo['pos'] = 0; $this->pagerList = $this->pager->get(); } /** * Iterator Interface: current * * @return array */ public function current() { return $this->pagerList[$this->pagerInfo['pos']]; } /** * Iterator Interface: key * * @return int */ public function key() { return $this->pagerInfo['current'] * $this->pagerInfo['limit'] + $this->pagerInfo['pos']; } /** * Iterator Interface: next */ public function next() { ++$this->pagerInfo['pos']; if ($this->pagerInfo['pos'] < count($this->pagerList)) { return; } if (1 > $this->pagerInfo['next']) { return; } $this->pagerInfo = $this->pager->browse($this->pagerInfo['page_next']); $this->pagerInfo['pos'] = 0; $this->pagerList = $this->pager->get(); } /** * Iterator Interface: valid * * @return bool */ public function valid() { if ($this->pagerInfo['total'] > $this->key()) { return true; } return false; } /** * callback when receive whole entry * * @param string $src name of source * @param string $key * @param array $data loaded data * @return bool true on success */ public function onDatasourceGet($src, $key, &$data) { if ($src != WBDatasource_Newsletter::TABLE_SUBSCRIBER) { return true; } $data['obscureid'] = $data['obscure'] . $data['id']; $data['salutation'] = $this->getSalutation4SubscriberData($data); return true; } /** * Get Salutation * * Use given subscriber data to figure out proper salutation * * @param array * @return string */ public function getSalutation4SubscriberData($data) { $par = array('table' => $this->table); /** @var WBUte_Person_Salutation */ $sal = WBClass::create('WBUte_Person_Salutation', $par); $sal->loadById($data[$sal->getIdentifier()]); return $sal->getHello($data); } /** * Get Salutation * * Use given subscriber data to figure out proper salutation * * @param array * @return string */ public function OLDgetSalutation4SubscriberData($data) { $genderMale = 'Sehr geehrter Herr'; $genderFemale = 'Sehr geehrte Frau'; $emptySaluttion = 'Sehr geehrte Damen und Herren'; // lang $lang = strtoupper($data['lang']); $lang = substr($lang, 0, 2); switch ($lang) { case 'DE': break; default: $lang = 'EN'; $genderMale = 'Dear Mr.'; $genderFemale = 'Dear Ms.'; $emptySaluttion = 'Dear Sir or Madam'; break; } $salutation = array(); switch ($data['gender']) { case 'male': $salutation[] = $genderMale; break; case 'female': $salutation[] = $genderFemale; break; default: break; } if (!empty($data['title'])) { $tmp = strtolower($data['title']); if (0 == strncmp('prof', $tmp, 5)) { switch ($lang) { case 'DE': if (!empty($salutation)) { $salutation[] = $data['title']; } break; default: $salutation = array('Dear'); $salutation[] = $data['title']; break; } } else if (0 == strncmp('dr', $tmp, 2)) { switch ($lang) { case 'DE': if (!empty($salutation)) { $salutation[] = $data['title']; } break; default: $salutation = array('Dear'); $salutation[] = $data['title']; break; } } } if (!empty($salutation) && !empty($data['surname'])) { $salutation[] = $data['surname']; } // default salutation if (empty($salutation)) { $salutation[] = $emptySaluttion; } $salutation = implode(' ', $salutation) . ','; return $salutation; } }