From 8ae334ee50ceb1b989583fb06d6a56f628200269 Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 12:42:42 +0100 Subject: [PATCH 01/11] Add email template backport for pre-23 Dolibarr --- admin/setup.php | 21 +- class/timesheetweek_reminder.class.php | 12 +- core/class/cemailtemplate.class.php | 345 ++++++++++++++++++ ...esheetWeek_TimesheetWeekTriggers.class.php | 24 +- 4 files changed, 388 insertions(+), 14 deletions(-) create mode 100644 core/class/cemailtemplate.class.php diff --git a/admin/setup.php b/admin/setup.php index ca49120..4174935 100644 --- a/admin/setup.php +++ b/admin/setup.php @@ -33,15 +33,28 @@ $res = require_once __DIR__.'/../../../main.inc.php'; } if (!$res) { - die('Include of main fails'); + die('Include of main fails'); } - require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; -require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php'; -// EN: Load document helper functions required for model toggles. +// EN: Load email template class with backward compatibility for older Dolibarr versions. +$emailTemplatePath = ''; +$moduleEmailTemplatePath = dol_buildpath('/timesheetweek/core/class/cemailtemplate.class.php', 0); +if (version_compare(DOL_VERSION, '23.0.0', '<')) { + if (is_readable($moduleEmailTemplatePath)) { + $emailTemplatePath = '/timesheetweek/core/class/cemailtemplate.class.php'; + } +} elseif (is_readable(DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php')) { + $emailTemplatePath = '/core/class/cemailtemplate.class.php'; +} +if (empty($emailTemplatePath) && is_readable($moduleEmailTemplatePath)) { + $emailTemplatePath = '/timesheetweek/core/class/cemailtemplate.class.php'; +} +if (!empty($emailTemplatePath)) { + dol_include_once($emailTemplatePath); +} require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php'; dol_include_once('/timesheetweek/lib/timesheetweek.lib.php'); dol_include_once('/timesheetweek/class/timesheetweek.class.php'); diff --git a/class/timesheetweek_reminder.class.php b/class/timesheetweek_reminder.class.php index bcc8280..da9d77a 100644 --- a/class/timesheetweek_reminder.class.php +++ b/class/timesheetweek_reminder.class.php @@ -96,10 +96,16 @@ public function run($dbInstance = null, $limit = 0, $forcerun = 0, array $target } $emailTemplateClassFile = ''; - if (is_readable(DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php')) { + $moduleEmailTemplatePath = dol_buildpath('/timesheetweek/core/class/cemailtemplate.class.php', 0); + if (version_compare(DOL_VERSION, '23.0.0', '<')) { + if (is_readable($moduleEmailTemplatePath)) { + $emailTemplateClassFile = '/timesheetweek/core/class/cemailtemplate.class.php'; + } + } elseif (is_readable(DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php')) { $emailTemplateClassFile = '/core/class/cemailtemplate.class.php'; - } elseif (is_readable(DOL_DOCUMENT_ROOT.'/core/class/emailtemplate.class.php')) { - $emailTemplateClassFile = '/core/class/emailtemplate.class.php'; + } + if (empty($emailTemplateClassFile) && is_readable($moduleEmailTemplatePath)) { + $emailTemplateClassFile = '/timesheetweek/core/class/cemailtemplate.class.php'; } if (!empty($emailTemplateClassFile)) { diff --git a/core/class/cemailtemplate.class.php b/core/class/cemailtemplate.class.php new file mode 100644 index 0000000..faf796e --- /dev/null +++ b/core/class/cemailtemplate.class.php @@ -0,0 +1,345 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file custom/timesheetweek/core/class/cemailtemplate.class.php + * \ingroup timesheetweek + * \brief Backport of Dolibarr email template classes for versions prior to v23. + */ + +// Load Dolibarr environment +if (!defined('DOL_DOCUMENT_ROOT')) { + die('This file must be included inside Dolibarr.'); +} + +dol_include_once('/core/lib/functions.lib.php'); + +/** + * Backport of the single e-mail template class used before Dolibarr v23. + */ +class CEmailTemplate +{ + /** @var DoliDB */ + public $db; + public $id; + public $rowid; + public $entity = 1; + public $module = ''; + public $type_template = ''; + public $label = ''; + public $lang = ''; + public $private = 0; + public $fk_user; + public $position = 0; + public $active = 1; + public $enabled = 1; + public $joinfiles = 0; + public $email_from = ''; + public $email_to = ''; + public $email_tocc = ''; + public $email_tobcc = ''; + public $topic = ''; + public $subject = ''; + public $content = ''; + public $code = ''; + public $error = ''; + public $errors = array(); + + /** + * @param DoliDB $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + } + + /** + * Fetch one template by id. + * + * @param int $id Template id + * @return int + */ + public function fetch($id) + { + $fields = $this->buildSelectFields(); + $sql = 'SELECT '.implode(', ', $fields).' FROM '.$this->getTableName().' WHERE rowid = '.((int) $id).' LIMIT 1'; + + $resql = $this->db->query($sql); + if (!$resql) { + $this->error = $this->db->lasterror(); + return -1; + } + + $obj = $this->db->fetch_object($resql); + if (!$obj) { + return 0; + } + + $this->setProperties($obj); + + return 1; + } + + /** + * API compatible wrapper around fetch. + * + * @param int $id Template id + * @return int + */ + public function apifetch($id) + { + return $this->fetch($id); + } + + /** + * Create a new template record. + * + * @param User $user Creator + * @return int + */ + public function create($user) + { + $fields = array('entity', 'module', 'type_template', 'label', 'lang', 'private', 'fk_user', 'position', 'active', 'enabled', 'joinfiles', 'email_from', 'email_to', 'email_tocc', 'email_tobcc', 'topic', 'content', 'code', 'subject'); + $columns = array(); + $values = array(); + + foreach ($fields as $field) { + if (!$this->columnExists($field)) { + continue; + } + + $value = $this->$field; + if ($field === 'entity') { + $value = (int) (isset($this->entity) ? $this->entity : $user->entity); + } + if ($field === 'fk_user') { + $value = isset($this->fk_user) ? (int) $this->fk_user : (int) $user->id; + } + + $columns[] = $field; + $values[] = "'".$this->db->escape($value)."'"; + } + + if ($this->columnExists('datec')) { + $columns[] = 'datec'; + $values[] = $this->db->idate(dol_now()); + } + + $sql = 'INSERT INTO '.$this->getTableName().' ('.implode(', ', $columns).') VALUES ('.implode(', ', $values).')'; + + $resql = $this->db->query($sql); + if (!$resql) { + $this->error = $this->db->lasterror(); + return -1; + } + + $this->id = $this->db->last_insert_id($this->getTableName(), 'rowid'); + $this->rowid = $this->id; + + return $this->id > 0 ? $this->id : 1; + } + + /** + * Build the list of existing columns to select. + * + * @return array + */ + protected function buildSelectFields() + { + $possible = array('rowid', 'entity', 'module', 'type_template', 'label', 'lang', 'private', 'fk_user', 'position', 'active', 'enabled', 'joinfiles', 'email_from', 'email_to', 'email_tocc', 'email_tobcc', 'topic', 'content', 'code', 'subject'); + $fields = array(); + + foreach ($possible as $field) { + if ($this->columnExists($field)) { + $fields[] = $field; + } + } + + return empty($fields) ? array('*') : $fields; + } + + /** + * Check if a column exists. + * + * @param string $column Column name + * @return bool + */ + protected function columnExists($column) + { + static $cache = array(); + if (isset($cache[$column])) { + return $cache[$column]; + } + + $table = MAIN_DB_PREFIX.'c_email_templates'; + $sql = "SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME='".$this->db->escape($table)."' AND COLUMN_NAME='".$this->db->escape($column)."' LIMIT 1"; + $resql = $this->db->query($sql); + $exists = false; + if ($resql) { + $exists = (bool) $this->db->fetch_object($resql); + } + + $cache[$column] = $exists; + + return $exists; + } + + /** + * Hydrate class properties from database row. + * + * @param stdClass $row Data row + * @return void + */ + protected function setProperties($row) + { + foreach ($row as $key => $value) { + $this->$key = $value; + } + + $this->id = isset($row->rowid) ? $row->rowid : $this->id; + $this->rowid = $this->id; + + if (empty($this->subject) && !empty($this->topic)) { + $this->subject = $this->topic; + } + } + + /** + * Get the table name with prefix. + * + * @return string + */ + protected function getTableName() + { + return MAIN_DB_PREFIX.'c_email_templates'; + } +} + +/** + * Backport of the e-mail templates collection class. + */ +class CEmailTemplates extends CEmailTemplate +{ + /** + * Fetch a template by trigger code. + * + * @param string $action Trigger code + * @param User $user Current user + * @param int|null $entity Entity id + * @return int + */ + public function fetchByTrigger($action, $user = null, $entity = null) + { + $fields = $this->buildSelectFields(); + $where = array(); + $entityId = is_null($entity) ? 1 : (int) $entity; + + if ($this->columnExists('code')) { + $where[] = "code='".$this->db->escape($action)."'"; + } else { + $where[] = "label='".$this->db->escape($action)."'"; + } + + if ($this->columnExists('entity')) { + $where[] = 'entity IN ('.$entityId.', 0)'; + } + + $sql = 'SELECT '.implode(', ', $fields).' FROM '.$this->getTableName(); + if (!empty($where)) { + $sql .= ' WHERE '.implode(' AND ', $where); + } + $sql .= ' ORDER BY entity DESC'; + $sql .= $this->columnExists('position') ? ' , position ASC' : ''; + $sql .= ' LIMIT 1'; + + $resql = $this->db->query($sql); + if (!$resql) { + $this->error = $this->db->lasterror(); + return -1; + } + + $row = $this->db->fetch_object($resql); + if (!$row) { + return 0; + } + + $this->setProperties($row); + + return 1; + } + + /** + * Fetch all templates according to filters. + * + * @param string $sortorder Sort order + * @param string $sortfield Sort field + * @param int $limit Limit + * @param int $offset Offset + * @param array $filter Filter array + * @return array|int + */ + public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array()) + { + $fields = $this->buildSelectFields(); + $sql = 'SELECT '.implode(', ', $fields).' FROM '.$this->getTableName(); + + $where = array(); + if (!empty($filter['entity']) && $this->columnExists('entity')) { + $where[] = 'entity IN ('.implode(',', array_map('intval', (array) $filter['entity'])).')'; + } + if (!empty($filter['type_template']) && $this->columnExists('type_template')) { + $where[] = "type_template='".$this->db->escape($filter['type_template'])."'"; + } + if (!empty($filter['module']) && $this->columnExists('module')) { + $where[] = "module='".$this->db->escape($filter['module'])."'"; + } + if (isset($filter['active']) && $this->columnExists('active')) { + $where[] = 'active='.(int) $filter['active']; + } + if (isset($filter['enabled']) && $this->columnExists('enabled')) { + $where[] = 'enabled='.(int) $filter['enabled']; + } + + if (!empty($where)) { + $sql .= ' WHERE '.implode(' AND ', $where); + } + + if (!empty($sortfield) && $this->columnExists($sortfield)) { + $order = !empty($sortorder) ? $sortorder : 'ASC'; + $sql .= ' ORDER BY '.$this->db->escape($sortfield).' '.$this->db->escape($order); + } + + if ((int) $limit > 0) { + $sql .= ' '.$this->db->plimit((int) $limit, (int) $offset); + } + + $resql = $this->db->query($sql); + if (!$resql) { + $this->error = $this->db->lasterror(); + return -1; + } + + $templates = array(); + while ($row = $this->db->fetch_object($resql)) { + $template = new CEmailTemplate($this->db); + $template->setProperties($row); + $templates[] = $template; + } + + return $templates; + } +} diff --git a/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php b/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php index e11e6e3..fe7d3c2 100644 --- a/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php +++ b/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php @@ -242,13 +242,23 @@ protected function sendNotification($action, TimesheetWeek $timesheet, User $act return 0; } - dol_include_once('/core/lib/functions2.lib.php'); - if (is_readable(DOL_DOCUMENT_ROOT.'/core/class/cemailtemplates.class.php')) { - dol_include_once('/core/class/cemailtemplates.class.php'); - } elseif (is_readable(DOL_DOCUMENT_ROOT.'/core/class/emailtemplates.class.php')) { - dol_include_once('/core/class/emailtemplates.class.php'); - } - dol_include_once('/core/class/CMailFile.class.php'); + dol_include_once('/core/lib/functions2.lib.php'); + $emailTemplatePath = ''; + $moduleEmailTemplatePath = dol_buildpath('/timesheetweek/core/class/cemailtemplate.class.php', 0); + if (version_compare(DOL_VERSION, '23.0.0', '<')) { + if (is_readable($moduleEmailTemplatePath)) { + $emailTemplatePath = '/timesheetweek/core/class/cemailtemplate.class.php'; + } + } elseif (is_readable(DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php')) { + $emailTemplatePath = '/core/class/cemailtemplate.class.php'; + } + if (empty($emailTemplatePath) && is_readable($moduleEmailTemplatePath)) { + $emailTemplatePath = '/timesheetweek/core/class/cemailtemplate.class.php'; + } + if (!empty($emailTemplatePath)) { + dol_include_once($emailTemplatePath); + } + dol_include_once('/core/class/CMailFile.class.php'); $templateClass = ''; if (class_exists('CEmailTemplates')) { From 563e0c36eb82b91e436f236d6f7c121e2ad8730c Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 12:53:37 +0100 Subject: [PATCH 02/11] Update setup.php --- admin/setup.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/admin/setup.php b/admin/setup.php index 4174935..97ef5bd 100644 --- a/admin/setup.php +++ b/admin/setup.php @@ -40,21 +40,13 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; // EN: Load email template class with backward compatibility for older Dolibarr versions. -$emailTemplatePath = ''; -$moduleEmailTemplatePath = dol_buildpath('/timesheetweek/core/class/cemailtemplate.class.php', 0); + if (version_compare(DOL_VERSION, '23.0.0', '<')) { - if (is_readable($moduleEmailTemplatePath)) { - $emailTemplatePath = '/timesheetweek/core/class/cemailtemplate.class.php'; - } -} elseif (is_readable(DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php')) { - $emailTemplatePath = '/core/class/cemailtemplate.class.php'; -} -if (empty($emailTemplatePath) && is_readable($moduleEmailTemplatePath)) { - $emailTemplatePath = '/timesheetweek/core/class/cemailtemplate.class.php'; -} -if (!empty($emailTemplatePath)) { - dol_include_once($emailTemplatePath); + dol_inclunde_once('/timesheetweek/core/class/cemailtemplate.class.php'); +} else { + require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php'; } + require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php'; dol_include_once('/timesheetweek/lib/timesheetweek.lib.php'); dol_include_once('/timesheetweek/class/timesheetweek.class.php'); From 1e73f586960625a4f696e5088fa8901d85cf981f Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 12:54:47 +0100 Subject: [PATCH 03/11] Update timesheetweek_reminder.class.php --- class/timesheetweek_reminder.class.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/class/timesheetweek_reminder.class.php b/class/timesheetweek_reminder.class.php index da9d77a..79b284c 100644 --- a/class/timesheetweek_reminder.class.php +++ b/class/timesheetweek_reminder.class.php @@ -95,21 +95,10 @@ public function run($dbInstance = null, $limit = 0, $forcerun = 0, array $target } } - $emailTemplateClassFile = ''; - $moduleEmailTemplatePath = dol_buildpath('/timesheetweek/core/class/cemailtemplate.class.php', 0); if (version_compare(DOL_VERSION, '23.0.0', '<')) { - if (is_readable($moduleEmailTemplatePath)) { - $emailTemplateClassFile = '/timesheetweek/core/class/cemailtemplate.class.php'; - } - } elseif (is_readable(DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php')) { - $emailTemplateClassFile = '/core/class/cemailtemplate.class.php'; - } - if (empty($emailTemplateClassFile) && is_readable($moduleEmailTemplatePath)) { - $emailTemplateClassFile = '/timesheetweek/core/class/cemailtemplate.class.php'; - } - - if (!empty($emailTemplateClassFile)) { - dol_include_once($emailTemplateClassFile); + dol_inclunde_once('/timesheetweek/core/class/cemailtemplate.class.php'); + } else { + require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php'; } if (!class_exists('CEmailTemplate') && !class_exists('EmailTemplate')) { From 9c37bad2857c331ee550422e06cdefb97e83afdc Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 12:56:16 +0100 Subject: [PATCH 04/11] Update interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php --- ...TimesheetWeek_TimesheetWeekTriggers.class.php | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php b/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php index fe7d3c2..97158d0 100644 --- a/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php +++ b/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php @@ -243,20 +243,10 @@ protected function sendNotification($action, TimesheetWeek $timesheet, User $act } dol_include_once('/core/lib/functions2.lib.php'); - $emailTemplatePath = ''; - $moduleEmailTemplatePath = dol_buildpath('/timesheetweek/core/class/cemailtemplate.class.php', 0); if (version_compare(DOL_VERSION, '23.0.0', '<')) { - if (is_readable($moduleEmailTemplatePath)) { - $emailTemplatePath = '/timesheetweek/core/class/cemailtemplate.class.php'; - } - } elseif (is_readable(DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php')) { - $emailTemplatePath = '/core/class/cemailtemplate.class.php'; - } - if (empty($emailTemplatePath) && is_readable($moduleEmailTemplatePath)) { - $emailTemplatePath = '/timesheetweek/core/class/cemailtemplate.class.php'; - } - if (!empty($emailTemplatePath)) { - dol_include_once($emailTemplatePath); + dol_inclunde_once('/timesheetweek/core/class/cemailtemplate.class.php'); + } else { + require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php'; } dol_include_once('/core/class/CMailFile.class.php'); From d6e463436a40c64b52d1830dd00dc11db646e02b Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 12:57:29 +0100 Subject: [PATCH 05/11] Update setup.php --- admin/setup.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/setup.php b/admin/setup.php index 97ef5bd..541baf0 100644 --- a/admin/setup.php +++ b/admin/setup.php @@ -42,7 +42,7 @@ // EN: Load email template class with backward compatibility for older Dolibarr versions. if (version_compare(DOL_VERSION, '23.0.0', '<')) { - dol_inclunde_once('/timesheetweek/core/class/cemailtemplate.class.php'); + dol_include_once('/timesheetweek/core/class/cemailtemplate.class.php'); } else { require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php'; } From 1a98a0acc6df9295b1fa4021ddc88cfc1a9b7bb3 Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 12:58:47 +0100 Subject: [PATCH 06/11] Update interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php --- ...nterface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php b/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php index 97158d0..5037c1c 100644 --- a/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php +++ b/core/triggers/interface_99_modTimesheetWeek_TimesheetWeekTriggers.class.php @@ -244,7 +244,7 @@ protected function sendNotification($action, TimesheetWeek $timesheet, User $act dol_include_once('/core/lib/functions2.lib.php'); if (version_compare(DOL_VERSION, '23.0.0', '<')) { - dol_inclunde_once('/timesheetweek/core/class/cemailtemplate.class.php'); + dol_include_once('/timesheetweek/core/class/cemailtemplate.class.php'); } else { require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php'; } From 282ce79a40227dd90ab5c0d8908044487e990d4d Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 12:59:10 +0100 Subject: [PATCH 07/11] Update timesheetweek_reminder.class.php --- class/timesheetweek_reminder.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class/timesheetweek_reminder.class.php b/class/timesheetweek_reminder.class.php index 79b284c..6b34817 100644 --- a/class/timesheetweek_reminder.class.php +++ b/class/timesheetweek_reminder.class.php @@ -96,7 +96,7 @@ public function run($dbInstance = null, $limit = 0, $forcerun = 0, array $target } if (version_compare(DOL_VERSION, '23.0.0', '<')) { - dol_inclunde_once('/timesheetweek/core/class/cemailtemplate.class.php'); + dol_include_once('/timesheetweek/core/class/cemailtemplate.class.php'); } else { require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php'; } From 00478b055c75ab959586ed216f92fa5cd0cbd7cc Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 13:01:14 +0100 Subject: [PATCH 08/11] Update cemailtemplate.class.php --- core/class/cemailtemplate.class.php | 709 ++++++++++++++++++---------- 1 file changed, 470 insertions(+), 239 deletions(-) diff --git a/core/class/cemailtemplate.class.php b/core/class/cemailtemplate.class.php index faf796e..6573f19 100644 --- a/core/class/cemailtemplate.class.php +++ b/core/class/cemailtemplate.class.php @@ -1,5 +1,14 @@ +/* Copyright (C) 2005-2012 Laurent Destailleur + * Copyright (C) 2005-2012 Regis Houssin + * Copyright (C) 2010-2011 Juanjo Menent + * Copyright (C) 2015-2017 Marcos García + * Copyright (C) 2015-2017 Nicolas ZABOURI + * Copyright (C) 2018-2024 Frédéric France + * Copyright (C) 2022 Charlene Benke + * Copyright (C) 2023 Anthony Berton + * Copyright (C) 2024-2025 MDW + * Copyright (C) 2025 Jon Bendtsen * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -12,334 +21,556 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * along with this program. If not, see . */ +require_once DOL_DOCUMENT_ROOT.'/core/class/doldeprecationhandler.class.php'; + /** - * \file custom/timesheetweek/core/class/cemailtemplate.class.php - * \ingroup timesheetweek - * \brief Backport of Dolibarr email template classes for versions prior to v23. + * Object of table llx_c_email_templates */ +class CEmailTemplate extends CommonObject +{ + const TRIGGER_PREFIX = 'EMAILTEMPLATE'; + /** + * @var string ID to identify managed object. + */ + public $element = 'email_template'; -// Load Dolibarr environment -if (!defined('DOL_DOCUMENT_ROOT')) { - die('This file must be included inside Dolibarr.'); -} + /** + * @var string Name of table without prefix where object is stored. This is also the key used for extrafields management (so extrafields know the link to the parent table). + */ + public $table_element = 'c_email_templates'; -dol_include_once('/core/lib/functions.lib.php'); -/** - * Backport of the single e-mail template class used before Dolibarr v23. - */ -class CEmailTemplate -{ - /** @var DoliDB */ - public $db; - public $id; + // BEGIN MODULEBUILDER PROPERTIES + /** + * @var array|string,position:int,notnull?:int,visible:int<-6,6>|string,alwayseditable?:int<0,1>|string,noteditable?:int<0,1>,default?:string,index?:int,foreignkey?:string,searchall?:int<0,1>,isameasure?:int<0,1>,css?:string,cssview?:string,csslist?:string,help?:string,showoncombobox?:int<0,4>|string,disabled?:int<0,1>,arrayofkeyval?:array,autofocusoncreate?:int<0,1>,comment?:string,copytoclipboard?:int<1,2>,validate?:int<0,1>,showonheader?:int<0,1>,searchmulti?:int<0,1>}> Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. + */ + public $fields = array( + "rowid" => array("type" => "integer", "label" => "TechnicalID", 'enabled' => 1, 'position' => 10, 'notnull' => 1, 'visible' => -1,), + "module" => array("type" => "varchar(32)", "label" => "Module", 'enabled' => 1, 'position' => 20, 'notnull' => 0, 'visible' => -1,), + "type_template" => array("type" => "varchar(32)", "label" => "Typetemplate", 'enabled' => 1, 'position' => 25, 'notnull' => 0, 'visible' => -1,), + "lang" => array("type" => "varchar(6)", "label" => "Lang", 'enabled' => 1, 'position' => 30, 'notnull' => 0, 'visible' => -1,), + "private" => array("type" => "smallint(6)", "label" => "Private", 'enabled' => 1, 'position' => 35, 'notnull' => 1, 'visible' => -1,), + "fk_user" => array("type" => "integer:User:user/class/user.class.php", "label" => "Fkuser", 'enabled' => 1, 'position' => 40, 'notnull' => 0, 'visible' => -1, "css" => "maxwidth500 widthcentpercentminusxx", "csslist" => "tdoverflowmax150",), + "datec" => array("type" => "datetime", "label" => "DateCreation", 'enabled' => 1, 'position' => 45, 'notnull' => 0, 'visible' => -1,), + "tms" => array("type" => "timestamp", "label" => "DateModification", 'enabled' => 1, 'position' => 50, 'notnull' => 1, 'visible' => -1,), + "label" => array("type" => "varchar(255)", "label" => "Label", 'enabled' => 1, 'position' => 55, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1, "css" => "minwidth300", "cssview" => "wordbreak", "csslist" => "tdoverflowmax150",), + "position" => array("type" => "smallint(6)", "label" => "Position", 'enabled' => 1, 'position' => 60, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "active" => array("type" => "integer", "label" => "Active", 'enabled' => 1, 'position' => 65, 'notnull' => 1, 'visible' => -1, 'alwayseditable' => 1,), + "topic" => array("type" => "text", "label" => "Topic", 'enabled' => 1, 'position' => 70, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "content" => array("type" => "mediumtext", "label" => "Content", 'enabled' => 1, 'position' => 75, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "content_lines" => array("type" => "text", "label" => "Contentlines", "enabled" => "getDolGlobalString('MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES')", 'position' => 80, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "enabled" => array("type" => "varchar(255)", "label" => "Enabled", 'enabled' => 1, 'position' => 85, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "joinfiles" => array("type" => "varchar(255)", "label" => "Joinfiles", 'enabled' => 1, 'position' => 90, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "email_from" => array("type" => "varchar(255)", "label" => "Emailfrom", 'enabled' => 1, 'position' => 95, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "email_to" => array("type" => "varchar(255)", "label" => "Emailto", 'enabled' => 1, 'position' => 100, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "email_tocc" => array("type" => "varchar(255)", "label" => "Emailtocc", 'enabled' => 1, 'position' => 105, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "email_tobcc" => array("type" => "varchar(255)", "label" => "Emailtobcc", 'enabled' => 1, 'position' => 110, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + "defaultfortype" => array("type" => "smallint(6)", "label" => "Defaultfortype", 'enabled' => 1, 'position' => 115, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,), + ); + /** + * @var int + */ public $rowid; - public $entity = 1; - public $module = ''; - public $type_template = ''; - public $label = ''; - public $lang = ''; - public $private = 0; - public $fk_user; - public $position = 0; - public $active = 1; - public $enabled = 1; - public $joinfiles = 0; - public $email_from = ''; - public $email_to = ''; - public $email_tocc = ''; - public $email_tobcc = ''; - public $topic = ''; - public $subject = ''; - public $content = ''; - public $code = ''; - public $error = ''; - public $errors = array(); - /** - * @param DoliDB $db Database handler + * @var string type of the template */ - public function __construct($db) - { - $this->db = $db; - } + public $type_template; + /** + * @var int|string + */ + public $datec; + /** + * @var int + */ + public $tms; + /** + * @var int + */ + public $active; + /** + * @var string if 0 hidden from GUI, if 1 visible in GUI + */ + public $enabled; + /** + * @var int is the template a default or not + */ + public $defaultfortype; /** - * Fetch one template by id. - * - * @param int $id Template id - * @return int + * @var int ID */ - public function fetch($id) - { - $fields = $this->buildSelectFields(); - $sql = 'SELECT '.implode(', ', $fields).' FROM '.$this->getTableName().' WHERE rowid = '.((int) $id).' LIMIT 1'; + public $id; - $resql = $this->db->query($sql); - if (!$resql) { - $this->error = $this->db->lasterror(); - return -1; - } + /** + * @var string Model mail label + */ + public $label; - $obj = $this->db->fetch_object($resql); - if (!$obj) { - return 0; - } + /** + * @var int Owner of email template + */ + public $fk_user; - $this->setProperties($obj); + /** + * @var int Is template private + */ + public $private; - return 1; - } + /** + * @var string Model mail topic + */ + public $topic; /** - * API compatible wrapper around fetch. - * - * @param int $id Template id - * @return int + * @var string Model mail content */ - public function apifetch($id) - { - return $this->fetch($id); - } + public $content; + /** + * @var string Model to use to generate the string with each lines + */ + public $content_lines; /** - * Create a new template record. - * - * @param User $user Creator - * @return int + * @var string language of the template */ - public function create($user) - { - $fields = array('entity', 'module', 'type_template', 'label', 'lang', 'private', 'fk_user', 'position', 'active', 'enabled', 'joinfiles', 'email_from', 'email_to', 'email_tocc', 'email_tobcc', 'topic', 'content', 'code', 'subject'); - $columns = array(); - $values = array(); + public $lang; + /** + * @var int<0,1> + */ + public $joinfiles; - foreach ($fields as $field) { - if (!$this->columnExists($field)) { - continue; - } + /** + * @var string sender email address + */ + public $email_from; - $value = $this->$field; - if ($field === 'entity') { - $value = (int) (isset($this->entity) ? $this->entity : $user->entity); - } - if ($field === 'fk_user') { - $value = isset($this->fk_user) ? (int) $this->fk_user : (int) $user->id; - } + /** + * @var string recipient email address + */ + public $email_to; - $columns[] = $field; - $values[] = "'".$this->db->escape($value)."'"; - } + /** + * @var string Additional visible recipients + */ + public $email_tocc; - if ($this->columnExists('datec')) { - $columns[] = 'datec'; - $values[] = $this->db->idate(dol_now()); - } + /** + * @var string additional hidden recipients + */ + public $email_tobcc; - $sql = 'INSERT INTO '.$this->getTableName().' ('.implode(', ', $columns).') VALUES ('.implode(', ', $values).')'; + /** + * @var string Module the template is dedicated for + */ + public $module; - $resql = $this->db->query($sql); - if (!$resql) { - $this->error = $this->db->lasterror(); - return -1; - } + /** + * @var int Position of template in a combo list + */ + public $position; + // END MODULEBUILDER PROPERTIES - $this->id = $this->db->last_insert_id($this->getTableName(), 'rowid'); - $this->rowid = $this->id; - return $this->id > 0 ? $this->id : 1; - } /** - * Build the list of existing columns to select. + * Constructor * - * @return array + * @param DoliDB $db Database handler */ - protected function buildSelectFields() + public function __construct(DoliDB $db) { - $possible = array('rowid', 'entity', 'module', 'type_template', 'label', 'lang', 'private', 'fk_user', 'position', 'active', 'enabled', 'joinfiles', 'email_from', 'email_to', 'email_tocc', 'email_tobcc', 'topic', 'content', 'code', 'subject'); - $fields = array(); + global $langs; + + $this->db = $db; + $this->ismultientitymanaged = 1; + $this->isextrafieldmanaged = 1; - foreach ($possible as $field) { - if ($this->columnExists($field)) { - $fields[] = $field; + // @phan-suppress-next-line PhanTypeMismatchProperty + if (!getDolGlobalInt('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid']) && !empty($this->fields['ref'])) { + $this->fields['rowid']['visible'] = 0; + } + if (!isModEnabled('multicompany') && isset($this->fields['entity'])) { + $this->fields['entity']['enabled'] = 0; + } + + // Example to show how to set values of fields definition dynamically + /*if ($user->hasRight('test', 'mailtemplate', 'read')) { + $this->fields['myfield']['visible'] = 1; + $this->fields['myfield']['noteditable'] = 0; + }*/ + + // Unset fields that are disabled + foreach ($this->fields as $key => $val) { + if (isset($val['enabled']) && empty($val['enabled'])) { + unset($this->fields[$key]); } } - return empty($fields) ? array('*') : $fields; + // Translate some data of arrayofkeyval + if (is_object($langs)) { + foreach ($this->fields as $key => $val) { + if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) { + foreach ($val['arrayofkeyval'] as $key2 => $val2) { + $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2); + } + } + } + } } /** - * Check if a column exists. + * Create email template + * Required fields: label, type_template, topic * - * @param string $column Column name - * @return bool + * @param User $user Object user that make creation + * @param int $notrigger Disable all triggers + * @return int Return integer <0 if KO, >0 if OK */ - protected function columnExists($column) + public function create($user, $notrigger = 0) { - static $cache = array(); - if (isset($cache[$column])) { - return $cache[$column]; + global $conf; + $error = 0; + + dol_syslog(get_class($this)."::create user=".$user->id); + + // Check parameters + if (!empty($this->label)) { // We check that label is not already used + $result = $this->isExistingObject($this->element, 0, $this->label); // Check label is not yet used + if ($result > 0) { + $this->error = 'ErrorLabelAlreadyExists'; + dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING); + $this->db->rollback(); + return -1; + } } - $table = MAIN_DB_PREFIX.'c_email_templates'; - $sql = "SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME='".$this->db->escape($table)."' AND COLUMN_NAME='".$this->db->escape($column)."' LIMIT 1"; - $resql = $this->db->query($sql); - $exists = false; - if ($resql) { - $exists = (bool) $this->db->fetch_object($resql); + $now = dol_now(); + + $this->db->begin(); + + $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (entity,"; + $sql .= " module, type_template, lang, private, fk_user, datec, label,"; + $sql .= " position, defaultfortype, enabled, active, email_from, email_to,"; + $sql .= " email_tocc, email_tobcc, topic, joinfiles, content, content_lines)"; + $sql .= " VALUES ("; + $sql .= " ".((int) $conf->entity).","; + if (is_null($this->module)) { + $sql .= " NULL,"; + } else { + $sql .= " '".$this->db->escape($this->module)."',"; + } + $sql .= " '".$this->db->escape($this->type_template)."',"; + if (is_null($this->lang)) { + $sql .= " NULL,"; + } else { + $sql .= " '".$this->db->escape($this->lang)."',"; + } + $sql .= " ".((int) $this->private).","; + if (is_null($this->fk_user)) { + $sql .= " NULL,"; + } else { + $sql .= " '".((int) $this->fk_user)."',"; + } + if (is_null($this->datec)) { + $sql .= " '".$this->db->idate($now)."',"; + } else { + $sql .= " '".$this->db->idate($this->datec)."',"; + } + $sql .= " '".$this->db->escape($this->label)."',"; + $sql .= " ".((int) $this->position).", ".((int) $this->defaultfortype).","; + if (is_null($this->enabled)) { + $sql .= " 1,"; + } else { + $sql .= " '".((int) $this->enabled)."',"; + } + if (is_null($this->active)) { + $sql .= " 1,"; + } else { + $sql .= " '".((int) $this->active)."',"; + } + if (is_null($this->email_from)) { + $sql .= " NULL,"; + } else { + $sql .= " '".$this->db->escape($this->email_from)."',"; + } + if (is_null($this->email_to)) { + $sql .= " NULL,"; + } else { + $sql .= " '".$this->db->escape($this->email_to)."',"; + } + if (is_null($this->email_tocc)) { + $sql .= " NULL,"; + } else { + $sql .= " '".$this->db->escape($this->email_tocc)."',"; + } + if (is_null($this->email_tobcc)) { + $sql .= " NULL,"; + } else { + $sql .= " '".$this->db->escape($this->email_tobcc)."',"; } + $sql .= " '".$this->db->escape($this->topic)."',"; + $sql .= " ".((int) $this->joinfiles).","; + if (is_null($this->content)) { + $sql .= " NULL,"; + } else { + $sql .= " '".((string) $this->db->escape($this->content))."',"; + } + if (is_null($this->content_lines)) { + $sql .= " NULL"; + } else { + $sql .= " '".((string) $this->db->escape($this->content_lines))."'"; + } + $sql .= ")"; + - $cache[$column] = $exists; + dol_syslog(get_class($this)."::create", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); + + if (!$notrigger) { + // Call trigger + $result = $this->call_trigger(self::TRIGGER_PREFIX.'_CREATE', $user); + if ($result < 0) { + $error++; + } + // End call triggers + } - return $exists; + if (!$error) { + $this->db->commit(); + return $this->id; + } else { + $this->db->rollback(); + return -1 * $error; + } + } else { + $this->error = $this->db->lasterror(); + $this->db->rollback(); + return -1; + } } /** - * Hydrate class properties from database row. + * Update database with changed email template * - * @param stdClass $row Data row - * @return void + * @param User $user User that modify + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int Return integer <0 if KO, >0 if OK */ - protected function setProperties($row) + public function update(User $user, $notrigger = 0) { - foreach ($row as $key => $value) { - $this->$key = $value; + $error = 0; + + // Update request + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql .= " module=".($this->module ? "'".$this->db->escape($this->module)."', " : 'NULL, '); + $sql .= " type_template=".($this->type_template ? "'".$this->db->escape($this->type_template)."', " : 'NULL, '); + $sql .= " lang=".($this->lang ? "'".$this->db->escape($this->lang)."', " : 'NULL, '); + $sql .= " private=".((int) $this->private).","; + $sql .= " fk_user=".((int) $this->fk_user).","; + $sql .= " label=".($this->label ? "'".$this->db->escape($this->label)."', " : 'NULL, '); + $sql .= " position=".((int) $this->position).","; + $sql .= " defaultfortype=".((int) $this->defaultfortype).","; + $sql .= " enabled=".($this->enabled ? "'".$this->db->escape($this->enabled)."', " : 'NULL, '); + $sql .= " active=".((int) $this->active).","; + $sql .= " email_from=".($this->email_from ? "'".$this->db->escape($this->email_from)."', " : 'NULL, '); + $sql .= " email_to=".($this->email_to ? "'".$this->db->escape($this->email_to)."', " : 'NULL, '); + $sql .= " email_tocc=".($this->email_tocc ? "'".$this->db->escape($this->email_tocc)."', " : 'NULL, '); + $sql .= " email_tobcc=".($this->email_tobcc ? "'".$this->db->escape($this->email_tobcc)."', " : 'NULL, '); + $sql .= " topic=".($this->topic ? "'".$this->db->escape($this->topic)."', " : 'NULL, '); + $sql .= " joinfiles=".((int) $this->joinfiles).","; + $sql .= " content=".($this->content ? "'".$this->db->escape($this->content)."', " : 'NULL, '); + $sql .= " content_lines=".($this->content_lines ? "'".$this->db->escape($this->content_lines)."'" : 'NULL'); + $sql .= " WHERE rowid=".((int) $this->id); + + $this->db->begin(); + + dol_syslog(get_class($this)."::update", LOG_DEBUG); + $resql = $this->db->query($sql); + if (!$resql) { + $error++; + $this->errors[] = "Error ".$this->db->lasterror(); } - $this->id = isset($row->rowid) ? $row->rowid : $this->id; - $this->rowid = $this->id; + if (!$error && !$notrigger) { + // Call trigger + $result = $this->call_trigger(self::TRIGGER_PREFIX.'_MODIFY', $user); + if ($result < 0) { + $error++; + } + // End call triggers + } - if (empty($this->subject) && !empty($this->topic)) { - $this->subject = $this->topic; + // Commit or rollback + if ($error) { + foreach ($this->errors as $errmsg) { + dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } else { + $this->db->commit(); + return 1; } } /** - * Get the table name with prefix. + * Delete the email template * - * @return string + * @param User $user User object + * @param int $notrigger 1=Does not execute triggers, 0= execute triggers + * @return int Return integer <=0 if KO, >0 if OK */ - protected function getTableName() + public function delete($user, $notrigger = 0) { - return MAIN_DB_PREFIX.'c_email_templates'; - } -} + $error = 0; -/** - * Backport of the e-mail templates collection class. - */ -class CEmailTemplates extends CEmailTemplate -{ - /** - * Fetch a template by trigger code. - * - * @param string $action Trigger code - * @param User $user Current user - * @param int|null $entity Entity id - * @return int - */ - public function fetchByTrigger($action, $user = null, $entity = null) - { - $fields = $this->buildSelectFields(); - $where = array(); - $entityId = is_null($entity) ? 1 : (int) $entity; + dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG); - if ($this->columnExists('code')) { - $where[] = "code='".$this->db->escape($action)."'"; - } else { - $where[] = "label='".$this->db->escape($action)."'"; - } + $this->db->begin(); - if ($this->columnExists('entity')) { - $where[] = 'entity IN ('.$entityId.', 0)'; + if (!$notrigger) { + // Call trigger + $result = $this->call_trigger(self::TRIGGER_PREFIX.'_DELETE', $user); + if ($result < 0) { + $error++; + } + // End call triggers } - $sql = 'SELECT '.implode(', ', $fields).' FROM '.$this->getTableName(); - if (!empty($where)) { - $sql .= ' WHERE '.implode(' AND ', $where); + // Delete object link + if (!$error) { + $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id); + $res = $this->db->query($sql); + if (!$res) { + $error++; + $this->error = $this->db->lasterror(); + $this->errors[] = $this->error; + dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR); + } } - $sql .= ' ORDER BY entity DESC'; - $sql .= $this->columnExists('position') ? ' , position ASC' : ''; - $sql .= ' LIMIT 1'; - $resql = $this->db->query($sql); - if (!$resql) { - $this->error = $this->db->lasterror(); + if (!$error) { + dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG); + $this->db->commit(); + return 1; + } else { + $this->db->rollback(); return -1; } - - $row = $this->db->fetch_object($resql); - if (!$row) { - return 0; - } - - $this->setProperties($row); - - return 1; } /** - * Fetch all templates according to filters. + * Load object in memory from the database * - * @param string $sortorder Sort order - * @param string $sortfield Sort field - * @param int $limit Limit - * @param int $offset Offset - * @param array $filter Filter array - * @return array|int + * @param int $id Id object + * @param string $label Ref + * @param int $noextrafields 0=Default to load extrafields, 1=No extrafields + * @param int $nolines 0=Default to load extrafields, 1=No extrafields + * @return int Return integer <0 if KO, 0 if not found, >0 if OK */ - public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array()) + public function fetch($id, $label = null, $noextrafields = 0, $nolines = 0) { - $fields = $this->buildSelectFields(); - $sql = 'SELECT '.implode(', ', $fields).' FROM '.$this->getTableName(); - - $where = array(); - if (!empty($filter['entity']) && $this->columnExists('entity')) { - $where[] = 'entity IN ('.implode(',', array_map('intval', (array) $filter['entity'])).')'; - } - if (!empty($filter['type_template']) && $this->columnExists('type_template')) { - $where[] = "type_template='".$this->db->escape($filter['type_template'])."'"; - } - if (!empty($filter['module']) && $this->columnExists('module')) { - $where[] = "module='".$this->db->escape($filter['module'])."'"; - } - if (isset($filter['active']) && $this->columnExists('active')) { - $where[] = 'active='.(int) $filter['active']; - } - if (isset($filter['enabled']) && $this->columnExists('enabled')) { - $where[] = 'enabled='.(int) $filter['enabled']; - } + // The table llx_c_email_templates has no field ref. The field ref was named "label" instead. So we change the call to fetchCommon. + //$result = $this->fetchCommon($id, $label, '', $noextrafields); + $result = $this->fetchCommon($id, '', " AND t.label = '".$this->db->escape($label)."'", $noextrafields); - if (!empty($where)) { - $sql .= ' WHERE '.implode(' AND ', $where); + if ($result > 0 && !empty($this->table_element_line) && empty($nolines)) { + $this->fetchLines($noextrafields); } + return $result; + } - if (!empty($sortfield) && $this->columnExists($sortfield)) { - $order = !empty($sortorder) ? $sortorder : 'ASC'; - $sql .= ' ORDER BY '.$this->db->escape($sortfield).' '.$this->db->escape($order); + /** + * Get email template from database. + * + * @param int $id row Id of email template + * @param string $label label of email template + * @return int >0 if OK, <0 if KO, 0 if not found + */ + public function apifetch($id, $label = '') + { + // Check parameters + if (($id == 0 || empty($id)) && empty($label)) { + dol_syslog(get_class($this)."::apifetch id and label are empty", LOG_DEBUG); + $this->error = 'id='.$id.' and label are empty'; + return -1; } - if ((int) $limit > 0) { - $sql .= ' '.$this->db->plimit((int) $limit, (int) $offset); + $sql = "SELECT e.rowid, e.entity, e.module, e.type_template, e.lang,"; + $sql .= " e.private, e.fk_user, e.datec, e.tms, e.label, e.position,"; + $sql .= " e.defaultfortype, e.enabled, e.active, e.email_from, e.email_to,"; + $sql .= " e.email_tocc, e.email_tobcc, e.topic, e.joinfiles, e.content,"; + $sql .= " e.content_lines FROM ".$this->db->prefix().$this->table_element." as e"; + if ($id) { + $sql .= " WHERE e.rowid = ".((int) $id); + } else { + $sql .= " WHERE e.entity IN (".getEntity($this->table_element).")"; + if ($label) { + $sql .= " AND e.label = '".$this->db->escape($label)."'"; + } } - $resql = $this->db->query($sql); - if (!$resql) { - $this->error = $this->db->lasterror(); + dol_syslog(get_class($this)."::apifetch", LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) { + $obj = $this->db->fetch_object($result); + if ($obj) { + $this->id = (int) $obj->rowid; + $this->entity = (int) $obj->entity; + + $this->active = (int) $obj->active; + $this->content = (string) $obj->content; + $this->content_lines = (string) $obj->content_lines; + $this->datec = $this->db->jdate($obj->datec); + $this->defaultfortype = (int) $obj->defaultfortype; + $this->email_from = (string) $obj->email_from; + $this->email_to = (string) $obj->email_to; + $this->email_tobcc = (string) $obj->email_tobcc; + $this->email_tocc = (string) $obj->email_tocc; + $this->enabled = (string) $obj->enabled; + $this->fk_user = (int) $obj->fk_user; + $this->joinfiles = (int) $obj->joinfiles; + $this->label = (string) $obj->label; + $this->lang = (string) $obj->lang; + $this->module = (string) $obj->module; + $this->position = (int) $obj->position; + $this->private = (int) $obj->private; + $this->tms = $this->db->jdate($obj->tms); + $this->topic = (string) $obj->topic; + $this->type_template = (string) $obj->type_template; + + // direct copy from facture.class.php + $this->date_creation = $this->db->jdate($obj->datec); + + return 1; + } else { + if ($id) { + $this->error = 'Email template with id '.((string) $id).' not found sql='.$sql; + } elseif ($label) { + $this->error = 'Email template with label '.$label.' not found sql='.$sql; + } + return 0; + } + } else { + $this->error = $this->db->error(); return -1; } - - $templates = array(); - while ($row = $this->db->fetch_object($resql)) { - $template = new CEmailTemplate($this->db); - $template->setProperties($row); - $templates[] = $template; - } - - return $templates; } } + +/** + * Old class name for Object of table llx_c_email_templates + * I prefer the CEmailTemplate name as it better reflects the database + * + * @deprecated Use now class CEmailTemplate + */ +class ModelMail extends CEmailTemplate +{ + // just another name for compatibility +} From 8f67b658d5eb1fe2d51b076b815c79dc39ce7c6d Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 13:06:46 +0100 Subject: [PATCH 09/11] Update setup.php --- admin/setup.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/setup.php b/admin/setup.php index 541baf0..e317b5a 100644 --- a/admin/setup.php +++ b/admin/setup.php @@ -41,7 +41,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; // EN: Load email template class with backward compatibility for older Dolibarr versions. -if (version_compare(DOL_VERSION, '23.0.0', '<')) { +if (floatval(DOL_VERSION) < 23)) { dol_include_once('/timesheetweek/core/class/cemailtemplate.class.php'); } else { require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php'; From 8b98626c4744490738c6c1cc03ada913fb8b3b8f Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 13:07:10 +0100 Subject: [PATCH 10/11] Update setup.php --- admin/setup.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/setup.php b/admin/setup.php index e317b5a..afa4118 100644 --- a/admin/setup.php +++ b/admin/setup.php @@ -41,7 +41,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; // EN: Load email template class with backward compatibility for older Dolibarr versions. -if (floatval(DOL_VERSION) < 23)) { +if (floatval(DOL_VERSION) < 23) { dol_include_once('/timesheetweek/core/class/cemailtemplate.class.php'); } else { require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php'; From 25682e2c7966c8204bdc448b1df3b87808b437e7 Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Fri, 5 Dec 2025 13:09:59 +0100 Subject: [PATCH 11/11] Update timesheetweek_reminder.class.php --- class/timesheetweek_reminder.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class/timesheetweek_reminder.class.php b/class/timesheetweek_reminder.class.php index 6b34817..9c9d47e 100644 --- a/class/timesheetweek_reminder.class.php +++ b/class/timesheetweek_reminder.class.php @@ -95,7 +95,7 @@ public function run($dbInstance = null, $limit = 0, $forcerun = 0, array $target } } - if (version_compare(DOL_VERSION, '23.0.0', '<')) { + if (floatval(DOL_VERSION) < 23) { dol_include_once('/timesheetweek/core/class/cemailtemplate.class.php'); } else { require_once DOL_DOCUMENT_ROOT.'/core/class/cemailtemplate.class.php';