Issues (762)

plugins/calender/classes/calender.php (1 issue)

Severity
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:23
26
 */
27
28
namespace Plugin\Calender;
29
30
use Cache;
31
use Database;
32
use PDO;
33
use IllegalStateException;
34
35
class Calender {
36
37
	protected $calenderID = -1;
38
	protected $row = null;
39
	protected $user_row = null;
40
41
	public function __construct (array $row = null, array $user_row = null) {
42
		if (!is_null($row) && !empty($row)) {
43
			$this->calenderID = $row['id'];
44
			$this->row = $row;
45
		}
46
47
		if (!is_null($user_row) && !empty($user_row)) {
48
			$this->user_row = $user_row;
49
		}
50
	}
51
52
	public function load (int $calenderID) {
53
		if ($calenderID <= 0) {
54
			throw new \IllegalArgumentException("calenderID has to be greater than 0.");
55
		}
56
57
		if (Cache::contains("plugin-calender", "calender-" . $calenderID)) {
58
			$this->row = Cache::get("plugin-calender", "calender-" . $calenderID);
59
		} else {
60
			//get calender from database
61
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}plugin_calender_calenders` WHERE `id` = :calenderID; ", array(
62
				'calenderID' => array(
63
					'type' => PDO::PARAM_INT,
64
					'value' => $calenderID
65
				)
66
			));
67
68
			if (!$row) {
69
				throw new IllegalStateException("calender with id '" . $calenderID . "' doesnt exists.");
70
			}
71
72
			$this->row = $row;
73
74
			//put row to cache
75
			Cache::put("plugin-calender", "calender-" . $calenderID, $row);
76
		}
77
78
		$this->calenderID = $this->row['id'];
79
	}
80
81
	public function getID () : int {
82
		return $this->calenderID;
83
	}
84
85
	public function getTitle () : string {
86
		return $this->row['title'];
87
	}
88
89
	public function getDescription () : string {
90
		return $this->row['description'];
91
	}
92
93
	public function getType () : string {
94
		return $this->row['type'];
95
	}
96
97
	public function getPermission () : string {
98
		if ($this->user_row == null || empty($this->user_row)) {
99
			throw new IllegalStateException("user row wasnt set in constructor.");
100
		}
101
102
		return $this->user_row['value'];
103
	}
104
105
	public function listAllEvents (bool $only_current_events = false) : array {
106
		//read from database, dont cache
107
		$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}plugin_calender_events` WHERE `calenderID` = :calenderID" . ($only_current_events ? " AND DATE(`to_date`) >= DATE(NOW())" : "") . " ORDER BY `from_date`; ", array(
108
			'calenderID' => array(
109
				'type' => PDO::PARAM_INT,
110
				'value' => $this->getID()
111
			)
112
		));
113
114
		$events = array();
115
116
		foreach ($rows as $row) {
117
			$events[] = new Event($row);
118
		}
119
120
		return $events;
121
	}
122
123
	public static function castCalender (Calender $calender) : Calender {
124
		return $calender;
125
	}
126
127
}
128
129
?>
0 ignored issues
show
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...
130