Completed
Push — master ( 38c7f6...20a216 )
by Justin
04:34
created
system/packages/com.jukusoft.cms.admin/classes/pageeditpage.php 1 patch
Indentation   +226 added lines, -226 removed lines patch added patch discarded remove patch
@@ -27,240 +27,240 @@  discard block
 block discarded – undo
27 27
 
28 28
 class PageEditPage extends PageType {
29 29
 
30
-	public function getContent(): string {
31
-		$template = new DwooTemplate("pages/editpage");
32
-
33
-		//check, if pageID is set
34
-		if (!isset($_REQUEST['edit']) || empty($_REQUEST['edit'])) {
35
-			//show error
36
-			return $this->showError("No pageID was set!");
37
-		}
38
-
39
-		$pageID = (int) $_REQUEST['edit'];
40
-
41
-		$page = new Page();
42
-		$page->loadByID($pageID);
43
-
44
-		//first check permissions
45
-		if (!PermissionChecker::current()->hasRight("can_edit_all_pages") && !(PermissionChecker::current()->hasRight("can_edit_own_pages") && $page->getAuthorID() == User::current()->getID())) {
46
-			//user doesn't have permissions to edit this page
47
-			return $this->showError("You don't have permissions to edit this page!");
48
-		}
49
-
50
-		//first, lock page
51
-		Page::lockPage($page->getPageID(), User::current()->getID());
52
-
53
-		$success_messages = array();
54
-		$error_messages = array();
55
-
56
-		//save page
57
-		if (isset($_REQUEST['submit'])) {
58
-			if ($_REQUEST['submit'] === "Save") {
59
-				//save page
60
-				$res = $this->save($page);
61
-
62
-				if ($res === true) {
63
-					$success_messages[] = "Saved page successfully!";
64
-				} else {
65
-					$error_messages[] = $res;
66
-				}
67
-			} else if ($_REQUEST['submit'] === "SaveUnlock") {
68
-				//save page
69
-				$res = $this->save($page);
70
-
71
-				if ($res === true) {
72
-					//unlock page
73
-					Page::unlockPage($page->getPageID());
74
-
75
-					//redirect to admin/pages
76
-					header("Location: " . DomainUtils::generateURL("admin/pages"));
77
-
78
-					ob_flush();
79
-					ob_end_flush();
80
-
81
-					exit;
82
-				} else {
83
-					$error_messages[] = $res;
84
-				}
85
-			} else if ($_REQUEST['submit'] === "Publish") {
86
-				//save page
87
-				$res = $this->save($page);
88
-
89
-				if ($res === true) {
90
-					$success_messages[] = "Saved page successfully!";
91
-				} else {
92
-					$error_messages[] = $res;
93
-				}
94
-
95
-				//publish page
96
-				$res = $this->publish($page);
97
-
98
-				if ($res === true) {
99
-					$success_messages[] = "Page published successfully!";
100
-				} else {
101
-					$error_messages[] = $res;
102
-				}
103
-			}
104
-		}
105
-
106
-		$template->assign("action_url", DomainUtils::generateURL($this->getPage()->getAlias(), array("edit" => $pageID)));
107
-
108
-		$template->assign("page", array(
109
-			'id' => $page->getPageID(),
110
-			'alias' => "/" . $page->getAlias(),
111
-			'title' => $page->getTitle(),
112
-			'content' => $page->getContent(),
113
-			'is_published' => $page->isPublished(),
114
-			'can_publish' => (!$page->isPublished() && (PermissionChecker::current()->hasRight("can_publish_all_pages") || (PermissionChecker::current()->hasRight("can_publish_own_pages") && $page->getAuthorID() == User::current()->getID()))),
115
-			'can_change_owner' => (PermissionChecker::current()->hasRight("can_change_page_owner") || $page->getAuthorID() == User::current()->getID()),
116
-			'folder' => $page->getFolder(),
117
-			'preview_url' => DomainUtils::generateURL($page->getAlias(), array("preview" => "true")),
118
-			'current_style' => $page->getStyle(),
119
-			'template' => $page->getCustomTemplate(),
120
-			'has_custom_template' => $page->hasCustomTemplate(),
121
-			'parent' => $page->getParentID()
122
-		));
123
-
124
-		//set available styles
125
-		$template->assign("styles", StyleController::listAllStyles());
126
-
127
-		//get all pages from database
128
-		$pages = array();
129
-		$rows = Database::getInstance()->listRows("SELECT `id`, `alias` FROM `{praefix}pages` WHERE `editable` = '1' AND `activated` = '1'; ");
130
-
131
-		foreach ($rows as $row) {
132
-			$pages[] = array(
133
-				'id' => $row['id'],
134
-				'alias' => $row['alias']
135
-			);
136
-		}
137
-
138
-		$template->assign("parent_pages", $pages);
139
-
140
-		//add support to show additional code from plugins
141
-		$additional_code_header = "";
142
-		$additional_code_footer = "";
143
-
144
-		Events::throwEvent("page_edit_additional_code_header", array(
145
-			'page' => &$page,
146
-			'code' => &$additional_code_header
147
-		));
148
-
149
-		$template->assign("additional_code_header", $additional_code_footer);
150
-
151
-		Events::throwEvent("page_edit_additional_code_footer", array(
152
-			'page' => &$page,
153
-			'code' => &$additional_code_footer
154
-		));
155
-
156
-		$template->assign("additional_code_footer", $additional_code_footer);
157
-
158
-		$template->assign("errors", $error_messages);
159
-		$template->assign("success_messages", $success_messages);
160
-
161
-		return $template->getCode();
162
-	}
163
-
164
-	protected function save (Page &$page) {
165
-		//first check permissions
166
-		if (!PermissionChecker::current()->hasRight("can_edit_all_pages") && !(PermissionChecker::current()->hasRight("can_edit_own_pages") && $page->getAuthorID() == User::current()->getID())) {
167
-			//user doesn't have permissions to edit this page
168
-			return "You don't have permissions to edit this page!";
169
-		}
170
-
171
-		if (!isset($_POST['title']) || empty($_POST['title'])) {
172
-			return "No title was set";
173
-		}
174
-
175
-		//validate title
176
-		$title = htmlentities($_POST['title']);
177
-
178
-		if (!isset($_POST['html_code']) || empty($_POST['html_code'])) {
179
-			return "No content was set or content is empty!";
180
-		}
181
-
182
-		$content = $_POST['html_code'];
183
-
184
-		//TODO: save page attributes
185
-		if (!isset($_REQUEST['parent']) || empty($_REQUEST['parent'])) {
186
-			return "Parent page wasn't set!";
187
-		}
188
-
189
-		$parent = (int) $_REQUEST['parent'];
190
-
191
-		if (!isset($_REQUEST['design']) || empty($_REQUEST['design'])) {
192
-			return "Design wasn't set!";
193
-		}
194
-
195
-		$design = $_REQUEST['design'];
196
-
197
-		//TODO: check, if style (design) exists
198
-
199
-		$template = "none";
200
-
201
-		if (isset($_REQUEST['has_custom_template']) && isset($_REQUEST['template']) && !empty($_REQUEST['template'])) {
202
-			$template = $_REQUEST['template'];
203
-		}
204
-
205
-		//update page in database
206
-		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `title` = :title, `content` = :content, `parent` => :parent, `design` = :design, `template` = :template WHERE `id` = :pageID; ", array(
207
-			'title' => $title,
208
-			'content' => $content,
209
-			'pageID' => $page->getPageID(),
210
-			'parent' => $parent,
211
-			'design' => $design,
212
-			'template' => $template
213
-		));
30
+    public function getContent(): string {
31
+        $template = new DwooTemplate("pages/editpage");
32
+
33
+        //check, if pageID is set
34
+        if (!isset($_REQUEST['edit']) || empty($_REQUEST['edit'])) {
35
+            //show error
36
+            return $this->showError("No pageID was set!");
37
+        }
38
+
39
+        $pageID = (int) $_REQUEST['edit'];
40
+
41
+        $page = new Page();
42
+        $page->loadByID($pageID);
43
+
44
+        //first check permissions
45
+        if (!PermissionChecker::current()->hasRight("can_edit_all_pages") && !(PermissionChecker::current()->hasRight("can_edit_own_pages") && $page->getAuthorID() == User::current()->getID())) {
46
+            //user doesn't have permissions to edit this page
47
+            return $this->showError("You don't have permissions to edit this page!");
48
+        }
49
+
50
+        //first, lock page
51
+        Page::lockPage($page->getPageID(), User::current()->getID());
52
+
53
+        $success_messages = array();
54
+        $error_messages = array();
55
+
56
+        //save page
57
+        if (isset($_REQUEST['submit'])) {
58
+            if ($_REQUEST['submit'] === "Save") {
59
+                //save page
60
+                $res = $this->save($page);
61
+
62
+                if ($res === true) {
63
+                    $success_messages[] = "Saved page successfully!";
64
+                } else {
65
+                    $error_messages[] = $res;
66
+                }
67
+            } else if ($_REQUEST['submit'] === "SaveUnlock") {
68
+                //save page
69
+                $res = $this->save($page);
70
+
71
+                if ($res === true) {
72
+                    //unlock page
73
+                    Page::unlockPage($page->getPageID());
74
+
75
+                    //redirect to admin/pages
76
+                    header("Location: " . DomainUtils::generateURL("admin/pages"));
77
+
78
+                    ob_flush();
79
+                    ob_end_flush();
80
+
81
+                    exit;
82
+                } else {
83
+                    $error_messages[] = $res;
84
+                }
85
+            } else if ($_REQUEST['submit'] === "Publish") {
86
+                //save page
87
+                $res = $this->save($page);
88
+
89
+                if ($res === true) {
90
+                    $success_messages[] = "Saved page successfully!";
91
+                } else {
92
+                    $error_messages[] = $res;
93
+                }
94
+
95
+                //publish page
96
+                $res = $this->publish($page);
97
+
98
+                if ($res === true) {
99
+                    $success_messages[] = "Page published successfully!";
100
+                } else {
101
+                    $error_messages[] = $res;
102
+                }
103
+            }
104
+        }
105
+
106
+        $template->assign("action_url", DomainUtils::generateURL($this->getPage()->getAlias(), array("edit" => $pageID)));
107
+
108
+        $template->assign("page", array(
109
+            'id' => $page->getPageID(),
110
+            'alias' => "/" . $page->getAlias(),
111
+            'title' => $page->getTitle(),
112
+            'content' => $page->getContent(),
113
+            'is_published' => $page->isPublished(),
114
+            'can_publish' => (!$page->isPublished() && (PermissionChecker::current()->hasRight("can_publish_all_pages") || (PermissionChecker::current()->hasRight("can_publish_own_pages") && $page->getAuthorID() == User::current()->getID()))),
115
+            'can_change_owner' => (PermissionChecker::current()->hasRight("can_change_page_owner") || $page->getAuthorID() == User::current()->getID()),
116
+            'folder' => $page->getFolder(),
117
+            'preview_url' => DomainUtils::generateURL($page->getAlias(), array("preview" => "true")),
118
+            'current_style' => $page->getStyle(),
119
+            'template' => $page->getCustomTemplate(),
120
+            'has_custom_template' => $page->hasCustomTemplate(),
121
+            'parent' => $page->getParentID()
122
+        ));
123
+
124
+        //set available styles
125
+        $template->assign("styles", StyleController::listAllStyles());
126
+
127
+        //get all pages from database
128
+        $pages = array();
129
+        $rows = Database::getInstance()->listRows("SELECT `id`, `alias` FROM `{praefix}pages` WHERE `editable` = '1' AND `activated` = '1'; ");
130
+
131
+        foreach ($rows as $row) {
132
+            $pages[] = array(
133
+                'id' => $row['id'],
134
+                'alias' => $row['alias']
135
+            );
136
+        }
137
+
138
+        $template->assign("parent_pages", $pages);
139
+
140
+        //add support to show additional code from plugins
141
+        $additional_code_header = "";
142
+        $additional_code_footer = "";
143
+
144
+        Events::throwEvent("page_edit_additional_code_header", array(
145
+            'page' => &$page,
146
+            'code' => &$additional_code_header
147
+        ));
148
+
149
+        $template->assign("additional_code_header", $additional_code_footer);
150
+
151
+        Events::throwEvent("page_edit_additional_code_footer", array(
152
+            'page' => &$page,
153
+            'code' => &$additional_code_footer
154
+        ));
155
+
156
+        $template->assign("additional_code_footer", $additional_code_footer);
157
+
158
+        $template->assign("errors", $error_messages);
159
+        $template->assign("success_messages", $success_messages);
160
+
161
+        return $template->getCode();
162
+    }
163
+
164
+    protected function save (Page &$page) {
165
+        //first check permissions
166
+        if (!PermissionChecker::current()->hasRight("can_edit_all_pages") && !(PermissionChecker::current()->hasRight("can_edit_own_pages") && $page->getAuthorID() == User::current()->getID())) {
167
+            //user doesn't have permissions to edit this page
168
+            return "You don't have permissions to edit this page!";
169
+        }
170
+
171
+        if (!isset($_POST['title']) || empty($_POST['title'])) {
172
+            return "No title was set";
173
+        }
174
+
175
+        //validate title
176
+        $title = htmlentities($_POST['title']);
177
+
178
+        if (!isset($_POST['html_code']) || empty($_POST['html_code'])) {
179
+            return "No content was set or content is empty!";
180
+        }
181
+
182
+        $content = $_POST['html_code'];
183
+
184
+        //TODO: save page attributes
185
+        if (!isset($_REQUEST['parent']) || empty($_REQUEST['parent'])) {
186
+            return "Parent page wasn't set!";
187
+        }
188
+
189
+        $parent = (int) $_REQUEST['parent'];
190
+
191
+        if (!isset($_REQUEST['design']) || empty($_REQUEST['design'])) {
192
+            return "Design wasn't set!";
193
+        }
194
+
195
+        $design = $_REQUEST['design'];
196
+
197
+        //TODO: check, if style (design) exists
198
+
199
+        $template = "none";
200
+
201
+        if (isset($_REQUEST['has_custom_template']) && isset($_REQUEST['template']) && !empty($_REQUEST['template'])) {
202
+            $template = $_REQUEST['template'];
203
+        }
204
+
205
+        //update page in database
206
+        Database::getInstance()->execute("UPDATE `{praefix}pages` SET `title` = :title, `content` = :content, `parent` => :parent, `design` = :design, `template` = :template WHERE `id` = :pageID; ", array(
207
+            'title' => $title,
208
+            'content' => $content,
209
+            'pageID' => $page->getPageID(),
210
+            'parent' => $parent,
211
+            'design' => $design,
212
+            'template' => $template
213
+        ));
214 214
 
215
-		//clear cache
216
-		$page->clearCache();
215
+        //clear cache
216
+        $page->clearCache();
217 217
 
218
-		//reload page from database
219
-		$page->loadByID($page->getPageID(), false);
218
+        //reload page from database
219
+        $page->loadByID($page->getPageID(), false);
220 220
 
221
-		//TODO: remove this line later
222
-		Cache::clear("pages");
221
+        //TODO: remove this line later
222
+        Cache::clear("pages");
223 223
 
224
-		return true;
225
-	}
224
+        return true;
225
+    }
226 226
 
227
-	protected function publish (Page &$page) {
228
-		//check permissions for publishing
229
-		if (PermissionChecker::current()->hasRight("can_publish_all_pages") || (PermissionChecker::current()->hasRight("can_publish_own_pages") && $page->getAuthorID() == User::current()->getID())) {
230
-			//update page in database
231
-			Database::getInstance()->execute("UPDATE `{praefix}pages` SET `published` = '1' WHERE `id` = :pageID; ", array(
232
-				'pageID' => $page->getPageID()
233
-			));
227
+    protected function publish (Page &$page) {
228
+        //check permissions for publishing
229
+        if (PermissionChecker::current()->hasRight("can_publish_all_pages") || (PermissionChecker::current()->hasRight("can_publish_own_pages") && $page->getAuthorID() == User::current()->getID())) {
230
+            //update page in database
231
+            Database::getInstance()->execute("UPDATE `{praefix}pages` SET `published` = '1' WHERE `id` = :pageID; ", array(
232
+                'pageID' => $page->getPageID()
233
+            ));
234 234
 
235
-			//clear cache
236
-			$page->clearCache();
235
+            //clear cache
236
+            $page->clearCache();
237 237
 
238
-			//reload page from database
239
-			$page->loadByID($page->getPageID(), false);
238
+            //reload page from database
239
+            $page->loadByID($page->getPageID(), false);
240 240
 
241
-			//TODO: remove this line later
242
-			Cache::clear("pages");
241
+            //TODO: remove this line later
242
+            Cache::clear("pages");
243 243
 
244
-			return true;
245
-		} else {
246
-			return "You don't have the permissions to publish this page!";
247
-		}
248
-	}
244
+            return true;
245
+        } else {
246
+            return "You don't have the permissions to publish this page!";
247
+        }
248
+    }
249 249
 
250
-	protected function showError (string $message) : string {
251
-		//show error
252
-		$template = new DwooTemplate("pages/error");
253
-		$template->assign("message", "No pageID was set!");
254
-		return $template->getCode();
255
-	}
250
+    protected function showError (string $message) : string {
251
+        //show error
252
+        $template = new DwooTemplate("pages/error");
253
+        $template->assign("message", "No pageID was set!");
254
+        return $template->getCode();
255
+    }
256 256
 
257
-	public function getFooterScripts(): string {
258
-		$style_name = Registry::singleton()->getSetting("current_style_name");
259
-		$style_path = DomainUtils::getBaseURL() . "/styles/" . $style_name . "/";
257
+    public function getFooterScripts(): string {
258
+        $style_name = Registry::singleton()->getSetting("current_style_name");
259
+        $style_path = DomainUtils::getBaseURL() . "/styles/" . $style_name . "/";
260 260
 
261
-		$thirdparty_url = Registry::singleton()->getSetting("thirdparty_url");
261
+        $thirdparty_url = Registry::singleton()->getSetting("thirdparty_url");
262 262
 
263
-		/*return "<!-- CK Editor -->
263
+        /*return "<!-- CK Editor -->
264 264
 			<script src=\"" . $style_path . "bower_components/ckeditor/ckeditor.js\"></script>
265 265
 			
266 266
 			<script>
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
 				});
275 275
 			</script>";*/
276 276
 
277
-		return "<script src=\"" . $thirdparty_url . "tinymce_4.8.2/js/tinymce/tinymce.min.js\"></script>
277
+        return "<script src=\"" . $thirdparty_url . "tinymce_4.8.2/js/tinymce/tinymce.min.js\"></script>
278 278
   				<script>tinymce.init({
279 279
 					  selector: 'textarea',
280 280
 					  height: 500,
@@ -293,11 +293,11 @@  discard block
 block discarded – undo
293 293
 					}
294 294
 				};
295 295
 				</script>";
296
-	}
296
+    }
297 297
 
298
-	public function listRequiredPermissions(): array {
299
-		return array("can_edit_all_pages", "can_edit_own_pages");
300
-	}
298
+    public function listRequiredPermissions(): array {
299
+        return array("can_edit_all_pages", "can_edit_own_pages");
300
+    }
301 301
 
302 302
 }
303 303
 
Please login to merge, or discard this patch.