dolibarr 20.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 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
65
69 public $fk_website;
70
71 public $fk_page; // If translation of another page
72
73 public $pageurl;
74 public $aliasalt;
75 public $type_container;
76
80 public $title;
84 public $description;
88 public $image;
92 public $keywords;
96 public $lang;
97
98 public $allowed_in_frames;
99 public $htmlheader;
100 public $content;
101 public $grabbed_from;
102
106 public $status;
107
108 public $fk_user_creat;
109 public $fk_user_modif;
110
114 public $author_alias;
115
119 public $object_type;
120
124 public $fk_object;
125
129 public $newid;
130
131
132 const STATUS_DRAFT = 0; // offline
133 const STATUS_VALIDATED = 1; // online
134
135
160 // BEGIN MODULEBUILDER PROPERTIES
164 public $fields = array(
165 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id'),
166 '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'),
167 'aliasalt' => array('type' => 'varchar(255)', 'label' => 'AliasAlt', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'index' => 0, 'position' => 11, 'searchall' => 0, 'comment' => 'Alias alternative of page'),
168 'type_container' => array('type' => 'varchar(16)', 'label' => 'Type', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'index' => 0, 'position' => 12, 'comment' => 'Type of container'),
169 'title' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'searchall' => 1, 'help' => 'UseTextBetween5And70Chars'),
170 'description' => array('type' => 'varchar(255)', 'label' => 'Description', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'searchall' => 1),
171 '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"'),
172 'keywords' => array('type' => 'varchar(255)', 'label' => 'Keywords', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'searchall' => 0),
173 'lang' => array('type' => 'varchar(6)', 'label' => 'Lang', 'enabled' => 1, 'notnull' => -1, 'visible' => 1, 'position' => 45, 'searchall' => 0),
174 //'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'index'=>true, 'position'=>1000),
175 'fk_website' => array('type' => 'integer', 'label' => 'WebsiteId', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 40, 'searchall' => 0, 'foreignkey' => 'websitepage.rowid'),
176 'fk_page' => array('type' => 'integer', 'label' => 'ParentPageId', 'enabled' => 1, 'visible' => 1, 'notnull' => -1, 'position' => 45, 'searchall' => 0, 'foreignkey' => 'website.rowid'),
177 'allowed_in_frames' => array('type' => 'integer', 'label' => 'AllowedInFrames', 'enabled' => 1, 'visible' => -1, 'position' => 48, 'searchall' => 0, 'default' => '0'),
178 'htmlheader' => array('type' => 'html', 'label' => 'HtmlHeader', 'enabled' => 1, 'visible' => 0, 'position' => 50, 'searchall' => 0),
179 'content' => array('type' => 'mediumtext', 'label' => 'Content', 'enabled' => 1, 'visible' => 0, 'position' => 51, 'searchall' => 0),
180 'grabbed_from' => array('type' => 'varchar(255)', 'label' => 'GrabbedFrom', 'enabled' => 1, 'visible' => 1, 'index' => 1, 'position' => 400, 'comment' => 'URL page content was grabbed from'),
181 'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 500),
182 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 501),
183 //'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>502),
184 'fk_user_creat' => array('type' => 'integer', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'notnull' => true, 'position' => 510),
185 'author_alias' => array('type' => 'varchar(64)', 'label' => 'AuthorAlias', 'enabled' => 1, 'visible' => -1, 'index' => 0, 'position' => 511, 'comment' => 'Author alias'),
186 'fk_user_modif' => array('type' => 'integer', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -1, 'position' => 512),
187 //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
188 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'index' => 1, 'position' => 1000, 'notnull' => -1),
189 'object_type' => array('type' => 'varchar(255)', 'label' => 'ObjectType', 'enabled' => 1, 'visible' => 0, 'position' => 46, 'searchall' => 0, 'help' => ''),
190 'fk_object' => array('type' => 'varchar(255)', 'label' => 'ObjectId', 'enabled' => 1, 'visible' => 0, 'position' => 47, 'searchall' => 0, 'help' => '')
191 );
192 // END MODULEBUILDER PROPERTIES
193
194
200 public function __construct(DoliDB $db)
201 {
202 $this->db = $db;
203 }
204
212 public function create(User $user, $notrigger = 0)
213 {
214 $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
215 $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
216 if ($this->aliasalt) {
217 $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
218 }
219
220 $this->pageurl = preg_replace('/[^a-z0-9\-\_]/i', '', $this->pageurl);
221 $this->pageurl = preg_replace('/\-\-+/', '-', $this->pageurl);
222 $this->pageurl = preg_replace('/^\-/', '', $this->pageurl);
223
224 // Remove spaces and be sure we have main language only
225 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
226
227 // Test if page contains dynamic PHP content
228 if (!$user->hasRight('website', 'writephp')) {
229 // Check there is no PHP content into the imported file (must be only HTML + JS)
230 $phpcontent = dolKeepOnlyPhpCode($this->content);
231
232 if ($phpcontent) {
233 $this->error = 'Error: you try to create a page with PHP content without having permissions for that.';
234 $this->errors[] = $this->error;
235 return -1;
236 }
237 }
238
239 return $this->createCommon($user, $notrigger);
240 }
241
253 public function fetch($id, $website_id = null, $page = null, $aliasalt = null)
254 {
255 dol_syslog(__METHOD__, LOG_DEBUG);
256
257 $sql = 'SELECT';
258 $sql .= ' t.rowid,';
259 $sql .= " t.fk_website,";
260 $sql .= ' t.type_container,';
261 $sql .= " t.pageurl,";
262 $sql .= " t.aliasalt,";
263 $sql .= " t.title,";
264 $sql .= " t.description,";
265 $sql .= " t.image,";
266 $sql .= " t.keywords,";
267 $sql .= " t.htmlheader,";
268 $sql .= " t.content,";
269 $sql .= " t.lang,";
270 $sql .= " t.fk_page,";
271 $sql .= " t.allowed_in_frames,";
272 $sql .= " t.status,";
273 $sql .= " t.grabbed_from,";
274 $sql .= " t.date_creation,";
275 $sql .= " t.tms as date_modification,";
276 $sql .= " t.fk_user_creat,";
277 $sql .= " t.author_alias,";
278 $sql .= " t.fk_user_modif,";
279 $sql .= " t.import_key,";
280 $sql .= " t.object_type,";
281 $sql .= " t.fk_object";
282 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
283 //$sql .= ' WHERE entity IN ('.getEntity('website').')'; // entity is on website level
284 $sql .= ' WHERE 1 = 1';
285 if ($id > 0) {
286 $sql .= ' AND t.rowid = '.((int) $id);
287 } else {
288 if ($id < 0) {
289 $sql .= ' AND t.rowid <> '.abs($id);
290 }
291 if (null !== $website_id) {
292 $sql .= " AND t.fk_website = '".$this->db->escape($website_id)."'";
293 if ($page) {
294 $pagetouse = $page;
295 $langtouse = '';
296 $tmppage = explode('/', $page);
297 if (!empty($tmppage[1])) {
298 $pagetouse = $tmppage[1];
299 if (strlen($tmppage[0])) {
300 $langtouse = $tmppage[0];
301 }
302 }
303 $sql .= " AND t.pageurl = '".$this->db->escape($pagetouse)."'";
304 if ($langtouse) {
305 $sql .= " AND t.lang = '".$this->db->escape($langtouse)."'";
306 }
307 }
308 if ($aliasalt) {
309 $sql .= " AND (t.aliasalt LIKE '%,".$this->db->escape($aliasalt).",%' OR t.aliasalt LIKE '%, ".$this->db->escape($aliasalt).",%')";
310 }
311 }
312 }
313 $sql .= $this->db->plimit(1);
314
315 $resql = $this->db->query($sql);
316 if ($resql) {
317 $numrows = $this->db->num_rows($resql);
318 if ($numrows) {
319 $obj = $this->db->fetch_object($resql);
320
321 $this->id = $obj->rowid;
322
323 $this->fk_website = $obj->fk_website;
324 $this->type_container = $obj->type_container;
325
326 $this->pageurl = $obj->pageurl;
327 $this->ref = $obj->pageurl;
328 $this->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
329
330 $this->title = $obj->title;
331 $this->description = $obj->description;
332 $this->image = $obj->image;
333 $this->keywords = $obj->keywords;
334 $this->htmlheader = $obj->htmlheader;
335 $this->content = $obj->content;
336 $this->lang = $obj->lang;
337 $this->fk_page = $obj->fk_page;
338 $this->allowed_in_frames = $obj->allowed_in_frames;
339 $this->status = $obj->status;
340 $this->grabbed_from = $obj->grabbed_from;
341 $this->date_creation = $this->db->jdate($obj->date_creation);
342 $this->date_modification = $this->db->jdate($obj->date_modification);
343 $this->fk_user_creat = $obj->fk_user_creat;
344 $this->author_alias = $obj->author_alias;
345 $this->fk_user_modif = $obj->fk_user_modif;
346 $this->import_key = $obj->import_key;
347 $this->object_type = $obj->object_type;
348 $this->fk_object = $obj->fk_object;
349 }
350 $this->db->free($resql);
351
352 if ($numrows) {
353 return 1;
354 } else {
355 return 0;
356 }
357 } else {
358 $this->errors[] = 'Error '.$this->db->lasterror();
359 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
360
361 return -1;
362 }
363 }
364
378 public function fetchAll($websiteid = '', $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
379 {
380 dol_syslog(__METHOD__, LOG_DEBUG);
381
382 $records = array();
383
384 $sql = 'SELECT';
385 $sql .= ' t.rowid,';
386 $sql .= " t.fk_website,";
387 $sql .= " t.type_container,";
388 $sql .= " t.pageurl,";
389 $sql .= " t.aliasalt,";
390 $sql .= " t.title,";
391 $sql .= " t.description,";
392 $sql .= " t.image,";
393 $sql .= " t.keywords,";
394 $sql .= " t.htmlheader,";
395 $sql .= " t.content,";
396 $sql .= " t.lang,";
397 $sql .= " t.fk_page,";
398 $sql .= " t.allowed_in_frames,";
399 $sql .= " t.status,";
400 $sql .= " t.grabbed_from,";
401 $sql .= " t.date_creation,";
402 $sql .= " t.tms as date_modification,";
403 $sql .= " t.fk_user_creat,";
404 $sql .= " t.author_alias,";
405 $sql .= " t.fk_user_modif,";
406 $sql .= " t.import_key,";
407 $sql .= " t.object_type,";
408 $sql .= " t.fk_object";
409 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
410 if (!empty($websiteid)) {
411 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
412 }
413
414 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
415 if (is_array($filter)) {
416 $sqlwhere = array();
417
418 if (count($filter) > 0) {
419 foreach ($filter as $key => $value) {
420 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
421 $sqlwhere[] = $key." = ".((int) $value);
422 } elseif ($key == 'type_container' || $key == 't.type_container') {
423 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
424 } elseif ($key == 'lang' || $key == 't.lang') {
425 $listoflang = array();
426 $foundnull = 0;
427 foreach (explode(',', $value) as $tmpvalue) {
428 if ($tmpvalue == 'null') {
429 $foundnull++;
430 continue;
431 }
432 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
433 }
434 $stringtouse = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
435 if ($foundnull) {
436 $stringtouse = "(".$stringtouse." OR ".$this->db->sanitize($key)." IS NULL)";
437 }
438 $sqlwhere[] = $stringtouse;
439 } else {
440 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
441 }
442 }
443 }
444 if (count($sqlwhere) > 0) {
445 if (!empty($websiteid)) {
446 $sql .= " AND (".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
447 } else {
448 $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
449 }
450 }
451
452 $filter = '';
453 }
454
455 $errormessage = '';
456 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
457 if ($errormessage) {
458 $this->errors[] = $errormessage;
459 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
460 return -1;
461 }
462
463 if (!empty($sortfield)) {
464 $sql .= $this->db->order($sortfield, $sortorder);
465 }
466 if (!empty($limit)) {
467 $sql .= $this->db->plimit($limit, $offset);
468 }
469
470 $resql = $this->db->query($sql);
471 if ($resql) {
472 $num = $this->db->num_rows($resql);
473
474 while ($obj = $this->db->fetch_object($resql)) {
475 $record = new self($this->db);
476
477 $record->id = $obj->rowid;
478 $record->fk_website = $obj->fk_website;
479 $record->type_container = $obj->type_container;
480 $record->pageurl = $obj->pageurl;
481 $record->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
482 $record->title = $obj->title;
483 $record->description = $obj->description;
484 $record->image = $obj->image;
485 $record->keywords = $obj->keywords;
486 $record->htmlheader = $obj->htmlheader;
487 $record->content = $obj->content;
488 $record->lang = $obj->lang;
489 $record->fk_page = $obj->fk_page;
490 $record->allowed_in_frames = $obj->allowed_in_frames;
491 $record->status = $obj->status;
492 $record->grabbed_from = $obj->grabbed_from;
493 $record->date_creation = $this->db->jdate($obj->date_creation);
494 $record->date_modification = $this->db->jdate($obj->date_modification);
495 $record->fk_user_creat = $obj->fk_user_creat;
496 $record->author_alias = $obj->author_alias;
497 $record->fk_user_modif = $obj->fk_user_modif;
498 $record->import_key = $obj->import_key;
499 $record->object_type = $obj->object_type;
500 $record->fk_object = $obj->fk_object;
501 //var_dump($record->id);
502 $records[$record->id] = $record;
503 }
504 $this->db->free($resql);
505
506 return $records;
507 } else {
508 $this->error = 'Error '.$this->db->lasterror();
509 $this->errors[] = $this->error;
510 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
511
512 return -1;
513 }
514 }
515
525 public function countAll($websiteid, $filter = '', $filtermode = 'AND')
526 {
527 dol_syslog(__METHOD__, LOG_DEBUG);
528
529 $result = 0;
530
531 $sql = 'SELECT COUNT(t.rowid) as nb';
532 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
533 $sql .= ' WHERE t.fk_website = '.((int) $websiteid);
534
535 // Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
536 if (is_array($filter)) {
537 $sqlwhere = array();
538
539 if (count($filter) > 0) {
540 foreach ($filter as $key => $value) {
541 if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
542 $sqlwhere[] = $key." = ".((int) $value);
543 } elseif ($key == 'type_container' || $key == 't.type_container') {
544 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
545 } elseif ($key == 'lang' || $key == 't.lang') {
546 $listoflang = array();
547 $foundnull = 0;
548 foreach (explode(',', $value) as $tmpvalue) {
549 if ($tmpvalue == 'null') {
550 $foundnull++;
551 continue;
552 }
553 $listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
554 }
555 $stringtouse = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
556 if ($foundnull) {
557 $stringtouse = "(".$stringtouse." OR ".$this->db->sanitize($key)." IS NULL)";
558 }
559 $sqlwhere[] = $stringtouse;
560 } else {
561 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
562 }
563 }
564 }
565 if (count($sqlwhere) > 0) {
566 if (!empty($websiteid)) {
567 $sql .= " AND (".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
568 } else {
569 $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
570 }
571 }
572
573 $filter = '';
574 }
575
576 $errormessage = '';
577 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
578 if ($errormessage) {
579 $this->errors[] = $errormessage;
580 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
581 return -1;
582 }
583
584 $resql = $this->db->query($sql);
585 if ($resql) {
586 $obj = $this->db->fetch_object($resql);
587 if ($obj) {
588 $result = $obj->nb;
589 }
590
591 $this->db->free($resql);
592
593 return $result;
594 } else {
595 $this->error = 'Error '.$this->db->lasterror();
596 $this->errors[] = $this->error;
597 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
598
599 return -1;
600 }
601 }
602
610 public function update(User $user, $notrigger = 0)
611 {
612 $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
613 $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
614 if ($this->aliasalt) {
615 $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
616 }
617
618 $this->pageurl = preg_replace('/[^a-z0-9\-\_]/i', '', $this->pageurl);
619 $this->pageurl = preg_replace('/\-\-+/', '-', $this->pageurl);
620 $this->pageurl = preg_replace('/^\-/', '', $this->pageurl);
621
622 // Remove spaces and be sure we have main language only
623 $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
624
625 if ($this->fk_page > 0) {
626 if (empty($this->lang)) {
627 $this->error = "ErrorLanguageMandatoryIfPageSetAsTranslationOfAnother";
628 return -1;
629 }
630 $tmppage = new WebsitePage($this->db);
631 $tmppage->fetch($this->fk_page);
632 if ($tmppage->lang == $this->lang) {
633 $this->error = "ErrorLanguageOfTranslatedPageIsSameThanThisPage";
634 return -1;
635 }
636 }
637
638 return $this->updateCommon($user, $notrigger);
639 }
640
648 public function delete(User $user, $notrigger = 0)
649 {
650 global $conf;
651
652 $error = 0;
653
654 // Delete all child tables
655 if (!$error) {
656 foreach ($this->childtablesoncascade as $table) {
657 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
658 $sql .= " WHERE fk_website_page = ".(int) $this->id;
659
660 $result = $this->db->query($sql);
661 if (!$result) {
662 $error++;
663 $this->errors[] = $this->db->lasterror();
664 break;
665 }
666 }
667 }
668
669 if (!$error) {
670 $result = $this->deleteCommon($user, $notrigger);
671 if ($result <= 0) {
672 $error++;
673 }
674 }
675
676 if (!$error) {
677 $websiteobj = new Website($this->db);
678 $result = $websiteobj->fetch($this->fk_website);
679
680 if ($result > 0) {
681 global $dolibarr_main_data_root;
682 $pathofwebsite = $dolibarr_main_data_root.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/'.$websiteobj->ref;
683
684 $filealias = $pathofwebsite.'/'.$this->pageurl.'.php';
685 $filetpl = $pathofwebsite.'/page'.$this->id.'.tpl.php';
686
687 dol_delete_file($filealias);
688 dol_delete_file($filetpl);
689 } else {
690 $this->error = $websiteobj->error;
691 $this->errors = $websiteobj->errors;
692 }
693 }
694
695 if (!$error) {
696 return 1;
697 } else {
698 return -1;
699 }
700 }
701
715 public function createFromClone(User $user, $fromid, $newref, $newlang = '', $istranslation = 0, $newwebsite = 0, $newtitle = '', $website = null)
716 {
717 global $hookmanager, $langs;
718
719 $now = dol_now();
720 $error = 0;
721
722 dol_syslog(__METHOD__, LOG_DEBUG);
723
724 $object = new self($this->db);
725
726 // Clean parameters
727 if (empty($newref) && !empty($newtitle)) {
728 $newref = strtolower(dol_sanitizeFileName(preg_replace('/\s+/', '-', $newtitle), '-', 1));
729 }
730
731 // Check parameters
732 if (empty($newref)) {
733 $langs->load("errors");
734 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WEBSITE_TITLE"));
735 return -1;
736 }
737
738 if ($istranslation) {
739 if (is_null($website)) {
740 $website = new Website($this->db);
741 }
742 $website->fetch($object->fk_website);
743
744 if ($website->id != $newwebsite) {
745 $langs->load("errors");
746 $this->error = $langs->trans("WebsiteMustBeSameThanClonedPageIfTranslation");
747 return -1;
748 }
749 }
750
751 $this->db->begin();
752
753 // Load source object
754 $object->fetch($fromid);
755 // Reset object
756 $object->id = 0;
757
758 // Clear fields
759 $object->ref = $newref;
760 $object->pageurl = $newref;
761 $object->aliasalt = '';
762 $object->fk_user_creat = $user->id;
763 $object->author_alias = '';
764 $object->date_creation = $now;
765 $object->title = ($newtitle == '1' ? $object->title : ($newtitle ? $newtitle : $object->title));
766 $object->description = $object->title;
767 if (!empty($newlang)) {
768 $object->lang = $newlang;
769 }
770
771 if ($istranslation) {
772 if ($website->lang == $newlang) {
773 // The new page is into the website language, the parent page will be 0, and we must instead update the source page later.
774 $object->fk_page = 0;
775 } else {
776 $object->fk_page = $fromid;
777 }
778 } else {
779 $object->fk_page = 0;
780 }
781
782 if (!empty($newwebsite)) {
783 $object->fk_website = $newwebsite;
784 }
785 $object->import_key = '';
786 $object->status = self::STATUS_DRAFT;
787
788 // Create clone
789 $object->context['createfromclone'] = 'createfromclone';
790 $result = $object->create($user);
791 if ($result < 0) {
792 $error++;
793 $this->error = $object->error;
794 $this->errors = $object->errors;
795 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
796 }
797
798 if ($istranslation) {
799 if ($website->lang == $newlang) {
800 // We must now update the source page to link to the new page as a translation of.
801 $sql = "UPDATE ".MAIN_DB_PREFIX."website_page SET fk_page = ".((int) $result)." WHERE rowid = ".((int) $fromid);
802
803 $result = $this->db->query($sql);
804 if (!$result) {
805 $error++;
806 $this->error = $this->db->lasterror();
807 }
808 }
809 }
810
811 unset($object->context['createfromclone']);
812
813 // End
814 if (!$error) {
815 $this->db->commit();
816
817 return $object;
818 } else {
819 $this->db->rollback();
820
821 return -1;
822 }
823 }
824
836 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
837 {
838 global $langs, $conf, $db;
839 global $dolibarr_main_authentication, $dolibarr_main_demo;
840 global $menumanager;
841
842 $result = '';
843
844 $label = '<u>'.$langs->trans("Page").'</u>';
845 $label .= '<br>';
846 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
847 $label .= '<b>'.$langs->trans('ID').':</b> '.$this->id.'<br>';
848 $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
849 $label .= '<b>'.$langs->trans('Language').':</b> '.$this->lang;
850
851 $url = DOL_URL_ROOT.'/website/index.php?websiteid='.$this->fk_website.'&pageid='.$this->id;
852
853 $linkclose = '';
854 if (empty($notooltip)) {
855 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
856 $label = $langs->trans("ShowMyObject");
857 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
858 }
859 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
860 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
861 } else {
862 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
863 }
864
865 $linkstart = '<a href="'.$url.'"';
866 $linkstart .= $linkclose.'>';
867 $linkend = '</a>';
868
869 //$linkstart = $linkend = '';
870
871 $result .= $linkstart;
872 if ($withpicto) {
873 $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);
874 }
875 if ($withpicto != 2) {
876 $result .= $this->ref;
877 }
878 $result .= $linkend;
879
880 return $result;
881 }
882
889 public function getLibStatut($mode = 0)
890 {
891 return $this->LibStatut($this->status, $mode);
892 }
893
894 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
902 public function LibStatut($status, $mode = 0)
903 {
904 // phpcs:enable
905 global $langs;
906
907 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
908 global $langs;
909 //$langs->load("mymodule");
910 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
911 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
912 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Offline');
913 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Online');
914 }
915
916 $statusType = 'status5';
917 if ($status == self::STATUS_VALIDATED) {
918 $statusType = 'status4';
919 }
920
921 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
922 }
923
934 public function setCategories($categories)
935 {
936 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
937 return $this->setCategoriesCommon($categories, Categorie::TYPE_WEBSITE_PAGE);
938 }
939
946 public function initAsSpecimen()
947 {
948 global $user;
949
950 $this->id = 0;
951
952 $now = dol_now();
953
954 $this->fk_website = 0;
955 $this->type_container = 'page';
956 $this->pageurl = 'specimen';
957 $this->aliasalt = 'specimenalt';
958 $this->title = 'My Page';
959 $this->description = 'This is my page';
960 $this->image = '';
961 $this->keywords = 'keyword1, keyword2';
962 $this->allowed_in_frames = 1;
963 $this->htmlheader = '';
964 $this->content = '<html><body>This is a html content</body></html>';
965 $this->status = self::STATUS_DRAFT;
966 $this->grabbed_from = '';
967 $this->date_creation = $now - (24 * 30 * 3600);
968 $this->date_modification = $now - (24 * 7 * 3600);
969 $this->fk_user_creat = $user->id;
970 $this->author_alias = 'mypublicpseudo';
971
972 return 1;
973 }
974}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:637
$object ref
Definition info.php:79
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.
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.
fetch($id, $website_id=null, $page=null, $aliasalt=null)
Load object in memory from the database.
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 $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)
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.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dolKeepOnlyPhpCode($str)
Keep only PHP code part from a HTML string page.