Newer
Older
use QUI\Lock\Locker;
use QUI\Memberships\Users\Handler as MembershipUsersHandler;
use QUI\Utils\Security\Orthos;
/**
* Get IDs of all QUIQQER Groups
*
* @return int[]
*/
public function getGroupIds()
{
$groupIds = $this->getAttribute('groupIds');
return explode(",", trim($groupIds, ","));
}
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Get membership title
*
* @param Locale $Locale (optional)
* @return string - localized title
*/
public function getTitle($Locale = null)
{
if (is_null($Locale)) {
$Locale = QUI::getLocale();
}
$trans = json_decode($this->getAttribute('title'), true);
if (isset($trans[$Locale->getCurrent()])) {
return $trans[$Locale->getCurrent()];
}
return '';
}
/**
* Get membership description
*
* @param Locale $Locale (optional)
* @return string - localized description
*/
public function getDescription($Locale = null)
{
if (is_null($Locale)) {
$Locale = QUI::getLocale();
}
$trans = json_decode($this->getAttribute('description'), true);
if (isset($trans[$Locale->getCurrent()])) {
return $trans[$Locale->getCurrent()];
}
return '';
}
/**
* Get membership content
*
* @param Locale $Locale (optional)
* @return string - localized content
*/
public function getContent($Locale = null)
{
if (is_null($Locale)) {
$Locale = QUI::getLocale();
}
$trans = json_decode($this->getAttribute('content'), true);
if (isset($trans[$Locale->getCurrent()])) {
return $trans[$Locale->getCurrent()];
}
return '';
}
* Check if this membership is auto-extended
public function isAutoExtend()
return $this->getAttribute('autoExtend') ? true : false;
Permission::checkPermission(Handler::PERMISSION_EDIT);
$attributes = $this->getAttributes();
// check groups
if (empty($attributes['groupIds'])
) {
throw new QUI\Memberships\Exception(array(
'quiqqer/memberships',
'exception.handler.no.groups'
));
}
$attributes['groupIds'] = ',' . $attributes['groupIds'] . ',';
// check duration
$duration = explode('-', $attributes['duration']);
if ($duration[0] < 1) {
throw new QUI\Memberships\Exception(array(
'quiqqer/memberships',
'exception.membership.update.duration.invalid'
));
}
// edit user and timestamp
$attributes['editUser'] = QUI::getUserBySession()->getId();
$attributes['editDate'] = Utils::getFormattedTimestamp();
$this->setAttributes($attributes);
* Delete membership
*
* Only possible if membership has no users in it
*/
public function delete()
{
Permission::checkPermission(Handler::PERMISSION_DELETE);
$MembershipUsers = MembershipUsersHandler::getInstance();
if (count($MembershipUsers->getIdsByMembershipId($this->id))) {
throw new Exception(array(
'quiqqer/memberships',
'exception.membership.cannot.delete.with.users.left'
));
}
parent::delete();
}
* Get a user of this membership (non-archived)
*
* @param int $userId - User ID
* @return QUI\Memberships\Users\MembershipUser
* @throws QUI\Memberships\Exception
*/
public function getMembershipUser($userId)
{
$result = QUI::getDataBase()->fetch(array(
'select' => array(
'id'
),
'from' => MembershipUsersHandler::getInstance()->getDataBaseTableName(),
'where' => array(
'membershipId' => $this->id,
'userId' => $userId,
'archived' => 0
)
));
if (empty($result)) {
throw new Exception(array(
'quiqqer/memberships',
'exception.membership.user.not.found',
array(
'userId' => $userId
)
), 404);
}
return MembershipUsersHandler::getInstance()->getChild($result[0]['id']);
}
/**
* Get IDs of all QUIQQER Groups that are UNIQUE to this membership
public function getUniqueGroupIds()
$Memberships = Handler::getInstance();
$groupIds = $this->getGroupIds();
$uniqueGroupIds = $groupIds;
foreach ($Memberships->getMembershipIdsByGroupIds($groupIds) as $membershipId) {
if ($membershipId == $this->getId()) {
continue;
}
$Membership = $Memberships->getChild($membershipId);
foreach ($Membership->getGroupIds() as $groupId) {
if (in_array($groupId, $groupIds)) {
$k = array_search($groupId, $uniqueGroupIds);
if ($k !== false) {
unset($uniqueGroupIds[$k]);
}
}
}
}
* Checks if this membership has an (active, non-archived) user assigned
*
* @param int $userId
* @return bool
*/
public function hasMembershipUserId($userId)
{
$result = QUI::getDataBase()->fetch(array(
'count' => 1,
'select' => array(
'id'
),
'from' => MembershipUsersHandler::getInstance()->getDataBaseTableName(),
'where' => array(
'membershipId' => $this->id,
)
));
return current(current($result)) > 0;
}
/**
*
* @param array $searchParams
* @param bool $countOnly (optional) - get count for search result only [default: false]
* @return int[]|int - membership user IDs or count
*/
public function searchUsers($searchParams, $countOnly = false)
{
$membershipUserIds = array();
$Grid = new QUI\Utils\Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
$tbl = MembershipUsersHandler::getInstance()->getDataBaseTableName();
$usersTbl = QUI::getDBTableName('users');
$binds = array();
$where = array();
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
$sql = "SELECT COUNT(*)";
} else {
$sql = "SELECT `musers`.id";
}
$sql .= " FROM `" . $tbl . "` musers, `" . $usersTbl . "` users";
$where[] = '`musers`.userId = `users`.id';
$where[] = '`musers`.membershipId = ' . $this->id;
$where[] = '`musers`.archived = 0';
if (!empty($searchParams['search'])) {
$whereOR = array();
$searchColumns = array(
'`users`.username',
'`users`.firstname',
'`users`.lastname'
);
foreach ($searchColumns as $tbl => $column) {
$whereOR[] = $column . ' LIKE :search';
$binds['search'] = array(
'value' => '%' . $searchParams['search'] . '%',
'type' => \PDO::PARAM_STR
);
}
$where[] = '(' . implode(' OR ', $whereOR) . ')';
// build WHERE query string
if (!empty($where)) {
$sql .= " WHERE " . implode(" AND ", $where);
}
// ORDER
if (!empty($searchParams['sortOn'])
) {
$order = "ORDER BY " . Orthos::clear($searchParams['sortOn']);
if (isset($searchParams['sortBy']) &&
!empty($searchParams['sortBy'])
) {
$order .= " " . Orthos::clear($searchParams['sortBy']);
} else {
$order .= " ASC";
}
$sql .= " " . $order;
}
// LIMIT
if (!empty($gridParams['limit'])
&& !$countOnly
) {
$sql .= " LIMIT " . $gridParams['limit'];
} else {
if (!$countOnly) {
$sql .= " LIMIT " . (int)20;
}
$Stmt = QUI::getPDO()->prepare($sql);
// bind search values
foreach ($binds as $var => $bind) {
$Stmt->bindValue(':' . $var, $bind['value'], $bind['type']);
}
// fetch information for all corresponding passwords
try {
$Stmt->execute();
$result = $Stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $Exception) {
QUI\System\Log::addError(
self::class . ' :: searchUsers() -> ' . $Exception->getMessage()
);
return array();
}
if ($countOnly) {
return (int)current(current($result));
}
foreach ($result as $row) {
$membershipUserIds[] = (int)$row['id'];
}
return $membershipUserIds;
}
/**
* Search membership users (archived)
*
* @param array $searchParams
* @param bool $countOnly (optional) - get count for search result only [default: false]
* @return int[] - membership user IDs
*/
public function searchArchivedUsers($searchParams, $countOnly = false)
{
$membershipUserIds = array();
$Grid = new QUI\Utils\Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
$tbl = MembershipUsersHandler::getInstance()->getDataBaseTableName();
$usersTbl = QUI::getDBTableName('users');
$binds = array();
$where = array();
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
$sql = "SELECT COUNT(*)";
} else {
$sql = "SELECT `musers`.id";
}
$sql .= " FROM `" . $tbl . "` musers, `" . $usersTbl . "` users";
$where[] = '`musers`.userId = `users`.id';
$where[] = '`musers`.membershipId = ' . $this->id;
$where[] = '`musers`.archived = 1';
if (!empty($searchParams['search'])) {
$whereOR = array();
$searchColumns = array(
'`users`.username',
'`users`.firstname',
'`users`.lastname'
);
foreach ($searchColumns as $tbl => $column) {
$whereOR[] = $column . ' LIKE :search';
$binds['search'] = array(
'value' => '%' . $searchParams['search'] . '%',
'type' => \PDO::PARAM_STR
);
}
$where[] = '(' . implode(' OR ', $whereOR) . ')';
// build WHERE query string
if (!empty($where)) {
$sql .= " WHERE " . implode(" AND ", $where);
}
// ORDER
if (!empty($searchParams['sortOn'])
) {
$order = "ORDER BY " . Orthos::clear($searchParams['sortOn']);
if (isset($searchParams['sortBy']) &&
!empty($searchParams['sortBy'])
) {
$order .= " " . Orthos::clear($searchParams['sortBy']);
} else {
$order .= " ASC";
}
$sql .= " " . $order;
}
// LIMIT
if (!empty($gridParams['limit'])
&& !$countOnly
) {
$sql .= " LIMIT " . $gridParams['limit'];
} else {
if (!$countOnly) {
$sql .= " LIMIT " . (int)20;
}
$Stmt = QUI::getPDO()->prepare($sql);
// bind search values
foreach ($binds as $var => $bind) {
$Stmt->bindValue(':' . $var, $bind['value'], $bind['type']);
}
// fetch information for all corresponding passwords
try {
$Stmt->execute();
$result = $Stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $Exception) {
QUI\System\Log::addError(
self::class . ' :: searchUsers() -> ' . $Exception->getMessage()
);
return array();
}
if ($countOnly) {
return (int)current(current($result));
}
foreach ($result as $row) {
$membershipUserIds[] = (int)$row['id'];
}
return $membershipUserIds;
}
/**
* Calculate the end date for this membership based on a given time
*
* @param int $start (optional) - UNIX timestamp; if omitted use time()
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
* @return string - formatted timestamp
*/
public function calcEndDate($start = null)
{
if (is_null($start)) {
$start = time();
}
$start = Utils::getFormattedTimestamp($start);
$duration = explode('-', $this->getAttribute('duration'));
$durationCount = $duration[0];
$durationScope = $duration[1];
$end = strtotime($start . ' +' . $durationCount . ' ' . $durationScope);
return Utils::getFormattedTimestamp($end);
}
/**
* Locks editing of this membership for the current session user
*
* @return void
*/
public function lock()
{
Locker::lock(QUI::getPackage('quiqqer/memberships'), $this->getLockKey());
}
/**
* Unlock membership (requires permission!)
*
* @return void
* @throws QUI\Permissions\Exception
*/
public function unlock()
{
Locker::unlockWithPermissions(
QUI::getPackage('quiqqer/memberships'),
$this->getLockKey(),
Handler::PERMISSION_FORCE_EDIT
);
}
/**
* Check if this membership is currently locked
*
* @return bool
*/
public function isLocked()
{
return Locker::isLocked(QUI::getPackage('quiqqer/memberships'), $this->getLockKey());
}
/**
* Get membership lock key
*
* @return string
*/
protected function getLockKey()
{
return 'membership_' . $this->id;
}