dolibarr 21.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 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 return $this->createCommon($user, $notrigger);
268 }
269
283 public function fetch($id, $website_id = null, $page = null, $aliasalt = null, $translationparentid = 0, $translationparentlang = '')
284 {
285 dol_syslog(__METHOD__, LOG_DEBUG);
286
287 $sql = 'SELECT';
288 $sql .= ' t.rowid,';
289 $sql .= " t.fk_website,";
290 $sql .= ' t.type_container,';
291 $sql .= " t.pageurl,";
292 $sql .= " t.aliasalt,";
293 $sql .= " t.title,";
294 $sql .= " t.description,";
295 $sql .= " t.image,";
296 $sql .= " t.keywords,";
297 $sql .= " t.htmlheader,";
298 $sql .= " t.content,";
299 $sql .= " t.lang,";
300 $sql .= " t.fk_page,"; // Translation parent page (in mani language)
301 $sql .= " t.allowed_in_frames,";
302 $sql .= " t.status,";
303 $sql .= " t.grabbed_from,";
304 $sql .= " t.date_creation,";
305 $sql .= " t.tms as date_modification,";
306 $sql .= " t.fk_user_creat,";
307 $sql .= " t.author_alias,";
308 $sql .= " t.fk_user_modif,";
309 $sql .= " t.import_key,";
310 $sql .= " t.object_type,";
311 $sql .= " t.fk_object";
312 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
313 //$sql .= ' WHERE entity IN ('.getEntity('website').')'; // entity is on website level
314 $sql .= ' WHERE 1 = 1';
315 if ($id > 0) {
316 $sql .= ' AND t.rowid = '.((int) $id);
317 } else {
318 if ($id < 0) {
319 $sql .= ' AND t.rowid <> '.((int) abs($id));
320 }
321 if (null !== $website_id) {
322 $sql .= " AND t.fk_website = '".$this->db->escape($website_id)."'";
323 if ($page) {
324 $pagetouse = $page;
325 $langtouse = '';
326 $tmppage = explode('/', $page);
327 if (!empty($tmppage[1])) {
328 $pagetouse = $tmppage[1];
329 if (strlen($tmppage[0])) {
330 $langtouse = $tmppage[0];
331 }
332 }
333 $sql .= " AND t.pageurl = '".$this->db->escape($pagetouse)."'";
334 if ($langtouse) {
335 $sql .= " AND t.lang = '".$this->db->escape($langtouse)."'";
336 }
337 }
338 if ($aliasalt) {
339 $sql .= " AND (t.aliasalt LIKE '%,".$this->db->escape($this->db->escapeforlike($aliasalt)).",%' OR t.aliasalt LIKE '%, ".$this->db->escape($this->db->escapeforlike($aliasalt)).",%')";
340 }
341 if ($translationparentid && $translationparentlang) {
342 $sql .= " AND t.fk_page = ".((int) $translationparentid);
343 $sql .= " AND t.lang = '".$this->db->escape($translationparentlang)."'";
344 }
345 }
346 }
347 $sql .= $this->db->plimit(1);
348
349 $resql = $this->db->query($sql);
350 if ($resql) {
351 $numrows = $this->db->num_rows($resql);
352 if ($numrows) {
353 $obj = $this->db->fetch_object($resql);
354
355 $this->id = $obj->rowid;
356
357 $this->fk_website = $obj->fk_website;
358 $this->type_container = $obj->type_container;
359
360 $this->pageurl = $obj->pageurl;
361 $this->ref = $obj->pageurl;
362 $this->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
363
364 $this->title = $obj->title;
365 $this->description = $obj->description;
366 $this->image = $obj->image;
367 $this->keywords = $obj->keywords;
368 $this->htmlheader = $obj->htmlheader;
369 $this->content = $obj->content;
370 $this->lang = $obj->lang;
371 $this->fk_page = $obj->fk_page;
372 $this->allowed_in_frames = $obj->allowed_in_frames;
373 $this->status = $obj->status;
374 $this->grabbed_from = $obj->grabbed_from;
375 $this->date_creation = $this->db->jdate($obj->date_creation);
376 $this->date_modification = $this->db->jdate($obj->date_modification);
377 $this->fk_user_creat = $obj->fk_user_creat;
378 $this->author_alias = $obj->author_alias;
379 $this->fk_user_modif = $obj->fk_user_modif;
380 $this->import_key = $obj->import_key;
381 $this->object_type = $obj->object_type;
382 $this->fk_object = $obj->fk_object;
383 }
384 $this->db->free($resql);
385
386 if ($numrows) {
387 return 1;
388 } else {
389 return 0;
390 }
391 } else {
392 $this->errors[] = 'Error '.$this->db->lasterror();
393 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
394
395 return -1;
396 }
397 }
398
412 public function fetchAll($websiteid = '', $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
413 {
414 dol_syslog(__METHOD__, LOG_DEBUG);
415
416 $records = array();
417
418 $sql = 'SELECT';
419 $sql .= ' t.rowid,';
420 $sql .= " t.fk_website,";
421 $sql .= " t.type_container,";
422 $sql .= " t.pageurl,";
423 $sql .= " t.aliasalt,";
424 $sql .= " t.title,";
425 $sql .= " t.description,";
426 $sql .= " t.image,";
427 $sql .= " t.keywords,";
428 $sql .= " t.htmlheader,";
429 $sql .= " t.content,";
430 $sql .= " t.lang,";
431 $sql .= " t.fk_page,";
432 $sql .= " t.allowed_in_frames,";
433 $sql .= " t.status,";
434 $sql .= " t.grabbed_from,";
435 $sql .= " t.date_creation,";
436 $sql .= " t.tms as date_modification,";
437 $sql .= " t.fk_user_creat,";
438 $sql .= " t.author_alias,";
439 $sql .= " t.fk_user_modif,";
440 $sql .= " t.import_key,";
441 $sql .= " t.object_type,";
442 $sql .= " t.fk_object";
443 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
444 if (!empty($websiteid)) {
445 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
446 }
447
448 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
449 if (is_array($filter)) {
450 $sqlwhere = array();
451
452 if (count($filter) > 0) {
453 foreach ($filter as $key => $value) {
454 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
455 $sqlwhere[] = $key." = ".((int) $value);
456 } elseif ($key == 'type_container' || $key == 't.type_container') {
457 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
458 } elseif ($key == 'lang' || $key == 't.lang') {
459 $listoflang = array();
460 $foundnull = 0;
461 foreach (explode(',', $value) as $tmpvalue) {
462 if ($tmpvalue == 'null') {
463 $foundnull++;
464 continue;
465 }
466 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
467 }
468 $stringtouse = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
469 if ($foundnull) {
470 $stringtouse = "(".$stringtouse." OR ".$this->db->sanitize($key)." IS NULL)";
471 }
472 $sqlwhere[] = $stringtouse;
473 } else {
474 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
475 }
476 }
477 }
478 if (count($sqlwhere) > 0) {
479 if (!empty($websiteid)) {
480 $sql .= " AND (".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
481 } else {
482 $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
483 }
484 }
485
486 $filter = '';
487 }
488
489 $errormessage = '';
490 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
491 if ($errormessage) {
492 $this->errors[] = $errormessage;
493 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
494 return -1;
495 }
496
497 if (!empty($sortfield)) {
498 $sql .= $this->db->order($sortfield, $sortorder);
499 }
500 if (!empty($limit)) {
501 $sql .= $this->db->plimit($limit, $offset);
502 }
503
504 $resql = $this->db->query($sql);
505 if ($resql) {
506 $num = $this->db->num_rows($resql);
507
508 while ($obj = $this->db->fetch_object($resql)) {
509 $record = new self($this->db);
510
511 $record->id = $obj->rowid;
512 $record->fk_website = $obj->fk_website;
513 $record->type_container = $obj->type_container;
514 $record->pageurl = $obj->pageurl;
515 $record->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
516 $record->title = $obj->title;
517 $record->description = $obj->description;
518 $record->image = $obj->image;
519 $record->keywords = $obj->keywords;
520 $record->htmlheader = $obj->htmlheader;
521 $record->content = $obj->content;
522 $record->lang = $obj->lang;
523 $record->fk_page = $obj->fk_page;
524 $record->allowed_in_frames = $obj->allowed_in_frames;
525 $record->status = $obj->status;
526 $record->grabbed_from = $obj->grabbed_from;
527 $record->date_creation = $this->db->jdate($obj->date_creation);
528 $record->date_modification = $this->db->jdate($obj->date_modification);
529 $record->fk_user_creat = $obj->fk_user_creat;
530 $record->author_alias = $obj->author_alias;
531 $record->fk_user_modif = $obj->fk_user_modif;
532 $record->import_key = $obj->import_key;
533 $record->object_type = $obj->object_type;
534 $record->fk_object = $obj->fk_object;
535 //var_dump($record->id);
536 $records[$record->id] = $record;
537 }
538 $this->db->free($resql);
539
540 return $records;
541 } else {
542 $this->error = 'Error '.$this->db->lasterror();
543 $this->errors[] = $this->error;
544 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
545
546 return -1;
547 }
548 }
549
559 public function countAll($websiteid, $filter = '', $filtermode = 'AND')
560 {
561 dol_syslog(__METHOD__, LOG_DEBUG);
562
563 $result = 0;
564
565 $sql = 'SELECT COUNT(t.rowid) as nb';
566 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
567 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
568
569 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
570 if (is_array($filter)) {
571 $sqlwhere = array();
572
573 if (count($filter) > 0) {
574 foreach ($filter as $key => $value) {
575 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
576 $sqlwhere[] = $key." = ".((int) $value);
577 } elseif ($key == 'type_container' || $key == 't.type_container') {
578 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
579 } elseif ($key == 'lang' || $key == 't.lang') {
580 $listoflang = array();
581 $foundnull = 0;
582 foreach (explode(',', $value) as $tmpvalue) {
583 if ($tmpvalue == 'null') {
584 $foundnull++;
585 continue;
586 }
587 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
588 }
589 $stringtouse = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
590 if ($foundnull) {
591 $stringtouse = "(".$stringtouse." OR ".$this->db->sanitize($key)." IS NULL)";
592 }
593 $sqlwhere[] = $stringtouse;
594 } else {
595 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
596 }
597 }
598 }
599 if (count($sqlwhere) > 0) {
600 if (!empty($websiteid)) {
601 $sql .= " AND (".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
602 } else {
603 $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
604 }
605 }
606
607 $filter = '';
608 }
609
610 $errormessage = '';
611 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
612 if ($errormessage) {
613 $this->errors[] = $errormessage;
614 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
615 return -1;
616 }
617
618 $resql = $this->db->query($sql);
619 if ($resql) {
620 $obj = $this->db->fetch_object($resql);
621 if ($obj) {
622 $result = $obj->nb;
623 }
624
625 $this->db->free($resql);
626
627 return $result;
628 } else {
629 $this->error = 'Error '.$this->db->lasterror();
630 $this->errors[] = $this->error;
631 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
632
633 return -1;
634 }
635 }
636
644 public function update(User $user, $notrigger = 0)
645 {
646 $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
647 $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
648 if ($this->aliasalt) {
649 $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
650 }
651
652 $this->pageurl = preg_replace('/[^a-z0-9\-\_]/i', '', $this->pageurl);
653 $this->pageurl = preg_replace('/\-\-+/', '-', $this->pageurl);
654 $this->pageurl = preg_replace('/^\-/', '', $this->pageurl);
655
656 // Remove spaces and be sure we have main language only
657 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
658
659 if ($this->fk_page > 0) {
660 if (empty($this->lang)) {
661 $this->error = "ErrorLanguageMandatoryIfPageSetAsTranslationOfAnother";
662 return -1;
663 }
664 $tmppage = new WebsitePage($this->db);
665 $tmppage->fetch($this->fk_page);
666 if ($tmppage->lang == $this->lang) {
667 $this->error = "ErrorLanguageOfTranslatedPageIsSameThanThisPage";
668 return -1;
669 }
670 }
671
672 return $this->updateCommon($user, $notrigger);
673 }
674
682 public function delete(User $user, $notrigger = 0)
683 {
684 global $conf;
685
686 $error = 0;
687
688 // Delete all child tables
689 if (!$error) {
690 foreach ($this->childtablesoncascade as $table) {
691 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
692 $sql .= " WHERE fk_website_page = ".(int) $this->id;
693
694 $result = $this->db->query($sql);
695 if (!$result) {
696 $error++;
697 $this->errors[] = $this->db->lasterror();
698 break;
699 }
700 }
701 }
702
703 if (!$error) {
704 $result = $this->deleteCommon($user, $notrigger);
705 if ($result <= 0) {
706 $error++;
707 }
708 }
709
710 if (!$error) {
711 $websiteobj = new Website($this->db);
712 $result = $websiteobj->fetch($this->fk_website);
713
714 if ($result > 0) {
715 global $dolibarr_main_data_root;
716 $pathofwebsite = $dolibarr_main_data_root.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.$websiteobj->ref;
717
718 $filealias = $pathofwebsite.'/'.$this->pageurl.'.php';
719 $filetpl = $pathofwebsite.'/page'.$this->id.'.tpl.php';
720
721 dol_delete_file($filealias);
722 dol_delete_file($filetpl);
723 } else {
724 $this->error = $websiteobj->error;
725 $this->errors = $websiteobj->errors;
726 }
727 }
728
729 if (!$error) {
730 return 1;
731 } else {
732 return -1;
733 }
734 }
735
749 public function createFromClone(User $user, $fromid, $newref, $newlang = '', $istranslation = 0, $newwebsite = 0, $newtitle = '', $website = null)
750 {
751 global $hookmanager, $langs;
752
753 $now = dol_now();
754 $error = 0;
755
756 dol_syslog(__METHOD__, LOG_DEBUG);
757
758 $object = new self($this->db);
759
760 // Clean parameters
761 if (empty($newref) && !empty($newtitle)) {
762 $newref = strtolower(dol_sanitizeFileName(preg_replace('/\s+/', '-', $newtitle), '-', 1));
763 }
764
765 // Check parameters
766 if (empty($newref)) {
767 $langs->load("errors");
768 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WEBSITE_TITLE"));
769 return -1;
770 }
771
772 if ($istranslation) {
773 if (is_null($website)) {
774 $website = new Website($this->db);
775 }
776 $website->fetch($object->fk_website);
777
778 if ($website->id != $newwebsite) {
779 $langs->load("errors");
780 $this->error = $langs->trans("WebsiteMustBeSameThanClonedPageIfTranslation");
781 return -1;
782 }
783 }
784
785 $this->db->begin();
786
787 // Load source object
788 $object->fetch($fromid);
789 // Reset object
790 $object->id = 0;
791
792 // Clear fields
793 $object->ref = $newref;
794 $object->pageurl = $newref;
795 $object->aliasalt = '';
796 $object->fk_user_creat = $user->id;
797 $object->author_alias = '';
798 $object->date_creation = $now;
799 $object->title = ($newtitle == '1' ? $object->title : ($newtitle ? $newtitle : $object->title));
800 $object->description = $object->title;
801 if (!empty($newlang)) {
802 $object->lang = $newlang;
803 }
804
805 if ($istranslation) {
806 if ($website->lang == $newlang) {
807 // The new page is into the website language, the parent page will be 0, and we must instead update the source page later.
808 $object->fk_page = 0;
809 } else {
810 $object->fk_page = $fromid;
811 }
812 } else {
813 $object->fk_page = 0;
814 }
815
816 if (!empty($newwebsite)) {
817 $object->fk_website = $newwebsite;
818 }
819 $object->import_key = '';
820 $object->status = self::STATUS_DRAFT;
821
822 // Create clone
823 $object->context['createfromclone'] = 'createfromclone';
824 $result = $object->create($user);
825 if ($result < 0) {
826 $error++;
827 $this->error = $object->error;
828 $this->errors = $object->errors;
829 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
830 }
831
832 if ($istranslation) {
833 if ($website->lang == $newlang) {
834 // We must now update the source page to link to the new page as a translation of.
835 $sql = "UPDATE ".MAIN_DB_PREFIX."website_page SET fk_page = ".((int) $result)." WHERE rowid = ".((int) $fromid);
836
837 $result = $this->db->query($sql);
838 if (!$result) {
839 $error++;
840 $this->error = $this->db->lasterror();
841 }
842 }
843 }
844
845 unset($object->context['createfromclone']);
846
847 // End
848 if (!$error) {
849 $this->db->commit();
850
851 return $object;
852 } else {
853 $this->db->rollback();
854
855 return -1;
856 }
857 }
858
870 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
871 {
872 global $langs, $conf, $db;
873 global $dolibarr_main_authentication, $dolibarr_main_demo;
874 global $menumanager;
875
876 $result = '';
877
878 $label = '<u>'.$langs->trans("Page").'</u>';
879 $label .= '<br>';
880 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
881 $label .= '<b>'.$langs->trans('ID').':</b> '.$this->id.'<br>';
882 $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
883 $label .= '<b>'.$langs->trans('Language').':</b> '.$this->lang;
884
885 $url = DOL_URL_ROOT.'/website/index.php?websiteid='.$this->fk_website.'&pageid='.$this->id;
886
887 $linkclose = '';
888 if (empty($notooltip)) {
889 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
890 $label = $langs->trans("ShowMyObject");
891 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
892 }
893 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
894 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
895 } else {
896 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
897 }
898
899 $linkstart = '<a href="'.$url.'"';
900 $linkstart .= $linkclose.'>';
901 $linkend = '</a>';
902
903 //$linkstart = $linkend = '';
904
905 $result .= $linkstart;
906 if ($withpicto) {
907 $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);
908 }
909 if ($withpicto != 2) {
910 $result .= $this->ref;
911 }
912 $result .= $linkend;
913
914 return $result;
915 }
916
923 public function getLibStatut($mode = 0)
924 {
925 return $this->LibStatut($this->status, $mode);
926 }
927
928 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
936 public function LibStatut($status, $mode = 0)
937 {
938 // phpcs:enable
939 global $langs;
940
941 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
942 global $langs;
943 //$langs->load("mymodule");
944 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
945 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
946 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
947 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
948 }
949
950 $statusType = 'status5';
951 if ($status == self::STATUS_VALIDATED) {
952 $statusType = 'status4';
953 }
954
955 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
956 }
957
968 public function setCategories($categories)
969 {
970 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
971 return $this->setCategoriesCommon($categories, Categorie::TYPE_WEBSITE_PAGE);
972 }
973
980 public function initAsSpecimen()
981 {
982 global $user;
983
984 $this->id = 0;
985
986 $now = dol_now();
987
988 $this->fk_website = 0;
989 $this->type_container = 'page';
990 $this->pageurl = 'specimen';
991 $this->aliasalt = 'specimenalt';
992 $this->title = 'My Page';
993 $this->description = 'This is my page';
994 $this->image = '';
995 $this->keywords = 'keyword1, keyword2';
996 $this->allowed_in_frames = 1;
997 $this->htmlheader = '';
998 $this->content = '<html><body>This is a html content</body></html>';
999 $this->status = self::STATUS_DRAFT;
1000 $this->grabbed_from = '';
1001 $this->date_creation = $now - (24 * 30 * 3600);
1002 $this->date_modification = $now - (24 * 7 * 3600);
1003 $this->fk_user_creat = $user->id;
1004 $this->author_alias = 'mypublicpseudo';
1005
1006 return 1;
1007 }
1008}
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