NEW triggers on mailing

This commit is contained in:
VESSILLER 2022-11-16 11:45:42 +01:00
parent 9921ce8edc
commit 3b3ebfa1c2

View File

@ -217,9 +217,10 @@ class Mailing extends CommonObject
* Create an EMailing * Create an EMailing
* *
* @param User $user Object of user making creation * @param User $user Object of user making creation
* @return int -1 if error, Id of created object if OK * @param int $notrigger Disable triggers
* @return int <0 if KO, Id of created object if OK
*/ */
public function create($user) public function create($user, $notrigger = 0)
{ {
global $conf, $langs; global $conf, $langs;
@ -229,8 +230,6 @@ class Mailing extends CommonObject
return -1; return -1;
} }
$this->db->begin();
$this->title = trim($this->title); $this->title = trim($this->title);
$this->email_from = trim($this->email_from); $this->email_from = trim($this->email_from);
@ -239,7 +238,9 @@ class Mailing extends CommonObject
return -1; return -1;
} }
$error = 0;
$now = dol_now(); $now = dol_now();
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing";
$sql .= " (date_creat, fk_user_creat, entity)"; $sql .= " (date_creat, fk_user_creat, entity)";
@ -250,19 +251,32 @@ class Mailing extends CommonObject
} }
dol_syslog("Mailing::Create", LOG_DEBUG); dol_syslog("Mailing::Create", LOG_DEBUG);
$result = $this->db->query($sql); $resql = $this->db->query($sql);
if ($result) { if ($resql) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mailing"); $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mailing");
if ($this->update($user) > 0) { $result = $this->update($user, 1);
$this->db->commit(); if ($result < 0) {
} else { $error++;
$this->error = $this->db->lasterror();
$this->db->rollback();
return -1;
} }
if (!$error && !$notrigger) {
// Call trigger
$result = $this->call_trigger('MAILING_CREATE', $user);
if ($result < 0) {
$error++;
}
// End call triggers
}
if (!$error) {
$this->db->commit();
return $this->id; return $this->id;
} else {
$this->db->rollback();
dol_syslog(__METHOD__ . ' ' . $this->error, LOG_ERR);
return -2;
}
} else { } else {
$this->error = $this->db->lasterror(); $this->error = $this->db->lasterror();
$this->db->rollback(); $this->db->rollback();
@ -274,9 +288,10 @@ class Mailing extends CommonObject
* Update emailing record * Update emailing record
* *
* @param User $user Object of user making change * @param User $user Object of user making change
* @param int $notrigger Disable triggers
* @return int < 0 if KO, > 0 if OK * @return int < 0 if KO, > 0 if OK
*/ */
public function update($user) public function update($user, $notrigger = 0)
{ {
// Check properties // Check properties
if ($this->body === 'InvalidHTMLString') { if ($this->body === 'InvalidHTMLString') {
@ -284,6 +299,9 @@ class Mailing extends CommonObject
return -1; return -1;
} }
$error = 0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing "; $sql = "UPDATE ".MAIN_DB_PREFIX."mailing ";
$sql .= " SET titre = '".$this->db->escape($this->title)."'"; $sql .= " SET titre = '".$this->db->escape($this->title)."'";
$sql .= ", sujet = '".$this->db->escape($this->sujet)."'"; $sql .= ", sujet = '".$this->db->escape($this->sujet)."'";
@ -295,12 +313,30 @@ class Mailing extends CommonObject
$sql .= ", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) : null)."'"; $sql .= ", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) : null)."'";
$sql .= " WHERE rowid = ".(int) $this->id; $sql .= " WHERE rowid = ".(int) $this->id;
dol_syslog("Mailing::Update", LOG_DEBUG); dol_syslog(__METHOD__, LOG_DEBUG);
$result = $this->db->query($sql); $resql = $this->db->query($sql);
if ($result) { if ($resql) {
if (!$error && !$notrigger) {
// Call trigger
$result = $this->call_trigger('MAILING_MODIFY', $user);
if ($result < 0) {
$error++;
}
// End call triggers
}
if (!$error) {
dol_syslog(__METHOD__ . ' success');
$this->db->commit();
return 1; return 1;
} else {
$this->db->rollback();
dol_syslog(__METHOD__ . ' ' . $this->error, LOG_ERR);
return -2;
}
} else { } else {
$this->error = $this->db->lasterror(); $this->error = $this->db->lasterror();
$this->db->rollback();
return -1; return -1;
} }
} }
@ -528,38 +564,48 @@ class Mailing extends CommonObject
{ {
global $user; global $user;
$error = 0;
$this->db->begin(); $this->db->begin();
if (!$notrigger) {
$result = $this->call_trigger('MAILING_DELETE', $user);
if ($result < 0) {
$error++;
}
}
if (!$error) {
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "mailing"; $sql = "DELETE FROM " . MAIN_DB_PREFIX . "mailing";
$sql .= " WHERE rowid = " . ((int)$rowid); $sql .= " WHERE rowid = " . ((int)$rowid);
dol_syslog("Mailing::delete", LOG_DEBUG); dol_syslog(__METHOD__, LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$res = $this->delete_targets(); $res = $this->delete_targets();
if ($res <= 0) { if ($res <= 0) {
$error++;
}
if (!$error) {
dol_syslog(__METHOD__ . ' success');
$this->db->commit();
return 1;
} else {
$this->db->rollback(); $this->db->rollback();
$this->error = $this->db->lasterror(); dol_syslog(__METHOD__ . ' ' . $this->error, LOG_ERR);
return -1; return -2;
} }
} else { } else {
$this->db->rollback(); $this->db->rollback();
$this->error = $this->db->lasterror(); $this->error = $this->db->lasterror();
return -1; return -1;
} }
} else {
if (!$notrigger) {
$result = $this->call_trigger('MAILING_DELETE', $user);
if ($result < 0) {
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
} }
$this->db->commit();
return 1;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/** /**
* Delete targets emailing * Delete targets emailing