forked from mtierltd/timetracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportItemMapper.php
More file actions
229 lines (203 loc) · 8.34 KB
/
ReportItemMapper.php
File metadata and controls
229 lines (203 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
// db/authormapper.php
namespace OCA\TimeTracker\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
class ReportItemMapper extends Mapper {
private $dbengine;
public function __construct(IDBConnection $db) {
$this->dbengine = 'MYSQL';
if (strpos(get_class($db->getDatabasePlatform()),'PostgreSQL') !== FALSE){
$this->dbengine = 'POSTGRES';
}
parent::__construct($db, 'timetracker_work_interval');
}
/*
public $id;
public $name;
public $projectId;
public $userUid;
public $start;
public $totalDuration;
public $project;
public $clientId;
public $client;
*/
public function report($user, $from, $to, $filterProjectId, $filterClientId, $filterTagId, $timegroup, $groupOn1, $groupOn2, $admin, $start, $limit ){
$selectFields = ['min(wi.id) as id', 'sum(duration) as "totalDuration"'];
$pg = 0;
if ($this->dbengine != 'MYSQL') {
$pg = 1;
if(empty($timegroup)){
$selectFields[]= "to_timestamp(min(start))::date as time";
} elseif ($timegroup == 'week') {
$selectFields[]= "to_char(to_timestamp(start)::date, 'YYYY-WW') as time";
}elseif ($timegroup == 'day') {
$selectFields[]= "to_timestamp(start)::date as time";
}elseif ($timegroup == 'year') {
$selectFields[]= 'date_part(\'year\', to_timestamp(start)::date) as time';
}elseif ($timegroup == 'month') {
$selectFields[]= "to_char(to_timestamp(start)::date, 'YYYY-MM') as time";
}
} else {
if(empty($timegroup)){
if (empty($groupOn1) && empty($groupOn2)) {
$selectFields[]= "DATE_FORMAT(FROM_UNIXTIME(start),'%Y-%m-%d %H:%i') as time";
} else {
$selectFields[]= "DATE_FORMAT(FROM_UNIXTIME(min(start)),'%Y-%m-%d') as time";
}
} elseif ($timegroup == 'week') {
$selectFields[]= "STR_TO_DATE(CONCAT(YEARWEEK(FROM_UNIXTIME(start)),' Monday'), '%x%v %W') as time";
}elseif ($timegroup == 'day') {
$selectFields[]= "DATE_FORMAT(FROM_UNIXTIME(start),'%Y-%m-%d') as time";
}elseif ($timegroup == 'year') {
$selectFields[]= 'YEAR(FROM_UNIXTIME(start)) as time';
}elseif ($timegroup == 'month') {
$selectFields[]= "DATE_FORMAT(FROM_UNIXTIME(start),'%Y-%m') as time";
}
}
if(($groupOn1 != 'name') && ($groupOn2 != 'name') && !empty($groupOn1) && !empty($groupOn2)){
if ($this->dbengine != 'MYSQL') {
$selectFields[] = '\'*\' as name';
} else {
$selectFields[] = 'CASE WHEN CHAR_LENGTH(group_concat(distinct wi.name)) > 40 THEN CONCAT(SUBSTRING(group_concat(distinct wi.name), 1, 40), "...") ELSE group_concat(distinct wi.name) END as name';
}
//$selectFields[] = 'group_concat(distinct wi.name) as name';
} else {
$selectFields[] = 'wi.name as name';
}
if(($groupOn1 != 'name') && ($groupOn2 != 'name') && !empty($groupOn1) && !empty($groupOn2)){
$selectFields[] = '\'*\' as details';
} else {
$selectFields[] = 'wi.details as details';
}
if(($groupOn1 != 'project') && ($groupOn2 != 'project' && !empty($groupOn1) && !empty($groupOn2))){
$selectFields[] = '\'*\' as "projectId"';
if ($this->dbengine != 'MYSQL') {
$selectFields[] = 'string_agg(distinct p.name, \',\') as project';
} else {
$selectFields[] = 'group_concat(distinct p.name) as project';
}
} else {
$selectFields[] = '\'*\' as "projectId"';
$selectFields[] = 'p.name as project';
}
if(($groupOn1 != 'client') && ($groupOn2 != 'client')){
$selectFields[] = '\'*\' as "clientId"';
if ($this->dbengine != 'MYSQL') {
$selectFields[] = 'string_agg(distinct c.name, \',\') as client';
} else {
$selectFields[] = 'group_concat(distinct c.name) as client';
}
} else {
$selectFields[] = '\'*\' as "clientId"';
$selectFields[] = 'c.name as client';
}
if(($groupOn1 != 'userUid') && ($groupOn2 != 'userUid')){
if ($this->dbengine != 'MYSQL') {
$selectFields[] = 'string_agg(distinct user_uid, \',\') as "userUid"';
} else {
$selectFields[] = 'group_concat(distinct user_uid) as "userUid"';
}
} else {
$selectFields[] = 'user_uid as "userUid"';
}
$selectItems = implode(", ",$selectFields).
' FROM *PREFIX*timetracker_work_interval wi
left join *PREFIX*timetracker_project p on wi.project_id = p.id
left join *PREFIX*timetracker_client c on p.client_id = c.id';
$filters = [];
$params = [];
if (!empty($from)){
$filters[] = "(start > ?)";
$params[] = $from;
}
if (!empty($to)){
$filters[] = "(start < ?)";
$params[] = $to;
}
if (!empty($filterProjectId)){
$qm = [];
$append = '';
foreach($filterProjectId as $f){
$qm[] = '?';
$params[] = $f;
if($f == null) {
$append = ' or wi.project_id is null ';
}
}
$filters[] = '(wi.project_id in ('.implode(",",$qm).')'.$append.')';
}
if (!empty($filterClientId)){
$qm = [];
$append = '';
foreach($filterClientId as $f){
$qm[] = '?';
$params[] = $f;
if ($f == null) {
$append = ' or c.id is null ';
}
}
$filters[] = '(c.id in ('.implode(",",$qm).')'.$append.')';
}
if ( (!empty($user)) && (!$admin) ){
$filters[] = "(user_uid = ?)";
$params[] = $user;
}
$group = '';
$groups = [];
if (!empty($timegroup)){
// if($timegroup == 'week'){
// $groups[] = "YEARWEEK(start)";
// } elseif ($timegroup == 'day') {
// $groups[] = "DATE(start)";
// } elseif ($timegroup == 'month') {
// $groups[] = "EXTRACT(YEAR_MONTH FROM start)";
// } elseif ($timegroup == 'year') {
// $groups[] = "YEAR(start)";
// }
$groups[] = 'time';
}
if (!empty($groupOn1)){
if ($groupOn1 == "project" || $groupOn1 == "client" || $groupOn1 == "name" || $groupOn1 == "userUid")
// $groups[] = $groupOn1;
if ($groupOn1 == 'name'){
$groups[] = 'wi.name';
} else {
if($pg) { // postgres needs quotes on names
$groups[] = '"'.$groupOn1.'"';
} else {
$groups[] = $groupOn1;
}
}
if (!empty($groupOn2)){
if ($groupOn2 == "project" || $groupOn2 == "client" || $groupOn2 == "name" || $groupOn2 == "userUid"){
if ($groupOn2 == 'name'){
$groups[] = 'wi.name';
} else {
if($pg) {
$groups[] = '"'.$groupOn2.'"';// postgres needs quotes on names
} else {
$groups[] = $groupOn2;
}
}
}
}
}
if (!empty($groups)){
$group = "group by ".implode(",",$groups);
} else {
$group = "group by wi.id";
}
if (empty($start)){
$start = 0;
}
if (empty($limit)){
$limit = 10000;
}
$sql = 'SELECT '.$selectItems.' where '.implode(" and ",$filters).' '.$group. ' order by time desc';
//var_dump($sql);
// var_dump($params);
return $this->findEntities($sql, $params, $limit, $start);
}
}