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('id') == '') {
11    header('Location: ' . $this->url('threads'));
12}
13
14/** @var bez\mdl\Thread $thread */
15$thread = $this->model->threadFactory->get_one($this->get_param('id'));
16
17if ($thread->acl_of('id') < BEZ_PERMISSION_VIEW) {
18    throw new bez\meta\PermissionDeniedException();
19}
20
21$this->tpl->set('thread', $thread);
22if ($thread->type == 'project') {
23    $this->tpl->set('lang_suffix', '_project');
24}
25
26$thread_comments = iterator_to_array($this->model->thread_commentFactory->get_from_thread($thread));
27$tasks = $this->model->taskFactory->get_from_thread($thread);
28
29$timeline = array_merge($thread_comments, $tasks['corrections']);
30usort($timeline, function($a, $b) {
31    if ($a->create_date == $b->create_date) {
32        return 0;
33    }
34    return ($a->create_date < $b->create_date) ? -1 : 1;
35});
36
37$this->tpl->set('timeline', $timeline);
38$this->tpl->set('tasks', $tasks);
39$this->tpl->set('task_programs',  $this->model->task_programFactory->get_all([], 'name'));
40
41/** @var bez\mdl\Thread_comment $thread_comment */
42$thread_comment = $this->model->thread_commentFactory->create_object(array('thread' => $thread));
43$this->tpl->set('thread_comment', $thread_comment);
44
45if ($this->get_param('action') == 'commcause_add') {
46
47    $this->model->thread_commentFactory->initial_save($thread_comment, $_POST);
48
49    $anchor = 'k'.$thread_comment->id;
50    $redirect = true;
51
52} elseif ($this->get_param('action') == 'subscribe') {
53
54    $thread->set_participant_flags($this->model->user_nick, array('subscribent'));
55    $redirect = true;
56
57} elseif ($this->get_param('action') == 'unsubscribe') {
58
59    $thread->remove_participant_flags($this->model->user_nick, array('subscribent'));
60    $this->add_notification($this->getLang('unsubscribed_com'));
61    $redirect = true;
62
63} elseif ($this->get_param('action') == 'invite') {
64    $client = $_POST['client'];
65
66    $thread->invite($client);
67
68    $this->add_notification($this->model->userFactory->get_user_email($client), $this->getLang('invitation_has_been_send'));
69
70    $redirect = true;
71} elseif ($this->get_param('action') == 'participant_remove') {
72    $user_id = $this->get_param('user_id');
73    $thread->remove_participant($user_id);
74
75    $name = $this->model->userFactory->get_user_full_name($user_id);
76    $notif = sprintf($this->getLang('participant_removed'), $name);
77    $this->add_notification($notif);
78
79    $redirect = true;
80
81} elseif ($this->get_param('action') == 'commcause_delete') {
82    /** @var bez\mdl\Thread_comment $thread_comment */
83    $thread_comment = $this->model->thread_commentFactory->get_one($this->get_param('kid'), array('thread' => $thread));
84    $this->model->thread_commentFactory->delete($thread_comment);
85
86    $redirect = true;
87} elseif ($this->get_param('action') == 'commcause_edit') {
88    /** @var bez\mdl\Thread_comment $thread_comment */
89    $thread_comment = $this->model->thread_commentFactory->get_one($this->get_param('kid'), array('thread' => $thread));
90
91    if(count($_POST) === 0) {
92        $this->tpl->set_values($thread_comment->get_assoc());
93    } else {
94        $this->model->thread_commentFactory->update_save($thread_comment, $_POST);
95
96        $anchor   = 'k' . $thread_comment->id;
97        $redirect = true;
98    }
99} elseif ($this->get_param('action') == 'task_add') {
100
101    $defaults = array('thread' => $thread);
102
103    if ($this->get_param('kid') != '') {
104        $thread_comment = $this->model->thread_commentFactory->get_one($this->get_param('kid'), array('thread' => $thread));
105        $defaults['thread_comment'] = $thread_comment;
106    }
107    /** @var bez\mdl\Task $task */
108    $task = $this->model->taskFactory->create_object($defaults);
109    $this->tpl->set('task_new', $task);
110
111    //save
112    if (count($_POST) > 0) {
113        $this->model->taskFactory->initial_save($task, $_POST);
114
115        $anchor   = 'z' . $task->id;
116        $redirect = true;
117    }
118} elseif ($this->get_param('action') == 'task_edit') {
119    /** @var bez\mdl\Task $task */
120    $task = $this->model->taskFactory->get_one($this->get_param('tid'), array('thread' => $thread));
121    $this->tpl->set('task', $task);
122
123    //save
124    if (count($_POST) === 0) {
125        $this->tpl->set_values($task->get_assoc());
126    } else {
127        $this->model->taskFactory->update_save($task, $_POST);
128
129        $anchor   = 'z' . $task->id;
130        $redirect = true;
131    }
132} elseif ($this->get_param('action') == 'task_delete') {
133    /** @var bez\mdl\Task $task */
134    $task = $this->model->taskFactory->get_one($this->get_param('tid'), array('thread' => $thread));
135    $this->model->taskFactory->delete($task);
136    $redirect = true;
137} elseif ($this->get_param('action') == 'delete') {
138    if ($thread->type == 'issue') {
139        $redirect_to = 'threads';
140    } else {
141        $redirect_to = 'projects';
142    }
143    $this->model->threadFactory->delete($thread);
144    header('Location: ' . $this->url($redirect_to));
145}
146
147if (isset($redirect) && $redirect == true) {
148    if (isset($anchor)) {
149        $anchor = '#'.$anchor;
150    } else {
151        $anchor = '';
152    }
153    header('Location: ' . $this->url('thread', 'id', $thread->id) . $anchor);
154}
155