xref: /plugin/bez/ctl/task.php (revision fe5d6d1ebd253c129098b67fff8cf438a54d8650)
1<?php
2
3if (!isset($nparams['tid'])) {
4    header('Location: ?id=bez:tasks');
5}
6$template['tid'] = $nparams['tid'];
7$template['action'] = isset($nparams['action']) ? $nparams['action'] : '-default';
8$template['state'] = isset($nparams['state']) ? $nparams['state'] : '-1';
9try {
10    $task = $this->model->tasks->get_one($template['tid']);
11    $template['task'] = $task;
12
13    if ($task->issue == '') {
14        //remove userts that are subscribents already
15        $template['users_to_invite'] = array_diff_key($this->model->users->get_all(), $task->get_subscribents());
16    }
17
18
19    if ($task->cause !== NULL && $task->cause !== '') {
20        $template['commcause'] = $this->model->commcauses->get_one($task->cause);
21    }
22
23    if ($task->issue !== NULL && $task->issue !== '') {
24        $template['issue'] = $this->model->issues->get_one($task->issue);
25    }
26
27
28    if ($template['action'] === 'task_change_state') {
29        if (count($_POST) > 0) {
30            if (isset($_POST['no_evaluation'])) {
31                $_POST['reason'] = '';
32            }
33
34            $task->set_state(array(
35                        'state' => $nparams['state'],
36                        'reason' => $_POST['reason'])
37                    );
38            $this->model->tasks->save($task);
39
40            if (isset($template['issue'])) {
41                $template['issue']->update_last_activity();
42                $this->model->issues->save($template['issue']);
43            }
44
45            $task->mail_notify_subscribents($template['issue'],
46                        array('action' => $bezlang['mail_task_change_state']));
47
48            $redirect = true;
49        } else {
50            $value = $task->get_assoc();
51        }
52    } elseif ($template['action'] === 'task_reopen') {
53        $task->set_state(array('state' => '0'));
54        $this->model->tasks->save($task);
55
56        if (isset($template['issue'])) {
57            $template['issue']->update_last_activity();
58            $this->model->issues->save($template['issue']);
59        }
60
61        $task->mail_notify_subscribents($template['issue'],
62                        array('action' => $bezlang['mail_task_reopened']));
63
64
65        $redirect = true;
66    } elseif($template['action'] === 'task_edit') {
67
68        $template['users'] = $this->model->users->get_all();
69        $template['tasktypes'] = $this->model->tasktypes->get_all();
70
71        if (isset($template['issue'])) {
72            $template['causes'] = $this->model->commcauses->get_all(array(
73                'issue' => $template['issue']->id,
74                'type' => array('!=', '0'),
75            ));
76        }
77
78        if (count($_POST) > 0) {
79            //checkboxes
80            if (!isset($_POST['all_day_event'])) {
81                $_POST['all_day_event'] = '0';
82            }
83            $task->set_data($_POST);
84            //for reason
85            //$task->set_state($_POST);
86
87            $this->model->tasks->save($task);
88
89            $redirect = true;
90        } else {
91            $value = $task->get_assoc();
92        }
93
94    } elseif ($template['action'] === 'subscribe') {
95			$task->add_subscribent($INFO['client']);
96			$this->model->tasks->save($task);
97
98            header("Location: ?id=bez:task:tid:".$task->id);
99
100    } elseif ($template['action'] === 'unsubscribe') {
101			$task->remove_subscribent($INFO['client']);
102			$this->model->tasks->save($task);
103
104            $this->add_notification($bezlang['unsubscribed_task_com']);
105
106            $redirect = true;
107
108    } elseif ($template['action'] === 'invite') {
109            $client = $_POST['client'];
110
111			$state = $task->add_subscribent($client);
112            //user wasn't subscribent
113            if ($state === true) {
114                $this->model->tasks->save($task);
115                $task->mail_notify_invite($client);
116
117                $this->add_notification($this->model->users->get_user_email($client), $bezlang['invitation_has_been_send']);
118
119                $redirect = true;
120            }
121
122    } elseif($template['action'] === 'task_edit_metadata') {
123        $template['users'] = $this->model->users->get_all();
124
125        if (count($_POST) > 0) {
126            $task->set_meta($_POST);
127            $this->model->tasks->save($task);
128
129            $redirect = true;
130        } else {
131            $value = $task->get_assoc();
132            $value['date'] = date('Y-m-d', (int)$value['date']);
133            $value['close_date'] = date('Y-m-d', (int)$value['close_date']);
134        }
135    }
136
137    if ($redirect) {
138        header("Location: ?id=bez:task:tid:".$template['tid']);
139    }
140} catch (ValidationException $e) {
141	$errors = $e->get_errors();
142	$value = $_POST;
143} catch (DBException $e) {
144    echo nl2br($e);
145}
146
147