ListingsWatch.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ MgG7FR96oeZh/MiAsxNz9tZgf6ZkJ1NpP3tQ34aquhc=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.4
  11. */
  12. /**
  13. * listings watch table service class
  14. */
  15. namespace Ppb\Service;
  16. use Cube\Db\Expr,
  17. Ppb\Db\Table\ListingsWatch as ListingsWatchTable;
  18. class ListingsWatch extends AbstractService
  19. {
  20. /**
  21. *
  22. * class constructor
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $this->setTable(
  28. new ListingsWatchTable());
  29. }
  30. /**
  31. *
  32. * create or update a row in the listings watch table
  33. *
  34. * @param array $data
  35. *
  36. * @return $this
  37. */
  38. public function save($data)
  39. {
  40. $row = null;
  41. $data = $this->_prepareSaveData($data);
  42. if (array_key_exists('id', $data)) {
  43. $select = $this->_table->select()
  44. ->where("id = ?", $data['id']);
  45. unset($data['id']);
  46. $row = $this->_table->fetchRow($select);
  47. }
  48. if (count($row) > 0) {
  49. $this->_table->update($data, "id='{$row['id']}'");
  50. }
  51. else {
  52. $data['created_at'] = new Expr('now()');
  53. $this->_table->insert($data);
  54. }
  55. return $this;
  56. }
  57. /**
  58. *
  59. * delete data from the table
  60. *
  61. * @param int|array $listingIds the id of the listing(s)
  62. * @param int $userId the id of the user that is watching the listing
  63. * @param string $userToken user token cookie
  64. *
  65. * @return int returns the number of affected rows
  66. */
  67. public function delete($listingIds, $userId, $userToken)
  68. {
  69. $adapter = $this->_table->getAdapter();
  70. if (!is_array($listingIds)) {
  71. $listingIds = array($listingIds);
  72. }
  73. $where[] = $adapter->quoteInto('listing_id IN (?)', $listingIds);
  74. if ($userId !== null) {
  75. $where[] = 'user_token = "' . $userToken . '" OR user_id = "' . $userId . '"';
  76. }
  77. else {
  78. $where[] = $adapter->quoteInto('user_token = ?', $userToken);
  79. }
  80. return $this->_table->delete($where);
  81. }
  82. }