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  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
29 {
30  private $db;
31 
35  public $error;
36 
40  public $num;
41 
42 
48  public function __construct($db)
49  {
50  $this->db = $db;
51  }
52 
53 
62  public function selectWebsite($selected = '', $htmlname = 'exportmodelid', $useempty = 0)
63  {
64  $out = '';
65 
66  $sql = "SELECT rowid, ref";
67  $sql .= " FROM ".$this->db->prefix()."website";
68  $sql .= " WHERE 1 = 1";
69  $sql .= " ORDER BY rowid";
70  $result = $this->db->query($sql);
71  if ($result) {
72  $out .= '<select class="flat minwidth100" name="'.$htmlname.'" id="'.$htmlname.'">';
73  if ($useempty) {
74  $out .= '<option value="-1">&nbsp;</option>';
75  }
76 
77  $num = $this->db->num_rows($result);
78  $i = 0;
79  while ($i < $num) {
80  $obj = $this->db->fetch_object($result);
81  if ($selected == $obj->rowid) {
82  $out .= '<option value="'.$obj->rowid.'" selected>';
83  } else {
84  $out .= '<option value="'.$obj->rowid.'">';
85  }
86  $out .= $obj->ref;
87  $out .= '</option>';
88  $i++;
89  }
90  $out .= "</select>";
91  } else {
92  dol_print_error($this->db);
93  }
94 
95  return $out;
96  }
97 
98 
110  public function selectTypeOfContainer($htmlname, $selected = '', $useempty = 0, $moreattrib = '', $addjscombo = 0, $morecss = 'minwidth200')
111  {
112  global $langs, $conf, $user;
113 
114  $langs->load("admin");
115 
116  $out = '';
117 
118  $sql = "SELECT rowid, code, label, entity, position, typecontainer";
119  $sql .= " FROM ".$this->db->prefix().'c_type_container';
120  $sql .= " WHERE active = 1 AND entity IN (".getEntity('c_type_container').")";
121  $sql .= " ORDER BY position ASC, typecontainer DESC, label ASC";
122 
123  dol_syslog(get_class($this)."::selectTypeOfContainer", LOG_DEBUG);
124 
125  $result = $this->db->query($sql);
126  if ($result) {
127  $num = $this->db->num_rows($result);
128  $i = 0;
129  if ($num) {
130  $out .= '<select id="select'.$htmlname.'" class="flat selectTypeOfContainer'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
131  if ($useempty == 1 || ($useempty == 2 && $num > 1)) {
132  $out .= '<option value="-1">&nbsp;</option>';
133  }
134 
135  $lasttypecontainer = '';
136  while ($i < $num) {
137  $obj = $this->db->fetch_object($result);
138  /*if (in_array($obj->typecontainer, array('library', 'service'))) {
139  if (!getDolGlobalString('WEBSITE_ADD_PAGE_TYPE_PHPLIB')) {
140  $i++;
141  continue;
142  }
143  }*/
144  if ($obj->typecontainer != $lasttypecontainer) {
145  $out .= '<option value="0" disabled>--- ';
146  $transcodecontainer = ucfirst($obj->typecontainer);
147  if ($obj->typecontainer == 'page') {
148  $transcodecontainer = 'CompletePage';
149  } elseif ($obj->typecontainer == 'container') {
150  $transcodecontainer = 'PortionOfPage';
151  } elseif ($obj->typecontainer == 'service') {
152  $transcodecontainer = 'ServiceComponent';
153  }
154  $out .= $langs->trans($transcodecontainer);
155  $out .= ' ---</option>';
156  $lasttypecontainer = $obj->typecontainer;
157  }
158 
159  if ($selected == $obj->rowid || $selected == $obj->code) {
160  $out .= '<option value="'.$obj->code.'" selected>';
161  } else {
162  $out .= '<option value="'.$obj->code.'">';
163  }
164  $out .= $langs->trans($obj->label);
165  $out .= '</option>';
166 
167  $conf->cache['type_of_container'][$obj->code] = $obj->label;
168 
169  $i++;
170  }
171  $out .= "</select>";
172  if ($user->admin) {
173  $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
174  }
175 
176  if ($addjscombo) {
177  $out .= ajax_combobox('select'.$htmlname);
178  }
179  } else {
180  $out .= $langs->trans("NoTypeOfPagePleaseEditDictionary");
181  }
182  } else {
183  $this->error = $this->db->lasterror();
184  }
185 
186  return $out;
187  }
188 
200  public function selectSampleOfContainer($htmlname, $selected = '', $useempty = 0, $moreattrib = '', $addjscombo = 0, $morecss = 'minwidth200')
201  {
202  global $langs, $user;
203 
204  $langs->load("admin");
205 
206  $listofsamples = dol_dir_list(DOL_DOCUMENT_ROOT.'/website/samples', 'files', 0, '^page-sample-.*\.html$');
207  $arrayofsamples = array();
208  $arrayofsamples['empty'] = 'EmptyPage'; // Always this one first
209  foreach ($listofsamples as $sample) {
210  $reg = array();
211  if (preg_match('/^page-sample-(.*)\.html$/', $sample['name'], $reg)) {
212  $key = $reg[1];
213  $labelkey = ucfirst($key);
214  if ($key == 'empty') {
215  $labelkey = 'EmptyPage';
216  }
217  $arrayofsamples[$key] = $labelkey;
218  }
219  }
220 
221  $out = '';
222  $out .= '<select id="select'.$htmlname.'" class="selectSampleOfContainer'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
223 
224  if ($useempty == 1 || $useempty == 2) {
225  $out .= '<option value="-1">&nbsp;</option>';
226  }
227 
228  foreach ($arrayofsamples as $key => $val) {
229  if ($selected == $key) {
230  $out .= '<option value="'.$key.'" selected>';
231  } else {
232  $out .= '<option value="'.$key.'">';
233  }
234  $out .= $langs->trans($val);
235  $out .= '</option>';
236  }
237  $out .= "</select>";
238 
239  if ($addjscombo) {
240  $out .= ajax_combobox('select'.$htmlname);
241  }
242 
243  return $out;
244  }
245 
246 
260  public function selectContainer($website, $htmlname = 'pageid', $pageid = 0, $showempty = 0, $action = '', $morecss = 'minwidth200', $excludeids = null)
261  {
262  global $conf, $langs;
263 
264  $this->num = 0;
265 
266  $atleastonepage = (is_array($website->lines) && count($website->lines) > 0);
267 
268  $out = '';
269  if ($atleastonepage && $action != 'editsource') {
270  $out .= '<select name="'.$htmlname.'" id="'.$htmlname.'" class="maxwidth300'.($morecss ? ' '.$morecss : '').'">';
271  } else {
272  $out .= '<select name="pageidbis" id="pageid" class="maxwidth300'.($morecss ? ' '.$morecss : '').'"'.($action == 'editsource' ? ' disabled="disabled"' : '').'>';
273  }
274 
275  if ($showempty || !$atleastonepage) {
276  $out .= '<option class="optiongrey" value="-1">'.(is_numeric($showempty) ? '&nbsp;' : $showempty).'</option>';
277  }
278 
279  /*if (!empty($conf->use_javascript_ajax)) {
280  $valueoption = '<span class="classlink">'.img_picto('', 'add', 'class="paddingrightonly"').$langs->trans("AddPage").'</span>';
281  $out .= '<option value="-2" data-html="'.dol_escape_htmltag($valueoption).'">'.$valueoption.'</option>';
282  }*/
283 
284  if ($atleastonepage) {
285  if (empty($pageid) && $action != 'createcontainer') { // Page id is not defined, we try to take one
286  $firstpageid = 0;
287  $homepageid = 0;
288  foreach ($website->lines as $key => $valpage) {
289  if (empty($firstpageid)) {
290  $firstpageid = $valpage->id;
291  }
292  if ($website->fk_default_home && $key == $website->fk_default_home) {
293  $homepageid = $valpage->id;
294  }
295  }
296  $pageid = $homepageid ? $homepageid : $firstpageid; // We choose home page and if not defined yet, we take first page
297  }
298 
299  foreach ($website->lines as $key => $valpage) {
300  if (is_array($excludeids) && count($excludeids) && in_array($valpage->id, $excludeids)) {
301  continue;
302  }
303 
304  $valueforoption = '<span class="opacitymedium">['.$valpage->type_container.' '.sprintf("%03d", $valpage->id).']</span> ';
305  $valueforoption .= $valpage->pageurl.' - '.$valpage->title;
306  if ($website->otherlang) { // If there is alternative lang for this web site, we show the language code
307  if ($valpage->lang) {
308  $valueforoption .= ' <span class="opacitymedium">('.$valpage->lang.')</span>';
309  }
310  }
311  if ($website->fk_default_home && $key == $website->fk_default_home) {
312  //$valueforoption .= ' <span class="opacitymedium">('.$langs->trans("HomePage").')</span>';
313  $valueforoption .= ' <span class="opacitymedium fas fa-home"></span>';
314  }
315 
316  $out .= '<option value="'.$key.'"';
317  if ($pageid > 0 && $pageid == $key) {
318  $out .= ' selected'; // To preselect a value
319  }
320  $out .= ' data-html="'.dol_escape_htmltag($valueforoption).'"';
321  $out .= '>';
322  $out .= $valueforoption;
323  $out .= '</option>';
324 
325  ++$this->num;
326  }
327  }
328  $out .= '</select>';
329 
330  if ($atleastonepage && $action != 'editsource') {
331  $out .= ajax_combobox($htmlname);
332  } else {
333  $out .= '<input type="hidden" name="'.$htmlname.'" value="'.$pageid.'">';
334  $out .= ajax_combobox($htmlname);
335  }
336  return $out;
337  }
338 
339 
346  public function getContentPageTemplate($htmlContent = 'message')
347  {
348  global $user, $langs;
349 
350  $htmlContent = preg_replace('/[^a-z0-9_]/', '', $htmlContent);
351 
352  require_once DOL_DOCUMENT_ROOT.'/core/lib/emaillayout.lib.php';
353 
354  $listofsamples = dol_dir_list(DOL_DOCUMENT_ROOT.'/website/samples', 'files', 0, '^page-sample-.*\.html$');
355  $arrayofsamples = array();
356  $arrayofsamples['empty'] = 'EmptyPage'; // Always this one first
357  foreach ($listofsamples as $sample) {
358  $reg = array();
359  if (preg_match('/^page-sample-(.*)\.html$/', $sample['name'], $reg)) {
360  $key = $reg[1];
361  $labelkey = ucfirst($key);
362  if ($key == 'empty') {
363  $labelkey = 'EmptyPage';
364  }
365  $arrayofsamples[$key] = $labelkey;
366  }
367  }
368  $out = '<div id="template-selector" class="template-container hidden">';
369 
370  // We disable some not ready templates
371  unset($arrayofsamples['dynamiccontent']);
372  //unset($arrayofsamples['news']);
373 
374  $templates = $arrayofsamples;
375 
376  foreach ($templates as $template => $templateFunction) {
377  $substitutionarray = array();
378  $substitutionarray['__WEBSITE_CREATED_BY__'] = $user->getFullName($langs);
379  $substitutionarray['__WEBSITE_CONTENT__'] = $langs->trans("WebpageContent");
380  $substitutionarray['__WEBSITE_TITLE1__'] = $langs->trans("Title1");
381  $substitutionarray['__WEBSITE_TITLE2__'] = $langs->trans("Title2");
382 
383  $pathtoTemplateFile = DOL_DOCUMENT_ROOT.'/website/samples/page-sample-'.dol_sanitizeFileName($template).'.html';
384  $contentHtml = file_exists($pathtoTemplateFile) ? make_substitutions(@file_get_contents($pathtoTemplateFile), $substitutionarray) : '';
385 
386  $out .= '<div class="template-option" data-template="'.$template.'" data-content="'.htmlentities($contentHtml).'">';
387  $out .= '<img class="maillayout" alt="'.$template.'" src="'.DOL_URL_ROOT.'/theme/common/maillayout/'.$template.'.png" />';
388  $out .= '<span class="template-option-text">'.($template != 'text' ? ucfirst($template) : ucfirst($templateFunction)).'</span>';
389  $out .= '</div>';
390  }
391  $out .= '<input type="hidden" name="sample" value="" />';
392  $out .= '</div>';
393 
394  $out .= '<script type="text/javascript">
395  $(document).ready(function() {
396  $(".template-option").click(function() {
397  console.log("We choose a layout for website, we fill the field \''.$htmlContent.'\'");
398 
399  $(".template-option").removeClass("selected");
400  $(this).addClass("selected");
401 
402  var template = $(this).data("template");
403  var contentHtml = $(this).data("content");
404 
405  jQuery("#'.$htmlContent.'").val(contentHtml);
406  //jQuery("#'.$htmlContent.'preview").val(contentHtml);
407 
408  var editorInstance = CKEDITOR.instances.'.$htmlContent.';
409  if (editorInstance) {
410  editorInstance.setData(contentHtml);
411  }
412  //var editorInstance = CKEDITOR.instances.'.$htmlContent.'preview;
413  //if (editorInstance) {
414  // editorInstance.setData(contentHtml);
415  //}
416  });
417  });
418  </script>';
419 
420  return $out;
421  }
422 }
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:456
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.
$num
var int A number of lines
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:751
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...
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information in HTML for admin users or standard users.
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...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.