Completed
Push — master ( 602a89...94ac79 )
by Justin
09:43 queued 06:38
created
system/packages/com.jukusoft.cms.admin/classes/pageeditpage.php 1 patch
Indentation   +209 added lines, -209 removed lines patch added patch discarded remove patch
@@ -27,215 +27,215 @@  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
-		));
122
-
123
-		//set available styles
124
-		$template->assign("styles", StyleController::listAllStyles());
125
-
126
-		//get all pages from database
127
-		$pages = array();
128
-		$rows = Database::getInstance()->listRows("SELECT `id`, `alias` as `activated` FROM `{praefix}pages` WHERE `editable` = '1' AND `activated` = '1'; ");
129
-
130
-		foreach ($rows as $row) {
131
-			$pages[] = array(
132
-				'id' => $row['id'],
133
-				'alias' => $row['alias']
134
-			);
135
-		}
136
-
137
-		$template->assign("parent_pages", $pages);
138
-
139
-		//add support to show additional code from plugins
140
-		$additional_code_header = "";
141
-		$additional_code_footer = "";
142
-
143
-		Events::throwEvent("page_edit_additional_code_header", array(
144
-			'page' => &$page,
145
-			'code' => &$additional_code_header
146
-		));
147
-
148
-		$template->assign("additional_code_header", $additional_code_footer);
149
-
150
-		Events::throwEvent("page_edit_additional_code_footer", array(
151
-			'page' => &$page,
152
-			'code' => &$additional_code_footer
153
-		));
154
-
155
-		$template->assign("additional_code_footer", $additional_code_footer);
156
-
157
-		$template->assign("errors", $error_messages);
158
-		$template->assign("success_messages", $success_messages);
159
-
160
-		return $template->getCode();
161
-	}
162
-
163
-	protected function save (Page &$page) {
164
-		//first check permissions
165
-		if (!PermissionChecker::current()->hasRight("can_edit_all_pages") && !(PermissionChecker::current()->hasRight("can_edit_own_pages") && $page->getAuthorID() == User::current()->getID())) {
166
-			//user doesn't have permissions to edit this page
167
-			return "You don't have permissions to edit this page!";
168
-		}
169
-
170
-		if (!isset($_POST['title']) || empty($_POST['title'])) {
171
-			return "No title was set";
172
-		}
173
-
174
-		//validate title
175
-		$title = htmlentities($_POST['title']);
176
-
177
-		if (!isset($_POST['html_code']) || empty($_POST['html_code'])) {
178
-			return "No content was set or content is empty!";
179
-		}
180
-
181
-		$content = $_POST['html_code'];
182
-
183
-		//update page in database
184
-		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `title` = :title, `content` = :content WHERE `id` = :pageID; ", array(
185
-			'title' => $title,
186
-			'content' => $content,
187
-			'pageID' => $page->getPageID()
188
-		));
189
-
190
-		//clear cache
191
-		$page->clearCache();
192
-
193
-		//reload page from database
194
-		$page->loadByID($page->getPageID(), false);
195
-
196
-		//TODO: remove this line later
197
-		Cache::clear("pages");
198
-
199
-		return true;
200
-	}
201
-
202
-	protected function publish (Page &$page) {
203
-		//check permissions for publishing
204
-		if (PermissionChecker::current()->hasRight("can_publish_all_pages") || (PermissionChecker::current()->hasRight("can_publish_own_pages") && $page->getAuthorID() == User::current()->getID())) {
205
-			//update page in database
206
-			Database::getInstance()->execute("UPDATE `{praefix}pages` SET `published` = '1' WHERE `id` = :pageID; ", array(
207
-				'pageID' => $page->getPageID()
208
-			));
209
-
210
-			//clear cache
211
-			$page->clearCache();
212
-
213
-			//reload page from database
214
-			$page->loadByID($page->getPageID(), false);
215
-
216
-			//TODO: remove this line later
217
-			Cache::clear("pages");
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
+        ));
122
+
123
+        //set available styles
124
+        $template->assign("styles", StyleController::listAllStyles());
125
+
126
+        //get all pages from database
127
+        $pages = array();
128
+        $rows = Database::getInstance()->listRows("SELECT `id`, `alias` as `activated` FROM `{praefix}pages` WHERE `editable` = '1' AND `activated` = '1'; ");
129
+
130
+        foreach ($rows as $row) {
131
+            $pages[] = array(
132
+                'id' => $row['id'],
133
+                'alias' => $row['alias']
134
+            );
135
+        }
136
+
137
+        $template->assign("parent_pages", $pages);
138
+
139
+        //add support to show additional code from plugins
140
+        $additional_code_header = "";
141
+        $additional_code_footer = "";
142
+
143
+        Events::throwEvent("page_edit_additional_code_header", array(
144
+            'page' => &$page,
145
+            'code' => &$additional_code_header
146
+        ));
147
+
148
+        $template->assign("additional_code_header", $additional_code_footer);
149
+
150
+        Events::throwEvent("page_edit_additional_code_footer", array(
151
+            'page' => &$page,
152
+            'code' => &$additional_code_footer
153
+        ));
154
+
155
+        $template->assign("additional_code_footer", $additional_code_footer);
156
+
157
+        $template->assign("errors", $error_messages);
158
+        $template->assign("success_messages", $success_messages);
159
+
160
+        return $template->getCode();
161
+    }
162
+
163
+    protected function save (Page &$page) {
164
+        //first check permissions
165
+        if (!PermissionChecker::current()->hasRight("can_edit_all_pages") && !(PermissionChecker::current()->hasRight("can_edit_own_pages") && $page->getAuthorID() == User::current()->getID())) {
166
+            //user doesn't have permissions to edit this page
167
+            return "You don't have permissions to edit this page!";
168
+        }
169
+
170
+        if (!isset($_POST['title']) || empty($_POST['title'])) {
171
+            return "No title was set";
172
+        }
173
+
174
+        //validate title
175
+        $title = htmlentities($_POST['title']);
176
+
177
+        if (!isset($_POST['html_code']) || empty($_POST['html_code'])) {
178
+            return "No content was set or content is empty!";
179
+        }
180
+
181
+        $content = $_POST['html_code'];
182
+
183
+        //update page in database
184
+        Database::getInstance()->execute("UPDATE `{praefix}pages` SET `title` = :title, `content` = :content WHERE `id` = :pageID; ", array(
185
+            'title' => $title,
186
+            'content' => $content,
187
+            'pageID' => $page->getPageID()
188
+        ));
189
+
190
+        //clear cache
191
+        $page->clearCache();
192
+
193
+        //reload page from database
194
+        $page->loadByID($page->getPageID(), false);
195
+
196
+        //TODO: remove this line later
197
+        Cache::clear("pages");
198
+
199
+        return true;
200
+    }
201
+
202
+    protected function publish (Page &$page) {
203
+        //check permissions for publishing
204
+        if (PermissionChecker::current()->hasRight("can_publish_all_pages") || (PermissionChecker::current()->hasRight("can_publish_own_pages") && $page->getAuthorID() == User::current()->getID())) {
205
+            //update page in database
206
+            Database::getInstance()->execute("UPDATE `{praefix}pages` SET `published` = '1' WHERE `id` = :pageID; ", array(
207
+                'pageID' => $page->getPageID()
208
+            ));
209
+
210
+            //clear cache
211
+            $page->clearCache();
212
+
213
+            //reload page from database
214
+            $page->loadByID($page->getPageID(), false);
215
+
216
+            //TODO: remove this line later
217
+            Cache::clear("pages");
218 218
 
219
-			return true;
220
-		} else {
221
-			return "You don't have the permissions to publish this page!";
222
-		}
223
-	}
219
+            return true;
220
+        } else {
221
+            return "You don't have the permissions to publish this page!";
222
+        }
223
+    }
224 224
 
225
-	protected function showError (string $message) : string {
226
-		//show error
227
-		$template = new DwooTemplate("pages/error");
228
-		$template->assign("message", "No pageID was set!");
229
-		return $template->getCode();
230
-	}
225
+    protected function showError (string $message) : string {
226
+        //show error
227
+        $template = new DwooTemplate("pages/error");
228
+        $template->assign("message", "No pageID was set!");
229
+        return $template->getCode();
230
+    }
231 231
 
232
-	public function getFooterScripts(): string {
233
-		$style_name = Registry::singleton()->getSetting("current_style_name");
234
-		$style_path = DomainUtils::getBaseURL() . "/styles/" . $style_name . "/";
232
+    public function getFooterScripts(): string {
233
+        $style_name = Registry::singleton()->getSetting("current_style_name");
234
+        $style_path = DomainUtils::getBaseURL() . "/styles/" . $style_name . "/";
235 235
 
236
-		$thirdparty_url = Registry::singleton()->getSetting("thirdparty_url");
236
+        $thirdparty_url = Registry::singleton()->getSetting("thirdparty_url");
237 237
 
238
-		/*return "<!-- CK Editor -->
238
+        /*return "<!-- CK Editor -->
239 239
 			<script src=\"" . $style_path . "bower_components/ckeditor/ckeditor.js\"></script>
240 240
 			
241 241
 			<script>
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
 				});
250 250
 			</script>";*/
251 251
 
252
-		return "<script src=\"" . $thirdparty_url . "tinymce_4.8.2/js/tinymce/tinymce.min.js\"></script>
252
+        return "<script src=\"" . $thirdparty_url . "tinymce_4.8.2/js/tinymce/tinymce.min.js\"></script>
253 253
   				<script>tinymce.init({
254 254
 					  selector: 'textarea',
255 255
 					  height: 500,
@@ -268,11 +268,11 @@  discard block
 block discarded – undo
268 268
 					}
269 269
 				};
270 270
 				</script>";
271
-	}
271
+    }
272 272
 
273
-	public function listRequiredPermissions(): array {
274
-		return array("can_edit_all_pages", "can_edit_own_pages");
275
-	}
273
+    public function listRequiredPermissions(): array {
274
+        return array("can_edit_all_pages", "can_edit_own_pages");
275
+    }
276 276
 
277 277
 }
278 278
 
Please login to merge, or discard this patch.