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