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