dolibarr 19.0.3
website.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2018 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
5 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6 * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Put here all includes required by your class file
29require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
31//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
32
33
37class Website extends CommonObject
38{
42 public $element = 'website';
43
47 public $table_element = 'website';
48
53 public $ismultientitymanaged = 1;
54
55
56 protected $childtablesoncascade = array();
57
58
62 public $picto = 'globe';
63
67 public $entity;
68
72 public $ref;
73
77 public $description;
78
82 public $lang;
83
87 public $otherlang;
88
92 public $status;
93
97 public $date_creation;
98
102 public $date_modification;
106 public $tms;
107
111 public $fk_default_home;
112
116 public $fk_user_creat;
117
121 public $fk_user_modif;
122
126 public $virtualhost;
127
131 public $use_manifest;
132
136 public $position;
137
141 public $lines;
142
143
144 const STATUS_DRAFT = 0;
145 const STATUS_VALIDATED = 1;
146
147
153 public function __construct(DoliDB $db)
154 {
155 $this->db = $db;
156 }
157
166 public function create(User $user, $notrigger = false)
167 {
168 global $conf, $langs;
169
170 dol_syslog(__METHOD__, LOG_DEBUG);
171
172 $error = 0;
173 $now = dol_now();
174
175 // Clean parameters
176 if (isset($this->entity)) {
177 $this->entity = (int) $this->entity;
178 }
179 if (isset($this->ref)) {
180 $this->ref = trim($this->ref);
181 }
182 if (isset($this->description)) {
183 $this->description = trim($this->description);
184 }
185 if (isset($this->status)) {
186 $this->status = (int) $this->status;
187 }
188 if (empty($this->date_creation)) {
189 $this->date_creation = $now;
190 }
191 if (empty($this->date_modification)) {
192 $this->date_modification = $now;
193 }
194 // Remove spaces and be sure we have main language only
195 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
196 $tmparray = explode(',', $this->otherlang);
197 if (is_array($tmparray)) {
198 foreach ($tmparray as $key => $val) {
199 // It possible we have empty val here if postparam WEBSITE_OTHERLANG is empty or set like this : 'en,,sv' or 'en,sv,'
200 if (empty(trim($val))) {
201 unset($tmparray[$key]);
202 continue;
203 }
204 $tmparray[$key] = preg_replace('/[_-].*$/', '', trim($val)); // en_US or en-US -> en
205 }
206 $this->otherlang = join(',', $tmparray);
207 }
208
209 // Check parameters
210 if (empty($this->entity)) {
211 $this->entity = $conf->entity;
212 }
213 if (empty($this->lang)) {
214 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MainLanguage"));
215 return -1;
216 }
217
218 // Insert request
219 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
220 $sql .= 'entity,';
221 $sql .= 'ref,';
222 $sql .= 'description,';
223 $sql .= 'lang,';
224 $sql .= 'otherlang,';
225 $sql .= 'status,';
226 $sql .= 'fk_default_home,';
227 $sql .= 'virtualhost,';
228 $sql .= 'fk_user_creat,';
229 $sql .= 'date_creation,';
230 $sql .= 'position,';
231 $sql .= 'tms';
232 $sql .= ') VALUES (';
233 $sql .= ' '.((empty($this->entity) && $this->entity != '0') ? 'NULL' : $this->entity).',';
234 $sql .= ' '.(!isset($this->ref) ? 'NULL' : "'".$this->db->escape($this->ref)."'").',';
235 $sql .= ' '.(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").',';
236 $sql .= ' '.(!isset($this->lang) ? 'NULL' : "'".$this->db->escape($this->lang)."'").',';
237 $sql .= ' '.(!isset($this->otherlang) ? 'NULL' : "'".$this->db->escape($this->otherlang)."'").',';
238 $sql .= ' '.(!isset($this->status) ? '1' : $this->status).',';
239 $sql .= ' '.(!isset($this->fk_default_home) ? 'NULL' : $this->fk_default_home).',';
240 $sql .= ' '.(!isset($this->virtualhost) ? 'NULL' : "'".$this->db->escape($this->virtualhost)."'").",";
241 $sql .= ' '.(!isset($this->fk_user_creat) ? $user->id : $this->fk_user_creat).',';
242 $sql .= ' '.(!isset($this->date_creation) || dol_strlen($this->date_creation) == 0 ? 'NULL' : "'".$this->db->idate($this->date_creation)."'").",";
243 $sql .= ' '.((int) $this->position).",";
244 $sql .= ' '.(!isset($this->date_modification) || dol_strlen($this->date_modification) == 0 ? 'NULL' : "'".$this->db->idate($this->date_modification)."'");
245 $sql .= ')';
246
247 $this->db->begin();
248
249 $resql = $this->db->query($sql);
250 if (!$resql) {
251 $error++;
252 $this->errors[] = 'Error '.$this->db->lasterror();
253 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
254 }
255
256 if (!$error) {
257 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
258
259 // Create a subdirectory for each language (except main language)
260 $tmplangarray = explode(',', $this->otherlang);
261 if (is_array($tmplangarray)) {
262 dol_mkdir($conf->website->dir_output.'/'.$this->ref);
263 foreach ($tmplangarray as $val) {
264 if (trim($val) == $this->lang) {
265 continue;
266 }
267 dol_mkdir($conf->website->dir_output.'/'.$this->ref.'/'.trim($val), DOL_DATA_ROOT);
268 }
269 }
270
271 // Create subdirectory for images and js
272 dol_mkdir($conf->medias->multidir_output[$conf->entity].'/image/'.$this->ref, DOL_DATA_ROOT);
273 dol_mkdir($conf->medias->multidir_output[$conf->entity].'/js/'.$this->ref, DOL_DATA_ROOT);
274
275 // Uncomment this and change WEBSITE to your own tag if you
276 // want this action to call a trigger.
277 // if (!$notrigger) {
278
279 // // Call triggers
280 // $result = $this->call_trigger('WEBSITE_CREATE',$user);
281 // if ($result < 0) $error++;
282 // // End call triggers
283 // }
284 }
285
286 if (!$error) {
287 $stringtodolibarrfile = "# Some properties for Dolibarr web site CMS\n";
288 $stringtodolibarrfile .= "param=value\n";
289 //print $conf->website->dir_output.'/'.$this->ref.'/.dolibarr';exit;
290 file_put_contents($conf->website->dir_output.'/'.$this->ref.'/.dolibarr', $stringtodolibarrfile);
291 }
292
293 // Commit or rollback
294 if ($error) {
295 $this->db->rollback();
296 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
297 return 0;
298 } else {
299 return -1 * $error;
300 }
301 } else {
302 $this->db->commit();
303
304 return $this->id;
305 }
306 }
307
315 public function fetch($id, $ref = null)
316 {
317 dol_syslog(__METHOD__, LOG_DEBUG);
318
319 $sql = "SELECT";
320 $sql .= " t.rowid,";
321 $sql .= " t.entity,";
322 $sql .= " t.ref,";
323 $sql .= " t.position,";
324 $sql .= " t.description,";
325 $sql .= " t.lang,";
326 $sql .= " t.otherlang,";
327 $sql .= " t.status,";
328 $sql .= " t.fk_default_home,";
329 $sql .= " t.use_manifest,";
330 $sql .= " t.virtualhost,";
331 $sql .= " t.fk_user_creat,";
332 $sql .= " t.fk_user_modif,";
333 $sql .= " t.date_creation,";
334 $sql .= " t.tms as date_modification";
335 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
336 $sql .= " WHERE t.entity IN (".getEntity('website').")";
337 if (!empty($ref)) {
338 $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
339 } else {
340 $sql .= " AND t.rowid = ".(int) $id;
341 }
342
343 $resql = $this->db->query($sql);
344 if ($resql) {
345 $numrows = $this->db->num_rows($resql);
346 if ($numrows) {
347 $obj = $this->db->fetch_object($resql);
348
349 $this->id = $obj->rowid;
350
351 $this->entity = $obj->entity;
352 $this->ref = $obj->ref;
353 $this->position = $obj->position;
354 $this->description = $obj->description;
355 $this->lang = $obj->lang;
356 $this->otherlang = $obj->otherlang;
357 $this->status = $obj->status;
358 $this->fk_default_home = $obj->fk_default_home;
359 $this->virtualhost = $obj->virtualhost;
360 $this->use_manifest = $obj->use_manifest;
361 $this->fk_user_creat = $obj->fk_user_creat;
362 $this->fk_user_modif = $obj->fk_user_modif;
363 $this->date_creation = $this->db->jdate($obj->date_creation);
364 $this->date_modification = $this->db->jdate($obj->date_modification);
365 }
366 $this->db->free($resql);
367
368 if ($numrows > 0) {
369 // Lines
370 $this->fetchLines();
371 }
372
373 if ($numrows > 0) {
374 return 1;
375 } else {
376 return 0;
377 }
378 } else {
379 $this->errors[] = 'Error '.$this->db->lasterror();
380 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
381
382 return -1;
383 }
384 }
385
391 public function fetchLines()
392 {
393 $this->lines = array();
394
395 // Load lines with object MyObjectLine
396
397 return count($this->lines) ? 1 : 0;
398 }
399
400
412 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
413 {
414 dol_syslog(__METHOD__, LOG_DEBUG);
415
416 $records = array();
417
418 $sql = "SELECT";
419 $sql .= " t.rowid,";
420 $sql .= " t.entity,";
421 $sql .= " t.ref,";
422 $sql .= " t.description,";
423 $sql .= " t.lang,";
424 $sql .= " t.otherlang,";
425 $sql .= " t.status,";
426 $sql .= " t.fk_default_home,";
427 $sql .= " t.virtualhost,";
428 $sql .= " t.fk_user_creat,";
429 $sql .= " t.fk_user_modif,";
430 $sql .= " t.date_creation,";
431 $sql .= " t.tms as date_modification";
432 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
433 $sql .= " WHERE t.entity IN (".getEntity('website').")";
434 // Manage filter
435 $sqlwhere = array();
436 if (count($filter) > 0) {
437 foreach ($filter as $key => $value) {
438 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
439 }
440 }
441 if (count($sqlwhere) > 0) {
442 $sql .= ' AND '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
443 }
444
445 if (!empty($sortfield)) {
446 $sql .= $this->db->order($sortfield, $sortorder);
447 }
448 if (!empty($limit)) {
449 $sql .= $this->db->plimit($limit, $offset);
450 }
451
452 $resql = $this->db->query($sql);
453 if ($resql) {
454 $num = $this->db->num_rows($resql);
455
456 while ($obj = $this->db->fetch_object($resql)) {
457 $record = new self($this->db);
458
459 $record->id = $obj->rowid;
460
461 $record->entity = $obj->entity;
462 $record->ref = $obj->ref;
463 $record->description = $obj->description;
464 $record->lang = $obj->lang;
465 $record->otherlang = $obj->otherlang;
466 $record->status = $obj->status;
467 $record->fk_default_home = $obj->fk_default_home;
468 $record->virtualhost = $obj->virtualhost;
469 $record->fk_user_creat = $obj->fk_user_creat;
470 $record->fk_user_modif = $obj->fk_user_modif;
471 $record->date_creation = $this->db->jdate($obj->date_creation);
472 $record->date_modification = $this->db->jdate($obj->date_modification);
473
474 $records[$record->id] = $record;
475 }
476 $this->db->free($resql);
477
478 return $records;
479 } else {
480 $this->errors[] = 'Error '.$this->db->lasterror();
481 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
482
483 return -1;
484 }
485 }
486
495 public function update(User $user, $notrigger = false)
496 {
497 global $conf, $langs;
498
499 $error = 0;
500
501 dol_syslog(__METHOD__, LOG_DEBUG);
502
503 // Clean parameters
504
505 if (isset($this->entity)) {
506 $this->entity = (int) $this->entity;
507 }
508 if (isset($this->ref)) {
509 $this->ref = trim($this->ref);
510 }
511 if (isset($this->description)) {
512 $this->description = trim($this->description);
513 }
514 if (isset($this->status)) {
515 $this->status = (int) $this->status;
516 }
517
518 // Remove spaces and be sure we have main language only
519 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
520 $tmparray = explode(',', $this->otherlang);
521 if (is_array($tmparray)) {
522 foreach ($tmparray as $key => $val) {
523 // It possible we have empty val here if postparam WEBSITE_OTHERLANG is empty or set like this : 'en,,sv' or 'en,sv,'
524 if (empty(trim($val))) {
525 unset($tmparray[$key]);
526 continue;
527 }
528 $tmparray[$key] = preg_replace('/[_-].*$/', '', trim($val)); // en_US or en-US -> en
529 }
530 $this->otherlang = join(',', $tmparray);
531 }
532 if (empty($this->lang)) {
533 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MainLanguage"));
534 return -1;
535 }
536
537 // Check parameters
538 // Put here code to add a control on parameters values
539
540 // Update request
541 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
542 $sql .= ' entity = '.(isset($this->entity) ? $this->entity : "null").',';
543 $sql .= ' ref = '.(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").',';
544 $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").',';
545 $sql .= ' lang = '.(isset($this->lang) ? "'".$this->db->escape($this->lang)."'" : "null").',';
546 $sql .= ' otherlang = '.(isset($this->otherlang) ? "'".$this->db->escape($this->otherlang)."'" : "null").',';
547 $sql .= ' status = '.(isset($this->status) ? $this->status : "null").',';
548 $sql .= ' fk_default_home = '.(($this->fk_default_home > 0) ? $this->fk_default_home : "null").',';
549 $sql .= ' use_manifest = '.((int) $this->use_manifest).',';
550 $sql .= ' virtualhost = '.(($this->virtualhost != '') ? "'".$this->db->escape($this->virtualhost)."'" : "null").',';
551 $sql .= ' fk_user_modif = '.(!isset($this->fk_user_modif) ? $user->id : $this->fk_user_modif).',';
552 $sql .= ' date_creation = '.(!isset($this->date_creation) || dol_strlen($this->date_creation) != 0 ? "'".$this->db->idate($this->date_creation)."'" : 'null').',';
553 $sql .= ' tms = '.(dol_strlen($this->date_modification) != 0 ? "'".$this->db->idate($this->date_modification)."'" : "'".$this->db->idate(dol_now())."'");
554 $sql .= ' WHERE rowid='.((int) $this->id);
555
556 $this->db->begin();
557
558 $resql = $this->db->query($sql);
559 if (!$resql) {
560 $error++;
561 $this->errors[] = 'Error '.$this->db->lasterror();
562 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
563 }
564
565 if (!$error && !$notrigger) {
566 // Uncomment this and change MYOBJECT to your own tag if you
567 // want this action calls a trigger.
568
569 // Create subdirectory per language
570 $tmplangarray = explode(',', $this->otherlang);
571 if (is_array($tmplangarray)) {
572 dol_mkdir($conf->website->dir_output.'/'.$this->ref);
573 foreach ($tmplangarray as $val) {
574 if (trim($val) == $this->lang) {
575 continue;
576 }
577 dol_mkdir($conf->website->dir_output.'/'.$this->ref.'/'.trim($val));
578 }
579 }
580
582 //$result=$this->call_trigger('WEBSITE_MODIFY',$user);
583 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
585 }
586
587 // Commit or rollback
588 if ($error) {
589 $this->db->rollback();
590
591 return -1 * $error;
592 } else {
593 $this->db->commit();
594
595 return 1;
596 }
597 }
598
607 public function delete(User $user, $notrigger = false)
608 {
609 global $conf;
610
611 dol_syslog(__METHOD__, LOG_DEBUG);
612
613 $error = 0;
614
615 $this->db->begin();
616
617 if (!$error) {
618 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'website_page';
619 $sql .= ' WHERE fk_website = '.((int) $this->id);
620
621 $resql = $this->db->query($sql);
622 if (!$resql) {
623 $error++;
624 $this->errors[] = 'Error '.$this->db->lasterror();
625 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
626 }
627 }
628
629 // Delete common code. This include execution of trigger.
630 $result = $this->deleteCommon($user, $notrigger);
631 if ($result <= 0) {
632 $error++;
633 }
634
635 if (!$error && !empty($this->ref)) {
636 $pathofwebsite = DOL_DATA_ROOT.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.$this->ref;
637
638 dol_delete_dir_recursive($pathofwebsite);
639 }
640
641 // Commit or rollback
642 if ($error) {
643 $this->db->rollback();
644
645 return -1 * $error;
646 } else {
647 $this->db->commit();
648
649 return 1;
650 }
651 }
652
663 public function createFromClone($user, $fromid, $newref, $newlang = '')
664 {
665 global $conf, $langs;
666 global $dolibarr_main_data_root;
667
668 $now = dol_now();
669 $error = 0;
670
671 dol_syslog(__METHOD__, LOG_DEBUG);
672
673 $newref = dol_sanitizeFileName($newref);
674
675 if (empty($newref)) {
676 $this->error = 'ErrorBadParameter';
677 return -1;
678 }
679
680 $object = new self($this->db);
681
682 // Check no site with ref exists
683 if ($object->fetch(0, $newref) > 0) {
684 $this->error = 'ErrorNewRefIsAlreadyUsed';
685 return -1;
686 }
687
688 $this->db->begin();
689
690 // Load source object
691 $object->fetch($fromid);
692
693 $oldidforhome = $object->fk_default_home;
694 $oldref = $object->ref;
695
696 $pathofwebsiteold = $dolibarr_main_data_root.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.dol_sanitizeFileName($oldref);
697 $pathofwebsitenew = $dolibarr_main_data_root.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.dol_sanitizeFileName($newref);
698 dol_delete_dir_recursive($pathofwebsitenew);
699
700 $fileindex = $pathofwebsitenew.'/index.php';
701
702 // Reset some properties
703 unset($object->id);
704 unset($object->fk_user_creat);
705 unset($object->import_key);
706
707 // Clear fields
708 $object->ref = $newref;
709 $object->fk_default_home = 0;
710 $object->virtualhost = '';
711 $object->date_creation = $now;
712 $object->fk_user_creat = $user->id;
713 $object->position = ((int) $object->position) + 1;
714 $object->status = self::STATUS_DRAFT;
715 if (empty($object->lang)) {
716 $object->lang = substr($langs->defaultlang, 0, 2); // Should not happen. Protection for corrupted site with no languages
717 }
718
719 // Create clone
720 $object->context['createfromclone'] = 'createfromclone';
721 $result = $object->create($user);
722 if ($result < 0) {
723 $error++;
724 $this->error = $object->error;
725 $this->errors = $object->errors;
726 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
727 }
728
729 if (!$error) {
730 dolCopyDir($pathofwebsiteold, $pathofwebsitenew, $conf->global->MAIN_UMASK, 0, null, 2);
731
732 // Check symlink to medias and restore it if ko
733 $pathtomedias = DOL_DATA_ROOT.'/medias'; // Target
734 $pathtomediasinwebsite = $pathofwebsitenew.'/medias'; // Source / Link name
735 if (!is_link(dol_osencode($pathtomediasinwebsite))) {
736 dol_syslog("Create symlink for ".$pathtomedias." into name ".$pathtomediasinwebsite);
737 dol_mkdir(dirname($pathtomediasinwebsite)); // To be sure dir for website exists
738 $result = symlink($pathtomedias, $pathtomediasinwebsite);
739 }
740
741 // Copy images and js dir
742 $pathofmediasjsold = DOL_DATA_ROOT.'/medias/js/'.$oldref;
743 $pathofmediasjsnew = DOL_DATA_ROOT.'/medias/js/'.$newref;
744 dolCopyDir($pathofmediasjsold, $pathofmediasjsnew, $conf->global->MAIN_UMASK, 0);
745
746 $pathofmediasimageold = DOL_DATA_ROOT.'/medias/image/'.$oldref;
747 $pathofmediasimagenew = DOL_DATA_ROOT.'/medias/image/'.$newref;
748 dolCopyDir($pathofmediasimageold, $pathofmediasimagenew, $conf->global->MAIN_UMASK, 0);
749
750 $newidforhome = 0;
751
752 // Duplicate pages
753 $objectpages = new WebsitePage($this->db);
754 $listofpages = $objectpages->fetchAll($fromid);
755 foreach ($listofpages as $pageid => $objectpageold) {
756 // Delete old file
757 $filetplold = $pathofwebsitenew.'/page'.$pageid.'.tpl.php';
758 dol_delete_file($filetplold);
759
760 // Create new file
761 $objectpagenew = $objectpageold->createFromClone($user, $pageid, $objectpageold->pageurl, '', 0, $object->id, 1);
762
763 //print $pageid.' = '.$objectpageold->pageurl.' -> '.$objectpagenew->id.' = '.$objectpagenew->pageurl.'<br>';
764 if (is_object($objectpagenew) && $objectpagenew->pageurl) {
765 $filealias = $pathofwebsitenew.'/'.$objectpagenew->pageurl.'.php';
766 $filetplnew = $pathofwebsitenew.'/page'.$objectpagenew->id.'.tpl.php';
767
768 // Save page alias
769 $result = dolSavePageAlias($filealias, $object, $objectpagenew);
770 if (!$result) {
771 setEventMessages('Failed to write file '.$filealias, null, 'errors');
772 }
773
774 $result = dolSavePageContent($filetplnew, $object, $objectpagenew);
775 if (!$result) {
776 setEventMessages('Failed to write file '.$filetplnew, null, 'errors');
777 }
778
779 if ($pageid == $oldidforhome) {
780 $newidforhome = $objectpagenew->id;
781 }
782 } else {
783 setEventMessages($objectpageold->error, $objectpageold->errors, 'errors');
784 $error++;
785 }
786 }
787 }
788
789 if (!$error) {
790 // Restore id of home page
791 $object->fk_default_home = $newidforhome;
792 $res = $object->update($user);
793 if (!($res > 0)) {
794 $error++;
795 setEventMessages($object->error, $object->errors, 'errors');
796 }
797
798 if (!$error) {
799 $filetpl = $pathofwebsitenew.'/page'.$newidforhome.'.tpl.php';
800 $filewrapper = $pathofwebsitenew.'/wrapper.php';
801
802 // Re-generates the index.php page to be the home page, and re-generates the wrapper.php
803 //--------------------------------------------------------------------------------------
804 $result = dolSaveIndexPage($pathofwebsitenew, $fileindex, $filetpl, $filewrapper, $object);
805 }
806 }
807
808 unset($object->context['createfromclone']);
809
810 // End
811 if (!$error) {
812 $this->db->commit();
813
814 return $object;
815 } else {
816 $this->db->rollback();
817
818 return -1;
819 }
820 }
821
833 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
834 {
835 global $langs, $conf, $db;
836 global $dolibarr_main_authentication, $dolibarr_main_demo;
837 global $menumanager;
838
839
840 $result = '';
841 $companylink = '';
842
843 $label = '<u>'.$langs->trans("WebSite").'</u>';
844 $label .= '<br>';
845 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
846 $label .= '<b>'.$langs->trans('MainLanguage').':</b> '.$this->lang;
847
848 $linkstart = '<a href="'.DOL_URL_ROOT.'/website/card.php?id='.$this->id.'"';
849 $linkstart .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
850 $linkstart .= '>';
851 $linkend = '</a>';
852
853 $linkstart = $linkend = '';
854
855 if ($withpicto) {
856 $result .= ($linkstart.img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
857 if ($withpicto != 2) {
858 $result .= ' ';
859 }
860 }
861 $result .= $linkstart.$this->ref.$linkend;
862 return $result;
863 }
864
871 public function getLibStatut($mode = 0)
872 {
873 return $this->LibStatut($this->status, $mode);
874 }
875
876 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
884 public function LibStatut($status, $mode = 0)
885 {
886 // phpcs:enable
887 global $langs;
888
889 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
890 global $langs;
891 //$langs->load("mymodule");
892 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
893 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
894 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
895 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
896 }
897
898 $statusType = 'status5';
899 if ($status == self::STATUS_VALIDATED) {
900 $statusType = 'status4';
901 }
902
903 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
904 }
905
906
913 public function initAsSpecimen()
914 {
915 global $user;
916
917 $this->id = 0;
918 $this->specimen = 1;
919 $this->entity = 1;
920 $this->ref = 'myspecimenwebsite';
921 $this->description = 'A specimen website';
922 $this->lang = 'en';
923 $this->otherlang = 'fr,es';
924 $this->status = 1;
925 $this->fk_default_home = null;
926 $this->virtualhost = 'http://myvirtualhost';
927 $this->fk_user_creat = $user->id;
928 $this->fk_user_modif = $user->id;
929 $this->date_creation = dol_now();
930 $this->tms = dol_now();
931 }
932
933
939 public function exportWebSite()
940 {
941 global $conf, $mysoc;
942
943 $website = $this;
944
945 if (empty($website->id) || empty($website->ref)) {
946 setEventMessages("Website id or ref is not defined", null, 'errors');
947 return '';
948 }
949
950 dol_syslog("Create temp dir ".$conf->website->dir_temp);
951 dol_mkdir($conf->website->dir_temp);
952 if (!is_writable($conf->website->dir_temp)) {
953 setEventMessages("Temporary dir ".$conf->website->dir_temp." is not writable", null, 'errors');
954 return '';
955 }
956
957 $destdir = $conf->website->dir_temp.'/'.$website->ref;
958
959 dol_syslog("Clear temp dir ".$destdir);
960 $count = 0;
961 $countreallydeleted = 0;
962 $counttodelete = dol_delete_dir_recursive($destdir, $count, 1, 0, $countreallydeleted);
963 if ($counttodelete != $countreallydeleted) {
964 setEventMessages("Failed to clean temp directory ".$destdir, null, 'errors');
965 return '';
966 }
967
968 $arrayreplacementinfilename = array();
969 $arrayreplacementincss = array();
970 $arrayreplacementincss['file=image/'.$website->ref.'/'] = "file=image/__WEBSITE_KEY__/";
971 $arrayreplacementincss['file=js/'.$website->ref.'/'] = "file=js/__WEBSITE_KEY__/";
972 $arrayreplacementincss['medias/image/'.$website->ref.'/'] = "medias/image/__WEBSITE_KEY__/";
973 $arrayreplacementincss['medias/js/'.$website->ref.'/'] = "medias/js/__WEBSITE_KEY__/";
974 if ($mysoc->logo_small) {
975 $arrayreplacementincss['file=logos%2Fthumbs%2F'.$mysoc->logo_small] = "file=logos%2Fthumbs%2F__LOGO_SMALL_KEY__";
976 }
977 if ($mysoc->logo_mini) {
978 $arrayreplacementincss['file=logos%2Fthumbs%2F'.$mysoc->logo_mini] = "file=logos%2Fthumbs%2F__LOGO_MINI_KEY__";
979 }
980 if ($mysoc->logo) {
981 $arrayreplacementincss['file=logos%2Fthumbs%2F'.$mysoc->logo] = "file=logos%2Fthumbs%2F__LOGO_KEY__";
982 }
983
984 // Create output directories
985 dol_syslog("Create containers dir");
986 dol_mkdir($conf->website->dir_temp.'/'.$website->ref.'/containers');
987 dol_mkdir($conf->website->dir_temp.'/'.$website->ref.'/medias/image/websitekey');
988 dol_mkdir($conf->website->dir_temp.'/'.$website->ref.'/medias/js/websitekey');
989
990 // Copy files into 'containers'
991 $srcdir = $conf->website->dir_output.'/'.$website->ref;
992 $destdir = $conf->website->dir_temp.'/'.$website->ref.'/containers';
993
994 dol_syslog("Copy pages from ".$srcdir." into ".$destdir);
995 dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacementinfilename, 2, array('old', 'back'));
996
997 // Copy file README.md and LICENSE from directory containers into directory root
998 if (dol_is_file($conf->website->dir_temp.'/'.$website->ref.'/containers/README.md')) {
999 dol_copy($conf->website->dir_temp.'/'.$website->ref.'/containers/README.md', $conf->website->dir_temp.'/'.$website->ref.'/README.md');
1000 }
1001 if (dol_is_file($conf->website->dir_temp.'/'.$website->ref.'/containers/LICENSE')) {
1002 dol_copy($conf->website->dir_temp.'/'.$website->ref.'/containers/LICENSE', $conf->website->dir_temp.'/'.$website->ref.'/LICENSE');
1003 }
1004
1005 // Copy files into medias/image
1006 $srcdir = DOL_DATA_ROOT.'/medias/image/'.$website->ref;
1007 $destdir = $conf->website->dir_temp.'/'.$website->ref.'/medias/image/websitekey';
1008
1009 dol_syslog("Copy content from ".$srcdir." into ".$destdir);
1010 dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacementinfilename);
1011
1012 // Copy files into medias/js
1013 $srcdir = DOL_DATA_ROOT.'/medias/js/'.$website->ref;
1014 $destdir = $conf->website->dir_temp.'/'.$website->ref.'/medias/js/websitekey';
1015
1016 dol_syslog("Copy content from ".$srcdir." into ".$destdir);
1017 dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacementinfilename);
1018
1019 // Make some replacement into some files
1020 $cssindestdir = $conf->website->dir_temp.'/'.$website->ref.'/containers/styles.css.php';
1021 if (dol_is_file($cssindestdir)) {
1022 dolReplaceInFile($cssindestdir, $arrayreplacementincss);
1023 }
1024
1025 $htmldeaderindestdir = $conf->website->dir_temp.'/'.$website->ref.'/containers/htmlheader.html';
1026 if (dol_is_file($htmldeaderindestdir)) {
1027 dolReplaceInFile($htmldeaderindestdir, $arrayreplacementincss);
1028 }
1029
1030 // Build sql file
1031 $filesql = $conf->website->dir_temp.'/'.$website->ref.'/website_pages.sql';
1032 $fp = fopen($filesql, "w");
1033 if (empty($fp)) {
1034 setEventMessages("Failed to create file ".$filesql, null, 'errors');
1035 return '';
1036 }
1037
1038 $objectpages = new WebsitePage($this->db);
1039 $listofpages = $objectpages->fetchAll($website->id);
1040
1041 // Assign ->newid and ->newfk_page
1042 $i = 1;
1043 foreach ($listofpages as $pageid => $objectpageold) {
1044 $objectpageold->newid = $i;
1045 $i++;
1046 }
1047 $i = 1;
1048 foreach ($listofpages as $pageid => $objectpageold) {
1049 // Search newid
1050 $newfk_page = 0;
1051 foreach ($listofpages as $pageid2 => $objectpageold2) {
1052 if ($pageid2 == $objectpageold->fk_page) {
1053 $newfk_page = $objectpageold2->newid;
1054 break;
1055 }
1056 }
1057 $objectpageold->newfk_page = $newfk_page;
1058 $i++;
1059 }
1060 foreach ($listofpages as $pageid => $objectpageold) {
1061 $allaliases = $objectpageold->pageurl;
1062 $allaliases .= ($objectpageold->aliasalt ? ','.$objectpageold->aliasalt : '');
1063
1064 $line = '-- File generated by Dolibarr '.DOL_VERSION.' -- '.dol_print_date(dol_now('gmt'), 'standard', 'gmt').' UTC --;';
1065 $line .= "\n";
1066
1067 $line .= '-- Page ID '.$objectpageold->id.' -> '.$objectpageold->newid.'__+MAX_llx_website_page__ - Aliases '.$allaliases.' --;'; // newid start at 1, 2...
1068 $line .= "\n";
1069 fputs($fp, $line);
1070
1071 // Warning: We must keep llx_ here. It is a generic SQL.
1072 $line = 'INSERT INTO llx_website_page(rowid, fk_page, fk_website, pageurl, aliasalt, title, description, lang, image, keywords, status, date_creation, tms, import_key, grabbed_from, type_container, htmlheader, content, author_alias, allowed_in_frames)';
1073
1074 $line .= " VALUES(";
1075 $line .= $objectpageold->newid."__+MAX_llx_website_page__, ";
1076 $line .= ($objectpageold->newfk_page ? $this->db->escape($objectpageold->newfk_page)."__+MAX_llx_website_page__" : "null").", ";
1077 $line .= "__WEBSITE_ID__, ";
1078 $line .= "'".$this->db->escape($objectpageold->pageurl)."', ";
1079 $line .= "'".$this->db->escape($objectpageold->aliasalt)."', ";
1080 $line .= "'".$this->db->escape($objectpageold->title)."', ";
1081 $line .= "'".$this->db->escape($objectpageold->description)."', ";
1082 $line .= "'".$this->db->escape($objectpageold->lang)."', ";
1083 $line .= "'".$this->db->escape($objectpageold->image)."', ";
1084 $line .= "'".$this->db->escape($objectpageold->keywords)."', ";
1085 $line .= "'".$this->db->escape($objectpageold->status)."', ";
1086 $line .= "'".$this->db->idate($objectpageold->date_creation)."', ";
1087 $line .= "'".$this->db->idate($objectpageold->date_modification)."', ";
1088 $line .= ($objectpageold->import_key ? "'".$this->db->escape($objectpageold->import_key)."'" : "null").", ";
1089 $line .= "'".$this->db->escape($objectpageold->grabbed_from)."', ";
1090 $line .= "'".$this->db->escape($objectpageold->type_container)."', ";
1091
1092 // Make substitution with a generic path into htmlheader content
1093 $stringtoexport = $objectpageold->htmlheader;
1094 $stringtoexport = str_replace(array("\r\n", "\r", "\n"), "__N__", $stringtoexport);
1095 $stringtoexport = str_replace('file=image/'.$website->ref.'/', "file=image/__WEBSITE_KEY__/", $stringtoexport);
1096 $stringtoexport = str_replace('file=js/'.$website->ref.'/', "file=js/__WEBSITE_KEY__/", $stringtoexport);
1097 $stringtoexport = str_replace('medias/image/'.$website->ref.'/', "medias/image/__WEBSITE_KEY__/", $stringtoexport);
1098 $stringtoexport = str_replace('medias/js/'.$website->ref.'/', "medias/js/__WEBSITE_KEY__/", $stringtoexport);
1099
1100 $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo_small, "file=logos%2Fthumbs%2F__LOGO_SMALL_KEY__", $stringtoexport);
1101 $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo_mini, "file=logos%2Fthumbs%2F__LOGO_MINI_KEY__", $stringtoexport);
1102 $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo, "file=logos%2Fthumbs%2F__LOGO_KEY__", $stringtoexport);
1103 $line .= "'".$this->db->escape(str_replace(array("\r\n", "\r", "\n"), "__N__", $stringtoexport))."', "; // Replace \r \n to have record on 1 line
1104
1105 // Make substitution with a generic path into page content
1106 $stringtoexport = $objectpageold->content;
1107 $stringtoexport = str_replace(array("\r\n", "\r", "\n"), "__N__", $stringtoexport);
1108 $stringtoexport = str_replace('file=image/'.$website->ref.'/', "file=image/__WEBSITE_KEY__/", $stringtoexport);
1109 $stringtoexport = str_replace('file=js/'.$website->ref.'/', "file=js/__WEBSITE_KEY__/", $stringtoexport);
1110 $stringtoexport = str_replace('medias/image/'.$website->ref.'/', "medias/image/__WEBSITE_KEY__/", $stringtoexport);
1111 $stringtoexport = str_replace('medias/js/'.$website->ref.'/', "medias/js/__WEBSITE_KEY__/", $stringtoexport);
1112 $stringtoexport = str_replace('"image/'.$website->ref.'/', '"image/__WEBSITE_KEY__/', $stringtoexport); // When we have a link src="image/websiteref/file.png" into html content
1113 $stringtoexport = str_replace('"/image/'.$website->ref.'/', '"/image/__WEBSITE_KEY__/', $stringtoexport); // When we have a link src="/image/websiteref/file.png" into html content
1114 $stringtoexport = str_replace('"js/'.$website->ref.'/', '"js/__WEBSITE_KEY__/', $stringtoexport);
1115 $stringtoexport = str_replace('"/js/'.$website->ref.'/', '"/js/__WEBSITE_KEY__/', $stringtoexport);
1116
1117 $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo_small, "file=logos%2Fthumbs%2F__LOGO_SMALL_KEY__", $stringtoexport);
1118 $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo_mini, "file=logos%2Fthumbs%2F__LOGO_MINI_KEY__", $stringtoexport);
1119 $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo, "file=logos%2Fthumbs%2F__LOGO_KEY__", $stringtoexport);
1120
1121
1122 $line .= "'".$this->db->escape($stringtoexport)."', "; // Replace \r \n to have record on 1 line
1123 $line .= "'".$this->db->escape($objectpageold->author_alias)."', ";
1124 $line .= (int) $objectpageold->allowed_in_frames;
1125 $line .= ");";
1126 $line .= "\n";
1127
1128 fputs($fp, $line);
1129
1130 // Add line to update home page id during import
1131 //var_dump($this->fk_default_home.' - '.$objectpageold->id.' - '.$objectpageold->newid);exit;
1132 if ($this->fk_default_home > 0 && ($objectpageold->id == $this->fk_default_home) && ($objectpageold->newid > 0)) { // This is the record with home page
1133 // Warning: We must keep llx_ here. It is a generic SQL.
1134 $line = "UPDATE llx_website SET fk_default_home = ".($objectpageold->newid > 0 ? $this->db->escape($objectpageold->newid)."__+MAX_llx_website_page__" : "null")." WHERE rowid = __WEBSITE_ID__;";
1135 $line .= "\n";
1136 fputs($fp, $line);
1137 }
1138 }
1139
1140 $line = "\n-- For Dolibarr v14+ --;\n";
1141 $line .= "UPDATE llx_website SET lang = '".$this->db->escape($this->lang)."' WHERE rowid = __WEBSITE_ID__;\n";
1142 $line .= "UPDATE llx_website SET otherlang = '".$this->db->escape($this->otherlang)."' WHERE rowid = __WEBSITE_ID__;\n";
1143 $line .= "\n";
1144 fputs($fp, $line);
1145
1146 fclose($fp);
1147 dolChmod($filesql);
1148
1149 // Build zip file
1150 $filedir = $conf->website->dir_temp.'/'.$website->ref.'/.';
1151 $fileglob = $conf->website->dir_temp.'/'.$website->ref.'/website_'.$website->ref.'-*.zip';
1152 $filename = $conf->website->dir_temp.'/'.$website->ref.'/website_'.$website->ref.'-'.dol_print_date(dol_now(), 'dayhourlog').'-V'.((float) DOL_VERSION).'.zip';
1153
1154 dol_delete_file($fileglob, 0);
1155 $result = dol_compress_file($filedir, $filename, 'zip');
1156
1157 if ($result > 0) {
1158 return $filename;
1159 } else {
1160 global $errormsg;
1161 $this->error = $errormsg;
1162 return '';
1163 }
1164 }
1165
1166
1173 public function importWebSite($pathtofile)
1174 {
1175 global $conf, $mysoc;
1176
1177 $error = 0;
1178
1179 $pathtofile = dol_sanitizePathName($pathtofile);
1180
1181 $object = $this;
1182 if (empty($object->ref)) {
1183 $this->error = 'Function importWebSite called on object not loaded (object->ref is empty)';
1184 return -2;
1185 }
1186
1187 dol_delete_dir_recursive($conf->website->dir_temp."/".$object->ref);
1188 dol_mkdir($conf->website->dir_temp.'/'.$object->ref);
1189
1190 $filename = basename($pathtofile);
1191 if (!preg_match('/^website_(.*)-(.*)$/', $filename, $reg)) {
1192 $this->errors[] = 'Bad format for filename '.$filename.'. Must be website_XXX-VERSION.';
1193 return -3;
1194 }
1195
1196 $result = dol_uncompress($pathtofile, $conf->website->dir_temp.'/'.$object->ref);
1197
1198 if (!empty($result['error'])) {
1199 $this->errors[] = 'Failed to unzip file '.$pathtofile.'.';
1200 return -4;
1201 }
1202
1203 $arrayreplacement = array();
1204 $arrayreplacement['__WEBSITE_ID__'] = $object->id;
1205 $arrayreplacement['__WEBSITE_KEY__'] = $object->ref;
1206 $arrayreplacement['__N__'] = $this->db->escape("\n"); // Restore \n
1207 $arrayreplacement['__LOGO_SMALL_KEY__'] = $this->db->escape($mysoc->logo_small);
1208 $arrayreplacement['__LOGO_MINI_KEY__'] = $this->db->escape($mysoc->logo_mini);
1209 $arrayreplacement['__LOGO_KEY__'] = $this->db->escape($mysoc->logo);
1210
1211 // Copy containers
1212 dolCopyDir($conf->website->dir_temp.'/'.$object->ref.'/containers', $conf->website->dir_output.'/'.$object->ref, 0, 1); // Overwrite if exists
1213
1214 // Make replacement into css and htmlheader file
1215 $cssindestdir = $conf->website->dir_output.'/'.$object->ref.'/styles.css.php';
1216 $result = dolReplaceInFile($cssindestdir, $arrayreplacement);
1217
1218 $htmldeaderindestdir = $conf->website->dir_output.'/'.$object->ref.'/htmlheader.html';
1219 $result = dolReplaceInFile($htmldeaderindestdir, $arrayreplacement);
1220
1221 // Now generate the master.inc.php page
1222 $filemaster = $conf->website->dir_output.'/'.$object->ref.'/master.inc.php';
1223 $result = dolSaveMasterFile($filemaster);
1224 if (!$result) {
1225 $this->errors[] = 'Failed to write file '.$filemaster;
1226 $error++;
1227 }
1228
1229 dolCopyDir($conf->website->dir_temp.'/'.$object->ref.'/medias/image/websitekey', $conf->website->dir_output.'/'.$object->ref.'/medias/image/'.$object->ref, 0, 1); // Medias can be shared, do not overwrite if exists
1230 dolCopyDir($conf->website->dir_temp.'/'.$object->ref.'/medias/js/websitekey', $conf->website->dir_output.'/'.$object->ref.'/medias/js/'.$object->ref, 0, 1); // Medias can be shared, do not overwrite if exists
1231
1232 $sqlfile = $conf->website->dir_temp."/".$object->ref.'/website_pages.sql';
1233
1234 $result = dolReplaceInFile($sqlfile, $arrayreplacement);
1235
1236 $this->db->begin();
1237
1238 // Search the $maxrowid because we need it later
1239 $sqlgetrowid = 'SELECT MAX(rowid) as max from '.MAIN_DB_PREFIX.'website_page';
1240 $resql = $this->db->query($sqlgetrowid);
1241 if ($resql) {
1242 $obj = $this->db->fetch_object($resql);
1243 $maxrowid = $obj->max;
1244 }
1245
1246 // Load sql record
1247 $runsql = run_sql($sqlfile, 1, '', 0, '', 'none', 0, 1, 0, 0, 1); // The maxrowid of table is searched into this function two
1248 if ($runsql <= 0) {
1249 $this->errors[] = 'Failed to load sql file '.$sqlfile.' (ret='.((int) $runsql).')';
1250 $error++;
1251 }
1252
1253 $objectpagestatic = new WebsitePage($this->db);
1254
1255 // Make replacement of IDs
1256 $fp = fopen($sqlfile, "r");
1257 if ($fp) {
1258 while (!feof($fp)) {
1259 $reg = array();
1260
1261 // Warning fgets with second parameter that is null or 0 hang.
1262 $buf = fgets($fp, 65000);
1263 if (preg_match('/^-- Page ID (\d+)\s[^\s]+\s(\d+).*Aliases\s(.*)\s--;/i', $buf, $reg)) {
1264 $oldid = $reg[1];
1265 $newid = ($reg[2] + $maxrowid);
1266 $aliasesarray = explode(',', $reg[3]);
1267
1268 dol_syslog("Found ID ".$oldid." to replace with ID ".$newid." and shortcut aliases to create: ".$reg[3]);
1269
1270 dol_move($conf->website->dir_output.'/'.$object->ref.'/page'.$oldid.'.tpl.php', $conf->website->dir_output.'/'.$object->ref.'/page'.$newid.'.tpl.php', 0, 1, 0, 0);
1271
1272 $objectpagestatic->fetch($newid);
1273
1274 // The move is not enough, so we regenerate page
1275 $filetpl = $conf->website->dir_output.'/'.$object->ref.'/page'.$newid.'.tpl.php';
1276 $result = dolSavePageContent($filetpl, $object, $objectpagestatic);
1277 if (!$result) {
1278 $this->errors[] = 'Failed to write file '.basename($filetpl);
1279 $error++;
1280 }
1281
1282 // Regenerate alternative aliases pages
1283 if (is_array($aliasesarray)) {
1284 foreach ($aliasesarray as $aliasshortcuttocreate) {
1285 if (trim($aliasshortcuttocreate)) {
1286 $filealias = $conf->website->dir_output.'/'.$object->ref.'/'.trim($aliasshortcuttocreate).'.php';
1287 $result = dolSavePageAlias($filealias, $object, $objectpagestatic);
1288 if (!$result) {
1289 $this->errors[] = 'Failed to write file '.basename($filealias);
1290 $error++;
1291 }
1292 }
1293 }
1294 }
1295 }
1296 }
1297 }
1298
1299 // Read record of website that has been updated by the run_sql function previously called so we can get the
1300 // value of fk_default_home that is ID of home page
1301 $sql = "SELECT fk_default_home FROM ".MAIN_DB_PREFIX."website WHERE rowid = ".((int) $object->id);
1302 $resql = $this->db->query($sql);
1303 if ($resql) {
1304 $obj = $this->db->fetch_object($resql);
1305 if ($obj) {
1306 $object->fk_default_home = $obj->fk_default_home;
1307 } else {
1308 //$this->errors[] = 'Failed to get the Home page';
1309 //$error++;
1310 }
1311 }
1312
1313 // Regenerate index page to point to the new index page
1314 $pathofwebsite = $conf->website->dir_output.'/'.$object->ref;
1315 dolSaveIndexPage($pathofwebsite, $pathofwebsite.'/index.php', $pathofwebsite.'/page'.$object->fk_default_home.'.tpl.php', $pathofwebsite.'/wrapper.php', $object);
1316
1317 if ($error) {
1318 $this->db->rollback();
1319 return -1;
1320 } else {
1321 $this->db->commit();
1322 return $object->id;
1323 }
1324 }
1325
1332 public function rebuildWebSiteFiles()
1333 {
1334 global $conf;
1335
1336 $error = 0;
1337
1338 $object = $this;
1339 if (empty($object->ref)) {
1340 $this->error = 'Function rebuildWebSiteFiles called on object not loaded (object->ref is empty)';
1341 return -1;
1342 }
1343
1344 $objectpagestatic = new WebsitePage($this->db);
1345
1346 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."website_page WHERE fk_website = ".((int) $this->id);
1347
1348 $resql = $this->db->query($sql);
1349 if (!$resql) {
1350 $this->error = $this->db->lasterror();
1351 return -1;
1352 }
1353
1354 $num = $this->db->num_rows($resql);
1355
1356 // Loop on each container/page
1357 $i = 0;
1358 while ($i < $num) {
1359 $obj = $this->db->fetch_object($resql);
1360
1361 $newid = $obj->rowid;
1362
1363 $objectpagestatic->fetch($newid);
1364
1365 $aliasesarray = explode(',', $objectpagestatic->aliasalt);
1366
1367 $filetpl = $conf->website->dir_output.'/'.$object->ref.'/page'.$newid.'.tpl.php';
1368 $result = dolSavePageContent($filetpl, $object, $objectpagestatic);
1369 if (!$result) {
1370 $this->errors[] = 'Failed to write file '.basename($filetpl);
1371 $error++;
1372 }
1373
1374 // Add main alias to list of alternative aliases
1375 if (!empty($objectpagestatic->pageurl) && !in_array($objectpagestatic->pageurl, $aliasesarray)) {
1376 $aliasesarray[] = $objectpagestatic->pageurl;
1377 }
1378
1379 // Regenerate also all aliases pages (pages with a natural name) by calling dolSavePageAlias()
1380 if (is_array($aliasesarray)) {
1381 foreach ($aliasesarray as $aliasshortcuttocreate) {
1382 if (trim($aliasshortcuttocreate)) {
1383 $filealias = $conf->website->dir_output.'/'.$object->ref.'/'.trim($aliasshortcuttocreate).'.php';
1384 $result = dolSavePageAlias($filealias, $object, $objectpagestatic); // This includes also a copy into sublanguage directories.
1385 if (!$result) {
1386 $this->errors[] = 'Failed to write file '.basename($filealias);
1387 $error++;
1388 }
1389 }
1390 }
1391 }
1392
1393 $i++;
1394 }
1395
1396 if (!$error) {
1397 // Save index.php and wrapper.php
1398 $pathofwebsite = $conf->website->dir_output.'/'.$object->ref;
1399 $fileindex = $pathofwebsite.'/index.php';
1400 $filetpl = '';
1401 if ($object->fk_default_home > 0) {
1402 $filetpl = $pathofwebsite.'/page'.$object->fk_default_home.'.tpl.php';
1403 }
1404 $filewrapper = $pathofwebsite.'/wrapper.php';
1405 dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper, $object); // This includes also a version of index.php into sublanguage directories
1406 }
1407
1408 if ($error) {
1409 return -1;
1410 } else {
1411 return $num;
1412 }
1413 }
1414
1420 public function isMultiLang()
1421 {
1422 return (empty($this->otherlang) ? false : true);
1423 }
1424
1434 public function componentSelectLang($languagecodes, $weblangs, $morecss = '', $htmlname = '')
1435 {
1436 global $websitepagefile, $website;
1437
1438 if (!is_object($weblangs)) {
1439 return 'ERROR componentSelectLang called with parameter $weblangs not defined';
1440 }
1441
1442 $arrayofspecialmainlanguages = array(
1443 'en'=>'en_US',
1444 'sq'=>'sq_AL',
1445 'ar'=>'ar_SA',
1446 'eu'=>'eu_ES',
1447 'bn'=>'bn_DB',
1448 'bs'=>'bs_BA',
1449 'ca'=>'ca_ES',
1450 'zh'=>'zh_CN',
1451 'cs'=>'cs_CZ',
1452 'da'=>'da_DK',
1453 'et'=>'et_EE',
1454 'ka'=>'ka_GE',
1455 'el'=>'el_GR',
1456 'he'=>'he_IL',
1457 'kn'=>'kn_IN',
1458 'km'=>'km_KH',
1459 'ko'=>'ko_KR',
1460 'lo'=>'lo_LA',
1461 'nb'=>'nb_NO',
1462 'fa'=>'fa_IR',
1463 'sr'=>'sr_RS',
1464 'sl'=>'sl_SI',
1465 'uk'=>'uk_UA',
1466 'vi'=>'vi_VN'
1467 );
1468
1469 // Load tmppage if we have $websitepagefile defined
1470 $tmppage = new WebsitePage($this->db);
1471
1472 $pageid = 0;
1473 if (!empty($websitepagefile)) {
1474 $websitepagefileshort = basename($websitepagefile);
1475 if ($websitepagefileshort == 'index.php') {
1476 $pageid = $website->fk_default_home;
1477 } else {
1478 $pageid = str_replace(array('.tpl.php', 'page'), array('', ''), $websitepagefileshort);
1479 }
1480 if ($pageid > 0) {
1481 $tmppage->fetch($pageid);
1482 }
1483 }
1484
1485 // Fill $languagecodes array with existing translation, nothing if none
1486 if (!is_array($languagecodes) && $pageid > 0) {
1487 $languagecodes = array();
1488
1489 $sql = "SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page";
1490 $sql .= " FROM ".MAIN_DB_PREFIX."website_page as wp";
1491 $sql .= " WHERE wp.fk_website = ".((int) $website->id);
1492 $sql .= " AND (wp.fk_page = ".((int) $pageid)." OR wp.rowid = ".((int) $pageid);
1493 if ($tmppage->fk_page > 0) {
1494 $sql .= " OR wp.fk_page = ".((int) $tmppage->fk_page)." OR wp.rowid = ".((int) $tmppage->fk_page);
1495 }
1496 $sql .= ")";
1497
1498 $resql = $this->db->query($sql);
1499 if ($resql) {
1500 while ($obj = $this->db->fetch_object($resql)) {
1501 $newlang = $obj->lang;
1502 if ($obj->rowid == $pageid) {
1503 $newlang = $obj->lang;
1504 }
1505 if (!in_array($newlang, $languagecodes)) {
1506 $languagecodes[] = $newlang;
1507 }
1508 }
1509 }
1510 }
1511 // Now $languagecodes is always an array. Example array('en', 'fr', 'es');
1512
1513 $languagecodeselected = substr($weblangs->defaultlang, 0, 2); // Because we must init with a value, but real value is the lang of main parent container
1514 if (!empty($websitepagefile)) {
1515 $pageid = str_replace(array('.tpl.php', 'page'), array('', ''), basename($websitepagefile));
1516 if ($pageid > 0) {
1517 $pagelang = substr($tmppage->lang, 0, 2);
1518 $languagecodeselected = substr($pagelang, 0, 2);
1519 if (!in_array($pagelang, $languagecodes)) {
1520 $languagecodes[] = $pagelang; // We add language code of page into combo list
1521 }
1522 }
1523 }
1524
1525 $weblangs->load('languages');
1526 //var_dump($weblangs->defaultlang);
1527
1528 $url = $_SERVER["REQUEST_URI"];
1529 $url = preg_replace('/(\?|&)l=([a-zA-Z_]*)/', '', $url); // We remove param l from url
1530 //$url = preg_replace('/(\?|&)lang=([a-zA-Z_]*)/', '', $url); // We remove param lang from url
1531 $url .= (preg_match('/\?/', $url) ? '&' : '?').'l=';
1532 if (!preg_match('/^\//', $url)) {
1533 $url = '/'.$url;
1534 }
1535
1536 $HEIGHTOPTION = 40;
1537 $MAXHEIGHT = 4 * $HEIGHTOPTION;
1538 $nboflanguage = count($languagecodes);
1539
1540 $out = '<!-- componentSelectLang'.$htmlname.' -->'."\n";
1541
1542 $out .= '<style>';
1543 $out .= '.componentSelectLang'.$htmlname.':hover { height: '.min($MAXHEIGHT, ($HEIGHTOPTION * $nboflanguage)).'px; overflow-x: hidden; overflow-y: '.((($HEIGHTOPTION * $nboflanguage) > $MAXHEIGHT) ? ' scroll' : 'hidden').'; }'."\n";
1544 $out .= '.componentSelectLang'.$htmlname.' li { line-height: '.$HEIGHTOPTION.'px; }'."\n";
1545 $out .= '.componentSelectLang'.$htmlname.' {
1546 display: inline-block;
1547 padding: 0;
1548 height: '.$HEIGHTOPTION.'px;
1549 overflow: hidden;
1550 transition: all .3s ease;
1551 margin: 0 0 0 0;
1552 vertical-align: top;
1553 }
1554 .componentSelectLang'.$htmlname.':hover, .componentSelectLang'.$htmlname.':hover a { background-color: #fff; color: #000 !important; }
1555 ul.componentSelectLang'.$htmlname.' { width: 150px; }
1556 ul.componentSelectLang'.$htmlname.':hover .fa { visibility: hidden; }
1557 .componentSelectLang'.$htmlname.' a { text-decoration: none; width: 100%; }
1558 .componentSelectLang'.$htmlname.' li { display: block; padding: 0px 15px; margin-left: 0; margin-right: 0; }
1559 .componentSelectLang'.$htmlname.' li:hover { background-color: #EEE; }
1560 ';
1561 $out .= '</style>';
1562 $out .= '<ul class="componentSelectLang'.$htmlname.($morecss ? ' '.$morecss : '').'">';
1563
1564 if ($languagecodeselected) {
1565 // Convert $languagecodeselected into a long language code
1566 if (strlen($languagecodeselected) == 2) {
1567 $languagecodeselected = (empty($arrayofspecialmainlanguages[$languagecodeselected]) ? $languagecodeselected.'_'.strtoupper($languagecodeselected) : $arrayofspecialmainlanguages[$languagecodeselected]);
1568 }
1569
1570 $countrycode = strtolower(substr($languagecodeselected, -2));
1571 $label = $weblangs->trans("Language_".$languagecodeselected);
1572 if ($countrycode == 'us') {
1573 $label = preg_replace('/\s*\‍(.*\‍)/', '', $label);
1574 }
1575 $out .= '<li><a href="'.$url.substr($languagecodeselected, 0, 2).'"><img height="12px" src="/medias/image/common/flags/'.$countrycode.'.png" style="margin-right: 5px;"/><span class="websitecomponentlilang">'.$label.'</span>';
1576 $out .= '<span class="fa fa-caret-down" style="padding-left: 5px;" />';
1577 $out .= '</a></li>';
1578 }
1579 $i = 0;
1580 if (is_array($languagecodes)) {
1581 foreach ($languagecodes as $languagecode) {
1582 // Convert $languagecode into a long language code
1583 if (strlen($languagecode) == 2) {
1584 $languagecode = (empty($arrayofspecialmainlanguages[$languagecode]) ? $languagecode.'_'.strtoupper($languagecode) : $arrayofspecialmainlanguages[$languagecode]);
1585 }
1586
1587 if ($languagecode == $languagecodeselected) {
1588 continue; // Already output
1589 }
1590
1591 $countrycode = strtolower(substr($languagecode, -2));
1592 $label = $weblangs->trans("Language_".$languagecode);
1593 if ($countrycode == 'us') {
1594 $label = preg_replace('/\s*\‍(.*\‍)/', '', $label);
1595 }
1596 $out .= '<li><a href="'.$url.substr($languagecode, 0, 2).'"><img height="12px" src="/medias/image/common/flags/'.$countrycode.'.png" style="margin-right: 5px;"/><span class="websitecomponentlilang">'.$label.'</span>';
1597 if (empty($i) && empty($languagecodeselected)) {
1598 $out .= '<span class="fa fa-caret-down" style="padding-left: 5px;" />';
1599 }
1600 $out .= '</a></li>';
1601 $i++;
1602 }
1603 }
1604 $out .= '</ul>';
1605
1606 return $out;
1607 }
1608}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Definition security.php:604
run_sql($sqlfile, $silent=1, $entity=0, $usesavepoint=1, $handler='', $okerror='default', $linelengthlimit=32768, $nocommentremoval=0, $offsetforchartofaccount=0, $colspan=0, $onlysqltoimportwebsite=0, $database='')
Launch a sql file.
$object ref
Definition info.php:79
Parent class of all other business classes (invoices, contracts, proposals, orders,...
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
Class Website.
fetchLines()
Load object lines in memory from the database.
update(User $user, $notrigger=false)
Update object into database.
isMultiLang()
Return if web site is a multilanguage web site.
rebuildWebSiteFiles()
Rebuild all files of all the pages/containers of a website.
exportWebSite()
Generate a zip with all data of web site.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
componentSelectLang($languagecodes, $weblangs, $morecss='', $htmlname='')
Component to select language inside a container (Full CSS Only)
createFromClone($user, $fromid, $newref, $newlang='')
Load a website its id and create a new one in database.
__construct(DoliDB $db)
Constructor.
getLibStatut($mode=0)
Return the label of the status.
LibStatut($status, $mode=0)
Return the label of a given status.
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
Return a link to the user card (with optionally the picto) Use this->id,this->lastname,...
fetch($id, $ref=null)
Load object in memory from the database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load all object in memory ($this->records) from the database.
importWebSite($pathtofile)
Open a zip with all data of web site and load it into database.
create(User $user, $notrigger=false)
Create object into database.
Class Websitepage.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array())
Move a file into another name.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_uncompress($inputfile, $outputdir)
Uncompress a file.
dol_is_file($pathoffile)
Return if path is a file.
dolReplaceInFile($srcfile, $arrayreplacement, $destfile='', $newmask=0, $indexdatabase=0, $arrayreplacementisregex=0)
Make replacement of strings into a file.
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=0)
Copy a file to another file.
dolCopyDir($srcfile, $destfile, $newmask, $overwriteifexists, $arrayreplacement=null, $excludesubdir=0, $excludefileext=null)
Copy a dir to another dir.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
rtl background position
dolSaveMasterFile($filemaster)
Save content of a page on disk.
dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper, $object=null)
Save content of the index.php and/or the wrapper.php page.
dolSavePageAlias($filealias, $object, $objectpage)
Save an alias page on disk (A page that include the reference page).
dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage, $backupold=0)
Save content of a page on disk (page name is generally ID_of_page.php).