xref: /plugin/bez/mdl/Task_commentFactory.php (revision 6f38077355681f273cc9db672efab6fff436baea)
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), '', 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                $notify = 'comment_added';
31                $task_comment->task->set_participant_flags($task_comment->author, array('subscribent', 'commentator'));
32            }
33
34            if ($data['fn'] == 'task_do') {
35                $task_comment->task->set_state('done');
36                $notify = 'mail_task_done';
37            } elseif ($data['fn'] == 'task_reopen') {
38                $task_comment->task->set_state('opened');
39                $notify = 'mail_task_repened';
40            }
41            //update prioirty
42            $task_comment->task->update_virutal();
43
44            if ($task_comment->task->thread_id != '') {
45                $task_comment->task->thread->update_last_activity();
46            }
47            $this->commitTransaction();
48
49            if ($notify == 'comment_added') {
50                $task_comment->mail_notify_add();
51            } elseif (isset($notify)) {
52                $task_comment->task->mail_notify_change_state($notify);
53                if ($task_comment->task->thread_id != '') {
54                    $task_comment->task->thread->mail_notify_task_state_changed($task_comment->task);
55                }
56            }
57        } catch(Exception $exception) {
58            $this->rollbackTransaction();
59        }
60
61
62    }
63
64    public function update_save(Entity $task_comment, $data) {
65
66        if ($task_comment->task->thread_id != '' && $task_comment->task->thread->state == 'closed') {
67            throw new ConsistencyViolationException('cannot add comments to closed threads');
68        }
69
70        try {
71            $this->beginTransaction();
72            parent::update_save($task_comment, $data);
73
74            $task_comment->task->update_last_activity();
75
76            $this->commitTransaction();
77        } catch(Exception $exception) {
78            $this->rollbackTransaction();
79        }
80    }
81
82    public function delete(Entity $obj) {
83
84        if ($obj->task->thread_id != '' && $obj->task->thread->state == 'closed') {
85            throw new ConsistencyViolationException('delete comments of closed threads');
86        }
87
88        try {
89            $this->beginTransaction();
90
91            parent::delete($obj);
92            $obj->task->update_last_activity();
93            //remove commentator flag
94            if ($this->count(array('task_id' => $obj->task_id, 'author' => $obj->author)) == 0) {
95                $obj->task->remove_participant_flags($obj->author, array('commentator'));
96            }
97
98            $this->commitTransaction();
99        } catch(Exception $exception) {
100            $this->rollbackTransaction();
101        }
102    }
103}