xref: /plugin/bez/mdl/Task_commentFactory.php (revision f7519ef1d6587610f3d1319f79256a91d679c6e2)
1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5use dokuwiki\plugin\bez\meta\ConsistencyViolationException;
6
7class Task_commentFactory extends Factory {
8
9    public function get_from_task(Task $task) {
10        return $this->get_all(array('task_id' => $task->id), $orderby='', $desc=true, array('task' => $task));
11    }
12
13    /**
14     * @param Thread_comment $thread_comment
15     * @param                $data
16     * @throws \Exception
17     */
18    public function initial_save(Entity $task_comment, $data) {
19
20        if ($task_comment->task->thread_id != '' && $task_comment->task->thread->state == 'closed') {
21            throw new ConsistencyViolationException('cannot add comments to closed threads');
22        }
23
24        try {
25            $this->beginTransaction();
26
27            //if empty content and task_do, do not save the comment
28            if ($data['fn'] == 'comment_add' || $data['content'] != '') {
29                parent::initial_save($task_comment, $data);
30                $task_comment->task->set_participant_flags($task_comment->author, array('subscribent', 'commentator'));
31            }
32
33            if ($data['fn'] == 'task_do') {
34                $task_comment->task->set_state('done');
35            } elseif ($data['fn'] == 'task_reopen') {
36                $task_comment->task->set_state('opened');
37            }
38
39            if ($task_comment->task->thread_id != '') {
40                $task_comment->task->thread->update_last_activity();
41            }
42
43            $this->commitTransaction();
44        } catch(Exception $exception) {
45            $this->rollbackTransaction();
46        }
47
48        $task_comment->mail_notify_add();
49    }
50
51    public function update_save(Entity $task_comment, $data) {
52
53        if ($task_comment->task->thread_id != '' && $task_comment->task->thread->state == 'closed') {
54            throw new ConsistencyViolationException('cannot add comments to closed threads');
55        }
56
57        try {
58            $this->beginTransaction();
59            parent::update_save($task_comment, $data);
60
61            $task_comment->task->update_last_activity();
62
63            $this->commitTransaction();
64        } catch(Exception $exception) {
65            $this->rollbackTransaction();
66        }
67    }
68
69    public function delete(Entity $obj) {
70
71        if ($obj->task->thread_id != '' && $obj->task->thread->state == 'closed') {
72            throw new ConsistencyViolationException('delete comments of closed threads');
73        }
74
75        try {
76            $this->beginTransaction();
77
78            parent::delete($obj);
79            $obj->task->update_last_activity();
80            //remove commentator flag
81            if ($this->count(array('task_id' => $obj->task_id, 'author' => $obj->author)) == 0) {
82                $obj->task->remove_participant_flags($obj->author, array('commentator'));
83            }
84
85            $this->commitTransaction();
86        } catch(Exception $exception) {
87            $this->rollbackTransaction();
88        }
89    }
90}