'ads', 'primary' => 'id_block_ads', 'multilang' => true, 'multilang_shop' => true, 'fields' => array( 'hook_name' => array('type' => self::TYPE_STRING, 'validate' => 'isHookName', 'copy_post' => false), 'label' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), 'width' => array('type' => self::TYPE_INT, 'validate' => 'isInt'), 'height' => array('type' => self::TYPE_INT, 'validate' => 'isInt'), 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), 'content' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isAnything'), ), ); public function getPublicationsIds($page_name, DateTime $date, $id = null) { $sql = 'SELECT ap.id_publication FROM '._DB_PREFIX_.'ads_publication ap INNER JOIN '._DB_PREFIX_.'ads_publication_page app ON ap.id_publication = app.id_publication AND app.page_name = "'.pSQL($page_name).'" WHERE ap.id_block_ads = '.(int)$this->id.' AND ( date_from = "0000-00-00 00:00:00" OR date_from <= "'.$date->format('Y-m-d H:i:s').'") AND ( date_to = "0000-00-00 00:00:00" OR date_to >= "'.$date->format('Y-m-d H:i:s').'") AND active = 1 GROUP BY ap.id_publication'; $publications = Db::getInstance()->executeS($sql); if(empty($publications) || count($publications) == 0) { return array(); } $publications = array_map(function($line){ return $line['id_publication']; }, $publications); if(empty($id)) return $publications; $sql = 'SELECT id_publication, id_free FROM '._DB_PREFIX_.'ads_publication_other WHERE page_name = "'.pSQL($page_name).'" AND id_publication IN ('.implode(',', $publications).')'; $ids_free = Db::getInstance()->executeS($sql); $publications = array(); foreach($ids_free as $line) if($line['id_free'] == $id) { $publications[$line['id_publication']] = $line['id_publication']; } return $publications; } public function getPublications($page_name, DateTime $date, $id = null, $id_lang = null) { if(empty($id_lang)) $id_lang = Context::getContext()->language->id; $ids_publications = $this->getPublicationsIds($page_name, $date, $id); if(count($ids_publications) == 0) return array(); $collection = new Collection('Publication', $id_lang); $collection->where('id_publication', 'in', $ids_publications); return $collection->getResults(); } public function getContent($page_name, DateTime $date, $id = null, $id_lang = null) { if(empty($id_lang)) $id_lang = Context::getContext()->language->id; $publications = $this->getPublications($page_name, $date, $id, $id_lang = null); if(count($publications) > 0) return $publications[0]->content; return $this->content; } public static function getContentByHook($hook_name, $page_name, DateTime $date = null, $id = null, $id_lang = null) { if(empty($id_lang)) $id_lang = Context::getContext()->language->id; $collection = new Collection('BlockAds', $id_lang); $collection->where('hook_name', '=', $hook_name); $ads = $collection->getFirst(); if(empty($ads)) return ''; if(empty($date)) $date = new DateTime(); return $ads->getContent($page_name, $date, $id, $id_lang); } }