dolibarr 22.0.5
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 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
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 $disable_waf = 'NOSCANAUDIOFORINJECTION,NOSCANIFRAMEFORINJECTION,NOSCANOBJECTFORINJECTION'; // TODO Manage field in page setup
124
128 public $htmlheader;
129
133 public $content;
134
138 public $grabbed_from;
139
143 public $status;
144
148 public $fk_user_creat;
149
153 public $fk_user_modif;
154
158 public $author_alias;
159
163 public $object_type;
164
168 public $fk_object;
169
173 public $newid;
174
175
176 const STATUS_DRAFT = 0; // offline
177 const STATUS_VALIDATED = 1; // online
178
179
204 // BEGIN MODULEBUILDER PROPERTIES
208 public $fields = array(
209 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id'),
210 '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'),
211 'aliasalt' => array('type' => 'varchar(255)', 'label' => 'AliasAlt', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'index' => 0, 'position' => 11, 'searchall' => 0, 'comment' => 'Alias alternative of page'),
212 'type_container' => array('type' => 'varchar(16)', 'label' => 'Type', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'index' => 0, 'position' => 12, 'comment' => 'Type of container'),
213 'title' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'searchall' => 1, 'help' => 'UseTextBetween5And70Chars'),
214 'description' => array('type' => 'varchar(255)', 'label' => 'Description', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'searchall' => 1),
215 '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"'),
216 'keywords' => array('type' => 'varchar(255)', 'label' => 'Keywords', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'searchall' => 0),
217 'lang' => array('type' => 'varchar(6)', 'label' => 'Lang', 'enabled' => 1, 'notnull' => -1, 'visible' => 1, 'position' => 45, 'searchall' => 0),
218 //'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'index'=>true, 'position'=>1000),
219 'fk_website' => array('type' => 'integer', 'label' => 'WebsiteId', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 40, 'searchall' => 0, 'foreignkey' => 'websitepage.rowid'),
220 'fk_page' => array('type' => 'integer', 'label' => 'ParentPageId', 'enabled' => 1, 'visible' => 1, 'notnull' => -1, 'position' => 45, 'searchall' => 0, 'foreignkey' => 'website.rowid'),
221 'allowed_in_frames' => array('type' => 'integer', 'label' => 'AllowedInFrames', 'enabled' => 1, 'visible' => -1, 'position' => 48, 'searchall' => 0, 'default' => '0'),
222 'htmlheader' => array('type' => 'html', 'label' => 'HtmlHeader', 'enabled' => 1, 'visible' => 0, 'position' => 50, 'searchall' => 0),
223 'content' => array('type' => 'mediumtext', 'label' => 'Content', 'enabled' => 1, 'visible' => 0, 'position' => 51, 'searchall' => 0),
224 'grabbed_from' => array('type' => 'varchar(255)', 'label' => 'GrabbedFrom', 'enabled' => 1, 'visible' => 1, 'index' => 1, 'position' => 400, 'comment' => 'URL page content was grabbed from'),
225 'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 500),
226 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 501),
227 //'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>502),
228 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 510),
229 'author_alias' => array('type' => 'varchar(64)', 'label' => 'AuthorAlias', 'enabled' => 1, 'visible' => -1, 'index' => 0, 'position' => 511, 'comment' => 'Author alias'),
230 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -1, 'position' => 512),
231 //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
232 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'index' => 1, 'position' => 1000, 'notnull' => -1),
233 'object_type' => array('type' => 'varchar(255)', 'label' => 'ObjectType', 'enabled' => 1, 'visible' => 0, 'position' => 46, 'searchall' => 0, 'help' => ''),
234 'fk_object' => array('type' => 'varchar(255)', 'label' => 'ObjectId', 'enabled' => 1, 'visible' => 0, 'position' => 47, 'searchall' => 0, 'help' => ''),
235 'status' => array('type' => 'integer', 'label' => 'Status', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 510),
236 );
237 // END MODULEBUILDER PROPERTIES
238
239
245 public function __construct(DoliDB $db)
246 {
247 $this->db = $db;
248 }
249
257 public function create(User $user, $notrigger = 0)
258 {
259 $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
260 $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
261 if ($this->aliasalt) {
262 $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
263 }
264
265 $this->pageurl = preg_replace('/[^a-z0-9\-\_]/i', '', $this->pageurl);
266 $this->pageurl = preg_replace('/\-\-+/', '-', $this->pageurl);
267 $this->pageurl = preg_replace('/^\-/', '', $this->pageurl);
268
269 // Remove spaces and be sure we have main language only
270 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
271
272 // Test if page contains dynamic PHP content
273 if (!$user->hasRight('website', 'writephp')) {
274 // Check there is no PHP content into the imported file (must be only HTML + JS)
275 $phpcontent = dolKeepOnlyPhpCode($this->content);
276
277 if ($phpcontent) {
278 $this->error = 'Error: you try to create a page with PHP content without having permissions for that.';
279 $this->errors[] = $this->error;
280 return -1;
281 }
282 }
283
284 return $this->createCommon($user, $notrigger);
285 }
286
300 public function fetch($id, $website_id = null, $page = null, $aliasalt = null, $translationparentid = 0, $translationparentlang = '')
301 {
302 dol_syslog(__METHOD__, LOG_DEBUG);
303
304 $sql = 'SELECT';
305 $sql .= ' t.rowid,';
306 $sql .= " t.fk_website,";
307 $sql .= ' t.type_container,';
308 $sql .= " t.pageurl,";
309 $sql .= " t.aliasalt,";
310 $sql .= " t.title,";
311 $sql .= " t.description,";
312 $sql .= " t.image,";
313 $sql .= " t.keywords,";
314 $sql .= " t.htmlheader,";
315 $sql .= " t.content,";
316 $sql .= " t.lang,";
317 $sql .= " t.fk_page,"; // Translation parent page (in mani language)
318 $sql .= " t.allowed_in_frames,";
319 $sql .= " t.status,";
320 $sql .= " t.grabbed_from,";
321 $sql .= " t.date_creation,";
322 $sql .= " t.tms as date_modification,";
323 $sql .= " t.fk_user_creat,";
324 $sql .= " t.author_alias,";
325 $sql .= " t.fk_user_modif,";
326 $sql .= " t.import_key,";
327 $sql .= " t.object_type,";
328 $sql .= " t.fk_object";
329 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
330 //$sql .= ' WHERE entity IN ('.getEntity('website').')'; // entity is on website level
331 $sql .= ' WHERE 1 = 1';
332 if ($id > 0) {
333 $sql .= ' AND t.rowid = '.((int) $id);
334 } else {
335 if ($id < 0) {
336 $sql .= ' AND t.rowid <> '.((int) abs($id));
337 }
338 if (null !== $website_id) {
339 $sql .= " AND t.fk_website = '".$this->db->escape($website_id)."'";
340 if ($page) {
341 $pagetouse = $page;
342 $langtouse = '';
343 $tmppage = explode('/', $page);
344 if (!empty($tmppage[1])) {
345 $pagetouse = $tmppage[1];
346 if (strlen($tmppage[0])) {
347 $langtouse = $tmppage[0];
348 }
349 }
350 $sql .= " AND t.pageurl = '".$this->db->escape($pagetouse)."'";
351 if ($langtouse) {
352 $sql .= " AND t.lang = '".$this->db->escape($langtouse)."'";
353 }
354 }
355 if ($aliasalt) {
356 $sql .= " AND (t.aliasalt LIKE '%,".$this->db->escape($this->db->escapeforlike($aliasalt)).",%' OR t.aliasalt LIKE '%, ".$this->db->escape($this->db->escapeforlike($aliasalt)).",%')";
357 }
358 if ($translationparentid && $translationparentlang) {
359 $sql .= " AND t.fk_page = ".((int) $translationparentid);
360 $sql .= " AND t.lang = '".$this->db->escape($translationparentlang)."'";
361 }
362 }
363 }
364 $sql .= $this->db->plimit(1);
365
366 $resql = $this->db->query($sql);
367 if ($resql) {
368 $numrows = $this->db->num_rows($resql);
369 if ($numrows) {
370 $obj = $this->db->fetch_object($resql);
371
372 $this->id = $obj->rowid;
373
374 $this->fk_website = $obj->fk_website;
375 $this->type_container = $obj->type_container;
376
377 $this->pageurl = $obj->pageurl;
378 $this->ref = $obj->pageurl;
379 $this->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
380
381 $this->title = $obj->title;
382 $this->description = $obj->description;
383 $this->image = $obj->image;
384 $this->keywords = $obj->keywords;
385 $this->htmlheader = $obj->htmlheader;
386 $this->content = $obj->content;
387 $this->lang = $obj->lang;
388 $this->fk_page = $obj->fk_page;
389 $this->allowed_in_frames = $obj->allowed_in_frames;
390 $this->status = $obj->status;
391 $this->grabbed_from = $obj->grabbed_from;
392 $this->date_creation = $this->db->jdate($obj->date_creation);
393 $this->date_modification = $this->db->jdate($obj->date_modification);
394 $this->fk_user_creat = $obj->fk_user_creat;
395 $this->author_alias = $obj->author_alias;
396 $this->fk_user_modif = $obj->fk_user_modif;
397 $this->import_key = $obj->import_key;
398 $this->object_type = $obj->object_type;
399 $this->fk_object = $obj->fk_object;
400 }
401 $this->db->free($resql);
402
403 if ($numrows) {
404 return 1;
405 } else {
406 return 0;
407 }
408 } else {
409 $this->errors[] = 'Error '.$this->db->lasterror();
410 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
411
412 return -1;
413 }
414 }
415
429 public function fetchAll($websiteid = '', $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
430 {
431 dol_syslog(__METHOD__, LOG_DEBUG);
432
433 $records = array();
434
435 $sql = 'SELECT';
436 $sql .= ' t.rowid,';
437 $sql .= " t.fk_website,";
438 $sql .= " t.type_container,";
439 $sql .= " t.pageurl,";
440 $sql .= " t.aliasalt,";
441 $sql .= " t.title,";
442 $sql .= " t.description,";
443 $sql .= " t.image,";
444 $sql .= " t.keywords,";
445 $sql .= " t.htmlheader,";
446 $sql .= " t.content,";
447 $sql .= " t.lang,";
448 $sql .= " t.fk_page,";
449 $sql .= " t.allowed_in_frames,";
450 $sql .= " t.status,";
451 $sql .= " t.grabbed_from,";
452 $sql .= " t.date_creation,";
453 $sql .= " t.tms as date_modification,";
454 $sql .= " t.fk_user_creat,";
455 $sql .= " t.author_alias,";
456 $sql .= " t.fk_user_modif,";
457 $sql .= " t.import_key,";
458 $sql .= " t.object_type,";
459 $sql .= " t.fk_object";
460 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
461 if (!empty($websiteid)) {
462 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
463 }
464
465 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
466 if (is_array($filter)) {
467 $sqlwhere = array();
468
469 if (count($filter) > 0) {
470 foreach ($filter as $key => $value) {
471 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
472 $sqlwhere[] = $key." = ".((int) $value);
473 } elseif ($key == 'type_container' || $key == 't.type_container') {
474 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
475 } elseif ($key == 'lang' || $key == 't.lang') {
476 $listoflang = array();
477 $foundnull = 0;
478 foreach (explode(',', $value) as $tmpvalue) {
479 if ($tmpvalue == 'null') {
480 $foundnull++;
481 continue;
482 }
483 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
484 }
485 $stringtouse = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
486 if ($foundnull) {
487 $stringtouse = "(".$stringtouse." OR ".$this->db->sanitize($key)." IS NULL)";
488 }
489 $sqlwhere[] = $stringtouse;
490 } else {
491 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
492 }
493 }
494 }
495 if (count($sqlwhere) > 0) {
496 if (!empty($websiteid)) {
497 $sql .= " AND (".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
498 } else {
499 $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
500 }
501 }
502
503 $filter = '';
504 }
505
506 $errormessage = '';
507 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
508 if ($errormessage) {
509 $this->errors[] = $errormessage;
510 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
511 return -1;
512 }
513
514 if (!empty($sortfield)) {
515 $sql .= $this->db->order($sortfield, $sortorder);
516 }
517 if (!empty($limit)) {
518 $sql .= $this->db->plimit($limit, $offset);
519 }
520
521 $resql = $this->db->query($sql);
522 if ($resql) {
523 $num = $this->db->num_rows($resql);
524
525 while ($obj = $this->db->fetch_object($resql)) {
526 $record = new self($this->db);
527
528 $record->id = $obj->rowid;
529 $record->fk_website = $obj->fk_website;
530 $record->type_container = $obj->type_container;
531 $record->pageurl = $obj->pageurl;
532 $record->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
533 $record->title = $obj->title;
534 $record->description = $obj->description;
535 $record->image = $obj->image;
536 $record->keywords = $obj->keywords;
537 $record->htmlheader = $obj->htmlheader;
538 $record->content = $obj->content;
539 $record->lang = $obj->lang;
540 $record->fk_page = $obj->fk_page;
541 $record->allowed_in_frames = $obj->allowed_in_frames;
542 $record->status = $obj->status;
543 $record->grabbed_from = $obj->grabbed_from;
544 $record->date_creation = $this->db->jdate($obj->date_creation);
545 $record->date_modification = $this->db->jdate($obj->date_modification);
546 $record->fk_user_creat = $obj->fk_user_creat;
547 $record->author_alias = $obj->author_alias;
548 $record->fk_user_modif = $obj->fk_user_modif;
549 $record->import_key = $obj->import_key;
550 $record->object_type = $obj->object_type;
551 $record->fk_object = $obj->fk_object;
552 //var_dump($record->id);
553 $records[$record->id] = $record;
554 }
555 $this->db->free($resql);
556
557 return $records;
558 } else {
559 $this->error = 'Error '.$this->db->lasterror();
560 $this->errors[] = $this->error;
561 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
562
563 return -1;
564 }
565 }
566
576 public function countAll($websiteid, $filter = '', $filtermode = 'AND')
577 {
578 dol_syslog(__METHOD__, LOG_DEBUG);
579
580 $result = 0;
581
582 $sql = 'SELECT COUNT(t.rowid) as nb';
583 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
584 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
585
586 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
587 if (is_array($filter)) {
588 $sqlwhere = array();
589
590 if (count($filter) > 0) {
591 foreach ($filter as $key => $value) {
592 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
593 $sqlwhere[] = $key." = ".((int) $value);
594 } elseif ($key == 'type_container' || $key == 't.type_container') {
595 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
596 } elseif ($key == 'lang' || $key == 't.lang') {
597 $listoflang = array();
598 $foundnull = 0;
599 foreach (explode(',', $value) as $tmpvalue) {
600 if ($tmpvalue == 'null') {
601 $foundnull++;
602 continue;
603 }
604 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
605 }
606 $stringtouse = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
607 if ($foundnull) {
608 $stringtouse = "(".$stringtouse." OR ".$this->db->sanitize($key)." IS NULL)";
609 }
610 $sqlwhere[] = $stringtouse;
611 } else {
612 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
613 }
614 }
615 }
616 if (count($sqlwhere) > 0) {
617 if (!empty($websiteid)) {
618 $sql .= " AND (".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
619 } else {
620 $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
621 }
622 }
623
624 $filter = '';
625 }
626
627 $errormessage = '';
628 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
629 if ($errormessage) {
630 $this->errors[] = $errormessage;
631 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
632 return -1;
633 }
634
635 $resql = $this->db->query($sql);
636 if ($resql) {
637 $obj = $this->db->fetch_object($resql);
638 if ($obj) {
639 $result = $obj->nb;
640 }
641
642 $this->db->free($resql);
643
644 return $result;
645 } else {
646 $this->error = 'Error '.$this->db->lasterror();
647 $this->errors[] = $this->error;
648 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
649
650 return -1;
651 }
652 }
653
661 public function update(User $user, $notrigger = 0)
662 {
663 $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
664 $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
665 if ($this->aliasalt) {
666 $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
667 }
668
669 $this->pageurl = preg_replace('/[^a-z0-9\-\_]/i', '', $this->pageurl);
670 $this->pageurl = preg_replace('/\-\-+/', '-', $this->pageurl);
671 $this->pageurl = preg_replace('/^\-/', '', $this->pageurl);
672
673 // Remove spaces and be sure we have main language only
674 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
675
676 if ($this->fk_page > 0) {
677 if (empty($this->lang)) {
678 $this->error = "ErrorLanguageMandatoryIfPageSetAsTranslationOfAnother";
679 return -1;
680 }
681 $tmppage = new WebsitePage($this->db);
682 $tmppage->fetch((int) $this->fk_page);
683 if ($tmppage->lang == $this->lang) {
684 $this->error = "ErrorLanguageOfTranslatedPageIsSameThanThisPage";
685 return -1;
686 }
687 }
688
689 return $this->updateCommon($user, $notrigger);
690 }
691
699 public function delete(User $user, $notrigger = 0)
700 {
701 global $conf;
702
703 $error = 0;
704
705 // Delete all child tables
706 if (!$error) {
707 foreach ($this->childtablesoncascade as $table) {
708 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
709 $sql .= " WHERE fk_website_page = ".(int) $this->id;
710
711 $result = $this->db->query($sql);
712 if (!$result) {
713 $error++;
714 $this->errors[] = $this->db->lasterror();
715 break;
716 }
717 }
718 }
719
720 if (!$error) {
721 $result = $this->deleteCommon($user, $notrigger);
722 if ($result <= 0) {
723 $error++;
724 }
725 }
726
727 if (!$error) {
728 $websiteobj = new Website($this->db);
729 $result = $websiteobj->fetch($this->fk_website);
730
731 if ($result > 0) {
732 global $dolibarr_main_data_root;
733 $pathofwebsite = $dolibarr_main_data_root.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.$websiteobj->ref;
734
735 $filealias = $pathofwebsite.'/'.$this->pageurl.'.php';
736 $filetpl = $pathofwebsite.'/page'.$this->id.'.tpl.php';
737
738 dol_delete_file($filealias);
739 dol_delete_file($filetpl);
740 } else {
741 $this->error = $websiteobj->error;
742 $this->errors = $websiteobj->errors;
743 }
744 }
745
746 if (!$error) {
747 return 1;
748 } else {
749 return -1;
750 }
751 }
752
766 public function createFromClone(User $user, $fromid, $newref, $newlang = '', $istranslation = 0, $newwebsite = 0, $newtitle = '', $website = null)
767 {
768 global $hookmanager, $langs;
769
770 $now = dol_now();
771 $error = 0;
772
773 dol_syslog(__METHOD__, LOG_DEBUG);
774
775 $object = new self($this->db);
776
777 // Clean parameters
778 if (empty($newref) && !empty($newtitle)) {
779 $newref = strtolower(dol_sanitizeFileName(preg_replace('/\s+/', '-', $newtitle), '-', 1));
780 }
781
782 // Check parameters
783 if (empty($newref)) {
784 $langs->load("errors");
785 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WEBSITE_TITLE"));
786 return -1;
787 }
788
789 if ($istranslation) {
790 if (is_null($website)) {
791 $website = new Website($this->db);
792 }
793 $website->fetch($object->fk_website);
794
795 if ($website->id != $newwebsite) {
796 $langs->load("errors");
797 $this->error = $langs->trans("WebsiteMustBeSameThanClonedPageIfTranslation");
798 return -1;
799 }
800 }
801
802 $this->db->begin();
803
804 // Load source object
805 $object->fetch($fromid);
806 // Reset object
807 $object->id = 0;
808
809 // Clear fields
810 $object->ref = $newref;
811 $object->pageurl = $newref;
812 $object->aliasalt = '';
813 $object->fk_user_creat = $user->id;
814 $object->author_alias = '';
815 $object->date_creation = $now;
816 $object->title = ($newtitle == '1' ? $object->title : ($newtitle ? $newtitle : $object->title));
817 $object->description = $object->title;
818 if (!empty($newlang)) {
819 $object->lang = $newlang;
820 }
821
822 if ($istranslation) {
823 if ($website->lang == $newlang) {
824 // The new page is into the website language, the parent page will be 0, and we must instead update the source page later.
825 $object->fk_page = 0;
826 } else {
827 $object->fk_page = $fromid;
828 }
829 } else {
830 $object->fk_page = 0;
831 }
832
833 if (!empty($newwebsite)) {
834 $object->fk_website = $newwebsite;
835 }
836 $object->import_key = '';
837 $object->status = self::STATUS_DRAFT;
838
839 // Create clone
840 $object->context['createfromclone'] = 'createfromclone';
841 $result = $object->create($user);
842 if ($result < 0) {
843 $error++;
844 $this->error = $object->error;
845 $this->errors = $object->errors;
846 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
847 }
848
849 if ($istranslation) {
850 if ($website->lang == $newlang) {
851 // We must now update the source page to link to the new page as a translation of.
852 $sql = "UPDATE ".MAIN_DB_PREFIX."website_page SET fk_page = ".((int) $result)." WHERE rowid = ".((int) $fromid);
853
854 $result = $this->db->query($sql);
855 if (!$result) {
856 $error++;
857 $this->error = $this->db->lasterror();
858 }
859 }
860 }
861
862 unset($object->context['createfromclone']);
863
864 // End
865 if (!$error) {
866 $this->db->commit();
867
868 return $object;
869 } else {
870 $this->db->rollback();
871
872 return -1;
873 }
874 }
875
887 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
888 {
889 global $langs, $conf, $db;
890 global $dolibarr_main_authentication, $dolibarr_main_demo;
891 global $menumanager;
892
893 $result = '';
894
895 $label = '<u>'.$langs->trans("Page").'</u>';
896 $label .= '<br>';
897 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
898 $label .= '<b>'.$langs->trans('ID').':</b> '.$this->id.'<br>';
899 $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
900 $label .= '<b>'.$langs->trans('Language').':</b> '.$this->lang;
901
902 $url = DOL_URL_ROOT.'/website/index.php?websiteid='.$this->fk_website.'&pageid='.$this->id;
903
904 $linkclose = '';
905 if (empty($notooltip)) {
906 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
907 $label = $langs->trans("ShowMyObject");
908 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
909 }
910 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
911 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
912 } else {
913 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
914 }
915
916 $linkstart = '<a href="'.$url.'"';
917 $linkstart .= $linkclose.'>';
918 $linkend = '</a>';
919
920 //$linkstart = $linkend = '';
921
922 $result .= $linkstart;
923 if ($withpicto) {
924 $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);
925 }
926 if ($withpicto != 2) {
927 $result .= $this->ref;
928 }
929 $result .= $linkend;
930
931 return $result;
932 }
933
940 public function getLibStatut($mode = 0)
941 {
942 return $this->LibStatut($this->status, $mode);
943 }
944
945 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
953 public function LibStatut($status, $mode = 0)
954 {
955 // phpcs:enable
956 global $langs;
957
958 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
959 global $langs;
960 //$langs->load("mymodule");
961 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
962 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
963 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
964 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
965 }
966
967 $statusType = 'status5';
968 if ($status == self::STATUS_VALIDATED) {
969 $statusType = 'status4';
970 }
971
972 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
973 }
974
985 public function setCategories($categories)
986 {
987 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
988 return $this->setCategoriesCommon($categories, Categorie::TYPE_WEBSITE_PAGE);
989 }
990
997 public function initAsSpecimen()
998 {
999 global $user;
1000
1001 $this->id = 0;
1002
1003 $now = dol_now();
1004
1005 $this->fk_website = 0;
1006 $this->type_container = 'page';
1007 $this->pageurl = 'specimen';
1008 $this->aliasalt = 'specimenalt';
1009 $this->title = 'My Page';
1010 $this->description = 'This is my page';
1011 $this->image = '';
1012 $this->keywords = 'keyword1, keyword2';
1013 $this->allowed_in_frames = 1;
1014 $this->htmlheader = '';
1015 $this->content = '<html><body>This is a html content</body></html>';
1016 $this->status = self::STATUS_DRAFT;
1017 $this->grabbed_from = '';
1018 $this->date_creation = $now - (24 * 30 * 3600);
1019 $this->date_modification = $now - (24 * 7 * 3600);
1020 $this->fk_user_creat = $user->id;
1021 $this->author_alias = 'mypublicpseudo';
1022
1023 return 1;
1024 }
1025}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
$object ref
Definition info.php:90
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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.
Class Websitepage.
__construct(DoliDB $db)
Constructor.
fetch($id, $website_id=null, $page=null, $aliasalt=null, $translationparentid=0, $translationparentlang='')
Load object in memory from the database.
update(User $user, $notrigger=0)
Update object into database.
create(User $user, $notrigger=0)
Create object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
LibStatut($status, $mode=0)
Return the label of a given status.
createFromClone(User $user, $fromid, $newref, $newlang='', $istranslation=0, $newwebsite=0, $newtitle='', $website=null)
Load an object from its id and create a new one in database.
fetchAll($websiteid='', $sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Return array of all web site pages.
getLibStatut($mode=0)
Return the label of the status.
setCategories($categories)
Sets object to given categories.
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
Return a link to the user card (with optionally the picto) Use this->id,this->lastname,...
countAll($websiteid, $filter='', $filtermode='AND')
Count objects in the database.
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.
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)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
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.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
global $dolibarr_main_demo
Definition index.php:57