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 $fullpageurl;
84
88 public $aliasalt;
89
93 public $type_container;
94
98 public $title;
99
103 public $description;
104
108 public $image;
109
113 public $keywords;
114
118 public $lang;
119
123 public $allowed_in_frames;
124
128 public $keep_history;
129
133 public $index = 1;
134
138 public $follow = 1;
139
143 public $disable_waf = 'NOSCANAUDIOFORINJECTION,NOSCANIFRAMEFORINJECTION,NOSCANOBJECTFORINJECTION'; // TODO Manage field in page setup
144
148 public $htmlheader;
149
153 public $content;
154
158 public $grabbed_from;
159
163 public $status;
164
168 public $fk_user_creat;
169
173 public $fk_user_modif;
174
178 public $author_alias;
179
183 public $object_type;
184
188 public $fk_object;
189
193 public $newid;
194
195
196 const STATUS_DRAFT = 0; // offline
197 const STATUS_VALIDATED = 1; // online
198
199
224 // BEGIN MODULEBUILDER PROPERTIES
228 public $fields = array(
229 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id'),
230 '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'),
231 'aliasalt' => array('type' => 'varchar(255)', 'label' => 'AliasAlt', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'index' => 0, 'position' => 11, 'searchall' => 0, 'comment' => 'Alias alternative of page'),
232 'type_container' => array('type' => 'varchar(16)', 'label' => 'Type', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'index' => 0, 'position' => 12, 'comment' => 'Type of container'),
233 'title' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'searchall' => 1, 'help' => 'UseTextBetween5And70Chars'),
234 'description' => array('type' => 'varchar(255)', 'label' => 'Description', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'searchall' => 1),
235 '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"'),
236 'keywords' => array('type' => 'varchar(255)', 'label' => 'Keywords', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'searchall' => 0),
237 'lang' => array('type' => 'varchar(6)', 'label' => 'Lang', 'enabled' => 1, 'notnull' => -1, 'visible' => 1, 'position' => 45, 'searchall' => 0),
238 //'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'index'=>true, 'position'=>1000),
239 'fk_website' => array('type' => 'integer', 'label' => 'WebsiteId', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 40, 'searchall' => 0, 'foreignkey' => 'websitepage.rowid'),
240 'fk_page' => array('type' => 'integer', 'label' => 'ParentPageId', 'enabled' => 1, 'visible' => 1, 'notnull' => -1, 'position' => 45, 'searchall' => 0, 'foreignkey' => 'website.rowid'),
241 'allowed_in_frames' => array('type' => 'integer', 'label' => 'AllowedInFrames', 'enabled' => 1, 'visible' => -1, 'position' => 48, 'searchall' => 0, 'default' => '0'),
242 'keep_history' => array('type' => 'integer', 'label' => 'NumberOfVersionsToKeepInHistory', 'enabled' => 1, 'visible' => -1, 'position' => 49, 'default' => '5', 'help' => 'NumberOfVersionsToKeepInHistoryHelp'),
243 'htmlheader' => array('type' => 'html', 'label' => 'HtmlHeader', 'enabled' => 1, 'visible' => 0, 'position' => 50, 'searchall' => 0),
244 'content' => array('type' => 'html', 'label' => 'Content', 'enabled' => 1, 'visible' => 0, 'position' => 51, 'searchall' => 0),
245 'grabbed_from' => array('type' => 'varchar(255)', 'label' => 'GrabbedFrom', 'enabled' => 1, 'visible' => 1, 'index' => 1, 'position' => 400, 'comment' => 'URL page content was grabbed from'),
246 'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 500),
247 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 501),
248 //'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>502),
249 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 510),
250 'author_alias' => array('type' => 'varchar(64)', 'label' => 'AuthorAlias', 'enabled' => 1, 'visible' => -1, 'index' => 0, 'position' => 511, 'comment' => 'Author alias'),
251 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -1, 'position' => 512),
252 //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
253 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'index' => 1, 'position' => 1000, 'notnull' => -1),
254 'object_type' => array('type' => 'varchar(255)', 'label' => 'ObjectType', 'enabled' => 1, 'visible' => 0, 'position' => 46, 'searchall' => 0, 'help' => ''),
255 'fk_object' => array('type' => 'varchar(255)', 'label' => 'ObjectId', 'enabled' => 1, 'visible' => 0, 'position' => 47, 'searchall' => 0, 'help' => ''),
256 'status' => array('type' => 'integer', 'label' => 'Status', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 510),
257 );
258 // END MODULEBUILDER PROPERTIES
259
260
266 public function __construct(DoliDB $db)
267 {
268 $this->db = $db;
269 }
270
279 public function create(User $user, $notrigger = 0, $phpwrappers = array())
280 {
281 $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
282 $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
283 if ($this->aliasalt) {
284 $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
285 }
286
287 $this->pageurl = preg_replace('/[^a-z0-9\-\_]/i', '', $this->pageurl);
288 $this->pageurl = preg_replace('/\-\-+/', '-', $this->pageurl);
289 $this->pageurl = preg_replace('/^\-/', '', $this->pageurl);
290
291 // Remove spaces and be sure we have main language only
292 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
293
294 // Check there is no PHP into HTML header
295 $dataposted = trim($this->htmlheader); // Must accept tags like '<script>' and '<link>'
296 $dataposted = preg_replace(array('/<html>\n*/ims', '/<\/html>\n*/ims'), array('', ''), $dataposted);
297 $dataposted = str_replace('<?=', '<?php', $dataposted);
298
299 // Check there is no PHP content into the imported file (must be only HTML + JS)
300 // 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.
301 $phpcontent = dolKeepOnlyPhpCode($this->htmlheader);
302
303 if ($phpcontent) {
304 $this->error = 'Error: you try to create htmlheader with PHP content inside, this is not allowed.';
305 $this->errors[] = $this->error;
306 return -1;
307 }
308
309 // Test if page contains dynamic PHP content
310 if (!$user->hasRight('website', 'writephp')) {
311 // Check there is no PHP content into the imported file (must be only HTML + JS)
312 // PHP wrappers added by the application itself (parameter $phpwrappers) are excluded from this test
313 $contenttocheck = (string) $this->content;
314 if (is_array($phpwrappers)) {
315 foreach ($phpwrappers as $wrapper) {
316 $contenttocheck = str_replace((string) $wrapper, '', $contenttocheck);
317 }
318 }
319 $phpcontent = dolKeepOnlyPhpCode($contenttocheck);
320
321 if ($phpcontent) {
322 $this->error = 'Error: you try to create a page with PHP content in HTML body without having permissions for that.';
323 $this->errors[] = $this->error;
324 return -1;
325 }
326 }
327
328 return $this->createCommon($user, $notrigger);
329 }
330
344 public function fetch($id, $website_id = null, $page = null, $aliasalt = null, $translationparentid = 0, $translationparentlang = '')
345 {
346 dol_syslog(__METHOD__, LOG_DEBUG);
347
348 $sql = 'SELECT';
349 $sql .= ' t.rowid,';
350 $sql .= " t.fk_website,";
351 $sql .= ' t.type_container,';
352 $sql .= " t.pageurl,";
353 $sql .= " t.aliasalt,";
354 $sql .= " t.title,";
355 $sql .= " t.description,";
356 $sql .= " t.image,";
357 $sql .= " t.keywords,";
358 $sql .= " t.htmlheader,";
359 $sql .= " t.content,";
360 $sql .= " t.lang,";
361 $sql .= " t.fk_page,"; // Translation parent page (in mani language)
362 $sql .= " t.allowed_in_frames,";
363 $sql .= " t.keep_history,";
364 $sql .= " t.status,";
365 $sql .= " t.grabbed_from,";
366 $sql .= " t.date_creation,";
367 $sql .= " t.tms as date_modification,";
368 $sql .= " t.fk_user_creat,";
369 $sql .= " t.author_alias,";
370 $sql .= " t.fk_user_modif,";
371 $sql .= " t.import_key,";
372 $sql .= " t.object_type,";
373 $sql .= " t.fk_object";
374 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
375 //$sql .= ' WHERE entity IN ('.getEntity('website').')'; // entity is on website level
376 $sql .= ' WHERE 1 = 1';
377 if ($id > 0) {
378 $sql .= ' AND t.rowid = '.((int) $id);
379 } else {
380 if ($id < 0) {
381 $sql .= ' AND t.rowid <> '.((int) abs($id));
382 }
383 if (null !== $website_id) {
384 $sql .= " AND t.fk_website = ".((int) $website_id);
385 if ($page) {
386 $pagetouse = $page;
387 $langtouse = '';
388 $tmppage = explode('/', $page);
389 if (!empty($tmppage[1])) {
390 $pagetouse = $tmppage[1];
391 if (strlen($tmppage[0])) {
392 $langtouse = $tmppage[0];
393 }
394 }
395 $sql .= " AND t.pageurl = '".$this->db->escape($pagetouse)."'";
396 if ($langtouse) {
397 $sql .= " AND t.lang = '".$this->db->escape($langtouse)."'";
398 }
399 }
400 if ($aliasalt) {
401 $sql .= " AND (t.aliasalt LIKE '%,".$this->db->escape($this->db->escapeforlike($aliasalt)).",%' OR t.aliasalt LIKE '%, ".$this->db->escape($this->db->escapeforlike($aliasalt)).",%')";
402 }
403 if ($translationparentid && $translationparentlang) {
404 $sql .= " AND t.fk_page = ".((int) $translationparentid);
405 $sql .= " AND t.lang = '".$this->db->escape($translationparentlang)."'";
406 }
407 }
408 }
409 $sql .= $this->db->plimit(1);
410
411 $resql = $this->db->query($sql);
412 if ($resql) {
413 $numrows = $this->db->num_rows($resql);
414 if ($numrows) {
415 $obj = $this->db->fetch_object($resql);
416
417 $this->id = $obj->rowid;
418
419 $this->fk_website = (int) $obj->fk_website;
420 $this->type_container = dol_sanitizeKeyCode($obj->type_container);
421
422 $this->pageurl = preg_replace('/[^\w_-]+/', '', $obj->pageurl); // Sanitize page url
423 $this->ref = $obj->pageurl;
424 $this->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
425
426 $this->title = $obj->title;
427 $this->description = $obj->description;
428 $this->image = $obj->image;
429 $this->keywords = $obj->keywords;
430 $this->htmlheader = $obj->htmlheader;
431 $this->content = $obj->content;
432 $this->lang = $obj->lang;
433 $this->fk_page = $obj->fk_page;
434 $this->allowed_in_frames = $obj->allowed_in_frames;
435 $this->keep_history = $obj->keep_history;
436 $this->status = (int) $obj->status;
437 $this->grabbed_from = $obj->grabbed_from;
438 $this->date_creation = $this->db->jdate($obj->date_creation);
439 $this->date_modification = $this->db->jdate($obj->date_modification);
440 $this->fk_user_creat = $obj->fk_user_creat;
441 $this->author_alias = $obj->author_alias;
442 $this->fk_user_modif = $obj->fk_user_modif;
443 $this->import_key = $obj->import_key;
444 $this->object_type = $obj->object_type;
445 $this->fk_object = $obj->fk_object;
446 }
447 $this->db->free($resql);
448
449 if ($numrows) {
450 return 1;
451 } else {
452 return 0;
453 }
454 } else {
455 $this->errors[] = 'Error '.$this->db->lasterror();
456 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
457
458 return -1;
459 }
460 }
461
475 public function fetchAll($websiteid = '', $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
476 {
477 dol_syslog(__METHOD__, LOG_DEBUG);
478
479 $records = array();
480
481 $sql = 'SELECT';
482 $sql .= ' t.rowid,';
483 $sql .= " t.fk_website,";
484 $sql .= " t.type_container,";
485 $sql .= " t.pageurl,";
486 $sql .= " t.aliasalt,";
487 $sql .= " t.title,";
488 $sql .= " t.description,";
489 $sql .= " t.image,";
490 $sql .= " t.keywords,";
491 $sql .= " t.htmlheader,";
492 $sql .= " t.content,";
493 $sql .= " t.lang,";
494 $sql .= " t.fk_page,";
495 $sql .= " t.allowed_in_frames,";
496 $sql .= " t.status,";
497 $sql .= " t.grabbed_from,";
498 $sql .= " t.date_creation,";
499 $sql .= " t.tms as date_modification,";
500 $sql .= " t.fk_user_creat,";
501 $sql .= " t.author_alias,";
502 $sql .= " t.fk_user_modif,";
503 $sql .= " t.import_key,";
504 $sql .= " t.object_type,";
505 $sql .= " t.fk_object";
506 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
507 if (!empty($websiteid)) {
508 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
509 }
510
511 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
512 if (is_array($filter)) {
513 $sqlwhere = array();
514
515 if (count($filter) > 0) {
516 foreach ($filter as $key => $value) {
517 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
518 $sqlwhere[] = $key." = ".((int) $value);
519 } elseif ($key == 'type_container' || $key == 't.type_container') {
520 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
521 } elseif ($key == 'lang' || $key == 't.lang') {
522 $listoflang = array();
523 $foundnull = 0;
524 foreach (explode(',', $value) as $tmpvalue) {
525 if ($tmpvalue == 'null') {
526 $foundnull++;
527 continue;
528 }
529 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
530 }
531 $sql_extrawhere = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
532 if ($foundnull) {
533 $sql_extrawhere = "(".$sql_extrawhere." OR ".$this->db->sanitize($key)." IS NULL)";
534 }
535 $sqlwhere[] = $sql_extrawhere;
536 } else {
537 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
538 }
539 }
540 }
541 if (count($sqlwhere) > 0) {
542 if (!empty($websiteid)) {
543 $sql .= " AND (".implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere).')';
544 } else {
545 $sql .= " WHERE ".implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere);
546 }
547 }
548
549 $filter = '';
550 }
551
552 $errormessage = '';
553 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
554 if ($errormessage) {
555 $this->errors[] = $errormessage;
556 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
557 return -1;
558 }
559
560 if (!empty($sortfield)) {
561 $sql .= $this->db->order($sortfield, $sortorder);
562 }
563 if (!empty($limit)) {
564 $sql .= $this->db->plimit($limit, $offset);
565 }
566
567 $resql = $this->db->query($sql);
568 if ($resql) {
569 $num = $this->db->num_rows($resql);
570
571 while ($obj = $this->db->fetch_object($resql)) {
572 $record = new self($this->db);
573
574 $record->id = $obj->rowid;
575 $record->fk_website = $obj->fk_website;
576 $record->type_container = $obj->type_container;
577 $record->pageurl = $obj->pageurl;
578 $record->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
579 $record->title = $obj->title;
580 $record->description = $obj->description;
581 $record->image = $obj->image;
582 $record->keywords = $obj->keywords;
583 $record->htmlheader = $obj->htmlheader;
584 $record->content = $obj->content;
585 $record->lang = $obj->lang;
586 $record->fk_page = $obj->fk_page;
587 $record->allowed_in_frames = $obj->allowed_in_frames;
588 $record->status = $obj->status;
589 $record->grabbed_from = $obj->grabbed_from;
590 $record->date_creation = $this->db->jdate($obj->date_creation);
591 $record->date_modification = $this->db->jdate($obj->date_modification);
592 $record->fk_user_creat = $obj->fk_user_creat;
593 $record->author_alias = $obj->author_alias;
594 $record->fk_user_modif = $obj->fk_user_modif;
595 $record->import_key = $obj->import_key;
596 $record->object_type = $obj->object_type;
597 $record->fk_object = $obj->fk_object;
598 //var_dump($record->id);
599 $records[$record->id] = $record;
600 }
601 $this->db->free($resql);
602
603 return $records;
604 } else {
605 $this->error = 'Error '.$this->db->lasterror();
606 $this->errors[] = $this->error;
607 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
608
609 return -1;
610 }
611 }
612
622 public function countAll($websiteid, $filter = '', $filtermode = 'AND')
623 {
624 dol_syslog(__METHOD__, LOG_DEBUG);
625
626 $result = 0;
627
628 $sql = 'SELECT COUNT(t.rowid) as nb';
629 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
630 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
631
632 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
633 if (is_array($filter)) {
634 $sqlwhere = array();
635
636 if (count($filter) > 0) {
637 foreach ($filter as $key => $value) {
638 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
639 $sqlwhere[] = $key." = ".((int) $value);
640 } elseif ($key == 'type_container' || $key == 't.type_container') {
641 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
642 } elseif ($key == 'lang' || $key == 't.lang') {
643 $listoflang = array();
644 $foundnull = 0;
645 foreach (explode(',', $value) as $tmpvalue) {
646 if ($tmpvalue == 'null') {
647 $foundnull++;
648 continue;
649 }
650 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
651 }
652 $sql_extrawhere = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
653 if ($foundnull) {
654 $sql_extrawhere = "(".$sql_extrawhere." OR ".$this->db->sanitize($key)." IS NULL)";
655 }
656 $sqlwhere[] = $sql_extrawhere;
657 } else {
658 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
659 }
660 }
661 }
662 if (count($sqlwhere) > 0) {
663 if (!empty($websiteid)) {
664 $sql .= " AND (".implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere).')';
665 } else {
666 $sql .= " WHERE ".implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere);
667 }
668 }
669
670 $filter = '';
671 }
672
673 $errormessage = '';
674 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
675 if ($errormessage) {
676 $this->errors[] = $errormessage;
677 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
678 return -1;
679 }
680
681 $resql = $this->db->query($sql);
682 if ($resql) {
683 $obj = $this->db->fetch_object($resql);
684 if ($obj) {
685 $result = $obj->nb;
686 }
687
688 $this->db->free($resql);
689
690 return $result;
691 } else {
692 $this->error = 'Error '.$this->db->lasterror();
693 $this->errors[] = $this->error;
694 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
695
696 return -1;
697 }
698 }
699
707 public function update(User $user, $notrigger = 0)
708 {
709 $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
710 $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
711 if ($this->aliasalt) {
712 $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
713 }
714
715 $this->pageurl = preg_replace('/[^a-z0-9\-\_]/i', '', $this->pageurl);
716 $this->pageurl = preg_replace('/\-\-+/', '-', $this->pageurl);
717 $this->pageurl = preg_replace('/^\-/', '', $this->pageurl);
718
719 // Remove spaces and be sure we have main language only
720 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
721
722 if ($this->fk_page > 0) {
723 if (empty($this->lang)) {
724 $this->error = "ErrorLanguageMandatoryIfPageSetAsTranslationOfAnother";
725 return -1;
726 }
727 $tmppage = new WebsitePage($this->db);
728 $tmppage->fetch((int) $this->fk_page);
729 if ($tmppage->lang == $this->lang) {
730 $this->error = "ErrorLanguageOfTranslatedPageIsSameThanThisPage";
731 return -1;
732 }
733 }
734
735 // Check there is no PHP content into the modifiedhtmlheader (must be only HTML + JS)
736 // 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.
737 $phpcontent = dolKeepOnlyPhpCode($this->htmlheader);
738
739 if ($phpcontent) {
740 $this->error = 'Error: you try to create htmlheader with PHP content inside, this is not allowed.';
741 $this->errors[] = $this->error;
742 return -1;
743 }
744
745 return $this->updateCommon($user, $notrigger);
746 }
747
755 public function delete(User $user, $notrigger = 0)
756 {
757 global $conf;
758
759 $error = 0;
760
761 // Delete all child tables
762 if (!$error) {
763 foreach ($this->childtablesoncascade as $table) {
764 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
765 $sql .= " WHERE fk_website_page = ".(int) $this->id;
766
767 $result = $this->db->query($sql);
768 if (!$result) {
769 $error++;
770 $this->errors[] = $this->db->lasterror();
771 break;
772 }
773 }
774 }
775
776 if (!$error) {
777 $result = $this->deleteCommon($user, $notrigger);
778 if ($result <= 0) {
779 $error++;
780 }
781 }
782
783 if (!$error) {
784 $websiteobj = new Website($this->db);
785 $result = $websiteobj->fetch($this->fk_website);
786
787 if ($result > 0) {
788 global $dolibarr_main_data_root;
789 $pathofwebsite = $dolibarr_main_data_root.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.$websiteobj->ref;
790
791 $filealias = $pathofwebsite.'/'.$this->pageurl.'.php';
792 $filetpl = $pathofwebsite.'/page'.$this->id.'.tpl.php';
793
794 dol_delete_file($filealias);
795 dol_delete_file($filetpl);
796 } else {
797 $this->error = $websiteobj->error;
798 $this->errors = $websiteobj->errors;
799 }
800 }
801
802 if (!$error) {
803 return 1;
804 } else {
805 return -1;
806 }
807 }
808
822 public function createFromClone(User $user, $fromid, $newref, $newlang = '', $istranslation = 0, $newwebsite = 0, $newtitle = '', $website = null)
823 {
824 global $hookmanager, $langs;
825
826 $now = dol_now();
827 $error = 0;
828
829 dol_syslog(__METHOD__, LOG_DEBUG);
830
831 $object = new self($this->db);
832
833 // Clean parameters
834 if (empty($newref) && !empty($newtitle)) {
835 $newref = strtolower(dol_sanitizeFileName(preg_replace('/\s+/', '-', $newtitle), '-', 1));
836 }
837
838 // Check parameters
839 if (empty($newref)) {
840 $langs->load("errors");
841 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WEBSITE_TITLE"));
842 return -1;
843 }
844
845 if ($istranslation) {
846 if (is_null($website)) {
847 $website = new Website($this->db);
848 }
849 $website->fetch($object->fk_website);
850
851 if ($website->id != $newwebsite) {
852 $langs->load("errors");
853 $this->error = $langs->trans("WebsiteMustBeSameThanClonedPageIfTranslation");
854 return -1;
855 }
856 }
857
858 $this->db->begin();
859
860 // Load source object
861 $object->fetch($fromid);
862 // Reset object
863 $object->id = 0;
864
865 // Clear fields
866 $object->ref = $newref;
867 $object->pageurl = $newref;
868 $object->aliasalt = '';
869 $object->fk_user_creat = $user->id;
870 $object->author_alias = '';
871 $object->date_creation = $now;
872 $object->title = ($newtitle == '1' ? $object->title : ($newtitle ? $newtitle : $object->title));
873 $object->description = $object->title;
874 if (!empty($newlang)) {
875 $object->lang = $newlang;
876 }
877
878 if ($istranslation) {
879 if ($website->lang == $newlang) {
880 // The new page is into the website language, the parent page will be 0, and we must instead update the source page later.
881 $object->fk_page = 0;
882 } else {
883 $object->fk_page = $fromid;
884 }
885 } else {
886 $object->fk_page = 0;
887 }
888
889 if (!empty($newwebsite)) {
890 $object->fk_website = $newwebsite;
891 }
892 $object->import_key = '';
893 $object->status = self::STATUS_DRAFT;
894
895 // Create clone
896 $object->context['createfromclone'] = 'createfromclone';
897 $result = $object->create($user);
898 if ($result < 0) {
899 $error++;
901 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
902 }
903
904 if ($istranslation) {
905 if ($website->lang == $newlang) {
906 // We must now update the source page to link to the new page as a translation of.
907 $sql = "UPDATE ".MAIN_DB_PREFIX."website_page SET fk_page = ".((int) $result)." WHERE rowid = ".((int) $fromid);
908
909 $result = $this->db->query($sql);
910 if (!$result) {
911 $error++;
912 $this->error = $this->db->lasterror();
913 }
914 }
915 }
916
917 unset($object->context['createfromclone']);
918
919 // End
920 if (!$error) {
921 $this->db->commit();
922
923 return $object;
924 } else {
925 $this->db->rollback();
926
927 return -1;
928 }
929 }
930
942 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
943 {
944 global $langs, $conf, $db;
945 global $dolibarr_main_authentication, $dolibarr_main_demo;
946 global $menumanager;
947
948 $result = '';
949
950 $label = '<u>'.$langs->trans("Page").'</u>';
951 $label .= '<br>';
952 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
953 $label .= '<b>'.$langs->trans('ID').':</b> '.$this->id.'<br>';
954 $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
955 $label .= '<b>'.$langs->trans('Language').':</b> '.$this->lang;
956
957 $url = DOL_URL_ROOT.'/website/index.php?websiteid='.$this->fk_website.'&pageid='.$this->id;
958
959 $linkclose = '';
960 if (empty($notooltip)) {
961 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
962 $label = $langs->trans("ShowMyObject");
963 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
964 }
965 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
966 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
967 } else {
968 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
969 }
970
971 $linkstart = '<a href="'.$url.'"';
972 $linkstart .= $linkclose.'>';
973 $linkend = '</a>';
974
975 //$linkstart = $linkend = '';
976
977 $result .= $linkstart;
978 if ($withpicto) {
979 $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);
980 }
981 if ($withpicto != 2) {
982 $result .= $this->ref;
983 }
984 $result .= $linkend;
985
986 return $result;
987 }
988
995 public function getLibStatut($mode = 0)
996 {
997 return $this->LibStatut($this->status, $mode);
998 }
999
1000 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1008 public function LibStatut($status, $mode = 0)
1009 {
1010 // phpcs:enable
1011 global $langs;
1012
1013 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1014 global $langs;
1015 //$langs->load("mymodule");
1016 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
1017 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
1018 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
1019 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
1020 }
1021
1022 $statusType = 'status5';
1023 if ($status == self::STATUS_VALIDATED) {
1024 $statusType = 'status4';
1025 }
1026
1027 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1028 }
1029
1040 public function setCategories($categories)
1041 {
1042 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1043 return $this->setCategoriesCommon($categories, Categorie::TYPE_WEBSITE_PAGE);
1044 }
1045
1052 public function initAsSpecimen()
1053 {
1054 global $user;
1055
1056 $this->id = 0;
1057
1058 $now = dol_now();
1059
1060 $this->fk_website = 0;
1061 $this->type_container = 'page';
1062 $this->pageurl = 'specimen';
1063 $this->aliasalt = 'specimenalt';
1064 $this->title = 'My Page';
1065 $this->description = 'This is my page';
1066 $this->image = '';
1067 $this->keywords = 'keyword1, keyword2';
1068 $this->allowed_in_frames = 1;
1069 $this->htmlheader = '';
1070 $this->content = '<html><body>This is a html content</body></html>';
1071 $this->status = self::STATUS_DRAFT;
1072 $this->grabbed_from = '';
1073 $this->date_creation = $now - (24 * 30 * 3600);
1074 $this->date_modification = $now - (24 * 7 * 3600);
1075 $this->fk_user_creat = $user->id;
1076 $this->author_alias = 'mypublicpseudo';
1077
1078 return 1;
1079 }
1080}
$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
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.
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_sanitizeKeyCode($str)
Clean a string to use it as a key or code.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.