Serveur preprod f0c0c48223 first push
2016-04-14 16:14:31 +02:00

338 lines
8.6 KiB
PHP
Executable File

<?php
/**
* Description of PictosInfo
*
* @author Pierre Beaumont
* @company Antadis
*/
class Picto extends ObjectModel{
public $id_picto;
public $name;
public $title;
public $alt;
public $link;
public $description;
public $date_add;
public $date_upd;
/**
*
* @var type
*/
public static $definition = array(
'table' => 'picto',
'primary' => 'id_picto',
'multilang' => true,
'fields' => array(
'name' => array(
'type' => ObjectModel::TYPE_STRING,
'validate' => 'isString',
'lang' => true
),
'title' => array(
'type' => ObjectModel::TYPE_STRING,
'validate' => 'isString',
'lang' => true
),
'alt' => array(
'type' => ObjectModel::TYPE_STRING,
'validate' => 'isString',
'lang' => true
),
'link' => array(
'type' => ObjectModel::TYPE_STRING,
'validate' => 'isString',
'lang' => true
),
'description' => array(
'type' => ObjectModel::TYPE_STRING,
'validate' => 'isString',
'lang' => true
),
'date_add' => array(
'type' => ObjectModel::TYPE_DATE,
),
'date_upd' => array(
'type' => ObjectModel::TYPE_DATE,
)
)
);
public function __construct($id = null, $id_lang = null, $id_shop = null){
parent::__construct($id, $id_lang, $id_shop);
}
public function delete(){
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'picto_product` WHERE `id_picto` = '.(int)$this->id_picto);
parent::delete();
}
/**
* Get all pictos that have been linked to the product
* @param Int $id_product Product ID
* @return Array<Picto> List of Pictos linked to the product
*/
public static function getAllPictoInfoChecked($id_product){
$query = new DbQuery();
$query->select('`id_picto`');
$query->from('picto_product');
$query->where('`id_product` = '.(int)$id_product);
$rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
if ($rows) {
$obj = array();
foreach($rows as $row){
$tmp_obj = new Picto($row['id_picto']);
$obj[] = $tmp_obj;
}
return $obj;
}
return false;
}
public static function getPosition($id_picto, $id_product){
$query = new DbQuery();
$query->select('`order`');
$query->from('picto_product');
$query->where('`id_picto` = '.(int)$id_picto);
$query->where('`id_product` = '.(int)$id_product);
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
}
/**
* Get all pictos that have not been linked to the product
* @param Int $id_product Product ID
* @return Array<Picto> List of Pictos not linked to the product
*/
public static function getAllPictoInfoNotChecked($id_product){
$prefix = _DB_PREFIX_ ;
$req = sprintf('
SELECT `id_picto`
FROM `%spicto`
WHERE `id_picto`
NOT IN (
SELECT `id_picto`
FROM `%spicto_product`
WHERE `id_product` = "%s"
)',$prefix,$prefix,(int)$id_product);
$rows = Db::getInstance()->executeS($req);
if ($rows) {
$obj = array();
foreach($rows as $row){
$tmp_obj = new Picto($row['id_picto']);
$obj[] = $tmp_obj;
}
return $obj;
}
return false;
}
/**
* Get all pictos
* @return Array<Picto> Array containing all Pictos
*/
public static function getAllPictos(){
$collection = new Collection('Picto', Context::getContext()->language->id);
return $collection->getAll();
}
/**
* Delete all PictosProduct for given product
* @param $id_product Product ID
*/
public static function DelAllPictosForProduct($id_product){
$prefix = _DB_PREFIX_;
$req = sprintf('DELETE FROM %spicto_product WHERE id_product = %d',$prefix,(int)$id_product);
Db::getInstance()->execute($req);
}
/**
* Get all Pictos for given product
* @param Int $id_product Product ID
* @return Array<Picto> List of Picto linked to current product
*/
public static function getPictosForProduct($id_product){
$query = new DbQuery();
$query->select('pp.`id_picto`');
$query->from('picto_product', 'pp');
$query->where('pp.`id_product` = '.(int)$id_product);
$query->orderBy(' pp.`order` ASC');
$rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($query);
if ($rows) {
$obj = array();
foreach($rows as $row){
$obj[] = new Picto((int)$row['id_picto']);
}
return $obj;
}
return false;
}
/**
* Get product from picto
*/
public static function getProductFromPicto($id_picto){
$query = new DbQuery();
$query->select('`id_product`');
$query->from('`picto_product`');
$query->where('`id_picto` = '.(int)$id_picto);
$rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($query);
$ret = array();
foreach ($rows as $row) {
$ret[] = $row['id_product'];
}
}
/**
* Update picto order
* @param Int $id_picto Picto ID
* @param Int $id_product Product ID
* @param Int $order Picto order
* @return Bool picto removed
*/
public static function updateOrder($id_picto, $id_product, $order){
return Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'picto_product` SET `order` = '.(int)$order.' WHERE `id_product` = '.(int)$id_product.' AND `id_picto` = '.(int)$id_picto);
}
/**
* Remove picto from product
* @param Int $id_picto Picto ID
* @param Int $id_product Product ID
* @param Bool $reOrder Re order pictos after deletion
* @return Bool picto removed
*/
public static function removeFromProduct($id_picto, $id_product=NULL, $reOrder=true){
$sql = 'DELETE FROM `'._DB_PREFIX_.'picto_product` WHERE `id_picto` = '.(int)$id_picto;
if ( $id_product )
$sql .= ' AND `id_product` = '.(int)$id_product;
$ret = Db::getInstance()->Execute($sql);
if ( !$id_product )
return $ret;
if ( $ret ){
self::reOrderPictos((int)$id_product);
}
return $ret;
}
/**
* Re order pictos after
* @param Int $id_product Product ID
*/
public static function reOrderPictos($id_product){
$compt = 1;
$pictos = self::getPictosForProduct((int)$id_product);
foreach ($pictos as $picto) {
Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'picto_product` SET `order` = '.(int)$compt.' WHERE `id_picto` = '.$picto->id_picto.' AND `id_product` = '.(int)$id_product);
$compt++;
}
}
/**
* Get nb pictos [for product]
* @param Int $id_picto Picto ID
* @param Int $id_product Product ID
* @return Int Nb pictos
*/
public static function getNbPictos($id_product=NULL){
if ( !$id_product )
return count(self::getAllPictos());
$query = new DbQuery();
$query->select('COUNT(*)');
$query->from('picto_product');
$query->where('`id_product` = '.(int)$id_product);
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
}
/**
* Get if picto is already add to the product
* @param Int $id_picto Picto ID
* @param Int $id_product Product ID
* @return Bool Picto is linked to product
*/
public static function isPictoProduct($id_picto, $id_product){
return Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'picto_product` WHERE `id_picto` = '.(int)$id_picto.' AND `id_product` = '.(int)$id_product);
}
/**
* Add picto to product
* @param Int $id_picto Picto ID
* @param Int $id_product Product ID
* @return Bool Picto is linked to product
*/
public static function addToProduct($id_picto, $id_product){
if ( self::isPictoProduct($id_picto, $id_product) )
return true;
$nb_pictos = self::getNbPictos($id_product);
$order=(int)$nb_pictos+1;
$ret = Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'picto_product` (`id_picto`, `id_product`, `order`)
VALUES ('.(int)$id_picto.', '.(int)$id_product.', '.(int)$order.')');
if ( $ret ){
self::reOrderPictos((int)$id_product);
return true;
}
return false;
}
/**
* Duplicate product picto to all product of current category
* @param Int $id_product Product ID
* @param Int $id_category Category ID
* @param Bool $all_categories All sub categories or current category only
*/
public static function duplicatePictosToCategory($id_product, $id_category, $all_categories){
$categories = array();
$pictos = self::getPictosForProduct((int)$id_product);
$category = new Category((int)$id_category);
$categories[] = $category;
$subcategories = $category->getChildrenWs();
foreach ($subcategories as $subcategory){
$categories[] = new Category((int)$subcategory['id']);
}
foreach ($categories as $category) {
$products = $category->getProductsWs();
if ( $products ){
foreach ($products as $product) {
// Current product do nothing
if ( (int)$product['id'] == (int)$id_product )
continue;
// Delete pictos for product
Picto::DelAllPictosForProduct((int)$product['id']);
// Add picto to product
if ( $pictos ){
foreach ($pictos as $picto) {
self::addToProduct((int)$picto->id_picto, (int)$product['id']);
}
}
}
}
}
}
}
?>