|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ownCloud - Calendar App |
|
4
|
|
|
* |
|
5
|
|
|
* @author Georg Ehrke |
|
6
|
|
|
* @copyright 2016 Georg Ehrke <[email protected]> |
|
7
|
|
|
* @author Raghu Nayyar |
|
8
|
|
|
* @copyright 2016 Raghu Nayyar <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* This library is free software; you can redistribute it and/or |
|
11
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|
12
|
|
|
* License as published by the Free Software Foundation; either |
|
13
|
|
|
* version 3 of the License, or any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This library is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public |
|
21
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
namespace OCA\Calendar\Controller; |
|
25
|
|
|
|
|
26
|
|
|
use OC\AppFramework\Http; |
|
27
|
|
|
use OCP\AppFramework\Controller; |
|
28
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse; |
|
29
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
30
|
|
|
use OCP\AppFramework\Http\NotFoundResponse; |
|
31
|
|
|
use OCP\AppFramework\Http\TemplateResponse; |
|
32
|
|
|
use OCP\IConfig; |
|
33
|
|
|
use OCP\IRequest; |
|
34
|
|
|
use OCP\IUserSession; |
|
35
|
|
|
|
|
36
|
|
|
class ViewController extends Controller { |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var IConfig |
|
40
|
|
|
*/ |
|
41
|
|
|
private $config; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var IUserSession |
|
45
|
|
|
*/ |
|
46
|
|
|
private $userSession; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $appName |
|
50
|
|
|
* @param IRequest $request an instance of the request |
|
51
|
|
|
* @param IConfig $config |
|
52
|
|
|
* @param IUserSession $userSession |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct($appName, IRequest $request, |
|
55
|
|
|
IUserSession $userSession, IConfig $config) { |
|
56
|
|
|
parent::__construct($appName, $request); |
|
57
|
|
|
$this->config = $config; |
|
58
|
|
|
$this->userSession = $userSession; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @NoAdminRequired |
|
63
|
|
|
* @NoCSRFRequired |
|
64
|
|
|
* |
|
65
|
|
|
* @return TemplateResponse |
|
66
|
|
|
*/ |
|
67
|
|
|
public function index() { |
|
68
|
|
|
$userId = $this->userSession->getUser()->getUID(); |
|
69
|
|
|
|
|
70
|
|
|
$appVersion = $this->config->getAppValue($this->appName, 'installed_version'); |
|
71
|
|
|
$defaultView = $this->config->getUserValue($userId, $this->appName, 'currentView', 'month'); |
|
72
|
|
|
$emailAddress = $this->config->getUserValue($userId, 'settings', 'email'); |
|
73
|
|
|
|
|
74
|
|
|
return new TemplateResponse('calendar', 'main', [ |
|
75
|
|
|
'appVersion' => $appVersion, |
|
76
|
|
|
'defaultView' => $defaultView, |
|
77
|
|
|
'emailAddress' => $emailAddress, |
|
78
|
|
|
]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @NoAdminRequired |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $id |
|
85
|
|
|
* @return DataDisplayResponse |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getTimezone($id) { |
|
88
|
|
|
if (!in_array($id, $this->getTimezoneList())) { |
|
89
|
|
|
return new NotFoundResponse(); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id); |
|
93
|
|
|
|
|
94
|
|
|
return new DataDisplayResponse($tzData, HTTP::STATUS_OK, [ |
|
95
|
|
|
'content-type' => 'text/calendar', |
|
96
|
|
|
]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @NoAdminRequired |
|
102
|
|
|
* |
|
103
|
|
|
* @param $region |
|
104
|
|
|
* @param $city |
|
105
|
|
|
* @return DataDisplayResponse |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getTimezoneWithRegion($region, $city) { |
|
108
|
|
|
return $this->getTimezone($region . '-' . $city); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @NoAdminRequired |
|
114
|
|
|
* |
|
115
|
|
|
* @param $region |
|
116
|
|
|
* @param $subregion |
|
117
|
|
|
* @param $city |
|
118
|
|
|
* @return DataDisplayResponse |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getTimezoneWithSubRegion($region, $subregion, $city) { |
|
121
|
|
|
return $this->getTimezone($region . '-' . $subregion . '-' . $city); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* get a list of default timezones |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
private function getTimezoneList() { |
|
131
|
|
|
$allFiles = scandir(__DIR__ . '/../timezones/'); |
|
132
|
|
|
|
|
133
|
|
|
return array_values(array_filter($allFiles, function($file) { |
|
134
|
|
|
return (substr($file, -4) === '.ics'); |
|
135
|
|
|
})); |
|
136
|
|
|
} |
|
137
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.