2016-01-04 12:48:08 +01:00
< ? php
class Product extends ProductCore
{
2016-09-20 14:49:02 +02:00
public $description_more ;
public $description_comment ;
2016-06-06 14:35:07 +02:00
public $description_delivery ;
public $videos ;
/**
* @ Override
*/
protected $fieldsValidateLang = array (
'meta_description' => 'isGenericName' , 'meta_keywords' => 'isGenericName' ,
'meta_title' => 'isGenericName' , 'link_rewrite' => 'isLinkRewrite' , 'name' => 'isCatalogName' ,
'description' => 'isString' , 'description_short' => 'isString' , 'available_now' => 'isGenericName' , 'available_later' => 'IsGenericName' ,
2016-09-20 14:49:02 +02:00
'description_more' => 'isString' , 'description_comment' => 'isString' , 'videos' => 'isString' , 'description_delivery' => 'isString' );
2016-06-06 14:35:07 +02:00
/**
* @ Override
* Check then return multilingual fields for database interaction
*
* @ return array Multilingual fields
*/
public function getTranslationsFieldsChild ()
{
self :: validateFieldsLang ();
$fieldsArray = array ( 'meta_description' , 'meta_keywords' , 'meta_title' , 'link_rewrite' , 'name' , 'available_now' , 'available_later' );
$fields = array ();
$languages = Language :: getLanguages ( false );
$defaultLanguage = Configuration :: get ( 'PS_LANG_DEFAULT' );
foreach ( $languages as $language )
{
$fields [ $language [ 'id_lang' ]][ 'id_lang' ] = $language [ 'id_lang' ];
$fields [ $language [ 'id_lang' ]][ $this -> identifier ] = ( int )( $this -> id );
$fields [ $language [ 'id_lang' ]][ 'description' ] = ( isset ( $this -> description [ $language [ 'id_lang' ]])) ? pSQL ( $this -> description [ $language [ 'id_lang' ]], true ) : '' ;
$fields [ $language [ 'id_lang' ]][ 'description_short' ] = ( isset ( $this -> description_short [ $language [ 'id_lang' ]])) ? pSQL ( $this -> description_short [ $language [ 'id_lang' ]], true ) : '' ;
$fields [ $language [ 'id_lang' ]][ 'description_more' ] = ( isset ( $this -> description_more [ $language [ 'id_lang' ]])) ? pSQL ( $this -> description_more [ $language [ 'id_lang' ]], true ) : '' ;
2016-09-20 14:49:02 +02:00
$fields [ $language [ 'id_lang' ]][ 'description_comment' ] = ( isset ( $this -> description_comment [ $language [ 'id_lang' ]])) ? pSQL ( $this -> description_comment [ $language [ 'id_lang' ]], true ) : '' ;
2016-06-06 14:35:07 +02:00
$fields [ $language [ 'id_lang' ]][ 'videos' ] = ( isset ( $this -> videos [ $language [ 'id_lang' ]])) ? pSQL ( $this -> videos [ $language [ 'id_lang' ]], true ) : '' ;
$fields [ $language [ 'id_lang' ]][ 'description_delivery' ] = ( isset ( $this -> description_delivery [ $language [ 'id_lang' ]])) ? pSQL ( $this -> description_delivery [ $language [ 'id_lang' ]], true ) : '' ;
foreach ( $fieldsArray as $field )
{
if ( ! Validate :: isTableOrIdentifier ( $field ))
die ( Tools :: displayError ());
/* Check fields validity */
if ( isset ( $this -> { $field }[ $language [ 'id_lang' ]]) AND ! empty ( $this -> { $field }[ $language [ 'id_lang' ]]))
$fields [ $language [ 'id_lang' ]][ $field ] = pSQL ( $this -> { $field }[ $language [ 'id_lang' ]]);
elseif ( in_array ( $field , $this -> fieldsRequiredLang ))
{
if ( $this -> { $field } != '' )
$fields [ $language [ 'id_lang' ]][ $field ] = pSQL ( $this -> { $field }[ $defaultLanguage ]);
}
else
$fields [ $language [ 'id_lang' ]][ $field ] = '' ;
}
}
return $fields ;
}
public function getCustomFields () {
$customs_data = Db :: getInstance () -> getRow ( '
SELECT `nc8` , `id_country`
FROM `'._DB_PREFIX_.'product_customs`
WHERE id_product = ' . ( int ) $this -> id
);
return $customs_data ;
}
2016-04-14 13:06:06 +02:00
/**
* @ Override
* Admin panel product search
2016-09-20 14:49:02 +02:00
*
2016-04-14 13:06:06 +02:00
* @ param integer $id_lang Language id
* @ param string $query Search query
* @ return array Matching products
*/
public static function searchByName ( $id_lang , $query , $orderBy = 'name' , $order = 'ASC' )
{
$result = Db :: getInstance () -> ExecuteS ( '
SELECT p . `id_product` , pl . `name` , p . `active` , p . `reference` , m . `name` AS manufacturer_name ,
MIN ( cp . `id_category` ) AS `main_category_id`
-- ( SELECT name FROM ps_category_lang WHERE `id_category` = MIN ( cp . `id_category` ) AND `id_lang` = 1 ) AS `main_category`
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p ON p . `id_product` = cp . `id_product`
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON ( p . `id_product` = pl . `id_product` AND pl . `id_lang` = '.(int)($id_lang).' )
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m . `id_manufacturer` = p . `id_manufacturer`
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON pa . `id_product` = p . `id_product`
WHERE pl . `name` LIKE \ '%' . pSQL ( $query ) . '%\' OR p.`reference` LIKE \'%' . pSQL ( $query ) . '%\' OR p.`supplier_reference` LIKE \'%' . pSQL ( $query ) . '%\' OR pa.`reference` LIKE \'%' . pSQL ( $query ) . ' % \ '
GROUP BY `id_product`
ORDER BY pl . `'.$orderBy.'` ' . $order );
if ( ! $result )
return false ;
$resultsArray = array ();
foreach ( $result AS $row )
{
$row [ 'price' ] = Product :: getPriceStatic ( $row [ 'id_product' ], true , NULL , 2 );
$row [ 'quantity' ] = Product :: getQuantity ( $row [ 'id_product' ]);
$resultsArray [] = $row ;
}
return $resultsArray ;
}
2016-01-04 12:48:08 +01:00
public static function getProductsProperties ( $id_lang , $query_result , $cached = true )
{
$resultsArray = array ();
if ( is_array ( $query_result ))
foreach ( $query_result AS $row )
if ( $row2 = Product :: getProductProperties ( $id_lang , $row , $cached ))
$resultsArray [] = $row2 ;
return $resultsArray ;
}
public static function getProductProperties ( $id_lang , $row , $cached = true )
{
if ( ! $row [ 'id_product' ])
return false ;
// Product::getDefaultAttribute is only called if id_product_attribute is missing from the SQL query at the origin of it: consider adding it in order to avoid unnecessary queries
$row [ 'allow_oosp' ] = Product :: isAvailableWhenOutOfStock ( $row [ 'out_of_stock' ]);
if (( ! isset ( $row [ 'id_product_attribute' ]) OR ! $row [ 'id_product_attribute' ])
AND (( isset ( $row [ 'cache_default_attribute' ]) AND ( $ipa_default = $row [ 'cache_default_attribute' ]) !== NULL )
OR ( $ipa_default = Product :: getDefaultAttribute ( $row [ 'id_product' ], ! $row [ 'allow_oosp' ])))
)
$row [ 'id_product_attribute' ] = $ipa_default ;
if ( ! isset ( $row [ 'id_product_attribute' ]))
$row [ 'id_product_attribute' ] = 0 ;
// Tax
$usetax = Tax :: excludeTaxeOption ();
if ( $cached ) {
$cacheKey = $row [ 'id_product' ] . '-' . $row [ 'id_product_attribute' ] . '-' . $id_lang . '-' . ( int )( $usetax );
if ( array_key_exists ( $cacheKey , self :: $producPropertiesCache ))
2016-03-09 16:33:05 +01:00
return self :: $producPropertiesCache [ $cacheKey ];
2016-01-04 12:48:08 +01:00
}
// Datas
$link = new Link ();
$row [ 'category' ] = Category :: getLinkRewrite (( int ) $row [ 'id_category_default' ], ( int )( $id_lang ));
$row [ 'link' ] = $link -> getProductLink (( int ) $row [ 'id_product' ], $row [ 'link_rewrite' ], $row [ 'category' ], $row [ 'ean13' ]);
$row [ 'attribute_price' ] = ( isset ( $row [ 'id_product_attribute' ]) AND $row [ 'id_product_attribute' ]) ? ( float )( Product :: getProductAttributePrice ( $row [ 'id_product_attribute' ])) : 0 ;
$row [ 'price_tax_exc' ] = Product :: getPriceStatic (( int ) $row [ 'id_product' ], false , (( isset ( $row [ 'id_product_attribute' ]) AND ! empty ( $row [ 'id_product_attribute' ])) ? ( int )( $row [ 'id_product_attribute' ]) : NULL ), ( self :: $_taxCalculationMethod == PS_TAX_EXC ? 2 : 6 ));
if ( self :: $_taxCalculationMethod == PS_TAX_EXC )
{
$row [ 'price_tax_exc' ] = Tools :: ps_round ( $row [ 'price_tax_exc' ], 2 );
$row [ 'price' ] = Product :: getPriceStatic (( int ) $row [ 'id_product' ], true , (( isset ( $row [ 'id_product_attribute' ]) AND ! empty ( $row [ 'id_product_attribute' ])) ? ( int )( $row [ 'id_product_attribute' ]) : NULL ), 6 );
$row [ 'price_without_reduction' ] = Product :: getPriceStatic (( int ) $row [ 'id_product' ], false , (( isset ( $row [ 'id_product_attribute' ]) AND ! empty ( $row [ 'id_product_attribute' ])) ? ( int )( $row [ 'id_product_attribute' ]) : NULL ), 2 , NULL , false , false );
}
else
{
$row [ 'price' ] = Tools :: ps_round ( Product :: getPriceStatic (( int ) $row [ 'id_product' ], true , (( isset ( $row [ 'id_product_attribute' ]) AND ! empty ( $row [ 'id_product_attribute' ])) ? ( int )( $row [ 'id_product_attribute' ]) : NULL ), 2 ), 2 );
$row [ 'price_without_reduction' ] = Product :: getPriceStatic (( int ) $row [ 'id_product' ], true , (( isset ( $row [ 'id_product_attribute' ]) AND ! empty ( $row [ 'id_product_attribute' ])) ? ( int )( $row [ 'id_product_attribute' ]) : NULL ), 6 , NULL , false , false );
}
$row [ 'reduction' ] = Product :: getPriceStatic (( int )( $row [ 'id_product' ]), ( bool ) $usetax , ( int )( $row [ 'id_product_attribute' ]), 6 , NULL , true , true , 1 , true , NULL , NULL , NULL , $specific_prices );
$row [ 'specific_prices' ] = $specific_prices ;
if ( $row [ 'id_product_attribute' ])
{
2016-08-31 11:55:14 +02:00
$row [ 'quantity_all_versions' ] = Product :: getTotalAttributeQuantity (( int ) $row [ 'id_product' ]);
2016-01-04 12:48:08 +01:00
$row [ 'quantity' ] = Product :: getQuantity (( int ) $row [ 'id_product' ], $row [ 'id_product_attribute' ], isset ( $row [ 'cache_is_pack' ]) ? $row [ 'cache_is_pack' ] : NULL );
}
$row [ 'id_image' ] = Product :: defineProductImage ( $row , $id_lang );
$row [ 'features' ] = Product :: getFrontFeaturesStatic (( int ) $id_lang , $row [ 'id_product' ]);
$row [ 'attachments' ] = (( ! isset ( $row [ 'cache_has_attachments' ]) OR $row [ 'cache_has_attachments' ]) ? Product :: getAttachmentsStatic (( int )( $id_lang ), $row [ 'id_product' ]) : array ());
// Pack management
$row [ 'pack' ] = ( ! isset ( $row [ 'cache_is_pack' ]) ? Pack :: isPack ( $row [ 'id_product' ]) : ( int ) $row [ 'cache_is_pack' ]);
$row [ 'packItems' ] = $row [ 'pack' ] ? Pack :: getItemTable ( $row [ 'id_product' ], $id_lang ) : array ();
$row [ 'nopackprice' ] = $row [ 'pack' ] ? Pack :: noPackPrice ( $row [ 'id_product' ]) : 0 ;
if ( $row [ 'pack' ] AND ! Pack :: isInStock ( $row [ 'id_product' ]))
$row [ 'quantity' ] = 0 ;
if ( $cached ) {
self :: $producPropertiesCache [ $cacheKey ] = $row ;
2016-03-09 16:33:05 +01:00
return self :: $producPropertiesCache [ $cacheKey ];
2016-01-04 12:48:08 +01:00
} else {
return $row ;
}
}
public function toggleCoupCoeur (){
if ( $this -> online_only == 1 ){
$this -> online_only = 0 ;
$this -> save ();
} else {
$this -> online_only = 1 ;
$this -> save ();
}
}
2016-03-09 16:33:05 +01:00
public static function getDetailsCombination ( $id_product_attribute , $id_lang ) {
return Db :: getInstance () -> ExecuteS ( '
SELECT pa .* , ag . `id_attribute_group` , ag . `is_color_group` , agl . `name` AS group_name , al . `name` AS attribute_name , a . `id_attribute` , pa . `unit_price_impact`
FROM `'._DB_PREFIX_.'product_attribute` pa
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac . `id_product_attribute` = pa . `id_product_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute` a ON a . `id_attribute` = pac . `id_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute_group` ag ON ag . `id_attribute_group` = a . `id_attribute_group`
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON ( a . `id_attribute` = al . `id_attribute` AND al . `id_lang` = '.(int)($id_lang).' )
LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON ( ag . `id_attribute_group` = agl . `id_attribute_group` AND agl . `id_lang` = '.(int)($id_lang).' )
WHERE pa . `id_product_attribute` = '.(int)($id_product_attribute).'
ORDER BY pa . `id_product_attribute` ' );
}
2016-03-21 10:07:35 +01:00
2016-03-23 11:49:34 +01:00
public static function duplicateOneAttribute ( $id_product_old , $id_product_new , $old_attribute_id , $quantity = 0 )
2016-03-21 10:07:35 +01:00
{
$return = true ;
$combinationImages = array ();
$result = Db :: getInstance () -> ExecuteS ( '
SELECT *
FROM `'._DB_PREFIX_.'product_attribute`
WHERE `id_product` = '.(int)($id_product_old).' AND `id_product_attribute` = ' . ( int )( $old_attribute_id ));
foreach ( $result as $row )
{
$id_product_attribute_old = ( int )( $row [ 'id_product_attribute' ]);
$result2 = Db :: getInstance () -> ExecuteS ( '
SELECT *
FROM `'._DB_PREFIX_.'product_attribute_combination`
WHERE `id_product_attribute` = ' . $id_product_attribute_old );
2016-03-23 11:49:34 +01:00
if ( $quantity > 0 ) {
$row [ 'quantity' ] = $quantity ;
}
$row [ 'id_product' ] = $id_product_new ;
unset ( $row [ 'id_product_attribute' ]);
$return &= Db :: getInstance () -> AutoExecute ( _DB_PREFIX_ . 'product_attribute' , $row , 'INSERT' );
$id_product_attribute_new = ( int )( Db :: getInstance () -> Insert_ID ());
if ( $resultImages = self :: _getAttributeImageAssociations ( $id_product_attribute_old ))
{
$combinationImages [ 'old' ][ $id_product_attribute_old ] = $resultImages ;
$combinationImages [ 'new' ][ $id_product_attribute_new ] = $resultImages ;
}
foreach ( $result2 AS $row2 )
{
$row2 [ 'id_product_attribute' ] = $id_product_attribute_new ;
$return &= Db :: getInstance () -> AutoExecute ( _DB_PREFIX_ . 'product_attribute_combination' , $row2 , 'INSERT' );
}
}
return ! $return ? false : $combinationImages ;
}
/*
* @ Override
* Ajout d ' une quantité choisie
*/
public static function duplicateAttributes ( $id_product_old , $id_product_new , $quantity = 0 )
{
$return = true ;
$combinationImages = array ();
$result = Db :: getInstance () -> ExecuteS ( '
SELECT *
FROM `'._DB_PREFIX_.'product_attribute`
WHERE `id_product` = ' . ( int )( $id_product_old ));
foreach ( $result as $row )
{
$id_product_attribute_old = ( int )( $row [ 'id_product_attribute' ]);
$result2 = Db :: getInstance () -> ExecuteS ( '
SELECT *
FROM `'._DB_PREFIX_.'product_attribute_combination`
WHERE `id_product_attribute` = ' . $id_product_attribute_old );
if ( $quantity > 0 ) {
$row [ 'quantity' ] = $quantity ;
}
2016-03-21 10:07:35 +01:00
$row [ 'id_product' ] = $id_product_new ;
unset ( $row [ 'id_product_attribute' ]);
$return &= Db :: getInstance () -> AutoExecute ( _DB_PREFIX_ . 'product_attribute' , $row , 'INSERT' );
$id_product_attribute_new = ( int )( Db :: getInstance () -> Insert_ID ());
if ( $resultImages = self :: _getAttributeImageAssociations ( $id_product_attribute_old ))
{
$combinationImages [ 'old' ][ $id_product_attribute_old ] = $resultImages ;
$combinationImages [ 'new' ][ $id_product_attribute_new ] = $resultImages ;
}
foreach ( $result2 AS $row2 )
{
$row2 [ 'id_product_attribute' ] = $id_product_attribute_new ;
$return &= Db :: getInstance () -> AutoExecute ( _DB_PREFIX_ . 'product_attribute_combination' , $row2 , 'INSERT' );
}
}
return ! $return ? false : $combinationImages ;
}
2016-08-31 11:55:14 +02:00
public static function getTotalAttributeQuantity ( $id_product )
{
$quantity = Db :: getInstance () -> getValue ( '
SELECT SUM ( `quantity` )
FROM `'._DB_PREFIX_.'product_attribute`
WHERE `id_product` = ' . ( int )( $id_product )
);
return $quantity ;
}
2016-01-04 12:48:08 +01:00
}