|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* SugarCRM Community Edition is a customer relationship management program developed by |
|
5
|
|
|
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc. |
|
6
|
|
|
* |
|
7
|
|
|
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd. |
|
8
|
|
|
* Copyright (C) 2011 - 2017 SalesAgility Ltd. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software; you can redistribute it and/or modify it under |
|
11
|
|
|
* the terms of the GNU Affero General Public License version 3 as published by the |
|
12
|
|
|
* Free Software Foundation with the addition of the following permission added |
|
13
|
|
|
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK |
|
14
|
|
|
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY |
|
15
|
|
|
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
18
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
19
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
|
20
|
|
|
* details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License along with |
|
23
|
|
|
* this program; if not, see http://www.gnu.org/licenses or write to the Free |
|
24
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
25
|
|
|
* 02110-1301 USA. |
|
26
|
|
|
* |
|
27
|
|
|
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road, |
|
28
|
|
|
* SW2-130, Cupertino, CA 95014, USA. or at email address [email protected]. |
|
29
|
|
|
* |
|
30
|
|
|
* The interactive user interfaces in modified source and object code versions |
|
31
|
|
|
* of this program must display Appropriate Legal Notices, as required under |
|
32
|
|
|
* Section 5 of the GNU Affero General Public License version 3. |
|
33
|
|
|
* |
|
34
|
|
|
* In accordance with Section 7(b) of the GNU Affero General Public License version 3, |
|
35
|
|
|
* these Appropriate Legal Notices must retain the display of the "Powered by |
|
36
|
|
|
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not |
|
37
|
|
|
* reasonably feasible for technical reasons, the Appropriate Legal Notices must |
|
38
|
|
|
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM". |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
if (!defined('sugarEntry') || !sugarEntry) { |
|
42
|
|
|
die('Not A Valid Entry Point'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns the display labels for a module path and field. |
|
47
|
|
|
* @param $modulePath |
|
48
|
|
|
* @param $field |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
function getDisplayForField($modulePath, $field, $reportModule) |
|
52
|
|
|
{ |
|
53
|
|
|
global $app_list_strings; |
|
54
|
|
|
$modulePathDisplay = array(); |
|
55
|
|
|
$currentBean = BeanFactory::getBean($reportModule); |
|
56
|
|
|
$modulePathDisplay[] = $currentBean->module_name; |
|
57
|
|
|
if (is_array($modulePath)) { |
|
58
|
|
|
$split = $modulePath; |
|
59
|
|
|
} else { |
|
60
|
|
|
$split = explode(':', $modulePath); |
|
61
|
|
|
} |
|
62
|
|
|
if ($split && $split[0] == $currentBean->module_dir) { |
|
|
|
|
|
|
63
|
|
|
array_shift($split); |
|
64
|
|
|
} |
|
65
|
|
|
foreach ($split as $relName) { |
|
66
|
|
|
if (empty($relName)) { |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
if (!empty($currentBean->field_name_map[$relName]['vname'])) { |
|
70
|
|
|
$moduleLabel = trim(translate($currentBean->field_name_map[$relName]['vname'], $currentBean->module_dir), |
|
71
|
|
|
':'); |
|
72
|
|
|
} |
|
73
|
|
|
$thisModule = getRelatedModule($currentBean->module_dir, $relName); |
|
74
|
|
|
$currentBean = BeanFactory::getBean($thisModule); |
|
75
|
|
|
|
|
76
|
|
|
if (!empty($moduleLabel)) { |
|
77
|
|
|
$modulePathDisplay[] = $moduleLabel; |
|
78
|
|
|
} else { |
|
79
|
|
|
$modulePathDisplay[] = $currentBean->module_name; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
$fieldDisplay = $currentBean->field_name_map[$field]['vname']; |
|
83
|
|
|
$fieldDisplay = translate($fieldDisplay, $currentBean->module_dir); |
|
84
|
|
|
$fieldDisplay = trim($fieldDisplay, ':'); |
|
85
|
|
|
foreach ($modulePathDisplay as &$module) { |
|
86
|
|
|
$module = isset($app_list_strings['aor_moduleList'][$module]) ? $app_list_strings['aor_moduleList'][$module] : ( |
|
87
|
|
|
isset($app_list_strings['moduleList'][$module]) ? $app_list_strings['moduleList'][$module] : $module |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return array('field' => $fieldDisplay, 'module' => str_replace(' ', ' ', implode(' : ', $modulePathDisplay))); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
function requestToUserParameters() |
|
95
|
|
|
{ |
|
96
|
|
|
$params = array(); |
|
97
|
|
|
if (isset($_REQUEST['parameter_id']) && $_REQUEST['parameter_id']) { |
|
98
|
|
|
foreach ($_REQUEST['parameter_id'] as $key => $parameterId) { |
|
99
|
|
|
if ($_REQUEST['parameter_type'][$key] === 'Multi') { |
|
100
|
|
|
$_REQUEST['parameter_value'][$key] = encodeMultienumValue(explode(',', |
|
101
|
|
|
$_REQUEST['parameter_value'][$key])); |
|
102
|
|
|
} |
|
103
|
|
|
$params[$parameterId] = array( |
|
104
|
|
|
'id' => $parameterId, |
|
105
|
|
|
'operator' => $_REQUEST['parameter_operator'][$key], |
|
106
|
|
|
'type' => $_REQUEST['parameter_type'][$key], |
|
107
|
|
|
'value' => $_REQUEST['parameter_value'][$key], |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
// Fix for issue #1272 - AOR_Report module cannot update Date type parameter. |
|
111
|
|
|
if ($_REQUEST['parameter_type'][$key] === 'Date') { |
|
112
|
|
|
$values = array(); |
|
113
|
|
|
$values[] = $_REQUEST['parameter_value'][0]; |
|
114
|
|
|
$values[] = $_REQUEST['parameter_value'][1];; |
|
115
|
|
|
$values[] = $_REQUEST['parameter_value'][2];; |
|
116
|
|
|
$values[] = $_REQUEST['parameter_value'][3];; |
|
117
|
|
|
|
|
118
|
|
|
$params[$parameterId] = array( |
|
119
|
|
|
'id' => $parameterId, |
|
120
|
|
|
'operator' => $_REQUEST['parameter_operator'][$key], |
|
121
|
|
|
'type' => $_REQUEST['parameter_type'][$key], |
|
122
|
|
|
'value' => $values, |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// determine if parameter is a date |
|
127
|
|
|
if ($_REQUEST['parameter_type'][$key] === 'Value') { |
|
128
|
|
|
$paramLength = strlen($_REQUEST['parameter_value'][$key]); |
|
129
|
|
|
$paramValue = $_REQUEST['parameter_value'][$key]; |
|
130
|
|
|
if ($paramLength === 10) { |
|
131
|
|
|
if (strpos($paramValue, '/') === 2 || strpos($paramValue, '/') === 4) { |
|
132
|
|
|
$params[$parameterId] = array( |
|
133
|
|
|
'id' => $parameterId, |
|
134
|
|
|
'operator' => $_REQUEST['parameter_operator'][$key], |
|
135
|
|
|
'type' => $_REQUEST['parameter_type'][$key], |
|
136
|
|
|
'value' => convertToDateTime($_REQUEST['parameter_value'][$key])->format('Y-m-d H:i:s'), |
|
137
|
|
|
); |
|
138
|
|
|
} elseif (strpos($paramValue, '-') === 2 || strpos($paramValue, '-') === 4) { |
|
139
|
|
|
$params[$parameterId] = array( |
|
140
|
|
|
'id' => $parameterId, |
|
141
|
|
|
'operator' => $_REQUEST['parameter_operator'][$key], |
|
142
|
|
|
'type' => $_REQUEST['parameter_type'][$key], |
|
143
|
|
|
'value' => convertToDateTime($_REQUEST['parameter_value'][$key])->format('Y-m-d H:i:s'), |
|
144
|
|
|
); |
|
145
|
|
|
} elseif (strpos($paramValue, '.') === 2 || strpos($paramValue, '.') === 4) { |
|
146
|
|
|
$params[$parameterId] = array( |
|
147
|
|
|
'id' => $parameterId, |
|
148
|
|
|
'operator' => $_REQUEST['parameter_operator'][$key], |
|
149
|
|
|
'type' => $_REQUEST['parameter_type'][$key], |
|
150
|
|
|
'value' => convertToDateTime($_REQUEST['parameter_value'][$key])->format('Y-m-d H:i:s'), |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $params; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
function getConditionsAsParameters($report, $override = array()) |
|
162
|
|
|
{ |
|
163
|
|
|
if (empty($report)) { |
|
164
|
|
|
return array(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
global $app_list_strings; |
|
168
|
|
|
$conditions = array(); |
|
169
|
|
|
foreach ($report->get_linked_beans('aor_conditions', 'AOR_Conditions') as $key => $condition) { |
|
170
|
|
|
if (!$condition->parameter) { |
|
171
|
|
|
continue; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$path = unserialize(base64_decode($condition->module_path)); |
|
175
|
|
|
$field_module = $report->report_module; |
|
176
|
|
|
if ($path[0] != $report->report_module) { |
|
177
|
|
|
foreach ($path as $rel) { |
|
178
|
|
|
if (empty($rel)) { |
|
179
|
|
|
continue; |
|
180
|
|
|
} |
|
181
|
|
|
$field_module = getRelatedModule($field_module, $rel); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$additionalConditions = unserialize(base64_decode($condition->value)); |
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
$value = isset($override[$condition->id]['value']) ? $override[$condition->id]['value'] : $value = $condition->value; |
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
$field = getModuleField($field_module, $condition->field, "parameter_value[$key]", 'EditView', $value); |
|
192
|
|
|
$disp = getDisplayForField($path, $condition->field, $report->report_module); |
|
193
|
|
|
$conditions[] = array( |
|
194
|
|
|
'id' => $condition->id, |
|
195
|
|
|
'operator' => $condition->operator, |
|
196
|
|
|
'operator_display' => $app_list_strings['aor_operator_list'][$condition->operator], |
|
197
|
|
|
'value_type' => $condition->value_type, |
|
198
|
|
|
'value' => $value, |
|
199
|
|
|
'field_display' => $disp['field'], |
|
200
|
|
|
'module_display' => $disp['module'], |
|
201
|
|
|
'field' => $field, |
|
202
|
|
|
'additionalConditions' => $additionalConditions |
|
203
|
|
|
); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $conditions; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* getPeriodDate |
|
211
|
|
|
* @param $date_time_period_list_selected |
|
212
|
|
|
* @return DateTime |
|
213
|
|
|
*/ |
|
214
|
|
|
function getPeriodDate($date_time_period_list_selected) |
|
215
|
|
|
{ |
|
216
|
|
|
global $sugar_config; |
|
217
|
|
|
$datetime_period = new DateTime(); |
|
218
|
|
|
|
|
219
|
|
|
// Setup when year quarters start & end |
|
220
|
|
|
if ($sugar_config['aor']['quarters_begin']) { |
|
221
|
|
|
$q = calculateQuarters($sugar_config['aor']['quarters_begin']); |
|
222
|
|
|
} else { |
|
223
|
|
|
$q = calculateQuarters(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if ($date_time_period_list_selected == 'today') { |
|
227
|
|
|
$datetime_period = new DateTime(); |
|
228
|
|
|
} elseif ($date_time_period_list_selected == 'yesterday') { |
|
229
|
|
|
$datetime_period = $datetime_period->sub(new DateInterval("P1D")); |
|
230
|
|
|
} elseif ($date_time_period_list_selected == 'this_week') { |
|
231
|
|
|
$datetime_period = $datetime_period->setTimestamp(strtotime('this week')); |
|
232
|
|
|
} elseif ($date_time_period_list_selected == 'last_week') { |
|
233
|
|
|
$datetime_period = $datetime_period->setTimestamp(strtotime('last week')); |
|
234
|
|
|
} elseif ($date_time_period_list_selected == 'this_month') { |
|
235
|
|
|
$datetime_period = $datetime_period->setDate($datetime_period->format('Y'), |
|
236
|
|
|
$datetime_period->format('m'), 1); |
|
237
|
|
|
} elseif ($date_time_period_list_selected == 'last_month') { |
|
238
|
|
|
$datetime_period = $datetime_period->modify('first day of last month'); |
|
239
|
|
|
} elseif ($date_time_period_list_selected == 'this_quarter') { |
|
240
|
|
|
$thisMonth = $datetime_period->setDate($datetime_period->format('Y'), |
|
241
|
|
|
$datetime_period->format('m'), 1); |
|
242
|
|
|
if ($thisMonth >= $q[1]['start'] && $thisMonth <= $q[1]['end']) { |
|
243
|
|
|
// quarter 1 |
|
244
|
|
|
$datetime_period = $datetime_period->setDate($q[1]['start']->format('Y'), |
|
245
|
|
|
$q[1]['start']->format('m'), $q[1]['start']->format('d')); |
|
246
|
|
|
} elseif ($thisMonth >= $q[2]['start'] && $thisMonth <= $q[2]['end']) { |
|
247
|
|
|
// quarter 2 |
|
248
|
|
|
$datetime_period = $datetime_period->setDate($q[2]['start']->format('Y'), |
|
249
|
|
|
$q[2]['start']->format('m'), $q[2]['start']->format('d')); |
|
250
|
|
|
} elseif ($thisMonth >= $q[3]['start'] && $thisMonth <= $q[3]['end']) { |
|
251
|
|
|
// quarter 3 |
|
252
|
|
|
$datetime_period = $datetime_period->setDate($q[3]['start']->format('Y'), |
|
253
|
|
|
$q[3]['start']->format('m'), $q[3]['start']->format('d')); |
|
254
|
|
|
} elseif ($thisMonth >= $q[4]['start'] && $thisMonth <= $q[4]['end']) { |
|
255
|
|
|
// quarter 4 |
|
256
|
|
|
$datetime_period = $datetime_period->setDate($q[4]['start']->format('Y'), |
|
257
|
|
|
$q[4]['start']->format('m'), $q[4]['start']->format('d')); |
|
258
|
|
|
} |
|
259
|
|
|
} elseif ($date_time_period_list_selected == 'last_quarter') { |
|
260
|
|
|
$thisMonth = $datetime_period->setDate($datetime_period->format('Y'), |
|
261
|
|
|
$datetime_period->format('m'), 1); |
|
262
|
|
|
if ($thisMonth >= $q[1]['start'] && $thisMonth <= $q[1]['end']) { |
|
263
|
|
|
// quarter 1 - 3 months |
|
264
|
|
|
$datetime_period = $q[1]['start']->sub(new DateInterval('P3M')); |
|
265
|
|
|
} elseif ($thisMonth >= $q[2]['start'] && $thisMonth <= $q[2]['end']) { |
|
266
|
|
|
// quarter 2 - 3 months |
|
267
|
|
|
$datetime_period = $q[2]['start']->sub(new DateInterval('P3M')); |
|
268
|
|
|
} elseif ($thisMonth >= $q[3]['start'] && $thisMonth <= $q[3]['end']) { |
|
269
|
|
|
// quarter 3 - 3 months |
|
270
|
|
|
$datetime_period = $q[3]['start']->sub(new DateInterval('P3M')); |
|
271
|
|
|
} elseif ($thisMonth >= $q[4]['start'] && $thisMonth <= $q[4]['end']) { |
|
272
|
|
|
// quarter 4 - 3 months |
|
273
|
|
|
$datetime_period = $q[3]['start']->sub(new DateInterval('P3M')); |
|
274
|
|
|
} |
|
275
|
|
|
} elseif ($date_time_period_list_selected == 'this_year') { |
|
276
|
|
|
$datetime_period = $datetime_period = $datetime_period->setDate($datetime_period->format('Y'), |
|
277
|
|
|
1, 1); |
|
278
|
|
|
} elseif ($date_time_period_list_selected == 'last_year') { |
|
279
|
|
|
$datetime_period = $datetime_period = $datetime_period->setDate($datetime_period->format('Y') - 1, |
|
280
|
|
|
1, 1); |
|
281
|
|
|
} |
|
282
|
|
|
// set time to 00:00:00 |
|
283
|
|
|
$datetime_period = $datetime_period->setTime(0, 0, 0); |
|
284
|
|
|
|
|
285
|
|
|
return $datetime_period; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* getPeriodDate |
|
290
|
|
|
* @param $date_time_period_list_selected |
|
291
|
|
|
* @return DateTime |
|
292
|
|
|
*/ |
|
293
|
|
|
function getPeriodEndDate($dateTimePeriodListSelected) |
|
294
|
|
|
{ |
|
295
|
|
|
switch ($dateTimePeriodListSelected) { |
|
296
|
|
|
case 'today': |
|
297
|
|
|
case 'yesterday': |
|
298
|
|
|
$datetimePeriod = new DateTime(); |
|
299
|
|
|
break; |
|
300
|
|
|
case 'this_week': |
|
301
|
|
|
$datetimePeriod = new DateTime("next week monday"); |
|
302
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
303
|
|
|
break; |
|
304
|
|
|
case 'last_week': |
|
305
|
|
|
$datetimePeriod = new DateTime("this week monday"); |
|
306
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
307
|
|
|
break; |
|
308
|
|
|
case 'this_month': |
|
309
|
|
|
$datetimePeriod = new DateTime('first day of next month'); |
|
310
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
311
|
|
|
break; |
|
312
|
|
|
case 'last_month': |
|
313
|
|
|
$datetimePeriod = new DateTime("first day of this month"); |
|
314
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
315
|
|
|
break; |
|
316
|
|
|
case 'this_quarter': |
|
317
|
|
|
$thisMonth = new DateTime('first day of this month'); |
|
318
|
|
|
$thisMonth = $thisMonth->format('n'); |
|
319
|
|
|
if ($thisMonth < 4) { |
|
320
|
|
|
// quarter 1 |
|
321
|
|
|
$datetimePeriod = new DateTime('first day of april'); |
|
322
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
323
|
|
|
} elseif ($thisMonth > 3 && $thisMonth < 7) { |
|
324
|
|
|
// quarter 2 |
|
325
|
|
|
$datetimePeriod = new DateTime('first day of july'); |
|
326
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
327
|
|
|
} elseif ($thisMonth > 6 && $thisMonth < 10) { |
|
328
|
|
|
// quarter 3 |
|
329
|
|
|
$datetimePeriod = new DateTime('first day of october'); |
|
330
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
331
|
|
|
} elseif ($thisMonth > 9) { |
|
332
|
|
|
// quarter 4 |
|
333
|
|
|
$datetimePeriod = new DateTime('next year first day of january'); |
|
334
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
335
|
|
|
} |
|
336
|
|
|
break; |
|
337
|
|
|
case 'last_quarter': |
|
338
|
|
|
$thisMonth = new DateTime('first day of this month'); |
|
339
|
|
|
$thisMonth = $thisMonth->format('n'); |
|
340
|
|
|
if ($thisMonth < 4) { |
|
341
|
|
|
// previous quarter 1 |
|
342
|
|
|
$datetimePeriod = new DateTime('this year first day of january'); |
|
343
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
344
|
|
|
} elseif ($thisMonth > 3 && $thisMonth < 7) { |
|
345
|
|
|
// previous quarter 2 |
|
346
|
|
|
$datetimePeriod = new DateTime('first day of april'); |
|
347
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
348
|
|
|
} elseif ($thisMonth > 6 && $thisMonth < 10) { |
|
349
|
|
|
// previous quarter 3 |
|
350
|
|
|
$datetimePeriod = new DateTime('first day of july'); |
|
351
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
352
|
|
|
} elseif ($thisMonth > 9) { |
|
353
|
|
|
// previous quarter 4 |
|
354
|
|
|
$datetimePeriod = new DateTime('first day of october'); |
|
355
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
356
|
|
|
} |
|
357
|
|
|
break; |
|
358
|
|
|
case 'this_year': |
|
359
|
|
|
$datetimePeriod = new DateTime('next year first day of january'); |
|
360
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
361
|
|
|
break; |
|
362
|
|
|
case 'last_year': |
|
363
|
|
|
$datetimePeriod = new DateTime("this year first day of january"); |
|
364
|
|
|
$datetimePeriod->setTime(0, 0, 0); |
|
365
|
|
|
break; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
return $datetimePeriod; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* @param int $offsetMonths - defines start of the year. |
|
373
|
|
|
* @return array - The each quarters boundary |
|
374
|
|
|
*/ |
|
375
|
|
|
function calculateQuarters($offsetMonths = 0) |
|
376
|
|
|
{ |
|
377
|
|
|
// define quarters |
|
378
|
|
|
$q = array(); |
|
379
|
|
|
$q['1'] = array(); |
|
380
|
|
|
$q['2'] = array(); |
|
381
|
|
|
$q['3'] = array(); |
|
382
|
|
|
$q['4'] = array(); |
|
383
|
|
|
|
|
384
|
|
|
// Get the start of this year |
|
385
|
|
|
$q1start = new DateTime(); |
|
386
|
|
|
$q1start = $q1start->setTime(0, 0, 0); |
|
387
|
|
|
$q1start = $q1start->setDate($q1start->format('Y'), 1, 1); |
|
388
|
|
|
/* |
|
389
|
|
|
* $offsetMonths gets added to the current month. Therefor we need this variable to equal one less than the start |
|
390
|
|
|
* month. So Jan becomes 0. Feb => 1 and so on. |
|
391
|
|
|
*/ |
|
392
|
|
|
$offsetMonths -= 1; |
|
393
|
|
|
// Offset months |
|
394
|
|
|
if ($offsetMonths > 0) { |
|
395
|
|
|
$q1start->add(new DateInterval('P' . $offsetMonths . 'M')); |
|
396
|
|
|
} |
|
397
|
|
|
$q1end = DateTime::createFromFormat(DATE_ISO8601, $q1start->format(DATE_ISO8601)); |
|
398
|
|
|
$q1end->add(new DateInterval('P2M')); |
|
399
|
|
|
|
|
400
|
|
|
$q2start = DateTime::createFromFormat(DATE_ISO8601, $q1start->format(DATE_ISO8601)); |
|
401
|
|
|
$q2start->add(new DateInterval('P3M')); |
|
402
|
|
|
$q2end = DateTime::createFromFormat(DATE_ISO8601, $q2start->format(DATE_ISO8601)); |
|
403
|
|
|
$q2end->add(new DateInterval('P2M')); |
|
404
|
|
|
|
|
405
|
|
|
$q3start = DateTime::createFromFormat(DATE_ISO8601, $q2start->format(DATE_ISO8601)); |
|
406
|
|
|
$q3start->add(new DateInterval('P3M')); |
|
407
|
|
|
$q3end = DateTime::createFromFormat(DATE_ISO8601, $q3start->format(DATE_ISO8601)); |
|
408
|
|
|
$q3end->add(new DateInterval('P2M')); |
|
409
|
|
|
|
|
410
|
|
|
$q4start = DateTime::createFromFormat(DATE_ISO8601, $q3start->format(DATE_ISO8601)); |
|
411
|
|
|
$q4start->add(new DateInterval('P3M')); |
|
412
|
|
|
$q4end = DateTime::createFromFormat(DATE_ISO8601, $q4start->format(DATE_ISO8601)); |
|
413
|
|
|
$q4end->add(new DateInterval('P2M')); |
|
414
|
|
|
|
|
415
|
|
|
// Assign quarter boundaries |
|
416
|
|
|
$q['1']['start'] = $q1start; |
|
417
|
|
|
$q['1']['end'] = $q1end; |
|
418
|
|
|
$q['2']['start'] = $q2start; |
|
419
|
|
|
$q['2']['end'] = $q2end; |
|
420
|
|
|
$q['3']['start'] = $q3start; |
|
421
|
|
|
$q['3']['end'] = $q3end; |
|
422
|
|
|
$q['4']['start'] = $q4start; |
|
423
|
|
|
$q['4']['end'] = $q4end; |
|
424
|
|
|
|
|
425
|
|
|
return $q; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* convertDateValue |
|
430
|
|
|
* @param string $value - date in any user format |
|
431
|
|
|
* @return DateTime $dateTime - converted string |
|
432
|
|
|
*/ |
|
433
|
|
|
function convertToDateTime($value) |
|
434
|
|
|
{ |
|
435
|
|
|
global $current_user; |
|
436
|
|
|
|
|
437
|
|
|
$user_dateformat = $current_user->getPreference('datef'); |
|
438
|
|
|
|
|
439
|
|
|
switch ($user_dateformat) { |
|
440
|
|
|
case 'Y-m-d': |
|
441
|
|
|
$formattedValue = date('Y-m-d', strtotime($value)); |
|
442
|
|
|
break; |
|
443
|
|
|
case 'm-d-Y': |
|
444
|
|
|
$formattedValue = $value; |
|
445
|
|
|
$day = substr($formattedValue, 3, 2); |
|
446
|
|
|
$month = substr($formattedValue, 0, 2); |
|
447
|
|
|
$year = substr($formattedValue, 6, 4); |
|
448
|
|
|
$formattedValue = substr_replace($formattedValue, $day, 6, 4); |
|
449
|
|
|
$formattedValue = substr_replace($formattedValue, $month, 3, 2); |
|
450
|
|
|
$formattedValue = substr_replace($formattedValue, $year, 0, 2); |
|
451
|
|
|
$formattedValue = date('Y-m-d', strtotime($formattedValue)); |
|
452
|
|
|
break; |
|
453
|
|
|
case 'd-m-Y': |
|
454
|
|
|
$formattedValue = $value; |
|
455
|
|
|
$day = substr($formattedValue, 0, 2); |
|
456
|
|
|
$month = substr($formattedValue, 3, 2); |
|
457
|
|
|
$year = substr($formattedValue, 6, 4); |
|
458
|
|
|
$formattedValue = substr_replace($formattedValue, $day, 6, 4); |
|
459
|
|
|
$formattedValue = substr_replace($formattedValue, $month, 3, 2); |
|
460
|
|
|
$formattedValue = substr_replace($formattedValue, $year, 0, 2); |
|
461
|
|
|
$formattedValue = date('Y-m-d', strtotime($formattedValue)); |
|
462
|
|
|
break; |
|
463
|
|
|
case 'Y/m/d': |
|
464
|
|
|
$formattedValue = str_replace('/', '-', $value); |
|
465
|
|
|
break; |
|
466
|
|
|
case 'm/d/Y': |
|
467
|
|
|
$formattedValue = str_replace('/', '-', $value); |
|
468
|
|
|
$day = substr($formattedValue, 3, 2); |
|
469
|
|
|
$month = substr($formattedValue, 0, 2); |
|
470
|
|
|
$year = substr($formattedValue, 6, 4); |
|
471
|
|
|
$formattedValue = substr_replace($formattedValue, $day, 6, 4); |
|
472
|
|
|
$formattedValue = substr_replace($formattedValue, $month, 3, 2); |
|
473
|
|
|
$formattedValue = substr_replace($formattedValue, $year, 0, 2); |
|
474
|
|
|
$formattedValue = date('Y-m-d', strtotime($formattedValue)); |
|
475
|
|
|
break; |
|
476
|
|
|
case 'd/m/Y': |
|
477
|
|
|
$formattedValue = str_replace('/', '-', $value); |
|
478
|
|
|
$day = substr($formattedValue, 0, 2); |
|
479
|
|
|
$month = substr($formattedValue, 3, 2); |
|
480
|
|
|
$year = substr($formattedValue, 6, 4); |
|
481
|
|
|
$formattedValue = substr_replace($formattedValue, $day, 6, 4); |
|
482
|
|
|
$formattedValue = substr_replace($formattedValue, $month, 3, 2); |
|
483
|
|
|
$formattedValue = substr_replace($formattedValue, $year, 0, 2); |
|
484
|
|
|
$formattedValue = date('Y-m-d', strtotime($formattedValue)); |
|
485
|
|
|
break; |
|
486
|
|
|
case 'Y.m.d': |
|
487
|
|
|
$formattedValue = str_replace('.', '-', $value); |
|
488
|
|
|
break; |
|
489
|
|
|
case 'd.m.Y': |
|
490
|
|
|
$formattedValue = str_replace('.', '-', $value); |
|
491
|
|
|
$day = substr($formattedValue, 0, 2); |
|
492
|
|
|
$month = substr($formattedValue, 3, 2); |
|
493
|
|
|
$year = substr($formattedValue, 6, 4); |
|
494
|
|
|
$formattedValue = substr_replace($formattedValue, $day, 6, 4); |
|
495
|
|
|
$formattedValue = substr_replace($formattedValue, $month, 3, 2); |
|
496
|
|
|
$formattedValue = substr_replace($formattedValue, $year, 0, 2); |
|
497
|
|
|
$formattedValue = date('Y-m-d', strtotime($formattedValue)); |
|
498
|
|
|
break; |
|
499
|
|
|
case 'm.d.Y': |
|
500
|
|
|
$formattedValue = str_replace('.', '-', $value); |
|
501
|
|
|
$day = substr($formattedValue, 3, 2); |
|
502
|
|
|
$month = substr($formattedValue, 0, 2); |
|
503
|
|
|
$year = substr($formattedValue, 6, 4); |
|
504
|
|
|
$formattedValue = substr_replace($formattedValue, $day, 6, 4); |
|
505
|
|
|
$formattedValue = substr_replace($formattedValue, $month, 3, 2); |
|
506
|
|
|
$formattedValue = substr_replace($formattedValue, $year, 0, 2); |
|
507
|
|
|
$formattedValue = date('Y-m-d', strtotime($formattedValue)); |
|
508
|
|
|
break; |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
$formattedValue .= ' 00:00:00'; |
|
512
|
|
|
$userTimezone = $current_user->getPreference('timezone'); |
|
513
|
|
|
$utz = new DateTimeZone($userTimezone); |
|
514
|
|
|
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', |
|
515
|
|
|
$formattedValue, $utz); |
|
516
|
|
|
$dateTime->setTimezone(new DateTimeZone('UTC')); |
|
517
|
|
|
|
|
518
|
|
|
return $dateTime; |
|
519
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.