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                if ($task_comment->id) {
37                    $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET closing=1 WHERE id=?",
38                        $task_comment->id);
39                }
40                $notify = 'mail_task_done';
41            } elseif ($data['fn'] == 'task_reopen') {
42                $task_comment->task->set_state('opened');
43                //clean closing flags
44                $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET closing=0 WHERE task_id=?",
45                    $task_comment->task_id);
46                $notify = 'mail_task_repened';
47            }
48            //update prioirty
49            $task_comment->task->update_virutal();
50
51            if ($task_comment->task->thread_id != '') {
52                $task_comment->task->thread->update_last_activity();
53            }
54            $this->commitTransaction();
55
56            if ($notify == 'comment_added') {
57                $task_comment->mail_notify_add();
58            } elseif (isset($notify)) {
59                $task_comment->task->mail_notify_change_state($notify);
60                if ($task_comment->task->thread_id != '') {
61                    $task_comment->task->thread->mail_notify_task_state_changed($task_comment->task);
62                }
63            }
64        } catch(Exception $exception) {
65            $this->rollbackTransaction();
66        }
67
68
69    }
70
71    public function update_save(Entity $task_comment, $data) {
72
73        if ($task_comment->task->thread_id != '' && $task_comment->task->thread->state == 'closed') {
74            throw new ConsistencyViolationException('cannot add comments to closed threads');
75        }
76
77        try {
78            $this->beginTransaction();
79            parent::update_save($task_comment, $data);
80
81            $task_comment->task->update_last_activity();
82
83            $this->commitTransaction();
84        } catch(Exception $exception) {
85            $this->rollbackTransaction();
86        }
87    }
88
89    public function delete(Entity $obj) {
90
91        if ($obj->task->thread_id != '' && $obj->task->thread->state == 'closed') {
92            throw new ConsistencyViolationException('delete comments of closed threads');
93        }
94
95        try {
96            $this->beginTransaction();
97
98            parent::delete($obj);
99            $obj->task->update_last_activity();
100            //remove commentator flag
101            if ($this->count(array('task_id' => $obj->task_id, 'author' => $obj->author)) == 0) {
102                $obj->task->remove_participant_flags($obj->author, array('commentator'));
103            }
104
105            $this->commitTransaction();
106        } catch(Exception $exception) {
107            $this->rollbackTransaction();
108        }
109    }
110}