* @copyright 2007-2014 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class ProductExtraTabModel extends ObjectModel { public $id_product_extratab; public $id_product; public $id_extratab; public $position; public $content; public $title; /** * @see ObjectModel::$definition */ public static $definition = array( 'table' => 'product_extratab', 'primary' => 'id_product_extratab', 'multilang' => true, 'fields' => array( 'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), 'id_extratab' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), 'position' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), 'title' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => false, 'lang' => true), 'content' => array('type' => self::TYPE_HTML, 'validate' => 'isAnything', 'required' => false, 'lang' => true), ) ); public static function createTables() { return ( self::createExtraTabTable() && self::createExtraTabLangTable() ); } public static function dropTables() { $sql = 'DROP TABLE `'._DB_PREFIX_.'product_extratab_lang`, `'._DB_PREFIX_.'product_extratab`'; return Db::getInstance()->execute($sql); } public static function createExtraTabTable() { $sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'product_extratab`( `id_product_extratab` int(10) unsigned NOT NULL auto_increment, `id_product` int(10) unsigned NOT NULL, `id_extratab` int(10) unsigned NOT NULL, `position` int(10) unsigned NOT NULL, PRIMARY KEY (`id_product_extratab`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'; return Db::getInstance()->execute($sql); } public static function createExtraTabLangTable() { $sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'product_extratab_lang`( `id_product_extratab` int(10) unsigned NOT NULL, `id_lang` int(10) unsigned NOT NULL, `title` varchar(256) NULL, `content` TEXT NULL, PRIMARY KEY (`id_product_extratab`, `id_lang`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'; return Db::getInstance()->execute($sql); } public function getTabsList($id_product, $id_lang = null){ if ( !$id_lang ) $id_lang = (int)$this->context->language->id; return self::getTabsListStatic($id_product, $id_lang); } public static function getTabsListStatic($id_product, $id_lang){ $extraTabs = Db::getInstance()->ExecuteS(' SELECT et.*, etl.*, tl.name as extratab, pl.name as product FROM `'._DB_PREFIX_.'product_extratab` et LEFT JOIN `'._DB_PREFIX_.'extratab_lang` tl ON (tl.`id_extratab` = et.`id_extratab` AND tl.`id_lang` = '.(int)$id_lang.') LEFT JOIN `'._DB_PREFIX_.'product_extratab_lang` etl ON (et.`id_product_extratab` = etl.`id_product_extratab` AND etl.`id_lang` = '.(int)$id_lang.') LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = et.`id_product` AND pl.`id_lang` = '.(int)$id_lang.') WHERE pl.id_product = '.(int)$id_product.' ORDER BY et.`position`' ); $displayList = array(); if ( count($extraTabs) ){ foreach ($extraTabs as $extraTab) { $displayList[] = array( 'id_product_extratab' => $extraTab['id_product_extratab'], 'product' => $extraTab['product'], 'associate' => 'product', 'extratab' => $extraTab['extratab'], 'position' => $extraTab['position'], ); } } return $displayList; } public static function getByProductID($id_product, $id_lang){ $collection = new Collection('ProductExtraTabModel', $id_lang); $collection->where('id_product', '=', $id_product); $collection->orderby('position'); $productExtraTabs = $collection->getResults(); $extraTabsIds = array(); $orderedExtraTabs = array(); foreach ($productExtraTabs as $productExtraTab) { if ( !in_array((int)$productExtraTab->id_extratab, $extraTabsIds) ){ $extraTabsIds[] = (int)$productExtraTab->id_extratab; $tabCollection = new Collection('ExtraTabModel', $id_lang); $tabCollection->where('id_extratab', '=', (int)$productExtraTab->id_extratab); $extraTab = $tabCollection->getFirst(); $orderedExtraTabs[(int)$productExtraTab->id_extratab] = array( 'id_extratab' => $extraTab->id_extratab, 'name' => $extraTab->name, 'description' => $extraTab->description, 'associate' => $extraTab->associate, 'position' => $extraTab->position ); } $orderedExtraTabs[(int)$productExtraTab->id_extratab]['productTabs'][] = $productExtraTab; } return $orderedExtraTabs; } public static function getMaxPosition($id_product){ return Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'product_extratab` WHERE `id_product`='.(int)$id_product); } public function updatePosition(){ // $products = Db::getInstance()->ExecuteS('SELECT DISTINCT `id_product` FROM `'._DB_PREFIX_.'product_extratab`'); // foreach ($products as $p) { // $tabs = Db::getInstance()->ExecuteS('SELECT * FROM `'._DB_PREFIX_.'product_extratab` WHERE `id_product` = '.(int)$p['id_product'].' ORDER BY `position` ASC'); // $pos=0; // foreach ($tabs as $tab) { // $pos++; // $extra = new ProductExtraTabModel((int)$tab['id_product_extratab']); // $extra->position = $pos; // $extra->save(); // } // } if ( !Tools::getValue('position') ) return false; $new_pos = Tools::getValue('position'); if ( $new_pos == $this->position ) return true; if ( $new_pos > $this->position ){ Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'product_extratab` SET `position` = `position`-1 WHERE `id_product` = '.(int)$this->id_product.' AND `position` BETWEEN '.(int)$this->position.' AND '.(int)$new_pos); } else{ Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'product_extratab` SET `position` = `position`+1 WHERE `id_product` = '.(int)$this->id_product.' AND `position` BETWEEN '.(int)$new_pos.' AND '.(int)$this->position); } $this->position = $new_pos; } }