Completed
Pull Request — master (#455)
by Joas
03:13
created

Navigation::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 9.2
cc 2
eloc 18
nc 2
nop 7
crap 2
1
<?php
2
3
/**
4
 * ownCloud - Activity App
5
 *
6
 * @author Joas Schilling
7
 * @copyright 2014 Joas Schilling [email protected]
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
 * License as published by the Free Software Foundation; either
12
 * version 3 of the License, or any later version.
13
 *
14
 * This library 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 Lesser General Public
20
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Activity;
25
26
27
use OCP\Activity\IManager;
28
use OCP\IL10N;
29
use OCP\IURLGenerator;
30
use OCP\Template;
31
32
/**
33
 * Class Navigation
34
 *
35
 * @package OCA\Activity
36
 */
37
class Navigation {
38
	/** @var IL10N */
39
	protected $l;
40
41
	/** @var IManager */
42
	protected $activityManager;
43
44
	/** @var IURLGenerator */
45
	protected $URLGenerator;
46
47
	/** @var UserSettings */
48
	protected $userSettings;
49
50
	/** @var string */
51
	protected $active;
52
53
	/** @var string */
54
	protected $user;
55
56
	/** @var string */
57
	protected $rssLink;
58
59
	/**
60
	 * Construct
61
	 *
62
	 * @param IL10N $l
63
	 * @param IManager $manager
64
	 * @param IURLGenerator $URLGenerator
65
	 * @param UserSettings $userSettings
66
	 * @param string $user
67
	 * @param string $rssToken
68
	 * @param null|string $active Navigation entry that should be marked as active
69
	 */
70 6
	public function __construct(IL10N $l,
71
								IManager $manager,
72
								IURLGenerator $URLGenerator,
73
								UserSettings $userSettings,
74
								$user,
75
								$rssToken,
76
								$active = 'all') {
77 6
		$this->l = $l;
78 6
		$this->activityManager = $manager;
79 6
		$this->URLGenerator = $URLGenerator;
80 6
		$this->userSettings = $userSettings;
81 6
		$this->user = $user;
82 6
		$this->active = $active;
83
84 6
		if ($rssToken) {
85 1
			$this->rssLink = $this->URLGenerator->getAbsoluteURL(
86 1
				$this->URLGenerator->linkToRoute('activity.Feed.show', array('token' => $rssToken))
87
			);
88
		} else {
89 5
			$this->rssLink = '';
90
		}
91 6
	}
92
93
	/**
94
	 * Get the users we want to send an email to
95
	 *
96
	 * @param null|string $forceActive Navigation entry that should be marked as active
97
	 * @return \OCP\Template
98
	 */
99 4
	public function getTemplate($forceActive = null) {
100 4
		$active = $forceActive ?: $this->active;
101
102 4
		$template = new Template('activity', 'stream.app.navigation', '');
103 4
		$entries = $this->getLinkList();
104
105 4
		if (sizeof($entries['apps']) === 1) {
106
			// If there is only the files app, we simply do not show it,
107
			// as it is the same as the 'all' filter.
108 4
			$entries['apps'] = array();
109
		}
110
111 4
		$template->assign('activeNavigation', $active);
112 4
		$template->assign('navigations', $entries);
113 4
		$template->assign('rssLink', $this->rssLink);
114
115 4
		return $template;
116
	}
117
118
	/**
119
	 * Get all items for the users we want to send an email to
120
	 *
121
	 * @return array Notification data (user => array of rows from the table)
122
	 */
123 4
	public function getLinkList() {
124
		$topEntries = [
125
			[
126 4
				'id' => 'all',
127 4
				'name' => (string) $this->l->t('All Activities'),
128 4
				'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList'),
129
			],
130
		];
131
132 4
		if ($this->user && $this->userSettings->getUserSetting($this->user, 'setting', 'self')) {
133 4
			$topEntries[] = [
134 4
				'id' => 'self',
135 4
				'name' => (string) $this->l->t('Activities by you'),
136 4
				'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', array('filter' => 'self')),
137
			];
138 4
			$topEntries[] = [
139 4
				'id' => 'by',
140 4
				'name' => (string) $this->l->t('Activities by others'),
141 4
				'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', array('filter' => 'by')),
142
			];
143
		}
144
145 4
		$additionalEntries = $this->activityManager->getNavigation();
146 4
		$topEntries = array_merge($topEntries, $additionalEntries['top']);
147
148
		return array(
149 4
			'top'		=> $topEntries,
150 4
			'apps'		=> $additionalEntries['apps'],
151
		);
152
	}
153
}
154