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$inputConfiguration = [
12	[
13		'staticKeyFilters' => [
14			'offset' => 'digits',
15			'maxRecords' => 'digits',
16			'removeevent' => 'digits',
17			'removetype' => 'word',
18			'sort_mode' => 'word',
19			'find' => 'striptags',
20			'email' => 'email',
21			'event' => 'text',
22			'add' => 'alpha',
23			'delsel_x' => 'alpha',
24		] ,
25		'staticKeyFiltersForArrays' => [
26			'checked' => 'alnum',
27		] ,
28	]
29];
30// Initialization
31require_once('tiki-setup.php');
32$access->check_permission(['tiki_p_admin_notifications']);
33
34$notificationlib = TikiLib::lib('notification');
35
36$auto_query_args = [
37	'offset',
38	'sort_mode',
39	'find',
40	'maxRecords'
41];
42$watches = $notificationlib->get_global_watch_types();
43
44$save = true;
45$login = '';
46if (isset($_REQUEST["add"]) && $access->checkCsrf() ) {
47	if (! empty($_REQUEST['login'])) {
48		if ($userlib->user_exists($_REQUEST['login'])) {
49			$login = $_REQUEST['login'];
50		} else {
51			Feedback::error(tra('Invalid username'));
52			$save = false;
53		}
54	} elseif (! empty($_REQUEST['email'])) {
55		if (validate_email($_REQUEST['email'], $prefs['validateEmail'])) {
56			$email = $_REQUEST['email'];
57		} else {
58			Feedback::error(tra('Invalid email'));
59			$save = false;
60		}
61	} else {
62		Feedback::error(tra('You need to provide a username or an email'));
63		$save = false;
64	}
65	if ($save and isset($_REQUEST['event']) and isset($watches[$_REQUEST['event']])) {
66		$result = $tikilib->add_user_watch($login, $_REQUEST["event"], $watches[$_REQUEST['event']]['object'], $watches[$_REQUEST['event']]['type'], $watches[$_REQUEST['event']]['label'], $watches[$_REQUEST['event']]['url'], isset($email) ? $email : null);
67		if (! $result) {
68			Feedback::error(tra('The user has no email set. No notifications will be sent.'));
69		} else {
70			Feedback::success(tr('Mail notification event added'));
71		}
72	}
73}
74
75if (isset($_REQUEST["removeevent"]) && isset($_REQUEST['removetype']) && $access->checkCsrfForm(tr('Delete mail notification event?'))) {
76	if ($_REQUEST['removetype'] == 'user') {
77		$result = $tikilib->remove_user_watch_by_id($_REQUEST["removeevent"]);
78	} else {
79		$result = $tikilib->remove_group_watch_by_id($_REQUEST["removeevent"]);
80	}
81	if ($result && $result->numRows()) {
82		Feedback::success(tr('Mail notification event deleted'));
83	} else {
84		Feedback::error('Mail notification event not deleted');
85	}
86}
87if (isset($_REQUEST['action'])
88	&& $_REQUEST['action'] == 'delete'
89	&& isset($_REQUEST['checked'])
90	&& $access->checkCsrfForm(tr('Delete selected notification events?')))
91{
92	$i = 0;
93	$i = 0;
94	foreach ($_REQUEST['checked'] as $id) {
95		if (strpos($id, 'user') === 0) {
96			$result = $tikilib->remove_user_watch_by_id(substr($id, 4));
97			if ($result && $result->numRows()) {
98				$i++;
99			}
100		} else {
101			$result = $tikilib->remove_group_watch_by_id(substr($id, 5));
102			if ($result && $result->numRows()) {
103				$i++;
104			}
105		}
106	}
107	$checkedCount = count($_REQUEST['checked']);
108	if ($checkedCount == $i) {
109		$msg = $i == 1 ? tr('One mail notification events deleted') : tr('%0 mail notifications events deleted', $i);
110		Feedback::success(tr($msg));
111	} elseif ($i < $checkedCount) {
112		Feedback::error('%0 of %1 selected mail notification events deleted', $i, $checkedCount);
113	}
114}
115if (! isset($_REQUEST["sort_mode"])) {
116	$sort_mode = 'event_asc';
117} else {
118	$sort_mode = $_REQUEST["sort_mode"];
119}
120if (! isset($_REQUEST["offset"])) {
121	$offset = 0;
122} else {
123	$offset = $_REQUEST["offset"];
124}
125$smarty->assign_by_ref('offset', $offset);
126if (isset($_REQUEST["find"])) {
127	$find = $_REQUEST["find"];
128} else {
129	$find = '';
130}
131$smarty->assign_by_ref('find', $find);
132if (! empty($_REQUEST['maxRecords'])) {
133	$maxRecords = $_REQUEST['maxRecords'];
134}
135$smarty->assign_by_ref('watches', $watches);
136$smarty->assign_by_ref('maxRecords', $maxRecords);
137$smarty->assign_by_ref('sort_mode', $sort_mode);
138$channels = $tikilib->list_watches($offset, $maxRecords, $sort_mode, $find);
139$smarty->assign_by_ref('cant', $channels['cant']);
140$smarty->assign_by_ref('channels', $channels["data"]);
141if ($prefs['feature_trackers'] == 'y') {
142	$trklib = TikiLib::lib('trk');
143	$trackers = $trklib->get_trackers_options(0, 'outboundemail', $find, 'empty');
144	$smarty->assign_by_ref('trackers', $trackers);
145}
146if ($prefs['feature_forums'] == 'y') {
147	$commentslib = TikiLib::lib('comments');
148	$forums = $commentslib->get_outbound_emails();
149	$smarty->assign_by_ref('forums', $forums);
150}
151// disallow robots to index page:
152$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
153
154// Display the template
155$smarty->assign('mid', 'tiki-admin_notifications.tpl');
156$smarty->display("tiki.tpl");
157