49 parent::__construct($key);
73 'type' => static::DEFAULT_TIME_PERIOD_TYPE,
85 return $this->filterId;
96 $this->filterId = $id;
107 if (isset($config[
'type']))
110 'type' => $config[
'type'],
111 'month' => !empty($config[
'month']) ? $config[
'month'] : $this->
getCurrentMonth(),
112 'quarter' => !empty($config[
'quarter']) ? $config[
'quarter'] : $this->
getCurrentQuarter(),
113 'year' => !empty($config[
'year']) ? $config[
'year'] : $this->
getCurrentYear(),
130 $defaultStartDate = strtotime(
"-1 year", time());
131 $defaultStartDate = date(
'Y-m-d', $defaultStartDate);
133 'start' =>
new DateTime($defaultStartDate,
'Y-m-d'),
136 switch ($value[
'type'])
139 $forFilter = $this->prepareTimePeriodForFilter();
142 $result = $forFilter;
146 $result = $this->prepareTimePeriodForQuarter($value);
149 $result = $this->prepareTimePeriodForYear($value);
152 $result = $this->prepareTimePeriodForMonth($value);
155 $result = $this->prepareTimePeriodForThisYear();
158 $result = $this->prepareTimePeriodForThisQuarter();
161 $result = $this->prepareTimePeriodForThisMonth();
164 $result = $this->prepareTimePeriodForLastDays();
167 $result = $this->prepareTimePeriodForLastDays(90);
170 $result = $this->prepareTimePeriodForLastDays(60);
173 $result = $this->prepareTimePeriodForLastDays(30);
176 $result = $this->prepareTimePeriodForLastDays(7);
180 $result[
'type'] = $value[
'type'];
189 private function prepareTimePeriodForFilter()
192 $filter = $this->getFilterOptions()->getFilter(Filter::getFieldsList());
196 'start' =>
new DateTime($filter[
'TIME_PERIOD_from']),
197 'end' =>
new DateTime($filter[
'TIME_PERIOD_to'])
209 private function prepareTimePeriodForQuarter($value)
211 $year = (int)$value[
'year'];
212 $startYear = $endYear = $year;
213 $quarter = (int)$value[
'quarter'];
214 $quarterStartMonth = 1 + ($quarter - 1) * 3;
215 if ($quarterStartMonth < 10)
217 $quarterStartMonth =
'0' . $quarterStartMonth;
220 $quarterEndMonth = 1 + $quarter * 3;
221 if ($quarterEndMonth < 10)
223 $quarterEndMonth =
'0' . $quarterEndMonth;
225 elseif ($quarterEndMonth === 13)
228 $quarterEndMonth =
'01';
231 $quarterStartStr = $startYear .
'-' . $quarterStartMonth .
'-01 00:00';
232 $quarterEndStr = $endYear .
'-' . $quarterEndMonth .
'-01 00:00';
234 'start' =>
new DateTime($quarterStartStr,
'Y-m-d H:i'),
235 'end' =>
new DateTime($quarterEndStr,
'Y-m-d H:i'),
246 private function prepareTimePeriodForYear($value)
248 $year = $value[
'year'];
249 $yearStartStr = $year .
'01-01 00:00';
250 $yearEndStr = $year + 1 .
'01-01 00:00';
252 'start' =>
new DateTime($yearStartStr,
'Y-m-d H:i'),
253 'end' =>
new DateTime($yearEndStr,
'Y-m-d H:i'),
264 private function prepareTimePeriodForMonth($value)
266 $year = $value[
'year'];
267 $month = $value[
'month'];
268 $startYear = $endYear = $year;
269 $startMonth = $month;
270 $endMonth = $month + 1;
272 if ($startMonth < 10)
274 $startMonth =
'0' . $startMonth;
278 $endMonth =
'0' . $endMonth;
285 $monthStartStr = $startYear .
'-' . $startMonth .
'-01 00:00';
286 $monthEndStr = $endYear .
'-' . $endMonth .
'-01 00:00';
288 'start' =>
new DateTime($monthStartStr,
'Y-m-d H:i'),
289 'end' =>
new DateTime($monthEndStr,
'Y-m-d H:i'),
299 private function prepareTimePeriodForThisYear()
301 $year =
new DateTime();
302 $thisYear = (int)$year->format(
'Y');
303 $value[
'year'] = $thisYear;
304 return $this->prepareTimePeriodForYear($value);
312 private function prepareTimePeriodForThisMonth()
314 $year =
new DateTime();
315 $thisYear = (int)$year->format(
'Y');
316 $thisMonth = (int)$year->format(
'm');
317 $value[
'year'] = $thisYear;
318 $value[
'month'] = $thisMonth;
319 return $this->prepareTimePeriodForMonth($value);
327 private function prepareTimePeriodForThisQuarter()
329 $year =
new DateTime();
330 $thisYear = (int)$year->format(
'Y');
331 $thisMonth = (int)$year->format(
'm');
332 $value[
'year'] = $thisYear;
333 $value[
'quarter'] = ceil($thisMonth / 3);
334 return $this->prepareTimePeriodForQuarter($value);
343 private function prepareTimePeriodForLastDays($dayCount = 0)
345 $nextDay = strtotime(
"+1 day", time());
346 $nextDay = date(
'Y-m-d', $nextDay);
347 $agoDays = strtotime(
"-" . $dayCount .
" day", time());
348 $agoDays = date(
'Y-m-d', $agoDays);
351 'start' =>
new DateTime($agoDays,
'Y-m-d H:i'),
352 'end' =>
new DateTime($nextDay,
'Y-m-d H:i'),
359 private function getFilterOptions()
361 $options =
new Options($this->
getFilterId(), Filter::getPresetsList());
373 self::FILTER_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_FILTER'),
377 self::YEAR_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_YEAR'),
378 self::QUARTER_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_QUARTER'),
379 self::MONTH_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_MONTH'),
380 self::THIS_YEAR_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_THIS_YEAR'),
381 self::THIS_QUARTER_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_THIS_QUARTER'),
382 self::THIS_MONTH_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_THIS_MONTH'),
383 self::THIS_DAY_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_THIS_DAY'),
384 self::LAST_90_DAYS_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_LAST_90_DAYS'),
385 self::LAST_60_DAYS_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_LAST_60_DAYS'),
386 self::LAST_30_DAYS_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_LAST_30_DAYS'),
387 self::LAST_7_DAYS_PERIOD_TIME =>
Loc::getMessage(
'REPORT_TIME_PERIOD_FIELD_TYPE_LAST_7_DAYS'),
418 $quarterList = array(
433 for ($year = static::DEFAULT_YEAR_LIST_START; $year < $this->
getCurrentYear() + 5; $year++)
435 $yearList[$year] = $year;
447 $result = $typeList[$value[
'type']];
448 switch ($value[
'type'])
451 $result .=
': ' . $value[
'year'];
455 $monthName = $monthList[$value[
'month']];
456 $result .=
': ' . $monthName .
' ' . $value[
'year'];
460 $quarterName = $quarterList[$value[
'quarter']];
461 $result .=
': ' . $quarterName .
' ' . $value[
'year'];
471 return new \DateTime();
static getMessage($code, $replace=null, $language=null)