Listings.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 7TpWuUNndfHVwBDz1rUqzAoDsI0XT6AA/3NqEqrjIMY=
  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.8
  11. */
  12. /**
  13. * listings table rowset class
  14. */
  15. namespace Ppb\Db\Table\Rowset;
  16. use Cube\Db\Expr,
  17. Ppb\Service;
  18. class Listings extends AbstractRowset
  19. {
  20. /**
  21. * email notifications keys
  22. */
  23. const EMAIL_CLOSED = 'listingsClosed';
  24. const EMAIL_SUSPENDED = 'listingsSuspended';
  25. const EMAIL_RELISTED = 'listingsRelisted';
  26. const EMAIL_APPROVED = 'listingsApproved';
  27. /**
  28. *
  29. * row object class
  30. *
  31. * @var string
  32. */
  33. protected $_rowClass = '\Ppb\Db\Table\Row\Listing';
  34. /**
  35. *
  36. * automatic flag
  37. *
  38. * @var bool
  39. */
  40. protected $_automatic = false;
  41. /**
  42. *
  43. * admin flag
  44. *
  45. * @var bool
  46. */
  47. protected $_admin = false;
  48. /**
  49. *
  50. * listings service
  51. *
  52. * @var \Ppb\Service\Listings
  53. */
  54. protected $_listings;
  55. /**
  56. *
  57. * users service
  58. *
  59. * @var \Ppb\Service\Users
  60. */
  61. protected $_users;
  62. /**
  63. *
  64. * output messages
  65. *
  66. * @var array
  67. */
  68. protected $_messages = array();
  69. /**
  70. *
  71. * actions counter
  72. *
  73. * @var int
  74. */
  75. protected $_counter = 0;
  76. /**
  77. *
  78. * get listings service
  79. *
  80. * @return \Ppb\Service\Listings
  81. */
  82. public function getListings()
  83. {
  84. if (!$this->_listings instanceof Service\Listings) {
  85. $this->setListings(
  86. new Service\Listings());
  87. }
  88. return $this->_listings;
  89. }
  90. /**
  91. *
  92. * set listings service
  93. *
  94. * @param \Ppb\Service\Listings $listings
  95. *
  96. * @return $this
  97. */
  98. public function setListings(Service\Listings $listings)
  99. {
  100. $this->_listings = $listings;
  101. return $this;
  102. }
  103. /**
  104. *
  105. * get users service
  106. *
  107. * @return \Ppb\Service\Users
  108. */
  109. public function getUsers()
  110. {
  111. if (!$this->_users instanceof Service\Users) {
  112. $this->setUsers(
  113. new Service\Users());
  114. }
  115. return $this->_users;
  116. }
  117. /**
  118. *
  119. * set users service
  120. *
  121. * @param \Ppb\Service\Users $users
  122. *
  123. * @return $this
  124. */
  125. public function setUsers(Service\Users $users)
  126. {
  127. $this->_users = $users;
  128. return $this;
  129. }
  130. /**
  131. *
  132. * set admin flag
  133. *
  134. * @param boolean $admin
  135. *
  136. * @return $this
  137. */
  138. public function setAdmin($admin)
  139. {
  140. $this->_admin = $admin;
  141. return $this;
  142. }
  143. /**
  144. *
  145. * get admin flag
  146. *
  147. * @return boolean
  148. */
  149. public function getAdmin()
  150. {
  151. return $this->_admin;
  152. }
  153. /**
  154. *
  155. * set automatic flag
  156. *
  157. * @param boolean $automatic
  158. *
  159. * @return $this
  160. */
  161. public function setAutomatic($automatic)
  162. {
  163. $this->_automatic = $automatic;
  164. return $this;
  165. }
  166. /**
  167. *
  168. * get automatic flag
  169. *
  170. * @return boolean
  171. */
  172. public function getAutomatic()
  173. {
  174. return $this->_automatic;
  175. }
  176. /**
  177. *
  178. * add single message
  179. *
  180. * @param string $message
  181. *
  182. * @return $this
  183. */
  184. public function addMessage($message)
  185. {
  186. $translate = $this->getTranslate();
  187. if (null !== $translate) {
  188. $message = $translate->_($message);
  189. }
  190. $this->_messages[] = $message;
  191. return $this;
  192. }
  193. /**
  194. *
  195. * get messages
  196. *
  197. * @return array
  198. */
  199. public function getMessages()
  200. {
  201. return $this->_messages;
  202. }
  203. /**
  204. *
  205. * increment counter
  206. *
  207. * @param int $value
  208. *
  209. * @return $this
  210. */
  211. public function incrementCounter($value = 1)
  212. {
  213. $this->_counter += $value;
  214. return $this;
  215. }
  216. /**
  217. *
  218. * reset counter
  219. *
  220. * @param int|bool $value if false then we use the external counter
  221. *
  222. * @return $this
  223. */
  224. public function resetCounter($value = 0)
  225. {
  226. $this->_counter = $value;
  227. return $this;
  228. }
  229. /**
  230. *
  231. * get counter value
  232. *
  233. * @return int
  234. */
  235. public function getCounter()
  236. {
  237. return $this->_counter;
  238. }
  239. /**
  240. *
  241. * proxy to class methods
  242. *
  243. * @param string $methodName
  244. * @param bool $admin
  245. * @param bool $automatic
  246. *
  247. * @return array
  248. */
  249. public function changeStatus($methodName = null, $admin = false, $automatic = false)
  250. {
  251. if (method_exists($this, $methodName)) {
  252. $this->setAdmin($admin)
  253. ->setAutomatic($automatic);
  254. $this->$methodName();
  255. }
  256. return $this->getMessages();
  257. }
  258. /**
  259. *
  260. * open listings from the selected rowset
  261. * only scheduled items can be opened, ended items can only be relisted
  262. *
  263. * @return $this
  264. */
  265. public function open()
  266. {
  267. $this->resetCounter();
  268. /** @var \Ppb\Db\Table\Row\Listing $listing */
  269. foreach ($this as $listing) {
  270. if (strtotime($listing['start_time']) > time() && $listing['closed'] == 1) {
  271. $params['closed'] = 0;
  272. $params['start_time'] = new Expr('now()');
  273. $listing->save($params);
  274. $this->incrementCounter();
  275. }
  276. }
  277. return $this;
  278. }
  279. /**
  280. *
  281. * close listings from the selected rowset
  282. * open items with end time > current time can be closed
  283. * send emails when listings have been closed (single email per user)
  284. *
  285. * 7.8: a listing can be closed if one of the flags: admin, automatic or canClose are true
  286. *
  287. * @return $this
  288. */
  289. public function close()
  290. {
  291. $this->resetCounter();
  292. $emails = array();
  293. /** @var \Ppb\Db\Table\Row\Listing $listing */
  294. foreach ($this as $listing) {
  295. if ($listing->canClose() || $this->_admin || $this->_automatic) {
  296. $listing->close($this->_automatic);
  297. if ($listing->getClosedFlag() === true) {
  298. $emails[$listing['user_id']][] = $listing;
  299. }
  300. $this->incrementCounter();
  301. }
  302. else {
  303. $translate = $this->getTranslate();
  304. $message = sprintf($translate->_('Listing ID: #%s cannot be closed.'), $listing['id']);
  305. $this->addMessage($message);
  306. }
  307. }
  308. return $this;
  309. }
  310. /**
  311. *
  312. * relist listings from the selected rowset
  313. * closed ended items can be relisted
  314. * send emails when listings have been relisted (single email per user)
  315. *
  316. * @return $this
  317. */
  318. public function relist()
  319. {
  320. $usersService = $this->getUsers();
  321. $listingsService = $this->getListings();
  322. $settings = $this->getSettings();
  323. $this->resetCounter();
  324. $emails = array();
  325. /** @var \Ppb\Db\Table\Row\Listing $listing */
  326. foreach ($this as $listing) {
  327. $relist = false;
  328. /** @var \Ppb\Db\Table\Row\User $seller */
  329. $seller = $listing->findParentRow('\Ppb\Db\Table\Users');
  330. if ($seller->canList() || $this->_admin) {
  331. if (!$this->_automatic) {
  332. $relist = true;
  333. }
  334. else if ($settings['auto_relist'] && $listing['nb_relists'] > 0) {
  335. if (!$listing['auto_relist_sold']) {
  336. if (!$listing->countDependentRowset('\Ppb\Db\Table\SalesListings')) {
  337. $relist = true;
  338. }
  339. }
  340. else {
  341. $relist = true;
  342. }
  343. }
  344. }
  345. if ($relist) {
  346. $listingId = $listing->relist($this->_automatic);
  347. $newListing = $listingsService->findBy('id', $listingId, false, true);
  348. if (!$this->_admin) {
  349. $message = $newListing->processPostSetupActions();
  350. $this->addMessage($message);
  351. }
  352. else {
  353. $newListing->updateActive();
  354. $newListing->save(array(
  355. 'approved' => 1,
  356. ));
  357. }
  358. $emails[$listing['user_id']][] = $newListing;
  359. $this->incrementCounter();
  360. }
  361. }
  362. // send email notifications to listings owners
  363. $mail = new \Listings\Model\Mail\OwnerNotification();
  364. foreach ($emails as $userId => $listings) {
  365. $user = $usersService->findBy('id', $userId);
  366. $mail->setUser($user)
  367. ->setListings($listings)
  368. ->listingsRelisted()
  369. ->send();
  370. }
  371. return $this;
  372. }
  373. /**
  374. *
  375. * list selected drafts / bulk items - it will work like relisting same
  376. *
  377. * @return $this
  378. */
  379. public function draftsList()
  380. {
  381. $listingsService = $this->getListings();
  382. $this->resetCounter();
  383. /** @var \Ppb\Db\Table\Row\Listing $listing */
  384. foreach ($this as $listing) {
  385. $listingId = $listing->setRelistMethod('same')
  386. ->relist();
  387. $newListing = $listingsService->findBy('id', $listingId, false, true);
  388. if (!$this->_admin) {
  389. $message = $newListing->processPostSetupActions();
  390. $this->addMessage($message);
  391. }
  392. else {
  393. $newListing->updateActive();
  394. $newListing->save(array(
  395. 'approved' => 1,
  396. ));
  397. }
  398. $this->incrementCounter();
  399. }
  400. return $this;
  401. }
  402. /**
  403. *
  404. * activate listings from the selected rowset
  405. * only suspended & approved items can be activated
  406. *
  407. * @return $this
  408. */
  409. public function activate()
  410. {
  411. $this->resetCounter();
  412. /** @var \Ppb\Db\Table\Row\Listing $listing */
  413. foreach ($this as $listing) {
  414. if ($listing['active'] != 1 && $listing['approved'] == 1) {
  415. $listing->updateActive(1);
  416. $this->incrementCounter();
  417. }
  418. }
  419. return $this;
  420. }
  421. /**
  422. *
  423. * approve listings from the selected rowset
  424. * unapproved items can be approved
  425. * send emails when listings are approved by admin (single email per user)
  426. *
  427. * @return $this
  428. */
  429. public function approve()
  430. {
  431. $usersService = $this->getUsers();
  432. $this->resetCounter();
  433. $emails = array();
  434. /** @var \Ppb\Db\Table\Row\Listing $listing */
  435. foreach ($this as $listing) {
  436. if ($listing['approved'] == 0) {
  437. $listing->updateApproved(1);
  438. $emails[$listing['user_id']][] = $listing;
  439. $this->incrementCounter();
  440. }
  441. }
  442. // send email notifications to listings owners
  443. $mail = new \Listings\Model\Mail\OwnerNotification();
  444. foreach ($emails as $userId => $listings) {
  445. $user = $usersService->findBy('id', $userId);
  446. $mail->setUser($user)
  447. ->setListings($listings)
  448. ->listingsApproved()
  449. ->send();
  450. }
  451. return $this;
  452. }
  453. /**
  454. *
  455. * suspend listings from the selected rowset
  456. * active items can be suspended
  457. * send emails when listings are suspended by admin (single email per user)
  458. *
  459. * @return $this
  460. */
  461. public function suspend()
  462. {
  463. $usersService = $this->getUsers();
  464. $this->resetCounter();
  465. $emails = array();
  466. /** @var \Ppb\Db\Table\Row\Listing $listing */
  467. foreach ($this as $listing) {
  468. if ($listing['active'] == 1 && $listing['approved'] == 1) {
  469. $listing->updateActive(-1);
  470. if ($this->_admin) {
  471. $emails[$listing['user_id']][] = $listing;
  472. }
  473. $this->incrementCounter();
  474. }
  475. }
  476. // send email notifications to listings owners
  477. $mail = new \Listings\Model\Mail\OwnerNotification();
  478. foreach ($emails as $userId => $listings) {
  479. $user = $usersService->findBy('id', $userId);
  480. $mail->setUser($user)
  481. ->setListings($listings)
  482. ->listingsSuspended()
  483. ->send();
  484. }
  485. return $this;
  486. }
  487. /**
  488. *
  489. * remove marked deleted status from marked deleted items
  490. *
  491. * @return $this
  492. */
  493. public function undelete()
  494. {
  495. $this->save(array(
  496. 'deleted' => 0,
  497. ));
  498. $this->resetCounter(false);
  499. return $this;
  500. }
  501. /**
  502. *
  503. * delete all rows from the rowset individually
  504. * mark deleted (if user) or delete (if admin) any item
  505. *
  506. * 7.8: a listing can be closed if one of the flags: admin, automatic or canClose are true
  507. *
  508. * @return $this
  509. */
  510. public function delete()
  511. {
  512. $this->resetCounter();
  513. /** @var \Ppb\Db\Table\Row\Listing $listing */
  514. foreach ($this as $listing) {
  515. if ($listing->canDelete() || $this->_admin || $this->_automatic) {
  516. $listing->delete($this->_admin);
  517. $this->incrementCounter();
  518. }
  519. else {
  520. $translate = $this->getTranslate();
  521. $message = sprintf($translate->_('Listing ID: #%s cannot be deleted.'), $listing['id']);
  522. $this->addMessage($message);
  523. }
  524. }
  525. return $this;
  526. }
  527. }