Calenders::listCalenderRowsByGroups()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 4
nc 8
nop 1
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: 26.04.2018
25
 * Time: 21:26
26
 */
27
28
namespace Plugin\Calender;
29
30
use Cache;
31
use Database;
32
use Groups;
33
use User;
34
use PDO;
35
36
class Calenders {
37
38
	public static function listMyCalenderIDs (int $userUD) : array {
39
		if (Cache::contains("plugin-calender", "calenderIDs-" . $userUD)) {
40
			return Cache::get("plugin-calender", "calenderIDs-" . $userUD);
41
		} else {
42
			$groups = new Groups();
43
			$groups->loadMyGroups(User::current()->getID());
44
			$groupIDs = $groups->listGroupIDs();
45
46
			//get calenders by groups
47
			$calender_ids = self::listCalenderRowsByGroups($groupIDs);
48
49
			//merge arrays
50
			foreach (self::listCalenderRowsByUser(User::current()->getID()) as $calenderID=>$row) {
51
				if (isset($calender_ids[$calender_ids])) {
52
					//check, which is higher permission
53
					if (self::valueToInt($calender_ids[$calenderID]['value']) < self::valueToInt($row['value'])) {
54
						$calender_ids[$calenderID] = $row;
55
					}
56
				} else {
57
					$calender_ids[$calenderID] = $row;
58
				}
59
			}
60
61
			//put results to cache
62
			Cache::put("plugin-calender", "calenderIDs-" . $userUD, $calender_ids);
63
64
			return $calender_ids;
65
		}
66
	}
67
68
	public static function listMyCalenders (int $userID) : array {
69
		if (Cache::contains("plugin-calender", "my-calenders-" . $userID)) {
70
			return Cache::get("plugin-calender", "my-calenders-" . $userID);
71
		} else {
72
			$array = array();
73
74
			$array1 = array();
75
			$user_rows = array();
76
77
			foreach (self::listMyCalenderIDs($userID) as $calenderID=>$row) {
78
				$array1[] = "`id` = '" . intval($calenderID) . "'";
79
				$user_rows[$calenderID] = $row;
80
			}
81
82
			$array_str = (!empty($array1) ? " OR " : "") . implode(" OR ", $array1);
83
84
			$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}plugin_calender_calenders` WHERE `id` = '-1'" . $array_str . "; ");
85
86
			foreach ($rows as $row) {
87
				$calenderID = $row['id'];
88
89
				//create new calender
90
				$calender = new Calender($row, $user_rows[$calenderID]);
91
92
				$array[] = $calender;
93
			}
94
95
			//put array to cache
96
			Cache::put("plugin-calender", "my-calenders-" . $userID, $array);
97
98
			return $array;
99
		}
100
	}
101
102
	/**
103
	 * list calender ids in this form calenderID=>value
104
	 *
105
	 * Dont use this method directly, because its not cached!
106
	 *
107
	 * @param $groupIDs array with ids of groups, user belongs to
108
	 *
109
	 * @return array with calender IDs in form calenderID=>value
110
	 */
111
	protected static function listCalenderRowsByGroups (array $groupIDs) : array {
112
		$array = array();
113
114
		foreach ($groupIDs as $id) {
115
			$array[] = "`groupID` = '" . intval($id) . "'";
116
		}
117
118
		$array_str = (!empty($array) ? " OR " : "") . implode(" OR ", $array);
119
120
		$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}plugin_calender_group_rights` WHERE `groupID` = '-1'" . $array_str . "; ");
121
122
		$res_array = array();
123
124
		foreach ($rows as $row) {
125
			$calenderID = $row['calenderID'];
126
			$res_array[$calenderID] = $row;
127
		}
128
129
		return $res_array;
130
	}
131
132
	protected static function listCalenderRowsByUser (int $userID) : array {
133
		if (Cache::contains("plugin-calender", "calender-ids-by-user-" . $userID)) {
134
			return Cache::get("plugin-calender", "calender-ids-by-user-" . $userID);
135
		} else {
136
			$array = array();
137
138
			//get calenders from database
139
			$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}plugin_calender_user_rights` WHERE `userID` = :userID; ", array(
140
				'userID' => array(
141
					'type' => PDO::PARAM_INT,
142
					'value' => $userID
143
				)
144
			));
145
146
			foreach ($rows as $row) {
147
				$calenderID = $row['calenderID'];
148
149
				$array[$calenderID] = $row;
150
			}
151
152
			//cache results
153
			Cache::put("plugin-calender", "calender-ids-by-user-" . $userID, $array);
154
155
			return $array;
156
		}
157
	}
158
159
	public static function valueToInt (string $value) : int {
160
		switch ($value) {
161
			case "read":
162
				return 1;
163
			case "write":
164
				return 2;
165
			case "owner":
166
				return 3;
167
			default:
168
				throw new \IllegalArgumentException("Unknown calender value '" . $value . "'!");
169
		}
170
	}
171
172
}
173
174
?>
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...
175