-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
websitepage.class.php
559 lines (497 loc) · 19.9 KB
/
websitepage.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php
/* Copyright (C) 2007-2018 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file website/websitepage.class.php
* \ingroup website
* \brief File for the CRUD class of websitepage (Create/Read/Update/Delete)
*/
// Put here all includes required by your class file
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
/**
* Class Websitepage
*/
class WebsitePage extends CommonObject
{
/**
* @var string Id to identify managed objects
*/
public $element = 'websitepage';
/**
* @var string Name of table without prefix where object is stored
*/
public $table_element = 'website_page';
/**
* @var string String with name of icon for websitepage. Must be the part after the 'object_' into object_myobject.png
*/
public $picto = 'label';
public $fk_website;
public $pageurl;
public $aliasalt;
public $type_container;
public $title;
/**
* @var string description
*/
public $description;
public $keywords;
public $htmlheader;
public $content;
public $grabbed_from;
public $status;
public $date_creation;
public $date_modification;
// BEGIN MODULEBUILDER PROPERTIES
/**
* @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
*/
public $fields=array(
'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
'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'),
'aliasalt' =>array('type'=>'varchar(255)', 'label'=>'AliasAlt', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'index'=>0, 'position'=>11, 'searchall'=>0, 'comment'=>'Alias alternative of page'),
'type_container' =>array('type'=>'varchar(16)', 'label'=>'Type', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'index'=>0, 'position'=>12, 'comment'=>'Type of container'),
'title' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1),
'description' =>array('type'=>'varchar(255)', 'label'=>'Description', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1),
'keywords' =>array('type'=>'varchar(255)', 'label'=>'Keywords', 'enabled'=>1, 'visible'=>1, 'position'=>45, 'searchall'=>0),
'lang' =>array('type'=>'varchar(6)', 'label'=>'Lang', 'enabled'=>1, 'visible'=>1, 'position'=>45, 'searchall'=>0),
//'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'index'=>true, 'position'=>1000),
'fk_website' =>array('type'=>'integer', 'label'=>'WebsiteId', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>40, 'searchall'=>0, 'foreignkey'=>'websitepage.rowid'),
'fk_page' =>array('type'=>'integer', 'label'=>'ParentPageId', 'enabled'=>1, 'visible'=>1, 'notnull'=>-1, 'position'=>45, 'searchall'=>0, 'foreignkey'=>'website.rowid'),
'htmlheader' =>array('type'=>'text', 'label'=>'HtmlHeader', 'enabled'=>1, 'visible'=>0, 'position'=>50, 'searchall'=>0),
'content' =>array('type'=>'mediumtext', 'label'=>'Content', 'enabled'=>1, 'visible'=>0, 'position'=>51, 'searchall'=>0),
'grabbed_from' =>array('type'=>'varchar(255)', 'label'=>'GrabbedFrom', 'enabled'=>1, 'visible'=>1, 'index'=>1, 'position'=>400, 'comment'=>'URL page content was grabbed from'),
'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>501),
//'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>502),
'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'notnull'=>true, 'position'=>510),
'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-1, 'position'=>511),
//'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'index'=>1, 'position'=>1000, 'notnull'=>-1),
);
// END MODULEBUILDER PROPERTIES
/**
* Constructor
*
* @param DoliDb $db Database handler
*/
public function __construct(DoliDB $db)
{
$this->db = $db;
}
/**
* Create object into database
*
* @param User $user User that creates
* @param bool $notrigger false=launch triggers after, true=disable triggers
* @return int <0 if KO, Id of created object if OK
*/
public function create(User $user, $notrigger = false)
{
$this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
$this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
if ($this->aliasalt) $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
return $this->createCommon($user, $notrigger);
}
/**
* Load object in memory from the database
*
* @param int $id Id object.
* - If this is 0, the value into $page will be used. If not found or $page not defined, the default page of website_id will be used or the first page found if not set.
* - If value is < 0, we must exclude this ID.
* @param string $website_id Web site id (page name must also be filled if this parameter is used)
* @param string $page Page name (website id must also be filled if this parameter is used)
* @param string $aliasalt Alternative alias to search page (slow)
*
* @return int <0 if KO, 0 if not found, >0 if OK
*/
public function fetch($id, $website_id = null, $page = null, $aliasalt = null)
{
dol_syslog(__METHOD__, LOG_DEBUG);
$sql = 'SELECT';
$sql .= ' t.rowid,';
$sql .= " t.fk_website,";
$sql .= ' t.type_container,';
$sql .= " t.pageurl,";
$sql .= " t.aliasalt,";
$sql .= " t.title,";
$sql .= " t.description,";
$sql .= " t.keywords,";
$sql .= " t.htmlheader,";
$sql .= " t.content,";
$sql .= " t.lang,";
$sql .= " t.fk_page,";
$sql .= " t.status,";
$sql .= " t.grabbed_from,";
$sql .= " t.date_creation,";
$sql .= " t.tms as date_modification,";
$sql .= " t.fk_user_creat,";
$sql .= " t.fk_user_modif";
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
//$sql .= ' WHERE entity IN ('.getEntity('website').')'; // entity is on website level
$sql .= ' WHERE 1 = 1';
if ($id > 0)
{
$sql .= ' AND t.rowid = ' . $id;
}
else
{
if ($id < 0) $sql .= ' AND t.rowid <> ' . abs($id);
if (null !== $website_id) {
$sql .= " AND t.fk_website = '" . $this->db->escape($website_id) . "'";
if ($page) $sql .= " AND t.pageurl = '" . $this->db->escape($page) . "'";
if ($aliasalt) $sql .= " AND t.aliasalt LIKE '%," . $this->db->escape($aliasalt) . ",%'";
}
}
$sql .= $this->db->plimit(1);
$resql = $this->db->query($sql);
if ($resql) {
$numrows = $this->db->num_rows($resql);
if ($numrows) {
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->fk_website = $obj->fk_website;
$this->type_container = $obj->type_container;
$this->pageurl = $obj->pageurl;
$this->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
$this->title = $obj->title;
$this->description = $obj->description;
$this->keywords = $obj->keywords;
$this->htmlheader = $obj->htmlheader;
$this->content = $obj->content;
$this->lang = $obj->lang;
$this->fk_page = $obj->fk_page;
$this->status = $obj->status;
$this->grabbed_from = $obj->grabbed_from;
$this->date_creation = $this->db->jdate($obj->date_creation);
$this->date_modification = $this->db->jdate($obj->date_modification);
$this->fk_user_creat = $obj->fk_user_creat;
$this->fk_user_modif = $obj->fk_user_modif;
}
$this->db->free($resql);
if ($numrows) {
return 1;
} else {
return 0;
}
} else {
$this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
return - 1;
}
}
/**
* Load object in memory from the database
*
* @param string $websiteid Web site
* @param string $sortorder Sort Order
* @param string $sortfield Sort field
* @param int $limit limit
* @param int $offset Offset
* @param array $filter Filter array
* @param string $filtermode Filter mode (AND or OR)
* @return array|int int <0 if KO, array of pages if OK
*/
public function fetchAll($websiteid, $sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
{
dol_syslog(__METHOD__, LOG_DEBUG);
$records=array();
$sql = 'SELECT';
$sql .= ' t.rowid,';
$sql .= " t.fk_website,";
$sql .= " t.type_container,";
$sql .= " t.pageurl,";
$sql .= " t.aliasalt,";
$sql .= " t.title,";
$sql .= " t.description,";
$sql .= " t.keywords,";
$sql .= " t.htmlheader,";
$sql .= " t.content,";
$sql .= " t.lang,";
$sql .= " t.fk_page,";
$sql .= " t.status,";
$sql .= " t.grabbed_from,";
$sql .= " t.date_creation,";
$sql .= " t.tms as date_modification,";
$sql .= " t.fk_user_creat,";
$sql .= " t.fk_user_modif";
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
$sql .= ' WHERE t.fk_website = '.$websiteid;
// Manage filter
$sqlwhere = array();
if (count($filter) > 0) {
foreach ($filter as $key => $value) {
if ($key=='t.rowid' || $key=='t.fk_website') {
$sqlwhere[] = $key . '='. $value;
} else {
$sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
}
}
}
if (count($sqlwhere) > 0) {
$sql .= ' AND (' . implode(' '.$filtermode.' ', $sqlwhere).')';
}
if (!empty($sortfield)) {
$sql .= $this->db->order($sortfield, $sortorder);
}
if (!empty($limit)) {
$sql .= ' ' . $this->db->plimit($limit, $offset);
}
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
while ($obj = $this->db->fetch_object($resql))
{
$record = new self($this->db);
$record->id = $obj->rowid;
$record->fk_website = $obj->fk_website;
$record->type_container = $obj->type_container;
$record->pageurl = $obj->pageurl;
$record->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
$record->title = $obj->title;
$record->description = $obj->description;
$record->keywords = $obj->keywords;
$record->htmlheader = $obj->htmlheader;
$record->content = $obj->content;
$record->lang = $obj->lang;
$record->fk_page = $obj->fk_page;
$record->status = $obj->status;
$record->grabbed_from = $obj->grabbed_from;
$record->date_creation = $this->db->jdate($obj->date_creation);
$record->date_modification = $this->db->jdate($obj->date_modification);
$record->fk_user_creat = $obj->fk_user_creat;
$record->fk_user_modif = $obj->fk_user_modif;
//var_dump($record->id);
$records[$record->id] = $record;
}
$this->db->free($resql);
return $records;
} else {
$this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
return -1;
}
}
/**
* Update object into database
*
* @param User $user User that modifies
* @param bool $notrigger false=launch triggers after, true=disable triggers
* @return int <0 if KO, >0 if OK
*/
public function update(User $user, $notrigger = false)
{
$this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
$this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
if ($this->aliasalt) $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
return $this->updateCommon($user, $notrigger);
}
/**
* Delete object in database
*
* @param User $user User that deletes
* @param bool $notrigger false=launch triggers after, true=disable triggers
* @return int <0 if KO, >0 if OK
*/
public function delete(User $user, $notrigger = false)
{
$result = $this->deleteCommon($user, $trigger);
if ($result > 0)
{
$websiteobj=new Website($this->db);
$result = $websiteobj->fetch($this->fk_website);
if ($result > 0)
{
global $dolibarr_main_data_root;
$pathofwebsite=$dolibarr_main_data_root.'/website/'.$websiteobj->ref;
$filealias=$pathofwebsite.'/'.$this->pageurl.'.php';
$filetpl=$pathofwebsite.'/page'.$this->id.'.tpl.php';
dol_delete_file($filealias);
dol_delete_file($filetpl);
}
}
return $result;
}
/**
* Load an object from its id and create a new one in database
*
* @param User $user User making the clone
* @param int $fromid Id of object to clone
* @param string $newref New ref/alias of page
* @param string $newlang New language
* @param int $istranslation 1=New page is a translation of the cloned page.
* @param int $newwebsite 0=Same web site, >0=Id of new website
* @param int $keeptitleunchanged 1=Keep title unchanged
* @return mixed New object created, <0 if KO
*/
public function createFromClone(User $user, $fromid, $newref, $newlang='', $istranslation=0, $newwebsite=0, $keeptitleunchanged=0)
{
global $hookmanager, $langs;
$error = 0;
dol_syslog(__METHOD__, LOG_DEBUG);
$object = new self($this->db);
$this->db->begin();
// Load source object
$object->fetch($fromid);
// Reset object
$object->id = 0;
// Clear fields
$object->ref = $newref;
$object->pageurl = $newref;
$object->aliasalt = '';
$object->fk_user_creat = $user->id;
$object->title = ($keeptitleunchanged ? '' : $langs->trans("CopyOf").' ').$object->title;
if (! empty($newlang)) $object->lang=$newlang;
if ($istranslation) $object->fk_page = $fromid;
else $object->fk_page = 0;
if (! empty($newwebsite)) $object->fk_website=$newwebsite;
// Create clone
$object->context['createfromclone'] = 'createfromclone';
$result = $object->create($user);
if ($result < 0) {
$error++;
$this->error = $object->error;
$this->errors = $object->errors;
dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
}
// End
if (!$error) {
$this->db->commit();
return $object;
} else {
$this->db->rollback();
return -1;
}
}
/**
* Return a link to the user card (with optionaly the picto)
* Use this->id,this->lastname, this->firstname
*
* @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
* @param string $option On what the link point to
* @param integer $notooltip 1=Disable tooltip
* @param int $maxlen Max length of visible user name
* @param string $morecss Add more css on link
* @return string String with URL
*/
function getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
{
global $langs, $conf, $db;
global $dolibarr_main_authentication, $dolibarr_main_demo;
global $menumanager;
$result = '';
$companylink = '';
$label = '<u>' . $langs->trans("Page") . '</u>';
$label.= '<div width="100%">';
$label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
$linkstart = '<a href="'.DOL_URL_ROOT.'/website/card.php?id='.$this->id.'"';
$linkstart.= ($notooltip?'':' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss?' '.$morecss:'').'"');
$linkstart.= '>';
$linkend='</a>';
$linkstart = $linkend = '';
if ($withpicto)
{
$result.=($linkstart.img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?'':'class="classfortooltip"')).$linkend);
if ($withpicto != 2) $result.=' ';
}
$result.= $linkstart . $this->ref . $linkend;
return $result;
}
/**
* Retourne le libelle du status d'un user (actif, inactif)
*
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* @return string Label of status
*/
function getLibStatut($mode=0)
{
return $this->LibStatut($this->status,$mode);
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
/**
* Renvoi le libelle d'un status donne
*
* @param int $status Id status
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* @return string Label of status
*/
function LibStatut($status,$mode=0)
{
// phpcs:enable
global $langs;
if ($mode == 0)
{
$prefix='';
if ($status == 1) return $langs->trans('Enabled');
if ($status == 0) return $langs->trans('Disabled');
}
elseif ($mode == 1)
{
if ($status == 1) return $langs->trans('Enabled');
if ($status == 0) return $langs->trans('Disabled');
}
elseif ($mode == 2)
{
if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
}
elseif ($mode == 3)
{
if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
}
elseif ($mode == 4)
{
if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
}
elseif ($mode == 5)
{
if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
}
}
/**
* Initialise object with example values
* Id must be 0 if object instance is a specimen
*
* @return void
*/
public function initAsSpecimen()
{
global $user;
$this->id = 0;
$now=dol_now();
$this->fk_website = '';
$this->type_container = 'page';
$this->pageurl = 'specimen';
$this->aliasalt = 'specimenalt';
$this->title = 'My Page';
$this->description = 'This is my page';
$this->keywords = 'keyword1, keyword2';
$this->htmlheader = '';
$this->content = '<html><body>This is a html content</body></html>';
$this->status = '';
$this->grabbed_from = '';
$this->date_creation = $now - (24 * 30 * 3600);
$this->date_modification = $now - (24 * 7 * 3600);
$this->fk_user_creat = $user->id;
}
}