Completed
Branch develop (2c126c)
by Laurent
24:08
created

WebsitePage::delete()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2007-2012  Laurent Destailleur <[email protected]>
3
 * Copyright (C) 2014       Juanjo Menent       <[email protected]>
4
 * Copyright (C) 2015       Florian Henry       <[email protected]>
5
 * Copyright (C) 2015       Raphaël Doursenaud  <[email protected]>
6
 * Copyright (C) ---Put here your own copyright and developer email---
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
/**
23
 * \file    websites/websitepage.class.php
24
 * \ingroup websites
25
 * \brief   File for the CRUD class of websitepage (Create/Read/Update/Delete)
26
 */
27
28
// Put here all includes required by your class file
29
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
30
//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
31
//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
32
33
/**
34
 * Class Websitepage
35
 */
36
class WebsitePage extends CommonObject
37
{
38
	/**
39
	 * @var string Id to identify managed objects
40
	 */
41
	public $element = 'websitepage';
42
	/**
43
	 * @var string Name of table without prefix where object is stored
44
	 */
45
	public $table_element = 'website_page';
46
47
	/**
48
	 */
49
50
	public $fk_website;
51
	public $pageurl;
52
	public $title;
53
	public $description;
54
	public $keywords;
55
	public $htmlheader;
56
	public $content;
57
	public $status;
58
	public $date_creation;
59
	public $date_modification;
60
61
	// BEGIN MODULEBUILDER PROPERTIES
62
	/**
63
     * @var array  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
64
     */
65
	public $fields=array(
66
	    'rowid'         =>array('type'=>'integer',      'label'=>'TechnicalID',      'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'index'=>1, 'position'=>1,  'comment'=>'Id'),
67
		'pageurl'       =>array('type'=>'varchar(16)',  'label'=>'WEBSITE_PAGENAME', 'enabled'=>1, 'visible'=>1,  'notnull'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Alias of page'),
68
	    'title'         =>array('type'=>'varchar(255)', 'label'=>'Label',            'enabled'=>1, 'visible'=>1,  'position'=>30,  'searchall'=>1),
69
	    'description'   =>array('type'=>'varchar(255)', 'label'=>'Description',      'enabled'=>1, 'visible'=>1,  'position'=>30,  'searchall'=>1),
70
	    'keywords'      =>array('type'=>'varchar(255)', 'label'=>'Keywords',         'enabled'=>1, 'visible'=>1,  'position'=>45,  'searchall'=>0),
71
		'lang'          =>array('type'=>'varchar(6)',   'label'=>'Lang',             'enabled'=>1, 'visible'=>1,  'position'=>45,  'searchall'=>0),
72
		//'status'        =>array('type'=>'integer',      'label'=>'Status',           'enabled'=>1, 'visible'=>1,  'index'=>true,   'position'=>1000),
73
	    'fk_website'    =>array('type'=>'integer',      'label'=>'WebsiteId',        'enabled'=>1, 'visible'=>1,  'notnull'=>1, 'position'=>40,  'searchall'=>0, 'foreignkey'=>'websitepage.rowid'),
74
	    'fk_page'       =>array('type'=>'integer',      'label'=>'ParentPageId',     'enabled'=>1, 'visible'=>1,  'notnull'=>-1, 'position'=>45,  'searchall'=>0, 'foreignkey'=>'website.rowid'),
75
	    'htmlheader'    =>array('type'=>'text',         'label'=>'HtmlHeader',       'enabled'=>1, 'visible'=>0,  'position'=>50,  'searchall'=>0),
76
	    'content'       =>array('type'=>'mediumtext',   'label'=>'Content',          'enabled'=>1, 'visible'=>0,  'position'=>51,  'searchall'=>0),
77
		'grabbed_from'  =>array('type'=>'varchar(255)', 'label'=>'GrabbedFrom',      'enabled'=>1, 'visible'=>1,  'index'=>1,   'position'=>400, 'comment'=>'URL page content was grabbed from'),
78
	    'date_creation' =>array('type'=>'datetime',     'label'=>'DateCreation',     'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
79
		'tms'           =>array('type'=>'timestamp',    'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
80
		//'date_valid'    =>array('type'=>'datetime',     'label'=>'DateCreation',     'enabled'=>1, 'visible'=>-1, 'position'=>500),
81
		//'fk_user_creat' =>array('type'=>'integer',      'label'=>'UserAuthor',       'enabled'=>1, 'visible'=>-1, 'notnull'=>true, 'position'=>500),
82
		//'fk_user_modif' =>array('type'=>'integer',      'label'=>'UserModif',        'enabled'=>1, 'visible'=>-1, 'position'=>500),
83
		//'fk_user_valid' =>array('type'=>'integer',      'label'=>'UserValid',        'enabled'=>1, 'visible'=>-1, 'position'=>500),
84
		'import_key'    =>array('type'=>'varchar(14)',  'label'=>'ImportId',         'enabled'=>1, 'visible'=>-1,  'index'=>1,  'position'=>1000, 'notnull'=>-1),
85
	);
86
	// END MODULEBUILDER PROPERTIES
87
88
89
	/**
90
	 * Constructor
91
	 *
92
	 * @param DoliDb $db Database handler
93
	 */
94
	public function __construct(DoliDB $db)
95
	{
96
		$this->db = $db;
97
	}
98
99
	/**
100
	 * Create object into database
101
	 *
102
	 * @param  User $user      User that creates
103
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
104
	 * @return int             <0 if KO, Id of created object if OK
105
	 */
106
	public function create(User $user, $notrigger = false)
107
	{
108
		return $this->createCommon($user, $notrigger);
109
	}
110
111
	/**
112
	 * Load object in memory from the database
113
	 *
114
	 * @param int    $id           Id object. If this is 0, the default page of website_id will be used, if not defined, the first one found.
115
	 * @param string $website_id   Web site id (page name must also be filled if this parameter is used)
116
	 * @param string $page         Page name (website id must also be filled if this parameter is used)
117
	 *
118
	 * @return int <0 if KO, 0 if not found, >0 if OK
119
	 */
120
	public function fetch($id, $website_id = null, $page = null)
121
	{
122
		dol_syslog(__METHOD__, LOG_DEBUG);
123
124
		$sql = 'SELECT';
125
		$sql .= ' t.rowid,';
126
		$sql .= " t.fk_website,";
127
		$sql .= " t.pageurl,";
128
		$sql .= " t.title,";
129
		$sql .= " t.description,";
130
		$sql .= " t.keywords,";
131
		$sql .= " t.htmlheader,";
132
		$sql .= " t.content,";
133
		$sql .= " t.lang,";
134
		$sql .= " t.fk_page,";
135
		$sql .= " t.status,";
136
		$sql .= " t.grabbed_from,";
137
		$sql .= " t.date_creation,";
138
		$sql .= " t.tms as date_modification";
139
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
140
		//$sql .= ' WHERE entity IN ('.getEntity('website').')';       // entity is on website level
141
		$sql .= ' WHERE 1 = 1';
142
		if ($id > 0)
143
		{
144
			$sql .= ' AND t.rowid = ' . $id;
145
		}
146
		else
147
		{
148
			if (null !== $website_id) {
149
			    $sql .= " AND t.fk_website = '" . $this->db->escape($website_id) . "'";
150
			    if ($page) $sql .= " AND t.pageurl = '" . $this->db->escape($page) . "'";
151
			}
152
		}
153
        $sql .= $this->db->plimit(1);
154
155
		$resql = $this->db->query($sql);
156
		if ($resql) {
157
			$numrows = $this->db->num_rows($resql);
158
			if ($numrows) {
159
				$obj = $this->db->fetch_object($resql);
160
161
				$this->id = $obj->rowid;
162
163
				$this->fk_website = $obj->fk_website;
164
				$this->pageurl = $obj->pageurl;
165
				$this->title = $obj->title;
166
				$this->description = $obj->description;
167
				$this->keywords = $obj->keywords;
168
				$this->htmlheader = $obj->htmlheader;
169
				$this->content = $obj->content;
170
				$this->lang = $obj->lang;
171
				$this->fk_page = $obj->fk_page;
172
				$this->status = $obj->status;
173
				$this->grabbed_from = $obj->grabbed_from;
174
				$this->date_creation = $this->db->jdate($obj->date_creation);
175
				$this->date_modification = $this->db->jdate($obj->date_modification);
176
			}
177
			$this->db->free($resql);
178
179
			if ($numrows) {
180
				return 1;
181
			} else {
182
				return 0;
183
			}
184
		} else {
185
			$this->errors[] = 'Error ' . $this->db->lasterror();
186
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
187
188
			return - 1;
189
		}
190
	}
191
192
	/**
193
	 * Load object in memory from the database
194
	 *
195
	 * @param  string      $websiteid    Web site
196
	 * @param  string      $sortorder    Sort Order
197
	 * @param  string      $sortfield    Sort field
198
	 * @param  int         $limit        limit
199
	 * @param  int         $offset       Offset
200
	 * @param  array       $filter       Filter array
201
	 * @param  string      $filtermode   Filter mode (AND or OR)
202
	 * @return array|int                 int <0 if KO, array of pages if OK
203
	 */
204
	public function fetchAll($websiteid, $sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
205
	{
206
		dol_syslog(__METHOD__, LOG_DEBUG);
207
208
		$records=array();
209
210
		$sql = 'SELECT';
211
		$sql .= ' t.rowid,';
212
		$sql .= " t.fk_website,";
213
		$sql .= " t.pageurl,";
214
		$sql .= " t.title,";
215
		$sql .= " t.description,";
216
		$sql .= " t.keywords,";
217
		$sql .= " t.htmlheader,";
218
		$sql .= " t.content,";
219
		$sql .= " t.lang,";
220
		$sql .= " t.fk_page,";
221
		$sql .= " t.status,";
222
		$sql .= " t.grabbed_from,";
223
		$sql .= " t.date_creation,";
224
		$sql .= " t.tms as date_modification";
225
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
226
		$sql .= ' WHERE t.fk_website = '.$websiteid;
227
		// Manage filter
228
		$sqlwhere = array();
229
		if (count($filter) > 0) {
230
			foreach ($filter as $key => $value) {
231
				if ($key=='t.rowid' || $key=='t.fk_website') {
232
					$sqlwhere[] = $key . '='. $value;
233
				} else {
234
					$sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
235
				}
236
			}
237
		}
238
		if (count($sqlwhere) > 0) {
239
			$sql .= ' AND ' . implode(' '.$filtermode.' ', $sqlwhere);
240
		}
241
242
		if (!empty($sortfield)) {
243
			$sql .= $this->db->order($sortfield,$sortorder);
244
		}
245
		if (!empty($limit)) {
246
            $sql .=  ' ' . $this->db->plimit($limit, $offset);
247
		}
248
249
		$resql = $this->db->query($sql);
250
		if ($resql) {
251
			$num = $this->db->num_rows($resql);
252
253
			while ($obj = $this->db->fetch_object($resql))
254
			{
255
				$record = new self($this->db);
256
257
				$record->id = $obj->rowid;
258
				$record->fk_website = $obj->fk_website;
259
				$record->pageurl = $obj->pageurl;
260
				$record->title = $obj->title;
261
				$record->description = $obj->description;
262
				$record->keywords = $obj->keywords;
263
				$record->htmlheader = $obj->htmlheader;
264
				$record->content = $obj->content;
265
				$record->lang = $obj->lang;
266
				$record->fk_page = $obj->fk_page;
267
				$record->status = $obj->status;
268
				$record->grabbed_from = $obj->grabbed_from;
269
				$record->date_creation = $this->db->jdate($obj->date_creation);
270
				$record->date_modification = $this->db->jdate($obj->date_modification);
271
				//var_dump($record->id);
272
				$records[$record->id] = $record;
273
			}
274
			$this->db->free($resql);
275
276
			return $records;
277
		} else {
278
			$this->errors[] = 'Error ' . $this->db->lasterror();
279
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
280
281
			return -1;
282
		}
283
	}
284
285
	/**
286
	 * Update object into database
287
	 *
288
	 * @param  User $user      User that modifies
289
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
290
	 * @return int             <0 if KO, >0 if OK
291
	 */
292
	public function update(User $user, $notrigger = false)
293
	{
294
		return $this->updateCommon($user, $notrigger);
295
	}
296
297
	/**
298
	 * Delete object in database
299
	 *
300
	 * @param User $user       User that deletes
301
	 * @param bool $notrigger  false=launch triggers after, true=disable triggers
302
	 * @return int             <0 if KO, >0 if OK
303
	 */
304
	public function delete(User $user, $notrigger = false)
305
	{
306
		$result = $this->deleteCommon($user, $trigger);
0 ignored issues
show
Bug introduced by
The variable $trigger does not exist. Did you mean $notrigger?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
307
308
		if ($result > 0)
309
		{
310
			$websiteobj=new Website($this->db);
311
			$result = $websiteobj->fetch($this->fk_website);
312
313
			if ($result > 0)
314
			{
315
				global $dolibarr_main_data_root;
316
				$pathofwebsite=$dolibarr_main_data_root.'/websites/'.$websiteobj->ref;
317
318
				$filealias=$pathofwebsite.'/'.$this->pageurl.'.php';
319
				$filetpl=$pathofwebsite.'/page'.$this->id.'.tpl.php';
320
321
				dol_delete_file($filealias);
322
				dol_delete_file($filetpl);
323
			}
324
		}
325
326
		return $result;
327
	}
328
329
	/**
330
	 * Load an object from its id and create a new one in database
331
	 *
332
	 * @param	User	$user				User making the clone
333
	 * @param 	int 	$fromid 			Id of object to clone
334
	 * @param	string	$newref				New ref/alias of page
335
	 * @param	string	$newlang			New language
336
	 * @param	int		$istranslation		1=New page is a translation of the cloned page.
337
	 * @param	int		$newwebsite			0=Same web site, 1=New web site
338
	 * @return 	mixed 						New object created, <0 if KO
339
	 */
340
	public function createFromClone(User $user, $fromid, $newref, $newlang='', $istranslation=0, $newwebsite=0)
341
	{
342
		global $hookmanager, $langs;
343
		$error = 0;
344
345
		dol_syslog(__METHOD__, LOG_DEBUG);
346
347
		$object = new self($this->db);
348
349
		$this->db->begin();
350
351
		// Load source object
352
		$object->fetch($fromid);
353
		// Reset object
354
		$object->id = 0;
355
356
		// Clear fields
357
		$object->ref = $newref;
358
		$object->pageurl = $newref;
359
		$object->title = $langs->trans("CopyOf").' '.$object->title;
360
		if (! empty($newlang)) $object->lang=$newlang;
361
		if ($istranslation) $object->fk_page = $fromid;
362
		else $object->fk_page = 0;
363
		if (! empty($newwebsite)) $object->fk_website=$newwebsite;
364
365
		// Create clone
366
		$object->context['createfromclone'] = 'createfromclone';
367
		$result = $object->create($user);
368
		if ($result < 0) {
369
			$error++;
370
			$this->error = $object->error;
371
			$this->errors = $object->errors;
372
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
373
		}
374
375
		// End
376
		if (!$error) {
377
			$this->db->commit();
378
379
			return $object;
380
		} else {
381
			$this->db->rollback();
382
383
			return -1;
384
		}
385
	}
386
387
	/**
388
	 *  Return a link to the user card (with optionaly the picto)
389
	 * 	Use this->id,this->lastname, this->firstname
390
	 *
391
	 *	@param	int		$withpicto			Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
392
	 *	@param	string	$option				On what the link point to
393
     *  @param	integer	$notooltip			1=Disable tooltip
394
     *  @param	int		$maxlen				Max length of visible user name
395
     *  @param  string  $morecss            Add more css on link
396
	 *	@return	string						String with URL
397
	 */
398
	function getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
399
	{
400
		global $langs, $conf, $db;
401
        global $dolibarr_main_authentication, $dolibarr_main_demo;
402
        global $menumanager;
403
404
        $result = '';
405
        $companylink = '';
406
407
        $label = '<u>' . $langs->trans("Page") . '</u>';
408
        $label.= '<div width="100%">';
409
        $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
410
411
        $link = '<a href="'.DOL_URL_ROOT.'/websites/card.php?id='.$this->id.'"';
412
        $link.= ($notooltip?'':' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss?' '.$morecss:'').'"');
413
        $link.= '>';
414
		$linkend='</a>';
415
416
        if ($withpicto)
417
        {
418
            $result.=($link.img_object(($notooltip?'':$label), 'label', ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend);
419
            if ($withpicto != 2) $result.=' ';
420
		}
421
		$result.= $link . $this->ref . $linkend;
422
		return $result;
423
	}
424
425
	/**
426
	 *  Retourne le libelle du status d'un user (actif, inactif)
427
	 *
428
	 *  @param	int		$mode          0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
429
	 *  @return	string 			       Label of status
430
	 */
431
	function getLibStatut($mode=0)
432
	{
433
		return $this->LibStatut($this->status,$mode);
434
	}
435
436
	/**
437
	 *  Renvoi le libelle d'un status donne
438
	 *
439
	 *  @param	int		$status        	Id status
440
	 *  @param  int		$mode          	0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
441
	 *  @return string 			       	Label of status
442
	 */
443
	function LibStatut($status,$mode=0)
444
	{
445
		global $langs;
446
447
		if ($mode == 0)
448
		{
449
			$prefix='';
450
			if ($status == 1) return $langs->trans('Enabled');
451
			if ($status == 0) return $langs->trans('Disabled');
452
		}
453
		if ($mode == 1)
454
		{
455
			if ($status == 1) return $langs->trans('Enabled');
456
			if ($status == 0) return $langs->trans('Disabled');
457
		}
458
		if ($mode == 2)
459
		{
460
			if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
461
			if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
462
		}
463
		if ($mode == 3)
464
		{
465
			if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
466
			if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
467
		}
468
		if ($mode == 4)
469
		{
470
			if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
471
			if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
472
		}
473
		if ($mode == 5)
474
		{
475
			if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
476
			if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
477
		}
478
	}
479
480
481
	/**
482
	 * Initialise object with example values
483
	 * Id must be 0 if object instance is a specimen
484
	 *
485
	 * @return void
486
	 */
487
	public function initAsSpecimen()
488
	{
489
		$this->id = 0;
490
491
		$now=dol_now();
492
493
		$this->fk_website = '';
494
		$this->pageurl = '';
495
		$this->title = 'My Page';
496
		$this->description = 'This is my page';
497
		$this->keywords = 'keyword1, keyword2';
498
		$this->htmlheader = '';
499
		$this->content = '<html><body>This is a html content</body></html>';
500
		$this->status = '';
501
		$this->grabbed_from = '';
502
		$this->date_creation = $now - (24 * 30 * 3600);
503
		$this->date_modification = $now - (24 * 7 * 3600);
504
	}
505
506
}
507