Merge pull request #14433 from c3do/patch-1

NEW Triggers Attributes and Attributes values
This commit is contained in:
Laurent Destailleur 2020-08-16 23:45:32 +02:00 committed by GitHub
commit b7c38f4617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 230 additions and 133 deletions

View File

@ -1161,9 +1161,23 @@ class Products extends DolibarrApi
throw new RestException(401); throw new RestException(401);
} }
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '".trim($ref)."' AND fk_product_attribute = ".(int) $id; $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '".trim($ref)."' AND fk_product_attribute = ".(int) $id." AND entity IN (".getEntity('product').")";
$query = $this->db->query($sql);
if ($this->db->query($sql)) { if (!$query) {
throw new RestException(401);
}
if (!$this->db->num_rows($query)) {
throw new RestException(404, 'Attribute value not found');
}
$result = $this->db->fetch_object($query);
$attrval = new ProductAttributeValue($this->db);
$attrval->id = $result->rowid;
$result = $attrval->delete(DolibarrApiAccess::$user);
if ($result > 0) {
return 1; return 1;
} }
@ -1328,7 +1342,7 @@ class Products extends DolibarrApi
$objectval = new ProductAttributeValue($this->db); $objectval = new ProductAttributeValue($this->db);
$objectval->id = (int) $id; $objectval->id = (int) $id;
if ($objectval->delete() > 0) { if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
return 1; return 1;
} }
throw new RestException(500, "Error deleting attribute value"); throw new RestException(500, "Error deleting attribute value");

View File

@ -90,9 +90,9 @@ if ($confirm == 'yes') {
if ($action == 'confirm_delete') { if ($action == 'confirm_delete') {
$db->begin(); $db->begin();
$res = $objectval->deleteByFkAttribute($object->id); $res = $objectval->deleteByFkAttribute($object->id, $user);
if ($res < 1 || ($object->delete() < 1)) { if ($res < 1 || ($object->delete($user) < 1)) {
$db->rollback(); $db->rollback();
setEventMessages($langs->trans('CoreErrorMessage'), $object->errors, 'errors'); setEventMessages($langs->trans('CoreErrorMessage'), $object->errors, 'errors');
header('Location: '.dol_buildpath('/variants/card.php?id='.$object->id, 2)); header('Location: '.dol_buildpath('/variants/card.php?id='.$object->id, 2));
@ -105,7 +105,7 @@ if ($confirm == 'yes') {
} elseif ($action == 'confirm_deletevalue') } elseif ($action == 'confirm_deletevalue')
{ {
if ($objectval->fetch($valueid) > 0) { if ($objectval->fetch($valueid) > 0) {
if ($objectval->delete() < 1) { if ($objectval->delete($user) < 1) {
setEventMessages($langs->trans('CoreErrorMessage'), $objectval->errors, 'errors'); setEventMessages($langs->trans('CoreErrorMessage'), $objectval->errors, 'errors');
} else { } else {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');

View File

@ -16,17 +16,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
/** /**
* Class ProductAttribute * Class ProductAttribute
* Used to represent a product attribute * Used to represent a product attribute
*/ */
class ProductAttribute class ProductAttribute extends CommonObject
{ {
/** /**
* Database handler * Database handler
* @var DoliDB * @var DoliDB
*/ */
private $db; public $db;
/** /**
* Id of the product attribute * Id of the product attribute
@ -119,7 +120,8 @@ class ProductAttribute
$return[] = $tmp; $return[] = $tmp;
} }
} else dol_print_error($this->db); }
else dol_print_error($this->db);
return $return; return $return;
} }
@ -127,11 +129,21 @@ class ProductAttribute
/** /**
* Creates a product attribute * Creates a product attribute
* *
* @param User $user Object user that create * @param User $user Object user
* @param int $notrigger Do not execute trigger
* @return int <0 KO, Id of new variant if OK * @return int <0 KO, Id of new variant if OK
*/ */
public function create(User $user) public function create(User $user, $notrigger = 0)
{ {
if (empty($notrigger)) {
// Call trigger
$result = $this->call_trigger('PRODUCT_ATTRIBUTE_CREATE', $user);
if ($result < 0) {
return -1;
}
// End call triggers
}
//Ref must be uppercase //Ref must be uppercase
$this->ref = strtoupper($this->ref); $this->ref = strtoupper($this->ref);
@ -153,10 +165,20 @@ class ProductAttribute
* Updates a product attribute * Updates a product attribute
* *
* @param User $user Object user * @param User $user Object user
* @param int $notrigger Do not execute trigger
* @return int <0 KO, >0 OK * @return int <0 KO, >0 OK
*/ */
public function update(User $user) public function update(User $user, $notrigger = 0)
{ {
if (empty($notrigger)) {
// Call trigger
$result = $this->call_trigger('PRODUCT_ATTRIBUTE_MODIFY', $user);
if ($result < 0) {
return -1;
}
// End call triggers
}
//Ref must be uppercase //Ref must be uppercase
$this->ref = trim(strtoupper($this->ref)); $this->ref = trim(strtoupper($this->ref));
$this->label = trim($this->label); $this->label = trim($this->label);
@ -174,10 +196,20 @@ class ProductAttribute
* Deletes a product attribute * Deletes a product attribute
* *
* @param User $user Object user * @param User $user Object user
* @param int $notrigger Do not execute trigger
* @return int <0 KO, >0 OK * @return int <0 KO, >0 OK
*/ */
public function delete($user = null) public function delete(User $user, $notrigger = 0)
{ {
if (empty($notrigger)) {
// Call trigger
$result = $this->call_trigger('PRODUCT_ATTRIBUTE_DELETE', $user);
if ($result < 0) {
return -1;
}
// End call triggers
}
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute WHERE rowid = ".(int) $this->id; $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute WHERE rowid = ".(int) $this->id;
if ($this->db->query($sql)) { if ($this->db->query($sql)) {

View File

@ -16,17 +16,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
/** /**
* Class ProductAttributeValue * Class ProductAttributeValue
* Used to represent a product attribute value * Used to represent a product attribute value
*/ */
class ProductAttributeValue class ProductAttributeValue extends CommonObject
{ {
/** /**
* Database handler * Database handler
* @var DoliDB * @var DoliDB
*/ */
private $db; public $db;
/** /**
* Attribute value id * Attribute value id
@ -145,9 +146,10 @@ class ProductAttributeValue
* Creates a value for a product attribute * Creates a value for a product attribute
* *
* @param User $user Object user * @param User $user Object user
* @param int $notrigger Do not execute trigger
* @return int <0 KO >0 OK * @return int <0 KO >0 OK
*/ */
public function create(User $user) public function create(User $user, $notrigger = 0)
{ {
if (!$this->fk_product_attribute) { if (!$this->fk_product_attribute) {
return -1; return -1;
@ -155,15 +157,25 @@ class ProductAttributeValue
// Ref must be uppercase // Ref must be uppercase
$this->ref = strtoupper($this->ref); $this->ref = strtoupper($this->ref);
$this->value = $this->db->escape($this->value);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_attribute_value (fk_product_attribute, ref, value, entity) $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)."', VALUES ('".(int) $this->fk_product_attribute."', '".$this->db->escape($this->ref)."',
'".$this->db->escape($this->value)."', ".(int) $this->entity.")"; '".$this->value."', ".(int) $this->entity.")";
$query = $this->db->query($sql); $query = $this->db->query($sql);
if ($query) { if ($query) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'product_attribute_value'); $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'product_attribute_value');
if (empty($notrigger)) {
// Call trigger
$result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_CREATE', $user);
if ($result < 0) {
return -1;
}
// End call triggers
}
return 1; return 1;
} }
@ -174,10 +186,20 @@ class ProductAttributeValue
* Updates a product attribute value * Updates a product attribute value
* *
* @param User $user Object user * @param User $user Object user
* @param int $notrigger Do not execute trigger
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function update(User $user) public function update(User $user, $notrigger = 0)
{ {
if (empty($notrigger)) {
// Call trigger
$result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
if ($result < 0) {
return -1;
}
// End call triggers
}
//Ref must be uppercase //Ref must be uppercase
$this->ref = trim(strtoupper($this->ref)); $this->ref = trim(strtoupper($this->ref));
$this->value = trim($this->value); $this->value = trim($this->value);
@ -196,12 +218,22 @@ class ProductAttributeValue
/** /**
* Deletes a product attribute value * Deletes a product attribute value
* *
* @param User $user Object user
* @param int $notrigger Do not execute trigger
* @return int <0 KO, >0 OK * @return int <0 KO, >0 OK
*/ */
public function delete() public function delete(User $user, $notrigger = 0)
{ {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $this->id;
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)) { if ($this->db->query($sql)) {
return 1; return 1;
} }
@ -213,16 +245,35 @@ class ProductAttributeValue
* Deletes all product attribute values by a product attribute id * Deletes all product attribute values by a product attribute id
* *
* @param int $fk_attribute Product attribute id * @param int $fk_attribute Product attribute id
* @param User $user Object user
* @return int <0 KO, >0 OK * @return int <0 KO, >0 OK
*/ */
public function deleteByFkAttribute($fk_attribute) public function deleteByFkAttribute($fk_attribute, User $user)
{ {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE fk_product_attribute = ".(int) $fk_attribute; $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE fk_product_attribute = ".(int) $fk_attribute;
if ($this->db->query($sql)) { $query = $this->db->query($sql);
if (!$query) {
return -1;
}
if (!$this->db->num_rows($query)) {
return 1; 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;
} }
} }
return 1;
}
}