mails_templates.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2010-2016 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2011-2018 Philippe Grand <philippe.grand@atoo-net.com>
  8. * Copyright (C) 2011 Remy Younes <ryounes@gmail.com>
  9. * Copyright (C) 2012-2015 Marcos García <marcosgdf@gmail.com>
  10. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@ltairis.fr>
  11. * Copyright (C) 2011-2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
  12. * Copyright (C) 2015 Ferran Marcet <fmarcet@2byte.es>
  13. * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  14. * Copyright (C) 2018-2023 Frédéric France <frederic.france@netlogic.fr>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  28. */
  29. /**
  30. * \file htdocs/admin/mails_templates.php
  31. * \ingroup core
  32. * \brief Page to administer emails templates
  33. */
  34. // Load Dolibarr environment
  35. require '../main.inc.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  40. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  42. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  43. // Load translation files required by the page
  44. $langsArray=array("errors", "admin", "mails", "languages");
  45. if (isModEnabled('adherent')) {
  46. $langsArray[]='members';
  47. }
  48. if (isModEnabled('eventorganization')) {
  49. $langsArray[]='eventorganization';
  50. }
  51. $langs->loadLangs($langsArray);
  52. $toselect = GETPOST('toselect', 'array');
  53. $action = GETPOST('action', 'aZ09') ? GETPOST('action', 'aZ09') : 'view';
  54. $massaction = GETPOST('massaction', 'alpha');
  55. $confirm = GETPOST('confirm', 'alpha'); // Result of a confirmation
  56. $mode = GETPOST('mode', 'aZ09');
  57. $optioncss = GETPOST('optioncss', 'alpha');
  58. $id = GETPOST('id', 'int');
  59. $rowid = GETPOST('rowid', 'alpha');
  60. $search_label = GETPOST('search_label', 'alphanohtml'); // Must allow value like 'Abc Def' or '(MyTemplateName)'
  61. $search_type_template = GETPOST('search_type_template', 'alpha');
  62. $search_lang = GETPOST('search_lang', 'alpha');
  63. $search_fk_user = GETPOST('search_fk_user', 'intcomma');
  64. $search_topic = GETPOST('search_topic', 'alpha');
  65. $search_module = GETPOST('search_module', 'alpha');
  66. $acts = array();
  67. $actl = array();
  68. $acts[0] = "activate";
  69. $acts[1] = "disable";
  70. $actl[0] = img_picto($langs->trans("Disabled"), 'switch_off', 'class="size15x"');
  71. $actl[1] = img_picto($langs->trans("Activated"), 'switch_on', 'class="size15x"');
  72. $listoffset = GETPOST('listoffset', 'alpha');
  73. $listlimit = GETPOST('listlimit', 'alpha') > 0 ?GETPOST('listlimit', 'alpha') : 1000;
  74. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  75. $sortfield = GETPOST('sortfield', 'aZ09comma');
  76. $sortorder = GETPOST('sortorder', 'aZ09comma');
  77. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  78. if (empty($page) || $page == -1) {
  79. $page = 0;
  80. } // If $page is not defined, or '' or -1
  81. $offset = $listlimit * $page;
  82. $pageprev = $page - 1;
  83. $pagenext = $page + 1;
  84. if (empty($sortfield)) {
  85. $sortfield = 'type_template,lang,position,label';
  86. }
  87. if (empty($sortorder)) {
  88. $sortorder = 'ASC';
  89. }
  90. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  91. $hookmanager->initHooks(array('emailtemplates'));
  92. // Name of SQL tables of dictionaries
  93. $tabname = array();
  94. $tabname[25] = MAIN_DB_PREFIX."c_email_templates";
  95. // Nom des champs en resultat de select pour affichage du dictionnaire
  96. $tabfield = array();
  97. $tabfield[25] = "label,lang,type_template,fk_user,private,position,module,topic,joinfiles,defaultfortype,content";
  98. if (!empty($conf->global->MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES)) {
  99. $tabfield[25] .= ',content_lines';
  100. }
  101. // Nom des champs d'edition pour modification d'un enregistrement
  102. $tabfieldvalue = array();
  103. $tabfieldvalue[25] = "label,lang,type_template,fk_user,private,position,topic,joinfiles,defaultfortype,content";
  104. if (!empty($conf->global->MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES)) {
  105. $tabfieldvalue[25] .= ',content_lines';
  106. }
  107. // Nom des champs dans la table pour insertion d'un enregistrement
  108. $tabfieldinsert = array();
  109. $tabfieldinsert[25] = "label,lang,type_template,fk_user,private,position,topic,joinfiles,defaultfortype,content";
  110. if (!empty($conf->global->MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES)) {
  111. $tabfieldinsert[25] .= ',content_lines';
  112. }
  113. $tabfieldinsert[25] .= ',entity'; // Must be at end because not into other arrays
  114. // Condition to show dictionary in setup page
  115. $tabcond = array();
  116. $tabcond[25] = true;
  117. // List of help for fields
  118. // Set MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES to allow edit of template for lines
  119. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  120. $formmail = new FormMail($db);
  121. if (empty($conf->global->MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES)) {
  122. $tmp = FormMail::getAvailableSubstitKey('formemail');
  123. $tmp['__(AnyTranslationKey)__'] = 'Translation';
  124. $helpsubstit = $langs->trans("AvailableVariables").':<br>';
  125. $helpsubstitforlines = $langs->trans("AvailableVariables").':<br>';
  126. foreach ($tmp as $key => $val) {
  127. $helpsubstit .= $key.' -> '.$val.'<br>';
  128. $helpsubstitforlines .= $key.' -> '.$val.'<br>';
  129. }
  130. } else {
  131. $tmp = FormMail::getAvailableSubstitKey('formemailwithlines');
  132. $tmp['__(AnyTranslationKey)__'] = 'Translation';
  133. $helpsubstit = $langs->trans("AvailableVariables").':<br>';
  134. $helpsubstitforlines = $langs->trans("AvailableVariables").':<br>';
  135. foreach ($tmp as $key => $val) {
  136. $helpsubstit .= $key.' -> '.$val.'<br>';
  137. }
  138. $tmp = FormMail::getAvailableSubstitKey('formemailforlines');
  139. foreach ($tmp as $key => $val) {
  140. $helpsubstitforlines .= $key.' -> '.$val.'<br>';
  141. }
  142. }
  143. $tabhelp = array();
  144. $tabhelp[25] = array(
  145. 'label'=>$langs->trans('EnterAnyCode'),
  146. 'type_template'=>$langs->trans("TemplateForElement"),
  147. 'private'=>$langs->trans("TemplateIsVisibleByOwnerOnly"),
  148. 'position'=>$langs->trans("PositionIntoComboList"),
  149. 'topic'=>'<span class="small">'.$helpsubstit.'</span>',
  150. 'joinfiles'=>$langs->trans('AttachMainDocByDefault'),
  151. 'defaultfortype'=>$langs->trans("DefaultForTypeDesc"),
  152. 'content'=>'<span class="small">'.$helpsubstit.'</span>',
  153. 'content_lines'=>'<span class="small">'.$helpsubstitforlines.'</span>'
  154. );
  155. // We save list of template email Dolibarr can manage. This list can found by a grep into code on "->param['models']"
  156. $elementList = array();
  157. // Add all and none after the sort
  158. $elementList['all'] = '-- '.dol_escape_htmltag($langs->trans("All")).' --';
  159. $elementList['none'] = '-- '.dol_escape_htmltag($langs->trans("None")).' --';
  160. $elementList['user'] = img_picto('', 'user', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToUser'));
  161. if (isModEnabled('adherent') && !empty($user->rights->adherent->lire)) {
  162. $elementList['member'] = img_picto('', 'object_member', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToMember'));
  163. }
  164. if (isModEnabled('recruitment') && !empty($user->rights->recruitment->recruitmentjobposition->read)) {
  165. $elementList['recruitmentcandidature_send'] = img_picto('', 'recruitmentcandidature', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('RecruitmentCandidatures'));
  166. }
  167. if (isModEnabled("societe") && $user->hasRight('societe', 'lire')) {
  168. $elementList['thirdparty'] = img_picto('', 'company', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToThirdparty'));
  169. }
  170. if (isModEnabled('project')) {
  171. $elementList['project'] = img_picto('', 'project', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToProject'));
  172. }
  173. if (isModEnabled("propal") && !empty($user->rights->propal->lire)) {
  174. $elementList['propal_send'] = img_picto('', 'propal', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendProposal'));
  175. }
  176. if (isModEnabled('commande') && !empty($user->rights->commande->lire)) {
  177. $elementList['order_send'] = img_picto('', 'order', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendOrder'));
  178. }
  179. if (isModEnabled('facture') && !empty($user->rights->facture->lire)) {
  180. $elementList['facture_send'] = img_picto('', 'bill', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendInvoice'));
  181. }
  182. if (isModEnabled("expedition")) {
  183. $elementList['shipping_send'] = img_picto('', 'dolly', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendShipment'));
  184. }
  185. if (isModEnabled("reception")) {
  186. $elementList['reception_send'] = img_picto('', 'dollyrevert', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendReception'));
  187. }
  188. if (isModEnabled('ficheinter')) {
  189. $elementList['fichinter_send'] = img_picto('', 'intervention', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendIntervention'));
  190. }
  191. if (isModEnabled('supplier_proposal')) {
  192. $elementList['supplier_proposal_send'] = img_picto('', 'propal', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendSupplierRequestForQuotation'));
  193. }
  194. if ((isModEnabled("fournisseur") && !empty($user->rights->fournisseur->commande->lire) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || (isModEnabled("supplier_order") && !empty($user->rights->supplier_order->lire))) {
  195. $elementList['order_supplier_send'] = img_picto('', 'order', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendSupplierOrder'));
  196. }
  197. if ((isModEnabled("fournisseur") && !empty($user->rights->fournisseur->facture->lire) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || (isModEnabled("supplier_invoice") && !empty($user->rights->supplier_invoice->lire))) {
  198. $elementList['invoice_supplier_send'] = img_picto('', 'bill', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendSupplierInvoice'));
  199. }
  200. if (isModEnabled('contrat') && !empty($user->rights->contrat->lire)) {
  201. $elementList['contract'] = img_picto('', 'contract', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendContract'));
  202. }
  203. if (isModEnabled('ticket') && !empty($user->rights->ticket->read)) {
  204. $elementList['ticket_send'] = img_picto('', 'ticket', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToTicket'));
  205. }
  206. if (isModEnabled('expensereport') && !empty($user->rights->expensereport->lire)) {
  207. $elementList['expensereport_send'] = img_picto('', 'trip', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToExpenseReport'));
  208. }
  209. if (isModEnabled('agenda')) {
  210. $elementList['actioncomm_send'] = img_picto('', 'action', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendEventPush'));
  211. }
  212. if (isModEnabled('eventorganization') && !empty($user->rights->eventorganization->read)) {
  213. $elementList['conferenceorbooth'] = img_picto('', 'action', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToSendEventOrganization'));
  214. }
  215. if (!empty($conf->partnership->enabled) && !empty($user->rights->partnership->read)) {
  216. $elementList['partnership_send'] = img_picto('', 'partnership', 'class="pictofixedwidth"').dol_escape_htmltag($langs->trans('MailToPartnership'));
  217. }
  218. $parameters = array('elementList'=>$elementList);
  219. $reshook = $hookmanager->executeHooks('emailElementlist', $parameters); // Note that $action and $object may have been modified by some hooks
  220. if ($reshook == 0) {
  221. foreach ($hookmanager->resArray as $item => $value) {
  222. $elementList[$item] = $value;
  223. }
  224. }
  225. $id = 25;
  226. $acceptlocallinktomedia = (acceptLocalLinktoMedia() > 0 ? 1 : 0);
  227. // Security
  228. if (!empty($user->socid)) {
  229. accessforbidden();
  230. }
  231. $permissiontoadd = 1;
  232. $permissiontodelete = 1;
  233. /*
  234. * Actions
  235. */
  236. if (GETPOST('cancel', 'alpha')) {
  237. $action = 'list';
  238. $massaction = '';
  239. }
  240. if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
  241. $massaction = '';
  242. }
  243. $parameters = array();
  244. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  245. if ($reshook < 0) {
  246. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  247. }
  248. if (empty($reshook)) {
  249. // Selection of new fields
  250. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  251. // Purge search criteria
  252. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
  253. // All tests are required to be compatible with all browsers
  254. $search_label = '';
  255. $search_type_template = '';
  256. $search_lang = '';
  257. $search_fk_user = '';
  258. $search_topic = '';
  259. $search_module = '';
  260. $toselect = array();
  261. $search_array_options = array();
  262. }
  263. // Actions add or modify an email template
  264. if ((GETPOST('actionadd', 'alpha') || GETPOST('actionmodify', 'alpha')) && $permissiontoadd) {
  265. $listfield = explode(',', str_replace(' ', '', $tabfield[$id]));
  266. $listfieldinsert = explode(',', $tabfieldinsert[$id]);
  267. $listfieldmodify = explode(',', $tabfieldinsert[$id]);
  268. $listfieldvalue = explode(',', $tabfieldvalue[$id]);
  269. // Check that all fields are filled
  270. $ok = 1;
  271. foreach ($listfield as $f => $value) {
  272. // Not mandatory fields
  273. if (in_array($value, ['joinfiles', 'defaultfortype', 'content', 'content_lines', 'module'])) {
  274. continue;
  275. }
  276. // Rename some POST variables into a generic name
  277. if (GETPOST('actionmodify', 'alpha') && $value == 'topic') {
  278. $_POST['topic'] = GETPOST('topic-'.$rowid);
  279. }
  280. if ((!GETPOSTISSET($value) || GETPOST($value) == '' || GETPOST($value) == '-1') && $value != 'lang' && $value != 'fk_user' && $value != 'position') {
  281. $ok = 0;
  282. $fieldnamekey = $listfield[$f];
  283. // We take translate key of field
  284. if ($fieldnamekey == 'libelle' || ($fieldnamekey == 'label')) {
  285. $fieldnamekey = 'Code';
  286. }
  287. if ($fieldnamekey == 'code') {
  288. $fieldnamekey = 'Code';
  289. }
  290. if ($fieldnamekey == 'note') {
  291. $fieldnamekey = 'Note';
  292. }
  293. if ($fieldnamekey == 'type_template') {
  294. $fieldnamekey = 'TypeOfTemplate';
  295. }
  296. if ($fieldnamekey == 'fk_user') {
  297. $fieldnamekey = 'Owner';
  298. }
  299. if ($fieldnamekey == 'private') {
  300. $fieldnamekey = 'Private';
  301. }
  302. if ($fieldnamekey == 'position') {
  303. $fieldnamekey = 'Position';
  304. }
  305. if ($fieldnamekey == 'topic') {
  306. $fieldnamekey = 'Topic';
  307. }
  308. setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->transnoentities($fieldnamekey)), null, 'errors');
  309. $action = 'create';
  310. }
  311. }
  312. // If previous test is ok action is add, we add the line
  313. if ($ok && GETPOST('actionadd')) {
  314. // Add new entry
  315. $sql = "INSERT INTO ".$tabname[$id]." (";
  316. // List of fields
  317. $sql .= $tabfieldinsert[$id];
  318. $sql .= ", active, enabled)";
  319. $sql .= " VALUES(";
  320. // List of values
  321. $i = 0;
  322. foreach ($listfieldinsert as $f => $value) {
  323. $keycode = isset($listfieldvalue[$i]) ? $listfieldvalue[$i] : "";
  324. if ($value == 'lang') {
  325. $keycode = 'langcode';
  326. }
  327. if (empty($keycode)) {
  328. $keycode = $value;
  329. }
  330. // Clean input variables
  331. if ($value == 'entity') {
  332. $_POST[$keycode] = $conf->entity;
  333. }
  334. if ($value == 'fk_user' && !($_POST[$keycode] > 0)) {
  335. $_POST[$keycode] = '';
  336. }
  337. if ($value == 'private' && !is_numeric($_POST[$keycode])) {
  338. $_POST[$keycode] = '0';
  339. }
  340. if ($value == 'position' && !is_numeric($_POST[$keycode])) {
  341. $_POST[$keycode] = '1';
  342. }
  343. if ($value == 'defaultfortype' && !is_numeric($_POST[$keycode])) {
  344. $_POST[$keycode] = '0';
  345. }
  346. //var_dump($keycode.' '.$value);
  347. if ($i) {
  348. $sql .= ", ";
  349. }
  350. if (GETPOST($keycode) == '' && $keycode != 'langcode') {
  351. $sql .= "null"; // langcode must be '' if not defined so the unique key that include lang will work
  352. } elseif (GETPOST($keycode) == '0' && $keycode == 'langcode') {
  353. $sql .= "''"; // langcode must be '' if not defined so the unique key that include lang will work
  354. } elseif ($keycode == 'fk_user') {
  355. if (!$user->admin) { // A non admin user can only edit its own template
  356. $sql .= " ".((int) $user->id);
  357. } else {
  358. $sql .= " ".((int) GETPOST($keycode, 'int'));
  359. }
  360. } elseif ($keycode == 'content') {
  361. $sql .= "'".$db->escape(GETPOST($keycode, 'restricthtml'))."'";
  362. } elseif (in_array($keycode, array('joinfiles', 'defaultfortype', 'private', 'position', 'entity'))) {
  363. $sql .= (int) GETPOST($keycode, 'int');
  364. } else {
  365. $sql .= "'".$db->escape(GETPOST($keycode, 'alphanohtml'))."'";
  366. }
  367. $i++;
  368. }
  369. $sql .= ", 1, 1)";
  370. dol_syslog("actionadd", LOG_DEBUG);
  371. $result = $db->query($sql);
  372. if ($result) { // Add is ok
  373. setEventMessages($langs->transnoentities("RecordSaved"), null, 'mesgs');
  374. $_POST = array('id'=>$id); // Clean $_POST array, we keep only id
  375. } else {
  376. if ($db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  377. setEventMessages($langs->transnoentities("ErrorRecordAlreadyExists"), null, 'errors');
  378. } else {
  379. dol_print_error($db);
  380. }
  381. $action = 'create';
  382. }
  383. }
  384. // We modify the line
  385. if ($ok && GETPOST('actionmodify')) {
  386. $rowidcol = "rowid";
  387. // Modify entry
  388. $sql = "UPDATE ".$tabname[$id]." SET ";
  389. // Modify value of fields
  390. $i = 0;
  391. foreach ($listfieldmodify as $field) {
  392. if ($field == 'entity') {
  393. // entity not present on listfieldmodify array
  394. $keycode = $field;
  395. $_POST[$keycode] = $conf->entity;
  396. } else {
  397. $keycode = $listfieldvalue[$i];
  398. }
  399. if ($field == 'lang') {
  400. $keycode = 'langcode';
  401. }
  402. if (empty($keycode)) {
  403. $keycode = $field;
  404. }
  405. // Rename some POST variables into a generic name
  406. if ($field == 'fk_user' && !(GETPOST('fk_user', 'int') > 0)) {
  407. $_POST['fk_user'] = '';
  408. }
  409. if ($field == 'topic') {
  410. $_POST['topic'] = GETPOST('topic-'.$rowid);
  411. }
  412. if ($field == 'joinfiles') {
  413. $_POST['joinfiles'] = GETPOST('joinfiles-'.$rowid);
  414. }
  415. if ($field == 'content') {
  416. $_POST['content'] = GETPOST('content-'.$rowid);
  417. }
  418. if ($field == 'content_lines') {
  419. $_POST['content_lines'] = GETPOST('content_lines-'.$rowid);
  420. }
  421. if ($i) {
  422. $sql .= ", ";
  423. }
  424. $sql .= $field."=";
  425. if (GETPOST($keycode) == '' || (!in_array($keycode, array('langcode', 'position', 'private', 'defaultfortype')) && !GETPOST($keycode))) {
  426. $sql .= "null"; // langcode,... must be '' if not defined so the unique key that include lang will work
  427. } elseif (GETPOST($keycode) == '0' && $keycode == 'langcode') {
  428. $sql .= "''"; // langcode must be '' if not defined so the unique key that include lang will work
  429. } elseif ($keycode == 'fk_user') {
  430. if (!$user->admin) { // A non admin user can only edit its own template
  431. $sql .= " ".((int) $user->id);
  432. } else {
  433. $sql .= " ".((int) GETPOST($keycode, 'int'));
  434. }
  435. } elseif ($keycode == 'content') {
  436. $sql .= "'".$db->escape(GETPOST($keycode, 'restricthtml'))."'";
  437. } elseif (in_array($keycode, array('joinfiles', 'defaultfortype', 'private', 'position'))) {
  438. $sql .= (int) GETPOST($keycode, 'int');
  439. } else {
  440. $sql .= "'".$db->escape(GETPOST($keycode, 'alphanohtml'))."'";
  441. }
  442. $i++;
  443. }
  444. $sql .= " WHERE ".$rowidcol." = ".((int) $rowid);
  445. if (!$user->admin) { // A non admin user can only edit its own template
  446. $sql .= " AND fk_user = ".((int) $user->id);
  447. }
  448. //print $sql;exit;
  449. dol_syslog("actionmodify", LOG_DEBUG);
  450. //print $sql;
  451. $resql = $db->query($sql);
  452. if ($resql) {
  453. setEventMessages($langs->transnoentities("RecordSaved"), null, 'mesgs');
  454. } else {
  455. setEventMessages($db->error(), null, 'errors');
  456. $action = 'edit';
  457. }
  458. }
  459. }
  460. if ($action == 'confirm_delete' && $confirm == 'yes' && $permissiontodelete) { // delete
  461. $rowidcol = "rowid";
  462. $sql = "DELETE from ".$tabname[$id]." WHERE ".$rowidcol." = ".((int) $rowid);
  463. if (!$user->admin) { // A non admin user can only edit its own template
  464. $sql .= " AND fk_user = ".((int) $user->id);
  465. }
  466. dol_syslog("delete", LOG_DEBUG);
  467. $result = $db->query($sql);
  468. if (!$result) {
  469. if ($db->errno() == 'DB_ERROR_CHILD_EXISTS') {
  470. setEventMessages($langs->transnoentities("ErrorRecordIsUsedByChild"), null, 'errors');
  471. } else {
  472. dol_print_error($db);
  473. }
  474. }
  475. }
  476. // activate
  477. if ($action == $acts[0] && $permissiontoadd) {
  478. $rowidcol = "rowid";
  479. $sql = "UPDATE ".$tabname[$id]." SET active = 1 WHERE rowid = ".((int) $rowid);
  480. $result = $db->query($sql);
  481. if (!$result) {
  482. dol_print_error($db);
  483. }
  484. }
  485. // disable
  486. if ($action == $acts[1] && $permissiontoadd) {
  487. $rowidcol = "rowid";
  488. $sql = "UPDATE ".$tabname[$id]." SET active = 0 WHERE rowid = ".((int) $rowid);
  489. $result = $db->query($sql);
  490. if (!$result) {
  491. dol_print_error($db);
  492. }
  493. }
  494. }
  495. /*
  496. * View
  497. */
  498. $form = new Form($db);
  499. $now = dol_now();
  500. $formadmin = new FormAdmin($db);
  501. //$help_url = "EN:Module_MyObject|FR:Module_MyObject_FR|ES:Módulo_MyObject";
  502. $help_url = '';
  503. if (!empty($user->admin) && (empty($_SESSION['leftmenu']) || $_SESSION['leftmenu'] != 'email_templates')) {
  504. $title = $langs->trans("EMailsSetup");
  505. } else {
  506. $title = $langs->trans("EMailTemplates");
  507. }
  508. $morejs = array();
  509. $morecss = array();
  510. $sql = "SELECT rowid as rowid, module, label, type_template, lang, fk_user, private, position, topic, joinfiles, defaultfortype, content_lines, content, enabled, active";
  511. $sql .= " FROM ".MAIN_DB_PREFIX."c_email_templates";
  512. $sql .= " WHERE entity IN (".getEntity('email_template').")";
  513. if (!$user->admin) {
  514. $sql .= " AND (private = 0 OR (private = 1 AND fk_user = ".((int) $user->id)."))"; // Show only public and private to me
  515. $sql .= " AND (active = 1 OR fk_user = ".((int) $user->id).")"; // Show only active or owned by me
  516. }
  517. if (!getDolGlobalInt('MAIN_MULTILANGS')) {
  518. $sql .= " AND (lang = '".$db->escape($langs->defaultlang)."' OR lang IS NULL OR lang = '')";
  519. }
  520. if ($search_label) {
  521. $sql .= natural_search('label', $search_label);
  522. }
  523. if ($search_type_template != '' && $search_type_template != '-1') {
  524. $sql .= natural_search('type_template', $search_type_template);
  525. }
  526. if ($search_lang) {
  527. $sql .= natural_search('lang', $search_lang);
  528. }
  529. if ($search_fk_user != '' && $search_fk_user != '-1') {
  530. $sql .= natural_search('fk_user', $search_fk_user, 2);
  531. }
  532. if ($search_module) {
  533. $sql .= natural_search('module', $search_module);
  534. }
  535. if ($search_topic) {
  536. $sql .= natural_search('topic', $search_topic);
  537. }
  538. // If sort order is "country", we use country_code instead
  539. if ($sortfield == 'country') {
  540. $sortfield = 'country_code';
  541. }
  542. $sql .= $db->order($sortfield, $sortorder);
  543. $sql .= $db->plimit($listlimit + 1, $offset);
  544. //print $sql;
  545. // Output page
  546. // --------------------------------------------------------------------
  547. llxHeader('', $title, $help_url, '', 0, 0, $morejs, $morecss, '', '');
  548. $arrayofselected = is_array($toselect) ? $toselect : array();
  549. $param = '';
  550. if (!empty($mode)) {
  551. $param .= '&mode='.urlencode($mode);
  552. }
  553. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  554. $param .= '&contextpage='.urlencode($contextpage);
  555. }
  556. if ($limit > 0 && $limit != $conf->liste_limit) {
  557. $param .= '&limit='.((int) $limit);
  558. }
  559. if (!empty($search) && is_array($search)) {
  560. foreach ($search as $key => $val) {
  561. if (is_array($search[$key]) && count($search[$key])) {
  562. foreach ($search[$key] as $skey) {
  563. if ($skey != '') {
  564. $param .= '&search_'.$key.'[]='.urlencode($skey);
  565. }
  566. }
  567. } elseif ($search[$key] != '') {
  568. $param .= '&search_'.$key.'='.urlencode($search[$key]);
  569. }
  570. }
  571. }
  572. if ($optioncss != '') {
  573. $param .= '&optioncss='.urlencode($optioncss);
  574. }
  575. // Add $param from extra fields
  576. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php';
  577. // Add $param from hooks
  578. $parameters = array();
  579. $reshook = $hookmanager->executeHooks('printFieldListSearchParam', $parameters, $object); // Note that $action and $object may have been modified by hook
  580. $param .= $hookmanager->resPrint;
  581. $linkback = '';
  582. $titlepicto = 'title_setup';
  583. $url = DOL_URL_ROOT.'/admin/mails_templates.php?action=create';
  584. $newcardbutton = dolGetButtonTitle($langs->trans('NewEMailTemplate'), '', 'fa fa-plus-circle', $url, '', $permissiontoadd);
  585. if (!empty($user->admin) && (empty($_SESSION['leftmenu']) || $_SESSION['leftmenu'] != 'email_templates')) {
  586. print load_fiche_titre($title, '', $titlepicto);
  587. } else {
  588. print load_fiche_titre($title, $newcardbutton, $titlepicto);
  589. }
  590. if (!empty($user->admin) && (empty($_SESSION['leftmenu']) || $_SESSION['leftmenu'] != 'email_templates')) {
  591. $head = email_admin_prepare_head();
  592. print dol_get_fiche_head($head, 'templates', '', -1);
  593. if (!empty($user->admin) && (empty($_SESSION['leftmenu']) || $_SESSION['leftmenu'] != 'email_templates')) {
  594. print load_fiche_titre('', $newcardbutton, '');
  595. }
  596. }
  597. // Confirm deletion of record
  598. if ($action == 'delete') {
  599. print $form->formconfirm($_SERVER["PHP_SELF"].'?'.($page ? 'page='.$page.'&' : '').'sortfield='.$sortfield.'&sortorder='.$sortorder.'&rowid='.((int) $rowid).'&code='.urlencode($code).'&id='.((int) $id), $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_delete', '', 0, 1);
  600. }
  601. $fieldlist = explode(',', $tabfield[$id]);
  602. if ($action == 'create') {
  603. // If data was already input, we define them in obj to populate input fields.
  604. $obj = new stdClass();
  605. $obj->label = GETPOST('label');
  606. $obj->lang = GETPOST('lang');
  607. $obj->type_template = GETPOST('type_template');
  608. $obj->fk_user = GETPOST('fk_user', 'int');
  609. $obj->private = GETPOST('private', 'int');
  610. $obj->position = GETPOST('position');
  611. $obj->topic = GETPOST('topic');
  612. $obj->joinfiles = GETPOST('joinfiles');
  613. $obj->defaultfortype = GETPOST('defaultfortype') ? 1 : 0;
  614. $obj->content = GETPOST('content', 'restricthtml');
  615. // Form to add a new line
  616. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST">';
  617. print '<input type="hidden" name="token" value="'.newToken().'">';
  618. print '<input type="hidden" name="action" value="add">';
  619. print '<input type="hidden" name="from" value="'.dol_escape_htmltag(GETPOST('from', 'alpha')).'">';
  620. print '<div class="div-table-responsive-no-min">';
  621. print '<table class="noborder centpercent">';
  622. // Line to enter new values (title)
  623. print '<tr class="liste_titre">';
  624. foreach ($fieldlist as $field => $value) {
  625. // Determine le nom du champ par rapport aux noms possibles
  626. // dans les dictionnaires de donnees
  627. $valuetoshow = ucfirst($fieldlist[$field]); // Par defaut
  628. $valuetoshow = $langs->trans($valuetoshow); // try to translate
  629. $css = "left";
  630. if ($fieldlist[$field] == 'module') {
  631. $valuetoshow = '&nbsp;';
  632. }
  633. if ($fieldlist[$field] == 'fk_user') {
  634. $valuetoshow = $langs->trans("Owner");
  635. }
  636. if ($fieldlist[$field] == 'lang') {
  637. $valuetoshow = (!getDolGlobalInt('MAIN_MULTILANGS') ? '&nbsp;' : $langs->trans("Language"));
  638. }
  639. if ($fieldlist[$field] == 'type') {
  640. $valuetoshow = $langs->trans("Type");
  641. }
  642. if ($fieldlist[$field] == 'position') {
  643. $css = 'center';
  644. }
  645. if ($fieldlist[$field] == 'code') {
  646. $valuetoshow = $langs->trans("Code");
  647. }
  648. if ($fieldlist[$field] == 'label') {
  649. $valuetoshow = $langs->trans("Code");
  650. }
  651. if ($fieldlist[$field] == 'type_template') {
  652. $valuetoshow = $langs->trans("TypeOfTemplate"); $css = "center";
  653. }
  654. if (in_array($fieldlist[$field], array('private', 'private', 'defaultfortype'))) {
  655. $css = 'center';
  656. }
  657. if ($fieldlist[$field] == 'topic') {
  658. $valuetoshow = '';
  659. }
  660. if ($fieldlist[$field] == 'joinfiles') {
  661. $valuetoshow = '';
  662. }
  663. if ($fieldlist[$field] == 'content') {
  664. $valuetoshow = '';
  665. }
  666. if ($fieldlist[$field] == 'content_lines') {
  667. $valuetoshow = '';
  668. }
  669. if ($valuetoshow != '') {
  670. print '<th class="'.$css.'">';
  671. if (!empty($tabhelp[$id][$value]) && preg_match('/^http(s*):/i', $tabhelp[$id][$value])) {
  672. print '<a href="'.$tabhelp[$id][$value].'" target="_blank" rel="noopener noreferrer">'.$valuetoshow.' '.img_help(1, $valuetoshow).'</a>';
  673. } elseif (!empty($tabhelp[$id][$value])) {
  674. if (in_array($value, array('topic'))) {
  675. print $form->textwithpicto($valuetoshow, $tabhelp[$id][$value], 1, 'help', '', 0, 2, $value); // Tooltip on click
  676. } else {
  677. print $form->textwithpicto($valuetoshow, $tabhelp[$id][$value], 1, 'help', '', 0, 2); // Tooltip on hover
  678. }
  679. } else {
  680. print $valuetoshow;
  681. }
  682. print '</th>';
  683. }
  684. }
  685. print '<th>';
  686. print '<input type="hidden" name="id" value="'.$id.'">';
  687. print '</th>';
  688. print '</tr>';
  689. $tmpaction = 'create';
  690. $parameters = array(
  691. 'fieldlist' => $fieldlist,
  692. 'tabname' => $tabname[$id]
  693. );
  694. $reshook = $hookmanager->executeHooks('createEmailTemplateFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  695. $error = $hookmanager->error;
  696. $errors = $hookmanager->errors;
  697. // Line to enter new values (input fields)
  698. print '<tr class="oddeven">';
  699. if (empty($reshook)) {
  700. if ($action == 'edit') {
  701. fieldList($fieldlist, $obj, $tabname[$id], 'hide');
  702. } else {
  703. fieldList($fieldlist, $obj, $tabname[$id], 'add');
  704. }
  705. }
  706. // Action column
  707. print '<td class="right">';
  708. print '</td>';
  709. print "</tr>";
  710. // Show fields for topic, join files and body
  711. $fieldsforcontent = array('topic', 'joinfiles', 'defaultfortype', 'content');
  712. if (!empty($conf->global->MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES)) {
  713. $fieldsforcontent = array('topic', 'joinfiles', 'defaultfortype', 'content', 'content_lines');
  714. }
  715. foreach ($fieldsforcontent as $tmpfieldlist) {
  716. print '<tr class="impair nodrag nodrop nohover"><td colspan="9" class="nobottom">';
  717. // Topic of email
  718. if ($tmpfieldlist == 'topic') {
  719. print '<strong>'.$form->textwithpicto($langs->trans("Topic"), $tabhelp[$id][$tmpfieldlist], 1, 'help', '', 0, 2, $tmpfieldlist).'</strong> ';
  720. }
  721. if ($tmpfieldlist == 'joinfiles') {
  722. print '<strong>'.$form->textwithpicto($langs->trans("FilesAttachedToEmail"), $tabhelp[$id][$tmpfieldlist], 1, 'help', '', 0, 2, $tmpfieldlist).'</strong> ';
  723. }
  724. if ($tmpfieldlist == 'content') {
  725. print $form->textwithpicto($langs->trans("Content"), $tabhelp[$id][$tmpfieldlist], 1, 'help', '', 0, 2, $tmpfieldlist).'<br>';
  726. }
  727. if ($tmpfieldlist == 'content_lines') {
  728. print $form->textwithpicto($langs->trans("ContentForLines"), $tabhelp[$id][$tmpfieldlist], 1, 'help', '', 0, 2, $tmpfieldlist).'<br>';
  729. }
  730. // Input field
  731. if ($tmpfieldlist == 'topic') {
  732. print '<input type="text" class="flat minwidth500" name="'.$tmpfieldlist.'" value="'.(!empty($obj->$tmpfieldlist) ? $obj->$tmpfieldlist : '').'">';
  733. } elseif ($tmpfieldlist == 'joinfiles') {
  734. print $form->selectyesno($tmpfieldlist, (isset($obj->$tmpfieldlist) ? $obj->$tmpfieldlist : '0'), 1, false, 0, 1);
  735. } else {
  736. $okforextended = true;
  737. if (empty($conf->global->FCKEDITOR_ENABLE_MAIL)) {
  738. $okforextended = false;
  739. }
  740. $doleditor = new DolEditor($tmpfieldlist, (!empty($obj->$tmpfieldlist) ? $obj->$tmpfieldlist : ''), '', 180, 'dolibarr_mailings', 'In', false, $acceptlocallinktomedia, $okforextended, ROWS_4, '90%');
  741. print $doleditor->Create(1);
  742. }
  743. print '</td>';
  744. print '</tr>';
  745. }
  746. print '</table>';
  747. if ($action != 'edit') {
  748. print '<center>';
  749. print '<input type="submit" class="button button-add" name="actionadd" value="'.$langs->trans("Add").'"> ';
  750. print '<input type="submit" class="button button-cancel" name="actioncancel" value="'.$langs->trans("Cancel").'">';
  751. print '</center>';
  752. }
  753. print '</div>';
  754. print '</form>';
  755. print '<br><br>';
  756. }
  757. // List of available record in database
  758. dol_syslog("htdocs/admin/dict", LOG_DEBUG);
  759. $resql = $db->query($sql);
  760. if (!$resql) {
  761. dol_print_error($db);
  762. exit;
  763. }
  764. $num = $db->num_rows($resql);
  765. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST">';
  766. print '<input type="hidden" name="token" value="'.newToken().'">';
  767. print '<input type="hidden" name="from" value="'.dol_escape_htmltag(GETPOST('from', 'alpha')).'">';
  768. print '<div class="div-table-responsive-no-min">';
  769. print '<table class="noborder centpercent">';
  770. $i = 0;
  771. $param = '&id='.$id;
  772. if ($search_label) {
  773. $param .= '&search_label='.urlencode($search_label);
  774. }
  775. if ($search_lang > 0) {
  776. $param .= '&search_lang='.urlencode($search_lang);
  777. }
  778. if ($search_type_template != '-1') {
  779. $param .= '&search_type_template='.urlencode($search_type_template);
  780. }
  781. if ($search_fk_user > 0) {
  782. $param .= '&search_fk_user='.urlencode($search_fk_user);
  783. }
  784. if ($search_module) {
  785. $param .= '&search_module='.urlencode($search_module);
  786. }
  787. if ($search_topic) {
  788. $param .= '&search_topic='.urlencode($search_topic);
  789. }
  790. $paramwithsearch = $param;
  791. if ($sortorder) {
  792. $paramwithsearch .= '&sortorder='.urlencode($sortorder);
  793. }
  794. if ($sortfield) {
  795. $paramwithsearch .= '&sortfield='.urlencode($sortfield);
  796. }
  797. if (GETPOST('from', 'alpha')) {
  798. $paramwithsearch .= '&from='.urlencode(GETPOST('from', 'alpha'));
  799. }
  800. // There is several pages
  801. if ($num > $listlimit) {
  802. print '<tr class="none"><td class="right" colspan="'.(3 + count($fieldlist)).'">';
  803. print_fleche_navigation($page, $_SERVER["PHP_SELF"], $paramwithsearch, ($num > $listlimit), '<li class="pagination"><span>'.$langs->trans("Page").' '.($page + 1).'</span></li>');
  804. print '</td></tr>';
  805. }
  806. // Title line with search boxes
  807. print '<tr class="liste_titre">';
  808. foreach ($fieldlist as $field => $value) {
  809. if ($value == 'module') {
  810. print '<td class="liste_titre"><input type="text" name="search_module" class="maxwidth75" value="'.dol_escape_htmltag($search_module).'"></td>';
  811. } elseif ($value == 'label') {
  812. print '<td class="liste_titre"><input type="text" name="search_label" class="maxwidth200" value="'.dol_escape_htmltag($search_label).'"></td>';
  813. } elseif ($value == 'lang') {
  814. print '<td class="liste_titre">';
  815. print $formadmin->select_language($search_lang, 'search_lang', 0, null, 1, 0, 0, 'maxwidth100');
  816. print '</td>';
  817. } elseif ($value == 'fk_user') {
  818. print '<td class="liste_titre">';
  819. print $form->select_dolusers($search_fk_user, 'search_fk_user', 1, null, 0, ($user->admin ? '' : 'hierarchyme'), null, 0, 0, 0, '', 0, '', 'maxwidth100', 1);
  820. print '</td>';
  821. } elseif ($value == 'topic') {
  822. print '<td class="liste_titre"><input type="text" name="search_topic" value="'.dol_escape_htmltag($search_topic).'"></td>';
  823. } elseif ($value == 'type_template') {
  824. print '<td class="liste_titre center">';
  825. print $form->selectarray('search_type_template', $elementList, $search_type_template, 1, 0, 0, '', 0, 0, 0, '', 'minwidth100 maxwidth125', 1, '', 0, 1);
  826. print '</td>';
  827. } elseif (!in_array($value, array('content', 'content_lines'))) {
  828. print '<td class="liste_titre"></td>';
  829. }
  830. }
  831. /*if (empty($conf->global->MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES)) {
  832. print '<td class="liste_titre"></td>';
  833. }*/
  834. // Status
  835. print '<td></td>';
  836. // Action column
  837. print '<td class="liste_titre center" width="64">';
  838. $searchpicto = $form->showFilterButtons();
  839. print $searchpicto;
  840. print '</td>';
  841. print '</tr>';
  842. // Title of lines
  843. print '<tr class="liste_titre">';
  844. foreach ($fieldlist as $field => $value) {
  845. $showfield = 1; // By defaut
  846. $css = "left";
  847. $sortable = 1;
  848. $valuetoshow = '';
  849. $forcenowrap = 1;
  850. /*
  851. $tmparray=getLabelOfField($fieldlist[$field]);
  852. $showfield=$tmp['showfield'];
  853. $valuetoshow=$tmp['valuetoshow'];
  854. $css=$tmp['align'];
  855. $sortable=$tmp['sortable'];
  856. */
  857. $valuetoshow = ucfirst($fieldlist[$field]); // By defaut
  858. $valuetoshow = $langs->trans($valuetoshow); // try to translate
  859. if ($fieldlist[$field] == 'module') {
  860. $css = 'tdoverflowmax100';
  861. }
  862. if ($fieldlist[$field] == 'fk_user') {
  863. $valuetoshow = $langs->trans("Owner");
  864. }
  865. if ($fieldlist[$field] == 'lang') {
  866. $valuetoshow = $langs->trans("Language");
  867. }
  868. if ($fieldlist[$field] == 'type') {
  869. $valuetoshow = $langs->trans("Type");
  870. }
  871. if ($fieldlist[$field] == 'libelle' || $fieldlist[$field] == 'label') {
  872. $valuetoshow = $langs->trans("Code");
  873. }
  874. if ($fieldlist[$field] == 'type_template') {
  875. $css = 'center';
  876. $valuetoshow = $langs->trans("TypeOfTemplate");
  877. }
  878. if ($fieldlist[$field] == 'private') {
  879. $css = 'center';
  880. }
  881. if ($fieldlist[$field] == 'position') {
  882. $css = 'center';
  883. }
  884. if ($fieldlist[$field] == 'joinfiles') {
  885. $valuetoshow = $langs->trans("FilesAttachedToEmail");
  886. $css = 'center';
  887. $forcenowrap = 0;
  888. }
  889. if ($fieldlist[$field] == 'content') {
  890. $valuetoshow = $langs->trans("Content"); $showfield = 0;
  891. }
  892. if ($fieldlist[$field] == 'content_lines') {
  893. $valuetoshow = $langs->trans("ContentForLines"); $showfield = 0;
  894. }
  895. // Show fields
  896. if ($showfield) {
  897. if (!empty($tabhelp[$id][$value])) {
  898. if (in_array($value, array('topic'))) {
  899. $valuetoshow = $form->textwithpicto($valuetoshow, $tabhelp[$id][$value], 1, 'help', '', 0, 2, 'tooltip'.$value, $forcenowrap); // Tooltip on click
  900. } else {
  901. $valuetoshow = $form->textwithpicto($valuetoshow, $tabhelp[$id][$value], 1, 'help', '', 0, 2, '', $forcenowrap); // Tooltip on hover
  902. }
  903. }
  904. $sortfieldtouse = ($sortable ? $fieldlist[$field] : '');
  905. if ($sortfieldtouse == 'type_template') {
  906. $sortfieldtouse.= 'type_template,lang,position,label';
  907. }
  908. print getTitleFieldOfList($valuetoshow, 0, $_SERVER["PHP_SELF"], $sortfieldtouse, ($page ? 'page='.$page.'&' : ''), $param, '', $sortfield, $sortorder, $css.' ');
  909. }
  910. }
  911. print getTitleFieldOfList($langs->trans("Status"), 0, $_SERVER["PHP_SELF"], "active", ($page ? 'page='.$page.'&' : ''), $param, '', $sortfield, $sortorder, 'center ');
  912. print getTitleFieldOfList('');
  913. print '</tr>';
  914. if ($num) {
  915. $nbqualified = 0;
  916. // Lines with values
  917. while ($i < $num) {
  918. $obj = $db->fetch_object($resql);
  919. if ($obj) {
  920. if ($action == 'edit' && ($rowid == (!empty($obj->rowid) ? $obj->rowid : $obj->code))) {
  921. print '<tr class="oddeven" id="rowid-'.$obj->rowid.'">';
  922. $tmpaction = 'edit';
  923. $parameters = array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  924. $reshook = $hookmanager->executeHooks('editEmailTemplateFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  925. $error = $hookmanager->error; $errors = $hookmanager->errors;
  926. // Show main fields
  927. if (empty($reshook)) {
  928. fieldList($fieldlist, $obj, $tabname[$id], 'edit');
  929. }
  930. print '<td class="center">';
  931. print '<input type="hidden" name="page" value="'.$page.'">';
  932. print '<input type="hidden" name="rowid" value="'.$rowid.'">';
  933. print '<input type="submit" class="button buttongen button-save" name="actionmodify" value="'.$langs->trans("Modify").'">';
  934. print '<div name="'.(!empty($obj->rowid) ? $obj->rowid : $obj->code).'"></div>';
  935. print '<input type="submit" class="button buttongen button-cancel" name="actioncancel" value="'.$langs->trans("Cancel").'">';
  936. print '</td>';
  937. $fieldsforcontent = array('topic', 'joinfiles', 'content');
  938. if (!empty($conf->global->MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES)) {
  939. $fieldsforcontent = array('topic', 'joinfiles', 'content', 'content_lines');
  940. }
  941. foreach ($fieldsforcontent as $tmpfieldlist) {
  942. $showfield = 1;
  943. $css = "left";
  944. $valuetoshow = $obj->$tmpfieldlist;
  945. $class = 'tddict';
  946. // Show value for field
  947. if ($showfield) {
  948. // Show line for topic, joinfiles and content
  949. print '</tr><tr class="oddeven" nohover tr-'.$tmpfieldlist.'-'.$rowid.' ">';
  950. print '<td colspan="10">';
  951. if ($tmpfieldlist == 'topic') {
  952. print '<strong>'.$form->textwithpicto($langs->trans("Topic"), $tabhelp[$id][$tmpfieldlist], 1, 'help', '', 0, 2, $tmpfieldlist).'</strong> ';
  953. print '<input type="text" class="flat minwidth500" name="'.$tmpfieldlist.'-'.$rowid.'" value="'.(!empty($obj->{$tmpfieldlist}) ? $obj->{$tmpfieldlist} : '').'">';
  954. }
  955. if ($tmpfieldlist == 'joinfiles') {
  956. print '<strong>'.$form->textwithpicto($langs->trans("FilesAttachedToEmail"), $tabhelp[$id][$tmpfieldlist], 1, 'help', '', 0, 2, $tmpfieldlist).'</strong> ';
  957. print $form->selectyesno($tmpfieldlist.'-'.$rowid, (isset($obj->$tmpfieldlist) ? $obj->$tmpfieldlist : '0'), 1, false, 0, 1);
  958. }
  959. if ($tmpfieldlist == 'content') {
  960. print $form->textwithpicto($langs->trans("Content"), $tabhelp[$id][$tmpfieldlist], 1, 'help', '', 0, 2, $tmpfieldlist).'<br>';
  961. $okforextended = true;
  962. if (empty($conf->global->FCKEDITOR_ENABLE_MAIL)) {
  963. $okforextended = false;
  964. }
  965. $doleditor = new DolEditor($tmpfieldlist.'-'.$rowid, (!empty($obj->{$tmpfieldlist}) ? $obj->{$tmpfieldlist} : ''), '', 500, 'dolibarr_mailings', 'In', 0, $acceptlocallinktomedia, $okforextended, ROWS_6, '90%');
  966. print $doleditor->Create(1);
  967. }
  968. if ($tmpfieldlist == 'content_lines') {
  969. print $form->textwithpicto($langs->trans("ContentForLines"), $tabhelp[$id][$tmpfieldlist], 1, 'help', '', 0, 2, $tmpfieldlist).'<br>';
  970. $okforextended = true;
  971. if (empty($conf->global->FCKEDITOR_ENABLE_MAIL)) {
  972. $okforextended = false;
  973. }
  974. $doleditor = new DolEditor($tmpfieldlist.'-'.$rowid, (!empty($obj->{$tmpfieldlist}) ? $obj->{$tmpfieldlist} : ''), '', 140, 'dolibarr_mailings', 'In', 0, $acceptlocallinktomedia, $okforextended, ROWS_6, '90%');
  975. print $doleditor->Create(1);
  976. }
  977. print '</td>';
  978. print '<td></td>';
  979. print '<td></td>';
  980. }
  981. }
  982. print "</tr>\n";
  983. $nbqualified++;
  984. } else {
  985. // If template is for a module, check module is enabled.
  986. if ($obj->module) {
  987. $tempmodulekey = $obj->module;
  988. if (empty($conf->$tempmodulekey) || empty($conf->$tempmodulekey->enabled)) {
  989. $i++;
  990. continue;
  991. }
  992. }
  993. $keyforobj = 'type_template';
  994. if (!in_array($obj->$keyforobj, array_keys($elementList))) {
  995. $i++;
  996. continue; // It means this is a type of template not into elementList (may be because enabled condition of this type is false because module is not enabled)
  997. }
  998. // Test on 'enabled'
  999. if (!dol_eval($obj->enabled, 1, 1, '1')) {
  1000. $i++;
  1001. continue; // Email template not qualified
  1002. }
  1003. $nbqualified++;
  1004. print '<tr class="oddeven" id="rowid-'.$obj->rowid.'">';
  1005. $tmpaction = 'view';
  1006. $parameters = array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  1007. $reshook = $hookmanager->executeHooks('viewEmailTemplateFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  1008. $error = $hookmanager->error; $errors = $hookmanager->errors;
  1009. if (empty($reshook)) {
  1010. foreach ($fieldlist as $field => $value) {
  1011. if (in_array($fieldlist[$field], array('content', 'content_lines'))) {
  1012. continue;
  1013. }
  1014. $showfield = 1;
  1015. $css = "";
  1016. $class = "tddict";
  1017. $title = '';
  1018. $tmpvar = $fieldlist[$field];
  1019. $valuetoshow = $obj->$tmpvar;
  1020. if ($value == 'label' || $value == 'topic') {
  1021. if ($langs->trans($valuetoshow) != $valuetoshow) {
  1022. $valuetoshow = $langs->trans($valuetoshow);
  1023. }
  1024. $valuetoshow = dol_escape_htmltag($valuetoshow);
  1025. }
  1026. if ($value == 'label') {
  1027. $class .= ' tdoverflowmax100';
  1028. }
  1029. if ($value == 'topic') {
  1030. $class .= ' tdoverflowmax200 small';
  1031. }
  1032. if ($value == 'type_template') {
  1033. $valuetoshow = isset($elementList[$valuetoshow]) ? $elementList[$valuetoshow] : $valuetoshow;
  1034. $css = "center";
  1035. }
  1036. if ($value == 'lang' && $valuetoshow) {
  1037. $valuetoshow = $valuetoshow.' - '.$langs->trans("Language_".$valuetoshow);
  1038. }
  1039. if ($value == 'fk_user') {
  1040. if ($valuetoshow > 0) {
  1041. $fuser = new User($db);
  1042. $fuser->fetch($valuetoshow);
  1043. $valuetoshow = $fuser->getNomUrl(1);
  1044. }
  1045. }
  1046. if ($value == 'private') {
  1047. $css = "center";
  1048. if ($valuetoshow) {
  1049. $valuetoshow = yn($valuetoshow);
  1050. } else {
  1051. $valuetoshow = '';
  1052. }
  1053. }
  1054. if ($value == 'position') {
  1055. $css = "center";
  1056. }
  1057. if (in_array($value, array('joinfiles', 'defaultfortype'))) {
  1058. $css = "center";
  1059. if ($valuetoshow) {
  1060. $valuetoshow = yn(1);
  1061. } else {
  1062. $valuetoshow = '';
  1063. }
  1064. }
  1065. if ($css) {
  1066. $class .= ' '.$css;
  1067. }
  1068. // Show value for field
  1069. if ($showfield) {
  1070. print '<!-- '.$fieldlist[$field].' -->';
  1071. print '<td class="'.$class.'"';
  1072. if (in_array($value, array('code', 'label', 'topic'))) {
  1073. print ' title="'.dol_escape_htmltag($valuetoshow).'"';
  1074. }
  1075. print '>';
  1076. print $valuetoshow;
  1077. print '</td>';
  1078. }
  1079. }
  1080. }
  1081. // Can an entry be erased or disabled ?
  1082. $iserasable = 1; $canbedisabled = 1; $canbemodified = 1; // true by default
  1083. if (!$user->admin && $obj->fk_user != $user->id) {
  1084. $iserasable = 0;
  1085. $canbedisabled = 0;
  1086. $canbemodified = 0;
  1087. }
  1088. $url = $_SERVER["PHP_SELF"].'?'.($page ? 'page='.$page.'&' : '').'sortfield='.$sortfield.'&sortorder='.$sortorder.'&rowid='.(!empty($obj->rowid) ? $obj->rowid : (!empty($obj->code) ? $obj->code : '')).'&code='.(!empty($obj->code) ?urlencode($obj->code) : '');
  1089. if ($param) {
  1090. $url .= '&'.$param;
  1091. }
  1092. // Status / Active
  1093. print '<td class="center nowrap">';
  1094. if ($canbedisabled) {
  1095. print '<a class="reposition" href="'.$url.'&action='.$acts[$obj->active].'&token='.newToken().'">'.$actl[$obj->active].'</a>';
  1096. } else {
  1097. print '<span class="opacitymedium">'.$actl[$obj->active].'</span>';
  1098. }
  1099. print "</td>";
  1100. // Modify link / Delete link
  1101. print '<td class="center nowraponall" width="64">';
  1102. if ($canbemodified) {
  1103. print '<a class="reposition editfielda" href="'.$url.'&action=edit&token='.newToken().'">'.img_edit().'</a>';
  1104. }
  1105. if ($iserasable) {
  1106. print '<a class="marginleftonly" href="'.$url.'&action=delete&token='.newToken().'">'.img_delete().'</a>';
  1107. //else print '<a href="#">'.img_delete().'</a>'; // Some dictionary can be edited by other profile than admin
  1108. }
  1109. print '</td>';
  1110. print "</tr>\n";
  1111. }
  1112. }
  1113. $i++;
  1114. }
  1115. }
  1116. // If no record found
  1117. if ($nbqualified == 0) {
  1118. $colspan = 10;
  1119. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
  1120. }
  1121. print '</table>';
  1122. print '</div>';
  1123. print '</form>';
  1124. if (!empty($user->admin) && (empty($_SESSION['leftmenu']) || $_SESSION['leftmenu'] != 'email_templates')) {
  1125. print dol_get_fiche_end();
  1126. }
  1127. // End of page
  1128. llxFooter();
  1129. $db->close();
  1130. /**
  1131. * Show fields in insert/edit mode
  1132. *
  1133. * @param array $fieldlist Array of fields
  1134. * @param Object $obj If we show a particular record, obj is filled with record fields
  1135. * @param string $tabname Name of SQL table
  1136. * @param string $context 'add'=Output field for the "add form", 'edit'=Output field for the "edit form", 'hide'=Output field for the "add form" but we dont want it to be rendered
  1137. * @return void
  1138. */
  1139. function fieldList($fieldlist, $obj = '', $tabname = '', $context = '')
  1140. {
  1141. global $langs, $user, $db;
  1142. global $form;
  1143. global $elementList;
  1144. $formadmin = new FormAdmin($db);
  1145. foreach ($fieldlist as $field => $value) {
  1146. //print $value;
  1147. if ($value == 'module') {
  1148. print '<td></td>';
  1149. } elseif ($value == 'fk_user') {
  1150. print '<td>';
  1151. if ($user->admin) {
  1152. print $form->select_dolusers(empty($obj->$value) ? '' : $obj->$value, 'fk_user', 1, null, 0, ($user->admin ? '' : 'hierarchyme'), null, 0, 0, 0, '', 0, '', 'minwidth75 maxwidth100');
  1153. } else {
  1154. if ($context == 'add') { // I am not admin and we show the add form
  1155. print $user->getNomUrl(1); // Me
  1156. $forcedvalue = $user->id;
  1157. } else {
  1158. if ($obj && !empty($obj->$value) && $obj->$value > 0) {
  1159. $fuser = new User($db);
  1160. $fuser->fetch($obj->$value);
  1161. print $fuser->getNomUrl(1);
  1162. $forcedvalue = $fuser->id;
  1163. } else {
  1164. $forcedvalue = $obj->$value;
  1165. }
  1166. }
  1167. $keyname = $value;
  1168. print '<input type="hidden" value="'.$forcedvalue.'" name="'.$keyname.'">';
  1169. }
  1170. print '</td>';
  1171. } elseif ($value == 'lang') {
  1172. print '<td>';
  1173. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  1174. $selectedlang = GETPOSTISSET('langcode') ?GETPOST('langcode', 'aZ09') : $langs->defaultlang;
  1175. if ($context == 'edit') {
  1176. $selectedlang = $obj->lang;
  1177. }
  1178. print $formadmin->select_language($selectedlang, 'langcode', 0, null, 1, 0, 0, 'maxwidth100');
  1179. } else {
  1180. if (!empty($obj->lang)) {
  1181. print $obj->lang.' - '.$langs->trans('Language_'.$obj->lang);
  1182. }
  1183. $keyname = $value;
  1184. if ($keyname == 'lang') {
  1185. $keyname = 'langcode'; // Avoid conflict with lang param
  1186. }
  1187. print '<input type="hidden" value="'.(empty($obj->lang) ? '' : $obj->lang).'" name="'.$keyname.'">';
  1188. }
  1189. print '</td>';
  1190. } elseif ($value == 'type_template') {
  1191. // Le type de template
  1192. print '<td class="center">';
  1193. if ($context == 'edit' && !empty($obj->type_template) && !in_array($obj->type_template, array_keys($elementList))) {
  1194. // Current template type is an unknown type, so we must keep it as it is.
  1195. print '<input type="hidden" name="type_template" value="'.$obj->type_template.'">';
  1196. print $obj->type_template;
  1197. } else {
  1198. print $form->selectarray('type_template', $elementList, (!empty($obj->type_template) ? $obj->type_template:''), 1, 0, 0, '', 0, 0, 0, '', 'minwidth75 maxwidth125', 1, '', 0, 1);
  1199. }
  1200. print '</td>';
  1201. } elseif ($context == 'add' && in_array($value, array('topic', 'joinfiles', 'content', 'content_lines'))) {
  1202. //print '<td></td>';
  1203. } elseif ($context == 'edit' && in_array($value, array('topic', 'joinfiles', 'content', 'content_lines'))) {
  1204. print '<td></td>';
  1205. } elseif ($context == 'hide' && in_array($value, array('topic', 'joinfiles', 'content', 'content_lines'))) {
  1206. //print '<td></td>';
  1207. } else {
  1208. $size = ''; $class = ''; $classtd = '';
  1209. if ($value == 'code') {
  1210. $class = 'maxwidth100';
  1211. }
  1212. if ($value == 'label') {
  1213. $class = 'maxwidth200';
  1214. }
  1215. if ($value == 'private') {
  1216. $class = 'maxwidth50';
  1217. $classtd = 'center';
  1218. }
  1219. if ($value == 'position') {
  1220. $class = 'maxwidth50 center';
  1221. $classtd = 'center';
  1222. }
  1223. if ($value == 'topic') {
  1224. $class = 'quatrevingtpercent';
  1225. }
  1226. if ($value == 'defaultfortype') {
  1227. $class = 'width25 center';
  1228. $classtd = 'center';
  1229. }
  1230. print '<td'.($classtd ? ' class="'.$classtd.'"' : '').'>';
  1231. if ($value == 'private') {
  1232. if (empty($user->admin)) {
  1233. print $form->selectyesno($value, '1', 1);
  1234. } else {
  1235. print $form->selectyesno($value, (isset($obj->$value) ? $obj->$value : ''), 1);
  1236. }
  1237. } else {
  1238. print '<input type="text" '.$size.'class="flat'.($class ? ' '.$class : '').'" value="'.(isset($obj->$value) ? $obj->$value : '').'" name="'. $value .'">';
  1239. }
  1240. print '</td>';
  1241. }
  1242. }
  1243. }