dolibarr 25.0.0-alpha
websitepage.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) 2020 Nicolas ZABOURI <info@inovea-conseil.com>
7 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30// Put here all includes required by your class file
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
32//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
33//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
34
38class WebsitePage extends CommonObject
39{
43 public $element = 'websitepage';
44
48 public $table_element = 'website_page';
49
53 public $picto = 'file-code';
54
58 public $fk_element = 'fk_website_page';
59
63 protected $childtablesoncascade = array('categorie_website_page');
64
68 public $fk_website;
69
73 public $fk_page; // If translation of another page
74
78 public $pageurl;
79
83 public $aliasalt;
84
88 public $type_container;
89
93 public $title;
94
98 public $description;
99
103 public $image;
104
108 public $keywords;
109
113 public $lang;
114
118 public $allowed_in_frames;
119
123 public $index = 1;
124
128 public $follow = 1;
129
133 public $disable_waf = 'NOSCANAUDIOFORINJECTION,NOSCANIFRAMEFORINJECTION,NOSCANOBJECTFORINJECTION'; // TODO Manage field in page setup
134
138 public $htmlheader;
139
143 public $content;
144
148 public $grabbed_from;
149
153 public $status;
154
158 public $fk_user_creat;
159
163 public $fk_user_modif;
164
168 public $author_alias;
169
173 public $object_type;
174
178 public $fk_object;
179
183 public $newid;
184
185
186 const STATUS_DRAFT = 0; // offline
187 const STATUS_VALIDATED = 1; // online
188
189
214 // BEGIN MODULEBUILDER PROPERTIES
218 public $fields = array(
219 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id'),
220 'pageurl' => array('type' => 'varchar(16)', 'label' => 'WEBSITE_PAGENAME', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'index' => 1, 'position' => 10, 'searchall' => 1, 'comment' => 'Ref/alias of page'),
221 'aliasalt' => array('type' => 'varchar(255)', 'label' => 'AliasAlt', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'index' => 0, 'position' => 11, 'searchall' => 0, 'comment' => 'Alias alternative of page'),
222 'type_container' => array('type' => 'varchar(16)', 'label' => 'Type', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'index' => 0, 'position' => 12, 'comment' => 'Type of container'),
223 'title' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'searchall' => 1, 'help' => 'UseTextBetween5And70Chars'),
224 'description' => array('type' => 'varchar(255)', 'label' => 'Description', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'searchall' => 1),
225 'image' => array('type' => 'varchar(255)', 'label' => 'Image', 'enabled' => 1, 'visible' => 1, 'position' => 32, 'searchall' => 0, 'help' => 'Relative path of media. Used if Type is "blogpost"'),
226 'keywords' => array('type' => 'varchar(255)', 'label' => 'Keywords', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'searchall' => 0),
227 'lang' => array('type' => 'varchar(6)', 'label' => 'Lang', 'enabled' => 1, 'notnull' => -1, 'visible' => 1, 'position' => 45, 'searchall' => 0),
228 //'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'index'=>true, 'position'=>1000),
229 'fk_website' => array('type' => 'integer', 'label' => 'WebsiteId', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 40, 'searchall' => 0, 'foreignkey' => 'websitepage.rowid'),
230 'fk_page' => array('type' => 'integer', 'label' => 'ParentPageId', 'enabled' => 1, 'visible' => 1, 'notnull' => -1, 'position' => 45, 'searchall' => 0, 'foreignkey' => 'website.rowid'),
231 'allowed_in_frames' => array('type' => 'integer', 'label' => 'AllowedInFrames', 'enabled' => 1, 'visible' => -1, 'position' => 48, 'searchall' => 0, 'default' => '0'),
232 'htmlheader' => array('type' => 'html', 'label' => 'HtmlHeader', 'enabled' => 1, 'visible' => 0, 'position' => 50, 'searchall' => 0),
233 'content' => array('type' => 'mediumtext', 'label' => 'Content', 'enabled' => 1, 'visible' => 0, 'position' => 51, 'searchall' => 0),
234 'grabbed_from' => array('type' => 'varchar(255)', 'label' => 'GrabbedFrom', 'enabled' => 1, 'visible' => 1, 'index' => 1, 'position' => 400, 'comment' => 'URL page content was grabbed from'),
235 'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 500),
236 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 501),
237 //'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>502),
238 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 510),
239 'author_alias' => array('type' => 'varchar(64)', 'label' => 'AuthorAlias', 'enabled' => 1, 'visible' => -1, 'index' => 0, 'position' => 511, 'comment' => 'Author alias'),
240 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -1, 'position' => 512),
241 //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
242 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'index' => 1, 'position' => 1000, 'notnull' => -1),
243 'object_type' => array('type' => 'varchar(255)', 'label' => 'ObjectType', 'enabled' => 1, 'visible' => 0, 'position' => 46, 'searchall' => 0, 'help' => ''),
244 'fk_object' => array('type' => 'varchar(255)', 'label' => 'ObjectId', 'enabled' => 1, 'visible' => 0, 'position' => 47, 'searchall' => 0, 'help' => ''),
245 'status' => array('type' => 'integer', 'label' => 'Status', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 510),
246 );
247 // END MODULEBUILDER PROPERTIES
248
249
255 public function __construct(DoliDB $db)
256 {
257 $this->db = $db;
258 }
259
267 public function create(User $user, $notrigger = 0)
268 {
269 $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
270 $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
271 if ($this->aliasalt) {
272 $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
273 }
274
275 $this->pageurl = preg_replace('/[^a-z0-9\-\_]/i', '', $this->pageurl);
276 $this->pageurl = preg_replace('/\-\-+/', '-', $this->pageurl);
277 $this->pageurl = preg_replace('/^\-/', '', $this->pageurl);
278
279 // Remove spaces and be sure we have main language only
280 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
281
282 // Check there is no PHP into HTML header
283 $dataposted = trim($this->htmlheader); // Must accept tags like '<script>' and '<link>'
284 $dataposted = preg_replace(array('/<html>\n*/ims', '/<\/html>\n*/ims'), array('', ''), $dataposted);
285 $dataposted = str_replace('<?=', '<?php', $dataposted);
286
287 // Check there is no PHP content into the imported file (must be only HTML + JS)
288 // Note: This one may be useless because this->htmlheader should be retrieved now using GETPOST(..., 'restricthtmlallowlinkscript') so without PHP content. We keep it in case of.
289 $phpcontent = dolKeepOnlyPhpCode($this->htmlheader);
290
291 if ($phpcontent) {
292 $this->error = 'Error: you try to create htmlheader with PHP content inside, this is not allowed.';
293 $this->errors[] = $this->error;
294 return -1;
295 }
296
297 // Test if page contains dynamic PHP content
298 if (!$user->hasRight('website', 'writephp')) {
299 // Check there is no PHP content into the imported file (must be only HTML + JS)
300 $phpcontent = dolKeepOnlyPhpCode($this->content);
301
302 if ($phpcontent) {
303 $this->error = 'Error: you try to create a page with PHP content in HTML body without having permissions for that.';
304 $this->errors[] = $this->error;
305 return -1;
306 }
307 }
308
309 return $this->createCommon($user, $notrigger);
310 }
311
325 public function fetch($id, $website_id = null, $page = null, $aliasalt = null, $translationparentid = 0, $translationparentlang = '')
326 {
327 dol_syslog(__METHOD__, LOG_DEBUG);
328
329 $sql = 'SELECT';
330 $sql .= ' t.rowid,';
331 $sql .= " t.fk_website,";
332 $sql .= ' t.type_container,';
333 $sql .= " t.pageurl,";
334 $sql .= " t.aliasalt,";
335 $sql .= " t.title,";
336 $sql .= " t.description,";
337 $sql .= " t.image,";
338 $sql .= " t.keywords,";
339 $sql .= " t.htmlheader,";
340 $sql .= " t.content,";
341 $sql .= " t.lang,";
342 $sql .= " t.fk_page,"; // Translation parent page (in mani language)
343 $sql .= " t.allowed_in_frames,";
344 $sql .= " t.status,";
345 $sql .= " t.grabbed_from,";
346 $sql .= " t.date_creation,";
347 $sql .= " t.tms as date_modification,";
348 $sql .= " t.fk_user_creat,";
349 $sql .= " t.author_alias,";
350 $sql .= " t.fk_user_modif,";
351 $sql .= " t.import_key,";
352 $sql .= " t.object_type,";
353 $sql .= " t.fk_object";
354 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
355 //$sql .= ' WHERE entity IN ('.getEntity('website').')'; // entity is on website level
356 $sql .= ' WHERE 1 = 1';
357 if ($id > 0) {
358 $sql .= ' AND t.rowid = '.((int) $id);
359 } else {
360 if ($id < 0) {
361 $sql .= ' AND t.rowid <> '.((int) abs($id));
362 }
363 if (null !== $website_id) {
364 $sql .= " AND t.fk_website = ".((int) $website_id);
365 if ($page) {
366 $pagetouse = $page;
367 $langtouse = '';
368 $tmppage = explode('/', $page);
369 if (!empty($tmppage[1])) {
370 $pagetouse = $tmppage[1];
371 if (strlen($tmppage[0])) {
372 $langtouse = $tmppage[0];
373 }
374 }
375 $sql .= " AND t.pageurl = '".$this->db->escape($pagetouse)."'";
376 if ($langtouse) {
377 $sql .= " AND t.lang = '".$this->db->escape($langtouse)."'";
378 }
379 }
380 if ($aliasalt) {
381 $sql .= " AND (t.aliasalt LIKE '%,".$this->db->escape($this->db->escapeforlike($aliasalt)).",%' OR t.aliasalt LIKE '%, ".$this->db->escape($this->db->escapeforlike($aliasalt)).",%')";
382 }
383 if ($translationparentid && $translationparentlang) {
384 $sql .= " AND t.fk_page = ".((int) $translationparentid);
385 $sql .= " AND t.lang = '".$this->db->escape($translationparentlang)."'";
386 }
387 }
388 }
389 $sql .= $this->db->plimit(1);
390
391 $resql = $this->db->query($sql);
392 if ($resql) {
393 $numrows = $this->db->num_rows($resql);
394 if ($numrows) {
395 $obj = $this->db->fetch_object($resql);
396
397 $this->id = $obj->rowid;
398
399 $this->fk_website = $obj->fk_website;
400 $this->type_container = $obj->type_container;
401
402 $this->pageurl = $obj->pageurl;
403 $this->ref = $obj->pageurl;
404 $this->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
405
406 $this->title = $obj->title;
407 $this->description = $obj->description;
408 $this->image = $obj->image;
409 $this->keywords = $obj->keywords;
410 $this->htmlheader = $obj->htmlheader;
411 $this->content = $obj->content;
412 $this->lang = $obj->lang;
413 $this->fk_page = $obj->fk_page;
414 $this->allowed_in_frames = $obj->allowed_in_frames;
415 $this->status = $obj->status;
416 $this->grabbed_from = $obj->grabbed_from;
417 $this->date_creation = $this->db->jdate($obj->date_creation);
418 $this->date_modification = $this->db->jdate($obj->date_modification);
419 $this->fk_user_creat = $obj->fk_user_creat;
420 $this->author_alias = $obj->author_alias;
421 $this->fk_user_modif = $obj->fk_user_modif;
422 $this->import_key = $obj->import_key;
423 $this->object_type = $obj->object_type;
424 $this->fk_object = $obj->fk_object;
425 }
426 $this->db->free($resql);
427
428 if ($numrows) {
429 return 1;
430 } else {
431 return 0;
432 }
433 } else {
434 $this->errors[] = 'Error '.$this->db->lasterror();
435 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
436
437 return -1;
438 }
439 }
440
454 public function fetchAll($websiteid = '', $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
455 {
456 dol_syslog(__METHOD__, LOG_DEBUG);
457
458 $records = array();
459
460 $sql = 'SELECT';
461 $sql .= ' t.rowid,';
462 $sql .= " t.fk_website,";
463 $sql .= " t.type_container,";
464 $sql .= " t.pageurl,";
465 $sql .= " t.aliasalt,";
466 $sql .= " t.title,";
467 $sql .= " t.description,";
468 $sql .= " t.image,";
469 $sql .= " t.keywords,";
470 $sql .= " t.htmlheader,";
471 $sql .= " t.content,";
472 $sql .= " t.lang,";
473 $sql .= " t.fk_page,";
474 $sql .= " t.allowed_in_frames,";
475 $sql .= " t.status,";
476 $sql .= " t.grabbed_from,";
477 $sql .= " t.date_creation,";
478 $sql .= " t.tms as date_modification,";
479 $sql .= " t.fk_user_creat,";
480 $sql .= " t.author_alias,";
481 $sql .= " t.fk_user_modif,";
482 $sql .= " t.import_key,";
483 $sql .= " t.object_type,";
484 $sql .= " t.fk_object";
485 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
486 if (!empty($websiteid)) {
487 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
488 }
489
490 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
491 if (is_array($filter)) {
492 $sqlwhere = array();
493
494 if (count($filter) > 0) {
495 foreach ($filter as $key => $value) {
496 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
497 $sqlwhere[] = $key." = ".((int) $value);
498 } elseif ($key == 'type_container' || $key == 't.type_container') {
499 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
500 } elseif ($key == 'lang' || $key == 't.lang') {
501 $listoflang = array();
502 $foundnull = 0;
503 foreach (explode(',', $value) as $tmpvalue) {
504 if ($tmpvalue == 'null') {
505 $foundnull++;
506 continue;
507 }
508 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
509 }
510 $sql_extrawhere = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
511 if ($foundnull) {
512 $sql_extrawhere = "(".$sql_extrawhere." OR ".$this->db->sanitize($key)." IS NULL)";
513 }
514 $sqlwhere[] = $sql_extrawhere;
515 } else {
516 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
517 }
518 }
519 }
520 if (count($sqlwhere) > 0) {
521 if (!empty($websiteid)) {
522 $sql .= " AND (".implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere).')';
523 } else {
524 $sql .= " WHERE ".implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere);
525 }
526 }
527
528 $filter = '';
529 }
530
531 $errormessage = '';
532 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
533 if ($errormessage) {
534 $this->errors[] = $errormessage;
535 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
536 return -1;
537 }
538
539 if (!empty($sortfield)) {
540 $sql .= $this->db->order($sortfield, $sortorder);
541 }
542 if (!empty($limit)) {
543 $sql .= $this->db->plimit($limit, $offset);
544 }
545
546 $resql = $this->db->query($sql);
547 if ($resql) {
548 $num = $this->db->num_rows($resql);
549
550 while ($obj = $this->db->fetch_object($resql)) {
551 $record = new self($this->db);
552
553 $record->id = $obj->rowid;
554 $record->fk_website = $obj->fk_website;
555 $record->type_container = $obj->type_container;
556 $record->pageurl = $obj->pageurl;
557 $record->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
558 $record->title = $obj->title;
559 $record->description = $obj->description;
560 $record->image = $obj->image;
561 $record->keywords = $obj->keywords;
562 $record->htmlheader = $obj->htmlheader;
563 $record->content = $obj->content;
564 $record->lang = $obj->lang;
565 $record->fk_page = $obj->fk_page;
566 $record->allowed_in_frames = $obj->allowed_in_frames;
567 $record->status = $obj->status;
568 $record->grabbed_from = $obj->grabbed_from;
569 $record->date_creation = $this->db->jdate($obj->date_creation);
570 $record->date_modification = $this->db->jdate($obj->date_modification);
571 $record->fk_user_creat = $obj->fk_user_creat;
572 $record->author_alias = $obj->author_alias;
573 $record->fk_user_modif = $obj->fk_user_modif;
574 $record->import_key = $obj->import_key;
575 $record->object_type = $obj->object_type;
576 $record->fk_object = $obj->fk_object;
577 //var_dump($record->id);
578 $records[$record->id] = $record;
579 }
580 $this->db->free($resql);
581
582 return $records;
583 } else {
584 $this->error = 'Error '.$this->db->lasterror();
585 $this->errors[] = $this->error;
586 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
587
588 return -1;
589 }
590 }
591
601 public function countAll($websiteid, $filter = '', $filtermode = 'AND')
602 {
603 dol_syslog(__METHOD__, LOG_DEBUG);
604
605 $result = 0;
606
607 $sql = 'SELECT COUNT(t.rowid) as nb';
608 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
609 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
610
611 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
612 if (is_array($filter)) {
613 $sqlwhere = array();
614
615 if (count($filter) > 0) {
616 foreach ($filter as $key => $value) {
617 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
618 $sqlwhere[] = $key." = ".((int) $value);
619 } elseif ($key == 'type_container' || $key == 't.type_container') {
620 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
621 } elseif ($key == 'lang' || $key == 't.lang') {
622 $listoflang = array();
623 $foundnull = 0;
624 foreach (explode(',', $value) as $tmpvalue) {
625 if ($tmpvalue == 'null') {
626 $foundnull++;
627 continue;
628 }
629 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
630 }
631 $sql_extrawhere = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
632 if ($foundnull) {
633 $sql_extrawhere = "(".$sql_extrawhere." OR ".$this->db->sanitize($key)." IS NULL)";
634 }
635 $sqlwhere[] = $sql_extrawhere;
636 } else {
637 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
638 }
639 }
640 }
641 if (count($sqlwhere) > 0) {
642 if (!empty($websiteid)) {
643 $sql .= " AND (".implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere).')';
644 } else {
645 $sql .= " WHERE ".implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere);
646 }
647 }
648
649 $filter = '';
650 }
651
652 $errormessage = '';
653 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
654 if ($errormessage) {
655 $this->errors[] = $errormessage;
656 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
657 return -1;
658 }
659
660 $resql = $this->db->query($sql);
661 if ($resql) {
662 $obj = $this->db->fetch_object($resql);
663 if ($obj) {
664 $result = $obj->nb;
665 }
666
667 $this->db->free($resql);
668
669 return $result;
670 } else {
671 $this->error = 'Error '.$this->db->lasterror();
672 $this->errors[] = $this->error;
673 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
674
675 return -1;
676 }
677 }
678
686 public function update(User $user, $notrigger = 0)
687 {
688 $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
689 $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
690 if ($this->aliasalt) {
691 $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
692 }
693
694 $this->pageurl = preg_replace('/[^a-z0-9\-\_]/i', '', $this->pageurl);
695 $this->pageurl = preg_replace('/\-\-+/', '-', $this->pageurl);
696 $this->pageurl = preg_replace('/^\-/', '', $this->pageurl);
697
698 // Remove spaces and be sure we have main language only
699 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
700
701 if ($this->fk_page > 0) {
702 if (empty($this->lang)) {
703 $this->error = "ErrorLanguageMandatoryIfPageSetAsTranslationOfAnother";
704 return -1;
705 }
706 $tmppage = new WebsitePage($this->db);
707 $tmppage->fetch((int) $this->fk_page);
708 if ($tmppage->lang == $this->lang) {
709 $this->error = "ErrorLanguageOfTranslatedPageIsSameThanThisPage";
710 return -1;
711 }
712 }
713
714 // Check there is no PHP content into the modifiedhtmlheader (must be only HTML + JS)
715 // Note: This one may be useless because this->htmlheader should be retrieved now using GETPOST(..., 'restricthtmlallowlinkscript') so without PHP content. We keep it in case of.
716 $phpcontent = dolKeepOnlyPhpCode($this->htmlheader);
717
718 if ($phpcontent) {
719 $this->error = 'Error: you try to create htmlheader with PHP content inside, this is not allowed.';
720 $this->errors[] = $this->error;
721 return -1;
722 }
723
724 return $this->updateCommon($user, $notrigger);
725 }
726
734 public function delete(User $user, $notrigger = 0)
735 {
736 global $conf;
737
738 $error = 0;
739
740 // Delete all child tables
741 if (!$error) {
742 foreach ($this->childtablesoncascade as $table) {
743 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
744 $sql .= " WHERE fk_website_page = ".(int) $this->id;
745
746 $result = $this->db->query($sql);
747 if (!$result) {
748 $error++;
749 $this->errors[] = $this->db->lasterror();
750 break;
751 }
752 }
753 }
754
755 if (!$error) {
756 $result = $this->deleteCommon($user, $notrigger);
757 if ($result <= 0) {
758 $error++;
759 }
760 }
761
762 if (!$error) {
763 $websiteobj = new Website($this->db);
764 $result = $websiteobj->fetch($this->fk_website);
765
766 if ($result > 0) {
767 global $dolibarr_main_data_root;
768 $pathofwebsite = $dolibarr_main_data_root.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.$websiteobj->ref;
769
770 $filealias = $pathofwebsite.'/'.$this->pageurl.'.php';
771 $filetpl = $pathofwebsite.'/page'.$this->id.'.tpl.php';
772
773 dol_delete_file($filealias);
774 dol_delete_file($filetpl);
775 } else {
776 $this->error = $websiteobj->error;
777 $this->errors = $websiteobj->errors;
778 }
779 }
780
781 if (!$error) {
782 return 1;
783 } else {
784 return -1;
785 }
786 }
787
801 public function createFromClone(User $user, $fromid, $newref, $newlang = '', $istranslation = 0, $newwebsite = 0, $newtitle = '', $website = null)
802 {
803 global $hookmanager, $langs;
804
805 $now = dol_now();
806 $error = 0;
807
808 dol_syslog(__METHOD__, LOG_DEBUG);
809
810 $object = new self($this->db);
811
812 // Clean parameters
813 if (empty($newref) && !empty($newtitle)) {
814 $newref = strtolower(dol_sanitizeFileName(preg_replace('/\s+/', '-', $newtitle), '-', 1));
815 }
816
817 // Check parameters
818 if (empty($newref)) {
819 $langs->load("errors");
820 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WEBSITE_TITLE"));
821 return -1;
822 }
823
824 if ($istranslation) {
825 if (is_null($website)) {
826 $website = new Website($this->db);
827 }
828 $website->fetch($object->fk_website);
829
830 if ($website->id != $newwebsite) {
831 $langs->load("errors");
832 $this->error = $langs->trans("WebsiteMustBeSameThanClonedPageIfTranslation");
833 return -1;
834 }
835 }
836
837 $this->db->begin();
838
839 // Load source object
840 $object->fetch($fromid);
841 // Reset object
842 $object->id = 0;
843
844 // Clear fields
845 $object->ref = $newref;
846 $object->pageurl = $newref;
847 $object->aliasalt = '';
848 $object->fk_user_creat = $user->id;
849 $object->author_alias = '';
850 $object->date_creation = $now;
851 $object->title = ($newtitle == '1' ? $object->title : ($newtitle ? $newtitle : $object->title));
852 $object->description = $object->title;
853 if (!empty($newlang)) {
854 $object->lang = $newlang;
855 }
856
857 if ($istranslation) {
858 if ($website->lang == $newlang) {
859 // The new page is into the website language, the parent page will be 0, and we must instead update the source page later.
860 $object->fk_page = 0;
861 } else {
862 $object->fk_page = $fromid;
863 }
864 } else {
865 $object->fk_page = 0;
866 }
867
868 if (!empty($newwebsite)) {
869 $object->fk_website = $newwebsite;
870 }
871 $object->import_key = '';
872 $object->status = self::STATUS_DRAFT;
873
874 // Create clone
875 $object->context['createfromclone'] = 'createfromclone';
876 $result = $object->create($user);
877 if ($result < 0) {
878 $error++;
880 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
881 }
882
883 if ($istranslation) {
884 if ($website->lang == $newlang) {
885 // We must now update the source page to link to the new page as a translation of.
886 $sql = "UPDATE ".MAIN_DB_PREFIX."website_page SET fk_page = ".((int) $result)." WHERE rowid = ".((int) $fromid);
887
888 $result = $this->db->query($sql);
889 if (!$result) {
890 $error++;
891 $this->error = $this->db->lasterror();
892 }
893 }
894 }
895
896 unset($object->context['createfromclone']);
897
898 // End
899 if (!$error) {
900 $this->db->commit();
901
902 return $object;
903 } else {
904 $this->db->rollback();
905
906 return -1;
907 }
908 }
909
921 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
922 {
923 global $langs, $conf, $db;
924 global $dolibarr_main_authentication, $dolibarr_main_demo;
925 global $menumanager;
926
927 $result = '';
928
929 $label = '<u>'.$langs->trans("Page").'</u>';
930 $label .= '<br>';
931 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
932 $label .= '<b>'.$langs->trans('ID').':</b> '.$this->id.'<br>';
933 $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
934 $label .= '<b>'.$langs->trans('Language').':</b> '.$this->lang;
935
936 $url = DOL_URL_ROOT.'/website/index.php?websiteid='.$this->fk_website.'&pageid='.$this->id;
937
938 $linkclose = '';
939 if (empty($notooltip)) {
940 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
941 $label = $langs->trans("ShowMyObject");
942 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
943 }
944 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
945 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
946 } else {
947 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
948 }
949
950 $linkstart = '<a href="'.$url.'"';
951 $linkstart .= $linkclose.'>';
952 $linkend = '</a>';
953
954 //$linkstart = $linkend = '';
955
956 $result .= $linkstart;
957 if ($withpicto) {
958 $result .= img_picto(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
959 }
960 if ($withpicto != 2) {
961 $result .= $this->ref;
962 }
963 $result .= $linkend;
964
965 return $result;
966 }
967
974 public function getLibStatut($mode = 0)
975 {
976 return $this->LibStatut($this->status, $mode);
977 }
978
979 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
987 public function LibStatut($status, $mode = 0)
988 {
989 // phpcs:enable
990 global $langs;
991
992 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
993 global $langs;
994 //$langs->load("mymodule");
995 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
996 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
997 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
998 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
999 }
1000
1001 $statusType = 'status5';
1002 if ($status == self::STATUS_VALIDATED) {
1003 $statusType = 'status4';
1004 }
1005
1006 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1007 }
1008
1019 public function setCategories($categories)
1020 {
1021 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1022 return $this->setCategoriesCommon($categories, Categorie::TYPE_WEBSITE_PAGE);
1023 }
1024
1031 public function initAsSpecimen()
1032 {
1033 global $user;
1034
1035 $this->id = 0;
1036
1037 $now = dol_now();
1038
1039 $this->fk_website = 0;
1040 $this->type_container = 'page';
1041 $this->pageurl = 'specimen';
1042 $this->aliasalt = 'specimenalt';
1043 $this->title = 'My Page';
1044 $this->description = 'This is my page';
1045 $this->image = '';
1046 $this->keywords = 'keyword1, keyword2';
1047 $this->allowed_in_frames = 1;
1048 $this->htmlheader = '';
1049 $this->content = '<html><body>This is a html content</body></html>';
1050 $this->status = self::STATUS_DRAFT;
1051 $this->grabbed_from = '';
1052 $this->date_creation = $now - (24 * 30 * 3600);
1053 $this->date_modification = $now - (24 * 7 * 3600);
1054 $this->fk_user_creat = $user->id;
1055 $this->author_alias = 'mypublicpseudo';
1056
1057 return 1;
1058 }
1059}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$object ref
Definition info.php:90
setErrorsFromObject($object)
setErrorsFromObject
createCommon(User $user, $notrigger=0)
Create object in the database.
updateCommon(User $user, $notrigger=0)
Update object into database.
setCategoriesCommon($categories, $type_categ='', $remove_existing=true)
Sets object to given categories.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
Class Website.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
dolKeepOnlyPhpCode($str)
Keep only PHP code part from a HTML string page.
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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_now($mode='gmt')
Return date for now.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.