* @license PHP License * @package WB */ /** * Load classes */ WBClass::load('WBDatasource_Newsletter'); /** * Wombat Newsletter System Receiver * * @version 1.4.0 * @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(); /** * 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(); $old = $this->doGetSent($issueId, $subId, $clause); if (empty($old)) { $save = array( $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_NEWSLETTERISSUE) => $issueId, $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_SUBSCRIBER) => $subId, 'status' => $status ); $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)]; } $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) { $clause = array(); $clause[] = array( 'field' => $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_NEWSLETTERISSUE), 'value' => $issueId ); $clause[] = array( 'field' => $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_SUBSCRIBER), 'value' => $subId ); $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 ); if (!empty($this->topic)) { $options['join'] = array(); $options['join'][] = array( 'table' => WBDatasource_Newsletter::TABLE_NEWSLETTERSUBSCRIBERTOPIC, 'using' => $pSub ); $pTop = $this->table->getIdentifier(WBDatasource_Newsletter::TABLE_NEWSLETTERTOPIC); $topic = array(); foreach ($this->topic as $t) { $topic[] = $t[$pTop]; } $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); } /** * 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) { $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; } }