Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
spotlight.php
1<?
2
3namespace Bitrix\Main\UI;
4
8
10{
11 const USER_TYPE_OLD = "OLD";
12 const USER_TYPE_NEW = "NEW";
13 const USER_TYPE_ALL = "ALL";
14
15 const CATEGORY_NAME = "spotlight";
16
17 private $id;
18 private $userType;
19 private $userTimeSpan;
20 private $lifetime;
21 private $startDate = null;
22 private $endDate = null;
23
31 public function __construct($id)
32 {
33 if (!is_string($id))
34 {
35 throw new ArgumentTypeException("id", "string");
36 }
37
38 $this->id = $id;
39
40 $this->userTimeSpan = 3600 * 24 * 30;
41 $this->userType = static::USER_TYPE_OLD;
42 $this->lifetime = 3600 * 24 * 30;
43 }
44
50 public function isAvailable($userId = false)
51 {
52 $now = time();
53
54 if (
55 ($this->getStartDate() && $now < $this->getStartDate()) ||
56 ($this->getEndDate() && $now > $this->getEndDate())
57 )
58 {
59 return false;
60 }
61
62 $userId = $this->getUserId($userId);
63 if ($userId < 1)
64 {
65 return false;
66 }
67
68 if ($this->isViewed($userId))
69 {
70 return false;
71 }
72
73 $activationDate = $this->getActivationDate();
74 if ($this->getLifetime() && $activationDate + $this->getLifetime() < $now)
75 {
76 return false;
77 }
78
79 $userType = $this->getUserType();
80 if ($userType !== static::USER_TYPE_ALL)
81 {
82 $registerDate = $this->getRegisterDate($userId);
83 if ($registerDate === false)
84 {
85 return false;
86 }
87
88 $newUserDate = $activationDate - $this->getUserTimeSpan();
89 if ($userType === static::USER_TYPE_OLD && $registerDate > $newUserDate)
90 {
91 return false;
92 }
93
94 if ($userType === static::USER_TYPE_NEW && $registerDate < $newUserDate)
95 {
96 return false;
97 }
98 }
99
100 return true;
101 }
102
103 public function getActivationDate()
104 {
105 $activationDate = intval(Option::get(static::CATEGORY_NAME, $this->getActDateOptionName(), 0));
106 if ($activationDate === 0)
107 {
108 $activationDate = $this->activate();
109 }
110
111 return $activationDate;
112 }
113
114 public function activate($activationDate = false)
115 {
116 $activationDate = is_int($activationDate) ? $activationDate : time();
117 Option::set(static::CATEGORY_NAME, $this->getActDateOptionName(), $activationDate);
118
119 return $activationDate;
120 }
121
122 public function deactivate()
123 {
124 Option::delete(static::CATEGORY_NAME, array("name" => $this->getActDateOptionName()));
125 }
126
127 public function isViewed($userId)
128 {
129 return $this->getViewDate($userId) > 0;
130 }
131
132 public function getViewDate($userId)
133 {
134 $userId = intval($userId);
135
136 return intval(\CUserOptions::getOption(static::CATEGORY_NAME, $this->getViewDateOptionName(), 0, $userId));
137 }
138
139 public function setViewDate($userId, $date = false)
140 {
141 $userId = intval($userId);
142 $date = is_int($date) ? $date : time();
143 \CUserOptions::setOption(static::CATEGORY_NAME, $this->getViewDateOptionName(), $date, false, $userId);
144
145 return $date;
146 }
147
148 public function unsetViewDate($userId)
149 {
150 $userId = intval($userId);
151 \CUserOptions::deleteOption(static::CATEGORY_NAME, $this->getViewDateOptionName(), false, $userId);
152 }
153
154 private function getViewDateOptionName()
155 {
156 return "view_date_".$this->getId();
157 }
158
159 private function getActDateOptionName()
160 {
161 return "activation_date_".$this->getId();
162 }
163
164 private function getRegisterDate($userId)
165 {
166 $user = $this->getUser($userId);
167 return $user && isset($user["DATE_REGISTER"]) ? $user["DATE_REGISTER"]->getTimestamp() : false;
168 }
169
170 private function getUserId($userId = false)
171 {
172 return $userId === false && is_object($GLOBALS["USER"]) ? $GLOBALS["USER"]->getID() : intval($userId);
173 }
174
175 private function getUser($userId)
176 {
177 return UserTable::getRow(
178 array(
179 "select" => array("ID", "DATE_REGISTER"),
180 "filter" => array(
181 "=ID" => $userId
182 ),
183 )
184 );
185 }
186
190 public function getId()
191 {
192 return $this->id;
193 }
194
198 public function getUserType()
199 {
200 return $this->userType;
201 }
202
206 public function setUserType($userType)
207 {
208 if (in_array($userType, static::getUserTypes()))
209 {
210 $this->userType = $userType;
211 }
212 }
213
214 public static function getUserTypes()
215 {
216 static $types = null;
217 if ($types !== null)
218 {
219 return $types;
220 }
221
222 $types = array();
223 $refClass = new \ReflectionClass(__CLASS__);
224 foreach ($refClass->getConstants() as $name => $value)
225 {
226 if (mb_substr($name, 0, 9) === "USER_TYPE")
227 {
228 $types[] = $value;
229 }
230 }
231
232 return $types;
233 }
234
238 public function getUserTimeSpan()
239 {
240 return $this->userTimeSpan;
241 }
242
246 public function setUserTimeSpan($userTimeSpan)
247 {
248 if (is_int($userTimeSpan) && $userTimeSpan >= 0)
249 {
250 $this->userTimeSpan = $userTimeSpan;
251 }
252 }
253
257 public function getLifetime()
258 {
259 return $this->lifetime;
260 }
261
265 public function setLifetime($lifetime)
266 {
267 if (is_int($lifetime) && $lifetime >= 0)
268 {
269 $this->lifetime = $lifetime;
270 }
271 }
272
276 public function getStartDate()
277 {
278 return $this->startDate;
279 }
280
284 public function setStartDate($startDate)
285 {
286 if (is_int($startDate) || $startDate === null)
287 {
288 $this->startDate = $startDate;
289 }
290 }
291
295 public function getEndDate()
296 {
297 return $this->endDate;
298 }
299
303 public function setEndDate($endDate)
304 {
305 if (is_int($endDate) || $endDate)
306 {
307 $this->endDate = $endDate;
308 }
309 }
310}
static getRow(array $parameters)
setUserTimeSpan($userTimeSpan)
activate($activationDate=false)
setViewDate($userId, $date=false)
isAvailable($userId=false)
Definition spotlight.php:50
$GLOBALS['____1444769544']
Definition license.php:1