Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
timeperiod.php
1<?php
2
4
9
10
16{
17 const JS_EVENT_ON_SELECT = 'onSelect';
18
19 const FILTER_PERIOD_TIME = 'FILTER';
20 const QUARTER_PERIOD_TIME = 'QUARTER';
21 const YEAR_PERIOD_TIME = 'YEAR';
22 const MONTH_PERIOD_TIME = 'MONTH';
23 const THIS_YEAR_PERIOD_TIME = 'THIS_YEAR';
24 const THIS_QUARTER_PERIOD_TIME = 'THIS_QUARTER';
25 const THIS_MONTH_PERIOD_TIME = 'THIS_MONTH';
26 const THIS_DAY_PERIOD_TIME = 'THIS_DAY';
27 const LAST_90_DAYS_PERIOD_TIME = 'LAST_90_DAYS';
28 const LAST_60_DAYS_PERIOD_TIME = 'LAST_60_DAYS';
29 const LAST_30_DAYS_PERIOD_TIME = 'LAST_30_DAYS';
30 const LAST_7_DAYS_PERIOD_TIME = 'LAST_7_DAYS';
31
32
34
35
37
38
39 private $filterId;
40
47 public function __construct($key, $filterId = null)
48 {
49 parent::__construct($key);
50 $this->setLabel('Time Period: ');
52 $this->setFilterId($filterId);
53 }
54
55
61 public function printContent()
62 {
63 $this->includeFieldComponent('timeperiod');
64 }
65
66
70 public function getDefaultConfigValue()
71 {
72 return array(
73 'type' => static::DEFAULT_TIME_PERIOD_TYPE,
74 'month' => $this->getCurrentMonth(),
75 'quarter' => $this->getCurrentQuarter(),
76 'year' => $this->getCurrentYear(),
77 );
78 }
79
83 public function getFilterId()
84 {
85 return $this->filterId;
86 }
87
94 public function setFilterId($id)
95 {
96 $this->filterId = $id;
97 }
98
105 protected function normalise($config)
106 {
107 if (isset($config['type']))
108 {
109 return array(
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(),
114 );
115 }
116 else
117 {
118 return $this->getDefaultConfigValue();
119 }
120 }
121
127 public function getValueAsPeriod()
128 {
129 $value = $this->getValue();
130 $defaultStartDate = strtotime("-1 year", time());
131 $defaultStartDate = date('Y-m-d', $defaultStartDate);
132 $result = array(
133 'start' => new DateTime($defaultStartDate, 'Y-m-d'),
134 'end' => new DateTime(),
135 );
136 switch ($value['type'])
137 {
139 $forFilter = $this->prepareTimePeriodForFilter();
140 if ($forFilter)
141 {
142 $result = $forFilter;
143 }
144 break;
146 $result = $this->prepareTimePeriodForQuarter($value);
147 break;
149 $result = $this->prepareTimePeriodForYear($value);
150 break;
152 $result = $this->prepareTimePeriodForMonth($value);
153 break;
155 $result = $this->prepareTimePeriodForThisYear();
156 break;
158 $result = $this->prepareTimePeriodForThisQuarter();
159 break;
161 $result = $this->prepareTimePeriodForThisMonth();
162 break;
164 $result = $this->prepareTimePeriodForLastDays();
165 break;
167 $result = $this->prepareTimePeriodForLastDays(90);
168 break;
170 $result = $this->prepareTimePeriodForLastDays(60);
171 break;
173 $result = $this->prepareTimePeriodForLastDays(30);
174 break;
176 $result = $this->prepareTimePeriodForLastDays(7);
177 break;
178
179 }
180 $result['type'] = $value['type'];
181 return $result;
182 }
183
189 private function prepareTimePeriodForFilter()
190 {
191 $result = array();
192 $filter = $this->getFilterOptions()->getFilter(Filter::getFieldsList());
193 if ($filter)
194 {
195 $result = array(
196 'start' => new DateTime($filter['TIME_PERIOD_from']),
197 'end' => new DateTime($filter['TIME_PERIOD_to'])
198 );
199 }
200 return $result;
201 }
202
209 private function prepareTimePeriodForQuarter($value)
210 {
211 $year = (int)$value['year'];
212 $startYear = $endYear = $year;
213 $quarter = (int)$value['quarter'];
214 $quarterStartMonth = 1 + ($quarter - 1) * 3;
215 if ($quarterStartMonth < 10)
216 {
217 $quarterStartMonth = '0' . $quarterStartMonth;
218 }
219
220 $quarterEndMonth = 1 + $quarter * 3;
221 if ($quarterEndMonth < 10)
222 {
223 $quarterEndMonth = '0' . $quarterEndMonth;
224 }
225 elseif ($quarterEndMonth === 13)
226 {
227 $endYear++;
228 $quarterEndMonth = '01';
229 }
230
231 $quarterStartStr = $startYear . '-' . $quarterStartMonth . '-01 00:00';
232 $quarterEndStr = $endYear . '-' . $quarterEndMonth . '-01 00:00';
233 $result = array(
234 'start' => new DateTime($quarterStartStr, 'Y-m-d H:i'),
235 'end' => new DateTime($quarterEndStr, 'Y-m-d H:i'),
236 );
237 return $result;
238 }
239
246 private function prepareTimePeriodForYear($value)
247 {
248 $year = $value['year'];
249 $yearStartStr = $year . '01-01 00:00';
250 $yearEndStr = $year + 1 . '01-01 00:00';
251 $result = array(
252 'start' => new DateTime($yearStartStr, 'Y-m-d H:i'),
253 'end' => new DateTime($yearEndStr, 'Y-m-d H:i'),
254 );
255 return $result;
256 }
257
264 private function prepareTimePeriodForMonth($value)
265 {
266 $year = $value['year'];
267 $month = $value['month'];
268 $startYear = $endYear = $year;
269 $startMonth = $month;
270 $endMonth = $month + 1;
271
272 if ($startMonth < 10)
273 {
274 $startMonth = '0' . $startMonth;
275 }
276 if ($endMonth < 10)
277 {
278 $endMonth = '0' . $endMonth;
279 }
280 if ($endMonth == 13)
281 {
282 $endYear++;
283 $endMonth = '01';
284 }
285 $monthStartStr = $startYear . '-' . $startMonth . '-01 00:00';
286 $monthEndStr = $endYear . '-' . $endMonth . '-01 00:00';
287 $result = array(
288 'start' => new DateTime($monthStartStr, 'Y-m-d H:i'),
289 'end' => new DateTime($monthEndStr, 'Y-m-d H:i'),
290 );
291 return $result;
292 }
293
299 private function prepareTimePeriodForThisYear()
300 {
301 $year = new DateTime();
302 $thisYear = (int)$year->format('Y');
303 $value['year'] = $thisYear;
304 return $this->prepareTimePeriodForYear($value);
305 }
306
312 private function prepareTimePeriodForThisMonth()
313 {
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);
320 }
321
327 private function prepareTimePeriodForThisQuarter()
328 {
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);
335 }
336
343 private function prepareTimePeriodForLastDays($dayCount = 0)
344 {
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);
349
350 return array(
351 'start' => new DateTime($agoDays, 'Y-m-d H:i'),
352 'end' => new DateTime($nextDay, 'Y-m-d H:i'),
353 );
354 }
355
359 private function getFilterOptions()
360 {
361 $options = new Options($this->getFilterId(), Filter::getPresetsList());
362 return $options;
363 }
367 public function getTypeList()
368 {
369 $typeList = array();
370 if ($this->getFilterId())
371 {
372 $typeList += array(
373 self::FILTER_PERIOD_TIME => Loc::getMessage('REPORT_TIME_PERIOD_FIELD_TYPE_FILTER'),
374 );
375 }
376 $typeList += array(
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'),
388 );
389 return $typeList;
390 }
394 public function getMonthList()
395 {
396 $monthList = array(
397 1 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_JANUARY'),
398 2 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_FEBRUARY'),
399 3 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_MARCH'),
400 4 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_APRIL'),
401 5 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_MAY'),
402 6 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_JUNE'),
403 7 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_JULY'),
404 8 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_AUGUST'),
405 9 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_SEPTEMBER'),
406 10 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_OCTOBER'),
407 11 => Loc::getMessage('REPORT_TIME_PERIOD_MONTH_NOVEMBER'),
408 12 =>Loc::getMessage('REPORT_TIME_PERIOD_MONTH_DECEMBER'),
409 );
410 return $monthList;
411 }
412
416 public function getQuarterList()
417 {
418 $quarterList = array(
419 1 => 'I',
420 2 => 'II',
421 3 => 'III',
422 4 => 'IV',
423 );
424 return $quarterList;
425 }
426
430 public function getYearList()
431 {
432 $yearList = array();
433 for ($year = static::DEFAULT_YEAR_LIST_START; $year < $this->getCurrentYear() + 5; $year++)
434 {
435 $yearList[$year] = $year;
436 }
437 return $yearList;
438 }
439
443 public function getValueForHuman()
444 {
445 $value = $this->getValue();
446 $typeList = $this->getTypeList();
447 $result = $typeList[$value['type']];
448 switch ($value['type'])
449 {
451 $result .= ': ' . $value['year'];
452 break;
454 $monthList = $this->getMonthList();
455 $monthName = $monthList[$value['month']];
456 $result .= ': ' . $monthName . ' ' . $value['year'];
457 break;
459 $quarterList = $this->getQuarterList();
460 $quarterName = $quarterList[$value['quarter']];
461 $result .= ': ' . $quarterName . ' ' . $value['year'];
462 }
463 return $result;
464 }
465
469 public function getDateTime()
470 {
471 return new \DateTime();
472 }
473
477 public function getCurrentYear()
478 {
479 return (int)$this->getDateTime()->format('Y');
480 }
481
485 public function getCurrentMonth()
486 {
487 return (int)$this->getDateTime()->format('m');
488 }
489
493 public function getCurrentQuarter()
494 {
495 return (int)ceil($this->getCurrentMonth() / 3);
496 }
497}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
includeFieldComponent($templateName, $params=array())
Definition base.php:530