Advert.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 4uPn9uhRsRNaH8ZOFoNy2EFgYgo9SQn/iqUN/jfLF7s=
  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. * advert display view helper class
  14. */
  15. namespace Ppb\View\Helper;
  16. use Cube\View\Helper\AbstractHelper,
  17. Cube\Controller\Front,
  18. Ppb\Service\Advertising as AdvertisingService,
  19. Ppb\Service\Table\Relational\Categories as CategoriesService,
  20. Ppb\Db\Table\Row\Advert as AdvertModel,
  21. Cube\Db\Select,
  22. Cube\Db\Expr;
  23. class Advert extends AbstractHelper
  24. {
  25. /**
  26. *
  27. * advertising service
  28. *
  29. * @var \Ppb\Service\Advertising
  30. */
  31. protected $_advertising;
  32. /**
  33. *
  34. * advert model
  35. *
  36. * @var \Ppb\Db\Table\Row\Advert
  37. */
  38. protected $_advert;
  39. /**
  40. *
  41. * get advert model
  42. *
  43. * @return \Ppb\Db\Table\Row\Advert
  44. * @throws \InvalidArgumentException
  45. */
  46. public function getAdvert()
  47. {
  48. if (!$this->_advert instanceof AdvertModel) {
  49. throw new \InvalidArgumentException("The advert model has not been instantiated");
  50. }
  51. return $this->_advert;
  52. }
  53. /**
  54. *
  55. * set advert model
  56. *
  57. * @param \Ppb\Db\Table\Row\Advert $advert
  58. *
  59. * @throws \InvalidArgumentException
  60. * @return $this
  61. */
  62. public function setAdvert(AdvertModel $advert)
  63. {
  64. if (!$advert instanceof AdvertModel) {
  65. throw new \InvalidArgumentException("The advert model must be an instance of \Ppb\Db\Table\Row\Advert");
  66. }
  67. $this->_advert = $advert;
  68. return $this;
  69. }
  70. /**
  71. *
  72. * get content sections table service
  73. *
  74. * @return \Ppb\Service\Advertising
  75. */
  76. public function getAdvertising()
  77. {
  78. if (!$this->_advertising instanceof AdvertisingService) {
  79. $this->setAdvertising(
  80. new AdvertisingService());
  81. }
  82. return $this->_advertising;
  83. }
  84. /**
  85. *
  86. * set advertising service
  87. *
  88. * @param \Ppb\Service\Advertising $advertising
  89. *
  90. * @return $this
  91. */
  92. public function setAdvertising(AdvertisingService $advertising)
  93. {
  94. $this->_advertising = $advertising;
  95. return $this;
  96. }
  97. /**
  98. *
  99. * display the currently selected advert
  100. *
  101. * return string|null
  102. */
  103. public function display()
  104. {
  105. $advert = $this->getAdvert();
  106. $advert->addView();
  107. switch ($advert['type']) {
  108. case 'image':
  109. $view = $this->getView();
  110. $imageSrc = $view->baseUrl . \Ppb\Utility::URI_DELIMITER . \Ppb\Utility::getFolder('uploads') . \Ppb\Utility::URI_DELIMITER . $advert['content'];
  111. return '<a href="' . $view->url($advert->link()) . '"
  112. target="' . (($advert->getData('new_tab')) ? '_blank' : '_self') . '"
  113. class="img-advert">
  114. <img src="' . $imageSrc . '" alt="Banner" class="img-advert">
  115. </a>';
  116. break;
  117. case 'code':
  118. return $advert['content'];
  119. break;
  120. }
  121. return null;
  122. }
  123. /**
  124. *
  125. * main method, only returns object instance
  126. *
  127. * @param \Ppb\Db\Table\Row\Advert $advert
  128. *
  129. * @return $this
  130. */
  131. public function advert(AdvertModel $advert = null)
  132. {
  133. if ($advert !== null) {
  134. $this->setAdvert($advert);
  135. }
  136. return $this;
  137. }
  138. /**
  139. *
  140. *
  141. * return one or an array of advert objects
  142. *
  143. * @param string $section
  144. * @param bool $all if true, return all adverts from the requested section
  145. * @param array $categoryIds
  146. *
  147. * @return \Ppb\Db\Table\Row\Advert|\Ppb\Db\Table\Rowset\Adverts|null
  148. */
  149. public function findBySection($section, $all = false, $categoryIds = array())
  150. {
  151. $select = $this->getAdvertising()->getTable()
  152. ->select(array('nb_rows' => new Expr('count(*)')))
  153. ->where('section = ?', $section)
  154. ->where('active = ?', 1);
  155. $categoriesFilter = array(0);
  156. $categoryIds = array_filter($categoryIds);
  157. if (count($categoryIds) > 0) {
  158. $categoriesService = new CategoriesService();
  159. foreach ($categoryIds as $categoryId) {
  160. $categoriesFilter = array_merge($categoriesFilter, array_keys(
  161. $categoriesService->getBreadcrumbs($categoryId)));
  162. }
  163. }
  164. $select->where("category_ids REGEXP '\"" . implode('"|"',
  165. array_unique($categoriesFilter)) . "\"' OR category_ids = ''");
  166. $locale = Front::getInstance()->getBootstrap()->getResource('locale')->getLocale();
  167. $select->where("language = '" . $locale . "' OR language IS NULL");
  168. $stmt = $select->query();
  169. $nbAdverts = (integer)$stmt->fetchColumn('nb_rows');
  170. if (!$nbAdverts) {
  171. return null;
  172. }
  173. $select->reset(Select::COLUMNS)
  174. ->columns('*');
  175. if ($all !== true) {
  176. $select = $select->order(new Expr('rand()'))
  177. ->limit(1);
  178. return $this->getAdvertising()->fetchAll($select)->getRow(0);
  179. }
  180. return $this->getAdvertising()->fetchAll($select);
  181. }
  182. }