xref: /plugin/bez/mdl/ThreadFactory.php (revision a0cd8c785f18b483f73582b411767428d04a78f6)
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            //save thread as project
52            if ($data['type'] == 'project') {
53                $thread->type = 'project';
54            }
55
56            parent::initial_save($thread, $data);
57
58            foreach($label_ids as $label_id) {
59                $thread->add_label($label_id);
60            }
61
62            $thread->set_participant_flags($thread->original_poster, array('original_poster', 'subscribent'));
63            if($thread->coordinator != null) {
64                $thread->set_participant_flags($thread->coordinator, array('coordinator', 'subscribent'));
65            }
66
67            if ($this->model->get_level() >= BEZ_AUTH_LEADER) {
68                $private = false;
69                if (isset($data['private'])) {
70                    $private = true;
71                }
72                $thread->set_private_flag($private);
73            }
74
75            $this->commitTransaction();
76        } catch(Exception $exception) {
77            $this->rollbackTransaction();
78        }
79
80        if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
81            $thread->mail_inform_coordinator();
82        }
83    }
84
85    public function update_save(Entity $thread, $data) {
86        $prev_coordinator = $thread->coordinator;
87
88        $label_ids = array();
89        if (isset($data['label_id']) && $data['label_id'] != '') {
90            $label_ids[] = $data['label_id'];
91        }
92        try {
93            $this->beginTransaction();
94            parent::update_save($thread, $data);
95
96            $cur_label_ids = array_keys($thread->get_labels());
97            $labels_to_add = array_diff($label_ids, $cur_label_ids);
98            $labels_to_rem = array_diff($cur_label_ids, $label_ids);
99
100            foreach($labels_to_add as $label_id) {
101                $thread->add_label($label_id);
102            }
103
104            foreach($labels_to_rem as $label_id) {
105                $thread->remove_label($label_id);
106            }
107
108            if($thread->coordinator != null && $thread->coordinator != $prev_coordinator) {
109                $thread->remove_participant_flags($prev_coordinator, array('coordinator'));
110                $thread->set_participant_flags($thread->coordinator, array('subscribent', 'coordinator'));
111            }
112
113            if ($thread->acl_of('private') >= BEZ_PERMISSION_CHANGE) {
114                $private = false;
115                if (isset($data['private'])) {
116                    $private = true;
117                }
118                $thread->set_private_flag($private);
119            }
120
121            $this->commitTransaction();
122        } catch(Exception $exception) {
123            $this->rollbackTransaction();
124        }
125
126        if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
127            $thread->mail_inform_coordinator();
128        }
129    }
130}
131