Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
user.php
1
<?php
8
namespace
Bitrix\Vote
;
9
use
Bitrix\Main\Application
;
10
use
Bitrix\Main\ArgumentException
;
11
use
Bitrix\Main\DB\SqlExpression
;
12
use \Bitrix\Main\Entity;
13
use \Bitrix\Main\Error;
14
use \Bitrix\Main\Localization\Loc;
15
use
Bitrix\Main\ORM\Data\AddResult
;
16
use
Bitrix\Main\ORM\Event
;
17
use \Bitrix\Main\Result;
18
use \Bitrix\Main\Type\DateTime;
19
use \Bitrix\Vote\Base\BaseObject;
20
use \Bitrix\Vote\Vote;
21
22
Loc::loadMessages
(__FILE__);
23
52
class
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
137
class
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
}
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getConnection
static getConnection($name="")
Definition
application.php:611
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\DB\SqlExpression
Definition
sqlexpression.php:19
Bitrix\Main\Event
Definition
event.php:5
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Data\AddResult
Definition
addresult.php:12
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Main\UserTable
Definition
user.php:46
Bitrix\Vote\Base\BaseObject
Definition
baseobject.php:10
Bitrix\Vote\Base\BaseObject\getId
getId()
Definition
baseobject.php:35
Bitrix\Vote\Base\BaseObject\$id
$id
Definition
baseobject.php:12
Bitrix\Vote\Base\BaseObject\loadFromId
static loadFromId($id, $shouldBeNewIfIdIsNull=false)
Definition
baseobject.php:141
Bitrix\Vote\User
Definition
user.php:138
Bitrix\Vote\User\getLockingKey
getLockingKey(int $voteId)
Definition
user.php:288
Bitrix\Vote\User\getCurrent
static getCurrent()
Definition
user.php:312
Bitrix\Vote\User\unlock
unlock(int $voteId)
Definition
user.php:303
Bitrix\Vote\User\$usersIds
static $usersIds
Definition
user.php:141
Bitrix\Vote\User\init
init()
Definition
user.php:149
Bitrix\Vote\User\lock
lock(int $voteId)
Definition
user.php:297
Bitrix\Vote\User\setCookieId
setCookieId($id)
Definition
user.php:191
Bitrix\Vote\User\getCookieId
getCookieId()
Definition
user.php:158
Bitrix\Vote\User\setVotedUserId
setVotedUserId($incrementCount=null)
Definition
user.php:200
Bitrix\Vote\User\SYSTEM_USER_ID
const SYSTEM_USER_ID
Definition
user.php:140
Bitrix\Vote\User\$instance
static $instance
Definition
user.php:143
Bitrix\Vote\User\onUserLogin
static onUserLogin()
Definition
user.php:320
Bitrix\Vote\User\getVotedUserId
getVotedUserId()
Definition
user.php:166
Bitrix\Vote\UserTable\getMap
static getMap()
Definition
user.php:69
Bitrix\Vote\UserTable\setCounter
static setCounter(array $id, $increment=true)
Definition
user.php:121
Bitrix\Vote\UserTable\getTableName
static getTableName()
Definition
user.php:59
Bitrix\Vote
Definition
answer.php:8
modules
vote
lib
user.php
Создано системой
1.10.0