Bid.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ CQPHe2YMpWhQHD7VvBO8Gd452CmyYZvNE5oHHB2SjgE=
  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.0
  11. */
  12. /**
  13. * bids table row object model
  14. */
  15. namespace Ppb\Db\Table\Row;
  16. use Ppb\Service,
  17. Cube\Db\Expr;
  18. class Bid extends AbstractRow
  19. {
  20. /**
  21. * bid statuses
  22. */
  23. const STATUS_HIGH_BID = 0;
  24. const STATUS_OUTBID = 1;
  25. /**
  26. *
  27. * allowed bid statuses
  28. *
  29. * @var array
  30. */
  31. public static $statuses = array(
  32. self::STATUS_HIGH_BID => 'High Bid',
  33. self::STATUS_OUTBID => 'Outbid',
  34. );
  35. /**
  36. *
  37. * check if the logged in user can retract the bid
  38. * (only the poster can retract it, and only high bids can be retracted)
  39. *
  40. * @param \Ppb\Db\Table\Row\Listing $listing
  41. * @return bool
  42. */
  43. public function canRetract(Listing $listing = null)
  44. {
  45. $settings = $this->getSettings();
  46. if ($settings['enable_bid_retraction']) {
  47. if ($listing === null) {
  48. /** @var \Ppb\Db\Table\Row\Listing $listing */
  49. $listing = $this->findParentRow('\Ppb\Db\Table\Listings');
  50. }
  51. if (
  52. !$listing->getData('closed') &&
  53. (strtotime($listing->getData('end_time')) > (time() + ($settings['bid_retraction_hours'] * 60 * 60)))
  54. ) {
  55. $user = $this->getUser();
  56. if ($this->getData('user_id') == $user['id'] && !$this->getData('outbid')) {
  57. return true;
  58. }
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. *
  65. * retract bid
  66. * theory of operation:
  67. * - first thing, all bids from the same user will have the maximum
  68. * bid column updated to be equal with the amount field
  69. * - the bid will be deleted
  70. * - we retrieve the next bid, we delete it and place it again
  71. * so that the new high bid has its amount set correctly
  72. *
  73. * @return bool
  74. */
  75. public function retract()
  76. {
  77. /** @var \Ppb\Db\Table\Row\Listing $listing */
  78. $listing = $this->findParentRow('\Ppb\Db\Table\Listings');
  79. if ($this->canRetract($listing)) {
  80. $adapter = $this->getTable()->getAdapter();
  81. $this->getTable()->update(
  82. array(
  83. 'maximum_bid' => new Expr('amount')
  84. ),
  85. array(
  86. $adapter->quoteInto('listing_id = ?', $listing['id']),
  87. $adapter->quoteInto('user_id = ?', $this->getData('user_id')),
  88. $adapter->quoteInto('outbid = ?', 1)
  89. ));
  90. $bid = clone $this;
  91. $this->delete();
  92. $bidsService = new Service\Bids();
  93. $highBid = $bidsService->fetchAll(
  94. $bidsService->getTable()->select()
  95. ->where('listing_id = ?', $listing['id'])
  96. ->order('id DESC')
  97. )->getRow(0);
  98. if ($highBid instanceof Bid) {
  99. $userId = $highBid->getData('user_id');
  100. $data = array(
  101. 'amount' => $highBid->getData('maximum_bid')
  102. );
  103. $highBid->delete();
  104. $listing->placeBid($data, 'bid', $userId);
  105. }
  106. // send mail notification to seller TODO: call not ideal
  107. $mail = new \Listings\Model\Mail\OwnerNotification();
  108. $mail->bidRetraction($listing, $bid)->send();
  109. return true;
  110. }
  111. return false;
  112. }
  113. }