1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Julius Härtl <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Julius Härtl <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\AppOrder\Controller; |
25
|
|
|
|
26
|
|
|
use \OCP\AppFramework\Controller; |
27
|
|
|
use \OCP\AppFramework\Http\TemplateResponse; |
28
|
|
|
use \OCP\IRequest; |
29
|
|
|
use \OCP\INavigationManager; |
30
|
|
|
use \OCA\AppOrder\Service\ConfigService; |
31
|
|
|
use \OCA\AppOrder\Util; |
32
|
|
|
|
33
|
|
|
class SettingsController extends Controller { |
34
|
|
|
|
35
|
|
|
private $userId; |
36
|
|
|
private $appConfig; |
37
|
|
|
private $navigationManager; |
38
|
|
|
private $util; |
39
|
|
|
|
40
|
7 |
|
public function __construct($appName, IRequest $request, ConfigService $appConfig, INavigationManager $urlGenerator, Util $util, $userId) { |
41
|
7 |
|
parent::__construct($appName, $request); |
42
|
7 |
|
$this->userId = $userId; |
43
|
7 |
|
$this->appConfig = $appConfig; |
44
|
7 |
|
$this->navigationManager = $urlGenerator; |
45
|
7 |
|
$this->util = $util; |
46
|
7 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Admin: render admin page |
50
|
|
|
* FIXME: Move to dedicated class |
51
|
|
|
* |
52
|
|
|
* @return TemplateResponse |
53
|
|
|
*/ |
54
|
1 |
|
public function adminIndex() { |
55
|
|
|
// Private API call |
56
|
1 |
|
$navigation = $this->navigationManager->getAll(); |
57
|
1 |
|
$order = json_decode($this->appConfig->getAppValue('order')); |
58
|
1 |
|
if($order === null) $order = array(); |
59
|
1 |
|
$nav = $this->util->matchOrder($navigation, $order); |
60
|
1 |
|
$hidden = json_decode($this->appConfig->getAppValue('hidden')); |
61
|
1 |
|
if($hidden === null) $hidden = array(); |
62
|
1 |
|
$force = json_decode($this->appConfig->getAppValue('force')); |
63
|
1 |
|
if($force === null) $force = false; |
64
|
1 |
|
return new TemplateResponse( |
65
|
1 |
|
$this->appName, |
66
|
1 |
|
'admin', |
67
|
|
|
["nav" => $nav, 'type' => 'admin', 'hidden' => $hidden, 'force' => $force], |
68
|
|
|
'blank' |
69
|
|
|
); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
public function personalIndex() { |
73
|
1 |
|
// Private API call |
74
|
1 |
|
$navigation = $this->navigationManager->getAll(); |
75
|
|
|
$order = json_decode($this->appConfig->getUserValue('order', $this->userId)); |
76
|
|
View Code Duplication |
if($order === null){ |
|
|
|
|
77
|
|
|
$order = json_decode($this->appConfig->getAppValue('order')); |
78
|
1 |
|
if($order === null) $order = array(); |
79
|
1 |
|
} |
80
|
1 |
|
$nav = $this->util->matchOrder($navigation, $order); |
81
|
|
|
$hidden = json_decode($this->appConfig->getUserValue('hidden',$this->userId)); |
82
|
|
View Code Duplication |
if($hidden === null){ |
|
|
|
|
83
|
|
|
$hidden = json_decode($this->appConfig->getAppValue('hidden')); |
84
|
1 |
|
if($hidden === null) $hidden = array(); |
85
|
1 |
|
} |
86
|
1 |
|
$force = json_decode($this->appConfig->getAppValue('force')); |
87
|
1 |
|
if($force === null) $force = false; |
88
|
1 |
|
return new TemplateResponse( |
89
|
|
|
$this->appName, |
90
|
|
|
'admin', |
91
|
|
|
["nav" => $nav, 'type' => 'personal', 'hidden' => $hidden, 'force' => $force], |
92
|
|
|
'blank' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Admin: save default order |
98
|
2 |
|
* |
99
|
2 |
|
* @param $order |
100
|
2 |
|
* @return array response |
101
|
|
|
*/ |
102
|
2 |
|
public function saveDefaultOrder($order) { |
103
|
|
|
if (!is_null($order)) { |
104
|
|
|
$this->appConfig->setAppValue('order', $order); |
105
|
|
|
} |
106
|
|
|
return array('status' => 'success', 'order' => $order); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return order for current user |
111
|
1 |
|
* |
112
|
1 |
|
* @NoAdminRequired |
113
|
1 |
|
* @return array response |
114
|
1 |
|
*/ |
115
|
|
|
public function getOrder() { |
116
|
|
|
$order = $this->util->getAppOrder(); |
117
|
|
|
$hidden = $this->util->getAppHidden(); |
118
|
|
|
return array('status' => 'success', 'order' => $order, 'hidden' => $hidden); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Save order for current user |
123
|
|
|
* |
124
|
2 |
|
* @NoAdminRequired |
125
|
2 |
|
* @param $order string |
126
|
|
|
* @return array response |
127
|
2 |
|
*/ |
128
|
|
View Code Duplication |
public function savePersonal($order) { |
|
|
|
|
129
|
2 |
|
$this->appConfig->setUserValue('order', $this->userId, $order); |
130
|
|
|
$response = array( |
131
|
2 |
|
'status' => 'success', |
132
|
|
|
'data' => array('message' => 'User order saved successfully.'), |
133
|
|
|
'order' => $order |
134
|
|
|
); |
135
|
|
|
return $response; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Save hidden for current user |
140
|
|
|
* |
141
|
|
|
* @NoAdminRequired |
142
|
|
|
* @param $hidden string |
143
|
|
|
* @return array response |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function savePersonalHidden($hidden) { |
|
|
|
|
146
|
|
|
$this->appConfig->setUserValue('hidden', $this->userId, $hidden); |
147
|
|
|
$response = array( |
148
|
|
|
'status' => 'success', |
149
|
|
|
'data' => array('message' => 'User hidden saved successfully.'), |
150
|
|
|
'hidden' => $hidden |
151
|
|
|
); |
152
|
|
|
return $response; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Admin: save default hidden |
157
|
|
|
* |
158
|
|
|
* @param $hidden |
159
|
|
|
* @return array response |
160
|
|
|
*/ |
161
|
|
|
public function saveDefaultHidden($hidden) { |
162
|
|
|
if (!is_null($hidden)) { |
163
|
|
|
$this->appConfig->setAppValue('hidden', $hidden); |
164
|
|
|
} |
165
|
|
|
return array('status' => 'success', 'hidden' => $hidden); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Admin: save force value |
170
|
|
|
* |
171
|
|
|
* @param $force |
172
|
|
|
* @return array response |
173
|
|
|
*/ |
174
|
|
|
public function saveDefaultForce($force) { |
175
|
|
|
if (!is_null($force)) { |
176
|
|
|
$this->appConfig->setAppValue('force', $force); |
177
|
|
|
} |
178
|
|
|
return array('status' => 'success', 'force' => $force); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.