PageListPage::getContent()   F
last analyzed

Complexity

Conditions 34
Paths > 20000

Size

Total Lines 158
Code Lines 94

Duplication

Lines 0
Ratio 0 %

Importance

Changes 25
Bugs 3 Features 11
Metric Value
cc 34
eloc 94
c 25
b 3
f 11
nc 27936
nop 0
dl 0
loc 158
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
20
/**
21
 * Project: RocketCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 31.08.2018
25
 * Time: 22:40
26
 */
27
28
class PageListPage extends PageType {
29
30
	public function getContent(): string {
31
		$template = new DwooTemplate("pages/pagelist");
32
33
		$template->assign("no_edit_permissions", (boolean) (!PermissionChecker::current()->hasRight("an_edit_all_pages") && !PermissionChecker::current()->hasRight("can_edit_own_pages")));
34
35
		//set table columns
36
		$template->assign("columns", array(
37
			Translator::translate("ID"),
38
			Translator::translate("Alias"),
39
			Translator::translate("Title"),
40
			Translator::translate("Author"),
41
			Translator::translate("State"),
42
			Translator::translate("Actions")
43
		));
44
45
		//get permissions
46
		$current_userID = User::current()->getID();
47
		$permission_can_edit_all_pages = PermissionChecker::current()->hasRight("can_edit_all_pages");
48
		$permission_can_edit_own_pages = PermissionChecker::current()->hasRight("can_edit_own_pages");
49
		$permission_can_unlock_all_pages = PermissionChecker::current()->hasRight("can_unlock_all_pages");
50
		$permission_can_delete_own_pages = PermissionChecker::current()->hasRight("can_delete_own_pages");
51
		$permission_can_delete_all_pages = PermissionChecker::current()->hasRight("can_delete_all_pages");
52
		$permission_can_see_trash_pages = PermissionChecker::current()->hasRight("can_see_trash_pages");
53
		$permission_can_restore_trash_pages = PermissionChecker::current()->hasRight("can_restore_trash_pages");
54
		$permission_can_delete_all_pages_permanently = PermissionChecker::current()->hasRight("can_delete_all_pages_permanently");
55
56
		$success_messages = array();
57
58
		//unlock pages
59
		if (isset($_REQUEST['unlock']) && $permission_can_unlock_all_pages) {
60
			$pageID = (int) $_REQUEST['unlock'];
61
			Page::unlockPage($pageID);
62
63
			$success_messages[] = "Unlocked page successfully!";
64
		}
65
66
		//move pages to trash
67
		if (isset($_REQUEST['trash']) && is_numeric($_REQUEST['trash']) && ($permission_can_delete_own_pages || $permission_can_delete_all_pages)) {
68
			//move page to trash
69
			$pageID = (int) $_REQUEST['trash'];
70
71
			//load page
72
			$page = new Page();
73
			$page->loadByID($pageID);
74
75
			//check permisssion
76
			if ($permission_can_delete_all_pages || ($permission_can_delete_own_pages && $page->getAuthorID() == User::current()->getID())) {
77
				//check, if page is deletable
78
				if ($page->isDeletable()) {
79
					//move page to trash
80
					$page->moveToTrash();
81
82
					$success_messages[] = "Moved page '" . $page->getAlias() . "' to trash.";
83
				}
84
			}
85
		}
86
87
		//restore pages from trash
88
		if (isset($_REQUEST['restore']) && is_numeric($_REQUEST['restore']) && $permission_can_restore_trash_pages) {
89
			//restore page
90
			$pageID = (int) $_REQUEST['restore'];
91
92
			//load page
93
			$page = new Page();
94
			$page->loadByID($pageID);
95
96
			if ($page->isTrash()) {
97
				$page->restore();
98
99
				$success_messages[] = "Restored page '" . $page->getAlias() . "' successfully!";
100
			}
101
		}
102
103
		//delete pages from trash
104
		if (isset($_REQUEST['delete_permanently']) && is_numeric($_REQUEST['delete_permanently']) && $permission_can_delete_all_pages_permanently) {
105
			$pageID = (int) $_REQUEST['delete_permanently'];
106
107
			//load page
108
			$page = new Page();
109
			$page->loadByID($pageID);
110
111
			//check, if page is in trash
112
			if ($page->isTrash()) {
113
				Page::deleteByID($page->getPageID());
114
115
				$success_messages[] = "Deleted page '" . $page->getAlias() . "' permanently successful!";
116
			}
117
		}
118
119
		$show_trash = false;
120
121
		//show pages in trash
122
		if (isset($_REQUEST['show_trash']) && $permission_can_see_trash_pages) {
123
			$show_trash = true;
124
		}
125
126
		$template->assign("show_trash", $show_trash);
127
		$template->assign("page_url", DomainUtils::generateURL($this->getPage()->getAlias()));
128
129
		$pages = array();
130
131
		if (!$show_trash) {
132
			//count pages in trash
133
			$row = Database::getInstance()->getRow("SELECT COUNT(*) FROM `{praefix}pages` WHERE `activated` = 2; ");
134
135
			$number_of_pages_in_trash = (int) $row['COUNT(*)'];
136
			$template->assign("pages_in_trash", $number_of_pages_in_trash);
137
			$template->assign("trash_url", DomainUtils::generateURL($this->getPage()->getAlias(), array("show_trash" => 1)));
138
		}
139
140
		//get all pages from database
141
		$rows = Database::getInstance()->listRows("SELECT *, `{praefix}pages`.`activated` as `activated` FROM `{praefix}pages` LEFT JOIN `{praefix}user` ON (`{praefix}pages`.`author` = `{praefix}user`.`userID`) WHERE `{praefix}pages`.`editable` = '1' AND `{praefix}pages`.`activated` = :activated; ", array(
142
			'activated' => (!$show_trash ? 1 : 2)
143
		));
144
145
		foreach ($rows as $row) {
146
			$is_author_online = $row['online'] == 1;
147
			$is_own_page = $row['author'] == $current_userID;
148
			$editable = $permission_can_edit_all_pages || ($permission_can_edit_own_pages && $is_own_page);
149
			$is_trash = $row['activated'] == 2;
150
151
			$pages[] = array(
152
				'id' => $row['id'],
153
				'alias' => $row['alias'],
154
				'title' => Translator::translateTitle($row['title']),
155
				'author' => $row['username'],
156
				'state' => ($row['published'] == 1 ? "Published" : "Draft"),
157
				'actions' => "&nbsp;",
158
				'user_online' => (boolean) $is_author_online,
159
				'url' => DomainUtils::generateURL($row['alias']),
160
				'own_page' => (boolean) $is_own_page,
161
				'editable' => (boolean) $editable,
162
				'published' => $row['published'] == 1,
163
				'locked' => $row['locked_by'] != -1,
164
				'locked_user' => $row['locked_by'],
165
				'locked_by_me' => $row['locked_by'] == User::current()->getID(),
166
				'locked_timestamp' => $row['locked_timestamp'],
167
				'unlock_url' => DomainUtils::generateURL($this->getPage()->getAlias(), array("unlock" => $row['id'])),
168
				'can_edit' => ($permission_can_edit_all_pages || ($permission_can_edit_own_pages && $is_own_page)) && $row['editable'] == 1,
169
				'edit_url' => DomainUtils::generateURL("admin/edit_page", array("edit" => $row['id'])),
170
				'can_delete' => ($permission_can_delete_all_pages || ($permission_can_delete_own_pages && $is_own_page)) && $row['deletable'] == 1,
171
				'delete_url' => DomainUtils::generateURL($this->getPage()->getAlias(), array("trash" => $row['id'])),
172
				'is_in_trash' => (boolean) $is_trash,
173
				'restore_url' => DomainUtils::generateURL($this->getPage()->getAlias(), array("restore" => $row['id'])),
174
				'delete_permanently_url' => DomainUtils::generateURL($this->getPage()->getAlias(), array("delete_permanently" => $row['id'], "show_trash" => 1)),
175
				'preview_url' => DomainUtils::generateURL($row['alias'], array("preview" => "true"))
176
			);
177
		}
178
179
		$template->assign("permission_can_unlock_all_pages", $permission_can_unlock_all_pages);
180
		$template->assign("permission_can_restore_trash_pages", $permission_can_restore_trash_pages);
181
		$template->assign("permission_can_delete_all_pages_permanently", $permission_can_delete_all_pages_permanently);
182
183
		$template->assign("success_messages", $success_messages);
184
185
		$template->assign("pagelist", $pages);
186
187
		return $template->getCode();
188
	}
189
190
	public function getFooterScripts(): string {
191
		return "<script>
192
		  $(function () {
193
			$('#pagetable').DataTable({
194
			  'paging'      : true,
195
			  'lengthChange': false,
196
			  'searching'   : true,
197
			  'ordering'    : true,
198
			  'info'        : true,
199
			  'autoWidth'   : false
200
			});
201
		  });
202
		</script>";
203
	}
204
205
	public function listRequiredPermissions(): array {
206
		return array("can_see_all_pages", "can_edit_all_pages");
207
	}
208
209
}
210
211
?>
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...
212