1<?php
2/** @var action_plugin_bez $this */
3
4use \dokuwiki\plugin\bez;
5
6if ($this->model->get_level() < BEZ_AUTH_USER) {
7    throw new bez\meta\PermissionDeniedException();
8}
9
10if ($this->get_param('tid') == '') {
11    header('Location: ' . $this->url('tasks'));
12}
13
14/** @var bez\mdl\Task $task */
15$task = $this->model->taskFactory->get_one($this->get_param('tid'));
16$this->tpl->set('task', $task);
17$this->tpl->set('task_comments', $this->model->task_commentFactory->get_from_task($task));
18$this->tpl->set('task_programs',  $this->model->task_programFactory->get_all([], 'name'));
19
20if ($this->get_param('action') == 'comment_add') {
21
22    /** @var bez\mdl\Thread_comment $thread_comment */
23    $task_comment = $this->model->task_commentFactory->create_object(array('task' => $task));
24    $this->model->task_commentFactory->initial_save($task_comment, $_POST);
25
26    $anchor = 'zk'.$task_comment->id;
27    $redirect = true;
28
29} elseif ($this->get_param('action') == 'subscribe') {
30
31    $task->set_participant_flags($this->model->user_nick, array('subscribent'));
32    $redirect = true;
33
34} elseif ($this->get_param('action') == 'unsubscribe') {
35
36    $task->remove_participant_flags($this->model->user_nick, array('subscribent'));
37
38    $this->add_notification($this->getLang('task_unsubscribed_com'));
39    $redirect = true;
40
41} elseif ($this->get_param('action') == 'invite') {
42     $client = $_POST['client'];
43
44    $task->invite($client);
45    $this->add_notification($this->model->userFactory->get_user_email($client), $this->getLang('invitation_has_been_send'));
46
47    $redirect = true;
48} elseif ($this->get_param('action') == 'participant_remove') {
49    $user_id = $this->get_param('user_id');
50    $task->remove_participant($user_id);
51
52    $name = $this->model->userFactory->get_user_full_name($user_id);
53    $notif = sprintf($this->getLang('participant_removed'), $name);
54    $this->add_notification($notif);
55
56    $redirect = true;
57} elseif ($this->get_param('action') == 'comment_delete') {
58    /** @var bez\mdl\Task_comment $task_comment */
59    $task_comment = $this->model->task_commentFactory->get_one($this->get_param('zkid'), array('task' => $task));
60    $this->model->task_commentFactory->delete($task_comment);
61
62    $redirect = true;
63} elseif ($this->get_param('action') == 'comment_edit') {
64    /** @var bez\mdl\Task_comment $task_comment */
65    $task_comment = $this->model->task_commentFactory->get_one($this->get_param('zkid'), array('thread' => $thread));
66
67    if(count($_POST) === 0) {
68        $this->tpl->set_values($task_comment->get_assoc());
69    } else {
70        $this->model->task_commentFactory->update_save($task_comment, $_POST);
71
72        $anchor   = 'zk' . $task_comment->id;
73        $redirect = true;
74    }
75} elseif ($this->get_param('action') == 'task_edit') {
76    //save
77    if (count($_POST) === 0) {
78        $this->tpl->set_values($task->get_assoc());
79    } else {
80        $this->model->taskFactory->update_save($task, $_POST);
81        $redirect = true;
82    }
83} elseif ($this->get_param('action') == 'task_delete') {
84    $this->model->taskFactory->delete($task);
85    if ($task->thread_id != '') {
86        header('Location: ' . $this->url('thread', 'id', $task->thread_id));
87    } else {
88        header("Location: " . $this->url('tasks'));
89    }
90} elseif ($this->get_param('action') == 'pin') {
91    $thread_id = $_POST['thread_id'];
92    $task->pin($thread_id);
93    $redirect = true;
94} elseif ($this->get_param('action') == 'unpin') {
95    $task->unpin();
96    $redirect = true;
97}
98
99
100if (isset($redirect) && $redirect == true) {
101    if (isset($anchor)) {
102        $anchor = '#'.$anchor;
103    } else {
104        $anchor = '';
105    }
106    header("Location: " . $this->url('task', 'tid', $task->id) . $anchor);
107}