NewslettersSubscribers.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ L9SKSvbScN/0fh0xdyWgPzBF9phz7F3GzNFzsJocnVOePsPkriFe27vLNLvaIS0hfcJWOFGlmVF6N4FZnp45MA==
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.9 [rev.7.9.01]
  11. */
  12. /**
  13. * newsletters subscribers table service class
  14. */
  15. namespace Ppb\Service;
  16. use Ppb\Db\Table\NewslettersSubscribers as NewslettersSubscribersTable,
  17. Cube\Db\Expr,
  18. Ppb\Db\Table\Row\User as UserModel;
  19. class NewslettersSubscribers extends AbstractService
  20. {
  21. /**
  22. *
  23. * class constructor
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->setTable(
  29. new NewslettersSubscribersTable());
  30. }
  31. /**
  32. *
  33. * create or update an newsletter subscriber
  34. *
  35. *
  36. * @param array $data (newsletter_subscription [flag], email, user_id)
  37. *
  38. * @return bool
  39. */
  40. public function save($data)
  41. {
  42. $row = null;
  43. $user = null;
  44. $saved = false;
  45. if (array_key_exists('user_id', $data)) {
  46. $usersService = new Users();
  47. $user = $usersService->findBy('id', $data['user_id']);
  48. }
  49. if (array_key_exists('newsletter_subscription', $data)) {
  50. // unsubscribe by email
  51. if (!$data['newsletter_subscription']) {
  52. if ($user instanceof UserModel) {
  53. $this->deleteOne('user_id', $user['id']);
  54. }
  55. $this->deleteOne('email', $data['email']);
  56. $saved = true;
  57. }
  58. }
  59. if ($saved === false) {
  60. // we have a subscription creation / update
  61. $saved = $this->_saveSubscription($user, $data['email']);
  62. }
  63. return $saved;
  64. }
  65. /**
  66. *
  67. * delete subscriber(s) and uncheck newsletter_subscription checkbox if thats the case
  68. *
  69. * @param string $column
  70. * @param mixed $value
  71. *
  72. * @return int returns the number of affected rows
  73. */
  74. public function deleteOne($column, $value)
  75. {
  76. $adapter = $this->_table->getAdapter();
  77. $where[] = $adapter->quoteInto("$column = ?", $value);
  78. $usersService = new Users();
  79. $user = null;
  80. if ($column == 'user_id') {
  81. $user = $usersService->findBy('id', $value);
  82. }
  83. else if ($column == 'email') {
  84. $user = $usersService->findBy('email', $value);
  85. }
  86. if ($user instanceof UserModel) {
  87. $user->save(array(
  88. 'newsletter_subscription' => 0,
  89. ));
  90. }
  91. return $this->_table->delete($where);
  92. }
  93. /**
  94. *
  95. * create or update a subscriber
  96. *
  97. * @param \PPb\Db\Table\Row\User $user
  98. * @param string $email
  99. *
  100. * @return bool
  101. */
  102. protected function _saveSubscription($user, $email)
  103. {
  104. $rowByEmail = $this->findBy('email', $email);
  105. $rowByUser = null;
  106. if ($user instanceof UserModel) {
  107. $rowByUser = $this->findBy('user_id', $user['id']);
  108. }
  109. $saved = true;
  110. if (count($rowByUser) > 0) {
  111. if ($rowByEmail)
  112. $this->_table->update(array('email' => $email), "id='{$rowByUser['id']}'");
  113. }
  114. else if (count($rowByEmail) > 0) {
  115. if ($user instanceof UserModel) {
  116. $this->_table->update(array('user_id' => $user['id']), "id='{$rowByEmail['id']}'");
  117. }
  118. else {
  119. $saved = false;
  120. }
  121. }
  122. else { // we have a new row altogether
  123. $this->_table->insert(array(
  124. 'user_id' => (!empty($user['id'])) ? $user['id'] : new Expr('null'),
  125. 'email' => $email,
  126. 'created_at' => new Expr('now()'),
  127. ));
  128. }
  129. return $saved;
  130. }
  131. }