dolibarr 21.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 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);
151 if ($obj->typecontainer == 'page') {
152 $transcodecontainer = 'CompletePage';
153 } elseif ($obj->typecontainer == 'container') {
154 $transcodecontainer = 'PortionOfPage';
155 } elseif ($obj->typecontainer == 'service') {
156 $transcodecontainer = 'ServiceComponent';
157 }
158 $out .= $langs->trans($transcodecontainer);
159 $out .= ' ---</option>';
160 $lasttypecontainer = $obj->typecontainer;
161 }
162
163 if ($selected == $obj->rowid || $selected == $obj->code) {
164 $out .= '<option value="'.$obj->code.'" selected>';
165 } else {
166 $out .= '<option value="'.$obj->code.'">';
167 }
168 $out .= $langs->trans($obj->label);
169 $out .= '</option>';
170
171 $conf->cache['type_of_container'][$obj->code] = $obj->label;
172
173 $i++;
174 }
175 $out .= "</select>";
176 if ($user->admin) {
177 $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
178 }
179
180 if ($addjscombo) {
181 $out .= ajax_combobox('select'.$htmlname);
182 }
183 } else {
184 $out .= $langs->trans("NoTypeOfPagePleaseEditDictionary");
185 }
186 } else {
187 $this->error = $this->db->lasterror();
188 }
189
190 return $out;
191 }
192
204 public function selectSampleOfContainer($htmlname, $selected = '', $useempty = 0, $moreattrib = '', $addjscombo = 0, $morecss = 'minwidth200')
205 {
206 global $langs, $user;
207
208 $langs->load("admin");
209
210 $listofsamples = dol_dir_list(DOL_DOCUMENT_ROOT.'/website/samples', 'files', 0, '^page-sample-.*\.html$');
211 $arrayofsamples = array();
212 $arrayofsamples['empty'] = 'EmptyPage'; // Always this one first
213 foreach ($listofsamples as $sample) {
214 $reg = array();
215 if (preg_match('/^page-sample-(.*)\.html$/', $sample['name'], $reg)) {
216 $key = $reg[1];
217 $labelkey = ucfirst($key);
218 if ($key == 'empty') {
219 $labelkey = 'EmptyPage';
220 }
221 $arrayofsamples[$key] = $labelkey;
222 }
223 }
224
225 $out = '';
226 $out .= '<select id="select'.$htmlname.'" class="selectSampleOfContainer'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
227
228 if ($useempty == 1 || $useempty == 2) {
229 $out .= '<option value="-1">&nbsp;</option>';
230 }
231
232 foreach ($arrayofsamples as $key => $val) {
233 if ($selected == $key) {
234 $out .= '<option value="'.$key.'" selected>';
235 } else {
236 $out .= '<option value="'.$key.'">';
237 }
238 $out .= $langs->trans($val);
239 $out .= '</option>';
240 }
241 $out .= "</select>";
242
243 if ($addjscombo) {
244 $out .= ajax_combobox('select'.$htmlname);
245 }
246
247 return $out;
248 }
249
250
264 public function selectContainer($website, $htmlname = 'pageid', $pageid = 0, $showempty = 0, $action = '', $morecss = 'minwidth200', $excludeids = null)
265 {
266 global $conf, $langs;
267
268 $this->num = 0;
269
270 $atleastonepage = (is_array($website->lines) && count($website->lines) > 0);
271
272 $out = '';
273 if ($atleastonepage && $action != 'editsource') {
274 $out .= '<select name="'.$htmlname.'" id="'.$htmlname.'" class="maxwidth300'.($morecss ? ' '.$morecss : '').'">';
275 } else {
276 $out .= '<select name="pageidbis" id="pageid" class="maxwidth300'.($morecss ? ' '.$morecss : '').'"'.($action == 'editsource' ? ' disabled="disabled"' : '').'>';
277 }
278
279 if ($showempty || !$atleastonepage) {
280 $out .= '<option class="optiongrey" value="-1">'.(is_numeric($showempty) ? '&nbsp;' : $showempty).'</option>';
281 }
282
283 /*if (!empty($conf->use_javascript_ajax)) {
284 $valueoption = '<span class="classlink">'.img_picto('', 'add', 'class="paddingrightonly"').$langs->trans("AddPage").'</span>';
285 $out .= '<option value="-2" data-html="'.dol_escape_htmltag($valueoption).'">'.$valueoption.'</option>';
286 }*/
287
288 if ($atleastonepage) {
289 if (empty($pageid) && $action != 'createcontainer') { // Page id is not defined, we try to take one
290 $firstpageid = 0;
291 $homepageid = 0;
292 foreach ($website->lines as $key => $valpage) {
293 if (empty($firstpageid)) {
294 $firstpageid = $valpage->id;
295 }
296 if ($website->fk_default_home && $key == $website->fk_default_home) {
297 $homepageid = $valpage->id;
298 }
299 }
300 $pageid = $homepageid ? $homepageid : $firstpageid; // We choose home page and if not defined yet, we take first page
301 }
302
303 foreach ($website->lines as $key => $valpage) {
304 if (is_array($excludeids) && count($excludeids) && in_array($valpage->id, $excludeids)) {
305 continue;
306 }
307
308 $valueforoption = '<span class="opacitymedium">['.$valpage->type_container.' '.sprintf("%03d", $valpage->id).']</span> ';
309 $valueforoption .= $valpage->pageurl.' - '.$valpage->title;
310 if ($website->otherlang) { // If there is alternative lang for this web site, we show the language code
311 if ($valpage->lang) {
312 $valueforoption .= ' <span class="opacitymedium">('.$valpage->lang.')</span>';
313 }
314 }
315 if ($website->fk_default_home && $key == $website->fk_default_home) {
316 //$valueforoption .= ' <span class="opacitymedium">('.$langs->trans("HomePage").')</span>';
317 $valueforoption .= ' <span class="opacitymedium fas fa-home"></span>';
318 }
319
320 $out .= '<option value="'.$key.'"';
321 if ($pageid > 0 && $pageid == $key) {
322 $out .= ' selected'; // To preselect a value
323 }
324 $out .= ' data-html="'.dol_escape_htmltag($valueforoption).'"';
325 $out .= '>';
326 $out .= $valueforoption;
327 $out .= '</option>';
328
329 ++$this->num;
330 }
331 }
332 $out .= '</select>';
333
334 if ($atleastonepage && $action != 'editsource') {
335 $out .= ajax_combobox($htmlname);
336 } else {
337 $out .= '<input type="hidden" name="'.$htmlname.'" value="'.$pageid.'">';
338 $out .= ajax_combobox($htmlname);
339 }
340 return $out;
341 }
342
343
350 public function getContentPageTemplate($htmlContent = 'message')
351 {
352 global $user, $langs;
353
354 $htmlContent = preg_replace('/[^a-z0-9_]/', '', $htmlContent);
355
356 require_once DOL_DOCUMENT_ROOT.'/core/lib/emaillayout.lib.php';
357
358 $listofsamples = dol_dir_list(DOL_DOCUMENT_ROOT.'/website/samples', 'files', 0, '^page-sample-.*\.html$');
359 $arrayofsamples = array();
360 $arrayofsamples['empty'] = 'EmptyPage'; // Always this one first
361 foreach ($listofsamples as $sample) {
362 $reg = array();
363 if (preg_match('/^page-sample-(.*)\.html$/', $sample['name'], $reg)) {
364 $key = $reg[1];
365 $labelkey = ucfirst($key);
366 if ($key == 'empty') {
367 $labelkey = 'EmptyPage';
368 }
369 $arrayofsamples[$key] = $labelkey;
370 }
371 }
372 $out = '<div id="template-selector" class="template-container hidden">';
373
374 // We disable some not ready templates
375 unset($arrayofsamples['dynamiccontent']);
376 //unset($arrayofsamples['news']);
377
378 $templates = $arrayofsamples;
379
380 foreach ($templates as $template => $templateFunction) {
381 $substitutionarray = array();
382 $substitutionarray['__WEBSITE_CREATED_BY__'] = $user->getFullName($langs);
383 $substitutionarray['__WEBSITE_CONTENT__'] = $langs->trans("WebpageContent");
384 $substitutionarray['__WEBSITE_TITLE1__'] = $langs->trans("Title1");
385 $substitutionarray['__WEBSITE_TITLE2__'] = $langs->trans("Title2");
386
387 $pathtoTemplateFile = DOL_DOCUMENT_ROOT.'/website/samples/page-sample-'.dol_sanitizeFileName($template).'.html';
388 $contentHtml = file_exists($pathtoTemplateFile) ? make_substitutions(@file_get_contents($pathtoTemplateFile), $substitutionarray) : '';
389
390 $out .= '<div class="template-option" data-template="'.$template.'" data-content="'.htmlentities($contentHtml).'">';
391 $out .= '<img class="maillayout" alt="'.$template.'" src="'.DOL_URL_ROOT.'/theme/common/maillayout/'.$template.'.png" />';
392 $out .= '<span class="template-option-text">'.($template != 'text' ? ucfirst($template) : ucfirst($templateFunction)).'</span>';
393 $out .= '</div>';
394 }
395 $out .= '<input type="hidden" name="sample" value="" />';
396 $out .= '</div>';
397
398 $out .= '<script type="text/javascript">
399 $(document).ready(function() {
400 $(".template-option").click(function() {
401 console.log("We choose a layout for website, we fill the field \''.$htmlContent.'\'");
402
403 $(".template-option").removeClass("selected");
404 $(this).addClass("selected");
405
406 var template = $(this).data("template");
407 var contentHtml = $(this).data("content");
408
409 jQuery("#'.$htmlContent.'").val(contentHtml);
410 //jQuery("#'.$htmlContent.'preview").val(contentHtml);
411
412 var editorInstance = CKEDITOR.instances.'.$htmlContent.';
413 if (editorInstance) {
414 editorInstance.setData(contentHtml);
415 }
416 //var editorInstance = CKEDITOR.instances.'.$htmlContent.'preview;
417 //if (editorInstance) {
418 // editorInstance.setData(contentHtml);
419 //}
420 });
421 });
422 </script>';
423
424 return $out;
425 }
426}
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:457
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_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.