* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ if (!defined('_PS_VERSION_')) { exit; } class StEasyVideoClass extends ObjectModel { public $id; public $id_st_easy_video; public $position; public $active; public $url; public $thumbnail; public $online_thumbnail; public $name; public $loop; public $muted; public $autoplay; public $sub_category; public $id_product; public $id_category; public $id_manufacturer; public $ratio; public $location; /** * @see ObjectModel::$definition */ public static $definition = array( 'table' => 'st_easy_video', 'primary' => 'id_st_easy_video', 'multilang' => false, 'fields' => array( 'position' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'), 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'), 'url' => array('type' => self::TYPE_STRING, 'validate' => 'isAnything', 'size' => 255, 'required'=>true), 'thumbnail' => array('type' => self::TYPE_STRING, 'validate' => 'isAnything', 'size' => 255), 'online_thumbnail' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'), 'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255), 'loop' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'), 'muted' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'), 'autoplay' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'), 'sub_category' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'), 'id_product' => array('type' => self::TYPE_STRING, 'validate' => 'isAnything'), 'id_category' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'), 'id_manufacturer' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'), 'location' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'), 'ratio' => array('type' => self::TYPE_STRING, 'validate' => 'isAnything', 'size' => 255), ), ); public function __construct($id = null, $id_lang = null, $id_shop = null) { Shop::addTableAssociation(self::$definition['table'], array('type' => 'shop')); parent::__construct($id, $id_lang, $id_shop); } public function delete() { $image = _PS_IMG_DIR_ . 'steasyvideo/' . $this->id . '.jpg'; if (file_exists($image)) @unlink($image); return parent::delete(); } public static function getOneVideo($id_product, $location=0) { Shop::addTableAssociation('st_easy_video', array('type' => 'shop')); $sql = 'SELECT * FROM `'._DB_PREFIX_.'st_easy_video` spv '.Shop::addSqlAssociation('st_easy_video', 'spv').' WHERE spv.`active`=1 AND ('.self::getProductWhereConds($id_product).') '.($location ? ' AND spv.`location` IN (0, 2, 3) ' : ' AND spv.`location` IN (0, 3) ').' ORDER BY spv.`position` desc'; return Db::getInstance()->getRow($sql); } public static function getVideos($id_product, $locations=[], $lang=0, $limit=0) { Shop::addTableAssociation('st_easy_video', array('type' => 'shop')); $sql = 'SELECT * FROM `'._DB_PREFIX_.'st_easy_video` spv INNER JOIN `'._DB_PREFIX_.'st_easy_video_yuyan` yy ON yy.id_st_easy_video = spv.id_st_easy_video '.Shop::addSqlAssociation('st_easy_video', 'spv').' WHERE spv.`active`=1 AND ('.self::getProductWhereConds($id_product).') '.(count($locations) ? ' AND spv.`location` IN ('.implode(',', $locations).') ' : '').($lang ? ' AND ( yy.`id_lang` = 0 OR yy.`id_lang` = '.(int)$lang.')' : '').' ORDER BY spv.`position` desc'.($limit ? ' LIMIT '.(int)$limit : ''); return Db::getInstance()->executeS($sql); } public static function get_lang_obj_data($id) { $data=Db::getInstance()->executeS((new DbQuery())->from(self::$definition['table'].'_yuyan')->where('id_st_easy_video='.(int)$id)); $new_data=[]; if($data && !empty($data)){ $new_data=array_column($data,'id_lang'); } return $new_data; } public static function getProductWhereConds($id) { $where = '0'; $product = new Product($id); if ($product->id_manufacturer) { $where .= ' OR (spv.`id_manufacturer` > 0 AND spv.`id_manufacturer`='.(int)$product->id_manufacturer.')'; } $id_category = $product->id_category_default; if ($cates = $product->getCategories()) { $categories = array(); $parents = array(); foreach ($cates as $id_cate) { $category = new Category($id_cate); if (!$category->active || !$category->id) { continue; } if ($category->id) { // For all product categories instead of deafult category. $categories[] = (int)$category->id; $id_array = array(); $category_parents = $category->getParentsCategories(); if (is_array($category_parents) && count($category_parents)) { foreach ($category_parents as $v) { $id_array[] = $v['id_category']; } } if (in_array($id_category, $id_array)) { $parents = array_merge($parents, $id_array); } } } $where .= ' OR (spv.`id_category` > 0 AND spv.`id_category` IN('.implode(',', array_unique(array_merge($categories, $parents))).') AND spv.`sub_category`=1) OR (spv.`id_category` > 0 AND spv.`id_category` IN('.implode(',', $categories).') AND spv.`sub_category`=0)'; } if ($product->id) { $where .= ' OR ( find_in_set(\''.(int)$product->id.'\', spv.`id_product`))'; } return $where; } }