xref: /plugin/bez/mdl/Thread_commentFactory.php (revision 522c019c0bd8bcd6e522fddaa2cf94cce8e0780d)
1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5use dokuwiki\plugin\bez\meta\ConsistencyViolationException;
6
7class Thread_commentFactory extends Factory {
8
9    public function get_table_view() {
10        return 'thread_comment_view';
11    }
12
13    public function get_from_thread(Thread $thread, $filters=array(), $orderby='', $limit=false) {
14        $filters['thread_id'] = $thread->id;
15        return $this->get_all($filters, $orderby, array('thread' => $thread), $limit);
16    }
17
18    /**
19     * @param Thread_comment $thread_comment
20     * @param                $data
21     * @throws \Exception
22     */
23    public function initial_save(Entity $thread_comment, $data) {
24        try {
25            $this->beginTransaction();
26
27            if ($data['fn'] == 'comment_add' ||
28                $data['fn'] == 'thread_close' ||
29                $data['content'] != '') {
30                parent::initial_save($thread_comment, $data);
31                $thread_comment->thread->set_participant_flags($thread_comment->author, array('subscribent', 'commentator'));
32                $notify = 'comment_added';
33            }
34
35            if ($data['fn'] == 'thread_close') {
36                $thread_comment->thread->set_state('closed');
37                $notify = 'mail_thread_closed';
38            } elseif ($data['fn'] == 'thread_reject') {
39                $thread_comment->thread->set_state('rejected');
40                $notify = 'mail_thread_rejected';
41            } elseif ($data['fn'] == 'thread_reopen') {
42                $thread_comment->thread->set_state('opened');
43                $notify = 'mail_thread_reopened';
44            }
45
46            $thread_comment->thread->update_last_activity();
47
48            $this->commitTransaction();
49
50            if ($notify == 'comment_added') {
51                $thread_comment->mail_notify_add();
52            } elseif (isset($notify)) {
53                $thread_comment->thread->mail_notify_change_state($notify);
54            }
55
56        } catch(Exception $exception) {
57            $this->rollbackTransaction();
58        }
59    }
60
61    public function update_save(Entity $thread_comment, $data) {
62        try {
63            $this->beginTransaction();
64
65            $prev_type = $thread_comment->type;
66
67            parent::update_save($thread_comment, $data);
68
69            //update task types
70            if ($thread_comment->type != 'comment' && $thread_comment->type != $prev_type) {
71                if ($thread_comment->type == 'cause_real') {
72                    $task_type = 'corrective';
73                } else {
74                    $task_type = 'preventive';
75                }
76                $this->model->sqlite->query('UPDATE task SET type=? WHERE thread_comment_id=?',
77                                            $task_type, $thread_comment->id);
78
79            }
80
81            $thread_comment->thread->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_count > 0) {
92            throw new ConsistencyViolationException('cannot delete when task are assigned');
93        }
94
95        try {
96            $this->beginTransaction();
97
98            parent::delete($obj);
99            $obj->thread->update_last_activity();
100            //remove commentator flag
101            if ($this->count(array('thread_id' => $obj->thread_id, 'author' => $obj->author)) == 0) {
102                $obj->thread->remove_participant_flags($obj->author, array('commentator'));
103            }
104
105            $this->commitTransaction();
106        } catch(Exception $exception) {
107            $this->rollbackTransaction();
108        }
109    }
110}