diff --git a/htdocs/comm/action/class/actioncomm.class.php b/htdocs/comm/action/class/actioncomm.class.php index 95f2a3f3494..c0885e4311a 100644 --- a/htdocs/comm/action/class/actioncomm.class.php +++ b/htdocs/comm/action/class/actioncomm.class.php @@ -2087,6 +2087,7 @@ class ActionComm extends CommonObject if ($res > 0) { // PREPARE EMAIL + $errormesg = ''; // Make substitution in email content $substitutionarray = getCommonSubstitutionArray($langs, 0, '', $this); @@ -2102,43 +2103,65 @@ class ActionComm extends CommonObject // Recipient $recipient = new User($this->db); $res = $recipient->fetch($actionCommReminder->fk_user); - if ($res > 0 && !empty($recipient->email)) $to = $recipient->email; - else { - $errorsMsg[] = "Failed to load recipient"; + if ($res > 0) { + if (!empty($recipient->email)) { + $to = $recipient->email; + } else { + $errormesg = "Failed to send remind to user id=".$actionCommReminder->fk_user.". No email defined for user."; + $error++; + } + } else { + $errormesg = "Failed to load recipient with user id=".$actionCommReminder->fk_user; $error++; } // Sender $from = $conf->global->MAIN_MAIL_EMAIL_FROM; if (empty($from)) { - $errorsMsg[] = "Failed to load recipient"; + $errormesg = "Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM"; $error++; } - // Errors Recipient - $errors_to = $conf->global->MAIN_MAIL_ERRORS_TO; + if (!$error) { + // Errors Recipient + $errors_to = $conf->global->MAIN_MAIL_ERRORS_TO; - // Mail Creation - $cMailFile = new CMailFile($sendTopic, $to, $from, $sendContent, array(), array(), array(), '', "", 0, 1, $errors_to, '', '', '', '', ''); + // Mail Creation + $cMailFile = new CMailFile($sendTopic, $to, $from, $sendContent, array(), array(), array(), '', "", 0, 1, $errors_to, '', '', '', '', ''); - // Sending Mail - if ($cMailFile->sendfile()) - { + // Sending Mail + if ($cMailFile->sendfile()) { + $nbMailSend++; + } else { + $errormesg = $cMailFile->error.' : '.$to; + $error++; + } + } + + if (!$error) { $actionCommReminder->status = $actionCommReminder::STATUS_DONE; + $res = $actionCommReminder->update($user); - if ($res < 0) - { - $errorsMsg[] = "Failed to update status of ActionComm Reminder"; + if ($res < 0) { + $errorsMsg[] = "Failed to update status to done of ActionComm Reminder"; + $error++; + break; // This is to avoid to have this error on all the selected email. If we fails here for one record, it may fails for others. We must solve first. + } + } else { + $actionCommReminder->status = $actionCommReminder::STATUS_ERROR; + $actionCommReminder->lasterror = dol_trunc($errormesg, 128, 'right', 'UTF-8', 1); + + $res = $actionCommReminder->update($user); + if ($res < 0) { + $errorsMsg[] = "Failed to update status to error of ActionComm Reminder"; $error++; break; // This is to avoid to have this error on all the selected email. If we fails here for one record, it may fails for others. We must solve first. } else { - $nbMailSend++; + $errorsMsg[] = $errormesg; } - } else { - $errorsMsg[] = $cMailFile->error.' : '.$to; - $error++; } } else { + $errorsMsg[] = 'Failed to fetch record actioncomm with ID = '.$actionCommReminder->fk_actioncomm; $error++; } } @@ -2152,6 +2175,7 @@ class ActionComm extends CommonObject // Delete also very old past events (we do not keep more than 1 month record in past) $sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_reminder"; $sql .= " WHERE dateremind < '".$this->db->idate($now - (3600 * 24 * 32))."'"; + $sql .= " AND status = ".$actionCommReminder::STATUS_DONE; $resql = $this->db->query($sql); if (!$resql) { @@ -2166,7 +2190,7 @@ class ActionComm extends CommonObject return 0; } else { - $this->db->rollback(); + $this->db->commit(); // We commit also on error, to have the error message recorded. $this->error = 'Nb of emails sent : '.$nbMailSend.', '.(!empty($errorsMsg)) ? join(', ', $errorsMsg) : $error; return $error; } diff --git a/htdocs/comm/action/class/actioncommreminder.class.php b/htdocs/comm/action/class/actioncommreminder.class.php index 09c9a25ca1a..0f43dae13f8 100644 --- a/htdocs/comm/action/class/actioncommreminder.class.php +++ b/htdocs/comm/action/class/actioncommreminder.class.php @@ -52,6 +52,7 @@ class ActionCommReminder extends CommonObject const STATUS_TODO = 0; const STATUS_DONE = 1; + const STATUS_ERROR = -1; /** @@ -84,8 +85,9 @@ class ActionCommReminder extends CommonObject 'offsetvalue' => array('type'=>'integer', 'label'=>'OffsetValue', 'visible'=>1, 'enabled'=>1, 'position'=>56, 'notnull'=>1,), 'offsetunit' => array('type'=>'varchar(1)', 'label'=>'OffsetUnit', 'visible'=>1, 'enabled'=>1, 'position'=>57, 'notnull'=>1, 'comment'=>"y, m, d, w, h, i",), 'status' => array('type'=>'integer', 'label'=>'Status', 'visible'=>1, 'enabled'=>1, 'position'=>58, 'notnull'=>1, 'default'=>0, 'index'=>0, 'arrayofkeyval'=>array('0'=>'ToDo', '1'=>'Done')), - 'fk_actioncomm' => array('type'=>'integer', 'label'=>'Project', 'visible'=>1, 'enabled'=>1, 'position'=>59, 'notnull'=>1, 'index'=>1,), - 'fk_email_template' => array('type'=>'integer', 'label'=>'EmailTemplate', 'visible'=>1, 'enabled'=>1, 'position'=>60, 'notnull'=>0), + 'lasterror' => array('type'=>'varchar(128)', 'label'=>'LastError', 'visible'=>-1, 'enabled'=>1, 'position'=>59, 'index'=>0), + 'fk_actioncomm' => array('type'=>'integer', 'label'=>'Project', 'visible'=>1, 'enabled'=>1, 'position'=>70, 'notnull'=>1, 'index'=>1,), + 'fk_email_template' => array('type'=>'integer', 'label'=>'EmailTemplate', 'visible'=>1, 'enabled'=>1, 'position'=>80, 'notnull'=>0), ); /** @@ -114,6 +116,11 @@ class ActionCommReminder extends CommonObject */ public $status; + /** + * @var string Last error message + */ + public $lasterror; + /** * @var int Project */ @@ -217,31 +224,19 @@ class ActionCommReminder extends CommonObject // phpcs:enable global $langs; - if ($mode == 0 || $mode == 1) - { - if ($status == 1) return $langs->trans('Done'); - elseif ($status == 0) return $langs->trans('ToDo'); - } elseif ($mode == 2) - { - if ($status == 1) return img_picto($langs->trans('Done'), 'statut4').' '.$langs->trans('Done'); - elseif ($status == 0) return img_picto($langs->trans('ToDo'), 'statut5').' '.$langs->trans('ToDo'); - } elseif ($mode == 3) - { - if ($status == 1) return img_picto($langs->trans('Done'), 'statut4'); - elseif ($status == 0) return img_picto($langs->trans('ToDo'), 'statut5'); - } elseif ($mode == 4) - { - if ($status == 1) return img_picto($langs->trans('Done'), 'statut4').' '.$langs->trans('Done'); - elseif ($status == 0) return img_picto($langs->trans('ToDo'), 'statut5').' '.$langs->trans('ToDo'); - } elseif ($mode == 5) - { - if ($status == 1) return $langs->trans('Done').' '.img_picto($langs->trans('Done'), 'statut4'); - elseif ($status == 0) return $langs->trans('ToDo').' '.img_picto($langs->trans('ToDo'), 'statut5'); - } elseif ($mode == 6) - { - if ($status == 1) return $langs->trans('Done').' '.img_picto($langs->trans('Done'), 'statut4'); - elseif ($status == 0) return $langs->trans('ToDo').' '.img_picto($langs->trans('ToDo'), 'statut5'); - } + $labelStatus = $langs->trans('ToDo'); + if ($status == 1) $labelStatus = $langs->trans('Done'); + elseif ($status == -1) $labelStatus = $langs->trans('Error'); + + $labelStatusShort = $langs->trans('ToDo'); + if ($status == 1) $labelStatus = $langs->trans('Done'); + elseif ($status == -1) $labelStatus = $langs->trans('Error'); + + $statusType = 'status5'; + if ($status == 1) $statusType = 'status4'; + elseif ($status == -1) $statusType = 'status8'; + + return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode); } /** diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 7c3b368ae14..55ab524daec 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7907,7 +7907,7 @@ abstract class CommonObject }*/ } - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET '.implode(',', $tmp).' WHERE rowid='.$this->id; + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET '.implode(', ', $tmp).' WHERE rowid='.$this->id; $this->db->begin(); if (!$error) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 74f77c28558..13a3a408abb 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3096,7 +3096,7 @@ function dol_substr($string, $start, $length, $stringencoding = '', $trunconbyte * * @param string $string String to truncate * @param int $size Max string size visible (excluding ...). 0 for no limit. WARNING: Final string size can have 3 more chars (if we added ..., or if size was max+1 or max+2 or max+3 so it does not worse to replace with ...) - * @param string $trunc Where to trunc: right, left, middle (size must be a 2 power), wrap + * @param string $trunc Where to trunc: 'right', 'left', 'middle' (size must be a 2 power), 'wrap' * @param string $stringencoding Tell what is source string encoding * @param int $nodot Truncation do not add ... after truncation. So it's an exact truncation. * @param int $display Trunc is used to display data and can be changed for small screen. TODO Remove this param (must be dealt with CSS) diff --git a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql index 9ef94188171..d4cfaddb4eb 100644 --- a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql +++ b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql @@ -356,6 +356,7 @@ insert into llx_c_action_trigger (code,label,description,elementtype,rang) value ALTER TABLE llx_actioncomm_reminder ADD COLUMN entity integer NOT NULL DEFAULT 1; ALTER TABLE llx_actioncomm_reminder ADD COLUMN fk_actioncomm integer NOT NULL; ALTER TABLE llx_actioncomm_reminder ADD COLUMN fk_email_template integer; +ALTER TABLE llx_actioncomm_reminder ADD COLUMN lasterror varchar(128) NULL; ALTER TABLE llx_actioncomm_reminder DROP INDEX uk_actioncomm_reminder_unique; ALTER TABLE llx_actioncomm_reminder ADD UNIQUE uk_actioncomm_reminder_unique (fk_user, typeremind, offsetvalue, offsetunit, fk_actioncomm); diff --git a/htdocs/install/mysql/tables/llx_actioncomm_reminder.sql b/htdocs/install/mysql/tables/llx_actioncomm_reminder.sql index 78dd00d6ea5..bc09c2e80dc 100644 --- a/htdocs/install/mysql/tables/llx_actioncomm_reminder.sql +++ b/htdocs/install/mysql/tables/llx_actioncomm_reminder.sql @@ -23,6 +23,7 @@ CREATE TABLE llx_actioncomm_reminder( offsetvalue integer NOT NULL, offsetunit varchar(1) NOT NULL, status integer NOT NULL DEFAULT 0, + lasterror varchar(128) NULL, entity integer NOT NULL DEFAULT 1, fk_actioncomm integer NOT NULL, fk_email_template integer