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];
if (!empty($data['code'])) {
if (self::existsCode($data['code'])) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.code_already_exists',
[
'code' => $data['code']
]
]);
}
$code = $data['code'];
} else {
$code = CodeGenerator::generate();
}
$couponCode = [
'title' => empty($data['title']) ? '' : $data['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => $code,
'isReusable' => !empty($data['reusable'])
];
if (!empty($data['validUntil'])) {
$ValidUntil = new \DateTime($data['validUntil']);
$couponCode['validUntilDate'] = $ValidUntil->format('Y-m-d H:i:s');
if (!empty($data['userIds'])) {
$couponCode['userIds'] = json_encode($data['userIds']);
}
if (!empty($data['groupIds'])) {
$couponCode['groupIds'] = json_encode($data['groupIds']);
}
QUI::getDataBase()->insert(
self::getTable(),
*
* @param array $searchParams
* @param bool $countOnly (optional) - get result count only [default: false]
*/
public static function search($searchParams, $countOnly = false)
{
$Grid = new Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
if ($countOnly) {
$sql = "SELECT COUNT(*)";
} else {
$sql = "SELECT id";
}
$binds['search'] = [
'value' => '%'.$searchParams['search'].'%',
}
}
// build WHERE query string
if (!empty($where)) {
}
// ORDER
if (!empty($searchParams['sortOn'])
) {
$sortOn = Orthos::clear($searchParams['sortOn']);
if (isset($searchParams['sortBy']) &&
!empty($searchParams['sortBy'])
) {
} else {
$sql .= " ORDER BY id DESC";
}
// LIMIT
if (!empty($gridParams['limit'])
&& !$countOnly
) {
}
}
$Stmt = QUI::getPDO()->prepare($sql);
// bind search values
foreach ($binds as $var => $bind) {
}
try {
$Stmt->execute();
$result = $Stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $Exception) {
QUI\System\Log::addError(
}
if ($countOnly) {
return (int)current(current($result));
}
foreach ($result as $row) {
}
/**
* Check if an invite code already eixsts
*
* @param string $code
* @return bool
*/
public static function existsCode($code)
{
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
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)
if (!is_null($days)) {
$days = (int)$days;
$OldDate = new \DateTime();
'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)
if (!is_null($days)) {
$days = (int)$days;
$OldDate = new \DateTime();
'type' => '<=',
'value' => $OldDate->format('Y-m-d H:i:s')
}
QUI::getDataBase()->delete(
self::getTable(),
$where
);
}
/**
*
* @return string
*/
public static function getTable()
{