xref: /plugin/bez/ctl/thread.php (revision 53df74e7ac5ae4234aac1fa716a33878a039026f)
1<?php
2/** @var action_plugin_bez $this */
3
4use \dokuwiki\plugin\bez;
5
6if ($this->model->acl->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);
22$this->tpl->set('thread_comments', $this->model->thread_commentFactory->get_from_thread($thread));
23$this->tpl->set('tasks', $this->model->taskFactory->get_from_thread($thread));
24$this->tpl->set('task_programs',  $this->model->task_programFactory->get_all());
25
26
27if ($this->get_param('action') == 'commcause_add') {
28
29    /** @var bez\mdl\Thread_comment $thread_comment */
30    $thread_comment = $this->model->thread_commentFactory->create_object(array('thread' => $thread));
31    $this->model->thread_commentFactory->initial_save($thread_comment, $_POST);
32
33    $anchor = 'k'.$thread_comment->id;
34    $redirect = true;
35
36} elseif ($this->get_param('action') == 'subscribe') {
37
38    $thread->set_participant_flags($this->model->user_nick, array('subscribent'));
39    $redirect = true;
40
41} elseif ($this->get_param('action') == 'unsubscribe') {
42
43    $thread->remove_participant_flags($this->model->user_nick, array('subscribent'));
44    $this->add_notification($this->getLang('unsubscribed_com'));
45    $redirect = true;
46
47} elseif ($this->get_param('action') == 'invite') {
48    $client = $_POST['client'];
49
50    $thread->invite($client);
51
52    $this->add_notification($this->model->userFactory->get_user_email($client), $this->getLang('invitation_has_been_send'));
53
54    $redirect = true;
55} elseif ($this->get_param('action') == 'commcause_delete') {
56    /** @var bez\mdl\Thread_comment $thread_comment */
57    $thread_comment = $this->model->thread_commentFactory->get_one($this->get_param('kid'), array('thread' => $thread));
58    $this->model->thread_commentFactory->delete($thread_comment);
59
60    $redirect = true;
61} elseif ($this->get_param('action') == 'commcause_edit') {
62    /** @var bez\mdl\Thread_comment $thread_comment */
63    $thread_comment = $this->model->thread_commentFactory->get_one($this->get_param('kid'), array('thread' => $thread));
64
65    if(count($_POST) === 0) {
66        $this->tpl->set_values($thread_comment->get_assoc());
67    } else {
68        $this->model->thread_commentFactory->update_save($thread_comment, $_POST);
69
70        $anchor   = 'k' . $thread_comment->id;
71        $redirect = true;
72    }
73} elseif ($this->get_param('action') == 'task_add') {
74
75    $defaults = array('thread' => $thread);
76
77    if ($this->get_param('kid') != '') {
78        $thread_comment = $this->model->thread_commentFactory->get_one($this->get_param('kid'), array('thread' => $thread));
79        $defaults['thread_comment'] = $thread_comment;
80    }
81    /** @var bez\mdl\Task $task */
82    $task = $this->model->taskFactory->create_object($defaults);
83    $this->tpl->set('task', $task);
84
85    //save
86    if (count($_POST) > 0) {
87        $this->model->taskFactory->initial_save($task, $_POST);
88
89        $anchor   = 'z' . $task->id;
90        $redirect = true;
91    }
92} elseif ($this->get_param('action') == 'task_edit') {
93    /** @var bez\mdl\Task $task */
94    $task = $this->model->taskFactory->get_one($this->get_param('tid'), array('thread' => $thread));
95    $this->tpl->set('task', $task);
96
97    //save
98    if (count($_POST) === 0) {
99        $this->tpl->set_values($task->get_assoc());
100    } else {
101        $this->model->taskFactory->update_save($task, $_POST);
102
103        $anchor   = 'z' . $task->id;
104        $redirect = true;
105    }
106}
107
108if (isset($redirect) && $redirect == true) {
109    if (isset($anchor)) {
110        $anchor = '#'.$anchor;
111    } else {
112        $anchor = '';
113    }
114    header('Location: ' . $this->url('thread', 'id', $thread->id) . $anchor);
115}
116