mails_templates.php 52 KB

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