Completed
Push — master ( 18d185...d094ea )
by Justin
12:48 queued 09:31
created

Page::getAuthor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
class Page {
20
21
	protected $pageID = -1;
22
	protected $alias = null;
23
	protected $row = null;
24
	protected $pagetype = "";
25
26
	protected $author = null;
27
28
	//changed columns to save with save()
29
	protected $changes = array();
30
31
	public function __construct() {
32
		//
33
	}
34
35
	public function load ($alias = null) {
36
		if ($alias == null) {
37
			if (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) {
38
				$alias = Validator_String::get($_REQUEST['page']);
39
			} else {
40
				$alias = $this->getDomain()->getHomePage();
41
			}
42
		} else {
43
			//$alias = Database::getInstance()->escape($alias);
44
		}
45
46
		Events::throwEvent("get_alias", array(
47
			'alias' => &$alias,
48
			'page' => &$this,
49
			'domain' => $this->getDomain()
50
		));
51
52
		$this->alias = $alias;
53
54
		if (Cache::contains("pages", "page_" . $alias)) {
55
			$this->row = Cache::get("pages", "page_" . $alias);
56
		} else {
57
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias AND `activated` = '1'; ", array('alias' => $alias));
58
59
			if (!$row) {
60
				if (PHPUtils::strEqs("error404", $alias)) {
61
					throw new IllegalStateException("No page with alias 'error404' exists (requested alias: " . $alias . ").");
62
				}
63
64
				//page not found
65
				$new_alias = "error404";
66
67
				Events::throwEvent("load_page_error404", array(
68
					'alias' => &$new_alias,
69
					'original_alias' => $alias,
70
					'page' => &$this,
71
					'domain' => $this->getDomain()
72
				));
73
74
				$this->load($new_alias);
75
				return null;
76
			}
77
78
			$this->row = $row;
79
80
			//cache result
81
			Cache::put("pages", "page_" . $alias, $row);
82
		}
83
84
		//get pageID
85
		$this->pageID = $this->row['id'];
86
87
		//get name of page type (class name)
88
		$this->pagetype = $this->row['page_type'];
89
	}
90
91
	public function loadByID (int $pageID, bool $use_cache = true) {
92
		if ($use_cache && Cache::contains("pages", "pageID_" . $pageID)) {
93
			$this->row = Cache::get("pages", "pageID_" . $pageID);
94
		} else {
95
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `id` = :pageID; ", array('pageID' => $pageID));
96
97
			if (!$row) {
98
				throw new IllegalStateException("Page with pageID " . $pageID . " doesnt exists!");
99
			}
100
101
			$this->row = $row;
102
103
			//cache result
104
			Cache::put("pages", "pageID_" . $pageID, $row);
105
		}
106
107
		$this->pageID = $this->row['id'];
108
		$this->alias = $this->row['alias'];
109
110
		//get name of page type (class name)
111
		$this->pagetype = $this->row['page_type'];
112
	}
113
114
	protected function getDomain () : Domain {
115
		return Registry::singleton()->getObject("domain");
116
	}
117
118
	public function reloadCache () {
119
		Cache::clear("pages");
120
	}
121
122
	public function getPageID () : int {
123
		return $this->row['id'];
124
	}
125
126
	public function getAlias () : string {
127
		return $this->alias;
128
	}
129
130
	public function getPageType () : string {
131
		return $this->pagetype;
132
	}
133
134
	public function getTitle () : string {
135
		return $this->row['title'];
136
	}
137
138
	public function setTitle (string $title) {
139
		$this->row['title'] = $title;
140
		$this->changes[] = "title";
141
	}
142
143
	public function getContent () : string {
144
		return $this->row['content'];
145
	}
146
147
	public function setContent (string $content) {
148
		$this->row['content'] = $content;
149
		$this->changes[] = "content";
150
		$this->changes[] = "content";
151
	}
152
153
	public function getGlobalMenuID () : int {
154
		return $this->row['global_menu'];
155
	}
156
157
	public function getLocalMenuID () : int {
158
		return $this->row['local_menu'];
159
	}
160
161
	public function getStyle () : string {
162
		return $this->row['design'];
163
	}
164
165
	public function getFolder () : string {
166
		return $this->row['folder'];
167
	}
168
169
	public function getLastEdit () {
170
		return $this->row['lastUpdate'];
171
	}
172
173
	public function hasCustomTemplate () : bool {
174
		return $this->row['template'] !== "none";
175
	}
176
177
	public function getCustomTemplate () : string {
178
		return $this->row['template'];
179
	}
180
181
	public function hasCustomPermissions () : bool {
182
		return $this->row['can_see_permissions'] !== "none";
183
	}
184
185
	public function listCustomPermissions () : array {
186
		return explode("|", $this->row['can_see_permissions']);
187
	}
188
189
	public function isPublished () : bool {
190
		return $this->row['published'] == 1;
191
	}
192
193
	public function publish () {
194
		$this->row['published'] = 1;
195
		$this->changes[] = "published";
196
	}
197
198
	public function getContentType () : string {
199
		return $this->row['content_type'];
200
	}
201
202
	public function getParentID () : int {
203
		return $this->row['parent'];
204
	}
205
206
	public function getLeftSidebarID () : int {
207
		return $this->row['sidebar_left'];
208
	}
209
210
	public function getRightSidebarID () : int {
211
		return $this->row['sidebar_right'];
212
	}
213
214
	public function getMetaDescription () : string {
215
		return $this->row['meta_description'];
216
	}
217
218
	public function getMetaKeywords () : string {
219
		return $this->row['meta_keywords'];
220
	}
221
222
	public function getMetaRobotsOptions () : string {
223
		return $this->row['meta_robots'];
224
	}
225
226
	public function getMetaCanonicals () : string {
227
		return $this->row['meta_canonicals'];
228
	}
229
230
	public function getOgType () : string {
231
		return $this->row['og_type'];
232
	}
233
234
	public function getOgTitle () : string {
235
		return !empty($this->row['og_title']) ? $this->row['og_title'] : $this->getTitle();
236
	}
237
238
	public function getOgDescription () : string {
239
		return !empty($this->row['og_description']) ? $this->row['og_description'] : $this->getMetaDescription();
240
	}
241
242
	public function getOgImages () : array {
243
		if (empty($this->row['og_image'])) {
244
			return array();
245
		}
246
247
		return explode(",", $this->row['og_image']);
248
	}
249
250
	public function getAuthorID () : int {
251
		return $this->row['author'];
252
	}
253
254
	public function getAuthor () : User {
255
		if ($this->author == null) {
256
			//load author
257
			$this->author = new User();
258
259
			if ($this->getAuthorID() <= 0) {
260
				throw new IllegalArgumentException("authorID has to be > 0.");
261
			}
262
263
			$this->author->load($this->getAuthorID());
264
		}
265
266
		return $this->author;
267
	}
268
269
	public function activate (bool $bool = true) {
270
		$this->row['activated'] = ($bool ? 1 : 0);
271
	}
272
273
	public function isTrash () : bool {
274
		return $this->row['activated'] == 2;
275
	}
276
277
	public function isEditable () : bool {
278
		return $this->row['editable'] == 1;
279
	}
280
281
	public function isDeletable () : bool {
282
		return $this->row['deletable'] == 1;
283
	}
284
285
	public function isActivated () : bool {
286
		return $this->row['activated'] == 1;
287
	}
288
289
	public function moveToTrash () {
290
		self::movePageToTrash($this->pageID);
291
292
		//clear cache
293
		$this->clearCache();
294
	}
295
296
	/**
297
	 * restore page from trash
298
	 */
299
	public function restore () {
300
		self::restorePage($this->pageID);
301
302
		//clear cache
303
		$this->clearCache();
304
	}
305
306
	/**
307
	 * save changes into database
308
	 */
309
	public function save () {
310
		//TODO: add code here
311
	}
312
313
	public function clearCache () {
314
		if (!is_int($this->getPageID())) {
0 ignored issues
show
introduced by
The condition is_int($this->getPageID()) is always true.
Loading history...
315
			throw new IllegalStateException("pageID isn't set.");
316
		}
317
318
		//clear cache
319
		Cache::clear("pages", "pageID_" . $this->getPageID());
320
		Cache::clear("pages", "page_" . $this->getAlias());
321
	}
322
323
	public static function createIfAbsent (string $alias, string $title, string $page_type, string $content = "", string $folder = "/", int $globalMenu = -1, int $localMenu = -1, int $parentID = -1, bool $sitemap = true, bool $published = true, bool $editable = true, bool $deletable = true, string $author = "system") : int {
324
		//throw event
325
		Events::throwEvent("create_page", array(
326
			'alias' => &$alias,
327
			'title' => &$title,
328
			'page_type' => &$page_type,
329
			'content' => &$content,
330
			'folder' => &$folder,
331
			'global_menu' => &$globalMenu,
332
			'local_menu' => &$localMenu,
333
			'parentID' => &$parentID,
334
			'sitemap' => &$sitemap,
335
			'published' => &$published,
336
			'editable' => &$editable,
337
			'author' => &$author
338
		));
339
340
		if (!is_int($author)) {
0 ignored issues
show
introduced by
The condition is_int($author) is always false.
Loading history...
341
			//get userID of author
342
			$author = User::getIDByUsernameFromDB($author);
343
344
			if ($author == -1) {
345
				//username doesnt exists, so choose first user
346
				$author = 1;
347
			}
348
		} else {
349
			$author = (int) $author;
350
		}
351
352
		Database::getInstance()->execute("INSERT INTO `{praefix}pages` (
353
			`id`, `alias`, `title`, `content`, `parent`, `folder`, `global_menu`, `local_menu`, `page_type`, `design`, `sitemap`, `published`, `version`, `last_update`, `created`, `editable`, `deletable`, `author`, `activated`
354
		) VALUES (
355
			NULL, :alias, :title, :content, :parent, :folder, :globalMenu, :localMenu, :pageType, 'none', :sitemap, :published, '1', '0000-00-00 00:00:00', CURRENT_TIMESTAMP, :editable, :deletable, :author, '1'
356
		) ON DUPLICATE KEY UPDATE `alias` = :alias, `editable` = :editable, `deletable` = :deletable; ", array(
357
			'alias' => $alias,
358
			'title' => $title,
359
			'content' => $content,
360
			'parent' => $parentID,
361
			'folder' => $folder,
362
			'globalMenu' => $globalMenu,
363
			'localMenu' => $localMenu,
364
			'pageType' => $page_type,
365
			'sitemap' => ($sitemap ? 1 : 0),
366
			'published' => ($published ? 1 : 0),
367
			'editable' => ($editable ? 1 : 0),
368
			'deletable' => ($deletable ? 1 : 0),
369
			'author' => $author
370
		));
371
372
		Cache::clear("pages");
373
374
		//return page id
375
		$insertID = Database::getInstance()->lastInsertId();
376
377
		//throw event
378
		Events::throwEvent("created_page", array(
379
			'alias' => $alias,
380
			'title' => $title,
381
			'insertID' => $insertID
382
		));
383
384
		//get pageID by alias
385
		$pageID = Page::getPageIDByAlias($alias);
386
387
		//set default rights, allow page for administrators, registered users, guests and bots
388
		PageRights::setDefaultAllowedGroups($pageID, array(1, 2, 3, 4));
389
390
		return $pageID;
391
	}
392
393
	public static function delete (string $alias) {
394
		$delete = true;
395
396
		//plugins can avoid deletion or change alias
397
		Events::throwEvent("delete_page_alias", array(
398
			'alias' => &$alias,
399
			'delete' => &$delete
400
		));
401
402
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
403
			//remove page from database
404
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
405
406
			Cache::clear("pages");
407
		}
408
	}
409
410
	public static function deleteByID (int $id) {
411
		$delete = true;
412
413
		//plugins can avoid deletion or change alias
414
		Events::throwEvent("delete_page_id", array(
415
			'alias' => &$id,
416
			'delete' => &$delete
417
		));
418
419
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
420
			//remove page from database
421
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `id` = :id; ", array('id' => $id));
422
423
			Cache::clear("pages");
424
		}
425
	}
426
427
	public static function get (string $alias) : Page {
428
		$page = new Page();
429
		$page->load($alias);
430
431
		return $page;
432
	}
433
434
	public static function setPageType (string $alias, string $page_type) {
435
		Events::throwEvent("set_pagetype", array(
436
			'alias' => &$alias,
437
			'page_type' => &$page_type
438
		));
439
440
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `page_type` = :page_type WHERE `alias` = :alias; ", array(
441
			'alias' => $alias,
442
			'page_type' => $page_type
443
		));
444
445
		Cache::clear("pages");
446
	}
447
448
	/**
449
	 * get id of page by alias
450
	 *
451
	 * only use this method for database upgrade, because their is no caching for this method!
452
	 *
453
	 * @param string $alias alias of page
454
	 *
455
	 * @throws IllegalStateException if alias doesnt exists
456
	 *
457
	 * @return int pageID
458
	 */
459
	public static function getPageIDByAlias (string $alias) : int {
460
		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
461
462
		if (!$row) {
463
			throw new IllegalStateException("page with alias '" . $alias . "' doesnt exists.");
464
		}
465
466
		return $row['id'];
467
	}
468
469
	public static function lockPage (int $pageID, int $userID) {
470
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `locked_by` = :userID, `locked_timestamp` = CURRENT_TIMESTAMP WHERE `id` = :pageID; ", array(
471
			'userID' => $userID,
472
			'pageID' => $pageID
473
		));
474
475
		//clear cache
476
		Cache::clear("pages", "pageID_" . $pageID);
477
	}
478
479
	public static function unlockPage (int $pageID) {
480
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `locked_by` = '-1' WHERE `id` = :pageID; ", array(
481
			'pageID' => $pageID
482
		));
483
484
		//clear cache
485
		Cache::clear("pages", "pageID_" . $pageID);
486
	}
487
488
	protected static function movePageToTrash (int $pageID) {
489
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `activated` = 2 WHERE `id` = :pageID; ", array(
490
			'pageID' => $pageID
491
		));
492
493
		//clear cache
494
		Cache::clear("pages", "pageID_" . $pageID);
495
	}
496
497
	protected static function restorePage (int $pageID) {
498
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `activated` = 1 WHERE `id` = :pageID; ", array(
499
			'pageID' => $pageID
500
		));
501
502
		//clear cache
503
		Cache::clear("pages", "pageID_" . $pageID);
504
	}
505
506
	public static function exists (string $alias) : bool {
507
		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias; ", array(
508
			'alias' => $alias
509
		));
510
511
		return $row !== false;
512
	}
513
514
}
515
516
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
517