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