* @copyright 2023 Anvanto * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) */ if (!defined('_PS_VERSION_')) { exit; } require_once _PS_MODULE_DIR_ . 'an_homeproducts/classes/anHomeProductFiles.php'; class anHomeProductsBanners extends ObjectModel { /** * @var int */ public $id_banner; /** * @var int */ public $id; public $special_id_banner; public $block; public $block_position = 0; public $col = 12; public $template; // public $show_on; public $active = 1; public $position; public $title; public $text; public $link; public $image; /** * @var array */ public static $definition = [ 'table' => 'an_homeproducts_banners', 'primary' => 'id_banner', 'multilang' => true, 'fields' => [ 'special_id_banner' => ['type' =>self::TYPE_STRING ], 'block' => ['type' =>self::TYPE_STRING ], 'block_position' => ['type' =>self::TYPE_INT ], 'col' => ['type' =>self::TYPE_INT ], 'template' => ['type' =>self::TYPE_STRING ], // 'show_on' => ['type' =>self::TYPE_INT ], 'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'], 'position' => ['type' =>self::TYPE_INT ], 'title' => ['type' =>self::TYPE_STRING,'lang' => true ], 'text' => ['type' =>self::TYPE_HTML,'lang' => true ], 'link' => ['type' =>self::TYPE_STRING,'lang' => true ], 'image' => ['type' =>self::TYPE_STRING,'lang' => true ], ], ]; public const imgDir = _PS_MODULE_DIR_.'an_homeproducts/img/'; public const imgUrl = __PS_BASE_URI__.'modules/an_homeproducts/img/'; public const tplsDir = _PS_MODULE_DIR_ . 'an_homeproducts/views/templates/front/banners/'; public const tplsThemeDir = _PS_THEME_DIR_ . 'modules/an_homeproducts/banners/'; public const tplPath = './banners/'; /** * Formula constructor. * * @param null $id */ public function __construct($id = null, $id_lang = null) { parent::__construct($id, $id_lang); $this->imgUrlFile = ''; $this->width = ''; $this->height = ''; if (!is_array($this->image) && $this->image){ $this->imgUrlFile = self::imgUrl . $this->image; $imgInfo = getimagesize(self::imgDir . $this->image); $this->width = $imgInfo['0']; $this->height = $imgInfo['1']; } if ($this->template == ''){ $this->template = 'default'; } $this->tplFilePath = self::getTemplatePath($this->template); if (!$this->tplFilePath){ $this->tplFilePath = self::getTemplatePath('default'); } $this->show_on_class = ''; if (isset($this->show_on) && $this->show_on == 1){ $this->show_on_class = 'an_homeproducts-hide-mobile'; } else if (isset($this->show_on) && $this->show_on == 2){ $this->show_on_class = 'an_homeproducts-hide-desktop'; } } public static function getTemplatePath($template) { if (Tools::file_exists_no_cache(self::tplsThemeDir . $template . '.tpl')){ return self::tplsThemeDir . $template . '.tpl'; } else if (Tools::file_exists_no_cache(self::tplsDir . $template . '.tpl')){ return self::tplPath . $template . '.tpl'; } return false; } public function add($auto_date = true, $null_values = false) { if (empty($this->special_id_banner)) { $this->special_id_banner = uniqid(); } return parent::add($auto_date, $null_values); } public static function getListTpl() { $list = []; $tplsTheme = glob(self::tplsThemeDir . '*.tpl'); foreach ($tplsTheme as $tpl){ $list[] = str_replace('.tpl', '', basename($tpl)); } $tplsMod = glob(self::tplsDir . '*.tpl'); foreach ($tplsMod as $tpl){ $list[] = str_replace('.tpl', '', basename($tpl)); } $list = array_unique($list); $return = []; foreach ($list as $item){ $return[]['file'] = $item; } return $return; } public static function getBanners($forExport = false, $all = false) { $context = Context::getContext(); $sql = ' SELECT * FROM `' . _DB_PREFIX_ . 'an_homeproducts_banners` sw LEFT JOIN `' . _DB_PREFIX_ . 'an_homeproducts_banners_lang` sl ON (sw.`id_banner` = sl.`id_banner` AND sl.`id_lang` = ' . (int) $context->language->id . ') '; if (!$all){ $sql .= 'WHERE sw.`active`=1 '; } if (Shop::isFeatureActive()) { $sql .= ' AND sw.`id_banner` IN ( SELECT sa.`id_banner` FROM `' . _DB_PREFIX_ . 'an_homeproducts_banners_shop` sa WHERE sa.id_shop IN (' . implode(', ', Shop::getContextListShopID()) . ') )'; } $sql .= ' ORDER BY sw.`position`'; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); if ($forExport){ return $result; } $banners = []; foreach ($result as $id => $banner){ $obj = new anHomeProductsBanners($banner['id_banner'], $context->language->id); $banners[$banner['block']][$banner['block_position']][$banner['id_banner']] = (array) $obj; } return $banners; } public function delete() { foreach ($this->image as $imgName){ @unlink(self::imgDir . $imgName); } return parent::delete(); } public static function findTemplatesBanners() { } public static function exportJsonBanners($filesManager) { $banners = self::getBanners(true, true); foreach ($banners as $key => $banner){ $obj = new anHomeProductsBanners($banner['id_banner']); $banners[$key]['imagesLang'] = $obj->image; $banners[$key]['link'] = '#'; } @file_put_contents($filesManager->getJsonDirBanners(), json_encode($banners, JSON_PRETTY_PRINT)); } public static function importJsonBanners($filesManager) { $data = json_decode($filesManager->getJsonBannersPathFile(), true); $context = Context::getContext(); $languages = Language::getLanguages(); if ($data){ foreach ($data as $item){ $bannerObj = new anHomeProductsBanners(); if (!isset($item['special_id_banner'])){ $item['special_id_banner'] = uniqid(); } $bannerObj->special_id_banner = $item['special_id_banner']; $bannerObj->block = $item['block']; $bannerObj->block_position = $item['block_position']; $bannerObj->col = $item['col']; $bannerObj->template = $item['template']; $bannerObj->active = $item['active']; $bannerObj->position = $item['position']; $bannerObj->image = array(); foreach ($languages as $language) { $bannerObj->title[$language['id_lang']] = $item['title']; $bannerObj->text[$language['id_lang']] = $item['text']; $bannerObj->link[$language['id_lang']] = $item['link']; if (!isset($bannerObj->image[$language['id_lang']]) && isset($item['imagesLang'][$language['id_lang']]) && $item['imagesLang'][$language['id_lang']] != $item['image']){ $nameImage = $item['imagesLang'][$language['id_lang']]; if (Shop::isFeatureActive() && Tools::file_exists_no_cache(self::imgDir . $nameImage)){ $nameImage = self::generateName($nameImage, $language['id_lang']); Tools::copy(self::imgDir . $item['image'], self::imgDir . $nameImage); } $bannerObj->image[$language['id_lang']] = $nameImage; } else { $nameImage = $item['image']; if (!Tools::file_exists_no_cache(self::imgDir . $nameImage)){ $nameImage = ''; } if ($nameImage !='' && (in_array($nameImage, $bannerObj->image) || Shop::isFeatureActive())){ $nameImage = self::generateName($nameImage, $language['id_lang']); Tools::copy(self::imgDir . $item['image'], self::imgDir . $nameImage); } $bannerObj->image[$language['id_lang']] = $nameImage; } } $bannerObj->save(); Db::getInstance()->insert('an_homeproducts_banners_shop', [ 'id_banner' => (int) $bannerObj->id, 'id_shop' => (int) Context::getContext()->shop->id ]); } } } public static function generateName($name, $id_lang) { $ext = substr($name, strrpos($name, '.') + 1); $nameImage = $id_lang . '_' . md5(uniqid()) .'.'.$ext; return 'new_'.$nameImage; } public static function getCountBanners() { $context = Context::getContext(); $sql = ' SELECT count(*) FROM `' . _DB_PREFIX_ . 'an_homeproducts_banners` sw LEFT JOIN `' . _DB_PREFIX_ . 'an_homeproducts_banners_lang` sl ON (sw.`id_banner` = sl.`id_banner` AND sl.`id_lang` = ' . (int) $context->language->id . ') '; if (Shop::isFeatureActive()) { $sql .= ' WHERE sw.`id_banner` IN ( SELECT sa.`id_banner` FROM `' . _DB_PREFIX_ . 'an_homeproducts_banners_shop` sa WHERE sa.id_shop IN (' . implode(', ', Shop::getContextListShopID()) . ') )'; } return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); } }