Completed
Push — master ( 833ded...e0bbff )
by Justin
16:24 queued 11:34
created

PageType::getOgDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 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 PageType {
20
21
	protected $page = null;
22
23
	public function __construct() {
24
		//
25
	}
26
27
	public function setPage (Page &$page) {
28
		$this->page = $page;
29
	}
30
31
	protected function getPage () : Page {
32
		return $this->page;
33
	}
34
35
	public function showDesign () {
36
		return true;
37
	}
38
39
	public function getContentType () : string {
40
		return "" . $this->page->getContentType() . "; charset=" . $this->getCharset();
41
	}
42
43
	public function getCharset () : string {
44
		return "utf-8";
45
	}
46
47
	public function setCustomHeader () {
48
		//
49
	}
50
51
	public function getAdditionalHeaderCode () : string {
52
		return "";
53
	}
54
55
	public function showFooter () : bool {
56
		return true;
57
	}
58
59
	public function getFooterScripts () : string {
60
		return "";
61
	}
62
63
	public function showHTMLComments () : bool {
64
		return true;
65
	}
66
67
	public function getOgTitle () : string {
68
		return $this->page->getOgTitle();
69
	}
70
71
	public function getOgDescription () : string {
72
		return $this->page->getOgDescription();
73
	}
74
75
	public function getOgType () : string {
76
		return $this->page->getOgType();
77
	}
78
79
	public function getOgImage () : string {
80
		return $this->page->getOgImage();
81
	}
82
83
	public function getOgUrl () : string {
84
		return DomainUtils::generateURL($this->page->getAlias());
85
	}
86
87
	public function getOgTags () : string {
88
		$tags = array();
89
		$tags['og:type'] = $this->getOgType();
90
		$tags['og:url'] = $this->getOgUrl();
91
		$tags['og:title'] = $this->getOgTitle();
92
		$tags['og:description'] = $this->getOgDescription();
93
94
		if (!empty($this->getOgImage())) {
95
			$tags['og:image'] = $this->getOgImage();
96
		}
97
98
		$this->getAdditionalTags($tags);
99
100
		Events::throwEvent("og_tags", array(
101
			'page' => &$this->page,
102
			'page_type' => &$this,
103
			'tags' => &$tags
104
		));
105
106
		$tags_lines = "<!-- OpenGraph tags -->\r\n";
107
108
		foreach ($tags as $property=>$content) {
109
			$tags_lines .= "\t<meta property=\"" . $property . "\" content=\"" . $content . "\" />\r\n";
110
		}
111
112
		return $tags_lines;
113
	}
114
115
	/**
116
	 * add additional tags
117
	 *
118
	 * This method was added so that page types doesn't have to override getOgTags() completely
119
	 */
120
	protected function getAdditionalTags (array &$tags) {
0 ignored issues
show
Unused Code introduced by
The parameter $tags is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

120
	protected function getAdditionalTags (/** @scrutinizer ignore-unused */ array &$tags) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
		//add additional tags
122
	}
123
124
	public function getContent () : string {
125
		$content = $this->getPage()->getContent();
126
127
		//check, if page has custom template
128
		if ($this->getPage()->hasCustomTemplate()) {
129
			//get custom template
130
			$template = Validator_String::get($this->getPage()->getCustomTemplate());
131
132
			$current_style = Registry::singleton()->getSetting("current_style_name");
133
134
			//check, if custom template exists
135
			if (file_exists(STYLE_PATH . $current_style . "/" . $template)) {
136
				$template = new Template($template);
137
138
				$template->assign("TITLE", $this->getPage()->getTitle());
139
				$template->assign("CONTENT", $content);
140
141
				$template->parse("main");
142
				$content = $template->getCode();
143
			} else {
144
				throw new IllegalStateException("Custom template '" . $template . "' doesnt exists.");
145
			}
146
		}
147
148
		Events::throwEvent("get_content", array(
149
			'content' => &$content,
150
			'page' => &$this->page,
151
			'page_type' => &$this
152
		));
153
154
		return $content;
155
	}
156
157
	public function generateNormalPage (string $content, $vars = array()) : string {
158
		$current_style = Registry::singleton()->getSetting("current_style_name");
159
160
		if (file_exists(STYLE_PATH . $current_style . "/pages/normal.tpl")) {
161
			$template = new DwooTemplate(STYLE_PATH . $current_style . "/pages/normal.tpl");
162
163
			$title_preafix = Settings::get("title_praefix", "");
164
			$title_suffix = Settings::get("title_suffix", "");
165
166
			//translate title
167
			$title = Translator::translateTitle($this->getPage()->getTitle());
168
169
			$template->assign("RAW_TITLE", $title);
170
			$template->assign("TITLE", $title_preafix . $title . $title_suffix);
171
			$template->assign("CONTENT", $content);
172
			$template->assign("FOOTER", Registry::singleton()->getSetting("footer", ""));
173
174
			Events::throwEvent("generate_normal_page", array(
175
				'template' => &$template,
176
				'current_style' => $current_style,
177
				'content' => &$content,
178
				'page_type' => &$this,
179
				'page' => $this->getPage()
180
			));
181
182
			foreach ($vars as $key=>$value) {
183
				$template->assign($key, $value);
184
			}
185
186
			return $template->getCode();
187
		} else {
188
			throw new IllegalStateException("no normal template (pages/normal.tpl) found!");
189
			//return $content;
190
		}
191
	}
192
193
	public function checkPermissions (PermissionChecker $permission_checker) {
194
		//first, check required permissions
195
		if (count($this->listRequiredPermissions()) > 0) {
196
			$bool = false;
197
198
			foreach ($this->listRequiredPermissions() as $permission) {
199
				if ($permission_checker->hasRight($permission)) {
200
					$bool = true;
201
					break;
202
				}
203
			}
204
205
			if (!$bool) {
206
				return false;
207
			}
208
		}
209
210
		if (!$this->getPage()->hasCustomPermissions()) {
211
			return true;
212
		} else {
213
			$permissions = $this->getPage()->listCustomPermissions();
214
215
			foreach ($permissions as $permission) {
216
				if ($permission_checker->hasRight($permission)) {
217
					return true;
218
				}
219
			}
220
221
			return false;
222
		}
223
	}
224
225
	protected function listRequiredPermissions () : array {
226
		return array();
227
	}
228
229
	public function exitAfterOutput () {
230
		return false;
231
	}
232
233
	public static function reloadCache () {
234
		Cache::clear("pagetypes");
235
	}
236
237
	public static function listPageTypes (bool $advanced = false) : array {
238
		$rows = array();
239
240
		$advanced_str = $advanced ? "advanced" : "normal";
241
242
		if (Cache::contains("pagetypes", "list-" . $advanced_str)) {
243
			$rows = Cache::get("pagetypes", "list-" . $advanced_str);
244
		} else {
245
			if ($advanced) {
246
				//show all page types
247
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}page_types` WHERE `activated` = '1' ORDER BY `order`; ");
248
			} else {
249
				//show only not-expert page types
250
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}page_types` WHERE `advanced` = '0' AND `activated` = '1' ORDER BY `order`; ");
251
			}
252
253
			//put into cache
254
			Cache::put("pagetypes", "list-" . $advanced_str, $rows);
255
		}
256
257
		return $rows;
258
	}
259
260
	public static function createPageType (string $class_name, string $title, bool $advanced = false, int $order = 10, array $permissions = array("none"), string $owner = "system") {
261
		//validate values
262
		$class_name = Validator_String::get($class_name);
263
		$title = Validator_String::get($title);
264
		$order = Validator_Int::get($order);
265
266
		Events::throwEvent("before_add_pagetype", array(
267
			'class_name' => &$class_name,
268
			'title' => &$title,
269
			'create_permissions' => &$permissions,
270
			'advanced' => &$advanced,
271
			'owner' => &$owner,
272
			'order' => &$order
273
		));
274
275
		//validate and convert array to string
276
		$permissions = implode("|", $permissions);
277
		$permissions = Validator_String::get($permissions);
278
279
		Database::getInstance()->execute("INSERT INTO `{praefix}page_types` (
280
			`page_type`, `title`, `create_permissions`, `advanced`, `owner`, `order`, `activated`
281
		) VALUES (
282
			:pagetype, :title, :permissions, :advanced, :owner, :order, '1'
283
		) ON DUPLICATE KEY UPDATE `title` = :title, `create_permissions` = :permissions, `advanced` = :advanced, `owner` = :owner, `order` = :order, `activated` = '1'; ", array(
284
			'pagetype' => $class_name,
285
			'title' => $title,
286
			'permissions' => $permissions,
287
			'advanced' => ($advanced ? 1 : 0),
288
			'owner' => $owner,
289
			'order' => $order
290
		));
291
292
		Events::throwEvent("after_add_pagetype", array(
293
			'class_name' => $class_name,
294
			'title' => $title,
295
			'create_permissions' => $permissions,
296
			'advanced' => $advanced,
297
			'owner' => $owner,
298
			'order' => $order
299
		));
300
301
		//clear cache
302
		Cache::clear("pagetypes");
303
	}
304
305
	public static function removePageType (string $class_name) {
306
		//validate value
307
		$class_name = Validator_String::get($class_name);
308
309
		$delete = true;
310
311
		//throw event, so plugins can interact
312
		Events::throwEvent("before_remove_pagetype", array(
313
			'class_name' => &$class_name,
314
			'delete' => &$delete
315
		));
316
317
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
318
			Database::getInstance()->execute("DELETE FROM `{praefix}page_types` WHERE `page_type` = :pagetype; ", array('pagetype' => $class_name));
319
320
			//throw event, so plugins can interact
321
			Events::throwEvent("after_remove_pagetype", array(
322
				'class_name' => $class_name
323
			));
324
325
			//clear cache
326
			Cache::clear("pagetypes");
327
		}
328
	}
329
330
	public static function removePageTypesByOwner (string $owner) {
331
		Database::getInstance()->execute("DELETE FROM `{praefix}page_types` WHERE `owner` = :owner; ", array('owner' => $owner));
332
333
		//TODO: change all pages which have this page type
334
335
		//clear cache
336
		Cache::clear("pagetypes");
337
	}
338
339
	public static function exists (string $class_name) : bool {
340
		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}page_types` WHERE `page_type` = :pagetype; ", array(
341
			'pagetype' => $class_name
342
		));
343
344
		return $row !== false;
345
	}
346
347
}
348
349
?>
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...
350