123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- /**
- *
- * PHP Pro Bid $Id$ 4uPn9uhRsRNaH8ZOFoNy2EFgYgo9SQn/iqUN/jfLF7s=
- *
- * @link http://www.phpprobid.com
- * @copyright Copyright (c) 2016 Online Ventures Software & CodeCube SRL
- * @license http://www.phpprobid.com/license Commercial License
- *
- * @version 7.8
- */
- /**
- * advert display view helper class
- */
- namespace Ppb\View\Helper;
- use Cube\View\Helper\AbstractHelper,
- Cube\Controller\Front,
- Ppb\Service\Advertising as AdvertisingService,
- Ppb\Service\Table\Relational\Categories as CategoriesService,
- Ppb\Db\Table\Row\Advert as AdvertModel,
- Cube\Db\Select,
- Cube\Db\Expr;
- class Advert extends AbstractHelper
- {
- /**
- *
- * advertising service
- *
- * @var \Ppb\Service\Advertising
- */
- protected $_advertising;
- /**
- *
- * advert model
- *
- * @var \Ppb\Db\Table\Row\Advert
- */
- protected $_advert;
- /**
- *
- * get advert model
- *
- * @return \Ppb\Db\Table\Row\Advert
- * @throws \InvalidArgumentException
- */
- public function getAdvert()
- {
- if (!$this->_advert instanceof AdvertModel) {
- throw new \InvalidArgumentException("The advert model has not been instantiated");
- }
- return $this->_advert;
- }
- /**
- *
- * set advert model
- *
- * @param \Ppb\Db\Table\Row\Advert $advert
- *
- * @throws \InvalidArgumentException
- * @return $this
- */
- public function setAdvert(AdvertModel $advert)
- {
- if (!$advert instanceof AdvertModel) {
- throw new \InvalidArgumentException("The advert model must be an instance of \Ppb\Db\Table\Row\Advert");
- }
- $this->_advert = $advert;
- return $this;
- }
- /**
- *
- * get content sections table service
- *
- * @return \Ppb\Service\Advertising
- */
- public function getAdvertising()
- {
- if (!$this->_advertising instanceof AdvertisingService) {
- $this->setAdvertising(
- new AdvertisingService());
- }
- return $this->_advertising;
- }
- /**
- *
- * set advertising service
- *
- * @param \Ppb\Service\Advertising $advertising
- *
- * @return $this
- */
- public function setAdvertising(AdvertisingService $advertising)
- {
- $this->_advertising = $advertising;
- return $this;
- }
- /**
- *
- * display the currently selected advert
- *
- * return string|null
- */
- public function display()
- {
- $advert = $this->getAdvert();
- $advert->addView();
- switch ($advert['type']) {
- case 'image':
- $view = $this->getView();
- $imageSrc = $view->baseUrl . \Ppb\Utility::URI_DELIMITER . \Ppb\Utility::getFolder('uploads') . \Ppb\Utility::URI_DELIMITER . $advert['content'];
- return '<a href="' . $view->url($advert->link()) . '"
- target="' . (($advert->getData('new_tab')) ? '_blank' : '_self') . '"
- class="img-advert">
- <img src="' . $imageSrc . '" alt="Banner" class="img-advert">
- </a>';
- break;
- case 'code':
- return $advert['content'];
- break;
- }
- return null;
- }
- /**
- *
- * main method, only returns object instance
- *
- * @param \Ppb\Db\Table\Row\Advert $advert
- *
- * @return $this
- */
- public function advert(AdvertModel $advert = null)
- {
- if ($advert !== null) {
- $this->setAdvert($advert);
- }
- return $this;
- }
- /**
- *
- *
- * return one or an array of advert objects
- *
- * @param string $section
- * @param bool $all if true, return all adverts from the requested section
- * @param array $categoryIds
- *
- * @return \Ppb\Db\Table\Row\Advert|\Ppb\Db\Table\Rowset\Adverts|null
- */
- public function findBySection($section, $all = false, $categoryIds = array())
- {
- $select = $this->getAdvertising()->getTable()
- ->select(array('nb_rows' => new Expr('count(*)')))
- ->where('section = ?', $section)
- ->where('active = ?', 1);
- $categoriesFilter = array(0);
- $categoryIds = array_filter($categoryIds);
- if (count($categoryIds) > 0) {
- $categoriesService = new CategoriesService();
- foreach ($categoryIds as $categoryId) {
- $categoriesFilter = array_merge($categoriesFilter, array_keys(
- $categoriesService->getBreadcrumbs($categoryId)));
- }
- }
- $select->where("category_ids REGEXP '\"" . implode('"|"',
- array_unique($categoriesFilter)) . "\"' OR category_ids = ''");
- $locale = Front::getInstance()->getBootstrap()->getResource('locale')->getLocale();
- $select->where("language = '" . $locale . "' OR language IS NULL");
- $stmt = $select->query();
- $nbAdverts = (integer)$stmt->fetchColumn('nb_rows');
- if (!$nbAdverts) {
- return null;
- }
- $select->reset(Select::COLUMNS)
- ->columns('*');
- if ($all !== true) {
- $select = $select->order(new Expr('rand()'))
- ->limit(1);
- return $this->getAdvertising()->fetchAll($select)->getRow(0);
- }
- return $this->getAdvertising()->fetchAll($select);
- }
- }
|