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