Completed
Push — master ( ac8acd...d50551 )
by Justin
04:00
created

Page::getMetaDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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 getMetaDescription () : string {
163
		return $this->row['meta_description'];
164
	}
165
166
	public function getKeywords () : string {
167
		return $this->row['meta_keywords'];
168
	}
169
170
	public function activate (bool $bool = true) {
171
		$this->row['activated'] = $bool;
172
	}
173
174
	/**
175
	 * save changes into database
176
	 */
177
	public function save () {
178
		//TODO: add code here
179
	}
180
181
	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 {
182
		//throw event
183
		Events::throwEvent("create_page", array(
184
			'alias' => &$alias,
185
			'title' => &$title,
186
			'page_type' => &$page_type,
187
			'content' => &$content,
188
			'folder' => &$folder,
189
			'global_menu' => &$globalMenu,
190
			'local_menu' => &$localMenu,
191
			'parentID' => &$parentID,
192
			'sitemap' => &$sitemap,
193
			'published' => &$published,
194
			'editable' => &$editable,
195
			'author' => &$author
196
		));
197
198
		Database::getInstance()->execute("INSERT INTO `{praefix}pages` (
199
			`id`, `alias`, `title`, `content`, `parent`, `folder`, `global_menu`, `local_menu`, `page_type`, `design`, `sitemap`, `published`, `version`, `last_update`, `created`, `editable`, `author`, `activated`
200
		) VALUES (
201
			NULL, :alias, :title, :content, :parent, :folder, :globalMenu, :localMenu, :pageType, 'none', :sitemap, :published, '1', '0000-00-00 00:00:00', CURRENT_TIMESTAMP, :editable, :author, '1'
202
		) ON DUPLICATE KEY UPDATE `alias` = :alias; ", array(
203
			'alias' => $alias,
204
			'title' => $title,
205
			'content' => $content,
206
			'parent' => $parentID,
207
			'folder' => $folder,
208
			'globalMenu' => $globalMenu,
209
			'localMenu' => $localMenu,
210
			'pageType' => $page_type,
211
			'sitemap' => ($sitemap ? 1 : 0),
212
			'published' => ($published ? 1 : 0),
213
			'editable' => ($editable ? 1 : 0),
214
			'author' => $author
215
		));
216
217
		Cache::clear("pages");
218
219
		//return page id
220
		$insertID = Database::getInstance()->lastInsertId();
221
222
		//throw event
223
		Events::throwEvent("created_page", array(
224
			'alias' => $alias,
225
			'title' => $title,
226
			'insertID' => $insertID
227
		));
228
229
		//get pageID by alias
230
		$pageID = Page::getPageIDByAlias($alias);
231
232
		//set default rights, allow page for administrators, registered users, guests and bots
233
		PageRights::setDefaultAllowedGroups($pageID, array(1, 2, 3, 4));
234
235
		return $pageID;
236
	}
237
238
	public static function delete (string $alias) {
239
		$delete = true;
240
241
		//plugins can avoid deletion or change alias
242
		Events::throwEvent("delete_page_alias", array(
243
			'alias' => &$alias,
244
			'delete' => &$delete
245
		));
246
247
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
248
			//remove page from database
249
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
250
251
			Cache::clear("pages");
252
		}
253
	}
254
255
	public static function deleteByID (int $id) {
256
		$delete = true;
257
258
		//plugins can avoid deletion or change alias
259
		Events::throwEvent("delete_page_id", array(
260
			'alias' => &$id,
261
			'delete' => &$delete
262
		));
263
264
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
265
			//remove page from database
266
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `id` = :id; ", array('id' => $id));
267
268
			Cache::clear("pages");
269
		}
270
	}
271
272
	public static function get (string $alias) : Page {
273
		$page = new Page();
274
		$page->load($alias);
275
276
		return $page;
277
	}
278
279
	public static function setPageType (string $alias, string $page_type) {
280
		Events::throwEvent("set_pagetype", array(
281
			'alias' => &$alias,
282
			'page_type' => &$page_type
283
		));
284
285
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `page_type` = :page_type WHERE `alias` = :alias; ", array(
286
			'alias' => $alias,
287
			'page_type' => $page_type
288
		));
289
290
		Cache::clear("pages");
291
	}
292
293
	/**
294
	 * get id of page by alias
295
	 *
296
	 * only use this method for database upgrade, because their is no caching for this method!
297
	 *
298
	 * @param string $alias alias of page
299
	 *
300
	 * @throws IllegalStateException if alias doesnt exists
301
	 *
302
	 * @return int pageID
303
	 */
304
	public static function getPageIDByAlias (string $alias) : int {
305
		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
306
307
		if (!$row) {
308
			throw new IllegalStateException("page with alias '" . $alias . "' doesnt exists.");
309
		}
310
311
		return $row['id'];
312
	}
313
314
}
315
316
?>
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...
317