dolibarr 23.0.3
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-2025 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 // Test if page contains dynamic PHP content
288 if (!$user->hasRight('website', 'writephp')) {
289 // Check there is no PHP content into the imported file (must be only HTML + JS)
290 $phpcontent = dolKeepOnlyPhpCode($this->content);
291
292 if ($phpcontent) {
293 $this->error = 'Error: you try to create a page with PHP content in HTML body without having permissions for that.';
294 $this->errors[] = $this->error;
295 return -1;
296 }
297
298 // Check there is no PHP content into the imported file (must be only HTML + JS)
299 // Note: This one may be uselss because this->htmlheader should be retrieved now using GETPOST(..., 'restricthtmlallowlinkscript') so without PHP content. We keep it in case of.
300 $phpcontent = dolKeepOnlyPhpCode($this->htmlheader);
301
302 if ($phpcontent) {
303 $this->error = 'Error: you try to create a page with PHP content in HTML header 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 $stringtouse = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
511 if ($foundnull) {
512 $stringtouse = "(".$stringtouse." OR ".$this->db->sanitize($key)." IS NULL)";
513 }
514 $sqlwhere[] = $stringtouse;
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->escape($filtermode).' ', $sqlwhere).')';
523 } else {
524 $sql .= " WHERE ".implode(' '.$this->db->escape($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 $stringtouse = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
632 if ($foundnull) {
633 $stringtouse = "(".$stringtouse." OR ".$this->db->sanitize($key)." IS NULL)";
634 }
635 $sqlwhere[] = $stringtouse;
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->escape($filtermode).' ', $sqlwhere).')';
644 } else {
645 $sql .= " WHERE ".implode(' '.$this->db->escape($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 return $this->updateCommon($user, $notrigger);
715 }
716
724 public function delete(User $user, $notrigger = 0)
725 {
726 global $conf;
727
728 $error = 0;
729
730 // Delete all child tables
731 if (!$error) {
732 foreach ($this->childtablesoncascade as $table) {
733 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
734 $sql .= " WHERE fk_website_page = ".(int) $this->id;
735
736 $result = $this->db->query($sql);
737 if (!$result) {
738 $error++;
739 $this->errors[] = $this->db->lasterror();
740 break;
741 }
742 }
743 }
744
745 if (!$error) {
746 $result = $this->deleteCommon($user, $notrigger);
747 if ($result <= 0) {
748 $error++;
749 }
750 }
751
752 if (!$error) {
753 $websiteobj = new Website($this->db);
754 $result = $websiteobj->fetch($this->fk_website);
755
756 if ($result > 0) {
757 global $dolibarr_main_data_root;
758 $pathofwebsite = $dolibarr_main_data_root.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.$websiteobj->ref;
759
760 $filealias = $pathofwebsite.'/'.$this->pageurl.'.php';
761 $filetpl = $pathofwebsite.'/page'.$this->id.'.tpl.php';
762
763 dol_delete_file($filealias);
764 dol_delete_file($filetpl);
765 } else {
766 $this->error = $websiteobj->error;
767 $this->errors = $websiteobj->errors;
768 }
769 }
770
771 if (!$error) {
772 return 1;
773 } else {
774 return -1;
775 }
776 }
777
791 public function createFromClone(User $user, $fromid, $newref, $newlang = '', $istranslation = 0, $newwebsite = 0, $newtitle = '', $website = null)
792 {
793 global $hookmanager, $langs;
794
795 $now = dol_now();
796 $error = 0;
797
798 dol_syslog(__METHOD__, LOG_DEBUG);
799
800 $object = new self($this->db);
801
802 // Clean parameters
803 if (empty($newref) && !empty($newtitle)) {
804 $newref = strtolower(dol_sanitizeFileName(preg_replace('/\s+/', '-', $newtitle), '-', 1));
805 }
806
807 // Check parameters
808 if (empty($newref)) {
809 $langs->load("errors");
810 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WEBSITE_TITLE"));
811 return -1;
812 }
813
814 if ($istranslation) {
815 if (is_null($website)) {
816 $website = new Website($this->db);
817 }
818 $website->fetch($object->fk_website);
819
820 if ($website->id != $newwebsite) {
821 $langs->load("errors");
822 $this->error = $langs->trans("WebsiteMustBeSameThanClonedPageIfTranslation");
823 return -1;
824 }
825 }
826
827 $this->db->begin();
828
829 // Load source object
830 $object->fetch($fromid);
831 // Reset object
832 $object->id = 0;
833
834 // Clear fields
835 $object->ref = $newref;
836 $object->pageurl = $newref;
837 $object->aliasalt = '';
838 $object->fk_user_creat = $user->id;
839 $object->author_alias = '';
840 $object->date_creation = $now;
841 $object->title = ($newtitle == '1' ? $object->title : ($newtitle ? $newtitle : $object->title));
842 $object->description = $object->title;
843 if (!empty($newlang)) {
844 $object->lang = $newlang;
845 }
846
847 if ($istranslation) {
848 if ($website->lang == $newlang) {
849 // The new page is into the website language, the parent page will be 0, and we must instead update the source page later.
850 $object->fk_page = 0;
851 } else {
852 $object->fk_page = $fromid;
853 }
854 } else {
855 $object->fk_page = 0;
856 }
857
858 if (!empty($newwebsite)) {
859 $object->fk_website = $newwebsite;
860 }
861 $object->import_key = '';
862 $object->status = self::STATUS_DRAFT;
863
864 // Create clone
865 $object->context['createfromclone'] = 'createfromclone';
866 $result = $object->create($user);
867 if ($result < 0) {
868 $error++;
870 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
871 }
872
873 if ($istranslation) {
874 if ($website->lang == $newlang) {
875 // We must now update the source page to link to the new page as a translation of.
876 $sql = "UPDATE ".MAIN_DB_PREFIX."website_page SET fk_page = ".((int) $result)." WHERE rowid = ".((int) $fromid);
877
878 $result = $this->db->query($sql);
879 if (!$result) {
880 $error++;
881 $this->error = $this->db->lasterror();
882 }
883 }
884 }
885
886 unset($object->context['createfromclone']);
887
888 // End
889 if (!$error) {
890 $this->db->commit();
891
892 return $object;
893 } else {
894 $this->db->rollback();
895
896 return -1;
897 }
898 }
899
911 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
912 {
913 global $langs, $conf, $db;
914 global $dolibarr_main_authentication, $dolibarr_main_demo;
915 global $menumanager;
916
917 $result = '';
918
919 $label = '<u>'.$langs->trans("Page").'</u>';
920 $label .= '<br>';
921 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
922 $label .= '<b>'.$langs->trans('ID').':</b> '.$this->id.'<br>';
923 $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
924 $label .= '<b>'.$langs->trans('Language').':</b> '.$this->lang;
925
926 $url = DOL_URL_ROOT.'/website/index.php?websiteid='.$this->fk_website.'&pageid='.$this->id;
927
928 $linkclose = '';
929 if (empty($notooltip)) {
930 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
931 $label = $langs->trans("ShowMyObject");
932 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
933 }
934 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
935 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
936 } else {
937 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
938 }
939
940 $linkstart = '<a href="'.$url.'"';
941 $linkstart .= $linkclose.'>';
942 $linkend = '</a>';
943
944 //$linkstart = $linkend = '';
945
946 $result .= $linkstart;
947 if ($withpicto) {
948 $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);
949 }
950 if ($withpicto != 2) {
951 $result .= $this->ref;
952 }
953 $result .= $linkend;
954
955 return $result;
956 }
957
964 public function getLibStatut($mode = 0)
965 {
966 return $this->LibStatut($this->status, $mode);
967 }
968
969 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
977 public function LibStatut($status, $mode = 0)
978 {
979 // phpcs:enable
980 global $langs;
981
982 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
983 global $langs;
984 //$langs->load("mymodule");
985 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
986 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
987 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
988 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
989 }
990
991 $statusType = 'status5';
992 if ($status == self::STATUS_VALIDATED) {
993 $statusType = 'status4';
994 }
995
996 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
997 }
998
1009 public function setCategories($categories)
1010 {
1011 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1012 return $this->setCategoriesCommon($categories, Categorie::TYPE_WEBSITE_PAGE);
1013 }
1014
1021 public function initAsSpecimen()
1022 {
1023 global $user;
1024
1025 $this->id = 0;
1026
1027 $now = dol_now();
1028
1029 $this->fk_website = 0;
1030 $this->type_container = 'page';
1031 $this->pageurl = 'specimen';
1032 $this->aliasalt = 'specimenalt';
1033 $this->title = 'My Page';
1034 $this->description = 'This is my page';
1035 $this->image = '';
1036 $this->keywords = 'keyword1, keyword2';
1037 $this->allowed_in_frames = 1;
1038 $this->htmlheader = '';
1039 $this->content = '<html><body>This is a html content</body></html>';
1040 $this->status = self::STATUS_DRAFT;
1041 $this->grabbed_from = '';
1042 $this->date_creation = $now - (24 * 30 * 3600);
1043 $this->date_modification = $now - (24 * 7 * 3600);
1044 $this->fk_user_creat = $user->id;
1045 $this->author_alias = 'mypublicpseudo';
1046
1047 return 1;
1048 }
1049}
$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
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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:171
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.
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)
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)
forgeSQLFromUniversalSearchCriteria
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
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.