xref: /plugin/bez/mdl/ThreadFactory.php (revision f4ba43dc4c2ffd4a56d5db3392811439ede14cfb)
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() {
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        } catch(Exception $exception) {
72            $this->rollbackTransaction();
73        }
74
75        if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
76            $thread->mail_inform_coordinator();
77        }
78    }
79
80    public function update_save(Entity $thread, $data) {
81        $prev_coordinator = $thread->coordinator;
82
83        $label_ids = array();
84        if (isset($data['label_id']) && $data['label_id'] != '') {
85            $label_ids[] = $data['label_id'];
86        }
87        try {
88            $this->beginTransaction();
89            parent::update_save($thread, $data);
90
91            $cur_label_ids = array_keys($thread->get_labels());
92            $labels_to_add = array_diff($label_ids, $cur_label_ids);
93            $labels_to_rem = array_diff($cur_label_ids, $label_ids);
94
95            foreach($labels_to_add as $label_id) {
96                $thread->add_label($label_id);
97            }
98
99            foreach($labels_to_rem as $label_id) {
100                $thread->remove_label($label_id);
101            }
102
103            if($thread->coordinator != null && $thread->coordinator != $prev_coordinator) {
104                $thread->remove_participant_flags($prev_coordinator, array('coordinator'));
105                $thread->set_participant_flags($thread->coordinator, array('subscribent', 'coordinator'));
106            }
107
108            if ($thread->acl_of('private') >= BEZ_PERMISSION_CHANGE) {
109                $private = false;
110                if (isset($data['private'])) {
111                    $private = true;
112                }
113                $thread->set_private_flag($private);
114            }
115
116            $this->commitTransaction();
117        } catch(Exception $exception) {
118            $this->rollbackTransaction();
119        }
120
121        if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
122            $thread->mail_inform_coordinator();
123        }
124    }
125}
126