1<?php
2/**
3 * @copyright Copyright (c) 2016, ownCloud, Inc.
4 *
5 * @author Joas Schilling <coding@schilljs.com>
6 * @author Roeland Jago Douma <roeland@famdouma.nl>
7 *
8 * @license AGPL-3.0
9 *
10 * This code is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License, version 3,
12 * as published by the Free Software Foundation.
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, version 3,
20 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21 *
22 */
23
24namespace OCA\Activity;
25
26use OCP\Activity\IFilter;
27use OCP\Activity\IManager;
28use OCP\IConfig;
29use OCP\IL10N;
30use OCP\IURLGenerator;
31use OCP\Template;
32
33/**
34 * Class Navigation
35 *
36 * @package OCA\Activity
37 */
38class Navigation {
39	/** @var IL10N */
40	protected $l;
41
42	/** @var IManager */
43	protected $activityManager;
44
45	/** @var IURLGenerator */
46	protected $URLGenerator;
47
48	/** @var IConfig */
49	protected $config;
50
51	/** @var CurrentUser */
52	protected $currentUser;
53
54	/**
55	 * Construct
56	 *
57	 * @param IL10N $l
58	 * @param IManager $manager
59	 * @param IURLGenerator $URLGenerator
60	 * @param IConfig $config
61	 * @param CurrentUser $currentUser
62	 */
63	public function __construct(IL10N $l,
64								IManager $manager,
65								IURLGenerator $URLGenerator,
66								IConfig $config,
67								CurrentUser $currentUser) {
68		$this->l = $l;
69		$this->activityManager = $manager;
70		$this->URLGenerator = $URLGenerator;
71		$this->config = $config;
72		$this->currentUser = $currentUser;
73	}
74
75	/**
76	 * Get the users we want to send an email to
77	 *
78	 * @param null|string $forceActive Navigation entry that should be marked as active
79	 * @return \OCP\Template
80	 */
81	public function getTemplate($forceActive = 'all') {
82		$active = $forceActive ?: 'all';
83
84		$template = new Template('activity', 'stream.app.navigation', '');
85
86		$template->assign('activeNavigation', $active);
87		$template->assign('navigations', $this->getLinkList());
88		$template->assign('rssLink', $this->getRSSLink());
89		$template->assign('personalSettingsLink', $this->getPersonalSettingsLink());
90
91		return $template;
92	}
93
94	/**
95	 * @return string
96	 */
97	protected function getRSSLink() {
98		$rssToken = $this->config->getUserValue($this->currentUser->getUID(), 'activity', 'rsstoken');
99		if ($rssToken) {
100			return $this->URLGenerator->linkToRouteAbsolute('activity.Feed.show', ['token' => $rssToken]);
101		} else {
102			return '';
103		}
104	}
105
106	/**
107	 * Get all items for the users we want to send an email to
108	 *
109	 * @return array Notification data (user => array of rows from the table)
110	 */
111	public function getLinkList() {
112		$filters = $this->activityManager->getFilters();
113		usort($filters, static function (IFilter $a, IFilter $b) {
114			if ($a->getPriority() === $b->getPriority()) {
115				return (int) ($a->getIdentifier() > $b->getIdentifier());
116			}
117
118			return (int) ($a->getPriority() > $b->getPriority());
119		});
120
121		$entries = [];
122		foreach ($filters as $filter) {
123			$entries[] = [
124				'id' => $filter->getIdentifier(),
125				'icon' => $filter->getIcon(),
126				'name' => $filter->getName(),
127				'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => $filter->getIdentifier()]),
128			];
129		}
130
131		return $entries;
132	}
133
134	/**
135	 * @return string
136	 */
137	protected function getPersonalSettingsLink() {
138		return $this->URLGenerator->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'notifications']);
139	}
140}
141