Offers.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ YJchxnH0uQ5jd3Aqxrw/zbzpGOURwX9sAwQ2lfy1W2s=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2016 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.6
  11. */
  12. /**
  13. * offers table service class
  14. */
  15. namespace Ppb\Service;
  16. use Ppb\Db\Table\Offers as OffersTable,
  17. Cube\Db\Expr;
  18. class Offers extends AbstractService
  19. {
  20. /**
  21. *
  22. * class constructor
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $this->setTable(
  28. new OffersTable());
  29. }
  30. /**
  31. *
  32. * create or update an offer on a listing
  33. * offers can be updated, but only if they have a 'pending' status.
  34. *
  35. * @param array $data
  36. *
  37. * @return int the id of the created/edited offer row
  38. */
  39. public function save($data)
  40. {
  41. $row = null;
  42. $data = $this->_prepareSaveData($data);
  43. if (array_key_exists('id', $data)) {
  44. $select = $this->_table->select()
  45. ->where("id = ?", $data['id']);
  46. unset($data['id']);
  47. $row = $this->_table->fetchRow($select);
  48. }
  49. if (count($row) > 0) {
  50. $data['updated_at'] = new Expr('now()');
  51. $this->_table->update($data, "id='{$row['id']}'");
  52. $id = $row['id'];
  53. }
  54. else {
  55. $data['created_at'] = new Expr('now()');
  56. $id = $this->_table->insert($data);
  57. if (!isset($data['topic_id'])) {
  58. $row = $this->findBy('id', $id);
  59. $row->save(array(
  60. 'topic_id' => $id,
  61. ));
  62. }
  63. }
  64. return $id;
  65. }
  66. }