use Bitrix\Main;
use Bitrix\Main\Config;
use Bitrix\Main\Localization\Loc;
use Bitrix\Sale\Location\Admin\LocationHelper as Helper;
use Bitrix\Sale\Location\Admin\ExternalServiceHelper;
use Bitrix\Sale\Location\Admin\SearchHelper;
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/sale/include.php");
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/sale/prolog.php');
Loc::loadMessages(__FILE__);
if($APPLICATION->GetGroupRight("sale") < "W")
$APPLICATION->AuthForm(Loc::getMessage("SALE_MODULE_ACCES_DENIED"));
$userIsAdmin = $APPLICATION->GetGroupRight("sale") >= "W";
CSaleLocation::locationProCheckEnabled(); // temporal
#####################################
#### Data prepare
#####################################
CJSCore::Init();
$APPLICATION->AddHeadScript('/bitrix/js/sale/core_ui_widget.js');
$APPLICATION->AddHeadScript('/bitrix/js/sale/core_ui_etc.js');
$APPLICATION->AddHeadScript('/bitrix/js/sale/core_ui_dynamiclist.js');
try
{
$fatalFailure = false;
#####################################
#### ACTIONS
#####################################
$actionFailure = false;
$id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : false;
$copyId = intval($_REQUEST['copy_id']) ? intval($_REQUEST['copy_id']) : false;
// the following parameter will be present visibly only when copying or creating blank with the same parent
$parentId = intval($_REQUEST['parent_id']) ? intval($_REQUEST['parent_id']) : '0';
if(!$parentId && $id)
$parentId = Helper::getParentId($id);
$actionSave = isset($_REQUEST['save']);
$actionApply = isset($_REQUEST['apply']);
$actionSaveAndAdd = isset($_REQUEST['save_and_add']);
$formSubmitted = ($actionSave || $actionApply || $actionSaveAndAdd) && check_bitrix_sessid();
$returnUrl = strlen($_REQUEST['return_url']) ? $_REQUEST['return_url'] : '0';
if($userIsAdmin && !empty($_REQUEST['element']) && $formSubmitted) // form submitted, handling it
{
$saveAsId = intval($_REQUEST['element']['ID']);
global $DB;
$redirectUrl = false;
// parent id might be updated, so re-read it from request
if(intval($_REQUEST['PARENT_ID']))
$parentId = intval($_REQUEST['PARENT_ID']);
try
{
$DB->StartTransaction();
if($saveAsId) // existed, updating
{
$res = Helper::update($saveAsId, $_REQUEST['element']);
if($res['success']) // on successfull update ...
{
if($actionSave)
$redirectUrl = $returnUrl ? $returnUrl : Helper::getListUrl($parentId); // go to the parent page
// $actionApply : do nothing
}
}
else // new or copyed item
{
$res = Helper::add($_REQUEST['element']);
if($res['success']) // on successfull add ...
{
if($actionSave)
$redirectUrl = $returnUrl ? $returnUrl : Helper::getListUrl($parentId); // go to the parent list page
if($actionApply)
$redirectUrl = $returnUrl ? $returnUrl : Helper::getEditUrl($res['id']); // go to the page of just created item
}
}
// no matter we updated or added a new item - we go to blank page on $actionSaveAndAdd
if($res['success'] && $actionSaveAndAdd)
$redirectUrl = Helper::getEditUrl(false, array('parent_id' => $parentId)); // go to the blank page with correct parent_id to create
// on failure just show sad message
if(!$res['success'])
throw new Main\SystemException(implode('
', $res['errors']));
$DB->Commit();
if($redirectUrl)
LocalRedirect($redirectUrl);
}
catch(Main\SystemException $e)
{
$actionFailure = true;
$code = $e->getCode();
$message = $e->getMessage().(!empty($code) ? ' ('.$code.')' : '');
$actionFailureMessage = Loc::getMessage('SALE_LOCATION_E_CANNOT_'.($saveAsId ? 'UPDATE' : 'SAVE').'_ITEM').(strlen($message) ? ':
'.$message : '');
$DB->Rollback();
}
}
if(!$returnUrl)
$returnUrl = Helper::getListUrl($parentId); // default return page for "cancel" action
#####################################
#### READ FORM DATA
#####################################
$readAsId = $id ? $id : $copyId;
if($formSubmitted && $actionFailure) // if form were submitted, but form action (add or update) failed
{
// load from request
$formData = $_REQUEST['element'];
if($readAsId)
$nameToDisplay = Helper::getNameToDisplay($readAsId);
// cleaning up empty external data
if(is_array($formData['EXTERNAL']) && !empty($formData['EXTERNAL']))
{
foreach($formData['EXTERNAL'] as $eId => $external)
{
if(!strlen($external['XML_ID']))
unset($formData['EXTERNAL'][$eId]);
}
}
}
else
{
if($readAsId)
{
// load from database
$formData = Helper::getFormData($readAsId);
if($readAsId)
{
$langU = ToUpper(LANGUAGE_ID);
$nameToDisplay = strlen($formData['NAME_'.$langU]) ? $formData['NAME_'.$langU] : $formData['CODE'];
}
}
else
{
// load blank form, optionally with parent id filled up
$formData = array();
if($parentId)
$formData['PARENT_ID'] = $parentId;
}
}
}
catch(Main\SystemException $e)
{
$fatalFailure = true;
$code = $e->getCode();
$fatalFailureMessage = $e->getMessage().(!empty($code) ? ' ('.$code.')' : '');
}
#####################################
#### PAGE INTERFACE GENERATION
#####################################
if(!$fatalFailure) // no fatals like "module not installed, etc."
{
$topMenu = new CAdminContextMenu(array(
array(
"TEXT" => GetMessage("SALE_LOCATION_E_GO_BACK"),
"LINK" => Helper::getListUrl($parentId),
"ICON" => "btn_list",
)
));
$tabControl = new CAdminForm("tabcntrl_location_node_edit", array(
array(
"DIV" => "main",
"TAB" => Loc::getMessage('SALE_LOCATION_E_MAIN_TAB'),
"TITLE" => Loc::getMessage('SALE_LOCATION_E_MAIN_TAB_TITLE')
),
array(
"DIV" => "external",
"TAB" => Loc::getMessage('SALE_LOCATION_E_EXTERNAL_TAB'),
"TITLE" => Loc::getMessage('SALE_LOCATION_E_EXTERNAL_TAB_TITLE')
)
));
$tabControl->BeginPrologContent();
$tabControl->EndPrologContent();
$tabControl->BeginEpilogContent();
?>