xref: /plugin/bez/mdl/Thread_commentFactory.php (revision 038c5d4a6a969d879580f53f12ade1bfabd5474f)
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            }
33
34            if ($data['fn'] == 'thread_close') {
35                $thread_comment->thread->set_state('closed');
36            } elseif ($data['fn'] == 'thread_reject') {
37                $thread_comment->thread->set_state('rejected');
38            } elseif ($data['fn'] == 'thread_reopen') {
39                $thread_comment->thread->set_state('opened');
40            }
41
42            $thread_comment->thread->update_last_activity();
43
44            $this->commitTransaction();
45        } catch(Exception $exception) {
46            $this->rollbackTransaction();
47        }
48
49        $thread_comment->mail_notify_add();
50    }
51
52    public function update_save(Entity $thread_comment, $data) {
53        try {
54            $this->beginTransaction();
55            parent::update_save($thread_comment, $data);
56
57            $thread_comment->thread->update_last_activity();
58
59            $this->commitTransaction();
60        } catch(Exception $exception) {
61            $this->rollbackTransaction();
62        }
63    }
64
65    public function delete(Entity $obj) {
66
67        if ($obj->task_count > 0) {
68            throw new ConsistencyViolationException('cannot delete when task are assigned');
69        }
70
71        try {
72            $this->beginTransaction();
73
74            parent::delete($obj);
75            $obj->thread->update_last_activity();
76            //remove commentator flag
77            if ($this->count(array('thread_id' => $obj->thread_id, 'author' => $obj->author)) == 0) {
78                $obj->thread->remove_participant_flags($obj->author, array('commentator'));
79            }
80
81            $this->commitTransaction();
82        } catch(Exception $exception) {
83            $this->rollbackTransaction();
84        }
85    }
86}