1 | <?php |
||
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) { |
|
58 | |||
59 | /** |
||
60 | * @param string $user |
||
61 | */ |
||
62 | 6 | public function setUser($user) { |
|
65 | |||
66 | /** |
||
67 | * @param IL10N $l |
||
68 | */ |
||
69 | 5 | public function setL10n(IL10N $l) { |
|
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) { |
|
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) { |
|
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() { |
|
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) { |
|
202 | } |
||
203 |