Skip to content

Commit df4cd2f

Browse files
JonBendtsenJon Bendtsen
andauthored
FIX adding tms and datec to email templates (#36189)
* NEW: adding tms and datec to email templates * Escaping a timestamp string I just generated myself * switching single and double quotes in escape line for tms and datec to make the build system happy? * Add an id to the table * showing better names than tms and datec * Changing code to label to fix #29116 * using hregis advice to use idate not escape the string with date * requested changes * forgot to add my name to editors * had to make class changes for API to work * setting datec back to int|string * removing unused code lines and using idate in the api file * no tms in create or update, but select. No datec in update * expanding hurl tests to prevent post with id or tms, put with id or datec --------- Co-authored-by: Jon Bendtsen <[email protected]>
1 parent 538baec commit df4cd2f

File tree

4 files changed

+82
-22
lines changed

4 files changed

+82
-22
lines changed

htdocs/admin/mails_templates.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Copyright (C) 2018-2024 Frédéric France <[email protected]>
1515
* Copyright (C) 2024-2025 MDW <[email protected]>
1616
* Copyright (C) 2025 Vincent Maury <[email protected]>
17+
* Copyright (C) 2025 Jon Bendtsen <[email protected]>
1718
*
1819
* This program is free software; you can redistribute it and/or modify
1920
* it under the terms of the GNU General Public License as published by
@@ -120,22 +121,25 @@
120121
$tabname[25] = MAIN_DB_PREFIX."c_email_templates";
121122

122123
// Nom des champs en resultat de select pour affichage du dictionnaire
124+
// Names of fields in select results for dictionary display (AI translated)
123125
$tabfield = array();
124126
$tabfield[25] = "label,lang,type_template,fk_user,private,position,module,topic,joinfiles,defaultfortype,content";
125127
if (getDolGlobalString('MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES')) {
126128
$tabfield[25] .= ',content_lines';
127129
}
128130

129131
// Nom des champs d'edition pour modification d'un enregistrement
132+
// Names of edit fields for modifying a record (AI translated)
130133
$tabfieldvalue = array();
131134
$tabfieldvalue[25] = "label,lang,type_template,fk_user,private,position,topic,email_from,joinfiles,defaultfortype,content";
132135
if (getDolGlobalString('MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES')) {
133136
$tabfieldvalue[25] .= ',content_lines';
134137
}
135138

136139
// Nom des champs dans la table pour insertion d'un enregistrement
140+
// Field names in the table for inserting a record (AI translated)
137141
$tabfieldinsert = array();
138-
$tabfieldinsert[25] = "label,lang,type_template,fk_user,private,position,topic,email_from,joinfiles,defaultfortype,content";
142+
$tabfieldinsert[25] = "label,lang,type_template,fk_user,private,position,topic,email_from,joinfiles,defaultfortype,content,datec";
139143
if (getDolGlobalString('MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES')) {
140144
$tabfieldinsert[25] .= ',content_lines';
141145
}
@@ -338,14 +342,14 @@
338342
if ((GETPOST('actionadd', 'alpha') && $permissiontoadd) || (GETPOST('actionmodify', 'alpha') && $permissiontoedit)) {
339343
$listfield = explode(',', str_replace(' ', '', $tabfield[25]));
340344
$listfieldinsert = explode(',', $tabfieldinsert[25]);
341-
$listfieldmodify = explode(',', $tabfieldinsert[25]);
345+
$listfieldmodify = explode(',', $tabfieldvalue[25]);
342346
$listfieldvalue = explode(',', $tabfieldvalue[25]);
343347

344348
// Check that all fields are filled
345349
$ok = 1;
346350
foreach ($listfield as $f => $value) {
347351
// Not mandatory fields
348-
if (in_array($value, ['joinfiles', 'defaultfortype', 'content', 'content_lines', 'module'])) {
352+
if (in_array($value, ['joinfiles', 'defaultfortype', 'content', 'content_lines', 'module', 'tms', 'datec'])) {
349353
continue;
350354
}
351355

@@ -399,6 +403,7 @@
399403

400404
// List of values
401405
$i = 0;
406+
$now = dol_now();
402407
foreach ($listfieldinsert as $f => $value) {
403408
$keycode = isset($listfieldvalue[$i]) ? $listfieldvalue[$i] : "";
404409
if ($value == 'lang') {
@@ -429,7 +434,9 @@
429434
if ($i) {
430435
$sql .= ", ";
431436
}
432-
if (GETPOST($keycode) == '' && $keycode != 'langcode') {
437+
if ($keycode == 'datec') {
438+
$sql .= "'".$db->idate($now)."'";
439+
} elseif (GETPOST($keycode) == '' && $keycode != 'langcode') {
433440
$sql .= "null"; // langcode must be '' if not defined so the unique key that include lang will work
434441
} elseif (GETPOST($keycode) == '0' && $keycode == 'langcode') {
435442
$sql .= "''"; // langcode must be '' if not defined so the unique key that include lang will work
@@ -631,7 +638,7 @@
631638
$morejs = array();
632639
$morecss = array();
633640

634-
$sql = "SELECT rowid as rowid, module, label, type_template, lang, fk_user, private, position, topic, email_from,joinfiles, defaultfortype, content_lines, content, enabled, active";
641+
$sql = "SELECT rowid as rowid, module, label, type_template, lang, fk_user, private, position, topic, email_from,joinfiles, defaultfortype, content_lines, content, enabled, active, tms, datec";
635642
$sql .= " FROM ".MAIN_DB_PREFIX."c_email_templates";
636643
$sql .= " WHERE entity IN (".getEntity('email_template').")";
637644
if (!$user->admin) {
@@ -757,14 +764,14 @@
757764
$obj->content = GETPOST('content', 'restricthtml');
758765

759766
// Form to add a new line
760-
print '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
767+
print '<form action="'.$_SERVER['PHP_SELF'].'" method="POST" id="create_c_email_template">';
761768
print '<input type="hidden" name="token" value="'.newToken().'">';
762769
print '<input type="hidden" name="action" value="add">';
763770
print '<input type="hidden" name="from" value="'.dol_escape_htmltag(GETPOST('from', 'alpha')).'">';
764771
print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
765772

766773
print '<div class="div-table-responsive-no-min">';
767-
print '<table class="noborder centpercent">';
774+
print '<table class="noborder centpercent" id="table_create_c_email_template">';
768775

769776
// Line to enter new values (title)
770777
print '<tr class="liste_titre">';
@@ -793,7 +800,7 @@
793800
$valuetoshow = $langs->trans("Code");
794801
}
795802
if ($fieldlist[$field] == 'label') {
796-
$valuetoshow = $langs->trans("Code");
803+
$valuetoshow = $langs->trans("Label");
797804
}
798805
if ($fieldlist[$field] == 'type_template') {
799806
$valuetoshow = $langs->trans("TypeOfTemplate");
@@ -932,12 +939,12 @@
932939

933940
$num = $db->num_rows($resql);
934941

935-
print '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
942+
print '<form action="'.$_SERVER['PHP_SELF'].'" method="POST" id="list_of_c_email_templates">';
936943
print '<input type="hidden" name="token" value="'.newToken().'">';
937944
print '<input type="hidden" name="from" value="'.dol_escape_htmltag(GETPOST('from', 'alpha')).'">';
938945

939946
print '<div class="div-table-responsive-no-min">';
940-
print '<table class="noborder centpercent">';
947+
print '<table class="noborder centpercent" id="table_list_of_c_email_templates">';
941948

942949
$i = 0;
943950

@@ -981,7 +988,7 @@
981988

982989

983990
// Title line with search boxes
984-
print '<tr class="liste_titre">';
991+
print '<tr class="liste_titre" id="Title line with search boxes">';
985992
// Action column
986993
if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
987994
print '<td class="liste_titre center" width="64">';
@@ -1018,6 +1025,9 @@
10181025
}*/
10191026
// Status
10201027
print '<td></td>';
1028+
// Have to expand the id="Title line with search boxes" with 2 extra fields because the line below id="Title of lines" are 2 fields longer
1029+
print '<td></td>'; // tms / Modif. date
1030+
print '<td></td>'; // datec / Date creation
10211031
// Action column
10221032
if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
10231033
print '<td class="liste_titre center" width="64">';
@@ -1028,11 +1038,12 @@
10281038
print '</tr>';
10291039

10301040
// Title of lines
1031-
print '<tr class="liste_titre">';
1041+
print '<tr class="liste_titre" id="Title of lines">';
10321042
// Action column
10331043
if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
10341044
print getTitleFieldOfList('');
10351045
}
1046+
array_push($fieldlist, "tms", "datec");
10361047
foreach ($fieldlist as $field => $value) {
10371048
$showfield = 1; // By default
10381049
$css = "left";
@@ -1061,7 +1072,7 @@
10611072
$valuetoshow = $langs->trans("Type");
10621073
}
10631074
if ($fieldlist[$field] == 'libelle' || $fieldlist[$field] == 'label') {
1064-
$valuetoshow = $langs->trans("Code");
1075+
$valuetoshow = $langs->trans("Label");
10651076
}
10661077
if ($fieldlist[$field] == 'type_template') {
10671078
$css = 'center';
@@ -1073,6 +1084,12 @@
10731084
if ($fieldlist[$field] == 'position') {
10741085
$css = 'center';
10751086
}
1087+
if ($fieldlist[$field] == 'tms') {
1088+
$valuetoshow = 'Modif. date';
1089+
}
1090+
if ($fieldlist[$field] == 'datec') {
1091+
$valuetoshow = 'Date creation';
1092+
}
10761093

10771094
if ($fieldlist[$field] == 'joinfiles') {
10781095
$valuetoshow = $langs->trans("FilesAttachedToEmail");
@@ -1123,7 +1140,13 @@
11231140
print '<tr class="nohover oddeven" id="rowid-'.$obj->rowid.'">';
11241141

11251142
$tmpaction = 'edit';
1126-
$parameters = array('fieldlist' => $fieldlist, 'tabname' => $tabname[25]);
1143+
if ($action == 'edit') {
1144+
// do not show tms and datec
1145+
$fieldlist = explode(',', $tabfield[25]);
1146+
$parameters = array('fieldlist' => $fieldlist, 'tabname' => $tabname[25]);
1147+
} else {
1148+
$parameters = array('fieldlist' => $fieldlist, 'tabname' => $tabname[25]);
1149+
}
11271150
$reshook = $hookmanager->executeHooks('editEmailTemplateFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
11281151
$error = $hookmanager->error;
11291152
$errors = $hookmanager->errors;

htdocs/api/class/api_emailtemplates.class.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ public function post($request_data = null)
323323
if ($field == 'id') {
324324
throw new RestException(400, 'Creating with id field is forbidden');
325325
}
326+
if ($field == 'tms') {
327+
throw new RestException(400, 'Creating with tms field is forbidden');
328+
}
326329

327330
$this->email_template->$field = $this->_checkValForAPI($field, $value, $this->email_template);
328331
}
@@ -349,6 +352,7 @@ public function post($request_data = null)
349352
*
350353
* @return Object Object with cleaned properties
351354
*
355+
* @throws RestException 400
352356
* @throws RestException 403
353357
* @throws RestException 404
354358
* @throws RestException 500
@@ -367,8 +371,12 @@ public function putById($id, $request_data = null)
367371

368372
foreach ($request_data as $field => $value) {
369373
if ($field == 'id') {
370-
continue;
374+
throw new RestException(400, 'Updating with id field is forbidden');
371375
}
376+
if ($field == 'datec') {
377+
throw new RestException(400, 'Updating with datec field is forbidden');
378+
}
379+
372380
if ($field === 'caller') {
373381
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
374382
$this->email_template->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
@@ -400,7 +408,7 @@ public function putById($id, $request_data = null)
400408
*
401409
* @return Object Object with cleaned properties
402410
*
403-
* @throws RestException 403
411+
* @throws RestException 400
404412
* @throws RestException 404
405413
* @throws RestException 500
406414
*/
@@ -419,8 +427,12 @@ public function putbyLabel($label, $request_data = null)
419427
$newlabel = $label;
420428
foreach ($request_data as $field => $value) {
421429
if ($field == 'id') {
422-
continue;
430+
throw new RestException(400, 'Updating with id field is forbidden');
423431
}
432+
if ($field == 'datec') {
433+
throw new RestException(400, 'Updating with datec field is forbidden');
434+
}
435+
424436
if ($field == 'label') {
425437
$newlabel = $this->_checkValForAPI($field, $value, $this->email_template);
426438
}

htdocs/core/class/cemailtemplate.class.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ public function create($user, $notrigger = 0)
275275
$sql .= " '".((int) $this->fk_user)."',";
276276
}
277277
if (is_null($this->datec)) {
278-
$sql .= " NULL,";
278+
$sql .= " '".$this->db->idate($now)."',";
279279
} else {
280-
$sql .= " ".((int) $this->datec).",";
280+
$sql .= " '".$this->db->idate($this->datec)."',";
281281
}
282282
$sql .= " '".$this->db->escape($this->label)."',";
283283
$sql .= " ".((int) $this->position).", ".((int) $this->defaultfortype).",";
@@ -372,7 +372,6 @@ public function update(User $user, $notrigger = 0)
372372
$sql .= " lang=".($this->lang ? "'".$this->db->escape($this->lang)."', " : 'NULL, ');
373373
$sql .= " private=".((int) $this->private).",";
374374
$sql .= " fk_user=".((int) $this->fk_user).",";
375-
$sql .= " datec=".((int) $this->datec).",";
376375
$sql .= " label=".($this->label ? "'".$this->db->escape($this->label)."', " : 'NULL, ');
377376
$sql .= " position=".((int) $this->position).",";
378377
$sql .= " defaultfortype=".((int) $this->defaultfortype).",";
@@ -528,7 +527,7 @@ public function apifetch($id, $label = '')
528527
$this->active = (int) $obj->active;
529528
$this->content = (string) $obj->content;
530529
$this->content_lines = (string) $obj->content_lines;
531-
$this->datec = (int) $obj->datec;
530+
$this->datec = $this->db->jdate($obj->datec);
532531
$this->defaultfortype = (int) $obj->defaultfortype;
533532
$this->email_from = (string) $obj->email_from;
534533
$this->email_to = (string) $obj->email_to;
@@ -542,7 +541,7 @@ public function apifetch($id, $label = '')
542541
$this->module = (string) $obj->module;
543542
$this->position = (int) $obj->position;
544543
$this->private = (int) $obj->private;
545-
$this->tms = $obj->tms;
544+
$this->tms = $this->db->jdate($obj->tms);
546545
$this->topic = (string) $obj->topic;
547546
$this->type_template = (string) $obj->type_template;
548547

test/hurl/api/emailtemplates/10_emailtemplates.hurl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ POST http://{{hostnport}}/api/index.php/emailtemplates
6565
HTTP 400
6666
{"error":{"code":400,"message":"Bad Request: Creating with id field is forbidden"}}
6767

68+
# POST No tms in post request data
69+
POST http://{{hostnport}}/api/index.php/emailtemplates
70+
{ "tms" : 42, "label" : "automatic test using 10_emailtemplates.hurl", "topic" : "automatic test using 10_emailtemplates.hurl", "type_template" : "all" }
71+
HTTP 400
72+
{"error":{"code":400,"message":"Bad Request: Creating with tms field is forbidden"}}
73+
6874
# DELETE
6975
DELETE http://{{hostnport}}/api/index.php/emailtemplates/
7076
HTTP 405
@@ -96,3 +102,23 @@ GET http://{{hostnport}}/api/index.php/emailtemplates
96102
DOLAPIENTITY: 2
97103
HTTP 401
98104
{"error":{"code":401,"message":"Unauthorized: Error user not valid (not found with api key or bad status or bad validity dates) (conf->entity=2)"}}
105+
106+
# GET id of first element
107+
GET http://{{hostnport}}/api/index.php/emailtemplates?limit=1&page=0
108+
HTTP 200
109+
[Asserts]
110+
jsonpath "$[0].id" exists
111+
[Captures]
112+
template-id: jsonpath "$[0]['id']"
113+
114+
# PUT with an id
115+
PUT http://{{hostnport}}/api/index.php/emailtemplates/{{ template-id }}
116+
{ "id" : 42, "label" : "automatic test using 10_emailtemplates.hurl", "topic" : "automatic test using 10_emailtemplates.hurl", "type_template" : "all" }
117+
HTTP 400
118+
{"error":{"code":400,"message":"Bad Request: Updating with id field is forbidden"}}
119+
120+
# PUT with an datec
121+
PUT http://{{hostnport}}/api/index.php/emailtemplates/{{ template-id }}
122+
{ "datec" : 42, "label" : "automatic test using 10_emailtemplates.hurl", "topic" : "automatic test using 10_emailtemplates.hurl", "type_template" : "all" }
123+
HTTP 400
124+
{"error":{"code":400,"message":"Bad Request: Updating with datec field is forbidden"}}

0 commit comments

Comments
 (0)