mails_templates.php 51 KB

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