xref: /plugin/bez/ctl/tasks.php (revision e8827d732aaeeee6f7b703c5654f86ca97056383)
1<?php
2
3/** @var action_plugin_bez $this */
4
5define('BEZ_THREAD_FILTERS_COOKIE_NAME', 'bez_task_filters');
6
7if (count($_POST) > 0) {
8    $raw_filters = $_POST;
9} elseif (empty($this->params) && isset($_COOKIE[BEZ_THREAD_FILTERS_COOKIE_NAME])) {
10    $raw_filters = $_COOKIE[BEZ_THREAD_FILTERS_COOKIE_NAME];
11}
12
13if (isset($raw_filters)) {
14    //save filters
15    foreach ($raw_filters as $k => $v) {
16        setcookie(BEZ_THREAD_FILTERS_COOKIE_NAME."[$k]", $v);
17    }
18
19    $filters = array_filter($raw_filters, function($v) {
20        return $v !== '-all' && $v !== '';
21    });
22
23    if (empty($filters)) {
24        $filters['year'] = '-all';
25    }
26
27    header('Location: '.$this->url('tasks', $filters));
28} else {
29    $filters = $this->params;
30}
31
32$this->tpl->set_values($filters);
33
34$years = $this->model->threadFactory->get_years_scope();
35
36//some filters are just copied
37$db_filters = array_filter($filters, function ($k) {
38    return in_array($k, array('thread_id', 'state', 'type', 'task_program_id'));
39}, ARRAY_FILTER_USE_KEY);
40
41//-none filters become empty filters
42$db_filters = array_map(function($v) {
43    if ($v === '-none') {
44        return '';
45    }
46    return $v;
47}, $db_filters);
48
49if (isset($filters['year']) && $filters['year'] !== '-all') {
50    $year = $filters['year'];
51
52    $start_day = "$year-01-01";
53    $end_day = "$year-12-31";
54
55    $db_filters['create_date'] = array('BETWEEN', array($start_day, $end_day), array('date'));
56}
57
58if (isset($filters['assignee']) &&
59    substr($filters['assignee'], 0, 1) === '@') {
60    $group = substr($filters['assignee'], 1);
61    $db_filters['assignee'] = array('OR', $this->model->userFactory->users_of_group($group));
62}
63
64if (isset($filters['content'])) {
65    $content = preg_replace('/\s/', '%', $filters['content']);
66    $db_filters['content'] = array('LIKE', "%$content%");
67}
68
69$orderby = 'last_activity_date';
70
71$tasks = $this->model->taskFactory->get_all($db_filters, $orderby);
72
73$this->tpl->set('task_programs', $this->model->task_programFactory->get_all());
74$this->tpl->set('tasks', $tasks);
75$this->tpl->set('years', $years);
76
77
78//include_once DOKU_PLUGIN."bez/models/tasks.php";
79//include_once DOKU_PLUGIN."bez/models/taskactions.php";
80//include_once DOKU_PLUGIN."bez/models/taskstates.php";
81//include_once DOKU_PLUGIN."bez/models/tasktypes.php";
82//include_once DOKU_PLUGIN."bez/models/users.php";
83//include_once DOKU_PLUGIN."bez/models/issues.php";
84//
85//if	(!$helper->user_viewer()) {
86//	throw new PermissionDeniedException();
87//}
88//
89//$tasko = new Tasks();
90//$taskao = new Taskactions();
91//$taskso = new Taskstates();
92//$tasktypeso = new Tasktypes();
93//$usro = new Users();
94//$isso = new Issues();
95//
96//if (count($_POST) > 0)
97//	$raw_filters = $_POST;
98//elseif (count($nparams) === 1 && isset($_COOKIE['bez_tasks_filters']))
99//	$raw_filters = $_COOKIE['bez_tasks_filters'];
100//
101//if (isset($raw_filters)) {
102//	$filters = $tasko->validate_filters($raw_filters);
103//	$query_uri = '';
104//	foreach ($filters as $k => $v)
105//		if ($v != '-all' && $v != '')
106//			$query_uri .= ':'.urlencode($k).':'.urlencode($v);
107//
108//	if ($query_uri == "")
109//		$query_uri = ":year:-all";
110//
111//	header('Location: ?id='.$this->id('tasks').$query_uri);
112//}
113//
114///*rekordy parzyste to nagłówki, nieparzyste to ich wartości.*/
115///*np. status:1:type:2:podmiot:PCA*/
116//$value = array('issue' => '-all', 'action' => '-all', 'taskstate' => '-all',
117//				'executor' => '-all', 'year' => '-all', 'tasktype' => '-all',
118//				'month' => '-all', 'task' => '', 'reason' => '', 'date_type' => 'plan');
119//for ($i = 0; $i < count($params); $i += 2)
120//	$value[urldecode($params[$i])] = urldecode($params[$i+1]);
121//
122////save filters
123//foreach ($value as $k => $v)
124//	setcookie("bez_tasks_filters[$k]", $v);
125//
126//$ical_link = '?id=bez:tasks_ical';
127//foreach ($value as $k => $v)
128//	if ($v != '-all' && $v != '')
129//		$ical_link .= ':'.urlencode($k).':'.urlencode($v);
130//
131//$template['ical_link'] = $ical_link;
132//
133//$template['uri'] = $uri;
134//
135//$template['issues'] = $isso->get_ids();
136//
137//$template['actions'] = $taskao->get();
138//
139//$template['states'] = $taskso->get();
140//
141//$template['executors'] = $usro->get();
142//$template['groups'] = $usro->groups();
143//
144//
145//$template['years'] = $tasko->get_years();
146//
147//$tasks = $tasko->get_filtered($value);
148//
149//
150//$template['tasks_stats']['total'] = count($tasks);
151//
152//$tcost = 0;
153//$thours = 0;
154//foreach ($tasks as &$task) {
155//	$tcost += (int)$task['cost'];
156//	if ($task['start_time'] != '') {
157//		$start_time = strtotime($task['start_time']);
158//		$finish_time = strtotime($task['finish_time']);
159//		$secs = $finish_time - $start_time;
160//		$hours = $secs / 3600;
161//		$hours_s = sprintf("%.1f", $hours);
162//		$task['hours'] = $hours_s;
163//		$thours += $hours;
164//	} else
165//		$task['hours'] = '';
166//}
167//$template['tasks'] = $tasks;
168//
169//$template['tasks_stats']['totalcost'] = $tcost;
170//$template['tasks_stats']['totalhours'] = sprintf("%.1f", $thours);
171//
172//$tasktypes = $tasktypeso->get();
173//$template['tasktypes'] = $tasktypes;
174//
175//
176//if ($nparams['taskstate'] == '0')
177//	$template['view'] = 'plan';
178//else
179//	$template['view'] = 'realization';
180//
181//$template['months'] = array(1 => 'jan',
182//							2 => 'feb',
183//							3 => 'mar',
184//							4 => 'apr',
185//							5 => 'may',
186//							6 => 'june',
187//							7 => 'july',
188//							8 => 'aug',
189//							9 => 'sept',
190//							10 => 'oct',
191//							11 => 'nov',
192//							12 => 'dec');
193