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