Newsletters.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ IdzhVuypRXQ/FsJC7Y7dyBy7ikL2xkuzLaf2iCtdwZg=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.9 [rev.7.9.01]
  11. */
  12. /**
  13. * newsletters table service class
  14. */
  15. namespace Ppb\Service;
  16. use Cube\Db\Expr,
  17. Ppb\Db\Table;
  18. class Newsletters extends AbstractService
  19. {
  20. /**
  21. *
  22. * newsletters recipients array
  23. *
  24. * @var array
  25. */
  26. protected $_recipients = array(
  27. 'all' => array(
  28. 'name' => 'All Users',
  29. 'query' => '1',
  30. 'table' => '\\Ppb\\Db\\Table\\Users',
  31. ),
  32. 'active' => array(
  33. 'name' => 'Active Users',
  34. 'query' => 'active = 1',
  35. 'table' => '\\Ppb\\Db\\Table\\Users',
  36. ),
  37. 'suspended' => array(
  38. 'name' => 'Suspended Users',
  39. 'query' => 'active = 0',
  40. 'table' => '\\Ppb\\Db\\Table\\Users',
  41. ),
  42. 'subscribers' => array(
  43. 'name' => 'Newsletter Subscribers',
  44. 'query' => '1',
  45. 'table' => '\\Ppb\\Db\\Table\\NewslettersSubscribers',
  46. ),
  47. 'store' => array(
  48. 'name' => 'Store Owners',
  49. 'query' => 'active = 1 AND store_active = 1',
  50. 'table' => '\\Ppb\\Db\\Table\\Users',
  51. ),
  52. );
  53. /**
  54. *
  55. * class constructor
  56. */
  57. public function __construct()
  58. {
  59. parent::__construct();
  60. $this->setTable(
  61. new Table\Newsletters());
  62. }
  63. /**
  64. *
  65. * set recipients array
  66. *
  67. * @param array $recipients
  68. *
  69. * @return $this
  70. */
  71. public function setRecipients(array $recipients)
  72. {
  73. $this->_recipients = $recipients;
  74. return $this;
  75. }
  76. /**
  77. *
  78. * get recipients array
  79. *
  80. * @return array
  81. */
  82. public function getRecipients()
  83. {
  84. return $this->_recipients;
  85. }
  86. /**
  87. *
  88. * get recipient by key
  89. *
  90. * @param string $key
  91. *
  92. * @return array|false
  93. */
  94. public function getRecipient($key)
  95. {
  96. if (array_key_exists($key, $this->_recipients)) {
  97. return $this->_recipients[$key];
  98. }
  99. return false;
  100. }
  101. /**
  102. *
  103. * save newsletter recipients in the recipients table
  104. * return the number recipients saved or false
  105. *
  106. * @param string $key recipients to send to
  107. * @param int $id newsletter id
  108. *
  109. * @return int|false
  110. */
  111. public function saveRecipients($key, $id)
  112. {
  113. if (($recipient = $this->getRecipient($key)) !== false) {
  114. $newslettersRecipients = new Table\NewslettersRecipients();
  115. $tableClassName = $recipient['table'];
  116. /** @var \Cube\Db\Table\AbstractTable $tableClass */
  117. $tableClass = new $tableClassName();
  118. $newslettersRecipients->getAdapter()
  119. ->query("INSERT INTO " . $newslettersRecipients->getPrefix() . $newslettersRecipients->getName() . "
  120. (newsletter_id, email)
  121. SELECT {$id}, email
  122. FROM " . $tableClass->getPrefix() . $tableClass->getName() . "
  123. WHERE {$recipient['query']}");
  124. return true;
  125. }
  126. return false;
  127. }
  128. /**
  129. *
  130. * create or update a newsletter
  131. *
  132. * @param array $data
  133. *
  134. * @return $this
  135. */
  136. public function save($data)
  137. {
  138. $row = null;
  139. $data = $this->_prepareSaveData($data);
  140. if (array_key_exists('id', $data)) {
  141. $select = $this->_table->select()
  142. ->where("id = ?", $data['id']);
  143. unset($data['id']);
  144. $row = $this->_table->fetchRow($select);
  145. }
  146. if (count($row) > 0) {
  147. // $data['updated_at'] = new Expr('now()');
  148. $this->_table->update($data, "id='{$row['id']}'");
  149. }
  150. else {
  151. $data['created_at'] = new Expr('now()');
  152. $this->_table->insert($data);
  153. }
  154. return $this;
  155. }
  156. /**
  157. *
  158. * delete a newsletter from the table
  159. *
  160. * @param integer $id the id of the newsletter
  161. *
  162. * @return integer returns the number of affected rows
  163. */
  164. public function delete($id)
  165. {
  166. return $this->_table->delete(
  167. $this->_table->getAdapter()->quoteInto('id = ?', $id));
  168. }
  169. }