1<?php
2/**
3 * @package tikiwiki
4 */
5// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
6//
7// All Rights Reserved. See copyright.txt for details and a complete list of authors.
8// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
9// $Id$
10
11$section = 'mytiki';
12include_once('tiki-setup.php');
13
14$access->check_user($user);
15$access->check_feature('feature_user_watches');
16
17if ($prefs['feature_user_watches_translations']) {
18	$langLib = TikiLib::lib('language');
19	$languages = $langLib->list_languages();
20	$smarty->assign_by_ref('languages', $languages);
21}
22
23$notificationlib = TikiLib::lib('notification');
24
25$notification_types = $notificationlib->get_global_watch_types(true);
26
27$smarty->assign('add_options', $notification_types);
28if (isset($_POST['langwatch'])) {
29	foreach ($languages as $lang) {
30		if ($_POST['langwatch'] == $lang['value']) {
31			$langwatch = $lang;
32			break;
33		}
34	}
35} else {
36	$langwatch = null;
37}
38
39if ($prefs['feature_categories']) {
40	$categlib = TikiLib::lib('categ');
41	$categories = $categlib->getCategories(null, true, false);
42} else {
43	$categories = [];
44}
45
46if (isset($_REQUEST['categwatch'])) {
47	$selected_categ = null;
48	foreach ($categories as $categ) {
49		if ($_REQUEST['categwatch'] == $categ['categId']) {
50			$selected_categ = $categ;
51			break;
52		}
53	}
54}
55// request from unsubscribe email link, like in templates/mail/user_watch_map_changed.tpl
56if (isset($_REQUEST['id'])) {
57	if ($tiki_p_admin_notifications != 'y' && $user != $tikilib->get_user_notification($_REQUEST['id'])) {
58		Feedback::errorPage(['mes' => tr('Permission denied'), 'errortype' => 401]);
59	}
60	if ($access->confirmRedirect(tr('Unsubscribe from user watch email notifications?'))) {
61		$result = $tikilib->remove_user_watch_by_id($_REQUEST['id']);
62	}
63	if ($result && $result->numRows()) {
64		Feedback::success(tr('Unsubscribed from user watch email notification'));
65	} else {
66		Feedback::error(tr('Unsubscribe failed'));
67	}
68}
69
70if (isset($_REQUEST["add"]) && $access->checkCsrf()) {
71	if (isset($_REQUEST['event'])) {
72		if (! isset($notification_types[$_REQUEST['event']])) {
73			Feedback::errorPage(tr('Unknown watch type'));
74		}
75		$watch_object = '*';
76		$watch_type = $notification_types[$_REQUEST['event']]['type'];
77		$watch_label = $notification_types[$_REQUEST['event']]['label'];
78		$watch_url = $notification_types[$_REQUEST['event']]['url'];
79		//special values
80		switch ($_REQUEST['event']) {
81			case 'wiki_page_in_lang_created':
82				$watch_object = $langwatch['value'];
83				$watch_label = tra('Language watch') . ": {$lang['name']}";
84				break;
85
86			case 'category_changed_in_lang':
87				if ($selected_categ && $langwatch) {
88					$watch_object = $selected_categ['categId'];
89					$watch_type = $langwatch['value'];
90					$watch_label = tr('Category watch: %0, Language: %1', $selected_categ['name'], $langwatch['name']);
91					$watch_url = "tiki-browse_categories.php?lang={$lang['value']}&parentId={$selected_categ['categId']}";
92				}
93		}
94		$result = $tikilib->add_user_watch(
95			$user,
96			$_REQUEST['event'],
97			$watch_object,
98			$watch_type,
99			$watch_label,
100			$watch_url
101		);
102		if ($result) {
103			Feedback::success(tr('User watch added'));
104		} else {
105			Feedback::error(tr('User watch not added'));
106		}
107		$_REQUEST['event'] = '';
108	} else {
109		// Don't see where this case is used in the code
110		$errors = 0;
111		foreach ($_REQUEST['cat_categories'] as $cat) {
112			if ($cat > 0) {
113				$result = $tikilib->add_user_watch($user, 'new_in_category', $cat, 'category', "tiki-browse_category.php?parentId=$cat");
114				$errors += $result ? 0: 1;
115			} else {
116				$tikilib->remove_user_watch($user, 'new_in_category', '*');
117				/** @var  TikiDb_Pdo_Result|TikiDb_Adodb_Result $result */
118				$result = $tikilib->add_user_watch($user, 'new_in_category', '*', 'category', "tiki-browse_category.php");
119				$errors += $result && $result->numRows() ? 0 : 1;
120			}
121			if ($errors) {
122				Feedback::error('Errors encountered in adding category user watches');
123			} else {
124				Feedback::success('Category user watches added');
125			}
126		}
127	}
128}
129// no confirmation needed as it is easy to add back a watch
130if (isset($_REQUEST["delete"]) && isset($_REQUEST['checked']) && $access->checkCsrf()) {
131	$checked = is_array($_REQUEST['checked']) ? $_REQUEST['checked'] : [$_REQUEST['checked']];
132	/* CSRL doesn't work if param as passed not in the uri */
133	foreach ($checked as $item) {
134		$result = $tikilib->remove_user_watch_by_id($item);
135		if ($result && $result->numRows()) {
136			Feedback::success(tr('User watch deleted'));
137		} else {
138			Feedback::error(tr('User watch not deleted'));
139		}
140	}
141}
142$notification_types = $notificationlib->get_global_watch_types();
143$rawEvents = $tikilib->get_watches_events();
144$events = [];
145foreach ($rawEvents as $event) {
146	if (array_key_exists($event, $notification_types)) {
147		$events[$event] = $notification_types[$event]['label'];
148	} else {
149		$events[$event] = $event; // Fallback to raw event name if no description found
150	}
151}
152$smarty->assign('events', $events);
153// if not set event type then all
154if (! isset($_REQUEST['event'])) {
155	$_REQUEST['event'] = '';
156}
157// get all the information for the event
158$watches = $tikilib->get_user_watches($user, $_REQUEST['event']);
159foreach ($watches as $key => $watch) {
160	if (array_key_exists($watch['event'], $notification_types)) {
161		$watches[$key]['label'] = $notification_types[$watch['event']]['label'];
162	}
163}
164$smarty->assign('watches', $watches);
165// this was never needed here, was it ? -- luci
166//include_once ('tiki-mytiki_shared.php');
167if ($prefs['feature_categories']) {
168	$watches = $tikilib->get_user_watches($user, 'new_in_category');
169	$nb = count($categories);
170	foreach ($watches as $watch) {
171		if ($watch['object'] == '*') {
172			$smarty->assign('all', 'y');
173			break;
174		}
175		for ($i = 0; $i < $nb; ++$i) {
176			if ($watch['object'] == $categories[$i]['categId']) {
177				$categories[$i]['incat'] = 'y';
178				break;
179			}
180		}
181	}
182	$smarty->assign('categories', $categories);
183}
184if ($prefs['feature_messages'] == 'y' && $tiki_p_messages == 'y') {
185	$unread = $tikilib->user_unread_messages($user);
186	$smarty->assign('unread', $unread);
187}
188$eok = $userlib->get_user_email($user);
189$smarty->assign('email_ok', empty($eok) ? 'n' : 'y');
190$reportsUsers = Reports_Factory::build('Reports_Users');
191$reportsUsersUser = $reportsUsers->get($user);
192$smarty->assign_by_ref('report_preferences', $reportsUsersUser);
193
194$smarty->assign('user_calendar_watch_editor', $tikilib->get_user_preference($user, 'user_calendar_watch_editor'));
195$smarty->assign('user_article_watch_editor', $tikilib->get_user_preference($user, 'user_article_watch_editor'));
196$smarty->assign('user_wiki_watch_editor', $tikilib->get_user_preference($user, 'user_wiki_watch_editor'));
197$smarty->assign('user_blog_watch_editor', $tikilib->get_user_preference($user, 'user_blog_watch_editor'));
198$smarty->assign('user_tracker_watch_editor', $tikilib->get_user_preference($user, 'user_tracker_watch_editor'));
199$smarty->assign('user_comment_watch_editor', $tikilib->get_user_preference($user, 'user_comment_watch_editor'));
200$smarty->assign('user_category_watch_editor', $tikilib->get_user_preference($user, 'user_category_watch_editor'));
201$smarty->assign('user_plugin_approval_watch_editor', $tikilib->get_user_preference($user, 'user_plugin_approval_watch_editor'));
202
203$smarty->assign('mid', 'tiki-user_watches.tpl');
204$smarty->display("tiki.tpl");
205