SaleListing.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ frM83EfMJNNl8fx784BDxX43wCl+htv9GnRQlIdsyAU=
  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.5
  11. */
  12. /**
  13. * sales listings table row object model
  14. */
  15. namespace Ppb\Db\Table\Row;
  16. use Ppb\Service,
  17. Cube\Controller\Front,
  18. Cube\Crypt;
  19. class SaleListing extends AbstractRow
  20. {
  21. /**
  22. *
  23. * crypt object
  24. *
  25. * @var \Cube\Crypt
  26. */
  27. protected $_crypt;
  28. /**
  29. *
  30. * set crypt object
  31. *
  32. * @param \Cube\Crypt $crypt
  33. *
  34. * @return $this
  35. */
  36. public function setCrypt(Crypt $crypt)
  37. {
  38. $this->_crypt = $crypt;
  39. return $this;
  40. }
  41. /**
  42. *
  43. * get crypt object
  44. *
  45. * @return \Cube\Crypt
  46. */
  47. public function getCrypt()
  48. {
  49. if (!$this->_crypt instanceof Crypt) {
  50. $options = Front::getInstance()->getOption('session');
  51. $crypt = new Crypt();
  52. $crypt->setKey($options['secret']);
  53. $this->setCrypt($crypt);
  54. }
  55. return $this->_crypt;
  56. }
  57. /**
  58. *
  59. * return the price of a sale listing, after applying the voucher code
  60. *
  61. * @param boolean $applyVoucher
  62. *
  63. * @return float
  64. */
  65. public function price($applyVoucher = false)
  66. {
  67. $price = $this->getData('price');
  68. if ($applyVoucher) {
  69. /** @var \Ppb\Db\Table\Row\Sale $sale */
  70. $sale = $this->findParentRow('\Ppb\Db\Table\Sales');
  71. $voucher = $sale->getVoucher();
  72. if ($voucher instanceof Voucher) {
  73. $price = $voucher->apply($price, $sale['currency'], $this->getData('listing_id'));
  74. }
  75. }
  76. return $price;
  77. }
  78. /**
  79. *
  80. * calculates the total amount of a sale row
  81. *
  82. * @param boolean $applyVoucher
  83. *
  84. * @return float
  85. */
  86. public function calculateTotal($applyVoucher)
  87. {
  88. return ($this->price($applyVoucher) * $this->getData('quantity'));
  89. }
  90. /**
  91. *
  92. * get digital downloads rowset or false if no downloads are available
  93. *
  94. * @param int $listingMediaId the id of a specific download file we want to retrieve
  95. *
  96. * @return \Ppb\Db\Table\Rowset\ListingsMedia|false
  97. */
  98. public function getDigitalDownloads($listingMediaId = null)
  99. {
  100. /** @var \Ppb\Db\Table\Row\Listing $listing */
  101. $listing = $this->findParentRow('\Ppb\Db\Table\Listings');
  102. /** @var \Ppb\Db\Table\Row\Sale $sale */
  103. $sale = $this->findParentRow('\Ppb\Db\Table\Sales');
  104. if ($listing) {
  105. $select = $this->getTable()->select()
  106. ->where('type = ?', Service\ListingsMedia::TYPE_DOWNLOAD)
  107. ->order('id DESC');
  108. if ($listingMediaId) {
  109. $select->where('id = ?', $listingMediaId);
  110. }
  111. $rowset = $listing->findDependentRowset('\Ppb\Db\Table\ListingsMedia', null, $select);
  112. $downloadsData = $this->getDownloadsData();
  113. // we get all downloads associated with the listing
  114. if (count($rowset)) {
  115. foreach ($rowset as $row) {
  116. if (!$sale->isActive() || !$this->getData('downloads_active')) {
  117. $row['active'] = 0;
  118. $row['download_link'] = null;
  119. }
  120. else {
  121. $row['active'] = isset($downloadsData[$row['id']]['active']) ?
  122. $downloadsData[$row['id']]['active'] : $this->getData('downloads_active');
  123. $row['download_link'] = array(
  124. 'module' => 'members',
  125. 'controller' => 'buying',
  126. 'action' => 'download',
  127. 'key' => $this->_generateDownloadKey($row['id'], $this->getData('id')),
  128. );
  129. }
  130. $row['nb_downloads'] = isset($downloadsData[$row['id']]['nb_downloads']) ?
  131. $downloadsData[$row['id']]['nb_downloads'] : 0;
  132. }
  133. if ($listingMediaId) {
  134. return $rowset->getRow(0);
  135. }
  136. return $rowset;
  137. }
  138. }
  139. return false;
  140. }
  141. /**
  142. *
  143. * update downloads data serializable field
  144. *
  145. * @param int $listingMediaId
  146. * @param int $active
  147. * @param bool $download
  148. *
  149. * @return $this
  150. */
  151. public function updateDownloadsData($listingMediaId, $active = null, $download = false)
  152. {
  153. $downloadsData = $this->getDownloadsData();
  154. if ($active !== null) {
  155. $downloadsData[$listingMediaId]['active'] = $active;
  156. }
  157. if ($download !== false) {
  158. $downloadsData[$listingMediaId]['nb_downloads']++;
  159. }
  160. $this->save(array(
  161. 'downloads_data' => $downloadsData,
  162. ));
  163. return $this;
  164. }
  165. /**
  166. *
  167. * get download data for a certain listing media id
  168. *
  169. * @param int $listingMediaId
  170. *
  171. * @return array|null
  172. */
  173. public function getDownloadsData($listingMediaId = null)
  174. {
  175. $downloadsData = \Ppb\Utility::unserialize($this->getData('downloads_data'), array());
  176. if ($listingMediaId !== null) {
  177. return isset($downloadsData[$listingMediaId]) ? $downloadsData[$listingMediaId] : null;
  178. }
  179. return $downloadsData;
  180. }
  181. /**
  182. *
  183. * count a downloaded file
  184. *
  185. * @param $listingMediaId
  186. *
  187. * @return $this
  188. */
  189. public function countDownload($listingMediaId)
  190. {
  191. $downloadsData = $this->getDownloadsData();
  192. if (array_key_exists($listingMediaId, $downloadsData)) {
  193. if (array_key_exists('nb_downloads', $downloadsData[$listingMediaId])) {
  194. $downloadsData[$listingMediaId]['nb_downloads']++;
  195. }
  196. else {
  197. $downloadsData[$listingMediaId]['nb_downloads'] = 1;
  198. }
  199. }
  200. else {
  201. $downloadsData[$listingMediaId] = array(
  202. 'nb_downloads' => 1,
  203. );
  204. }
  205. $this->save(array(
  206. 'downloads_data' => serialize($downloadsData)
  207. ));
  208. return $this;
  209. }
  210. /**
  211. *
  212. * delete the sale listing row and its parent sale it has no other listings in it
  213. *
  214. * @return int
  215. */
  216. public function delete()
  217. {
  218. $salesListings = $this->_table->fetchAll(
  219. $this->_table->select()
  220. ->where('sale_id = ?', $this->getData('sale_id'))
  221. ->where('id != ?', $this->getData('id')));
  222. if (!count($salesListings)) {
  223. $this->findParentRow('\Ppb\Db\Table\Sales')->delete(true);
  224. }
  225. return parent::delete();
  226. }
  227. /**
  228. *
  229. * generate an encrypted download key
  230. *
  231. * @param int $listingMediaId
  232. * @param int $saleListingId
  233. *
  234. * @return string
  235. */
  236. protected function _generateDownloadKey($listingMediaId, $saleListingId)
  237. {
  238. return $this->getCrypt()->encrypt(
  239. $listingMediaId . Service\Table\SalesListings::KEY_SEPARATOR . $saleListingId);
  240. }
  241. }