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/>.
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
/**
* Class ProductAttributeValue
* Used to represent a product attribute value
*/
class ProductAttributeValue
class ProductAttributeValue extends CommonObject
{
/**
* Database handler
* @var DoliDB
*/
private $db;
public $db;
/**
* Attribute value id
@ -144,14 +145,24 @@ class ProductAttributeValue
/**
* Creates a value for a product attribute
*
* @param User $user Object user
* @return int <0 KO >0 OK
* @param User $user Object user
* @param int $notrigger Do not execute trigger
* @return int <0 KO >0 OK
*/
public function create(User $user)
public function create(User $user, $notrigger = 0)
{
if (!$this->fk_product_attribute) {
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
$this->ref = strtoupper($this->ref);
@ -173,11 +184,21 @@ class ProductAttributeValue
/**
* Updates a product attribute value
*
* @param User $user Object user
* @return int <0 if KO, >0 if OK
* @param User $user Object user
* @param int $notrigger Do not execute trigger
* @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
$this->ref = trim(strtoupper($this->ref));
$this->value = trim($this->value);
@ -196,33 +217,63 @@ class ProductAttributeValue
/**
* Deletes a product attribute value
*
* @param User $user Object user
* @param int $notrigger Do not execute trigger
* @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 ($this->db->query($sql)) {
return 1;
}
return -1;
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;
}
/**
* Deletes all product attribute values by a product attribute id
*
* @param int $fk_attribute Product attribute id
* @param int $notrigger Do not execute trigger
* @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;
if ($this->db->query($sql)) {
return 1;
}
return -1;
$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;
}
}