Skip to content

Commit

Permalink
Merge pull request #98 from ATM-Consulting/FIX_notrigger
Browse files Browse the repository at this point in the history
FIX: SeedObject: handle notrigger everywhere
  • Loading branch information
John BOTELLA authored Feb 3, 2020
2 parents 4abb9b1 + fa034bd commit f321955
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions includes/class/class.seedobject.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,15 @@ private function checkFieldType($field, $type)
}

/**
* @param User $user object
* @return int
* @param User $user object
* @param bool $notrigger false=launch triggers after, true=disable triggers
* @return int
*/
public function cloneObject($user)
public function cloneObject($user, $notrigger = false)
{
$this->clear();

return $this->create($user);
return $this->create($user, $notrigger);
}

/**
Expand Down Expand Up @@ -847,9 +848,10 @@ public function fetchChild()
/**
* Function to update children data
*
* @param User $user user object
* @param User $user user object
* @param bool $notrigger false=launch triggers after, true=disable triggers
*/
public function saveChild(User &$user)
public function saveChild(User &$user, $notrigger = false)
{
if($this->withChild && !empty($this->childtables) && !empty($this->fk_element))
{
Expand All @@ -863,7 +865,7 @@ public function saveChild(User &$user)
{
$object->{$this->fk_element} = $this->id;

$object->update($user);
$object->update($user, $notrigger);
if($this->unsetChildDeleted && isset($object->to_delete) && $object->to_delete==true) unset($this->{'T'.$className}[$i]);
}
}
Expand All @@ -875,21 +877,22 @@ public function saveChild(User &$user)
/**
* Function to update object or create or delete if needed
*
* @param User $user user object
* @return int < 0 if ko, > 0 if ok
* @param User $user user object
* @param bool $notrigger false=launch triggers after, true=disable triggers
* @return int < 0 if ko, > 0 if ok
*/
public function update(User &$user)
public function update(User &$user, $notrigger = false)
{
if (empty($this->id)) return $this->create($user); // To test, with that, no need to test on high level object, the core decide it, update just needed
elseif (isset($this->to_delete) && $this->to_delete==true) return $this->delete($user);
if (empty($this->id)) return $this->create($user, $notrigger); // To test, with that, no need to test on high level object, the core decide it, update just needed
elseif (isset($this->to_delete) && $this->to_delete==true) return $this->delete($user, $notrigger);

$error = 0;
$this->db->begin();

$res = $this->updateCommon($user);
$res = $this->updateCommon($user, $notrigger);
if ($res)
{
$this->saveChild($user);
$this->saveChild($user, $notrigger);
}
else
{
Expand All @@ -914,21 +917,22 @@ public function update(User &$user)
/**
* Function to create object in database
*
* @param User $user user object
* @return int < 0 if ko, > 0 if ok
* @param User $user user object
* @param bool $notrigger false=launch triggers after, true=disable triggers
* @return int < 0 if ko, > 0 if ok
*/
public function create(User &$user)
public function create(User &$user, $notrigger = false)
{
if($this->id > 0) return $this->update($user);
if($this->id > 0) return $this->update($user, $notrigger);

$error = 0;
$this->db->begin();

$res = $this->createCommon($user);
$res = $this->createCommon($user, $notrigger);
if($res)
{

$this->saveChild($user);
$this->saveChild($user, $notrigger);
}
else
{
Expand All @@ -952,17 +956,18 @@ public function create(User &$user)
/**
* Function to delete object in database
*
* @param User $user user object
* @return int < 0 if ko, > 0 if ok
* @param User $user user object
* @param bool $notrigger false=launch triggers after, true=disable triggers
* @return int < 0 if ko, > 0 if ok
*/
public function delete(User &$user)
public function delete(User &$user, $notrigger = false)
{
if ($this->id <= 0) return 0;

$error = 0;
$this->db->begin();

if ($this->deleteCommon($user)>0)
if ($this->deleteCommon($user, $notrigger)>0)
{
if($this->withChild && !empty($this->childtables))
{
Expand All @@ -974,7 +979,7 @@ public function delete(User &$user)
{
foreach($this->{'T'.$className} as &$object)
{
$object->delete($user);
$object->delete($user, $notrigger);
}
}
}
Expand Down Expand Up @@ -1117,10 +1122,10 @@ public function createCommon(User $user, $notrigger = false)
}
}

if (! $error && ! $notrigger) {
if (! $error) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);

if (!$notrigger) {
if (! $notrigger) {
// Call triggers
$result=$this->call_trigger(strtoupper(get_class($this)).'_CREATE',$user);
if ($result < 0) { $error++; }
Expand Down

0 comments on commit f321955

Please sign in to comment.