mails_templates.php 46 KB

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