xref: /plugin/bez/ctl/threads.php (revision 14a1f0a435358d79e2e5814db93baefc0cfe6877)
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
52if (isset($filters['year']) && $filters['year'] !== '-all') {
53    $year = $filters['year'];
54
55    $start_day = "$year-01-01";
56    $end_day = "$year-12-31";
57
58    $db_filters['create_date'] = array('BETWEEN', array($start_day, $end_day), array('date'));
59}
60
61if (isset($filters['coordinator']) &&
62        substr($filters['coordinator'], 0, 1) === '@') {
63    $group = substr($filters['coordinator'], 1);
64    $db_filters['coordinator'] = array('OR', $this->model->userFactory->users_of_group($group));
65}
66
67if (isset($filters['original_poster']) &&
68    substr($filters['original_poster'], 0, 1) === '@') {
69    $group = substr($filters['original_poster'], 1);
70    $db_filters['original_poster'] = array('OR', $this->model->userFactory->users_of_group($group));
71}
72
73if (isset($filters['title'])) {
74    $title = preg_replace('/\s/', '%', $filters['title']);
75    $db_filters['title'] = array('LIKE', "%$title%");
76}
77
78$orderby = array('sort', 'priority DESC', 'create_date DESC');
79if (isset($filters['sort_open']) && $filters['sort_open'] == 'on') {
80    $orderby = 'id DESC';
81}
82
83if ($this->get_action() == 'threads') {
84    $db_filters['type'] = 'issue';
85} else {
86    $db_filters['type'] = 'project';
87}
88
89$threads = $this->model->threadFactory->get_all($db_filters, $orderby);
90
91$this->tpl->set('labels', $this->model->labelFactory->get_all());
92$this->tpl->set('threads', $threads);
93$this->tpl->set('years', $years);
94