2016-07-23 16:37:21 +02:00
< ? php
2024-10-13 21:08:55 +02:00
/* Copyright ( C ) 2016 Marcos García < marcosgdf @ gmail . com >
* Copyright ( C ) 2022 Open - Dsi < support @ open - dsi . fr >
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2016-07-23 16:37:21 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2016-07-23 16:37:21 +02:00
*/
2022-03-18 15:38:44 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobjectline.class.php' ;
2016-07-25 10:37:39 +02:00
/**
* Class ProductAttributeValue
* Used to represent a product attribute value
*/
2022-03-18 15:38:44 +01:00
class ProductAttributeValue extends CommonObjectLine
2016-07-23 16:37:21 +02:00
{
/**
2022-03-18 15:38:44 +01:00
* @ var string ID of module .
2016-07-23 16:37:21 +02:00
*/
2022-03-18 15:38:44 +01:00
public $module = 'variants' ;
2016-07-23 16:37:21 +02:00
/**
2022-03-18 15:38:44 +01:00
* @ var string ID to identify managed object .
2016-07-23 16:37:21 +02:00
*/
2022-03-18 15:38:44 +01:00
public $element = 'productattributevalue' ;
2016-07-23 16:37:21 +02:00
/**
2022-03-18 15:38:44 +01:00
* @ var string Name of table without prefix where object is stored . This is also the key used for extrafields management .
2016-07-23 16:37:21 +02:00
*/
2022-03-18 15:38:44 +01:00
public $table_element = 'product_attribute_value' ;
2016-07-23 16:37:21 +02:00
/**
2022-03-18 15:38:44 +01:00
* 'type' field format ( 'integer' , 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]' , 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter]]]' , 'varchar(x)' , 'double(24,8)' , 'real' , 'price' , 'text' , 'text:none' , 'html' , 'date' , 'datetime' , 'timestamp' , 'duration' , 'mail' , 'phone' , 'url' , 'password' )
* Note : Filter can be a string like " (t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL) "
* 'label' the translation key .
* 'picto' is code of a picto to show before value in forms
2024-03-22 00:37:56 +01:00
* 'enabled' is a condition when the field must be managed ( Example : 1 or 'getDolGlobalString("MY_SETUP_PARAM")'
2022-03-18 15:38:44 +01:00
* 'position' is the sort order of field .
* 'notnull' is set to 1 if not null in database . Set to - 1 if we must set data to null if empty ( '' or 0 ) .
* 'visible' says if field is visible in list ( Examples : 0 = Not visible , 1 = Visible on list and create / update / view forms , 2 = Visible on list only , 3 = Visible on create / update / view form only ( not list ), 4 = Visible on list and update / view form only ( not create ) . 5 = Visible on list and view only ( not create / not update ) . Using a negative value means field is not shown by default on list but can be selected for viewing )
* 'noteditable' says if field is not editable ( 1 or 0 )
* 'default' is a default value for creation ( can still be overwrote by the Setup of Default Values if field is editable in creation form ) . Note : If default is set to '(PROV)' and field is 'ref' , the default value will be set to '(PROVid)' where id is rowid when a new record is created .
* 'index' if we want an index in database .
2024-01-12 17:18:52 +01:00
* 'foreignkey' => 'tablename.field' if the field is a foreign key ( it is recommended to name the field fk_ ... ) .
2022-03-18 15:38:44 +01:00
* 'searchall' is 1 if we want to search in this field when making a search from the quick search button .
* 'isameasure' must be set to 1 if you want to have a total on list for this field . Field type must be summable like integer or double ( 24 , 8 ) .
* 'css' and 'cssview' and 'csslist' is the CSS style to use on field . 'css' is used in creation and update . 'cssview' is used in view mode . 'csslist' is used for columns in lists . For example : 'maxwidth200' , 'wordbreak' , 'tdoverflowmax200'
* 'help' is a 'TranslationString' to use to show a tooltip on field . You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click .
* 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
* 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute . In most cases , this is never set into the definition of $fields into class , but is set dynamically by some part of code .
* 'arrayofkeyval' to set list of value if type is a list of predefined values . For example : array ( " 0 " => " Draft " , " 1 " => " Active " , " -1 " => " Cancel " )
* 'autofocusoncreate' to have field having the focus on a create form . Only 1 field should have this property set to 1.
* 'comment' is not used . You can store here any text of your choice . It is not used by application .
*
* Note : To have value dynamic , you can set value to 0 in definition and edit the value on the fly into the constructor .
2016-07-23 16:37:21 +02:00
*/
2022-03-18 15:38:44 +01:00
/**
2025-02-05 00:40:06 +01:00
* @ var array < string , array { type : string , label : string , enabled : int < 0 , 2 >| string , position : int , notnull ? : int , visible : int <- 6 , 6 >| string , alwayseditable ? : int < 0 , 1 > , noteditable ? : int < 0 , 1 > , default ? : string , index ? : int , foreignkey ? : string , searchall ? : int < 0 , 1 > , isameasure ? : int < 0 , 1 > , css ? : string , csslist ? : string , help ? : string , showoncombobox ? : int < 0 , 4 > , disabled ? : int < 0 , 1 > , arrayofkeyval ? : array < int | string , string > , autofocusoncreate ? : int < 0 , 1 > , comment ? : string , copytoclipboard ? : int < 1 , 2 > , validate ? : int < 0 , 1 > , showonheader ? : int < 0 , 1 > } > Array with all fields and their property . Do not use it as a static var . It may be modified by constructor .
2022-03-18 15:38:44 +01:00
*/
2024-03-12 13:38:08 +01:00
public $fields = array (
2024-03-15 00:54:04 +01:00
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'position' => 1 , 'notnull' => 1 , 'visible' => 0 , 'noteditable' => 1 , 'index' => 1 , 'css' => 'left' , 'comment' => " Id " ),
2024-03-12 13:38:08 +01:00
'fk_product_attribute' => array ( 'type' => 'integer:ProductAttribute:variants/class/ProductAttribute.class.php' , 'label' => 'ProductAttribute' , 'enabled' => 1 , 'visible' => 0 , 'position' => 10 , 'notnull' => 1 , 'index' => 1 ,),
'ref' => array ( 'type' => 'varchar(255)' , 'label' => 'Ref' , 'visible' => 1 , 'enabled' => 1 , 'position' => 20 , 'notnull' => 1 , 'index' => 1 , 'searchall' => 1 , 'comment' => " Reference of object " , 'css' => '' ),
2024-03-15 00:54:04 +01:00
'value' => array ( 'type' => 'varchar(255)' , 'label' => 'Value' , 'enabled' => 1 , 'position' => 30 , 'notnull' => 1 , 'visible' => 1 , 'searchall' => 1 , 'css' => 'minwidth300' , 'help' => " " , 'showoncombobox' => 1 ,),
'position' => array ( 'type' => 'integer' , 'label' => 'Rank' , 'enabled' => 1 , 'visible' => 0 , 'default' => '0' , 'position' => 200 , 'notnull' => 1 ,),
2022-03-18 15:38:44 +01:00
);
2024-06-17 13:22:47 +02:00
/**
* ID of the ProductAttributeValue
* @ var int
*/
2022-03-18 15:38:44 +01:00
public $id ;
2024-06-17 13:22:47 +02:00
/**
* ID of the parent attribute ( ex : ID of the attribute " COLOR " )
* @ var int
*/
2022-03-18 15:38:44 +01:00
public $fk_product_attribute ;
2024-06-17 13:22:47 +02:00
/**
* Reference of the ProductAttributeValue ( ex : " BLUE_1 " or " RED_3 " )
* @ var string
*/
2022-03-18 15:38:44 +01:00
public $ref ;
2024-06-17 13:22:47 +02:00
/**
* Label of the ProductAttributeValue ( ex : " Dark blue " or " Chili Red " )
* @ var string
*/
2016-07-23 16:37:21 +02:00
public $value ;
2024-06-17 13:22:47 +02:00
/**
* Sorting position of the ProductAttributeValue
* @ var int
*/
2022-03-18 15:38:44 +01:00
public $position ;
2016-07-23 16:37:21 +02:00
2020-09-07 10:18:17 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( DoliDB $db )
{
2022-03-18 15:38:44 +01:00
global $conf , $langs ;
2016-07-23 16:37:21 +02:00
$this -> db = $db ;
2024-05-05 00:34:19 +02:00
$this -> ismultientitymanaged = 1 ;
2024-10-13 21:08:55 +02:00
$this -> isextrafieldmanaged = 1 ;
2016-07-23 16:37:21 +02:00
$this -> entity = $conf -> entity ;
2022-03-18 15:38:44 +01:00
2023-11-27 12:08:48 +01:00
if ( ! getDolGlobalString ( 'MAIN_SHOW_TECHNICAL_ID' ) && isset ( $this -> fields [ 'rowid' ])) {
2022-03-18 15:38:44 +01:00
$this -> fields [ 'rowid' ][ 'visible' ] = 0 ;
}
2022-08-28 14:43:31 +02:00
if ( ! isModEnabled ( 'multicompany' ) && isset ( $this -> fields [ 'entity' ])) {
2022-03-18 15:38:44 +01:00
$this -> fields [ 'entity' ][ 'enabled' ] = 0 ;
}
// Unset fields that are disabled
foreach ( $this -> fields as $key => $val ) {
if ( isset ( $val [ 'enabled' ]) && empty ( $val [ 'enabled' ])) {
unset ( $this -> fields [ $key ]);
}
}
// Translate some data of arrayofkeyval
if ( is_object ( $langs )) {
foreach ( $this -> fields as $key => $val ) {
if ( ! empty ( $val [ 'arrayofkeyval' ]) && is_array ( $val [ 'arrayofkeyval' ])) {
foreach ( $val [ 'arrayofkeyval' ] as $key2 => $val2 ) {
$this -> fields [ $key ][ 'arrayofkeyval' ][ $key2 ] = $langs -> trans ( $val2 );
}
}
}
}
}
/**
* Creates a value for a product attribute
*
* @ param User $user Object user
* @ param int $notrigger Do not execute trigger
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 KO > 0 OK
2022-03-18 15:38:44 +01:00
*/
public function create ( User $user , $notrigger = 0 )
{
global $langs ;
$error = 0 ;
// Clean parameters
$this -> fk_product_attribute = $this -> fk_product_attribute > 0 ? $this -> fk_product_attribute : 0 ;
$this -> ref = strtoupper ( dol_sanitizeFileName ( dol_string_nospecial ( trim ( $this -> ref )))); // Ref must be uppercase
$this -> value = trim ( $this -> value );
// Check parameters
if ( empty ( $this -> fk_product_attribute )) {
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " ProductAttribute " ));
$error ++ ;
}
if ( empty ( $this -> ref )) {
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Ref " ));
$error ++ ;
}
if ( empty ( $this -> value )) {
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Value " ));
$error ++ ;
}
2024-10-13 21:08:55 +02:00
// Position to use
if ( empty ( $this -> position )) {
$positionmax = $this -> getMaxAttributesValuesPosition ( $this -> fk_product_attribute );
$this -> position = $positionmax + 1 ;
}
2022-03-18 15:38:44 +01:00
if ( $error ) {
dol_syslog ( __METHOD__ . ' ' . $this -> errorsToString (), LOG_ERR );
return - 1 ;
}
$this -> db -> begin ();
$sql = " INSERT INTO " . MAIN_DB_PREFIX . $this -> table_element . " ( " ;
$sql .= " fk_product_attribute, ref, value, entity, position " ;
$sql .= " ) " ;
$sql .= " VALUES ( " ;
$sql .= " " . (( int ) $this -> fk_product_attribute );
$sql .= " , ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> value ) . " ' " ;
$sql .= " , " . (( int ) $this -> entity );
$sql .= " , " . (( int ) $this -> position );
$sql .= " ) " ;
dol_syslog ( __METHOD__ , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> errors [] = " Error " . $this -> db -> lasterror ();
$error ++ ;
}
if ( ! $error ) {
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . $this -> table_element );
2024-10-13 21:08:55 +02:00
$result = $this -> insertExtraFields ();
if ( $result < 0 ) {
$error ++ ;
}
2022-03-18 15:38:44 +01:00
}
if ( ! $error && ! $notrigger ) {
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_ATTRIBUTE_VALUE_CREATE' , $user );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
if ( $error ) {
$this -> db -> rollback ();
return - 1 * $error ;
} else {
$this -> db -> commit ();
return $this -> id ;
}
2020-09-07 10:18:17 +02:00
}
2016-07-23 16:37:21 +02:00
/**
* Gets a product attribute value
*
2022-03-18 15:38:44 +01:00
* @ param int $id Product attribute value id
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 KO , > 0 OK
2016-07-23 16:37:21 +02:00
*/
2022-03-18 15:38:44 +01:00
public function fetch ( $id )
2016-07-23 16:37:21 +02:00
{
2022-03-18 15:38:44 +01:00
global $langs ;
$error = 0 ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
// Clean parameters
$id = $id > 0 ? $id : 0 ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
// Check parameters
if ( empty ( $id )) {
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " TechnicalID " ));
$error ++ ;
}
if ( $error ) {
dol_syslog ( __METHOD__ . ' ' . $this -> errorsToString (), LOG_ERR );
2016-07-23 16:37:21 +02:00
return - 1 ;
}
2022-03-18 15:38:44 +01:00
$sql = " SELECT rowid, fk_product_attribute, ref, value " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " WHERE rowid = " . (( int ) $id );
$sql .= " AND entity IN ( " . getEntity ( 'product' ) . " ) " ;
dol_syslog ( __METHOD__ , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> errors [] = " Error " . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . $this -> errorsToString (), LOG_ERR );
2016-07-23 16:37:21 +02:00
return - 1 ;
}
2022-03-18 15:38:44 +01:00
$numrows = $this -> db -> num_rows ( $resql );
if ( $numrows ) {
$obj = $this -> db -> fetch_object ( $resql );
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
$this -> id = $obj -> rowid ;
$this -> fk_product_attribute = $obj -> fk_product_attribute ;
$this -> ref = $obj -> ref ;
$this -> value = $obj -> value ;
2024-10-13 21:08:55 +02:00
$this -> fetch_optionals ();
2022-03-18 15:38:44 +01:00
}
$this -> db -> free ( $resql );
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
return $numrows ;
2016-07-23 16:37:21 +02:00
}
/**
* Returns all product attribute values of a product attribute
*
2022-10-08 00:07:20 +02:00
* @ param int $prodattr_id Product attribute id
* @ param bool $only_used Fetch only used attribute values
2024-03-29 18:23:08 +01:00
* @ param int < 0 , 1 > $returnonlydata 0 : return object , 1 : return only data
* @ return ProductAttributeValue [] | stdClass [] Array of object
2016-07-23 16:37:21 +02:00
*/
2022-10-08 07:49:02 +02:00
public function fetchAllByProductAttribute ( $prodattr_id , $only_used = false , $returnonlydata = 0 )
2016-07-23 16:37:21 +02:00
{
$return = array ();
2024-11-10 18:15:04 +01:00
$sql = " SELECT v.fk_product_attribute, v.rowid, v.ref, v.value FROM " . MAIN_DB_PREFIX . " product_attribute_value v " ;
2017-11-26 15:08:00 +01:00
2022-03-18 15:38:44 +01:00
$sql .= " WHERE v.fk_product_attribute = " . (( int ) $prodattr_id );
2017-11-26 15:08:00 +01:00
2016-07-23 16:37:21 +02:00
if ( $only_used ) {
2024-11-10 18:15:04 +01:00
$sql .= " AND EXISTS (SELECT c2v.fk_prod_attr_val " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_attribute_combination2val c2v " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product_attribute_combination c ON c.rowid = c2v.fk_prod_combination " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product p ON p.rowid = c.fk_product_child " ;
$sql .= " WHERE c2v.rowid IS NOT NULL AND p.tosell = 1 AND c2v.fk_prod_attr_val = v.rowid) " ;
2016-07-23 16:37:21 +02:00
}
2023-11-03 16:32:31 +01:00
$sql .= " ORDER BY v.position ASC " ;
2016-07-23 16:37:21 +02:00
$query = $this -> db -> query ( $sql );
2024-11-10 18:15:04 +01:00
if ( $query ) {
while ( $result = $this -> db -> fetch_object ( $query )) {
if ( empty ( $returnonlydata )) {
$tmp = new ProductAttributeValue ( $this -> db );
} else {
$tmp = new stdClass ();
}
2022-10-08 07:49:02 +02:00
2024-11-10 18:15:04 +01:00
$tmp -> fk_product_attribute = $result -> fk_product_attribute ;
$tmp -> id = $result -> rowid ;
$tmp -> ref = $result -> ref ;
$tmp -> value = $result -> value ;
2016-07-23 16:37:21 +02:00
2024-11-10 18:15:04 +01:00
$return [] = $tmp ;
}
2016-07-23 16:37:21 +02:00
}
return $return ;
}
/**
2022-03-18 15:38:44 +01:00
* Updates a product attribute value
2016-07-23 16:37:21 +02:00
*
2022-03-18 15:38:44 +01:00
* @ param User $user Object user
2020-09-07 10:18:17 +02:00
* @ param int $notrigger Do not execute trigger
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2016-07-23 16:37:21 +02:00
*/
2022-03-18 15:38:44 +01:00
public function update ( User $user , $notrigger = 0 )
2016-07-23 16:37:21 +02:00
{
2022-03-18 15:38:44 +01:00
global $langs ;
$error = 0 ;
// Clean parameters
$this -> fk_product_attribute = $this -> fk_product_attribute > 0 ? $this -> fk_product_attribute : 0 ;
$this -> ref = strtoupper ( dol_sanitizeFileName ( dol_string_nospecial ( trim ( $this -> ref )))); // Ref must be uppercase
$this -> value = trim ( $this -> value );
// Check parameters
if ( empty ( $this -> fk_product_attribute )) {
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " ProductAttribute " ));
$error ++ ;
}
if ( empty ( $this -> ref )) {
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Ref " ));
$error ++ ;
}
if ( empty ( $this -> value )) {
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Value " ));
$error ++ ;
}
if ( $error ) {
dol_syslog ( __METHOD__ . ' ' . $this -> errorsToString (), LOG_ERR );
2016-07-23 16:37:21 +02:00
return - 1 ;
}
2022-03-18 15:38:44 +01:00
$this -> db -> begin ();
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " SET " ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
$sql .= " fk_product_attribute = " . (( int ) $this -> fk_product_attribute );
$sql .= " , ref = ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
$sql .= " , value = ' " . $this -> db -> escape ( $this -> value ) . " ' " ;
$sql .= " , position = " . (( int ) $this -> position );
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-08-08 22:58:51 +02:00
2022-03-18 15:38:44 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> errors [] = " Error " . $this -> db -> lasterror ();
$error ++ ;
2016-07-23 16:37:21 +02:00
}
2022-03-18 15:38:44 +01:00
if ( ! $error && ! $notrigger ) {
2020-09-07 10:18:17 +02:00
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_ATTRIBUTE_VALUE_MODIFY' , $user );
if ( $result < 0 ) {
2022-03-18 15:38:44 +01:00
$error ++ ;
2020-09-07 10:18:17 +02:00
}
// End call triggers
}
2020-08-08 22:58:51 +02:00
2022-03-18 15:38:44 +01:00
if ( ! $error ) {
$this -> db -> commit ();
2016-07-23 16:37:21 +02:00
return 1 ;
2022-03-18 15:38:44 +01:00
} else {
$this -> db -> rollback ();
return - 1 * $error ;
2016-07-23 16:37:21 +02:00
}
}
/**
* Deletes a product attribute value
*
2020-08-08 22:08:46 +02:00
* @ param User $user Object user
2020-09-07 10:18:17 +02:00
* @ param int $notrigger Do not execute trigger
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 KO , > 0 OK
2016-07-23 16:37:21 +02:00
*/
2020-08-08 22:08:46 +02:00
public function delete ( User $user , $notrigger = 0 )
2016-07-23 16:37:21 +02:00
{
2022-03-18 15:38:44 +01:00
global $langs ;
$error = 0 ;
// Clean parameters
$this -> id = $this -> id > 0 ? $this -> id : 0 ;
// Check parameters
if ( empty ( $this -> id )) {
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " TechnicalID " ));
$error ++ ;
}
if ( $error ) {
dol_syslog ( __METHOD__ . ' ' . $this -> errorsToString (), LOG_ERR );
return - 1 ;
}
2020-08-08 22:58:51 +02:00
2022-03-18 15:38:44 +01:00
$result = $this -> isUsed ();
if ( $result < 0 ) {
return - 1 ;
} elseif ( $result > 0 ) {
$this -> errors [] = $langs -> trans ( 'ErrorAttributeValueIsUsedIntoProduct' );
return - 1 ;
}
$this -> db -> begin ();
if ( ! $error && ! $notrigger ) {
2020-09-07 10:18:17 +02:00
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_ATTRIBUTE_VALUE_DELETE' , $user );
if ( $result < 0 ) {
2022-03-18 15:38:44 +01:00
$error ++ ;
2020-09-07 10:18:17 +02:00
}
// End call triggers
}
2022-03-18 15:38:44 +01:00
if ( ! $error ) {
$sql = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element . " WHERE rowid = " . (( int ) $this -> id );
dol_syslog ( __METHOD__ , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> errors [] = " Error " . $this -> db -> lasterror ();
$error ++ ;
}
2020-09-07 10:18:17 +02:00
}
2024-10-13 21:08:55 +02:00
if ( ! $error ) {
$result = $this -> insertExtraFields ();
if ( $result < 0 ) {
$error ++ ;
}
}
2022-03-18 15:38:44 +01:00
if ( ! $error ) {
$this -> db -> commit ();
return 1 ;
} else {
$this -> db -> rollback ();
return - 1 * $error ;
}
2016-07-23 16:37:21 +02:00
}
2024-10-13 21:08:55 +02:00
/**
* Get max value used for position of attributes
* @ param int $fk_product_attribute Id of attribute
*
* @ return int Max value of position in table of attributes
*/
public function getMaxAttributesValuesPosition ( $fk_product_attribute )
{
// Search the last position of attributes
$sql = " SELECT max(position) FROM " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " WHERE entity IN ( " . getEntity ( 'product' ) . " ) " ;
$sql .= ' AND fk_product_attribute=' . ( int ) $fk_product_attribute ;
dol_syslog ( __METHOD__ , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
return 0 ;
}
2016-07-23 16:37:21 +02:00
/**
2022-03-18 15:38:44 +01:00
* Test if used by a product
2016-07-23 16:37:21 +02:00
*
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 KO , = 0 if No , = 1 if Yes
2016-07-23 16:37:21 +02:00
*/
2022-03-18 15:38:44 +01:00
public function isUsed ()
2016-07-23 16:37:21 +02:00
{
2022-03-18 15:38:44 +01:00
global $langs ;
$error = 0 ;
2020-09-07 10:18:17 +02:00
2022-03-18 15:38:44 +01:00
// Clean parameters
$this -> id = $this -> id > 0 ? $this -> id : 0 ;
2020-09-07 10:18:17 +02:00
2022-03-18 15:38:44 +01:00
// Check parameters
if ( empty ( $this -> id )) {
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " TechnicalID " ));
$error ++ ;
}
if ( $error ) {
dol_syslog ( __METHOD__ . ' ' . $this -> errorsToString (), LOG_ERR );
2020-09-07 10:18:17 +02:00
return - 1 ;
}
2022-03-18 15:38:44 +01:00
$sql = " SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX . " product_attribute_combination2val WHERE fk_prod_attr_val = " . (( int ) $this -> id );
dol_syslog ( __METHOD__ , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> errors [] = " Error " . $this -> db -> lasterror ();
return - 1 ;
2020-09-07 10:18:17 +02:00
}
2022-03-18 15:38:44 +01:00
$used = 0 ;
if ( $obj = $this -> db -> fetch_object ( $resql )) {
$used = $obj -> nb ;
2020-09-07 10:18:17 +02:00
}
2022-03-18 15:38:44 +01:00
return $used ? 1 : 0 ;
2016-07-23 16:37:21 +02:00
}
2019-02-03 15:21:21 +01:00
}