activitymanager.php 5.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
 * Copyright (c) 2013 Thomas Müller thomas.mueller@tmit.eu
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 *
 */
namespace OC;


use OCP\Activity\IConsumer;
13
use OCP\Activity\IExtension;
14
use OCP\Activity\IManager;
15

16
class ActivityManager implements IManager {
17

18
19
20
	/**
	 * @var \Closure[]
	 */
21
22
	private $consumers = array();

23
24
25
26
27
	/**
	 * @var \Closure[]
	 */
	private $extensions = array();

28
29
30
	/**
	 * @param $app
	 * @param $subject
31
	 * @param $subjectParams
32
	 * @param $message
33
	 * @param $messageParams
34
35
	 * @param $file
	 * @param $link
36
37
38
	 * @param $affectedUser
	 * @param $type
	 * @param $priority
39
40
	 * @return mixed
	 */
41
	function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
42
43
44
		foreach($this->consumers as $consumer) {
			$c = $consumer();
			if ($c instanceof IConsumer) {
45
46
47
48
49
50
51
52
53
54
55
56
57
				try {
				$c->receive(
					$app,
					$subject,
					$subjectParams,
					$message,
					$messageParams,
					$file,
					$link,
					$affectedUser,
					$type,
					$priority);
				} catch (\Exception $ex) {
Lukas Reschke's avatar
Lukas Reschke committed
58
					// TODO: log the exception
59
				}
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
			}

		}
	}

	/**
	 * In order to improve lazy loading a closure can be registered which will be called in case
	 * activity consumers are actually requested
	 *
	 * $callable has to return an instance of OCA\Activity\IConsumer
	 *
	 * @param \Closure $callable
	 */
	function registerConsumer(\Closure $callable) {
		array_push($this->consumers, $callable);
	}
76

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	/**
	 * In order to improve lazy loading a closure can be registered which will be called in case
	 * activity consumers are actually requested
	 *
	 * $callable has to return an instance of OCA\Activity\IConsumer
	 *
	 * @param \Closure $callable
	 * @return void
	 */
	function registerExtension(\Closure $callable) {
		array_push($this->extensions, $callable);
	}

	/**
	 * Will return additional notification types as specified by other apps
	 *
	 * @param string $languageCode
	 * @return array
	 */
	function getNotificationTypes($languageCode) {
		$notificationTypes = array();
		foreach($this->extensions as $extension) {
			$c = $extension();
			if ($c instanceof IExtension) {
				$result = $c->getNotificationTypes($languageCode);
				if (is_array($result)) {
					$notificationTypes = array_merge($notificationTypes, $result);
				}
			}
		}

		return $notificationTypes;
	}

	/**
	 * @param array $types
	 * @param string $filter
	 * @return array
	 */
	function filterNotificationTypes($types, $filter) {
		foreach($this->extensions as $extension) {
			$c = $extension();
			if ($c instanceof IExtension) {
				$result = $c->filterNotificationTypes($types, $filter);
				if (is_array($result)) {
					$types = $result;
				}
			}
		}
		return $types;
	}

	/**
	 * @param string $method
	 * @return array
	 */
	function getDefaultTypes($method) {
		$defaultTypes = array();
		foreach($this->extensions as $extension) {
			$c = $extension();
			if ($c instanceof IExtension) {
				$types = $c->getDefaultTypes($method);
				if (is_array($types)) {
					$defaultTypes = array_merge($types, $defaultTypes);
				}
			}
		}
		return $defaultTypes;
	}

	/**
	 * @param string $app
	 * @param string $text
	 * @param array $params
	 * @param boolean $stripPath
	 * @param boolean $highlightParams
	 * @param string $languageCode
	 * @return string|false
	 */
	function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
		foreach($this->extensions as $extension) {
			$c = $extension();
			if ($c instanceof IExtension) {
				$translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode);
				if (is_string($translation)) {
					return $translation;
				}
			}
		}

		return false;
	}

	/**
	 * @param string $type
	 * @return string
	 */
	function getTypeIcon($type) {
		foreach($this->extensions as $extension) {
			$c = $extension();
			if ($c instanceof IExtension) {
				$icon = $c->getTypeIcon($type);
				if (is_string($icon)) {
					return $icon;
				}
			}
		}

		return '';
	}

	/**
	 * @param array $activity
	 * @return integer|false
	 */
	function getGroupParameter($activity) {
		foreach($this->extensions as $extension) {
			$c = $extension();
			if ($c instanceof IExtension) {
				$parameter = $c->getGroupParameter($activity);
				if ($parameter !== false) {
					return $parameter;
				}
			}
		}

		return false;
	}

	/**
	 * @return array
	 */
	function getNavigation() {
		$entries = array(
			'apps' => array(),
			'top' => array(),
		);
		foreach($this->extensions as $extension) {
			$c = $extension();
			if ($c instanceof IExtension) {
				$additionalEntries = $c->getNavigation();
				if (is_array($additionalEntries)) {
					$entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']);
					$entries['top'] = array_merge($entries['top'], $additionalEntries['top']);
				}
			}
		}

		return $entries;
	}

	/**
	 * @param string $filterValue
	 * @return boolean
	 */
	function isFilterValid($filterValue) {
		foreach($this->extensions as $extension) {
			$c = $extension();
			if ($c instanceof IExtension) {
				if ($c->isFilterValid($filterValue) === true) {
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * @param string $filter
	 * @return array
	 */
	function getQueryForFilter($filter) {
250
251
252
253

		$conditions = array();
		$parameters = array();

254
255
256
257
258
		foreach($this->extensions as $extension) {
			$c = $extension();
			if ($c instanceof IExtension) {
				$result = $c->getQueryForFilter($filter);
				if (is_array($result)) {
259
260
261
262
263
					list($condition, $parameter) = $result;
					if ($condition && is_array($parameter)) {
						$conditions[] = $condition;
						$parameters = array_merge($parameters, $parameter);
					}
264
265
266
267
				}
			}
		}

268
269
270
271
272
		if (empty($conditions)) {
			return array(null, null);
		}

		return array(' and ((' . implode(') or (', $conditions) . '))', $parameters);
273
	}
274
}