xref: /plugin/bez/ctl/thread.php (revision 8a6381983135ed7de69b33e64aa0c1b16dbf69b0)
1<?php
2/** @var action_plugin_bez_default $this */
3
4use \dokuwiki\plugin\bez;
5
6if ($this->get_param('id') == '') {
7    header('Location: ' . $this->url('threads'));
8}
9
10/** @var bez\mdl\Thread $thread */
11$thread = $this->model->threadFactory->get_one($this->get_param('id'));
12$this->tpl->set('thread', $thread);
13$this->tpl->set('corrections', array());
14$this->tpl->set('thread_comments', $this->model->thread_commentFactory->get_from_thread($thread));
15$this->tpl->set('corrections', $this->model->taskFactory->get_all(array('thread_id' => $thread->id,
16                                                                      'type' => 'correction')));
17
18
19if ($this->get_param('action') == 'commcause_add') {
20
21    /** @var bez\mdl\Thread_comment $thread_comment */
22    $thread_comment = $this->model->thread_commentFactory->create_object(array('thread' => $thread));
23    $this->model->thread_commentFactory->initial_save($thread_comment, $_POST);
24
25    $anchor = 'k'.$thread_comment->id;
26    $redirect = true;
27
28} elseif ($this->get_param('action') == 'subscribe') {
29
30    $thread->set_participant_flags($this->model->user_nick, array('subscribent'));
31    $redirect = true;
32
33} elseif ($this->get_param('action') == 'unsubscribe') {
34
35    $thread->remove_participant_flags($this->model->user_nick, array('subscribent'));
36    $this->add_notification($this->getLang('unsubscribed_com'));
37    $redirect = true;
38
39} elseif ($this->get_param('action') == 'invite') {
40    $client = $_POST['client'];
41
42    $thread->set_participant_flags($client, array('subscribent'));
43    $thread->mail_notify_invite($client);
44    $this->add_notification($this->model->userFactory->get_user_email($client), $this->getLang('invitation_has_been_send'));
45
46    $redirect = true;
47} elseif ($this->get_param('action') == 'commcause_delete') {
48    /** @var bez\mdl\Thread_comment $thread_comment */
49    $thread_comment = $this->model->thread_commentFactory->get_one($this->get_param('kid'), array('thread' => $thread));
50    $this->model->thread_commentFactory->delete($thread_comment);
51
52    $redirect = true;
53} elseif ($this->get_param('action') == 'commcause_edit') {
54    /** @var bez\mdl\Thread_comment $thread_comment */
55    $thread_comment = $this->model->thread_commentFactory->get_one($this->get_param('kid'), array('thread' => $thread));
56
57    if(count($_POST) === 0) {
58        $this->tpl->set_values($thread_comment->get_assoc());
59    } else {
60        $this->model->thread_commentFactory->update_save($thread_comment, $_POST);
61
62        $anchor   = 'k' . $thread_comment->id;
63        $redirect = true;
64    }
65} elseif ($this->get_param('action') == 'task_correction_add') {
66    /** @var bez\mdl\Task $task */
67    $task = $this->model->taskFactory->create_object(array('thread' => $thread));
68    $this->tpl->set('task', $task);
69
70    //save
71    if (count($_POST) > 0) {
72        $this->model->taskFactory->initial_save($task, $_POST);
73
74        $anchor   = 'z' . $task->id;
75        $redirect = true;
76    }
77} elseif ($this->get_param('action') == 'task_edit') {
78    /** @var bez\mdl\Task $task */
79    $task = $this->model->taskFactory->get_one($this->get_param('tid'), array('thread' => $thread));
80    $this->tpl->set('task', $task);
81
82    //save
83    if (count($_POST) === 0) {
84        $this->tpl->set_values($task->get_assoc());
85    } else {
86        $this->model->taskFactory->update_save($task, $_POST);
87
88        $anchor   = 'z' . $task->id;
89        $redirect = true;
90    }
91}
92
93if (isset($redirect) && $redirect == true) {
94    if (isset($anchor)) {
95        $anchor = '#'.$anchor;
96    } else {
97        $anchor = '';
98    }
99    header('Location: ' . $this->url('thread', 'id', $thread->id) . $anchor);
100}
101
102
103
104//    $template['tid'] = isset($nparams['tid']) ? $nparams['tid'] : '-1';
105//    $template['kid'] = isset($nparams['kid']) ? $nparams['kid'] : '-1';
106//    $template['state'] = isset($nparams['state']) ? $nparams['state'] : '-1';
107//    $template['action'] = isset($nparams['action']) ? $nparams['action'] : '-default';
108
109//    $template['issue'] = $issue;
110//    $template['commcauses'] = $this->model->commcauses->get_all(
111//        array('issue' => $issue_id)
112//    );
113//
114//    $template['commcause'] = $this->model->commcauses->
115//                            create_dummy_object(array('issue' => $issue->id));
116//
117//    $template['corrections'] = $this->model->tasks->get_all(array(
118//        'issue' => $issue_id,
119//        'action' => 0,
120//    ));
121//
122//    $template['commcauses_tasks'] = array();
123//    foreach ($this->model->commcauses->get_causes_ids($issue_id) as $kid) {
124//        $template['commcauses_tasks'][$kid] = $this->model->tasks->get_all(array(
125//            'cause' => $kid,
126//        ));
127//    }
128
129
130//    $template['users'] = $this->model->users->get_all();
131//
132//    //remove userts that are subscribents already
133//    $template['users_to_invite'] = array_diff_key($template['users'], $issue->get_subscribents());
134
135
136//	$action = '';
137//	if (isset($nparams['action'])) {
138//		$action = $nparams['action'];
139//		$redirect = false;
140//		$anchor = '';
141//
142//		if ($action === 'commcause_add') {
143//
144//            $defaults = array('issue' => (string)$issue_id);
145//            if ($issue->user_is_coordinator()) {
146//                $defaults['type'] = $_POST['type'];
147//            }
148//
149//			$commcause = $this->model->commcauses->create_object($defaults);
150//
151//            $data = array('content' => $_POST['content']);
152//			$commcause->set_data($data);
153//
154//			$id = $this->model->commcauses->save($commcause);
155//
156//			$issue->add_participant($INFO['client']);
157//			$issue->add_subscribent($INFO['client']);
158//
159//			$issue->update_last_activity();
160//			$this->model->issues->save($issue);
161//
162//            $commcause->mail_notify_add($issue);
163//
164//			$anchor = 'k'.$id;
165//			$redirect = true;
166//		} elseif ($action === 'subscribe') {
167//			$issue->add_subscribent($INFO['client']);
168//			$this->model->issues->save($issue);
169//
170//			$redirect = true;
171//		} elseif ($action === 'unsubscribe') {
172//			$issue->remove_subscribent($INFO['client']);
173//			$this->model->issues->save($issue);
174//
175//            $this->add_notification($bezlang['unsubscribed_com']);
176//
177//            $redirect = true;
178//
179//        } elseif ($action === 'invite') {
180//            $client = $_POST['client'];
181//
182//			$state = $issue->add_subscribent($client);
183//            //user wasn't subscribent
184//            if ($state === true) {
185//                $this->model->issues->save($issue);
186//                $issue->mail_notify_invite($client);
187//
188//                $this->add_notification($this->model->users->get_user_email($client), $bezlang['invitation_has_been_send']);
189//
190//                $redirect = true;
191//            }
192//
193//		} elseif ($action === 'commcause_delete') {
194//			$commcause = $this->model->commcauses->get_one($template['kid']);
195//
196//			$this->model->commcauses->delete($commcause);
197//
198//			$issue->update_last_activity();
199//			$this->model->issues->save($issue);
200//
201//			$redirect = true;
202//		} elseif ($action === 'commcause_edit') {
203//			if (count($_POST) === 0) {
204//				$commcause = $this->model->commcauses->get_one($template['kid']);
205//				$template['kid'] = $commcause->id;
206//				$value = $commcause->get_assoc();
207//			} else {
208//				$commcause = $this->model->commcauses->get_one($template['kid']);
209//
210//                $data = array('content' => $_POST['content']);
211//                if ($issue->user_is_coordinator()) {
212//                    $data['type'] = $_POST['type'];
213//                }
214//
215//				$commcause->set_data($data);
216//				$this->model->commcauses->save($commcause);
217//
218//				$issue->update_last_activity();
219//				$this->model->issues->save($issue);
220//
221//				$anchor = 'k'.$commcause->id;
222//				$redirect = true;
223//			}
224//
225//        } elseif ($action === 'commcause_edit_metadata') {
226//            if (count($_POST) === 0) {
227//				$commcause = $this->model->commcauses->get_one($template['kid']);
228//				$template['kid'] = $commcause->id;
229//				$value = $commcause->get_assoc(array('datetime', 'reporter'));
230//                $unix = strtotime($value['datetime']);
231//                $value['date'] = date('Y-m-d', $unix);
232//                $value['time'] = date('H:i:s', $unix);
233//			} else {
234//				$commcause = $this->model->commcauses->get_one($template['kid']);
235//                $_POST['datetime'] = $_POST['date']. ' '.$_POST['time'];
236//				$commcause->set_meta($_POST);
237//				$this->model->commcauses->save($commcause);
238//
239//				$anchor = 'k'.$commcause->id;
240//				$redirect = true;
241//			}
242//		} elseif ($action === 'issue_close') {
243//			$value['opinion'] = $issue->opinion;
244//		} elseif ($action == 'issue_close_confirm') {
245//			$issue->set_state($_POST);
246//			$this->model->issues->save($issue);
247//
248//            $issue->mail_notify_change_state();
249//
250//			$redirect = true;
251//		} elseif ($action === 'reopen') {
252//			$issue->set_state(array('state' => '0'));
253//			$this->model->issues->save($issue);
254//
255//            $issue->mail_notify_change_state();
256//
257//            $redirect = true;
258//        } elseif ($action === 'issue_edit_metadata') {
259//            if (count($_POST) > 0) {
260//
261//                $_POST['last_activity'] = $_POST['last_activity_date']. ' '.$_POST['last_activity_time'];
262//
263//                $issue->set_meta($_POST);
264//                $this->model->issues->save($issue);
265//
266//                $redirect = true;
267//            } else {
268//                $value = $issue->get_assoc();
269//                $value['date'] = date('Y-m-d', (int)$value['date']);
270//                $value['last_mod'] = date('Y-m-d', (int)$value['last_mod']);
271//
272//                $unix = strtotime($value['last_activity']);
273//                $value['last_activity_date'] = date('Y-m-d', $unix);
274//                $value['last_activity_time'] = date('H:i:s', $unix);
275//            }
276// 		} elseif (strpos($action, 'task') === 0) {
277//            $template['task'] = $this->model->tasks->
278//                    create_dummy_object(array('issue' => $issue->id));
279//			$template['users'] = $this->model->users->get_all();
280//			$template['tasktypes'] = $this->model->tasktypes->get_all();
281//
282//			if (count($_POST) > 0) {
283//				if (!isset($_POST['all_day_event'])) {
284//					$_POST['all_day_event'] = '0';
285//				}
286//			}
287//
288//			if ($action === 'task_reopen') {
289//				$task = $this->model->tasks->get_one($nparams['tid']);
290//				$task->set_state(array('state' => '0'));
291//				$this->model->tasks->save($task);
292//
293//				$issue->update_last_activity();
294//				$this->model->issues->save($issue);
295//
296//                $task->mail_notify_subscribents($template['issue'],
297//                        array('action' => $bezlang['mail_task_reopened']));
298//
299//				$redirect = true;
300//				$anchor = 'z'.$task->id;
301//
302//			} elseif ($action === 'task_edit') {
303//				$template['tid'] = $nparams['tid'];
304//
305//				$template['causes'] = $this->model->commcauses->get_all(array(
306//					'issue' => $issue_id,
307//					'type' => array('!=', '0'),
308//				));
309//
310//				$task = $this->model->tasks->get_one($template['tid']);
311//				$value = $task->get_assoc();
312//
313//            } elseif ($action === 'task_change_state') {
314//                $template['tid'] = $nparams['tid'];
315//				$task = $this->model->tasks->get_one($template['tid']);
316//				$value = array('reason' => $task->reason);
317//			} elseif($action === 'task_edit_metadata') {
318//
319//                $task = $this->model->tasks->get_one($template['tid']);
320//
321//                if (count($_POST) > 0) {
322//                    $task->set_meta($_POST);
323//                    $this->model->tasks->save($task);
324//
325//                    header("Location: ?id=bez:issue:id:$issue_id#z".$task->id);
326//                } else {
327//                    $value = $task->get_assoc();
328//                    $value['date'] = date('Y-m-d', (int)$value['date']);
329//                    $value['close_date'] = date('Y-m-d', (int)$value['close_date']);
330//                }
331//            }
332//
333//			if (count($_POST) > 0) {
334//				//ends with
335//				if (substr($action, -strlen('add')) === 'add') {
336//					$defaults = array('issue' => (string)$issue_id);
337//					if ($template['kid'] !== '-1') {
338//						$defaults['cause'] = $template['kid'];
339//					}
340//					$task = $this->model->tasks->create_object($defaults);
341//
342//					$task->set_data($_POST);
343//					$id = $this->model->tasks->save($task);
344//
345//					$issue->add_participant($task->executor);
346//					$issue->add_subscribent($task->executor);
347//
348//					$issue->update_last_activity();
349//					$this->model->issues->save($issue);
350//
351//                    $task->mail_notify_add($issue);
352//
353//					$anchor = 'z'.$id;
354//					$redirect = true;
355//				} elseif ($action === 'task_change_state') {
356//					$task = $this->model->tasks->get_one($template['tid']);
357//
358//                    if (isset($_POST['no_evaluation'])) {
359//                        $_POST['reason'] = '';
360//                    }
361//
362//					$task->set_state(array(
363//								'state' => $nparams['state'],
364//								'reason' => $_POST['reason'])
365//							);
366//					$this->model->tasks->save($task);
367//
368//					$issue->update_last_activity();
369//					$this->model->issues->save($issue);
370//
371//                    $task->mail_notify_subscribents($template['issue'],
372//                        array('action' => $bezlang['mail_task_change_state']));
373//
374//					$anchor = 'z'.$task->id;
375//					$redirect = true;
376//				} elseif ($action === 'task_edit') {
377//					$task = $this->model->tasks->get_one($template['tid']);
378//					$task->set_data($_POST);
379//					$this->model->tasks->save($task);
380//
381//					$issue->add_participant($task->executor);
382//					$issue->add_subscribent($task->executor);
383//
384//					//don't upgrade last activity!!!
385//					$anchor = 'z'.$task->id;
386//					$redirect = true;
387//				}
388//			}
389//		}
390//
391//		if ($redirect) {
392//			if ($anchor !== '') {
393//				$anchor = '#'.$anchor;
394//			}
395//			header("Location: ?id=bez:issue:id:$issue_id$anchor");
396//		}
397//	}
398
399
400//} catch (ValidationException $e) {
401//	$errors = $e->get_errors();
402//	$value = $_POST;
403//} catch (DBException $e) {
404//	echo nl2br($e);
405////	header("Location: ?id=bez:issue:id:$issue_id");
406//}
407
408
409