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:10:50 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2016-07-25 10:37:39 +02:00
/**
* Class ProductAttribute
* Used to represent a product attribute
*/
2020-08-08 22:10:50 +02:00
class ProductAttribute extends CommonObject
2016-07-23 16:37:21 +02:00
{
/**
* Database handler
* @ var DoliDB
*/
2020-08-08 22:10:50 +02:00
public $db ;
2016-07-23 16:37:21 +02:00
/**
* Id of the product attribute
* @ var int
*/
public $id ;
/**
* Ref of the product attribute
2019-12-15 10:02:20 +01:00
* @ var string
2016-07-23 16:37:21 +02:00
*/
public $ref ;
2020-09-04 12:10:37 +02:00
2020-09-04 12:06:09 +02:00
/**
* External ref of the product attribute
* @ var string
*/
public $ref_ext ;
2016-07-23 16:37:21 +02:00
/**
* Label of the product attribute
* @ var string
*/
public $label ;
/**
* Order of attribute .
* Lower ones will be shown first and higher ones last
* @ var int
*/
public $rang ;
2020-09-07 10:18:17 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2016-07-23 16:37:21 +02:00
public function __construct ( DoliDB $db )
{
global $conf ;
$this -> db = $db ;
$this -> entity = $conf -> entity ;
}
/**
* Fetches the properties of a product attribute
*
* @ param int $id Attribute id
* @ return int < 1 KO , > 1 OK
*/
public function fetch ( $id )
{
if ( ! $id ) {
return - 1 ;
}
2021-03-22 11:30:18 +01:00
$sql = " SELECT rowid, ref, ref_ext, label, rang FROM " . MAIN_DB_PREFIX . " product_attribute WHERE rowid = " . (( int ) $id ) . " AND entity IN ( " . getEntity ( 'product' ) . " ) " ;
2020-09-04 12:10:37 +02:00
2016-07-23 16:37:21 +02:00
$query = $this -> db -> query ( $sql );
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 -> ref = $obj -> ref ;
2020-09-04 12:06:09 +02:00
$this -> ref_ext = $obj -> ref_ext ;
2019-11-12 10:11:30 +01:00
$this -> label = $obj -> label ;
$this -> rang = $obj -> rang ;
2016-07-23 16:37:21 +02:00
return 1 ;
}
/**
2017-02-08 12:37:38 +01:00
* Returns an array of all product variants
2016-07-23 16:37:21 +02:00
*
* @ return ProductAttribute []
*/
public function fetchAll ()
{
$return = array ();
2020-09-04 12:06:09 +02:00
$sql = 'SELECT rowid, ref, ref_ext, label, rang FROM ' . MAIN_DB_PREFIX . " product_attribute WHERE entity IN ( " . getEntity ( 'product' ) . ')' ;
2016-07-23 16:37:21 +02:00
$sql .= $this -> db -> order ( 'rang' , 'asc' );
$query = $this -> db -> query ( $sql );
2021-02-26 13:10:10 +01:00
if ( $query ) {
2020-09-07 17:24:35 +02:00
while ( $result = $this -> db -> fetch_object ( $query )) {
$tmp = new ProductAttribute ( $this -> db );
$tmp -> id = $result -> rowid ;
$tmp -> ref = $result -> ref ;
$tmp -> ref_ext = $result -> ref_ext ;
$tmp -> label = $result -> label ;
$tmp -> rang = $result -> rang ;
$return [] = $tmp ;
}
2021-02-26 13:10:10 +01:00
} else {
dol_print_error ( $this -> db );
2020-08-08 22:10:50 +02:00
}
2017-11-26 10:13:32 +01:00
2016-07-23 16:37:21 +02:00
return $return ;
}
/**
* Creates a product attribute
*
2020-08-08 22:10:50 +02:00
* @ param User $user Object user
2020-09-07 10:18:17 +02:00
* @ param int $notrigger Do not execute trigger
2017-11-26 10:13:32 +01:00
* @ return int < 0 KO , Id of new variant if OK
2016-07-23 16:37:21 +02:00
*/
2020-08-08 22:10:50 +02:00
public function create ( 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_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
//Ref must be uppercase
$this -> ref = strtoupper ( $this -> ref );
2020-09-04 12:06:09 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " product_attribute (ref, ref_ext, label, entity, rang)
VALUES ( '".$this->db->escape($this->ref)."' , '".$this->db->escape($this->ref_ext)."' , '".$this->db->escape($this->label)."' , " .(int) $this->entity . " , " .(int) $this->rang . " ) " ;
2020-09-04 12:10:37 +02:00
2017-11-26 10:13:32 +01:00
$query = $this -> db -> query ( $sql );
2021-02-26 13:10:10 +01:00
if ( $query ) {
2016-07-23 16:37:21 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . 'product_attribute' );
2017-11-26 10:13:32 +01:00
return $this -> id ;
2016-07-23 16:37:21 +02:00
}
return - 1 ;
}
/**
* Updates a product attribute
*
2020-08-08 22:10:50 +02:00
* @ param User $user Object user
2020-09-07 10:18:17 +02:00
* @ param int $notrigger Do not execute trigger
2017-11-26 15:08:00 +01:00
* @ return int < 0 KO , > 0 OK
2016-07-23 16:37:21 +02:00
*/
2020-08-08 22:10:50 +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_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 -> label = trim ( $this -> label );
2016-07-23 16:37:21 +02:00
2020-09-04 12:06:09 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " product_attribute SET ref = ' " . $this -> db -> escape ( $this -> ref ) . " ', ref_ext = ' " . $this -> db -> escape ( $this -> ref_ext ) . " ', label = ' " . $this -> db -> escape ( $this -> label ) . " ', rang = " . ( int ) $this -> rang . " WHERE rowid = " . ( int ) $this -> id ;
2020-09-04 12:10:37 +02:00
2016-07-23 16:37:21 +02:00
if ( $this -> db -> query ( $sql )) {
return 1 ;
}
return - 1 ;
}
/**
* Deletes a product attribute
*
2020-08-08 22:10:50 +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:10:50 +02:00
* @ return int < 0 KO , > 0 OK
2016-07-23 16:37:21 +02:00
*/
2020-08-08 22:10:50 +02:00
public function delete ( 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_DELETE' , $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
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " product_attribute WHERE rowid = " . ( int ) $this -> id ;
if ( $this -> db -> query ( $sql )) {
return 1 ;
}
return - 1 ;
}
2017-02-08 14:07:54 +01:00
/**
* Returns the number of values for this attribute
*
* @ return int
*/
public function countChildValues ()
{
$sql = " SELECT COUNT(*) count FROM " . MAIN_DB_PREFIX . " product_attribute_value WHERE fk_product_attribute = " . ( int ) $this -> id ;
$query = $this -> db -> query ( $sql );
$result = $this -> db -> fetch_object ( $query );
return $result -> count ;
}
2017-11-26 10:13:32 +01:00
2016-07-23 16:37:21 +02:00
/**
* Returns the number of products that are using this attribute
*
* @ return int
*/
public function countChildProducts ()
{
$sql = " SELECT COUNT(*) count FROM " . MAIN_DB_PREFIX . " product_attribute_combination2val pac2v
2021-03-22 11:30:18 +01:00
LEFT JOIN " .MAIN_DB_PREFIX. " product_attribute_combination pac ON pac2v . fk_prod_combination = pac . rowid WHERE pac2v . fk_prod_attr = " .((int) $this->id ). " AND pac . entity IN ( " .getEntity('product'). " ) " ;
2016-07-23 16:37:21 +02:00
$query = $this -> db -> query ( $sql );
$result = $this -> db -> fetch_object ( $query );
return $result -> count ;
}
2017-11-26 10:13:32 +01:00
2016-07-23 16:37:21 +02:00
/**
2017-02-08 12:37:38 +01:00
* Reorders the order of the variants .
2016-07-23 16:37:21 +02:00
* This is an internal function used by moveLine function
*
* @ return int < 0 KO > 0 OK
*/
protected function reorderLines ()
{
2017-11-26 18:43:43 +01:00
global $user ;
2016-07-23 16:37:21 +02:00
$tmp_order = array ();
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'product_attribute WHERE rang = 0' ;
$sql .= $this -> db -> order ( 'rang, rowid' , 'asc' );
$query = $this -> db -> query ( $sql );
if ( ! $query ) {
return - 1 ;
}
while ( $result = $this -> db -> fetch_object ( $query )) {
$tmp_order [] = $result -> rowid ;
}
foreach ( $tmp_order as $order => $rowid ) {
$tmp = new ProductAttribute ( $this -> db );
$tmp -> fetch ( $rowid );
2020-04-10 10:59:32 +02:00
$tmp -> rang = $order + 1 ;
2016-07-23 16:37:21 +02:00
2017-11-26 18:43:43 +01:00
if ( $tmp -> update ( $user ) < 0 ) {
2016-07-23 16:37:21 +02:00
return - 1 ;
}
}
return 1 ;
}
/**
* Internal function to handle moveUp and moveDown functions
*
* @ param string $type up / down
* @ return int < 0 KO > 0 OK
*/
private function moveLine ( $type )
{
2017-11-26 18:43:43 +01:00
global $user ;
2016-07-23 16:37:21 +02:00
if ( $this -> reorderLines () < 0 ) {
return - 1 ;
}
$this -> db -> begin ();
if ( $type == 'up' ) {
$newrang = $this -> rang - 1 ;
} else {
$newrang = $this -> rang + 1 ;
}
2021-03-30 11:36:50 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'product_attribute SET rang = ' . (( int ) $this -> rang ) . ' WHERE rang = ' . (( int ) $newrang );
2016-07-23 16:37:21 +02:00
if ( ! $this -> db -> query ( $sql )) {
$this -> db -> rollback ();
return - 1 ;
}
$this -> rang = $newrang ;
2017-11-26 18:43:43 +01:00
if ( $this -> update ( $user ) < 0 ) {
2016-07-23 16:37:21 +02:00
$this -> db -> rollback ();
return - 1 ;
}
$this -> db -> commit ();
return 1 ;
}
/**
* Shows this attribute before others
*
* @ return int < 0 KO > 0 OK
*/
public function moveUp ()
{
return $this -> moveLine ( 'up' );
}
/**
* Shows this attribute after others
2017-11-26 10:13:32 +01:00
*
2016-07-23 16:37:21 +02:00
* @ return int < 0 KO > 0 OK
*/
public function moveDown ()
{
return $this -> moveLine ( 'down' );
}
/**
2017-02-08 12:37:38 +01:00
* Updates the order of all variants . Used by AJAX page for drag & drop
2016-07-23 16:37:21 +02:00
*
* @ param DoliDB $db Database handler
* @ param array $order Array with row id ordered in ascendent mode
* @ return int < 0 KO > 0 OK
*/
public static function bulkUpdateOrder ( DoliDB $db , array $order )
{
2017-11-26 18:43:43 +01:00
global $user ;
2016-07-23 16:37:21 +02:00
$tmp = new ProductAttribute ( $db );
foreach ( $order as $key => $attrid ) {
if ( $tmp -> fetch ( $attrid ) < 0 ) {
return - 1 ;
}
$tmp -> rang = $key ;
2017-11-26 18:43:43 +01:00
if ( $tmp -> update ( $user ) < 0 ) {
2016-07-23 16:37:21 +02:00
return - 1 ;
}
}
return 1 ;
}
2019-02-03 15:21:21 +01:00
}