privilegedemarque/modules/extratabs/models/ProductExtraTabModel.php
Serveur preprod f0c0c48223 first push
2016-04-14 16:14:31 +02:00

192 lines
6.8 KiB
PHP
Executable File

<?php
/*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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;
}
}