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