1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Joas Schilling <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Activity; |
23
|
|
|
|
24
|
|
|
use OCA\Activity\Parameter\Factory; |
25
|
|
|
use OCA\Activity\Parameter\IParameter; |
26
|
|
|
use OCA\Activity\Parameter\Collection; |
27
|
|
|
use OCP\Activity\IEvent; |
28
|
|
|
use OCP\Activity\IManager; |
29
|
|
|
use OCP\IL10N; |
30
|
|
|
use OCP\L10N\IFactory; |
31
|
|
|
|
32
|
|
|
class DataHelper { |
33
|
|
|
/** @var \OCP\Activity\IManager */ |
34
|
|
|
protected $activityManager; |
35
|
|
|
|
36
|
|
|
/** @var \OCA\Activity\Parameter\Factory */ |
37
|
|
|
protected $parameterFactory; |
38
|
|
|
|
39
|
|
|
/** @var IFactory */ |
40
|
|
|
protected $l10Nfactory; |
41
|
|
|
|
42
|
|
|
/** @var IL10N */ |
43
|
|
|
protected $l; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param IManager $activityManager |
47
|
|
|
* @param Factory $parameterFactory |
48
|
|
|
* @param IFactory $l10Nfactory |
49
|
|
|
* @param IL10N $l |
50
|
|
|
*/ |
51
|
35 |
|
public function __construct(IManager $activityManager, Factory $parameterFactory, IFactory $l10Nfactory, IL10N $l) { |
52
|
35 |
|
$this->activityManager = $activityManager; |
53
|
35 |
|
$this->parameterFactory = $parameterFactory; |
54
|
35 |
|
$this->l10Nfactory = $l10Nfactory; |
55
|
35 |
|
$this->l = $l; |
56
|
35 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $user |
60
|
|
|
*/ |
61
|
6 |
|
public function setUser($user) { |
62
|
6 |
|
$this->parameterFactory->setUser($user); |
63
|
6 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param IL10N $l |
67
|
|
|
*/ |
68
|
1 |
|
public function setL10n(IL10N $l) { |
69
|
1 |
|
$this->parameterFactory->setL10n($l); |
70
|
1 |
|
$this->l = $l; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @brief Translate an event string with the translations from the app where it was send from |
75
|
|
|
* @param string $app The app where this event comes from |
76
|
|
|
* @param string $text The text including placeholders |
77
|
|
|
* @param IParameter[] $params The parameter for the placeholder |
78
|
|
|
* @return string translated |
79
|
|
|
*/ |
80
|
10 |
|
public function translation($app, $text, array $params) { |
81
|
10 |
|
if (!$text) { |
82
|
5 |
|
return ''; |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
$preparedParams = []; |
86
|
9 |
|
foreach ($params as $parameter) { |
87
|
6 |
|
$preparedParams[] = $parameter->format(); |
88
|
9 |
|
} |
89
|
|
|
|
90
|
|
|
// Allow apps to correctly translate their activities |
91
|
9 |
|
$translation = $this->activityManager->translate( |
92
|
9 |
|
$app, $text, $preparedParams, false, false, $this->l->getLanguageCode()); |
93
|
|
|
|
94
|
9 |
|
if ($translation !== false) { |
95
|
6 |
|
return $translation; |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
$l = $this->l10Nfactory->get($app, $this->l->getLanguageCode()); |
99
|
3 |
|
return $l->t($text, $preparedParams); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* List with special parameters for the message |
104
|
|
|
* |
105
|
|
|
* @param string $app |
106
|
|
|
* @param string $text |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
7 |
|
protected function getSpecialParameterList($app, $text) { |
110
|
7 |
|
$specialParameters = $this->activityManager->getSpecialParameterList($app, $text); |
111
|
|
|
|
112
|
7 |
|
if ($specialParameters !== false) { |
113
|
6 |
|
return $specialParameters; |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
return array(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Format strings for display |
121
|
|
|
* |
122
|
|
|
* @param array $activity |
123
|
|
|
* @param string $message 'subject' or 'message' |
124
|
|
|
* @return array Modified $activity |
125
|
|
|
*/ |
126
|
6 |
|
public function formatStrings($activity, $message) { |
127
|
6 |
|
$activity[$message . 'params'] = $activity[$message . 'params_array']; |
128
|
6 |
|
unset($activity[$message . 'params_array']); |
129
|
6 |
|
$activity[$message . '_prepared'] = $this->translation($activity['app'], $activity[$message], $activity[$message . 'params']); |
130
|
|
|
|
131
|
6 |
|
return $activity; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the parameter array from the parameter string of the database table |
136
|
|
|
* |
137
|
|
|
* @param IEvent $event |
138
|
|
|
* @param string $parsing What are we parsing `message` or `subject` |
139
|
|
|
* @param string $parameterString can be a JSON string, serialize() or a simple string. |
140
|
|
|
* @return array List of Parameters |
141
|
|
|
*/ |
142
|
8 |
|
public function getParameters(IEvent $event, $parsing, $parameterString) { |
143
|
8 |
|
$parameters = $this->parseParameters($parameterString); |
144
|
8 |
|
$parameterTypes = $this->getSpecialParameterList( |
145
|
8 |
|
$event->getApp(), |
146
|
8 |
|
($parsing === 'subject') ? $event->getSubject() : $event->getMessage() |
147
|
8 |
|
); |
148
|
|
|
|
149
|
8 |
|
foreach ($parameters as $i => $parameter) { |
150
|
7 |
|
$parameters[$i] = $this->parameterFactory->get( |
151
|
7 |
|
$parameter, |
152
|
7 |
|
$event, |
153
|
7 |
|
isset($parameterTypes[$i]) ? $parameterTypes[$i] : 'base' |
154
|
7 |
|
); |
155
|
8 |
|
} |
156
|
|
|
|
157
|
8 |
|
return $parameters; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return Collection |
162
|
|
|
*/ |
163
|
1 |
|
public function createCollection() { |
164
|
1 |
|
return $this->parameterFactory->createCollection(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the parameter array from the parameter string of the database table |
169
|
|
|
* |
170
|
|
|
* @param string $parameterString can be a JSON string, serialize() or a simple string. |
171
|
|
|
* @return array List of Parameters |
172
|
|
|
*/ |
173
|
11 |
|
public function parseParameters($parameterString) { |
174
|
11 |
|
if (!is_string($parameterString)) { |
175
|
1 |
|
return []; |
176
|
|
|
} |
177
|
10 |
|
$parameters = $parameterString; |
178
|
|
|
|
179
|
10 |
|
if ($parameterString[0] === '[' && substr($parameterString, -1) === ']' || $parameterString[0] === '"' && substr($parameterString, -1) === '"') { |
180
|
|
|
// ownCloud 8.1+ |
181
|
7 |
|
$parameters = json_decode($parameterString, true); |
182
|
|
|
|
183
|
|
|
/* |
184
|
|
|
* Remove gid from the parameters array |
185
|
|
|
* While displaying in the UI we want only |
186
|
|
|
* displayName. Convert the array to string. |
187
|
|
|
*/ |
188
|
7 |
|
if((count($parameters) > 1) && is_array($parameters[1])) { |
189
|
1 |
|
if(isset($parameters[1]['gid'])) { |
190
|
1 |
|
unset($parameters[1]['gid']); |
191
|
1 |
|
if(isset($parameters[1]['displayName'])) { |
192
|
1 |
|
$parameters[1] = $parameters[1]['displayName']; |
193
|
1 |
|
} |
194
|
1 |
|
} |
195
|
1 |
|
} |
196
|
|
|
|
197
|
7 |
|
if ($parameters === null) { |
198
|
|
|
// Error on json decode |
199
|
1 |
|
$parameters = $parameterString; |
200
|
1 |
|
} |
201
|
|
|
|
202
|
10 |
|
} else if (isset($parameterString[7]) && $parameterString[1] === ':' && ($parameterString[0] === 's' && substr($parameterString, -1) === ';' || $parameterString[0] === 'a' && substr($parameterString, -1) === '}')) { |
203
|
|
|
// ownCloud 7+ |
204
|
|
|
// Min length 8: `s:1:"a";` |
205
|
|
|
// Accepts: `s:1:"a";` for single string `a:1:{i:0;s:1:"a";}` for array |
206
|
2 |
|
$parameters = unserialize($parameterString); |
207
|
2 |
|
} |
208
|
|
|
|
209
|
10 |
|
if (is_array($parameters)) { |
210
|
7 |
|
return $parameters; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// ownCloud <7 |
214
|
3 |
|
return [$parameters]; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|