Completed
Pull Request — master (#56)
by Morris
59:27
created

Extension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\AnnouncementCenter\Activity;
25
26
use OCA\AnnouncementCenter\Manager;
27
use OCP\Activity\IExtension;
28
use OCP\Activity\IManager;
29
use OCP\L10N\IFactory;
30
31
class Extension implements IExtension {
32
	/** @var Manager */
33
	protected $manager;
34
	/** @var IManager */
35
	protected $activityManager;
36
	/** @var IFactory */
37
	protected $languageFactory;
38
39
	/**
40
	 * @param Manager $manager
41
	 * @param IManager $activityManager
42
	 * @param IFactory $languageFactory
43
	 */
44 29
	public function __construct(Manager $manager, IManager $activityManager, IFactory $languageFactory) {
45 29
		$this->manager = $manager;
46 29
		$this->activityManager = $activityManager;
47 29
		$this->languageFactory = $languageFactory;
48 29
	}
49
50
	/**
51
	 * The extension can return an array of additional notification types.
52
	 * If no additional types are to be added false is to be returned
53
	 *
54
	 * @param string $languageCode
55
	 * @return array|false
56
	 */
57 1
	public function getNotificationTypes($languageCode) {
58 1
		return false;
59
	}
60
61
	/**
62
	 * For a given method additional types to be displayed in the settings can be returned.
63
	 * In case no additional types are to be added false is to be returned.
64
	 *
65
	 * @param string $method
66
	 * @return array|false
67
	 */
68 2
	public function getDefaultTypes($method) {
69 2
		return ['announcementcenter'];
70
	}
71
72
	/**
73
	 * A string naming the css class for the icon to be used can be returned.
74
	 * If no icon is known for the given type false is to be returned.
75
	 *
76
	 * @param string $type
77
	 * @return string|false
78
	 */
79 2
	public function getTypeIcon($type) {
80 2
		return $type === 'announcementcenter' ? 'icon-info' : false;
81
	}
82
83
	/**
84
	 * The extension can translate a given message to the requested languages.
85
	 * If no translation is available false is to be returned.
86
	 *
87
	 * @param string $app
88
	 * @param string $text
89
	 * @param array $params
90
	 * @param boolean $stripPath
91
	 * @param boolean $highlightParams
92
	 * @param string $languageCode
93
	 * @return string|false
94
	 */
95 6
	public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
96 6
		if ($app === 'announcementcenter') {
97 5
			$l = $this->languageFactory->get('announcementcenter', $languageCode);
98
99 5
			list(, $id) = explode('#', $text);
100
101
			try {
102 5
				$announcement = $this->manager->getAnnouncement((int) $id, true);
103 5
			} catch (\InvalidArgumentException $e) {
104 1
				return (string) $l->t('Announcement does not exist anymore', $params);
105
			}
106
107 4
			if (strpos($text, 'announcementmessage#') === 0) {
108 1
				return $announcement['message'];
109
			}
110
111 3
			$params[] = '<parameter>' . $announcement['subject'] . '</parameter>';
112
113
			try {
114 3
				if ($announcement['author'] === $this->activityManager->getCurrentUserId()) {
115 1
					array_shift($params);
116 1
					return (string) $l->t('You announced %s', $params);
117
				}
118 2
			} catch (\UnexpectedValueException $e) {
119
				// FIXME this is awkward, but we have no access to the current user in emails
120
			}
121 2
			return (string) $l->t('%s announced %s', $params);
122
		}
123
124 1
		return false;
125
	}
126
127
	/**
128
	 * The extension can define the type of parameters for translation
129
	 *
130
	 * Currently known types are:
131
	 * * file		=> will strip away the path of the file and add a tooltip with it
132
	 * * username	=> will add the avatar of the user
133
	 *
134
	 * @param string $app
135
	 * @param string $text
136
	 * @return array|false
137
	 */
138 3
	public function getSpecialParameterList($app, $text) {
139 3
		if ($app === 'announcementcenter'&& strpos($text, 'announcementsubject#') === 0) {
140
			return [
141 1
				0 => 'username',
142 1
			];
143
		}
144 2
		return false;
145
	}
146
147
	/**
148
	 * The extension can define the parameter grouping by returning the index as integer.
149
	 * In case no grouping is required false is to be returned.
150
	 *
151
	 * @param array $activity
152
	 * @return integer|false
153
	 */
154 1
	public function getGroupParameter($activity) {
155 1
		return false;
156
	}
157
158
	/**
159
	 * The extension can define additional navigation entries. The array returned has to contain two keys 'top'
160
	 * and 'apps' which hold arrays with the relevant entries.
161
	 * If no further entries are to be added false is no be returned.
162
	 *
163
	 * @return array|false
164
	 */
165 1
	public function getNavigation() {
166 1
		return false;
167
	}
168
169
	/**
170
	 * The extension can check if a customer filter (given by a query string like filter=abc) is valid or not.
171
	 *
172
	 * @param string $filterValue
173
	 * @return boolean
174
	 */
175 4
	public function isFilterValid($filterValue) {
176 4
		return false;
177
	}
178
179
	/**
180
	 * The extension can filter the types based on the filter if required.
181
	 * In case no filter is to be applied false is to be returned unchanged.
182
	 *
183
	 * @param array $types
184
	 * @param string $filter
185
	 * @return array|false
186
	 */
187 4
	public function filterNotificationTypes($types, $filter) {
188 4
		if (in_array($filter, ['all', 'by', 'self'])) {
189 3
			$types[] = 'announcementcenter';
190 3
			return $types;
191
		}
192 1
		return false;
193
	}
194
195
	/**
196
	 * For a given filter the extension can specify the sql query conditions including parameters for that query.
197
	 * In case the extension does not know the filter false is to be returned.
198
	 * The query condition and the parameters are to be returned as array with two elements.
199
	 * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%'));
200
	 *
201
	 * @param string $filter
202
	 * @return array|false
203
	 */
204 4
	public function getQueryForFilter($filter) {
205 4
		return false;
206
	}
207
}
208