Skip to content
Code-Schnipsel Gruppen Projekte
create.php 1,87 KiB
Newer Older
  • Learn to ignore specific revisions
  • Patrick Müller's avatar
    Patrick Müller committed
    <?php
    
    /**
     * This file contains package_quiqqer_coupons_ajax_create
     */
    
    use QUI\ERP\Coupons\Handler;
    use QUI\Utils\Security\Orthos;
    
    /**
     * Create new InviteCode(s)
     *
     * @param array $attributes
     * @return bool - success
     */
    QUI::$Ajax->registerFunction(
        'package_quiqqer_coupons_ajax_create',
        function ($attributes) {
            $attributes = Orthos::clearArray(json_decode($attributes, true));
    
            try {
                $amount      = 1;
                $inviteCodes = array();
    
                if (!empty($attributes['amount'])) {
                    $amount = (int)$attributes['amount'];
                    unset($attributes['amount']);
                }
    
                for ($i = 0; $i < $amount; $i++) {
                    $inviteCodes[] = Handler::createCouponCode($attributes);
                }
            } catch (\QUI\ERP\Coupons\CouponCodeException $Exception) {
                QUI::getMessagesHandler()->addError(
                    QUI::getLocale()->get(
                        'quiqqer/invitecode',
                        'message.ajax.create.error',
                        array(
                            'error' => $Exception->getMessage()
                        )
                    )
                );
    
                return false;
    
            } catch (QUI\Permissions\Exception $Exception) {
                throw $Exception;
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
    
                QUI::getMessagesHandler()->addError(
                    QUI::getLocale()->get(
                        'quiqqer/invitecode',
                        'message.ajax.general_error'
                    )
                );
    
                return false;
            }
    
            QUI::getMessagesHandler()->addSuccess(
                QUI::getLocale()->get(
                    'quiqqer/invitecode',
                    'message.ajax.create.success'
                )
            );
    
            return true;
        },
        array('attributes'),
        'Permission::checkAdminUser'
    );