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