1<?php
2/**
3 * Defines AJAX calls used exclusively in the smartmobile view.
4 *
5 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
6 *
7 * See the enclosed file COPYING for license information (GPL). If you
8 * did not receive this file, see http://www.horde.org/licenses/gpl.
9 *
10 * @author   Michael Slusarz <slusarz@horde.org>
11 * @category Horde
12 * @license  http://www.horde.org/licenses/gpl GPL
13 * @package  Nag
14 */
15class Nag_Ajax_Application_Handler_Smartmobile extends Horde_Core_Ajax_Application_Handler
16{
17    /**
18     * AJAX action: Toggle the completed flag.
19     *
20     * Variables used:
21     *   - task: TODO
22     *   - tasklist: TODO
23     *
24     * @return array  TODO
25     */
26    public function smartmobileToggle()
27    {
28        $out = new stdClass;
29
30        if (!isset($this->vars->task) || !isset($this->vars->tasklist)) {
31            $out->error = 'missing parameters';
32        } else {
33            $nag_task = new Nag_CompleteTask();
34            $out = (object)$nag_task->result($this->vars->task, $this->vars->tasklist);
35        }
36
37        return $out;
38    }
39
40    public function getTaskLists()
41    {
42        $lists = Nag::listTasklists();
43        $results = array();
44        foreach ($lists as $name => $list) {
45            $tasklist  = new Nag_Tasklist($list);
46            $results[$name] = $tasklist->toHash();
47        }
48        $return = new stdClass;
49        $return->tasklists = $results;
50
51        return $return;
52    }
53
54    /**
55     * AJAX action: Return a task list.
56     *
57     * @return stdClass  An object containing a tasklist in the tasks property.
58     */
59    public function listTasks()
60    {
61        $options = array('include_history' => false);
62        if ($this->vars->tasklist) {
63            $options['tasklists'] = array($this->vars->tasklist);
64        }
65
66        $tasks = Nag::listTasks($options);
67        $list = array();
68        $tasks->reset();
69        while ($task = $tasks->each()) {
70            $list[] = $task->toJson(true);
71        }
72        $results = new stdClass;
73        $results->tasks = $list;
74
75        return $results;
76    }
77
78    public function deleteTask()
79    {
80        if (!$this->vars->task_id) {
81            $GLOBALS['notification']->push(_("Missing required task id"), 'horde.error');
82            return $results;
83        }
84        if (!$this->vars->tasklist) {
85            $GLOBALS['notification']->push(_("Missing required task list"), 'horde.error');
86            return $results;
87        }
88
89        $results = new stdClass();
90        $storage = $GLOBALS['injector']
91            ->getInstance('Nag_Factory_Driver')
92            ->create($this->vars->tasklist);
93        try {
94            $task = $storage->get($this->vars->task_id);
95        } catch (Nag_Exception $e) {
96            $GLOBALS['notification']->push($e);
97            return $results;
98        }
99        try {
100            $share = $GLOBALS['nag_shares']->getShare($task->tasklist);
101        } catch (Horde_Share_Exception $e) {
102            $GLOBALS['notification']->push($e);
103            return $results;
104        }
105        if (!$share->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::DELETE)) {
106            $GLOBALS['notification']->push(_("You are not allowed to delete this task."), 'horde.error');
107            return $results;
108        }
109        try {
110            $storage->delete($this->vars->task_id);
111        } catch (Nag_Exception $e) {
112            $GLOBALS['notification']->push($e);
113            return $results;
114        }
115        $GLOBALS['notification']->push(_("Successfully deleted"), 'horde.success');
116        $results->deleted = $this->vars->task_id;
117        $results->l = $this->vars->tasklist;
118        return $results;
119    }
120
121    public function saveTask()
122    {
123        $results = new stdClass();
124        $task = array(
125            'name' => $this->vars->task_title,
126            'desc' => $this->vars->task_desc,
127            'assignee' => $this->vars->task_assignee,
128            'priority' => $this->vars->task_priority,
129            'owner' => $GLOBALS['registry']->getAuth()
130        );
131
132        if ($this->vars->task_private) {
133            $task['private'] = true;
134        }
135
136        if ($this->vars->task_start) {
137            $date = new Horde_Date($this->vars->task_start);
138            $task['start'] = $date->timestamp();
139        }
140
141        if ($this->vars->task_due) {
142            $date = new Horde_Date($this->vars->task_due);
143            $task['due'] = $date->timestamp();
144        }
145
146        if ($this->vars->task_estimate) {
147            $task['estimate'] = $this->vars->task_estimate;
148        }
149
150        if ($this->vars->task_completed) {
151            $task['completed'] = $this->vars->task_completed == 'on' ? true : false;
152        }
153
154        if ($this->vars->tasklist) {
155            $tasklist = $this->vars->tasklist;
156        } else {
157            $tasklist = $GLOBALS['prefs']->getValue('default_tasklist');
158        }
159        try {
160            $storage = $GLOBALS['injector']
161                ->getInstance('Nag_Factory_Driver')
162                ->create($tasklist);
163        } catch (Nag_Exception $e) {
164            $GLOBALS['notification']->push($e);
165            return $results;
166        }
167
168        if ($this->vars->task_id) {
169            // Existing task
170            try {
171                $existing_task = $storage->get($this->vars->task_id);
172                $existing_task->merge($task);
173                $existing_task->save();
174                $results->task = $existing_task->toJson(true);
175            } catch (Nag_Exception $e) {
176                $GLOBALS['notification']->push($e);
177                return $results;
178            } catch (Horde_Exception_NotFound $e) {
179                $GLOBALS['notification']->push($e);
180                return $results;
181            }
182        } else {
183            try {
184                $ids = $storage->add($task);
185                $results->task = $storage->get($ids[0])->toJson(true);
186            } catch (Nag_Exception $e) {
187                $GLOBALS['notification']->push($e);
188                return $results;
189            }
190        }
191
192        $GLOBALS['notification']->push(sprintf(_("Saved %s"), $task['name']), 'horde.success');
193        return $results;
194    }
195
196    public function getTask()
197    {
198        $out = new StdClass;
199        if (!isset($this->vars->task) || !isset($this->vars->tasklist)) {
200            $out->error = 'Missing Parameters';
201        } else {
202            $task = Nag::getTask($this->vars->tasklist, $this->vars->task);
203            $out->task = $task->toJson(true);
204        }
205
206        return $out;
207    }
208}
209