xref: /plugin/bez/mdl/Thread_commentFactory.php (revision ff14b1073c2dab2f863cab3b8baf8b1a01f7993a)
1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5class Thread_commentFactory extends Factory {
6
7    protected function select_query() {
8        return "SELECT thread_comment.*, thread.coordinator
9                FROM thread_comment JOIN thread ON thread_comment.thread_id=thread.id";
10    }
11
12    public function get_from_thread(Thread $thread, $filters=array(), $orderby='', $desc=true, $limit=false) {
13        $filters['thread_id'] = $thread->id;
14        return $this->get_all($filters, $orderby, $desc, array('thread' => $thread), $limit);
15    }
16
17    /**
18     * @param Thread_comment $thread_comment
19     * @param                $data
20     * @throws \Exception
21     */
22    public function initial_save(Entity $thread_comment, $data) {
23        parent::initial_save($thread_comment, $data);
24
25        try {
26            $this->beginTransaction();
27
28            if ($data['fn'] == 'comment_add' ||
29                $data['fn'] == 'thread_close' ||
30                $data['content'] != '') {
31                $thread_comment->set_data($data);
32                $this->save($thread_comment);
33                $thread_comment->thread->set_participant_flags($thread_comment->author, array('subscribent', 'commentator'));
34            }
35
36            if ($data['fn'] == 'thread_close') {
37                $thread_comment->thread->set_state('closed');
38            } elseif ($data['fn'] == 'thread_reject') {
39                $thread_comment->thread->set_state('rejected');
40            } elseif ($data['fn'] == 'thread_reopen') {
41                $thread_comment->thread->set_state('opened');
42            }
43
44            $thread_comment->thread->update_last_activity();
45
46            $this->commitTransaction();
47        } catch(Exception $exception) {
48            $this->rollbackTransaction();
49        }
50
51        $thread_comment->mail_notify_add();
52    }
53
54    public function update_save(Entity $thread_comment, $data) {
55        parent::update_save($thread_comment, $data);
56
57        $thread_comment->set_data($data);
58        try {
59            $this->beginTransaction();
60            $this->save($thread_comment);
61
62            $thread_comment->thread->update_last_activity();
63
64            $this->commitTransaction();
65        } catch(Exception $exception) {
66            $this->rollbackTransaction();
67        }
68    }
69
70    public function delete(Entity $obj) {
71        try {
72            $this->beginTransaction();
73
74            parent::delete($obj);
75            $obj->thread->update_last_activity();
76
77            $this->commitTransaction();
78        } catch(Exception $exception) {
79            $this->rollbackTransaction();
80        }
81    }
82}