Newer
Older
<?php
namespace QUI\ERP\Coupons;
use QUI;
use QUI\Utils\Grid;
use QUI\Utils\Security\Orthos;
/**
* Class Handler
*
* Main CouponCode Code handler
*/
class Handler
{
/**
* Permissions
*/
const PERMISSION_VIEW = 'quiqqer.couponcode.view';
const PERMISSION_CREATE = 'quiqqer.couponcode.create';
const PERMISSION_DELETE = 'quiqqer.couponcode.delete';
if (isset(self::$couponCodes[$id])) {
return self::$couponCodes[$id];
{
$result = QUI::getDataBase()->fetch(array(
'select' => array(
'id'
),
'from' => self::getTable(),
'where' => array(
'code' => $code
),
'limit' => 1
));
if (empty($result)) {
{
$Now = new \DateTime();
$inviteCode = array(
'title' => empty($data['title']) ? '' : $data['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => CodeGenerator::generate()
);
if (!empty($data['validUntil'])) {
$ValidUntil = new \DateTime($data['validUntil']);
$inviteCode['validUntilDate'] = $ValidUntil->format('Y-m-d H:i:s');
}
if (!empty($data['email'])) {
try {
QUI::getUsers()->getUserByMail($data['email']);
array(
'email' => $data['email']
)
));
} catch (QUI\Users\Exception $Exception) {
if ($Exception->getCode() !== 404) {
throw $Exception;
}
}
$inviteCode['email'] = trim($data['email']);
}
QUI::getDataBase()->insert(
self::getTable(),
$inviteCode
);
*
* @param array $searchParams
* @param bool $countOnly (optional) - get result count only [default: false]
*/
public static function search($searchParams, $countOnly = false)
{
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
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
$Grid = new Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
$binds = array();
$where = array();
if ($countOnly) {
$sql = "SELECT COUNT(*)";
} else {
$sql = "SELECT id";
}
$sql .= " FROM `" . self::getTable() . "`";
if (!empty($searchParams['search'])) {
$searchColumns = array(
'id',
'code',
'email'
);
$whereOr = array();
foreach ($searchColumns as $searchColumn) {
$whereOr[] = '`' . $searchColumn . '` LIKE :search';
}
if (!empty($whereOr)) {
$where[] = '(' . implode(' OR ', $whereOr) . ')';
$binds['search'] = array(
'value' => '%' . $searchParams['search'] . '%',
'type' => \PDO::PARAM_STR
);
}
}
// build WHERE query string
if (!empty($where)) {
$sql .= " WHERE " . implode(" AND ", $where);
}
// ORDER
if (!empty($searchParams['sortOn'])
) {
$sortOn = Orthos::clear($searchParams['sortOn']);
$order = "ORDER BY " . $sortOn;
if (isset($searchParams['sortBy']) &&
!empty($searchParams['sortBy'])
) {
$order .= " " . Orthos::clear($searchParams['sortBy']);
} else {
$order .= " ASC";
}
$sql .= " " . $order;
} else {
$sql .= " ORDER BY id DESC";
}
// 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']);
}
try {
$Stmt->execute();
$result = $Stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $Exception) {
QUI\System\Log::addError(
self::class . ' :: search() -> ' . $Exception->getMessage()
);
return array();
}
if ($countOnly) {
return (int)current(current($result));
}
foreach ($result as $row) {
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
}
/**
* Check if an invite code already eixsts
*
* @param string $code
* @return bool
*/
public static function existsCode($code)
{
$result = QUI::getDataBase()->fetch(array(
'select' => 'id',
'from' => self::getTable(),
'where' => array(
'code' => $code
),
'limit' => 1
));
return !empty($result);
}
/**
* Get Registration site
*
* @return QUI\Projects\Site|false
*/
public static function getRegistrationSite()
{
$Conf = QUI::getPackage('quiqqer/invitecode')->getConfig();
$regSite = $Conf->get('settings', 'registrationSite');
if (empty($regSite)) {
return false;
}
try {
return QUI\Projects\Site\Utils::getSiteByLink($regSite);
} catch (\Exception $Exception) {
return false;
}
}
/**
*
* @param int $days (optional) - Delete expired Codes that are older than X days [default: delete all]
* @return void
*
* @throws \Exception
*/
public static function deleteExpiredCouponCodes($days = null)
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
{
$Now = new \DateTime();
$where = array(
'validUntilDate' => array(
'type' => '<=',
'value' => $Now->format('Y-m-d H:i:s')
)
);
if (!is_null($days)) {
$days = (int)$days;
$OldDate = new \DateTime();
$OldDate->sub(new \DateInterval('P' . $days . 'D'));
$where['validUntilDate'] = array(
'type' => '<=',
'value' => $OldDate->format('Y-m-d H:i:s')
);
}
QUI::getDataBase()->delete(
self::getTable(),
$where
);
}
/**
*
* @param int $days (optional) - Delete redeemed Codes that are older than X days [default: delete all]
* @return void
*
* @throws \Exception
*/
public static function deleteRedeemedCouponCodes($days = null)
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
{
$where = array(
'useDate' => array(
'type' => 'NOT',
'value' => null
)
);
if (!is_null($days)) {
$days = (int)$days;
$OldDate = new \DateTime();
$OldDate->sub(new \DateInterval('P' . $days . 'D'));
$where['useDate'] = array(
'type' => '<=',
'value' => $OldDate->format('Y-m-d H:i:s')
);
}
QUI::getDataBase()->delete(
self::getTable(),
$where
);
}
/**
*
* @return string
*/
public static function getTable()
{