| Total Complexity | 58 |
| Total Lines | 384 |
| Duplicated Lines | 0 % |
| Changes | 40 | ||
| Bugs | 4 | Features | 19 |
Complex classes like PageEditPage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PageEditPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class PageEditPage extends PageType { |
||
| 29 | |||
| 30 | protected $sitemap_change_frequencies = array( |
||
| 31 | "AlWAYS", "HOURLY", "DAILY", "WEEKLY", "MONTHLY", "YEARLY", "NEVER" |
||
| 32 | ); |
||
| 33 | |||
| 34 | public function getContent(): string { |
||
| 35 | $template = new DwooTemplate("pages/editpage"); |
||
| 36 | |||
| 37 | //check, if pageID is set |
||
| 38 | if (!isset($_REQUEST['edit']) || empty($_REQUEST['edit'])) { |
||
| 39 | //show error |
||
| 40 | return $this->showError("No pageID was set!"); |
||
| 41 | } |
||
| 42 | |||
| 43 | $pageID = (int) $_REQUEST['edit']; |
||
| 44 | |||
| 45 | $page = new Page(); |
||
| 46 | $page->loadByID($pageID); |
||
| 47 | |||
| 48 | //first check permissions |
||
| 49 | if (!PermissionChecker::current()->hasRight("can_edit_all_pages") && !(PermissionChecker::current()->hasRight("can_edit_own_pages") && $page->getAuthorID() == User::current()->getID())) { |
||
| 50 | //user doesn't have permissions to edit this page |
||
| 51 | return $this->showError("You don't have permissions to edit this page!"); |
||
| 52 | } |
||
| 53 | |||
| 54 | //first, lock page |
||
| 55 | Page::lockPage($page->getPageID(), User::current()->getID()); |
||
| 56 | |||
| 57 | $success_messages = array(); |
||
| 58 | $error_messages = array(); |
||
| 59 | |||
| 60 | //save page |
||
| 61 | if (isset($_REQUEST['submit'])) { |
||
| 62 | if ($_REQUEST['submit'] === "Save") { |
||
| 63 | //save page |
||
| 64 | $res = $this->save($page); |
||
| 65 | |||
| 66 | if ($res === true) { |
||
| 67 | $success_messages[] = "Saved page successfully!"; |
||
| 68 | } else { |
||
| 69 | $error_messages[] = $res; |
||
| 70 | } |
||
| 71 | } else if ($_REQUEST['submit'] === "SaveUnlock") { |
||
| 72 | //save page |
||
| 73 | $res = $this->save($page); |
||
| 74 | |||
| 75 | if ($res === true) { |
||
| 76 | //unlock page |
||
| 77 | Page::unlockPage($page->getPageID()); |
||
| 78 | |||
| 79 | //redirect to admin/pages |
||
| 80 | header("Location: " . DomainUtils::generateURL("admin/pages")); |
||
| 81 | |||
| 82 | ob_flush(); |
||
| 83 | ob_end_flush(); |
||
| 84 | |||
| 85 | exit; |
||
| 86 | } else { |
||
| 87 | $error_messages[] = $res; |
||
| 88 | } |
||
| 89 | } else if ($_REQUEST['submit'] === "Publish") { |
||
| 90 | //save page |
||
| 91 | $res = $this->save($page); |
||
| 92 | |||
| 93 | if ($res === true) { |
||
| 94 | $success_messages[] = "Saved page successfully!"; |
||
| 95 | } else { |
||
| 96 | $error_messages[] = $res; |
||
| 97 | } |
||
| 98 | |||
| 99 | //publish page |
||
| 100 | $res = $this->publish($page); |
||
| 101 | |||
| 102 | if ($res === true) { |
||
| 103 | $success_messages[] = "Page published successfully!"; |
||
| 104 | } else { |
||
| 105 | $error_messages[] = $res; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | $template->assign("action_url", DomainUtils::generateURL($this->getPage()->getAlias(), array("edit" => $pageID))); |
||
| 111 | |||
| 112 | $template->assign("page", array( |
||
| 113 | 'id' => $page->getPageID(), |
||
| 114 | 'alias' => "/" . $page->getAlias(), |
||
| 115 | 'title' => $page->getTitle(), |
||
| 116 | 'content' => $page->getContent(), |
||
| 117 | 'is_published' => $page->isPublished(), |
||
| 118 | 'can_publish' => (!$page->isPublished() && (PermissionChecker::current()->hasRight("can_publish_all_pages") || (PermissionChecker::current()->hasRight("can_publish_own_pages") && $page->getAuthorID() == User::current()->getID()))), |
||
| 119 | 'can_change_owner' => (PermissionChecker::current()->hasRight("can_change_page_owner") || $page->getAuthorID() == User::current()->getID()), |
||
| 120 | 'folder' => $page->getFolder(), |
||
| 121 | 'preview_url' => DomainUtils::generateURL($page->getAlias(), array("preview" => "true")), |
||
| 122 | 'current_style' => $page->getStyle(), |
||
| 123 | 'template' => $page->getCustomTemplate(), |
||
| 124 | 'has_custom_template' => $page->hasCustomTemplate(), |
||
| 125 | 'parent' => $page->getParentID(), |
||
| 126 | 'meta_description' => $page->getMetaDescription(), |
||
| 127 | 'meta_keywords' => $page->getMetaKeywords(), |
||
| 128 | 'meta_robots' => $page->getMetaRobotsOptions(), |
||
| 129 | 'meta_canonicals' => $page->getMetaCanonicals(), |
||
| 130 | 'has_canoncials' => !empty($page->getMetaCanonicals()), |
||
| 131 | 'sitemap' => $page->isSitemapEnabled(), |
||
| 132 | 'sitemap_changefreq' => $page->getSitemapChangeFreq(), |
||
| 133 | 'sitemap_priority' => $page->getSitemapPriority(), |
||
| 134 | 'og_type' => $page->getOgType() |
||
| 135 | )); |
||
| 136 | |||
| 137 | //set available styles |
||
| 138 | $template->assign("styles", StyleController::listAllStyles()); |
||
| 139 | |||
| 140 | //get all pages from database |
||
| 141 | $pages = array(); |
||
| 142 | $rows = Database::getInstance()->listRows("SELECT `id`, `alias` FROM `{praefix}pages` WHERE `editable` = '1' AND `activated` = '1'; "); |
||
| 143 | |||
| 144 | foreach ($rows as $row) { |
||
| 145 | $pages[] = array( |
||
| 146 | 'id' => $row['id'], |
||
| 147 | 'alias' => $row['alias'] |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | |||
| 151 | $template->assign("parent_pages", $pages); |
||
| 152 | |||
| 153 | //https://developers.google.com/search/reference/robots_meta_tag?hl=de |
||
| 154 | $robots_options = array( |
||
| 155 | "all", |
||
| 156 | "noindex", |
||
| 157 | "nofollow", |
||
| 158 | "none", |
||
| 159 | "noarchive", |
||
| 160 | "nosnippet", |
||
| 161 | "noodp", |
||
| 162 | "notranslate", |
||
| 163 | "noimageindex", |
||
| 164 | "unavailable_after: " |
||
| 165 | ); |
||
| 166 | |||
| 167 | $template->assign("robots_options", $robots_options); |
||
| 168 | |||
| 169 | $sitemap_change_frequencies = $this->sitemap_change_frequencies; |
||
| 170 | |||
| 171 | $template->assign("sitemap_change_frequencies", $sitemap_change_frequencies); |
||
| 172 | |||
| 173 | //OpenGraph types,https://developers.facebook.com/docs/reference/opengraph/ |
||
| 174 | $og_types = array("website", "article", "book", "profile"); |
||
| 175 | |||
| 176 | $template->assign("og_types", $og_types); |
||
| 177 | |||
| 178 | //add support to show additional code from plugins |
||
| 179 | $additional_code_header = ""; |
||
| 180 | $additional_code_footer = ""; |
||
| 181 | |||
| 182 | Events::throwEvent("page_edit_additional_code_header", array( |
||
| 183 | 'page' => &$page, |
||
| 184 | 'code' => &$additional_code_header |
||
| 185 | )); |
||
| 186 | |||
| 187 | $template->assign("additional_code_header", $additional_code_footer); |
||
| 188 | |||
| 189 | Events::throwEvent("page_edit_additional_code_footer", array( |
||
| 190 | 'page' => &$page, |
||
| 191 | 'code' => &$additional_code_footer |
||
| 192 | )); |
||
| 193 | |||
| 194 | $template->assign("additional_code_footer", $additional_code_footer); |
||
| 195 | |||
| 196 | $template->assign("errors", $error_messages); |
||
| 197 | $template->assign("success_messages", $success_messages); |
||
| 198 | |||
| 199 | return $template->getCode(); |
||
| 200 | } |
||
| 201 | |||
| 202 | protected function save (Page &$page) { |
||
| 324 | } |
||
| 325 | |||
| 326 | protected function publish (Page &$page) { |
||
| 327 | //check permissions for publishing |
||
| 328 | if (PermissionChecker::current()->hasRight("can_publish_all_pages") || (PermissionChecker::current()->hasRight("can_publish_own_pages") && $page->getAuthorID() == User::current()->getID())) { |
||
| 329 | //update page in database |
||
| 330 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `published` = '1' WHERE `id` = :pageID; ", array( |
||
| 331 | 'pageID' => $page->getPageID() |
||
| 332 | )); |
||
| 333 | |||
| 334 | //clear cache |
||
| 335 | $page->clearCache(); |
||
| 336 | |||
| 337 | //reload page from database |
||
| 338 | $page->loadByID($page->getPageID(), false); |
||
| 339 | |||
| 340 | //TODO: remove this line later |
||
| 341 | Cache::clear("pages"); |
||
| 342 | |||
| 343 | return true; |
||
| 344 | } else { |
||
| 345 | return "You don't have the permissions to publish this page!"; |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | protected function showError (string $message) : string { |
||
| 354 | } |
||
| 355 | |||
| 356 | public function getFooterScripts(): string { |
||
| 377 | <script>tinymce.init({ |
||
| 378 | selector: 'textarea', |
||
| 379 | height: 500, |
||
| 380 | theme: 'modern', |
||
| 381 | removed_menuitems: 'newdocument', |
||
| 382 | plugins: 'print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount imagetools contextmenu colorpicker textpattern help', |
||
| 383 | toolbar1: 'formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | removeformat', |
||
| 384 | image_advtab: true |
||
| 385 | }); |
||
| 386 | |||
| 387 | document.getElementById('customTplCheckbox').onchange = function() { |
||
| 388 | document.getElementById('inputTpl').disabled = !this.checked; |
||
| 389 | |||
| 390 | if (!this.checked) { |
||
| 391 | document.getElementById('inputTpl').value = 'none'; |
||
| 392 | } |
||
| 393 | }; |
||
| 394 | |||
| 395 | document.getElementById('customCanoncialsCheckbox').onchange = function() { |
||
| 396 | document.getElementById('inputCanoncials').disabled = !this.checked; |
||
| 397 | |||
| 398 | if (!this.checked) { |
||
| 399 | document.getElementById('inputCanoncials').value = ''; |
||
| 400 | } |
||
| 401 | }; |
||
| 402 | |||
| 403 | document.getElementById('inputSitemap').onchange = function() { |
||
| 404 | document.getElementById('inputSitemapChangeFrequency').disabled = !this.checked; |
||
| 405 | document.getElementById('inputSitemapPriority').disabled = !this.checked; |
||
| 406 | }; |
||
| 407 | </script>"; |
||
| 408 | } |
||
| 409 | |||
| 410 | public function listRequiredPermissions(): array { |
||
| 417 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: