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 = 'user_messages';
12require_once('tiki-setup.php');
13$messulib = TikiLib::lib('message');
14$access->check_user($user);
15$access->check_feature('feature_messages');
16$access->check_permission('tiki_p_messages');
17
18$maxRecords = $messulib->get_user_preference($user, 'mess_maxRecords', 20);
19// auto-archiving of read mails?
20$mess_archiveAfter = $messulib->get_user_preference($user, 'mess_archiveAfter', 0);
21$smarty->assign_by_ref('mess_archiveAfter', $mess_archiveAfter);
22if ($mess_archiveAfter > 0) {
23	// get date of last check. if not set yet, set it to 'before 10 minutes'
24	$mess_archiveLast = $messulib->get_user_preference($user, 'mess_archiveLast', $tikilib->now - 600);
25	// only run auto-archive job every 10 minutes:
26	if ($tikilib->now - $mess_archiveLast >= 600) {
27		$messulib->set_user_preference($user, 'mess_archiveLast', $tikilib->now);
28		$messulib->archive_messages($user, $mess_archiveAfter);
29	}
30}
31
32//set defaults
33$sort_mode = 'date_desc';
34$offset = 0;
35$find = '';
36$orig_or_reply = "r";
37
38// Mark messages if the mark button was pressed
39if (isset($_POST['flagmsg']) && $access->checkCsrf()) {
40	$parts = explode("_", $_POST['flagmsg']);
41	$messulib->flag_message($user, $parts[1], 'isFlagged', $parts[0]);
42}
43
44if (isset($_POST["mark"])) {
45	if (isset($_POST["msg"]) && $access->checkCsrf()) {
46		$i = 0;
47		foreach (array_keys($_POST["msg"]) as $msg) {
48			$parts = explode('_', $_POST['action']);
49			$result = $messulib->flag_message($user, $msg, $parts[0], $parts[1]);
50			$i = $i + $result->numRows();
51		}
52		if ($i) {
53			$msg = $i === 1 ? tr('%0 message was marked', $i) : tr('%0 messages were marked', $i);
54			Feedback::success($msg);
55		} else {
56			Feedback::error(tra('No messages were marked'));
57		}
58	} elseif (!isset($_POST["msg"])) {
59		Feedback::error(tra('No messages were selected to mark'));
60	}
61}
62// Delete messages if the delete button was pressed
63if (isset($_POST["delete"])) {
64	if (isset($_POST["msg"]) && $access->checkCsrfForm(tra('Delete selected messages?'))) {
65		$i = 0;
66		foreach (array_keys($_POST["msg"]) as $msg) {
67			$result = $messulib->delete_message($user, $msg);
68			$i = $i + $result->numRows();
69		}
70		if ($i) {
71			$msg = $i === 1 ? tr('%0 message was deleted', $i) : tr('%0 messages were deleted', $i);
72			Feedback::success($msg);
73		} else {
74			Feedback::error(tra('No messages were deleted'));
75		}
76	} elseif (!isset($_POST["msg"])) {
77		Feedback::error(tra('No messages were selected to delete'));
78	}
79}
80// Archive messages if the archive button was pressed
81if (isset($_POST["archive"])) {
82	if (isset($_POST["msg"]) && $access->checkCsrf()) {
83		$tmp = $messulib->count_messages($user, 'archive');
84		$i = 0;
85		foreach (array_keys($_POST["msg"]) as $msg) {
86			if (($prefs['messu_archive_size'] > 0) && ($tmp + $i >= $prefs['messu_archive_size'])) {
87				$smarty->assign('msg', tra("Archive is full. Delete some messages from archive first."));
88				$smarty->display("error.tpl");
89				die;
90			}
91			$result = $messulib->archive_message($user, $msg);
92			$i = $i + $result->numRows();
93		}
94		if ($i) {
95			$msg = $i === 1 ? tr('%0 message was archived', $i) : tr('%0 messages were archived', $i);
96			Feedback::success($msg);
97		} else {
98			Feedback::error(tra('No messages were archived'));
99		}
100	} elseif (!isset($_POST["msg"])) {
101		Feedback::error(tra('No messages were selected to archive'));
102	}
103}
104
105// Download messages if the download button was pressed
106if (isset($_REQUEST["download"])) {
107	// if message ids are handed over, use them:
108	if (isset($_REQUEST["msg"])) {
109		foreach (array_keys($_REQUEST["msg"]) as $msg) {
110			$tmp = $messulib->get_message($user, $msg, 'messages');
111			$items[] = $tmp;
112		}
113	} else {
114		$items = $messulib->get_messages($user, 'messages', '', '', '');
115	}
116	$smarty->assign_by_ref('items', $items);
117	header("Content-Disposition: attachment; filename=tiki-msg-mailbox-" . time() . ".txt ");
118	$smarty->display('messu-download.tpl', null, null, null, 'application/download');
119	die;
120}
121
122if (isset($_REQUEST['filter'])) {
123	if ($_REQUEST['flags'] != '') {
124		$parts = explode('_', $_REQUEST['flags']);
125		$_REQUEST['flag'] = $parts[0];
126		$_REQUEST['flagval'] = $parts[1];
127	}
128}
129if (isset($_REQUEST["sort_mode"])) {
130	$sort_mode = $_REQUEST["sort_mode"];
131}
132if (isset($_REQUEST["offset"])) {
133	$offset = $_REQUEST["offset"];
134}
135if (isset($_REQUEST["find"])) {
136	$find = $_REQUEST["find"];
137}
138if (isset($_REQUEST["origto"])) {
139	$_REQUEST["replyto"] = $_REQUEST["origto"];
140	$orig_or_reply = "o";
141}
142if (! isset($_REQUEST["replyto"])) {
143	$_REQUEST["replyto"] = '';
144}
145if (! isset($_REQUEST["priority"])) {
146	$_REQUEST["priority"] = '';
147}
148if (! isset($_REQUEST["flag"])) {
149	$_REQUEST["flag"] = '';
150}
151if (! isset($_REQUEST["flagval"])) {
152	$_REQUEST["flagval"] = '';
153}
154$smarty->assign_by_ref('flag', $_REQUEST['flag']);
155$smarty->assign_by_ref('priority', $_REQUEST['priority']);
156$smarty->assign_by_ref('flagval', $_REQUEST['flagval']);
157$smarty->assign_by_ref('offset', $offset);
158$smarty->assign_by_ref('sort_mode', $sort_mode);
159$smarty->assign('find', $find);
160// What are we paginating: items
161$items = $messulib->list_user_messages($user, $offset, $maxRecords, $sort_mode, $find, $_REQUEST["flag"], $_REQUEST["flagval"], $_REQUEST['priority'], '', $_REQUEST["replyto"], $orig_or_reply);
162$smarty->assign_by_ref('cant_pages', $items["cant"]);
163$smarty->assign_by_ref('mess_maxRecords', $maxRecords);
164$smarty->assign_by_ref('items', $items["data"]);
165$cellsize = 200;
166$percentage = 1;
167if ($prefs['messu_mailbox_size'] > 0) {
168	$current_number = $messulib->count_messages($user);
169	$smarty->assign('messu_mailbox_number', $current_number);
170	$smarty->assign('messu_mailbox_size', $prefs['messu_mailbox_size']);
171	$percentage = ($current_number / $prefs['messu_mailbox_size']) * 100;
172	$cellsize = round($percentage / 100 * 200);
173	if ($current_number > $prefs['messu_mailbox_size']) {
174		$cellsize = 200;
175	}
176	if ($cellsize < 1) {
177		$cellsize = 1;
178	}
179	$percentage = round($percentage);
180}
181$smarty->assign('cellsize', $cellsize);
182$smarty->assign('percentage', $percentage);
183include_once('tiki-section_options.php');
184include_once('tiki-mytiki_shared.php');
185$smarty->assign('mid', 'messu-mailbox.tpl');
186$smarty->display("tiki.tpl");
187