mails_templates.php 49 KB

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