1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Services_Tracker_TodoController
9{
10	function setUp()
11	{
12		global $prefs;
13
14		if ($prefs['feature_trackers'] != 'y') {
15			throw new Services_Exception_Disabled('feature_trackers');
16		}
17
18		if (! Perms::get()->admin_trackers) {
19			throw new Services_Exception(tr('Operation reserved for tracker administrators'), 403);
20		}
21	}
22
23	function action_view($input)
24	{
25		$trackerId = $input->trackerId->int();
26		$definition = Tracker_Definition::get($trackerId);
27
28		if (! $definition) {
29			throw new Services_Exception_NotFound;
30		}
31
32		$todolib = TikiLib::lib('todo');
33		$trklib = TikiLib::lib('trk');
34
35		return [
36			'title' => tr('Events'),
37			'trackerId' => $trackerId,
38			'todos' => $todolib->listTodoObject('tracker', $trackerId),
39			'statusTypes' => $trklib->status_types(),
40		];
41	}
42
43	function action_add($input)
44	{
45		$trackerId = $input->trackerId->int();
46		$definition = Tracker_Definition::get($trackerId);
47
48		if (! $definition) {
49			throw new Services_Exception_NotFound;
50		}
51
52		$delayAfter = abs($input->after->int() * $input->after_unit->int());
53		$delayNotif = abs($input->notif->int() * $input->notif_unit->int());
54		$from = $input->from->word();
55		$to = $input->to->word();
56		$event = $input->event->word();
57		$subject = $input->subject->text();
58		$body = $input->body->text();
59
60		$todolib = TikiLib::lib('todo');
61
62		if (! $delayAfter) {
63			throw new Services_Exception_MissingValue('after');
64		}
65
66		$todoId = $todolib->addTodo(
67			$delayAfter,
68			$event,
69			'tracker',
70			$trackerId,
71			['status' => $from],
72			['status' => $to]
73		);
74
75		if ($delayNotif) {
76			$detail = [
77				'mail' => 'creator',
78				'before' => $delayNotif,
79			];
80
81			if ($subject) {
82				$detail['subject'] = $subject;
83			}
84
85			if ($body) {
86				$detail['body'] = $body;
87			}
88
89			$todolib->addTodo($delayAfter - $delayNotif, $event, 'todo', $todoId, "", $detail);
90		}
91
92		return [
93			'trackerId' => $trackerId,
94			'todoId' => $todoId,
95		];
96	}
97
98	function action_delete($input)
99	{
100		TikiLib::lib('todo')->delTodo($input->todoId->int());
101		return [
102			'FORWARD' => [
103				'controller' => 'tracker_todo',
104				'action' => 'view',
105				'trackerId' => $input->trackerId->int(),
106			],
107		];
108	}
109}
110