Debug feature to send remind by email

This commit is contained in:
Laurent Destailleur 2020-11-21 17:42:07 +01:00
parent f39ad2359a
commit 9004be1df2
6 changed files with 69 additions and 48 deletions

View File

@ -2087,6 +2087,7 @@ class ActionComm extends CommonObject
if ($res > 0) if ($res > 0)
{ {
// PREPARE EMAIL // PREPARE EMAIL
$errormesg = '';
// Make substitution in email content // Make substitution in email content
$substitutionarray = getCommonSubstitutionArray($langs, 0, '', $this); $substitutionarray = getCommonSubstitutionArray($langs, 0, '', $this);
@ -2102,43 +2103,65 @@ class ActionComm extends CommonObject
// Recipient // Recipient
$recipient = new User($this->db); $recipient = new User($this->db);
$res = $recipient->fetch($actionCommReminder->fk_user); $res = $recipient->fetch($actionCommReminder->fk_user);
if ($res > 0 && !empty($recipient->email)) $to = $recipient->email; if ($res > 0) {
else { if (!empty($recipient->email)) {
$errorsMsg[] = "Failed to load recipient"; $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++; $error++;
} }
// Sender // Sender
$from = $conf->global->MAIN_MAIL_EMAIL_FROM; $from = $conf->global->MAIN_MAIL_EMAIL_FROM;
if (empty($from)) { if (empty($from)) {
$errorsMsg[] = "Failed to load recipient"; $errormesg = "Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
$error++; $error++;
} }
// Errors Recipient if (!$error) {
$errors_to = $conf->global->MAIN_MAIL_ERRORS_TO; // Errors Recipient
$errors_to = $conf->global->MAIN_MAIL_ERRORS_TO;
// Mail Creation // Mail Creation
$cMailFile = new CMailFile($sendTopic, $to, $from, $sendContent, array(), array(), array(), '', "", 0, 1, $errors_to, '', '', '', '', ''); $cMailFile = new CMailFile($sendTopic, $to, $from, $sendContent, array(), array(), array(), '', "", 0, 1, $errors_to, '', '', '', '', '');
// Sending Mail // Sending Mail
if ($cMailFile->sendfile()) if ($cMailFile->sendfile()) {
{ $nbMailSend++;
} else {
$errormesg = $cMailFile->error.' : '.$to;
$error++;
}
}
if (!$error) {
$actionCommReminder->status = $actionCommReminder::STATUS_DONE; $actionCommReminder->status = $actionCommReminder::STATUS_DONE;
$res = $actionCommReminder->update($user); $res = $actionCommReminder->update($user);
if ($res < 0) if ($res < 0) {
{ $errorsMsg[] = "Failed to update status to done of ActionComm Reminder";
$errorsMsg[] = "Failed to update status 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++; $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. 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 { } else {
$nbMailSend++; $errorsMsg[] = $errormesg;
} }
} else {
$errorsMsg[] = $cMailFile->error.' : '.$to;
$error++;
} }
} else { } else {
$errorsMsg[] = 'Failed to fetch record actioncomm with ID = '.$actionCommReminder->fk_actioncomm;
$error++; $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) // 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 = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
$sql .= " WHERE dateremind < '".$this->db->idate($now - (3600 * 24 * 32))."'"; $sql .= " WHERE dateremind < '".$this->db->idate($now - (3600 * 24 * 32))."'";
$sql .= " AND status = ".$actionCommReminder::STATUS_DONE;
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if (!$resql) { if (!$resql) {
@ -2166,7 +2190,7 @@ class ActionComm extends CommonObject
return 0; return 0;
} }
else { 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; $this->error = 'Nb of emails sent : '.$nbMailSend.', '.(!empty($errorsMsg)) ? join(', ', $errorsMsg) : $error;
return $error; return $error;
} }

View File

@ -52,6 +52,7 @@ class ActionCommReminder extends CommonObject
const STATUS_TODO = 0; const STATUS_TODO = 0;
const STATUS_DONE = 1; 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,), '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",), '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')), '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,), 'lasterror' => array('type'=>'varchar(128)', 'label'=>'LastError', 'visible'=>-1, 'enabled'=>1, 'position'=>59, 'index'=>0),
'fk_email_template' => array('type'=>'integer', 'label'=>'EmailTemplate', 'visible'=>1, 'enabled'=>1, 'position'=>60, 'notnull'=>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; public $status;
/**
* @var string Last error message
*/
public $lasterror;
/** /**
* @var int Project * @var int Project
*/ */
@ -217,31 +224,19 @@ class ActionCommReminder extends CommonObject
// phpcs:enable // phpcs:enable
global $langs; global $langs;
if ($mode == 0 || $mode == 1) $labelStatus = $langs->trans('ToDo');
{ if ($status == 1) $labelStatus = $langs->trans('Done');
if ($status == 1) return $langs->trans('Done'); elseif ($status == -1) $labelStatus = $langs->trans('Error');
elseif ($status == 0) return $langs->trans('ToDo');
} elseif ($mode == 2) $labelStatusShort = $langs->trans('ToDo');
{ if ($status == 1) $labelStatus = $langs->trans('Done');
if ($status == 1) return img_picto($langs->trans('Done'), 'statut4').' '.$langs->trans('Done'); elseif ($status == -1) $labelStatus = $langs->trans('Error');
elseif ($status == 0) return img_picto($langs->trans('ToDo'), 'statut5').' '.$langs->trans('ToDo');
} elseif ($mode == 3) $statusType = 'status5';
{ if ($status == 1) $statusType = 'status4';
if ($status == 1) return img_picto($langs->trans('Done'), 'statut4'); elseif ($status == -1) $statusType = 'status8';
elseif ($status == 0) return img_picto($langs->trans('ToDo'), 'statut5');
} elseif ($mode == 4) return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
{
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');
}
} }
/** /**

View File

@ -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(); $this->db->begin();
if (!$error) if (!$error)

View File

@ -3096,7 +3096,7 @@ function dol_substr($string, $start, $length, $stringencoding = '', $trunconbyte
* *
* @param string $string String to truncate * @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 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 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 $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) * @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)

View File

@ -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 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_actioncomm integer NOT NULL;
ALTER TABLE llx_actioncomm_reminder ADD COLUMN fk_email_template integer; 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 DROP INDEX uk_actioncomm_reminder_unique;
ALTER TABLE llx_actioncomm_reminder ADD UNIQUE uk_actioncomm_reminder_unique (fk_user, typeremind, offsetvalue, offsetunit, fk_actioncomm); ALTER TABLE llx_actioncomm_reminder ADD UNIQUE uk_actioncomm_reminder_unique (fk_user, typeremind, offsetvalue, offsetunit, fk_actioncomm);

View File

@ -23,6 +23,7 @@ CREATE TABLE llx_actioncomm_reminder(
offsetvalue integer NOT NULL, offsetvalue integer NOT NULL,
offsetunit varchar(1) NOT NULL, offsetunit varchar(1) NOT NULL,
status integer NOT NULL DEFAULT 0, status integer NOT NULL DEFAULT 0,
lasterror varchar(128) NULL,
entity integer NOT NULL DEFAULT 1, entity integer NOT NULL DEFAULT 1,
fk_actioncomm integer NOT NULL, fk_actioncomm integer NOT NULL,
fk_email_template integer fk_email_template integer