Completed
Push — master ( 8c2e26...e49b46 )
by Justin
03:16
created

PageListPage::getFooterScripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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
		$pages = array();
127
128
		//get all pages from database
129
		$rows = Database::getInstance()->listRows("SELECT * 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(
130
			'activated' => (!$show_trash ? 1 : 2)
131
		));
132
133
		foreach ($rows as $row) {
134
			$is_author_online = $row['online'] == 1;
135
			$is_own_page = $row['author'] == $current_userID;
136
			$editable = $permission_can_edit_all_pages || ($permission_can_edit_own_pages && $is_own_page);
137
138
			$pages[] = array(
139
				'id' => $row['id'],
140
				'alias' => $row['alias'],
141
				'title' => Translator::translateTitle($row['title']),
142
				'author' => $row['username'],
143
				'state' => ($row['published'] == 1 ? "Published" : "Draft"),
144
				'actions' => "&nbsp;",
145
				'user_online' => (boolean) $is_author_online,
146
				'url' => DomainUtils::generateURL($row['alias']),
147
				'own_page' => (boolean) $is_own_page,
148
				'editable' => (boolean) $editable,
149
				'published' => $row['published'] == 1,
150
				'locked' => $row['locked_by'] != -1,
151
				'locked_user' => $row['locked_by'],
152
				'locked_timestamp' => $row['locked_timestamp'],
153
				'unlock_url' => DomainUtils::generateURL($this->getPage()->getAlias(), array("unlock" => $row['id'])),
154
				'can_edit' => ($permission_can_edit_all_pages || ($permission_can_edit_own_pages && $is_own_page)) && $row['editable'] == 1,
155
				'edit_url' => DomainUtils::generateURL("admin/edit_page", array("edit" => $row['id'])),
156
				'can_delete' => ($permission_can_delete_all_pages || ($permission_can_delete_own_pages && $is_own_page)) && $row['deletable'] == 1,
157
				'delete_url' => DomainUtils::generateURL($this->getPage()->getAlias(), array("trash" => $row['id'])),
158
				'is_trash' => $row['activated'] == 2,
159
				'restore_url' => DomainUtils::generateURL($this->getPage()->getAlias(), array("restore" => $row['id'])),
160
				'delete_permanently_url' => DomainUtils::generateURL($this->getPage()->getAlias(), array("delete_permanently" => $row['id']))
161
			);
162
		}
163
164
		$template->assign("permission_can_unlock_all_pages", $permission_can_unlock_all_pages);
165
		$template->assign("permission_can_restore_trash_pages", $permission_can_restore_trash_pages);
166
		$template->assign("permission_can_delete_all_pages_permanently", $permission_can_delete_all_pages_permanently);
167
168
		$template->assign("success_messages", $success_messages);
169
170
		$template->assign("pagelist", $pages);
171
172
		return $template->getCode();
173
	}
174
175
	public function getFooterScripts(): string {
176
		return "<script>
177
		  $(function () {
178
			$('#pagetable').DataTable({
179
			  'paging'      : true,
180
			  'lengthChange': false,
181
			  'searching'   : true,
182
			  'ordering'    : true,
183
			  'info'        : true,
184
			  'autoWidth'   : false
185
			});
186
		  });
187
		</script>";
188
	}
189
190
	public function listRequiredPermissions(): array {
191
		return array("can_see_all_pages", "can_edit_all_pages");
192
	}
193
194
}
195
196
?>
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...
197