Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
holiday.php
1<?php
10
14
15Loc::loadMessages(__FILE__);
16
18{
20 private $code;
22 private $date;
24 private $dateFrom;
26 private $dateTo;
27
29 private static $list = [
30 'feb14' => [14, 2], // code => [day, month]
31 'feb23' => [23, 2],
32 'mar8' => [8, 3],
33 'halloween' => [31, 10],
34 'thanks' => [22, 11],
35 'christmas' => [25, 12],
36 'new_year' => [
37 [1, 12],
38 [10, 1],
39 ],
40 ];
41
43 private static $defaultYear = 2049;
44
51 public static function getList($languageId = LANGUAGE_ID)
52 {
53 switch ($languageId)
54 {
55 case 'ru':
56 $listLocal = ['feb14', 'feb23', 'mar8', 'halloween', 'new_year'];
57 break;
58
59 case 'ua':
60 $listLocal = ['feb14', 'day_mar8', 'halloween', 'new_year'];
61 break;
62
63 default:
64 $listLocal = ['feb14', 'halloween', 'thanks', 'christmas'];
65 }
66
67 $list = [];
68 foreach ($listLocal as $code)
69 {
70 if (!isset(self::$list[$code]))
71 {
72 continue;
73 }
74
75 $period = self::$list[$code];
76 if (is_array($period[0]))
77 {
78 $date = null;
79 $dateFrom = self::createDate($period[0][0], $period[0][1]);
80 $dateTo = self::createDate($period[1][0], $period[1][1]);
81 }
82 else
83 {
84 $date = self::createDate($period[0], $period[1]);
85 $dateFrom = self::createDate($period[0], $period[1])->add('-5 days');
86 $dateTo = self::createDate($period[0], $period[1])->add('+3 days');
87 }
88
89 $list[] = new self($code, $date, $dateFrom, $dateTo);
90 }
91
92 return $list;
93 }
94
103 public function __construct($code, Date $date = null, Date $dateFrom, Date $dateTo)
104 {
105 $this->code = $code;
106 $this->date = $date;
107 $this->dateFrom = $dateFrom;
108 $this->dateTo = $dateTo;
109 }
110
116 public function getCode()
117 {
118 return $this->code;
119 }
120
126 public function getCodeUpper()
127 {
128 return mb_strtoupper($this->code);
129 }
130
138 public function getName($template = null, $placeholder = '%name%')
139 {
140 $name = Loc::getMessage('SENDER_INTEGRATION_HOLIDAY_' . $this->getCodeUpper());
141 if ($template)
142 {
143 return str_replace($placeholder, $name, $template);
144 }
145
146 return $name;
147 }
148
154 public function formatDate()
155 {
156 return $this->date ? PrettyDate::formatDate($this->date) : $this->getName();
157 }
158
164 public function getDate()
165 {
166 return $this->date;
167 }
168
174 public function getDay()
175 {
176 return $this->dateFrom->format('j');
177 }
178
184 public function getMonth()
185 {
186 return $this->dateFrom->format('n');
187 }
188
194 public function getDateFrom()
195 {
196 return $this->dateFrom;
197 }
198
204 public function getDateTo()
205 {
206 return $this->dateTo;
207 }
208
209 private static function createDate($day, $month, $year = null)
210 {
211 return Date::createFromTimestamp(mktime(0, 0, 0, $month, $day, $year ?: date('Y')));
212 }
213}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
getName($template=null, $placeholder='%name%')
Definition holiday.php:138
static getList($languageId=LANGUAGE_ID)
Definition holiday.php:51
__construct($code, Date $date=null, Date $dateFrom, Date $dateTo)
Definition holiday.php:103