xref: /plugin/bez/ctl/tasks.php (revision a0cd8c785f18b483f73582b411767428d04a78f6)
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', 'bez_task_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('tasks', $filters));
33} else {
34    $filters = $this->params;
35}
36
37$this->tpl->set_values($filters);
38
39$years = $this->model->taskFactory->get_years_scope();
40
41//some filters are just copied
42$db_filters = array_filter($filters, function ($k) {
43    return in_array($k, array('thread_id', 'state', 'type', 'assignee', 'original_poster', 'task_program_id'));
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_month = '01';
58    $end_month = '12';
59    if (isset($filters['month']) && $filters['month'] !== '-all') {
60        $start_month = $end_month = sprintf("%02d", (int)$filters['month']);
61    }
62
63    $start_day = "$year-$start_month-01";
64    $end_day = "$year-$end_month-31";
65
66    $db_filters[$filters['date_type']] = array('BETWEEN', array($start_day, $end_day), array('date'));
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['assignee']) &&
76    substr($filters['assignee'], 0, 1) === '@') {
77    $group = substr($filters['assignee'], 1);
78    $db_filters['assignee'] = array('OR', $this->model->userFactory->users_of_group($group));
79}
80
81if (isset($filters['content'])) {
82    $content = preg_replace('/\s/', '%', $filters['content']);
83    $db_filters['content'] = array('LIKE', "%$content%");
84}
85
86$orderby = 'id';
87
88$tasks = $this->model->taskFactory->get_all($db_filters, $orderby);
89
90$this->tpl->set('task_programs', $this->model->task_programFactory->get_all());
91$this->tpl->set('tasks', $tasks);
92$this->tpl->set('months', array(1 => 'jan',
93                                2 => 'feb',
94                                3 => 'mar',
95                                4 => 'apr',
96                                5 => 'may',
97                                6 => 'june',
98                                7 => 'july',
99                                8 => 'aug',
100                                9 => 'sept',
101                                10 => 'oct',
102                                11 => 'nov',
103                                12 => 'dec'));
104$this->tpl->set('years', $years);
105