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['fn'] == 'thread_reject' ||
30                $data['content'] != '') {
31                parent::initial_save($thread_comment, $data);
32                $thread_comment->thread->set_participant_flags($thread_comment->author, array('subscribent', 'commentator'));
33                $notify = 'comment_added';
34            }
35
36            if ($data['fn'] == 'thread_close') {
37                $thread_comment->thread->set_state('closed');
38                $notify = 'mail_thread_closed';
39            } elseif ($data['fn'] == 'thread_reject') {
40                $thread_comment->thread->set_state('rejected');
41                $notify = 'mail_thread_rejected';
42            } elseif ($data['fn'] == 'thread_reopen') {
43                $thread_comment->thread->set_state('opened');
44                $notify = 'mail_thread_reopened';
45            }
46
47            $thread_comment->thread->update_last_activity();
48
49            $this->commitTransaction();
50
51            if ($notify == 'comment_added') {
52                $thread_comment->mail_notify_add();
53            } elseif (isset($notify)) {
54                $thread_comment->thread->mail_notify_change_state($notify);
55            }
56
57        } catch(Exception $exception) {
58            $this->rollbackTransaction();
59        }
60    }
61
62    public function update_save(Entity $thread_comment, $data) {
63        try {
64            $this->beginTransaction();
65
66            $prev_type = $thread_comment->type;
67
68            parent::update_save($thread_comment, $data);
69
70            //update task types
71            if ($thread_comment->type != 'comment' && $thread_comment->type != $prev_type) {
72                if ($thread_comment->type == 'cause') {
73                    $task_type = 'corrective';
74                } else {
75                    $task_type = 'preventive';
76                }
77                $this->model->sqlite->query('UPDATE task SET type=? WHERE thread_comment_id=?',
78                                            $task_type, $thread_comment->id);
79
80            }
81
82            $thread_comment->thread->update_last_activity();
83
84            $this->commitTransaction();
85        } catch(Exception $exception) {
86            $this->rollbackTransaction();
87        }
88    }
89
90    public function delete(Entity $obj) {
91
92        if ($obj->task_count > 0) {
93            throw new ConsistencyViolationException('cannot delete when task are assigned');
94        }
95
96        try {
97            $this->beginTransaction();
98
99            parent::delete($obj);
100            $obj->thread->update_last_activity();
101            //remove commentator flag
102            if ($this->count(array('thread_id' => $obj->thread_id, 'author' => $obj->author)) == 0) {
103                $obj->thread->remove_participant_flags($obj->author, array('commentator'));
104            }
105
106            $this->commitTransaction();
107        } catch(Exception $exception) {
108            $this->rollbackTransaction();
109        }
110    }
111
112    public function report(\DatePeriod $period=NULL) {
113        if ($period) {
114            $from = $period->getStartDate()->format(\DateTime::ISO8601);
115            $to = $period->getEndDate()->format(\DateTime::ISO8601);
116
117            $sql = "SELECT type, COUNT(type) AS 'cnt'
118                        FROM thread_comment
119                        WHERE type != 'comment'
120                            AND create_date BETWEEN ? AND ?
121                        GROUP BY type";
122            $r = $this->model->sqlite->query($sql, $from, $to);
123        } else {
124            $sql = "SELECT type, COUNT(type) AS 'cnt'
125                        FROM thread_comment
126                        WHERE type != 'comment'
127                        GROUP BY type";
128            $r = $this->model->sqlite->query($sql);
129        }
130
131        $counted = [
132            'cause' => 0,
133            'risk' => 0,
134            'opportunity' => 0,
135            'all' => 0
136        ];
137
138        $result = $r->fetchAll();
139        foreach ($result as $row) {
140            $cnt = (int) $row['cnt'];
141            $counted[$row['type']] += $cnt;
142            $counted['all'] += $cnt;
143        }
144
145        return $counted;
146    }
147}
148