Newer
Older
use QUI\Lock\Locker;
use QUI\Memberships\Users\Handler as MembershipUsersHandler;
/**
* Get IDs of all QUIQQER Groups
*
* @return int[]
*/
public function getGroupIds()
{
$groupIds = $this->getAttribute('groupIds');
return explode(",", trim($groupIds, ","));
}
24
25
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
/**
* 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-renewed
*
* @return bool
*/
public function isAutoRenew()
{
return $this->getAttribute('autoRenew') ? true : false;
}
$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);
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Get a user of this membership
*
* @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
)
));
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']);
}
/**
* Remove a membership user from this membership
*
* @param int $userId - User ID
* @throws QUI\Memberships\Exception
*/
public function removeMembershipUser($userId)
{
$MembershipUser = $this->getMembershipUser($userId);
// remove from quiqqer groups
}
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* Checks if this membership has a 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,
'userId' => $userId
)
));
return current(current($result)) > 0;
}
/**
* Search memberships
*
* @param array $searchParams
* @param bool $countOnly (optional) - get count for search result only [default: false]
* @return int[] - membership user IDs
*/
public function searchUsers($searchParams, $countOnly = false)
{
$membershipUserIds = array();
$Grid = new QUI\Utils\Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
$tbl = MembershipUsersHandler::getInstance()->getDataBaseTableName();
if ($countOnly) {
$result = QUI::getDataBase()->fetch(array(
'count' => 1,
'from' => $tbl,
'where' => array(
'membershipId' => $this->id
),
));
return current(current($result));
}
$result = QUI::getDataBase()->fetch(array(
'select' => array(
'id'
),
'from' => $tbl,
'where' => array(
'membershipId' => $this->id
),
'limit' => $gridParams['limit']
));
foreach ($result as $row) {
$membershipUserIds[] = (int)$row['id'];
}
return $membershipUserIds;
}
/**
* Set all membership users to the assigned membership groups
*/
protected function setUsersToGroups()
{
// @todo
}
/**
* Calculate the end date for this membership based on a given time
*
* @param int $start (optional) - UNIX timestamp; of omitted use time()
* @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;
}