Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
user.php
1<?php
8namespace Bitrix\Vote;
12use \Bitrix\Main\Entity;
13use \Bitrix\Main\Error;
14use \Bitrix\Main\Localization\Loc;
17use \Bitrix\Main\Result;
18use \Bitrix\Main\Type\DateTime;
19use \Bitrix\Vote\Base\BaseObject;
20use \Bitrix\Vote\Vote;
21
22Loc::loadMessages(__FILE__);
23
52class UserTable extends Entity\DataManager
53{
59 public static function getTableName()
60 {
61 return 'b_vote_user';
62 }
63
69 public static function getMap()
70 {
71 return array(
72 'ID' => array(
73 'data_type' => 'integer',
74 'primary' => true,
75 'autocomplete' => true,
76 'title' => Loc::getMessage('V_TABLE_FIELD_ID'),
77 ),
78 'COOKIE_ID' => array(
79 'data_type' => 'integer',
80 'title' => Loc::getMessage('V_TABLE_FIELD_AUTH_USER_ID'),
81 ),
82 'AUTH_USER_ID' => array(
83 'data_type' => 'integer',
84 'title' => Loc::getMessage('V_TABLE_FIELD_AUTH_USER_ID'),
85 ),
86 'COUNTER' => array(
87 'data_type' => 'integer',
88 'title' => Loc::getMessage('V_TABLE_FIELD_COUNTER'),
89 ),
90 'DATE_FIRST' => array(
91 'data_type' => 'datetime',
92 'title' => Loc::getMessage('V_TABLE_FIELD_DATE_FIRST'),
93 ),
94 'DATE_LAST' => array(
95 'data_type' => 'datetime',
96 'title' => Loc::getMessage('V_TABLE_FIELD_DATE_LAST'),
97 ),
98 'LAST_IP' => array(
99 'data_type' => 'string',
100 'size' => 15,
101 'title' => Loc::getMessage('V_TABLE_FIELD_STAT_SESSION_ID')
102 ),
103 'STAT_GUEST_ID' => array(
104 'data_type' => 'integer',
105 'title' => Loc::getMessage('V_TABLE_FIELD_STAT_GUEST_ID'),
106 ),
107 'USER' => array(
108 'data_type' => '\Bitrix\Main\UserTable',
109 'reference' => array(
110 '=this.AUTH_USER_ID' => 'ref.ID',
111 ),
112 'join_type' => 'LEFT',
113 ),
114 );
115 }
121 public static function setCounter(array $id, $increment = true)
122 {
123 if (empty($id))
124 return;
125
126 $connection = \Bitrix\Main\Application::getInstance()->getConnection();
127
128 $sql = intval($increment);
129 if ($increment === true)
130 $sql = "COUNTER+1";
131 else if ($increment === false)
132 $sql = "COUNTER-1";
133 $connection->queryExecute("UPDATE ".self::getTableName()." SET COUNTER=".$sql." WHERE ID IN (".implode(", ", $id).")");
134 }
135}
136
137class User extends BaseObject
138{
139 private const DB_TIMELOCK = 15;
140 const SYSTEM_USER_ID = 0;
141 static $usersIds = [];
142
143 static $instance = null;
144
149 public function init()
150 {
151/* if ($this->id != $this->getUser()->getId())
152 throw new ArgumentException("User id is wrong.");*/
153 }
154
158 public function getCookieId()
159 {
160 global $APPLICATION;
161 return intval($APPLICATION->get_cookie("VOTE_USER_ID"));
162 }
166 public function getVotedUserId()
167 {
168 $cookieId = self::getCookieId();
169 $filter = [
170 "COOKIE_ID" => $cookieId,
171 "AUTH_USER_ID" => intval($this->getId())
172 ];
173 $id = implode("_", $filter);
174
175 if ($cookieId > 0 && !array_key_exists($id, self::$usersIds) && ($res = UserTable::getList([
176 "select" => ["ID"],
177 "filter" => [
178 "COOKIE_ID" => $cookieId,
179 "AUTH_USER_ID" => intval($this->getId())
180 ]
181 ])->fetch()))
182 {
183 self::$usersIds[$id] = intval($res["ID"]);
184 }
185 return isset(self::$usersIds[$id]) ? self::$usersIds[$id] : 0;
186 }
187
191 public function setCookieId($id)
192 {
193 $cookie = new \Bitrix\Main\Web\Cookie("VOTE_USER_ID", strval($id));
194 \Bitrix\Main\Context::getCurrent()->getResponse()->addCookie($cookie);
195 }
200 public function setVotedUserId($incrementCount = null)
201 {
202 $id = $this->getVotedUserId();
203 $fields = array(
204 "STAT_GUEST_ID" => intval($_SESSION["SESS_GUEST_ID"]),
205 "DATE_LAST" => new DateTime(),
206 "LAST_IP" => $_SERVER["REMOTE_ADDR"]
207 );
208 if ($incrementCount === true)
209 $fields["COUNTER"] = new SqlExpression('?# + 1', 'COUNTER');
210 else if ($incrementCount === false)
211 $fields["COUNTER"] = new SqlExpression('?# - 1', 'COUNTER');
212
213 if ($id > 0)
214 {
215 $dbRes = UserTable::update($id, $fields);
216 $dbRes->setData(["COOKIE_ID" => $this->getCookieId()]);
217 }
218 else
219 {
220 $add = true;
221 $fields = [
222 "AUTH_USER_ID" => intval($this->getId()),
223 "DATE_FIRST" => new DateTime(),
224 "COUNTER" => ($incrementCount === true ? 1 : 0)
225 ] + $fields;
226 if ($this->getCookieId() > 0)
227 {
228 $dbRes = UserTable::add(["COOKIE_ID" => $this->getCookieId()] + $fields);
229 $add = !$dbRes->isSuccess();
230 }
231 if ($add)
232 {
233 $connection = \Bitrix\Main\Application::getInstance()->getConnection();
234 $insert = $connection->getSqlHelper()->prepareInsert(UserTable::getTableName(), $fields);
235 $connection->queryExecute(
236 "INSERT INTO ".UserTable::getTableName()."(COOKIE_ID, ".$insert[0].") ".
237 "SELECT MAX(COOKIE_ID) + 1, ".$insert[1] . " FROM ".UserTable::getTableName());
238 $dbRes = new AddResult();
239 $dbRes->setId($connection->getInsertedId());
240 $dbRes->setData(UserTable::getById($dbRes->getId())->fetch());
241 }
242 }
243 $id = $dbRes->getId();
244 $fields = $dbRes->getData();
245 self::$usersIds[implode(
246 "_",
247 [
248 "COOKIE_ID" => $fields["COOKIE_ID"],
249 "AUTH_USER_ID" => $fields["AUTH_USER_ID"]
250 ]
251 )] = $id;
252 self::setCookieId($fields["COOKIE_ID"]);
253 return $id;
254 }
255
260 public function isVotedFor($voteId)
261 {
262 $result = false;
263 if ($voteId > 0)
264 {
266 $vote = Vote::loadFromId($voteId);
267 $result = $vote->isVotedFor($this);
268 }
269 return $result;
270 }
276 public static function isUserVotedFor($voteId, $userId)
277 {
278 $result = false;
279 if ($voteId > 0)
280 {
282 $vote = Vote::loadFromId($voteId);
283 $result = $vote->isVotedFor($userId);
284 }
285 return $result;
286 }
287
288 protected function getLockingKey(int $voteId): string
289 {
290 return implode('_', [
291 static::class,
292 $this->getId() > 0 ? $this->getId() : bitrix_sessid(),
293 $voteId
294 ]);
295 }
296
297 public function lock(int $voteId): bool
298 {
299 $lockingKey = $this->getLockingKey($voteId);
300 return Application::getConnection()->lock($lockingKey, self::DB_TIMELOCK);
301 }
302
303 public function unlock(int $voteId)
304 {
305 $lockingKey = $this->getLockingKey($voteId);
306 Application::getConnection()->unlock($lockingKey);
307 }
308
312 public static function getCurrent()
313 {
314 global $USER;
315 if (is_null(self::$instance))
316 self::$instance = self::loadFromId($USER->getId());
317 return self::$instance;
318 }
319
320 public static function onUserLogin()
321 {
322 $_SESSION["VOTE"] = ["VOTES" => []];
323 }
324}
static getConnection($name="")
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static loadFromId($id, $shouldBeNewIfIdIsNull=false)
getLockingKey(int $voteId)
Definition user.php:288
static getCurrent()
Definition user.php:312
unlock(int $voteId)
Definition user.php:303
static $usersIds
Definition user.php:141
lock(int $voteId)
Definition user.php:297
setCookieId($id)
Definition user.php:191
setVotedUserId($incrementCount=null)
Definition user.php:200
const SYSTEM_USER_ID
Definition user.php:140
static $instance
Definition user.php:143
static onUserLogin()
Definition user.php:320
static getMap()
Definition user.php:69
static setCounter(array $id, $increment=true)
Definition user.php:121
static getTableName()
Definition user.php:59