Activity::translate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 6
crap 20
1
<?php
2
/**
3
 * ownCloud - Files_antivirus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Viktar Dubiniuk <[email protected]>
9
 *
10
 * @copyright Viktar Dubiniuk 2015-2018
11
 * @license AGPL-3.0
12
 */
13
14
namespace OCA\Files_Antivirus;
15
16
use OC\L10N\Factory;
17
use OCP\Activity\IExtension;
18
use OCP\IURLGenerator;
19
20
class Activity implements IExtension {
21
	const APP_NAME = 'files_antivirus';
22
23
	/**
24
	 * Filter with all app related activities
25
	 */
26
	const FILTER_AVIR = 'files_antivirus';
27
28
	/**
29
	 * Activity types known to this extension
30
	 */
31
	const TYPE_VIRUS_DETECTED = 'virus_detected';
32
33
	/**
34
	 * Subject keys for translation of the subjections
35
	 */
36
	const SUBJECT_VIRUS_DETECTED = 'virus_detected';
37
	
38
	/**
39
	 * Infected file deletion notice
40
	 */
41
	const MESSAGE_FILE_DELETED = 'file_deleted';
42
43
	/**
44
	 * @var Factory
45
	 */
46
	protected $languageFactory;
47
48
	/**
49
	 * @var IURLGenerator
50
	 */
51
	protected $URLGenerator;
52
53
	/**
54
	 * @param Factory $languageFactory
55
	 * @param IURLGenerator $URLGenerator
56
	 */
57 3
	public function __construct(Factory $languageFactory, IURLGenerator $URLGenerator) {
58 3
		$this->languageFactory = $languageFactory;
59 3
		$this->URLGenerator = $URLGenerator;
60 3
	}
61
62
	protected function getL10N($languageCode = null) {
63
		return $this->languageFactory->get(self::APP_NAME, $languageCode);
64
	}
65
66
	/**
67
	 * The extension can return an array of additional notification types.
68
	 * If no additional types are to be added false is to be returned
69
	 *
70
	 * @param string $languageCode
71
	 *
72
	 * @return array|false
73
	 */
74
	public function getNotificationTypes($languageCode) {
75
		$l = $this->getL10N($languageCode);
76
77
		return [
78
			self::TYPE_VIRUS_DETECTED => (string) $l->t(
79
				'<strong>Infected file</strong> has been <strong>found</strong>'
80
			),
81
		];
82
	}
83
84
	/**
85
	 * For a given method additional types to be displayed in the settings can be returned.
86
	 * In case no additional types are to be added false is to be returned.
87
	 *
88
	 * @param string $method
89
	 * @return array|false
90
	 */
91
	public function getDefaultTypes($method) {
92
		$defaultTypes = [
93
			self::TYPE_VIRUS_DETECTED,
94
		];
95
96
		return $defaultTypes;
97
	}
98
99
	/**
100
	 * A string naming the css class for the icon to be used can be returned.
101
	 * If no icon is known for the given type false is to be returned.
102
	 *
103
	 * @param string $type
104
	 * @return string|false
105
	 */
106 1
	public function getTypeIcon($type) {
107
		switch ($type) {
108 1
			case self::TYPE_VIRUS_DETECTED:
109 1
				return 'icon-info';
110
		}
111 1
		return false;
112
	}
113
114
	/**
115
	 * The extension can translate a given message to the requested languages.
116
	 * If no translation is available false is to be returned.
117
	 *
118
	 * @param string $app
119
	 * @param string $text
120
	 * @param array $params
121
	 * @param boolean $stripPath
122
	 * @param boolean $highlightParams
123
	 * @param string $languageCode
124
	 *
125
	 * @return string|false
126
	 */
127
	public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
128
		$l = $this->getL10N($languageCode);
129
130
		if ($app === self::APP_NAME) {
131
			switch ($text) {
132
				case self::SUBJECT_VIRUS_DETECTED:
133
					return (string) $l->t('File %s is infected with %s', $params);
134
				case self::MESSAGE_FILE_DELETED:
135
					return (string) $l->t('It is going to be deleted', $params);
136
			}
137
		}
138
139
		return false;
140
	}
141
142
	/**
143
	 * The extension can define the type of parameters for translation
144
	 *
145
	 * Currently known types are:
146
	 * * file		=> will strip away the path of the file and add a tooltip with it
147
	 * * username	=> will add the avatar of the user
148
	 *
149
	 * @param string $app
150
	 * @param string $text
151
	 *
152
	 * @return array|false
153
	 */
154 1
	public function getSpecialParameterList($app, $text) {
155 1
		return false;
156
	}
157
158
	/**
159
	 * The extension can define the parameter grouping by returning the index as integer.
160
	 * In case no grouping is required false is to be returned.
161
	 *
162
	 * @param array $activity
163
	 *
164
	 * @return integer|false
165
	 */
166 1
	public function getGroupParameter($activity) {
167 1
		return false;
168
	}
169
170
	/**
171
	 * The extension can define additional navigation entries. The array returned has to contain two keys 'top'
172
	 * and 'apps' which hold arrays with the relevant entries.
173
	 * If no further entries are to be added false is no be returned.
174
	 *
175
	 * @return array|false
176
	 */
177
	public function getNavigation() {
178
		$l = $this->getL10N();
179
		return [
180
			'apps' => [
181
				self::FILTER_AVIR => [
182
					'id' => self::FILTER_AVIR,
183
					'name' => (string) $l->t('Antivirus'),
184
					'url' => $this->URLGenerator->linkToRoute(
185
						'activity.Activities.showList',
186
						['filter' => self::FILTER_AVIR]
187
					),
188
				],
189
			],
190
			'top' => []
191
		];
192
	}
193
194
	/**
195
	 * The extension can check if a customer filter (given by
196
	 * a query string like filter=abc) is valid or not.
197
	 *
198
	 * @param string $filterValue
199
	 *
200
	 * @return boolean
201
	 */
202
	public function isFilterValid($filterValue) {
203
		return $filterValue === self::FILTER_AVIR;
204
	}
205
206
	/**
207
	 * The extension can filter the types based on the filter if required.
208
	 * In case no filter is to be applied false is to be returned unchanged.
209
	 *
210
	 * @param array $types
211
	 * @param string $filter
212
	 *
213
	 * @return array|false
214
	 */
215
	public function filterNotificationTypes($types, $filter) {
216
		return $filter === self::FILTER_AVIR ? [self::TYPE_VIRUS_DETECTED] : $types;
217
	}
218
219
	/**
220
	 * For a given filter the extension can specify the sql query conditions
221
	 * including parameters for that query.
222
	 * In case the extension does not know the filter false is to be returned.
223
	 * The query condition and the parameters are to be returned as array with two elements.
224
	 * E.g. return ['`app` = ? and `message` like ?', array('mail', 'ownCloud%')];
225
	 *
226
	 * @param string $filter
227
	 *
228
	 * @return array|false
229
	 */
230
	public function getQueryForFilter($filter) {
231
		if ($filter === self::FILTER_AVIR) {
232
			return [
233
				'(`app` = ?)',
234
				[self::APP_NAME],
235
			];
236
		}
237
		return false;
238
	}
239
}
240