NEW triggers create, modify, delete

Also modified deleteByFkAttribute() to use the function delete()
This commit is contained in:
Cédric 2020-08-08 22:08:46 +02:00 committed by GitHub
parent a53f7ff9c9
commit bb16564b98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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,14 +146,24 @@ 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;
} }
if (empty($notrigger)) {
// Call trigger
$result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_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);
@ -174,10 +185,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,10 +217,21 @@ 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)
{ {
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; $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $this->id;
if ($this->db->query($sql)) { if ($this->db->query($sql)) {
@ -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 int $notrigger Do not execute trigger
* @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; return -1;
} }
} else {
return -1;
}
}
return 1;
}
} }