dolibarr 25.0.0-alpha
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
141 // Sort by group of type first, then position: sorting on position only can
142 // interleave the groups and make them appear several times in the list.
143 $arrayofcontainers = array();
144 while ($i < $num) {
145 $arrayofcontainers[] = $this->db->fetch_object($result);
146 $i++;
147 }
148 usort($arrayofcontainers, array('FormWebsite', 'compareTypeOfContainer'));
149
150 foreach ($arrayofcontainers as $obj) {
151 /*if (in_array($obj->typecontainer, array('library', 'service'))) {
152 if (!getDolGlobalString('WEBSITE_ADD_PAGE_TYPE_PHPLIB')) {
153 $i++;
154 continue;
155 }
156 }*/
157 if ($obj->typecontainer != $lasttypecontainer) {
158 $out .= '<option value="0" disabled>--- ';
159 $transcodecontainer = ucfirst($obj->typecontainer); // Label of group of page type
160 if ($transcodecontainer == 'Library') {
161 $transcodecontainer = 'System';
162 }
163 if ($obj->typecontainer == 'page') {
164 $transcodecontainer = 'CompletePage';
165 } elseif ($obj->typecontainer == 'container') {
166 $transcodecontainer = 'PortionOfPage';
167 } elseif ($obj->typecontainer == 'service') {
168 $transcodecontainer = 'ServiceComponent';
169 }
170 $out .= $langs->trans($transcodecontainer);
171 $out .= ' ---</option>';
172 $lasttypecontainer = $obj->typecontainer;
173 }
174
175 if ($selected == $obj->rowid || $selected == $obj->code) {
176 $out .= '<option value="'.$obj->code.'" selected>';
177 } else {
178 $out .= '<option value="'.$obj->code.'">';
179 }
180 $out .= $langs->trans($obj->label);
181 $out .= '</option>';
182
183 $conf->cache['type_of_container'][$obj->code] = $obj->label;
184 }
185 $out .= "</select>";
186 if ($user->admin) {
187 $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
188 }
189
190 if ($addjscombo) {
191 $out .= ajax_combobox('select'.$htmlname);
192 }
193 } else {
194 $out .= $langs->trans("NoTypeOfPagePleaseEditDictionary");
195 }
196 } else {
197 $this->error = $this->db->lasterror();
198 }
199
200 return $out;
201 }
202
211 public static function compareTypeOfContainer($a, $b)
212 {
213 $rankofgroup = array('page' => 0, 'container' => 1, 'library' => 2);
214
215 $ga = array_key_exists($a->typecontainer, $rankofgroup) ? $rankofgroup[$a->typecontainer] : 3;
216 $gb = array_key_exists($b->typecontainer, $rankofgroup) ? $rankofgroup[$b->typecontainer] : 3;
217 if ($ga != $gb) {
218 return $ga - $gb;
219 }
220 if ($a->position != $b->position) {
221 return $a->position - $b->position;
222 }
223 return strcmp($a->label, $b->label);
224 }
225
237 public function selectSampleOfContainer($htmlname, $selected = '', $useempty = 0, $moreattrib = '', $addjscombo = 0, $morecss = 'minwidth200')
238 {
239 global $langs, $user;
240
241 $langs->load("admin");
242
243 $listofsamples = dol_dir_list(DOL_DOCUMENT_ROOT.'/website/samples', 'files', 0, '^page-sample-.*\.html$');
244 $arrayofsamples = array();
245 $arrayofsamples['empty'] = 'EmptyPage'; // Always this one first
246 foreach ($listofsamples as $sample) {
247 $reg = array();
248 if (preg_match('/^page-sample-(.*)\.html$/', $sample['name'], $reg)) {
249 $key = $reg[1];
250 $labelkey = ucfirst($key);
251 if ($key == 'empty') {
252 $labelkey = 'EmptyPage';
253 }
254 $arrayofsamples[$key] = $labelkey;
255 }
256 }
257
258 $out = '';
259 $out .= '<select id="select'.$htmlname.'" class="selectSampleOfContainer'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
260
261 if ($useempty == 1 || $useempty == 2) {
262 $out .= '<option value="-1">&nbsp;</option>';
263 }
264
265 foreach ($arrayofsamples as $key => $val) {
266 if ($selected == $key) {
267 $out .= '<option value="'.$key.'" selected>';
268 } else {
269 $out .= '<option value="'.$key.'">';
270 }
271 $out .= $langs->trans($val);
272 $out .= '</option>';
273 }
274 $out .= "</select>";
275
276 if ($addjscombo) {
277 $out .= ajax_combobox('select'.$htmlname);
278 }
279
280 return $out;
281 }
282
283
297 public function selectContainer($website, $htmlname = 'pageid', $pageid = 0, $showempty = 0, $action = '', $morecss = 'minwidth200', $excludeids = null)
298 {
299 global $conf, $langs;
300
301 $this->num = 0;
302
303 $atleastonepage = (is_array($website->lines) && count($website->lines) > 0);
304
305 $out = '';
306 if ($atleastonepage && $action != 'editsource') {
307 $out .= '<select name="'.$htmlname.'" id="'.$htmlname.'" class="maxwidth300'.($morecss ? ' '.$morecss : '').'">';
308 } else {
309 $out .= '<select name="pageidbis" id="pageid" class="maxwidth300'.($morecss ? ' '.$morecss : '').'"'.($action == 'editsource' ? ' disabled="disabled"' : '').'>';
310 }
311
312 if ($showempty || !$atleastonepage) {
313 $out .= '<option class="optiongrey" value="-1">'.(is_numeric($showempty) ? '&nbsp;' : $showempty).'</option>';
314 }
315
316 /*if (!empty($conf->use_javascript_ajax)) {
317 $valueoption = '<span class="classlink">'.img_picto('', 'add', 'class="paddingrightonly"').$langs->trans("AddPage").'</span>';
318 $out .= '<option value="-2" data-html="'.dol_escape_htmltag($valueoption).'">'.$valueoption.'</option>';
319 }*/
320
321 if ($atleastonepage) {
322 if (empty($pageid) && $action != 'createcontainer') { // Page id is not defined, we try to take one
323 $firstpageid = 0;
324 $homepageid = 0;
325 foreach ($website->lines as $key => $valpage) {
326 if (empty($firstpageid)) {
327 $firstpageid = $valpage->id;
328 }
329 if ($website->fk_default_home && $key == $website->fk_default_home) {
330 $homepageid = $valpage->id;
331 }
332 }
333 $pageid = $homepageid ? $homepageid : $firstpageid; // We choose home page and if not defined yet, we take first page
334 }
335
336 foreach ($website->lines as $key => $valpage) {
337 if (is_array($excludeids) && count($excludeids) && in_array($valpage->id, $excludeids)) {
338 continue;
339 }
340
341 $valueforoption = '<span class="opacitymedium">['.$valpage->type_container.' '.sprintf("%03d", $valpage->id).']</span> ';
342 $valueforoption .= $valpage->pageurl.' - '.$valpage->title;
343 if ($website->otherlang) { // If there is alternative lang for this web site, we show the language code
344 if ($valpage->lang) {
345 $valueforoption .= ' <span class="opacitymedium">('.$valpage->lang.')</span>';
346 }
347 }
348 if ($website->fk_default_home && $key == $website->fk_default_home) {
349 //$valueforoption .= ' <span class="opacitymedium">('.$langs->trans("HomePage").')</span>';
350 $valueforoption .= ' <span class="opacitymedium fas fa-home"></span>';
351 }
352
353 $out .= '<option value="'.$key.'"';
354 if ($pageid > 0 && $pageid == $key) {
355 $out .= ' selected'; // To preselect a value
356 }
357 $out .= ' data-html="'.dol_escape_htmltag($valueforoption).'"';
358 $out .= '>';
359 $out .= $valueforoption;
360 $out .= '</option>';
361
362 ++$this->num;
363 }
364 }
365 $out .= '</select>';
366
367 if ($atleastonepage && $action != 'editsource') {
368 $out .= ajax_combobox($htmlname);
369 } else {
370 $out .= '<input type="hidden" name="'.$htmlname.'" value="'.$pageid.'">';
371 $out .= ajax_combobox($htmlname);
372 }
373 return $out;
374 }
375
376
384 public function getContentPageTemplate($htmlContent = 'message')
385 {
386 global $user, $langs;
387
388 $htmlContent = preg_replace('/[^a-z0-9_]/', '', $htmlContent);
389
390 require_once DOL_DOCUMENT_ROOT.'/core/lib/emaillayout.lib.php';
391
392 $listofsamples = dol_dir_list(DOL_DOCUMENT_ROOT.'/website/samples', 'files', 0, '^page-sample-.*\.html$');
393
394 // Define list of layouts to use
395 $arrayofsamples = array(
396 'empty' => 'EmptyPage',
397 );
398
399 foreach ($listofsamples as $sample) {
400 $reg = array();
401 if (preg_match('/^page-sample-(.*)\.html$/', $sample['name'], $reg)) {
402 $key = $reg[1];
403 $labelkey = ucfirst($key);
404 if ($key == 'empty') {
405 $labelkey = 'EmptyPage';
406 }
407 $arrayofsamples[$key] = $labelkey;
408 }
409 }
410 $out = '<div id="template-selector" class="template-selector template-container hidden">';
411
412 // We disable some not ready templates
413 unset($arrayofsamples['dynamiccontent']);
414 //unset($arrayofsamples['news']);
415
416 $templates = $arrayofsamples;
417
418 foreach ($templates as $template => $templateFunction) {
419 $substitutionarray = array();
420 $substitutionarray['__WEBSITE_CREATED_BY__'] = $user->getFullName($langs);
421 $substitutionarray['__WEBSITE_CONTENT__'] = $langs->trans("WebpageContent");
422 $substitutionarray['__WEBSITE_TITLE1__'] = $langs->trans("Title1");
423 $substitutionarray['__WEBSITE_TITLE2__'] = $langs->trans("Title2");
424
425 $pathtoTemplateFile = DOL_DOCUMENT_ROOT.'/website/samples/page-sample-'.dol_sanitizeFileName($template).'.html';
426 $contentHtml = file_exists($pathtoTemplateFile) ? make_substitutions(@file_get_contents($pathtoTemplateFile), $substitutionarray) : '';
427
428 $out .= '<div class="template-option" data-template="'.$template.'" data-content="'.htmlentities($contentHtml).'">';
429 $out .= '<img class="maillayout" alt="'.$template.'" src="'.DOL_URL_ROOT.'/theme/common/maillayout/'.($template === 'empty' ? 'none' : $template).'.png" />';
430 $out .= '<span class="template-option-text">'.($template != 'text' ? ucfirst($template) : ucfirst($templateFunction)).'</span>';
431 $out .= '</div>';
432 }
433 $out .= '<input type="hidden" name="sample" value="" />';
434 $out .= '</div>';
435
436 $out .= '<!-- Js code to manage choice of a page layout for website -->'."\n";
437 $out .= '<script type="text/javascript">
438 $(document).ready(function() {
439 $(".template-option").click(function() {
440 console.log("We choose a layout for website, we fill the field \''.$htmlContent.'\'");
441
442 $(".template-option").removeClass("selected");
443 $(this).addClass("selected");
444
445 var template = $(this).data("template");
446 var contentHtml = $(this).data("content");
447
448 jQuery("#'.$htmlContent.'").val(contentHtml);
449 //jQuery("#'.$htmlContent.'preview").val(contentHtml);
450
451 var editorInstance = CKEDITOR.instances.'.$htmlContent.';
452 if (editorInstance) {
453 editorInstance.setData(contentHtml);
454 }
455 //var editorInstance = CKEDITOR.instances.'.$htmlContent.'preview;
456 //if (editorInstance) {
457 // editorInstance.setData(contentHtml);
458 //}
459 });
460 });
461 </script>';
462
463 return $out;
464 }
465}
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='', $placeholder='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:488
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.
static compareTypeOfContainer($a, $b)
Compare 2 types of container to sort them by group of type, then position, then label.
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:66
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='', $textonpictotooltip='', $cssfordropdown='info_admin')
Show information in HTML for admin users or standard users.