* @copyright HiPresta 2020 * @license Addons PrestaShop license limitation * @link https://hipresta.com * * NOTICE OF LICENSE * * Don't use this module on several shops. The license provided by PrestaShop Addons * for all its modules is valid only once for a single shop. */ class GalleryItem extends ObjectModel { public $id_gallery; public $name; public $position; public $active; public $link_rewrite; public $title; public $description; public $meta_title; public $meta_description; public $meta_keywords; public $date_add; public $date_upd; public static $definition = array( 'table' => 'higallery', 'primary' => 'id_gallery', 'multilang' => true, 'fields' => array( 'name' => array( 'type' => self::TYPE_STRING, 'validate' => 'isCleanHtml', 'lang' => true, 'size' => 255, 'required' => true), 'position' => array( 'type' => self::TYPE_STRING, 'validate' => 'isCleanHtml', 'size' => 100, 'required' => true), 'active' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'), 'link_rewrite' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 255), 'title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255), 'description' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'), 'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255), 'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255), 'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255), 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false), 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false), ), ); public function add($autodate = true, $null_values = false) { $context = Context::getContext(); $res = parent::add($autodate, $null_values); $res &= Db::getInstance()->execute(' INSERT INTO `'._DB_PREFIX_.'higallery_shop` (`id_gallery`, `id_shop`) VALUES('.(int)$this->id.', '.(int)$context->shop->id.')'); return $res; } public function delete() { $this->deleteImages(); $context = Context::getContext(); $res = parent::delete(); $res &= Db::getInstance()->execute(' DELETE FROM `'._DB_PREFIX_.'higallery_shop` WHERE id_gallery = '.(int)$this->id.' AND id_shop = '.(int)$context->shop->id); return $res; } public static function getGalleriesList() { $id_lang = Context::getContext()->language->id; $id_shop = Context::getContext()->shop->id; $query = new DbQuery(); return Db::getInstance()->executeS( $query ->select('g.*') ->select('gl.*') ->from('higallery', 'g') ->leftJoin('higallery_lang', 'gl', 'gl.`id_gallery` = g.`id_gallery`') ->leftJoin('higallery_shop', 'gs', 'gs.`id_gallery` = g.`id_gallery`') ->where('gl.`id_lang` = '.(int)$id_lang) ->where('gs.`id_shop` = '.(int)$id_shop) ->orderBy('g.sort ASC') ->build() ); } public static function getAllGalleries($active = '', $position = '') { $context = Context::getContext(); $id_lang = Context::getContext()->language->id; $id_shop = Context::getContext()->shop->id; $active_in = $active != '' ? '("1")' : '("0","1")'; $query = new DbQuery(); $query ->select('g.*') ->select('gl.*') ->from('higallery', 'g') ->leftJoin('higallery_lang', 'gl', 'gl.`id_gallery` = g.`id_gallery`') ->leftJoin('higallery_shop', 'gs', 'gs.`id_gallery` = g.`id_gallery`') ->where('gl.`id_lang` = '.(int)$id_lang) ->where('gs.`id_shop` = '.(int)$id_shop) ->where('g.active IN '.$active_in); if ($position && $position != '') { $query->where('g.`position` = \''.pSQL($position).'\''); } $query->orderBy('g.sort ASC'); $result = Db::getInstance()->executeS( $query->build() ); $i = 0; if (!empty($result)) { foreach ($result as $key => $res) { $result[$key]['url'] = $context->link->getPageLink('galleryimages', null, null, array('link_rewrite' => $res['link_rewrite'])); $result[$key]['images'] = GalleryImage::getGalleryAllImages($res['id_gallery']); $gallery_images = GalleryImage::getGalleryAllImages($res['id_gallery']); if (!empty($gallery_images)) { $i++; } } $result['visible'] = $i > 0 ? true : false; } return $result; } public static function getGalleriesContentByPosition($position) { $id_lang = Context::getContext()->language->id; $id_shop = Context::getContext()->shop->id; $query = new DbQuery(); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( $query ->select('g.*') ->select('gl.*') ->from('higallery', 'g') ->leftJoin('higallery_lang', 'gl', 'gl.`id_gallery` = g.`id_gallery`') ->leftJoin('higallery_shop', 'gs', 'gs.`id_gallery` = g.`id_gallery`') ->where('gl.`id_lang` = '.(int)$id_lang) ->where('gs.`id_shop` = '.(int)$id_shop) ->where('g.`position` = \''.pSQL($position).'\'') ->where('g.active = 1') ->build() ); $i = 0; if (!empty($result)) { foreach ($result as $key => $res) { $result[$key]['images'] = GalleryImage::getGalleryAllImages($res['id_gallery']); $gallery_images = GalleryImage::getGalleryAllImages($res['id_gallery']); if (!empty($gallery_images)) { $i++; } } $result['visible'] = $i > 0 ? true : false; } return $result; } public static function getGalleriesContentById($id_gallery) { $id_lang = Context::getContext()->language->id; $id_shop = Context::getContext()->shop->id; $query = new DbQuery(); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( $query ->select('g.*') ->select('gl.*') ->from('higallery', 'g') ->leftJoin('higallery_lang', 'gl', 'gl.`id_gallery` = g.`id_gallery`') ->leftJoin('higallery_shop', 'gs', 'gs.`id_gallery` = g.`id_gallery`') ->where('gl.`id_lang` = '.(int)$id_lang) ->where('gs.`id_shop` = '.(int)$id_shop) ->where('g.`id_gallery` = '.(int)$id_gallery) ->where('g.active = 1') ->build() ); $i = 0; if (!empty($result)) { foreach ($result as $key => $res) { $result[$key]['images'] = GalleryImage::getGalleryAllImages($res['id_gallery']); $gallery_images = GalleryImage::getGalleryAllImages($res['id_gallery']); if (!empty($gallery_images)) { $i++; } } $result['visible'] = $i > 0 ? true : false; } return $result; } public static function getGalleryByID($id_gallery) { $id_lang = Context::getContext()->language->id; $id_shop = Context::getContext()->shop->id; $query = new DbQuery(); $gallery = Db::getInstance()->getRow( $query ->select('g.*') ->select('gl.*') ->from('higallery', 'g') ->leftJoin('higallery_lang', 'gl', 'gl.`id_gallery` = g.`id_gallery`') ->leftJoin('higallery_shop', 'gs', 'gs.`id_gallery` = g.`id_gallery`') ->where('gl.`id_lang` = '.(int)$id_lang) ->where('gs.`id_shop` = '.(int)$id_shop) ->where('g.`id_gallery` = '.(int)$id_gallery) ->where('g.active = 1') ->build() ); if ($gallery) { $gallery['images'] = GalleryImage::getGalleryAllImages($gallery['id_gallery']); } return $gallery; } public static function getGalleriesContentByFriendlyUrl($link_rewrite) { $id_lang = Context::getContext()->language->id; $id_shop = Context::getContext()->shop->id; $query = new DbQuery(); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow( $query ->select('g.*') ->select('gl.*') ->from('higallery', 'g') ->leftJoin('higallery_lang', 'gl', 'gl.`id_gallery` = g.`id_gallery`') ->leftJoin('higallery_shop', 'gs', 'gs.`id_gallery` = g.`id_gallery`') ->where('gl.`id_lang` = '.(int)$id_lang) ->where('gs.`id_shop` = '.(int)$id_shop) ->where('gl.`link_rewrite` = \''.pSQL($link_rewrite).'\'') ->where('g.active = 1') ->build() ); if (!empty($result)) { $result['images'] = GalleryImage::getGalleryAllImages($result['id_gallery']); } return $result; } public static function galleryExists($id_gallery) { $query = new DbQuery(); return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow( $query ->select('g.id_gallery') ->from('higallery', 'g') ->where('g.id_gallery = '.(int)$id_gallery) ->build() ); } public function deleteImages() { $images = GalleryImage::getGalleryAllImages((int)$this->id); if (!empty($images)) { foreach ($images as $image) { $gallery_image = new GalleryImage($image['id_image']); if (!empty($gallery_image)) { if ($gallery_image->image) { if (file_exists(_PS_MODULE_DIR_.'higallery/views/img/upload/original/'.$gallery_image->image)) { unlink(_PS_MODULE_DIR_.'higallery/views/img/upload/original/'.$gallery_image->image); } if (file_exists(_PS_MODULE_DIR_.'higallery/views/img/upload/small/'.$gallery_image->image)) { unlink(_PS_MODULE_DIR_.'higallery/views/img/upload/small/'.$gallery_image->image); } if (file_exists(_PS_MODULE_DIR_.'higallery/views/img/upload/thumbnail/'.$gallery_image->image)) { unlink(_PS_MODULE_DIR_.'higallery/views/img/upload/thumbnail/'.$gallery_image->image); } } } $gallery_image->delete(); } } } }