2016-07-23 16:37:21 +02:00
< ? php
/* Copyright ( C ) 2016 Marcos García < marcosgdf @ gmail . com >
*
* 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
*/
2020-08-08 22:08:46 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2016-07-25 10:37:39 +02:00
/**
* Class ProductAttributeValue
* Used to represent a product attribute value
*/
2020-08-08 22:08:46 +02:00
class ProductAttributeValue extends CommonObject
2016-07-23 16:37:21 +02:00
{
/**
* Database handler
* @ var DoliDB
*/
2020-08-08 22:08:46 +02:00
public $db ;
2016-07-23 16:37:21 +02:00
/**
* Attribute value id
* @ var int
*/
public $id ;
/**
* Product attribute id
* @ var int
*/
public $fk_product_attribute ;
/**
* Attribute value ref
* @ var string
*/
public $ref ;
/**
* Attribute value value
* @ var string
*/
public $value ;
2020-09-07 10:18:17 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( DoliDB $db )
{
2016-07-23 16:37:21 +02:00
global $conf ;
$this -> db = $db ;
$this -> entity = $conf -> entity ;
2020-09-07 10:18:17 +02:00
}
2016-07-23 16:37:21 +02:00
/**
* Gets a product attribute value
*
* @ param int $valueid Product attribute value id
* @ return int < 0 KO , > 0 OK
*/
public function fetch ( $valueid )
{
2017-05-30 18:50:54 +02:00
$sql = " SELECT rowid, fk_product_attribute, ref, value FROM " . MAIN_DB_PREFIX . " product_attribute_value WHERE rowid = " . ( int ) $valueid . " AND entity IN ( " . getEntity ( 'product' ) . " ) " ;
2016-07-23 16:37:21 +02:00
$query = $this -> db -> query ( $sql );
if ( ! $query ) {
return - 1 ;
}
if ( ! $this -> db -> num_rows ( $query )) {
return - 1 ;
}
2019-11-12 10:11:30 +01:00
$obj = $this -> db -> fetch_object ( $query );
2016-07-23 16:37:21 +02:00
2019-11-12 10:11:30 +01:00
$this -> id = $obj -> rowid ;
$this -> fk_product_attribute = $obj -> fk_product_attribute ;
$this -> ref = $obj -> ref ;
$this -> value = $obj -> value ;
2016-07-23 16:37:21 +02:00
return 1 ;
}
/**
* Returns all product attribute values of a product attribute
*
* @ param int $prodattr_id Product attribute id
* @ param bool $only_used Fetch only used attribute values
* @ return ProductAttributeValue []
*/
public function fetchAllByProductAttribute ( $prodattr_id , $only_used = false )
{
$return = array ();
$sql = 'SELECT ' ;
2017-11-26 15:08:00 +01:00
2016-07-23 16:37:21 +02:00
if ( $only_used ) {
$sql .= 'DISTINCT ' ;
}
2017-11-26 15:08:00 +01:00
2016-07-23 16:37:21 +02:00
$sql .= 'v.fk_product_attribute, v.rowid, v.ref, v.value FROM ' . MAIN_DB_PREFIX . 'product_attribute_value v ' ;
if ( $only_used ) {
$sql .= 'LEFT JOIN ' . MAIN_DB_PREFIX . 'product_attribute_combination2val c2v ON c2v.fk_prod_attr_val = v.rowid ' ;
$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 ' ;
}
2017-11-26 15:08:00 +01:00
2016-07-23 16:37:21 +02: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 ) {
$sql .= ' AND c2v.rowid IS NOT NULL AND p.tosell = 1' ;
}
$query = $this -> db -> query ( $sql );
while ( $result = $this -> db -> fetch_object ( $query )) {
$tmp = new ProductAttributeValue ( $this -> db );
$tmp -> fk_product_attribute = $result -> fk_product_attribute ;
$tmp -> id = $result -> rowid ;
$tmp -> ref = $result -> ref ;
$tmp -> value = $result -> value ;
$return [] = $tmp ;
}
return $return ;
}
/**
* Creates a value for a product attribute
*
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
2020-08-08 22:08:46 +02:00
* @ return int < 0 KO > 0 OK
2016-07-23 16:37:21 +02:00
*/
2020-08-08 22:08:46 +02:00
public function create ( User $user , $notrigger = 0 )
2016-07-23 16:37:21 +02:00
{
if ( ! $this -> fk_product_attribute ) {
return - 1 ;
}
2018-03-10 12:37:44 +01:00
// Ref must be uppercase
2016-07-23 16:37:21 +02:00
$this -> ref = strtoupper ( $this -> ref );
2020-09-07 10:18:17 +02:00
$this -> value = $this -> db -> escape ( $this -> value );
2016-07-23 16:37:21 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " product_attribute_value (fk_product_attribute, ref, value, entity)
VALUES ( '".(int) $this->fk_product_attribute."' , '".$this->db->escape($this->ref)."' ,
2020-08-08 22:45:51 +02:00
'".$this->value."' , " .(int) $this->entity . " ) " ;
2016-07-23 16:37:21 +02:00
$query = $this -> db -> query ( $sql );
if ( $query ) {
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . 'product_attribute_value' );
2020-09-07 10:18:17 +02:00
if ( empty ( $notrigger )) {
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_ATTRIBUTE_VALUE_CREATE' , $user );
if ( $result < 0 ) {
return - 1 ;
}
// End call triggers
}
2020-08-08 22:58:51 +02:00
2016-07-23 16:37:21 +02:00
return 1 ;
}
return - 1 ;
}
/**
* Updates 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
2020-08-08 22:08:46 +02:00
* @ return int < 0 if KO , > 0 if OK
2016-07-23 16:37:21 +02:00
*/
2020-08-08 22:08:46 +02:00
public function update ( User $user , $notrigger = 0 )
2016-07-23 16:37:21 +02:00
{
2020-09-07 10:18:17 +02:00
if ( empty ( $notrigger )) {
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_ATTRIBUTE_VALUE_MODIFY' , $user );
if ( $result < 0 ) {
return - 1 ;
}
// End call triggers
}
2020-08-08 22:58:51 +02:00
2016-07-23 16:37:21 +02:00
//Ref must be uppercase
2017-11-26 15:08:00 +01:00
$this -> ref = trim ( strtoupper ( $this -> ref ));
$this -> value = trim ( $this -> value );
2016-07-23 16:37:21 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " product_attribute_value
SET fk_product_attribute = '".(int) $this->fk_product_attribute."' , ref = '".$this->db->escape($this->ref)."' ,
value = '".$this->db->escape($this->value)."' WHERE rowid = " .(int) $this->id ;
if ( $this -> db -> query ( $sql )) {
return 1 ;
}
return - 1 ;
}
/**
* 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
2016-07-23 16:37:21 +02:00
* @ return int < 0 KO , > 0 OK
*/
2020-08-08 22:08:46 +02:00
public function delete ( User $user , $notrigger = 0 )
2016-07-23 16:37:21 +02:00
{
2020-08-08 22:58:51 +02:00
2020-09-07 10:18:17 +02:00
if ( empty ( $notrigger )) {
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_ATTRIBUTE_VALUE_DELETE' , $user );
if ( $result < 0 ) {
return - 1 ;
}
// End call triggers
}
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " product_attribute_value WHERE rowid = " . ( int ) $this -> id ;
if ( $this -> db -> query ( $sql )) {
return 1 ;
}
return - 1 ;
2016-07-23 16:37:21 +02:00
}
/**
* Deletes all product attribute values by a product attribute id
*
2020-08-08 23:01:41 +02:00
* @ param int $fk_attribute Product attribute id
* @ param User $user Object user
2016-07-23 16:37:21 +02:00
* @ return int < 0 KO , > 0 OK
*/
2020-08-08 22:08:46 +02:00
public function deleteByFkAttribute ( $fk_attribute , User $user )
2016-07-23 16:37:21 +02:00
{
2020-09-07 10:18:17 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " product_attribute_value WHERE fk_product_attribute = " . ( int ) $fk_attribute ;
$query = $this -> db -> query ( $sql );
if ( ! $query ) {
return - 1 ;
}
if ( ! $this -> db -> num_rows ( $query )) {
return 1 ;
}
while ( $obj = $this -> db -> fetch_object ( $query )) {
$tmp = new ProductAttributeValue ( $this -> db );
if ( $tmp -> fetch ( $obj -> rowid ) > 0 ) {
$result = $tmp -> delete ( $user );
if ( $result < 0 ) {
return - 1 ;
}
} else {
return - 1 ;
}
}
return 1 ;
2016-07-23 16:37:21 +02:00
}
2019-02-03 15:21:21 +01:00
}