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