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