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
10// Admin actions
11if ($this->model->get_level() >= BEZ_AUTH_ADMIN && isset($_POST['action']) && isset($_POST['task_id'])) {
12    if ($_POST['action'] == 'bulk_delete') {
13        foreach ($_POST['task_id'] as $id) {
14            $task = $this->model->taskFactory->get_one($id);
15            $this->model->taskFactory->delete($task);
16        }
17    } elseif ($_POST['action'] == 'bulk_move') {
18        foreach ($_POST['task_id'] as $id) {
19            $task = $this->model->taskFactory->get_one($id);
20            $task->set_task_program($_POST['task_program']);
21            $this->model->taskFactory->save($task);
22        }
23    }
24}
25
26define('BEZ_THREAD_FILTERS_COOKIE_NAME', 'plugin__bez_task_filters');
27
28if (isset($_POST['action']) && $_POST['action'] == 'filter') {
29    unset($_POST['action']);
30    $raw_filters = $_POST;
31} elseif (empty($this->params) && isset($_COOKIE[BEZ_THREAD_FILTERS_COOKIE_NAME])) {
32    $raw_filters = json_decode($_COOKIE[BEZ_THREAD_FILTERS_COOKIE_NAME], true);;
33}
34
35if (isset($raw_filters)) {
36    //save filters
37    setcookie(BEZ_THREAD_FILTERS_COOKIE_NAME, json_encode($raw_filters));
38
39    $filters = array_filter($raw_filters, function($v) {
40        return $v !== '-all' && $v !== '';
41    });
42
43    if (empty($filters)) {
44        $filters['year'] = '-all';
45    }
46
47    header('Location: '.$this->url('tasks', $filters));
48} else {
49    $filters = $this->params;
50}
51
52$this->tpl->set_values($filters);
53
54$years = $this->model->taskFactory->get_years_scope();
55
56//some filters are just copied
57$db_filters = array_filter($filters, function ($k) {
58    return in_array($k, array('thread_id', 'state', 'type', 'assignee', 'original_poster', 'task_program_id'));
59}, ARRAY_FILTER_USE_KEY);
60
61//-none filters become empty filters
62$db_filters = array_map(function($v) {
63    if ($v === '-none') {
64        return '';
65    }
66    return $v;
67}, $db_filters);
68
69if (isset($filters['year']) && $filters['year'] !== '-all') {
70    $year = $filters['year'];
71
72    $start_month = '01';
73    $end_month = '12';
74    if (isset($filters['month']) && $filters['month'] !== '-all') {
75        $start_month = $end_month = sprintf("%02d", (int)$filters['month']);
76    }
77
78    $start_day = "$year-$start_month-01";
79    $end_day = "$year-$end_month-31";
80
81    $db_filters[$filters['date_type']] = array('BETWEEN', array($start_day, $end_day), array('date'));
82}
83
84if (isset($filters['original_poster']) &&
85    substr($filters['original_poster'], 0, 1) === '@') {
86    $group = substr($filters['original_poster'], 1);
87    $db_filters['original_poster'] = array('OR', $this->model->userFactory->users_of_group($group));
88}
89
90if (isset($filters['assignee']) &&
91    substr($filters['assignee'], 0, 1) === '@') {
92    $group = substr($filters['assignee'], 1);
93    $db_filters['assignee'] = array('OR', $this->model->userFactory->users_of_group($group));
94}
95
96if (isset($filters['content'])) {
97    $content = preg_replace('/\s/', '%', $filters['content']);
98    $db_filters['content'] = array('LIKE', "%$content%");
99}
100
101$orderby = array('priority DESC', 'close_date DESC', 'plan_date');
102
103$tasks = $this->model->taskFactory->get_all($db_filters, $orderby);
104
105$this->tpl->set('task_programs', $this->model->task_programFactory->get_all([], 'name')->fetchAll());
106$this->tpl->set('tasks', $tasks);
107$this->tpl->set('months', array(1 => 'jan',
108                                2 => 'feb',
109                                3 => 'mar',
110                                4 => 'apr',
111                                5 => 'may',
112                                6 => 'june',
113                                7 => 'july',
114                                8 => 'aug',
115                                9 => 'sept',
116                                10 => 'oct',
117                                11 => 'nov',
118                                12 => 'dec'));
119$this->tpl->set('years', $years);
120