dolibarr 22.0.5
html.formwebsite.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
30{
34 private $db;
35
39 public $error;
40
44 public $num;
45
46
52 public function __construct($db)
53 {
54 $this->db = $db;
55 }
56
57
66 public function selectWebsite($selected = '', $htmlname = 'exportmodelid', $useempty = 0)
67 {
68 $out = '';
69
70 $sql = "SELECT rowid, ref";
71 $sql .= " FROM ".$this->db->prefix()."website";
72 $sql .= " WHERE 1 = 1";
73 $sql .= " ORDER BY rowid";
74 $result = $this->db->query($sql);
75 if ($result) {
76 $out .= '<select class="flat minwidth100" name="'.$htmlname.'" id="'.$htmlname.'">';
77 if ($useempty) {
78 $out .= '<option value="-1">&nbsp;</option>';
79 }
80
81 $num = $this->db->num_rows($result);
82 $i = 0;
83 while ($i < $num) {
84 $obj = $this->db->fetch_object($result);
85 if ($selected == $obj->rowid) {
86 $out .= '<option value="'.$obj->rowid.'" selected>';
87 } else {
88 $out .= '<option value="'.$obj->rowid.'">';
89 }
90 $out .= $obj->ref;
91 $out .= '</option>';
92 $i++;
93 }
94 $out .= "</select>";
95 } else {
96 dol_print_error($this->db);
97 }
98
99 return $out;
100 }
101
102
114 public function selectTypeOfContainer($htmlname, $selected = '', $useempty = 0, $moreattrib = '', $addjscombo = 0, $morecss = 'minwidth200')
115 {
116 global $langs, $conf, $user;
117
118 $langs->load("admin");
119
120 $out = '';
121
122 $sql = "SELECT rowid, code, label, entity, position, typecontainer";
123 $sql .= " FROM ".$this->db->prefix().'c_type_container';
124 $sql .= " WHERE active = 1 AND entity IN (".getEntity('c_type_container').")";
125 $sql .= " ORDER BY position ASC, typecontainer DESC, label ASC";
126
127 dol_syslog(get_class($this)."::selectTypeOfContainer", LOG_DEBUG);
128
129 $result = $this->db->query($sql);
130 if ($result) {
131 $num = $this->db->num_rows($result);
132 $i = 0;
133 if ($num) {
134 $out .= '<select id="select'.$htmlname.'" class="flat selectTypeOfContainer'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
135 if ($useempty == 1 || ($useempty == 2 && $num > 1)) {
136 $out .= '<option value="-1">&nbsp;</option>';
137 }
138
139 $lasttypecontainer = '';
140 while ($i < $num) {
141 $obj = $this->db->fetch_object($result);
142 /*if (in_array($obj->typecontainer, array('library', 'service'))) {
143 if (!getDolGlobalString('WEBSITE_ADD_PAGE_TYPE_PHPLIB')) {
144 $i++;
145 continue;
146 }
147 }*/
148 if ($obj->typecontainer != $lasttypecontainer) {
149 $out .= '<option value="0" disabled>--- ';
150 $transcodecontainer = ucfirst($obj->typecontainer); // Label of group of page type
151 if ($transcodecontainer == 'Library') {
152 $transcodecontainer = 'System';
153 }
154 if ($obj->typecontainer == 'page') {
155 $transcodecontainer = 'CompletePage';
156 } elseif ($obj->typecontainer == 'container') {
157 $transcodecontainer = 'PortionOfPage';
158 } elseif ($obj->typecontainer == 'service') {
159 $transcodecontainer = 'ServiceComponent';
160 }
161 $out .= $langs->trans($transcodecontainer);
162 $out .= ' ---</option>';
163 $lasttypecontainer = $obj->typecontainer;
164 }
165
166 if ($selected == $obj->rowid || $selected == $obj->code) {
167 $out .= '<option value="'.$obj->code.'" selected>';
168 } else {
169 $out .= '<option value="'.$obj->code.'">';
170 }
171 $out .= $langs->trans($obj->label);
172 $out .= '</option>';
173
174 $conf->cache['type_of_container'][$obj->code] = $obj->label;
175
176 $i++;
177 }
178 $out .= "</select>";
179 if ($user->admin) {
180 $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
181 }
182
183 if ($addjscombo) {
184 $out .= ajax_combobox('select'.$htmlname);
185 }
186 } else {
187 $out .= $langs->trans("NoTypeOfPagePleaseEditDictionary");
188 }
189 } else {
190 $this->error = $this->db->lasterror();
191 }
192
193 return $out;
194 }
195
207 public function selectSampleOfContainer($htmlname, $selected = '', $useempty = 0, $moreattrib = '', $addjscombo = 0, $morecss = 'minwidth200')
208 {
209 global $langs, $user;
210
211 $langs->load("admin");
212
213 $listofsamples = dol_dir_list(DOL_DOCUMENT_ROOT.'/website/samples', 'files', 0, '^page-sample-.*\.html$');
214 $arrayofsamples = array();
215 $arrayofsamples['empty'] = 'EmptyPage'; // Always this one first
216 foreach ($listofsamples as $sample) {
217 $reg = array();
218 if (preg_match('/^page-sample-(.*)\.html$/', $sample['name'], $reg)) {
219 $key = $reg[1];
220 $labelkey = ucfirst($key);
221 if ($key == 'empty') {
222 $labelkey = 'EmptyPage';
223 }
224 $arrayofsamples[$key] = $labelkey;
225 }
226 }
227
228 $out = '';
229 $out .= '<select id="select'.$htmlname.'" class="selectSampleOfContainer'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
230
231 if ($useempty == 1 || $useempty == 2) {
232 $out .= '<option value="-1">&nbsp;</option>';
233 }
234
235 foreach ($arrayofsamples as $key => $val) {
236 if ($selected == $key) {
237 $out .= '<option value="'.$key.'" selected>';
238 } else {
239 $out .= '<option value="'.$key.'">';
240 }
241 $out .= $langs->trans($val);
242 $out .= '</option>';
243 }
244 $out .= "</select>";
245
246 if ($addjscombo) {
247 $out .= ajax_combobox('select'.$htmlname);
248 }
249
250 return $out;
251 }
252
253
267 public function selectContainer($website, $htmlname = 'pageid', $pageid = 0, $showempty = 0, $action = '', $morecss = 'minwidth200', $excludeids = null)
268 {
269 global $conf, $langs;
270
271 $this->num = 0;
272
273 $atleastonepage = (is_array($website->lines) && count($website->lines) > 0);
274
275 $out = '';
276 if ($atleastonepage && $action != 'editsource') {
277 $out .= '<select name="'.$htmlname.'" id="'.$htmlname.'" class="maxwidth300'.($morecss ? ' '.$morecss : '').'">';
278 } else {
279 $out .= '<select name="pageidbis" id="pageid" class="maxwidth300'.($morecss ? ' '.$morecss : '').'"'.($action == 'editsource' ? ' disabled="disabled"' : '').'>';
280 }
281
282 if ($showempty || !$atleastonepage) {
283 $out .= '<option class="optiongrey" value="-1">'.(is_numeric($showempty) ? '&nbsp;' : $showempty).'</option>';
284 }
285
286 /*if (!empty($conf->use_javascript_ajax)) {
287 $valueoption = '<span class="classlink">'.img_picto('', 'add', 'class="paddingrightonly"').$langs->trans("AddPage").'</span>';
288 $out .= '<option value="-2" data-html="'.dol_escape_htmltag($valueoption).'">'.$valueoption.'</option>';
289 }*/
290
291 if ($atleastonepage) {
292 if (empty($pageid) && $action != 'createcontainer') { // Page id is not defined, we try to take one
293 $firstpageid = 0;
294 $homepageid = 0;
295 foreach ($website->lines as $key => $valpage) {
296 if (empty($firstpageid)) {
297 $firstpageid = $valpage->id;
298 }
299 if ($website->fk_default_home && $key == $website->fk_default_home) {
300 $homepageid = $valpage->id;
301 }
302 }
303 $pageid = $homepageid ? $homepageid : $firstpageid; // We choose home page and if not defined yet, we take first page
304 }
305
306 foreach ($website->lines as $key => $valpage) {
307 if (is_array($excludeids) && count($excludeids) && in_array($valpage->id, $excludeids)) {
308 continue;
309 }
310
311 $valueforoption = '<span class="opacitymedium">['.$valpage->type_container.' '.sprintf("%03d", $valpage->id).']</span> ';
312 $valueforoption .= $valpage->pageurl.' - '.$valpage->title;
313 if ($website->otherlang) { // If there is alternative lang for this web site, we show the language code
314 if ($valpage->lang) {
315 $valueforoption .= ' <span class="opacitymedium">('.$valpage->lang.')</span>';
316 }
317 }
318 if ($website->fk_default_home && $key == $website->fk_default_home) {
319 //$valueforoption .= ' <span class="opacitymedium">('.$langs->trans("HomePage").')</span>';
320 $valueforoption .= ' <span class="opacitymedium fas fa-home"></span>';
321 }
322
323 $out .= '<option value="'.$key.'"';
324 if ($pageid > 0 && $pageid == $key) {
325 $out .= ' selected'; // To preselect a value
326 }
327 $out .= ' data-html="'.dol_escape_htmltag($valueforoption).'"';
328 $out .= '>';
329 $out .= $valueforoption;
330 $out .= '</option>';
331
332 ++$this->num;
333 }
334 }
335 $out .= '</select>';
336
337 if ($atleastonepage && $action != 'editsource') {
338 $out .= ajax_combobox($htmlname);
339 } else {
340 $out .= '<input type="hidden" name="'.$htmlname.'" value="'.$pageid.'">';
341 $out .= ajax_combobox($htmlname);
342 }
343 return $out;
344 }
345
346
353 public function getContentPageTemplate($htmlContent = 'message')
354 {
355 global $user, $langs;
356
357 $htmlContent = preg_replace('/[^a-z0-9_]/', '', $htmlContent);
358
359 require_once DOL_DOCUMENT_ROOT.'/core/lib/emaillayout.lib.php';
360
361 $listofsamples = dol_dir_list(DOL_DOCUMENT_ROOT.'/website/samples', 'files', 0, '^page-sample-.*\.html$');
362 $arrayofsamples = array();
363 $arrayofsamples['empty'] = 'EmptyPage'; // Always this one first
364 foreach ($listofsamples as $sample) {
365 $reg = array();
366 if (preg_match('/^page-sample-(.*)\.html$/', $sample['name'], $reg)) {
367 $key = $reg[1];
368 $labelkey = ucfirst($key);
369 if ($key == 'empty') {
370 $labelkey = 'EmptyPage';
371 }
372 $arrayofsamples[$key] = $labelkey;
373 }
374 }
375 $out = '<div id="template-selector" class="template-selector template-container hidden">';
376
377 // We disable some not ready templates
378 unset($arrayofsamples['dynamiccontent']);
379 //unset($arrayofsamples['news']);
380
381 $templates = $arrayofsamples;
382
383 foreach ($templates as $template => $templateFunction) {
384 $substitutionarray = array();
385 $substitutionarray['__WEBSITE_CREATED_BY__'] = $user->getFullName($langs);
386 $substitutionarray['__WEBSITE_CONTENT__'] = $langs->trans("WebpageContent");
387 $substitutionarray['__WEBSITE_TITLE1__'] = $langs->trans("Title1");
388 $substitutionarray['__WEBSITE_TITLE2__'] = $langs->trans("Title2");
389
390 $pathtoTemplateFile = DOL_DOCUMENT_ROOT.'/website/samples/page-sample-'.dol_sanitizeFileName($template).'.html';
391 $contentHtml = file_exists($pathtoTemplateFile) ? make_substitutions(@file_get_contents($pathtoTemplateFile), $substitutionarray) : '';
392
393 $out .= '<div class="template-option" data-template="'.$template.'" data-content="'.htmlentities($contentHtml).'">';
394 $out .= '<img class="maillayout" alt="'.$template.'" src="'.DOL_URL_ROOT.'/theme/common/maillayout/'.$template.'.png" />';
395 $out .= '<span class="template-option-text">'.($template != 'text' ? ucfirst($template) : ucfirst($templateFunction)).'</span>';
396 $out .= '</div>';
397 }
398 $out .= '<input type="hidden" name="sample" value="" />';
399 $out .= '</div>';
400
401 $out .= '<!-- Js code to manage choice of a page layout for website -->'."\n";
402 $out .= '<script type="text/javascript">
403 $(document).ready(function() {
404 $(".template-option").click(function() {
405 console.log("We choose a layout for website, we fill the field \''.$htmlContent.'\'");
406
407 $(".template-option").removeClass("selected");
408 $(this).addClass("selected");
409
410 var template = $(this).data("template");
411 var contentHtml = $(this).data("content");
412
413 jQuery("#'.$htmlContent.'").val(contentHtml);
414 //jQuery("#'.$htmlContent.'preview").val(contentHtml);
415
416 var editorInstance = CKEDITOR.instances.'.$htmlContent.';
417 if (editorInstance) {
418 editorInstance.setData(contentHtml);
419 }
420 //var editorInstance = CKEDITOR.instances.'.$htmlContent.'preview;
421 //if (editorInstance) {
422 // editorInstance.setData(contentHtml);
423 //}
424 });
425 });
426 </script>';
427
428 return $out;
429 }
430}
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:475
Class to manage component html for module website.
selectContainer($website, $htmlname='pageid', $pageid=0, $showempty=0, $action='', $morecss='minwidth200', $excludeids=null)
Return a HTML select list of containers of a website.
selectWebsite($selected='', $htmlname='exportmodelid', $useempty=0)
Return HTML select list of websites.
selectTypeOfContainer($htmlname, $selected='', $useempty=0, $moreattrib='', $addjscombo=0, $morecss='minwidth200')
Return a HTML select list of type of containers from the dictionary.
__construct($db)
Constructor.
selectSampleOfContainer($htmlname, $selected='', $useempty=0, $moreattrib='', $addjscombo=0, $morecss='minwidth200')
Return a HTML select list of samples of containers content.
getContentPageTemplate($htmlContent='message')
Return HTML code for selection of page layout.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:63
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='')
Show information in HTML for admin users or standard users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79