123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace Ppb\Service;
- use Cube\Db\Expr,
- Ppb\Db\Table;
- class Newsletters extends AbstractService
- {
-
- protected $_recipients = array(
- 'all' => array(
- 'name' => 'All Users',
- 'query' => '1',
- 'table' => '\\Ppb\\Db\\Table\\Users',
- ),
- 'active' => array(
- 'name' => 'Active Users',
- 'query' => 'active = 1',
- 'table' => '\\Ppb\\Db\\Table\\Users',
- ),
- 'suspended' => array(
- 'name' => 'Suspended Users',
- 'query' => 'active = 0',
- 'table' => '\\Ppb\\Db\\Table\\Users',
- ),
- 'subscribers' => array(
- 'name' => 'Newsletter Subscribers',
- 'query' => '1',
- 'table' => '\\Ppb\\Db\\Table\\NewslettersSubscribers',
- ),
- 'store' => array(
- 'name' => 'Store Owners',
- 'query' => 'active = 1 AND store_active = 1',
- 'table' => '\\Ppb\\Db\\Table\\Users',
- ),
- );
-
- public function __construct()
- {
- parent::__construct();
- $this->setTable(
- new Table\Newsletters());
- }
-
- public function setRecipients(array $recipients)
- {
- $this->_recipients = $recipients;
- return $this;
- }
-
- public function getRecipients()
- {
- return $this->_recipients;
- }
-
- public function getRecipient($key)
- {
- if (array_key_exists($key, $this->_recipients)) {
- return $this->_recipients[$key];
- }
- return false;
- }
-
- public function saveRecipients($key, $id)
- {
- if (($recipient = $this->getRecipient($key)) !== false) {
- $newslettersRecipients = new Table\NewslettersRecipients();
- $tableClassName = $recipient['table'];
-
- $tableClass = new $tableClassName();
- $newslettersRecipients->getAdapter()
- ->query("INSERT INTO " . $newslettersRecipients->getPrefix() . $newslettersRecipients->getName() . "
- (newsletter_id, email)
- SELECT {$id}, email
- FROM " . $tableClass->getPrefix() . $tableClass->getName() . "
- WHERE {$recipient['query']}");
- return true;
- }
- return false;
- }
-
- public function save($data)
- {
- $row = null;
- $data = $this->_prepareSaveData($data);
- if (array_key_exists('id', $data)) {
- $select = $this->_table->select()
- ->where("id = ?", $data['id']);
- unset($data['id']);
- $row = $this->_table->fetchRow($select);
- }
- if (count($row) > 0) {
- $this->_table->update($data, "id='{$row['id']}'");
- }
- else {
- $data['created_at'] = new Expr('now()');
- $this->_table->insert($data);
- }
- return $this;
- }
-
- public function delete($id)
- {
- return $this->_table->delete(
- $this->_table->getAdapter()->quoteInto('id = ?', $id));
- }
- }
|