xref: /plugin/bez/mdl/ThreadFactory.php (revision b331b8925cd7271407de49196821e2fbf4786dba)
1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5use Assetic\Exception\Exception;
6
7class ThreadFactory extends Factory {
8
9    public function get_table_view() {
10        return 'thread_view';
11    }
12
13    public function get_years_scope() {
14        $r = $this->model->sqlite->query('SELECT create_date FROM thread ORDER BY id LIMIT 1');
15        $date = $this->model->sqlite->res2single($r);
16
17        //get only year
18		$first =  (int) substr($date, 0, strpos($date, '-'));
19        $last = (int) date('Y');
20
21		$years = array();
22		for ($year = $first; $year <= $last; $year++) {
23			$years[] = (string) $year;
24        }
25		return $years;
26    }
27
28    public function users_involvement($range=array()) {
29        $sql = 'SELECT user_id,
30                       SUM(original_poster),
31                       SUM(coordinator),
32                       SUM(commentator),
33                       SUM(task_assignee),
34                       COUNT(*)
35                       FROM thread_participant
36                       GROUP BY user_id
37                       ORDER BY user_id';
38
39        $r = $this->model->sqlite->query($sql);
40        return $r;
41    }
42
43    public function initial_save(Entity $thread, $data) {
44        $label_ids = array();
45        if (isset($data['label_id']) && $data['label_id'] != '') {
46            $label_ids[] = $data['label_id'];
47        }
48        try {
49            $this->beginTransaction();
50
51            parent::initial_save($thread, $data);
52
53            foreach($label_ids as $label_id) {
54                $thread->add_label($label_id);
55            }
56
57            $thread->set_participant_flags($thread->original_poster, array('original_poster', 'subscribent'));
58            if($thread->coordinator != null) {
59                $thread->set_participant_flags($thread->coordinator, array('coordinator', 'subscribent'));
60            }
61
62            if ($this->model->get_level() >= BEZ_AUTH_LEADER) {
63                $private = false;
64                if (isset($data['private'])) {
65                    $private = true;
66                }
67                $thread->set_private_flag($private);
68            }
69
70            $this->commitTransaction();
71
72            if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
73                $thread->mail_inform_coordinator();
74            } elseif ($thread->state == 'proposal') {
75                $thread->mail_inform_admins();
76            }
77
78        } catch(Exception $exception) {
79            $this->rollbackTransaction();
80        }
81    }
82
83    public function update_save(Entity $thread, $data) {
84        $prev_coordinator = $thread->coordinator;
85
86        $label_ids = array();
87        if (isset($data['label_id']) && $data['label_id'] != '') {
88            $label_ids[] = $data['label_id'];
89        }
90        try {
91            $this->beginTransaction();
92            parent::update_save($thread, $data);
93
94            $cur_label_ids = array_keys($thread->get_labels());
95            $labels_to_add = array_diff($label_ids, $cur_label_ids);
96            $labels_to_rem = array_diff($cur_label_ids, $label_ids);
97
98            foreach($labels_to_add as $label_id) {
99                $thread->add_label($label_id);
100            }
101
102            foreach($labels_to_rem as $label_id) {
103                $thread->remove_label($label_id);
104            }
105
106            if($thread->coordinator != null && $thread->coordinator != $prev_coordinator) {
107                $thread->remove_participant_flags($prev_coordinator, array('coordinator'));
108                $thread->set_participant_flags($thread->coordinator, array('subscribent', 'coordinator'));
109            }
110
111            if ($thread->acl_of('private') >= BEZ_PERMISSION_CHANGE) {
112                $private = false;
113                if (isset($data['private'])) {
114                    $private = true;
115                }
116                $thread->set_private_flag($private);
117            }
118
119            $this->commitTransaction();
120        } catch(Exception $exception) {
121            $this->rollbackTransaction();
122        }
123
124        if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
125            $thread->mail_inform_coordinator();
126        }
127    }
128}
129