dolibarr 24.0.0-beta
cemailtemplate.class.php
1<?php
2/* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2015-2017 Marcos García <marcosgdf@gmail.com>
6 * Copyright (C) 2015-2017 Nicolas ZABOURI <info@inovea-conseil.com>
7 * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2022 Charlene Benke <charlene@patas-monkey.com>
9 * Copyright (C) 2023 Anthony Berton <anthony.berton@bb2a.fr>
10 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
11 * Copyright (C) 2025 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26require_once DOL_DOCUMENT_ROOT.'/core/class/doldeprecationhandler.class.php';
27
28
33{
34 const TRIGGER_PREFIX = 'EMAILTEMPLATE';
38 public $element = 'email_template';
39
43 public $table_element = 'c_email_templates';
44
45
46 // BEGIN MODULEBUILDER PROPERTIES
50 public $fields = array(
51 "rowid" => array("type" => "integer", "label" => "TechnicalID", 'enabled' => 1, 'position' => 10, 'notnull' => 1, 'visible' => -1,),
52 "module" => array("type" => "varchar(32)", "label" => "Module", 'enabled' => 1, 'position' => 20, 'notnull' => 0, 'visible' => -1,),
53 "type_template" => array("type" => "varchar(32)", "label" => "Typetemplate", 'enabled' => 1, 'position' => 25, 'notnull' => 0, 'visible' => -1,),
54 "lang" => array("type" => "varchar(6)", "label" => "Lang", 'enabled' => 1, 'position' => 30, 'notnull' => 0, 'visible' => -1,),
55 "private" => array("type" => "smallint(6)", "label" => "Private", 'enabled' => 1, 'position' => 35, 'notnull' => 1, 'visible' => -1,),
56 "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",),
57 "datec" => array("type" => "datetime", "label" => "DateCreation", 'enabled' => 1, 'position' => 45, 'notnull' => 0, 'visible' => -1,),
58 "tms" => array("type" => "timestamp", "label" => "DateModification", 'enabled' => 1, 'position' => 50, 'notnull' => 1, 'visible' => -1,),
59 "label" => array("type" => "varchar(255)", "label" => "Label", 'enabled' => 1, 'position' => 55, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1, "css" => "minwidth300", "cssview" => "wordbreak", "csslist" => "tdoverflowmax150",),
60 "position" => array("type" => "smallint(6)", "label" => "Position", 'enabled' => 1, 'position' => 60, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
61 "active" => array("type" => "integer", "label" => "Active", 'enabled' => 1, 'position' => 65, 'notnull' => 1, 'visible' => -1, 'alwayseditable' => 1,),
62 "topic" => array("type" => "text", "label" => "Topic", 'enabled' => 1, 'position' => 70, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
63 "content" => array("type" => "mediumtext", "label" => "Content", 'enabled' => 1, 'position' => 75, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
64 "content_lines" => array("type" => "text", "label" => "Contentlines", "enabled" => "getDolGlobalString('MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES')", 'position' => 80, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
65 "enabled" => array("type" => "varchar(255)", "label" => "Enabled", 'enabled' => 1, 'position' => 85, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
66 "joinfiles" => array("type" => "varchar(255)", "label" => "Joinfiles", 'enabled' => 1, 'position' => 90, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
67 "email_from" => array("type" => "varchar(255)", "label" => "Emailfrom", 'enabled' => 1, 'position' => 95, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
68 "email_to" => array("type" => "varchar(255)", "label" => "Emailto", 'enabled' => 1, 'position' => 100, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
69 "email_tocc" => array("type" => "varchar(255)", "label" => "Emailtocc", 'enabled' => 1, 'position' => 105, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
70 "email_tobcc" => array("type" => "varchar(255)", "label" => "Emailtobcc", 'enabled' => 1, 'position' => 110, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
71 "defaultfortype" => array("type" => "smallint(6)", "label" => "Defaultfortype", 'enabled' => 1, 'position' => 115, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
72 );
76 public $rowid;
80 public $type_template;
84 public $datec;
88 public $tms;
92 public $active;
96 public $enabled;
100 public $defaultfortype;
101
105 public $id;
106
110 public $label;
111
115 public $fk_user;
116
120 public $private;
121
125 public $topic;
126
130 public $content;
134 public $content_lines;
135
139 public $lang;
143 public $joinfiles;
144
148 public $email_from;
149
153 public $email_to;
154
158 public $email_tocc;
159
163 public $email_tobcc;
164
168 public $module;
169
173 public $position;
174 // END MODULEBUILDER PROPERTIES
175
176
177
183 public function __construct(DoliDB $db)
184 {
185 global $langs;
186
187 $this->db = $db;
188 $this->ismultientitymanaged = 1;
189 $this->isextrafieldmanaged = 1;
190
191 // @phan-suppress-next-line PhanTypeMismatchProperty
192 if (!getDolGlobalInt('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid']) && !empty($this->fields['ref'])) {
193 $this->fields['rowid']['visible'] = 0;
194 }
195 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
196 $this->fields['entity']['enabled'] = 0;
197 }
198
199 // Example to show how to set values of fields definition dynamically
200 /*if ($user->hasRight('test', 'mailtemplate', 'read')) {
201 $this->fields['myfield']['visible'] = 1;
202 $this->fields['myfield']['noteditable'] = 0;
203 }*/
204
205 // Unset fields that are disabled
206 foreach ($this->fields as $key => $val) {
207 if (isset($val['enabled']) && empty($val['enabled'])) {
208 unset($this->fields[$key]);
209 }
210 }
211
212 // Translate some data of arrayofkeyval
213 if (is_object($langs)) {
214 foreach ($this->fields as $key => $val) {
215 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
216 foreach ($val['arrayofkeyval'] as $key2 => $val2) {
217 $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
218 }
219 }
220 }
221 }
222 }
223
232 public function create($user, $notrigger = 0)
233 {
234 global $conf;
235 $error = 0;
236
237 dol_syslog(get_class($this)."::create user=".$user->id);
238
239 // Check parameters
240 if (!empty($this->label)) { // We check that label is not already used
241 $result = $this->isExistingObject($this->element, 0, $this->label); // Check label is not yet used
242 if ($result > 0) {
243 $this->error = 'ErrorLabelAlreadyExists';
244 dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
245 $this->db->rollback();
246 return -1;
247 }
248 }
249
250 $now = dol_now();
251
252 $this->db->begin();
253
254 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (entity,";
255 $sql .= " module, type_template, lang, private, fk_user, datec, label,";
256 $sql .= " position, defaultfortype, enabled, active, email_from, email_to,";
257 $sql .= " email_tocc, email_tobcc, topic, joinfiles, content, content_lines)";
258 $sql .= " VALUES (";
259 $sql .= " ".((int) $conf->entity).",";
260 if (is_null($this->module)) {
261 $sql .= " NULL,";
262 } else {
263 $sql .= " '".$this->db->escape($this->module)."',";
264 }
265 $sql .= " '".$this->db->escape($this->type_template)."',";
266 if (is_null($this->lang)) {
267 $sql .= " NULL,";
268 } else {
269 $sql .= " '".$this->db->escape($this->lang)."',";
270 }
271 $sql .= " ".((int) $this->private).",";
272 if (is_null($this->fk_user)) {
273 $sql .= " NULL,";
274 } else {
275 $sql .= " '".((int) $this->fk_user)."',";
276 }
277 if (is_null($this->datec)) {
278 $sql .= " '".$this->db->idate($now)."',";
279 } else {
280 $sql .= " '".$this->db->idate($this->datec)."',";
281 }
282 $sql .= " '".$this->db->escape($this->label)."',";
283 $sql .= " ".((int) $this->position).", ".((int) $this->defaultfortype).",";
284 if (is_null($this->enabled)) {
285 $sql .= " 1,";
286 } else {
287 $sql .= " '".((int) $this->enabled)."',";
288 }
289 if (is_null($this->active)) {
290 $sql .= " 1,";
291 } else {
292 $sql .= " '".((int) $this->active)."',";
293 }
294 if (is_null($this->email_from)) {
295 $sql .= " NULL,";
296 } else {
297 $sql .= " '".$this->db->escape($this->email_from)."',";
298 }
299 if (is_null($this->email_to)) {
300 $sql .= " NULL,";
301 } else {
302 $sql .= " '".$this->db->escape($this->email_to)."',";
303 }
304 if (is_null($this->email_tocc)) {
305 $sql .= " NULL,";
306 } else {
307 $sql .= " '".$this->db->escape($this->email_tocc)."',";
308 }
309 if (is_null($this->email_tobcc)) {
310 $sql .= " NULL,";
311 } else {
312 $sql .= " '".$this->db->escape($this->email_tobcc)."',";
313 }
314 $sql .= " '".$this->db->escape($this->topic)."',";
315 $sql .= " ".((int) $this->joinfiles).",";
316 if (is_null($this->content)) {
317 $sql .= " NULL,";
318 } else {
319 $sql .= " '".((string) $this->db->escape($this->content))."',";
320 }
321 if (is_null($this->content_lines)) {
322 $sql .= " NULL";
323 } else {
324 $sql .= " '".((string) $this->db->escape($this->content_lines))."'";
325 }
326 $sql .= ")";
327
328
329 dol_syslog(get_class($this)."::create", LOG_DEBUG);
330 $resql = $this->db->query($sql);
331 if ($resql) {
332 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
333
334 if (!$notrigger) {
335 // Call trigger
336 $result = $this->call_trigger(self::TRIGGER_PREFIX.'_CREATE', $user);
337 if ($result < 0) {
338 $error++;
339 }
340 // End call triggers
341 }
342
343 if (!$error) {
344 $this->db->commit();
345 return $this->id;
346 } else {
347 $this->db->rollback();
348 return -1 * $error;
349 }
350 } else {
351 $this->error = $this->db->lasterror();
352 $this->db->rollback();
353 return -1;
354 }
355 }
356
364 public function update(User $user, $notrigger = 0)
365 {
366 $error = 0;
367
368 // Update request
369 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
370 $sql .= " module=".($this->module ? "'".$this->db->escape($this->module)."', " : 'NULL, ');
371 $sql .= " type_template=".($this->type_template ? "'".$this->db->escape($this->type_template)."', " : 'NULL, ');
372 $sql .= " lang=".($this->lang ? "'".$this->db->escape($this->lang)."', " : 'NULL, ');
373 $sql .= " private=".((int) $this->private).",";
374 $sql .= " fk_user=".((int) $this->fk_user).",";
375 $sql .= " label=".($this->label ? "'".$this->db->escape($this->label)."', " : 'NULL, ');
376 $sql .= " position=".((int) $this->position).",";
377 $sql .= " defaultfortype=".((int) $this->defaultfortype).",";
378 $sql .= " enabled=".($this->enabled ? "'".$this->db->escape($this->enabled)."', " : 'NULL, ');
379 $sql .= " active=".((int) $this->active).",";
380 $sql .= " email_from=".($this->email_from ? "'".$this->db->escape($this->email_from)."', " : 'NULL, ');
381 $sql .= " email_to=".($this->email_to ? "'".$this->db->escape($this->email_to)."', " : 'NULL, ');
382 $sql .= " email_tocc=".($this->email_tocc ? "'".$this->db->escape($this->email_tocc)."', " : 'NULL, ');
383 $sql .= " email_tobcc=".($this->email_tobcc ? "'".$this->db->escape($this->email_tobcc)."', " : 'NULL, ');
384 $sql .= " topic=".($this->topic ? "'".$this->db->escape($this->topic)."', " : 'NULL, ');
385 $sql .= " joinfiles=".((int) $this->joinfiles).",";
386 $sql .= " content=".($this->content ? "'".$this->db->escape($this->content)."', " : 'NULL, ');
387 $sql .= " content_lines=".($this->content_lines ? "'".$this->db->escape($this->content_lines)."'" : 'NULL');
388 $sql .= " WHERE rowid=".((int) $this->id);
389
390 $this->db->begin();
391
392 dol_syslog(get_class($this)."::update", LOG_DEBUG);
393 $resql = $this->db->query($sql);
394 if (!$resql) {
395 $error++;
396 $this->errors[] = "Error ".$this->db->lasterror();
397 }
398
399 if (!$error && !$notrigger) {
400 // Call trigger
401 $result = $this->call_trigger(self::TRIGGER_PREFIX.'_MODIFY', $user);
402 if ($result < 0) {
403 $error++;
404 }
405 // End call triggers
406 }
407
408 // Commit or rollback
409 if ($error) {
410 foreach ($this->errors as $errmsg) {
411 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
412 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
413 }
414 $this->db->rollback();
415 return -1 * $error;
416 } else {
417 $this->db->commit();
418 return 1;
419 }
420 }
421
429 public function delete($user, $notrigger = 0)
430 {
431 $error = 0;
432
433 dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
434
435 $this->db->begin();
436
437 if (!$notrigger) {
438 // Call trigger
439 $result = $this->call_trigger(self::TRIGGER_PREFIX.'_DELETE', $user);
440 if ($result < 0) {
441 $error++;
442 }
443 // End call triggers
444 }
445
446 // Delete object link
447 if (!$error) {
448 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id);
449 $res = $this->db->query($sql);
450 if (!$res) {
451 $error++;
452 $this->error = $this->db->lasterror();
453 $this->errors[] = $this->error;
454 dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
455 }
456 }
457
458 if (!$error) {
459 dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
460 $this->db->commit();
461 return 1;
462 } else {
463 $this->db->rollback();
464 return -1;
465 }
466 }
467
477 public function fetch($id, $label = null, $noextrafields = 0, $nolines = 0)
478 {
479 // The table llx_c_email_templates has no field ref. The field ref was named "label" instead. So we change the call to fetchCommon.
480 $result = $this->fetchCommon($id, '', ((!$id || $label) ? " AND t.label = '".$this->db->escape($label)."'" : ''), $noextrafields);
481
482 if ($result > 0 && !empty($this->table_element_line) && empty($nolines)) {
483 $this->fetchLines($noextrafields);
484 }
485 return $result;
486 }
487
495 public function apifetch($id, $label = '')
496 {
497 // Check parameters
498 if (($id == 0 || empty($id)) && empty($label)) {
499 dol_syslog(get_class($this)."::apifetch id and label are empty", LOG_DEBUG);
500 $this->error = 'id='.$id.' and label are empty';
501 return -1;
502 }
503
504 $sql = "SELECT e.rowid, e.entity, e.module, e.type_template, e.lang,";
505 $sql .= " e.private, e.fk_user, e.datec, e.tms, e.label, e.position,";
506 $sql .= " e.defaultfortype, e.enabled, e.active, e.email_from, e.email_to,";
507 $sql .= " e.email_tocc, e.email_tobcc, e.topic, e.joinfiles, e.content,";
508 $sql .= " e.content_lines FROM ".$this->db->prefix().$this->table_element." as e";
509 if ($id) {
510 $sql .= " WHERE e.rowid = ".((int) $id);
511 } else {
512 $sql .= " WHERE e.entity IN (".getEntity($this->table_element).")";
513 if ($label) {
514 $sql .= " AND e.label = '".$this->db->escape($label)."'";
515 }
516 }
517
518 dol_syslog(get_class($this)."::apifetch", LOG_DEBUG);
519 $result = $this->db->query($sql);
520 if ($result) {
521 $obj = $this->db->fetch_object($result);
522 if ($obj) {
523 $this->id = (int) $obj->rowid;
524 $this->entity = (int) $obj->entity;
525
526 $this->active = (int) $obj->active;
527 $this->content = (string) $obj->content;
528 $this->content_lines = (string) $obj->content_lines;
529 $this->datec = $this->db->jdate($obj->datec);
530 $this->defaultfortype = (int) $obj->defaultfortype;
531 $this->email_from = (string) $obj->email_from;
532 $this->email_to = (string) $obj->email_to;
533 $this->email_tobcc = (string) $obj->email_tobcc;
534 $this->email_tocc = (string) $obj->email_tocc;
535 $this->enabled = (string) $obj->enabled;
536 $this->fk_user = (int) $obj->fk_user;
537 $this->joinfiles = (int) $obj->joinfiles;
538 $this->label = (string) $obj->label;
539 $this->lang = (string) $obj->lang;
540 $this->module = (string) $obj->module;
541 $this->position = (int) $obj->position;
542 $this->private = (int) $obj->private;
543 $this->tms = $this->db->jdate($obj->tms);
544 $this->topic = (string) $obj->topic;
545 $this->type_template = (string) $obj->type_template;
546
547 // direct copy from facture.class.php
548 $this->date_creation = $this->db->jdate($obj->datec);
549
550 return 1;
551 } else {
552 if ($id) {
553 $this->error = 'Email template with id '.((string) $id).' not found sql='.$sql;
554 } elseif ($label) {
555 $this->error = 'Email template with label '.$label.' not found sql='.$sql;
556 }
557 return 0;
558 }
559 } else {
560 $this->error = $this->db->error();
561 return -1;
562 }
563 }
564}
565
573{
574 // just another name for compatibility
575}
print $object position
Definition edit.php:206
Object of table llx_c_email_templates.
__construct(DoliDB $db)
Constructor.
create($user, $notrigger=0)
Create email template Required fields: label, type_template, topic.
apifetch($id, $label='')
Get email template from database.
fetch($id, $label=null, $noextrafields=0, $nolines=0)
Load object in memory from the database.
update(User $user, $notrigger=0)
Update database with changed email template.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
static isExistingObject($element, $id, $ref='', $ref_ext='')
Check if an object id or ref exists If you don't need or want to instantiate the object and just need...
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
Class to manage Dolibarr database access.
Old class name for Object of table llx_c_email_templates I prefer the CEmailTemplate name as it bette...
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
editval_textarea active
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php