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