Completed
Push — master ( 3d00b3...2addcd )
by Justin
03:28
created

Page::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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 $alias = null;
22
	protected $row = null;
23
	protected $pagetype = "";
24
25
	public function __construct() {
26
		//
27
	}
28
29
	public function load ($alias = null) {
30
		if ($alias == null) {
31
			if (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) {
32
				$alias = Validator_String::get($_REQUEST['page']);
33
			} else {
34
				$alias = $this->getDomain()->getHomePage();
35
			}
36
		} else {
37
			//$alias = Database::getInstance()->escape($alias);
38
		}
39
40
		Events::throwEvent("get_alias", array(
41
			'alias' => &$alias,
42
			'page' => &$this,
43
			'domain' => $this->getDomain()
44
		));
45
46
		$this->alias = $alias;
47
48
		if (Cache::contains("pages", "page_" . $alias)) {
49
			$this->row = Cache::get("pages", "page_" . $alias);
50
		} else {
51
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias AND `activated` = '1'; ", array('alias' => $alias));
52
53
			if (!$row) {
54
				if (PHPUtils::strEqs("error404", $alias)) {
55
					throw new IllegalStateException("No page with alias 'error404' exists (requested alias: " . $alias . ").");
56
				}
57
58
				//page not found
59
				$new_alias = "error404";
60
61
				Events::throwEvent("load_page_error404", array(
62
					'alias' => &$new_alias,
63
					'original_alias' => $alias,
64
					'page' => &$this,
65
					'domain' => $this->getDomain()
66
				));
67
68
				$this->load($new_alias);
69
				return null;
70
			}
71
72
			$this->row = $row;
73
74
			//cache result
75
			Cache::put("pages", "page_" . $alias, $row);
76
		}
77
78
		//get name of page type (class name)
79
		$this->pagetype = $this->row['page_type'];
80
	}
81
82
	protected function getDomain () : Domain {
83
		return Registry::singleton()->getObject("domain");
84
	}
85
86
	public function reloadCache () {
87
		Cache::clear("pages");
88
	}
89
90
	public function getPageID () : int {
91
		return $this->row['id'];
92
	}
93
94
	public function getAlias () : string {
95
		return $this->alias;
96
	}
97
98
	public function getPageType () : string {
99
		return $this->pagetype;
100
	}
101
102
	public function getTitle () : string {
103
		return $this->row['title'];
104
	}
105
106
	public function getContent () : string {
107
		return $this->row['content'];
108
	}
109
110
	public function getGlobalMenuID () : int {
111
		return $this->row['global_menu'];
112
	}
113
114
	public function getLocalMenuID () : int {
115
		return $this->row['local_menu'];
116
	}
117
118
	public function getStyle () : string {
119
		return $this->row['design'];
120
	}
121
122
	public function getFolder () : string {
123
		return $this->row['folder'];
124
	}
125
126
	public function getLastEdit () {
127
		return $this->row['lastUpdate'];
128
	}
129
130
	public function hasCustomTemplate () : bool {
131
		return $this->row['template'] !== "none";
132
	}
133
134
	public function getCustomTemplate () : string {
135
		return $this->row['template'];
136
	}
137
138
	public function hasCustomPermissions () : bool {
139
		return $this->row['can_see_permissions'] !== "none";
140
	}
141
142
	public function listCustomPermissions () : array {
143
		return explode("|", $this->row['can_see_permissions']);
144
	}
145
146
	public function isPublished () : bool {
147
		return $this->row['published'] == 1;
148
	}
149
150
	public function getContentType () : string {
151
		return $this->row['content_type'];
152
	}
153
154
	public function getLeftSidebarID () : int {
155
		return $this->row['sidebar_left'];
156
	}
157
158
	public function getRightSidebarID () : int {
159
		return $this->row['sidebar_right'];
160
	}
161
162
	public function activate (bool $bool = true) {
163
		$this->row['activated'] = $bool;
164
	}
165
166
	/**
167
	 * save changes into database
168
	 */
169
	public function save () {
170
		//TODO: add code here
171
	}
172
173
	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, string $author = "system") : int {
174
		//throw event
175
		Events::throwEvent("create_page", array(
176
			'alias' => &$alias,
177
			'title' => &$title,
178
			'page_type' => &$page_type,
179
			'content' => &$content,
180
			'folder' => &$folder,
181
			'global_menu' => &$globalMenu,
182
			'local_menu' => &$localMenu,
183
			'parentID' => &$parentID,
184
			'sitemap' => &$sitemap,
185
			'published' => &$published,
186
			'editable' => &$editable,
187
			'author' => &$author
188
		));
189
190
		Database::getInstance()->execute("INSERT INTO `{praefix}pages` (
191
			`id`, `alias`, `title`, `content`, `parent`, `folder`, `global_menu`, `local_menu`, `page_type`, `design`, `sitemap`, `published`, `version`, `last_update`, `created`, `editable`, `author`, `activated`
192
		) VALUES (
193
			NULL, :alias, :title, :content, :parent, :folder, :globalMenu, :localMenu, :pageType, 'none', :sitemap, :published, '1', '0000-00-00 00:00:00', CURRENT_TIMESTAMP, :editable, :author, '1'
194
		) ON DUPLICATE KEY UPDATE `alias` = :alias; ", array(
195
			'alias' => $alias,
196
			'title' => $title,
197
			'content' => $content,
198
			'parent' => $parentID,
199
			'folder' => $folder,
200
			'globalMenu' => $globalMenu,
201
			'localMenu' => $localMenu,
202
			'pageType' => $page_type,
203
			'sitemap' => ($sitemap ? 1 : 0),
204
			'published' => ($published ? 1 : 0),
205
			'editable' => ($editable ? 1 : 0),
206
			'author' => $author
207
		));
208
209
		Cache::clear("pages");
210
211
		//return page id
212
		$insertID = Database::getInstance()->lastInsertId();
213
214
		//throw event
215
		Events::throwEvent("created_page", array(
216
			'alias' => $alias,
217
			'title' => $title,
218
			'insertID' => $insertID
219
		));
220
221
		//get pageID by alias
222
		$pageID = Page::getPageIDByAlias($alias);
223
224
		//set default rights, allow page for administrators, registered users, guests and bots
225
		PageRights::setDefaultAllowedGroups($pageID, array(1, 2, 3, 4));
226
227
		return $pageID;
228
	}
229
230
	public static function delete (string $alias) {
231
		$delete = true;
232
233
		//plugins can avoid deletion or change alias
234
		Events::throwEvent("delete_page_alias", array(
235
			'alias' => &$alias,
236
			'delete' => &$delete
237
		));
238
239
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
240
			//remove page from database
241
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
242
243
			Cache::clear("pages");
244
		}
245
	}
246
247
	public static function deleteByID (int $id) {
248
		$delete = true;
249
250
		//plugins can avoid deletion or change alias
251
		Events::throwEvent("delete_page_id", array(
252
			'alias' => &$id,
253
			'delete' => &$delete
254
		));
255
256
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
257
			//remove page from database
258
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `id` = :id; ", array('id' => $id));
259
260
			Cache::clear("pages");
261
		}
262
	}
263
264
	public static function get (string $alias) : Page {
265
		$page = new Page();
266
		$page->load($alias);
267
268
		return $page;
269
	}
270
271
	public static function setPageType (string $alias, string $page_type) {
272
		Events::throwEvent("set_pagetype", array(
273
			'alias' => &$alias,
274
			'page_type' => &$page_type
275
		));
276
277
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `page_type` = :page_type WHERE `alias` = :alias; ", array(
278
			'alias' => $alias,
279
			'page_type' => $page_type
280
		));
281
282
		Cache::clear("pages");
283
	}
284
285
	/**
286
	 * get id of page by alias
287
	 *
288
	 * only use this method for database upgrade, because their is no caching for this method!
289
	 *
290
	 * @param string $alias alias of page
291
	 *
292
	 * @throws IllegalStateException if alias doesnt exists
293
	 *
294
	 * @return int pageID
295
	 */
296
	public static function getPageIDByAlias (string $alias) : int {
297
		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
298
299
		if (!$row) {
300
			throw new IllegalStateException("page with alias '" . $alias . "' doesnt exists.");
301
		}
302
303
		return $row['id'];
304
	}
305
306
}
307
308
?>
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...
309