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