getSelfFolderUrl(); $listUrl = $selfFolderUrl."sale_discount_coupons.php?lang=".LANGUAGE_ID; $listUrl = $adminSidePanelHelper->editUrlToPublicPage($listUrl); $subWindow = defined('BX_PUBLIC_MODE') && BX_PUBLIC_MODE == 1; $prefix = ($subWindow ? 'COUPON_' : ''); $saleModulePermissions = $APPLICATION->GetGroupRight('sale'); $readOnly = ($saleModulePermissions < 'W'); if ($saleModulePermissions < 'R') $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED")); Loader::includeModule('sale'); Loc::loadMessages(__FILE__); if ($subWindow) require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/iblock/classes/general/subelement.php'); if (!$subWindow && $ex = $APPLICATION->GetException()) { require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_after.php'); ShowError($ex->GetString()); require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/epilog_admin.php'); die(); } $couponTypes = Internals\DiscountCouponTable::getCouponTypes(true); $request = Main\Context::getCurrent()->getRequest(); $multiCoupons = false; $discountID = 0; if ($subWindow) { $multiCoupons = (string)$request->get('MULTI') == 'Y'; $discountID = (int)$request->get('DISCOUNT_ID'); $discount = Internals\DiscountTable::getList(array( 'select' => array('ID'), 'filter' => array('=ID' => $discountID) ))->fetch(); if (!$discount) { require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_after.php'); ShowError(Loc::getMessage('BX_SALE_DISCOUNT_COUPON_ERR_DISCOUNT_ID_ABSENT')); require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/epilog_admin.php'); die(); } unset($discount); } $returnUrl = ''; if (!$subWindow) { $rawReturnUrl = (string)$request->get('return_url'); if ($rawReturnUrl != '') { $currentUrl = $APPLICATION->GetCurPage(); if (mb_strtolower(mb_substr($rawReturnUrl, mb_strlen($currentUrl))) != mb_strtolower($currentUrl)) $returnUrl = $rawReturnUrl; } unset($rawReturnUrl); } $tabList = array( array( 'ICON' => 'sale', 'DIV' => 'couponEdit01', 'TAB' => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TAB_NAME_COMMON'), 'TITLE' => ( $multiCoupons ? Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TAB_TITLE_MULTI_COMMON') : Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TAB_TITLE_COMMON') ) ) ); $couponFormID = ''; if ($subWindow) { $subFormListUrl = [ 'LINK' => $APPLICATION->GetCurPageParam(), 'POST_PARAMS' => [ 'bxpublic' => 'Y', 'DISCOUNT_ID' => $discountID, 'sessid' => bitrix_sessid() ] ]; $couponFormID = 'saleSubCouponControl'; $control = new CAdminSubForm($couponFormID, $tabList, false, true, $subFormListUrl, false); unset($subFormListUrl); } else { $couponFormID = ($multiCoupons ? 'saleMultiCouponControl' : 'saleCouponControl'); $control = new CAdminForm($couponFormID, $tabList); $control->SetShowSettings(false); } unset($tabList); $couponFormID .= '_form'; $errors = array(); $fields = array(); $copy = false; $couponID = (int)$request->get('ID'); if ($couponID < 0) $couponID = 0; if ($couponID > 0) $copy = ($request->get('action') == 'copy'); if ( check_bitrix_sessid() && !$readOnly && $request->isPost() && (string)$request->getPost('Update') == 'Y' ) { $adminSidePanelHelper->decodeUriComponent($request); $rawData = $request->getPostList(); if ($multiCoupons) { $fields['COUNT'] = 0; $fields['COUPON'] = array( 'DISCOUNT_ID' => $discountID, 'MAX_USE' => 0 ); if (!empty($rawData[$prefix.'ACTIVE_FROM'])) $fields['COUPON']['ACTIVE_FROM'] = Main\Type\DateTime::createFromUserTime($rawData[$prefix.'ACTIVE_FROM']); if (!empty($rawData[$prefix.'ACTIVE_TO'])) $fields['COUPON']['ACTIVE_TO'] = Main\Type\DateTime::createFromUserTime($rawData[$prefix.'ACTIVE_TO']); if (isset($rawData[$prefix.'TYPE'])) $fields['COUPON']['TYPE'] = $rawData[$prefix.'TYPE']; if (isset($fields['COUPON']['TYPE']) && $fields['COUPON']['TYPE'] == Internals\DiscountCouponTable::TYPE_MULTI_ORDER) { if (isset($rawData[$prefix.'MAX_USE'])) $fields['COUPON']['MAX_USE'] = $rawData[$prefix.'MAX_USE']; } if (isset($rawData[$prefix.'COUNT'])) $fields['COUNT'] = (int)$rawData[$prefix.'COUNT']; if ($fields['COUNT'] <= 0) $errors[] = Loc::getMessage('BX_SALE_DISCOUNT_COUPON_ERR_COUPON_COUNT'); $checkResult = Internals\DiscountCouponTable::checkPacket($fields['COUPON'], false); if (!$checkResult->isSuccess(true)) { $errors = $checkResult->getErrorMessages(); } else { $couponsResult = Internals\DiscountCouponTable::addPacket( $fields['COUPON'], $fields['COUNT'] ); if (!$couponsResult->isSuccess()) $errors = $couponsResult->getErrorMessages(); unset($couponsResult); } unset($checkResult); } else { if ($subWindow) $fields['DISCOUNT_ID'] = $discountID; elseif (!empty($rawData['DISCOUNT_ID'])) $fields['DISCOUNT_ID'] = $rawData['DISCOUNT_ID']; if (isset($rawData['COUPON'])) $fields['COUPON'] = $rawData['COUPON']; if (!empty($rawData[$prefix.'ACTIVE'])) $fields['ACTIVE'] = $rawData[$prefix.'ACTIVE']; $fields['ACTIVE_FROM'] = (!empty($rawData[$prefix.'ACTIVE_FROM']) ? Main\Type\DateTime::createFromUserTime($rawData[$prefix.'ACTIVE_FROM']) : null); $fields['ACTIVE_TO'] = (!empty($rawData[$prefix.'ACTIVE_TO']) ? Main\Type\DateTime::createFromUserTime($rawData[$prefix.'ACTIVE_TO']) : null); if (isset($rawData[$prefix.'TYPE'])) $fields['TYPE'] = $rawData[$prefix.'TYPE']; if (isset($fields['TYPE']) && $fields['TYPE'] == Internals\DiscountCouponTable::TYPE_MULTI_ORDER) { if (isset($rawData[$prefix.'MAX_USE'])) $fields['MAX_USE'] = $rawData[$prefix.'MAX_USE']; } if (isset($rawData[$prefix.'USER_ID'])) $fields['USER_ID'] = $rawData[$prefix.'USER_ID']; if (isset($rawData[$prefix.'DESCRIPTION'])) $fields['DESCRIPTION'] = $rawData[$prefix.'DESCRIPTION']; if ($couponID == 0 || $copy) $result = Internals\DiscountCouponTable::add($fields); else $result = Internals\DiscountCouponTable::update($couponID, $fields); if (!$result->isSuccess()) { $errors = $result->getErrorMessages(); } else { if ($couponID == 0 || $copy) $couponID = $result->getId(); } unset($result); } unset($rawData); if (empty($errors)) { if ($subWindow) { ?> isAjaxRequest()) { $adminSidePanelHelper->sendSuccessResponse("base", array("ID" => $couponID)); } if ((string)$request->getPost('apply') != '') { $applyUrl = $selfFolderUrl.'sale_discount_coupon_edit.php?lang='.LANGUAGE_ID.'&ID='.$couponID.'&'.$control->ActiveTabParam(); $applyUrl = $adminSidePanelHelper->setDefaultQueryParams($applyUrl); LocalRedirect($applyUrl); } else { $adminSidePanelHelper->localRedirect($listUrl); LocalRedirect($listUrl); } } } else { $adminSidePanelHelper->sendJsonErrorResponse(implode("; ", $errors)); } } elseif ($subWindow) { if ((string)$request->get('dontsave') != '') { ?> SetTitle( $couponID == 0 ? ( !$multiCoupons ? Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TITLE_ADD') : Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TITLE_MULTI_ADD') ) : ( !$copy ? Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TITLE_UPDATE', array('#ID#' => $couponID)) : Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TITLE_COPY', array('#ID#' => $couponID)) ) ); require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_after.php'); $contextMenuItems = array( array( 'ICON' => 'btn_list', 'TEXT' => Loc::getMessage('BX_SALE_DISCOUNT_COUPONT_CONTEXT_COUPON_LIST'), 'LINK' => $listUrl ) ); if (!$subWindow && !$readOnly && $couponID > 0) { if (!$copy) { $addUrl = $selfFolderUrl."sale_discount_coupon_edit.php?lang=".LANGUAGE_ID; $addUrl = $adminSidePanelHelper->editUrlToPublicPage($addUrl); if (!$adminSidePanelHelper->isPublicFrame()) $addUrl = $adminSidePanelHelper->setDefaultQueryParams($addUrl); $contextMenuItems[] = array( 'ICON' => 'btn_new', 'TEXT' => Loc::getMessage('BX_SALE_DISCOUNT_COUPONT_CONTEXT_NEW'), 'LINK' => $addUrl ); $copyUrl = $selfFolderUrl."sale_discount_coupon_edit.php?lang=".LANGUAGE_ID."&ID=".$discountID."&action=copy"; $copyUrl = $adminSidePanelHelper->editUrlToPublicPage($copyUrl); if (!$adminSidePanelHelper->isPublicFrame()) $copyUrl = $adminSidePanelHelper->setDefaultQueryParams($copyUrl); $contextMenuItems[] = array( 'ICON' => 'btn_copy', 'TEXT' => Loc::getMessage('BX_SALE_DISCOUNT_COUPONT_CONTEXT_COPY'), 'LINK' => $copyUrl ); $deleteUrl = $selfFolderUrl."sale_discount_coupons.php?lang=".LANGUAGE_ID."&ID=".$couponID."&action=delete&".bitrix_sessid_get(); $buttonAction = "LINK"; if ($adminSidePanelHelper->isPublicFrame()) { $deleteUrl = $adminSidePanelHelper->editUrlToPublicPage($deleteUrl); $buttonAction = "ONCLICK"; } $contextMenuItems[] = array( 'ICON' => 'btn_delete', 'TEXT' => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_CONTEXT_DELETE'), $buttonAction => "javascript:if(confirm('".CUtil::JSEscape(Loc::getMessage('BX_SALE_DISCOUNT_COUPON_CONTEXT_DELETE_CONFIRM'))."')) top.window.location.href='".$deleteUrl."';", 'WARNING' => 'Y', ); } } $contextMenu = new CAdminContextMenu($contextMenuItems); $contextMenu->Show(); unset($contextMenu, $contextMenuItems); if (!empty($errors)) { $errorMessage = new CAdminMessage( array( 'DETAILS' => implode('
', $errors), 'TYPE' => 'ERROR', 'MESSAGE' => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_ERR_SAVE'), 'HTML' => true ) ); echo $errorMessage->Show(); unset($errorMessage); } $selectFields = array(); if (!$multiCoupons) { $defaultValues = array( 'DISCOUNT_ID' => '', 'COUPON' => '', 'ACTIVE' => 'Y', 'ACTIVE_FROM' => null, 'ACTIVE_TO' => null, 'TYPE' => Internals\DiscountCouponTable::TYPE_ONE_ORDER, 'MAX_USE' => 0, 'USE_COUNT' => 0, 'USER_ID' => 0, 'DESCRIPTION' => '', 'DATE_APPLY' => null ); $selectFields = array('ID', 'DISCOUNT_NAME' => 'DISCOUNT.NAME'); $selectFields = array_merge($selectFields, array_keys($defaultValues)); } else { $defaultValues = array( 'COUNT' => '', 'COUPON' => array( 'DISCOUNT_ID' => '', 'ACTIVE_FROM' => null, 'ACTIVE_TO' => null, 'TYPE' => Internals\DiscountCouponTable::TYPE_ONE_ORDER, 'MAX_USE' => 0, ) ); } $coupon = array(); if (!$multiCoupons && $couponID > 0) { $coupon = Internals\DiscountCouponTable::getList(array( 'select' => $selectFields, 'filter' => array('=ID' => $couponID) ))->fetch(); if (!$coupon) $couponID = 0; } if ($couponID == 0) $coupon = $defaultValues; if (!empty($coupon)) { if (!$multiCoupons) { $coupon['DISCOUNT_NAME'] = (string)($coupon['DISCOUNT_NAME'] ?? ''); $coupon['DISCOUNT_ID'] = (int)($coupon['DISCOUNT_ID'] ?? 0); $coupon['TYPE'] = (int)($coupon['TYPE'] ?? 0); $coupon['USE_COUNT'] = (int)($coupon['USE_COUNT'] ?? 0); $coupon['MAX_USE'] = (int)($coupon['MAX_USE'] ?? 0); $coupon['USER_ID'] = (int)($coupon['USER_ID'] ?? 0); $coupon['DESCRIPTION'] = (string)($coupon['DESCRIPTION'] ?? ''); } else { $coupon['COUNT'] = (int)($coupon['COUNT'] ?? 0); $coupon['COUPON']['DISCOUNT_ID'] = (int)($coupon['COUPON']['DISCOUNT_ID'] ?? 0); $coupon['COUPON']['TYPE'] = (int)($coupon['COUPON']['TYPE'] ?? 0); $coupon['COUPON']['MAX_USE'] = (int)($coupon['COUPON']['MAX_USE'] ?? 0); } } if (!empty($errors)) $coupon = array_merge($coupon, $fields); $control->BeginPrologContent(); CJSCore::Init(array('date')); $control->EndPrologContent(); $control->BeginEpilogContent(); echo GetFilterHiddens("filter_");?> EndEpilogContent(); $formActionUrl = $selfFolderUrl.'sale_discount_coupon_edit.php?lang='.LANGUAGE_ID; $formActionUrl = $adminSidePanelHelper->setDefaultQueryParams($formActionUrl); $control->Begin(array('FORM_ACTION' => $formActionUrl)); $control->BeginNextFormTab(); if ($multiCoupons) { $control->AddEditField($prefix.'COUNT', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_COUNT'), true, array(), ($coupon['COUNT'] > 0 ? $coupon['COUNT'] : '')); $control->BeginCustomField($prefix.'PERIOD', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_PERIOD'), false); ?> GetCustomLabelHTML(); ?> toString() : ''); $activeTo = ($coupon['COUPON']['ACTIVE_TO'] instanceof Main\Type\DateTime ? $coupon['COUPON']['ACTIVE_TO']->toString() : ''); CTimeZone::Enable(); if ($activeFrom != '' || $activeTo != '') $periodValue = CAdminCalendar::PERIOD_INTERVAL; $calendar = new CAdminCalendar; echo $calendar->CalendarPeriodCustom( $prefix.'ACTIVE_FROM', $prefix.'ACTIVE_TO', $activeFrom, $activeTo, true, 19, true, array( CAdminCalendar::PERIOD_EMPTY => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_PERIOD_EMPTY'), CAdminCalendar::PERIOD_INTERVAL => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_PERIOD_INTERVAL') ), $periodValue ); unset($calendar, $activeTo, $activeFrom, $periodValue); ?> EndCustomField($prefix.'PERIOD'); $control->AddDropDownField( $prefix.'TYPE', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_TYPE'), true, $couponTypes, $coupon['COUPON']['TYPE'], array('id="'.$prefix.'TYPE'.'"', 'size="3"') ); $control->AddEditField( $prefix.'MAX_USE', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_MAX_USE'), false, array('id' => $prefix.'MAX_USE'), ($coupon['COUPON']['MAX_USE'] > 0 ? $coupon['COUPON']['MAX_USE'] : '') ); $control->Buttons(false, ''); $control->Show(); ?>