1<?php
2/** @var action_plugin_bez $this */
3
4use \dokuwiki\plugin\bez;
5
6if ($this->model->get_level() < BEZ_AUTH_USER) {
7    throw new bez\meta\PermissionDeniedException();
8}
9
10define('BEZ_THREAD_FILTERS_COOKIE_NAME', 'plugin__bez_thread_filters_' . $this->tpl->action());
11
12if (count($_POST) > 0) {
13    $raw_filters = $_POST;
14} elseif (empty($this->params) && isset($_COOKIE[BEZ_THREAD_FILTERS_COOKIE_NAME])) {
15	$raw_filters = json_decode($_COOKIE[BEZ_THREAD_FILTERS_COOKIE_NAME], true);
16}
17
18if (isset($raw_filters)) {
19    //save filters
20    setcookie(BEZ_THREAD_FILTERS_COOKIE_NAME, json_encode($raw_filters));
21
22    $filters = array_filter($raw_filters, function($v) {
23       return $v !== '-all' && $v !== '';
24    });
25
26    if (empty($filters)) {
27        $filters['year'] = '-all';
28    }
29
30	header('Location: '.$this->url($this->get_action(), $filters));
31} else {
32    $filters = $this->params;
33}
34
35$this->tpl->set_values($filters);
36
37$years = $this->model->threadFactory->get_years_scope();
38
39//some filters are just copied
40$db_filters = array_filter($filters, function ($k) {
41    return in_array($k, array('original_poster', 'state', 'label_id', 'coordinator'));
42}, ARRAY_FILTER_USE_KEY);
43
44//-none filters become empty filters
45$db_filters = array_map(function($v) {
46    if ($v === '-none') {
47        return '';
48    }
49    return $v;
50}, $db_filters);
51
52//don't show rejecetd by default
53if (!isset($db_filters['state'])) {
54    $db_filters['state'] = array('!=', 'rejected');
55}
56
57if (isset($filters['year']) && $filters['year'] !== '-all') {
58    $year = $filters['year'];
59
60    $start_day = "$year-01-01";
61    $end_day = "$year-12-31";
62
63    $db_filters['create_date'] = array('BETWEEN', array($start_day, $end_day), array('date'));
64}
65
66if (isset($filters['coordinator']) &&
67        substr($filters['coordinator'], 0, 1) === '@') {
68    $group = substr($filters['coordinator'], 1);
69    $db_filters['coordinator'] = array('OR', $this->model->userFactory->users_of_group($group));
70}
71
72if (isset($filters['original_poster']) &&
73    substr($filters['original_poster'], 0, 1) === '@') {
74    $group = substr($filters['original_poster'], 1);
75    $db_filters['original_poster'] = array('OR', $this->model->userFactory->users_of_group($group));
76}
77
78if (isset($filters['title'])) {
79    $title = preg_replace('/\s/', '%', $filters['title']);
80    $db_filters['title'] = array('LIKE', "%$title%");
81}
82
83if (isset($filters['content'])) {
84    $content = preg_replace('/\s/', '%', $filters['content']);
85    $db_filters['content'] = array('LIKE', "%$content%");
86}
87
88if (isset($filters['has_corrective'])) {
89    $db_filters['corrective_count'] = array('>', 0);
90}
91
92if (isset($filters['has_preventive'])) {
93    $db_filters['preventive_count'] = array('>', 0);
94}
95
96
97$orderby = array('sort', 'priority DESC', 'create_date DESC');
98if (isset($filters['sort_open']) && $filters['sort_open'] == 'on') {
99    $orderby = 'id DESC';
100}
101
102if ($this->get_action() == 'threads') {
103    $db_filters['type'] = 'issue';
104} else {
105    $db_filters['type'] = 'project';
106}
107
108$threads = $this->model->threadFactory->get_all($db_filters, $orderby);
109
110$this->tpl->set('labels', $this->model->labelFactory->get_all());
111$this->tpl->set('threads', $threads);
112$this->tpl->set('years', $years);
113