Completed
Pull Request — master (#50)
by Roeland
01:32
created

Activity::getL10N()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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