xref: /plugin/bez/mdl/Thread_commentFactory.php (revision e8827d732aaeeee6f7b703c5654f86ca97056383)
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['content'] != '') {
30                $thread_comment->set_data($data);
31                $this->save($thread_comment);
32                $thread_comment->thread->set_participant_flags($thread_comment->author, array('subscribent', 'commentator'));
33            }
34
35            if ($data['fn'] == 'thread_close') {
36                $thread_comment->thread->set_state('closed');
37            } elseif ($data['fn'] == 'thread_reject') {
38                $thread_comment->thread->set_state('rejected');
39            } elseif ($data['fn'] == 'thread_reopen') {
40                $thread_comment->thread->set_state('opened');
41            }
42
43            $thread_comment->thread->update_last_activity();
44
45            $this->commitTransaction();
46        } catch(Exception $exception) {
47            $this->rollbackTransaction();
48        }
49
50        $thread_comment->mail_notify_add();
51    }
52
53    public function update_save(Entity $thread_comment, $data) {
54        parent::update_save($thread_comment, $data);
55
56        $thread_comment->set_data($data);
57        try {
58            $this->beginTransaction();
59            $this->save($thread_comment);
60
61            $thread_comment->thread->update_last_activity();
62
63            $this->commitTransaction();
64        } catch(Exception $exception) {
65            $this->rollbackTransaction();
66        }
67    }
68
69    public function delete(Entity $obj) {
70        try {
71            $this->beginTransaction();
72
73            parent::delete($obj);
74            $obj->thread->update_last_activity();
75
76            $this->commitTransaction();
77        } catch(Exception $exception) {
78            $this->rollbackTransaction();
79        }
80    }
81}