mails_templates.php 49 KB

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