Guest User

Untitled

a guest
Feb 17th, 2021
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.19 KB | None | 0 0
  1. <?
  2. use Bitrix\Main;
  3. use Bitrix\Main\Config;
  4. use Bitrix\Main\Localization\Loc;
  5.  
  6. use Bitrix\Sale\Location\Admin\LocationHelper as Helper;
  7. use Bitrix\Sale\Location\Admin\ExternalServiceHelper;
  8. use Bitrix\Sale\Location\Admin\SearchHelper;
  9.  
  10. require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
  11.  
  12. \Bitrix\Main\Loader::includeModule('sale');
  13.  
  14. require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/sale/prolog.php');
  15.  
  16. Loc::loadMessages(__FILE__);
  17.  
  18. $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : false;
  19. $copyId = intval($_REQUEST['copy_id']) ? intval($_REQUEST['copy_id']) : false;
  20.  
  21. // the following parameter will be present visibly only when copying or creating blank with the same parent
  22. $parentId = intval($_REQUEST['parent_id']) ? intval($_REQUEST['parent_id']) : '0';
  23. if(!$parentId && $id)
  24.     $parentId = Helper::getParentId($id);
  25.  
  26. $selfFolderUrl = $adminPage->getSelfFolderUrl();
  27. $listUrl = Helper::getListUrl($parentId);
  28. $listUrl = $adminSidePanelHelper->editUrlToPublicPage($listUrl);
  29.  
  30. if($APPLICATION->GetGroupRight("sale") < "W")
  31.     $APPLICATION->AuthForm(Loc::getMessage("SALE_MODULE_ACCES_DENIED"));
  32.  
  33. $userIsAdmin = $APPLICATION->GetGroupRight("sale") >= "W";
  34. CSaleLocation::locationProCheckEnabled(); // temporal
  35.  
  36. #####################################
  37. #### Data prepare
  38. #####################################
  39.  
  40. CJSCore::Init();
  41. $APPLICATION->AddHeadScript('/bitrix/js/sale/core_ui_widget.js');
  42. $APPLICATION->AddHeadScript('/bitrix/js/sale/core_ui_etc.js');
  43. $APPLICATION->AddHeadScript('/bitrix/js/sale/core_ui_dynamiclist.js');
  44.  
  45. try
  46. {
  47.     $fatalFailure = false;
  48.  
  49.     #####################################
  50.     #### ACTIONS
  51.     #####################################
  52.  
  53.     $actionFailure = false;
  54.  
  55.     $adminSidePanelHelper->decodeUriComponent();
  56.  
  57.     $actionSave = isset($_REQUEST['save']);
  58.     $actionApply = isset($_REQUEST['apply']);
  59.     $actionSaveAndAdd = isset($_REQUEST['save_and_add']);
  60.  
  61.     $formSubmitted = ($actionSave || $actionApply || $actionSaveAndAdd) && check_bitrix_sessid();
  62.  
  63.     $returnUrl = $_REQUEST['return_url'] <> ''? $_REQUEST['return_url'] : '0';
  64.  
  65.     if($userIsAdmin && !empty($_REQUEST['element']) && $formSubmitted) // form submitted, handling it
  66.     {
  67.         $saveAsId = intval($_REQUEST['element']['ID']);
  68.  
  69.         global $DB;
  70.         $redirectUrl = false;
  71.  
  72.         // parent id might be updated, so re-read it from request
  73.         if(intval($_REQUEST['PARENT_ID']))
  74.             $parentId = intval($_REQUEST['PARENT_ID']);
  75.  
  76.         try
  77.         {
  78.             $DB->StartTransaction();
  79.  
  80.             $saveUrl = "";
  81.             $applyUrl = "";
  82.  
  83.             if($saveAsId) // existed, updating
  84.             {
  85.                 $res = Helper::update($saveAsId, $_REQUEST['element']);
  86.  
  87.                 if($res['success']) // on successfull update ...
  88.                 {
  89.                     if($actionSave)
  90.                         $saveUrl = $returnUrl ? $returnUrl : $listUrl; // go to the parent page
  91.  
  92.                     if($actionApply)
  93.                         $applyUrl = $returnUrl ? $returnUrl : Helper::getEditUrl($saveAsId);
  94.                 }
  95.             }
  96.             else // new or copyed item
  97.             {
  98.                 $res = Helper::add($_REQUEST['element']);
  99.                 if($res['success']) // on successfull add ...
  100.                 {
  101.                     if($actionSave)
  102.                         $saveUrl = $returnUrl ? $returnUrl : $listUrl; // go to the parent list page
  103.  
  104.                     if($actionApply)
  105.                         $applyUrl = $returnUrl ? $returnUrl : Helper::getEditUrl($res['id']); // go to the page of just created item
  106.                 }
  107.             }
  108.  
  109.             // no matter we updated or added a new item - we go to blank page on $actionSaveAndAdd
  110.             if($res['success'] && $actionSaveAndAdd)
  111.                 $applyUrl = Helper::getEditUrl(false, array('parent_id' => $parentId)); // go to the blank page with correct parent_id to create
  112.  
  113.             // on failure just show sad message
  114.             if(!$res['success'])
  115.                 throw new Main\SystemException(implode('<br />', $res['errors']));
  116.  
  117.             $DB->Commit();
  118.  
  119.             $baseId = ($saveAsId ? $saveAsId : $res['id']);
  120.             $adminSidePanelHelper->sendSuccessResponse("base", array("element[ID]" => $baseId));
  121.  
  122.             if($saveUrl)
  123.             {
  124.                 $adminSidePanelHelper->localRedirect($saveUrl);
  125.                 LocalRedirect($saveUrl);
  126.             }
  127.             elseif($applyUrl)
  128.             {
  129.                 $applyUrl = $adminSidePanelHelper->setDefaultQueryParams($applyUrl);
  130.                 LocalRedirect($applyUrl);
  131.             }
  132.             else
  133.             {
  134.                 $adminSidePanelHelper->localRedirect($redirectUrl);
  135.                 LocalRedirect($redirectUrl);
  136.             }
  137.         }
  138.         catch(Main\SystemException $e)
  139.         {
  140.             $actionFailure = true;
  141.  
  142.             $code = $e->getCode();
  143.             $message = $e->getMessage().(!empty($code) ? ' ('.$code.')' : '');
  144.  
  145.             $actionFailureMessage = Loc::getMessage('SALE_LOCATION_E_CANNOT_'.($saveAsId ? 'UPDATE' : 'SAVE').'_ITEM').($message <> ''? ': <br /><br />'.$message : '');
  146.  
  147.             $DB->Rollback();
  148.  
  149.             $adminSidePanelHelper->sendJsonErrorResponse($actionFailureMessage);
  150.         }
  151.     }
  152.  
  153.     if(!$returnUrl)
  154.         $returnUrl = Helper::getListUrl($parentId); // default return page for "cancel" action
  155.  
  156.     #####################################
  157.     #### READ FORM DATA
  158.     #####################################
  159.  
  160.     $readAsId = $id ? $id : $copyId;
  161.  
  162.     if($formSubmitted && $actionFailure) // if form were submitted, but form action (add or update) failed
  163.     {
  164.         // load from request
  165.         $formData = $_REQUEST['element'];
  166.  
  167.         if($readAsId)
  168.             $nameToDisplay = Helper::getNameToDisplay($readAsId);
  169.  
  170.         // cleaning up empty external data
  171.         if(is_array($formData['EXTERNAL']) && !empty($formData['EXTERNAL']))
  172.         {
  173.             foreach($formData['EXTERNAL'] as $eId => $external)
  174.             {
  175.                 if($external['XML_ID'] == '')
  176.                     unset($formData['EXTERNAL'][$eId]);
  177.             }
  178.         }
  179.     }
  180.     else
  181.     {
  182.         if($readAsId)
  183.         {
  184.             // load from database
  185.             $formData = Helper::getFormData($readAsId);
  186.  
  187.             if($readAsId)
  188.             {
  189.                 $langU = ToUpper(LANGUAGE_ID);
  190.                 $nameToDisplay = $formData['NAME_'.$langU] <> ''? $formData['NAME_'.$langU] : $formData['CODE'];
  191.             }
  192.         }
  193.         else
  194.         {
  195.             // load blank form, optionally with parent id filled up
  196.             $formData = array();
  197.             if($parentId)
  198.                 $formData['PARENT_ID'] = $parentId;
  199.         }
  200.     }
  201. }
  202. catch(Main\SystemException $e)
  203. {
  204.     $fatalFailure = true;
  205.  
  206.     $code = $e->getCode();
  207.     $fatalFailureMessage = $e->getMessage().(!empty($code) ? ' ('.$code.')' : '');
  208. }
  209.  
  210. #####################################
  211. #### PAGE INTERFACE GENERATION
  212. #####################################
  213.  
  214. if(!$fatalFailure) // no fatals like "module not installed, etc."
  215. {
  216.     $topMenu = new CAdminContextMenu(array(
  217.         array(
  218.             "TEXT" => GetMessage("SALE_LOCATION_E_GO_BACK"),
  219.             "LINK" => $listUrl,
  220.             "ICON" => "btn_list",
  221.         )
  222.     ));
  223.  
  224.     $tabControl = new CAdminForm("tabcntrl_location_node_edit", array(
  225.         array(
  226.             "DIV" => "main",
  227.             "TAB" => Loc::getMessage('SALE_LOCATION_E_MAIN_TAB'),
  228.             "TITLE" =>  Loc::getMessage('SALE_LOCATION_E_MAIN_TAB_TITLE')
  229.         ),
  230.         array(
  231.             "DIV" => "external",
  232.             "TAB" => Loc::getMessage('SALE_LOCATION_E_EXTERNAL_TAB'),
  233.             "TITLE" => Loc::getMessage('SALE_LOCATION_E_EXTERNAL_TAB_TITLE')
  234.         )
  235.     ));
  236.     $tabControl->BeginPrologContent();
  237.     $tabControl->EndPrologContent();
  238.     $tabControl->BeginEpilogContent();
  239.  
  240.     ?>
  241.     <? if($_REQUEST['return_url'] <> ''):?>
  242.     <input type="hidden" name="return_url" value="<?= htmlspecialcharsbx($returnUrl) ?>">
  243. <?endif?>
  244.     <?=bitrix_sessid_post()?>
  245.     <?
  246.     $tabControl->EndEpilogContent();
  247. }
  248.  
  249. $APPLICATION->SetTitle($nameToDisplay <> ''? Loc::getMessage('SALE_LOCATION_E_ITEM_EDIT', array('#ITEM_NAME#' => $nameToDisplay)) : Loc::getMessage('SALE_LOCATION_E_ITEM_NEW'));
  250. ?>
  251.  
  252. <?
  253. require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_after.php");
  254. ?>
  255.  
  256. <?
  257. #####################################
  258. #### Data output
  259. #####################################
  260. ?>
  261.  
  262. <?//temporal code?>
  263. <?if(!CSaleLocation::locationProCheckEnabled())require($DOCUMENT_ROOT."/bitrix/modules/main/include/epilog_admin.php");?>
  264.  
  265. <?SearchHelper::checkIndexesValid();?>
  266.  
  267. <?if($fatalFailure):?>
  268.  
  269.     <?CAdminMessage::ShowMessage(array('MESSAGE' => $fatalFailureMessage, 'type' => 'ERROR'))?>
  270.  
  271. <?else:?>
  272.  
  273.     <?if($actionFailure):?>
  274.         <?CAdminMessage::ShowMessage(array('MESSAGE' => $actionFailureMessage, 'type' => 'ERROR'))?>
  275.     <?endif?>
  276.  
  277.     <?
  278.     $topMenu->Show();
  279.  
  280.     $args = array();
  281.     if(intval($_REQUEST['parent_id']))
  282.         $args['parent_id'] = intval($_REQUEST['parent_id']);
  283.  
  284.     $formActionUrl = Helper::getEditUrl(intval($_REQUEST[Helper::URL_PARAM_ID]) ? intval($_REQUEST[Helper::URL_PARAM_ID]) : false, $args); // generally, it is not safe to leave action empty
  285.     $formActionUrl = $adminSidePanelHelper->setDefaultQueryParams($formActionUrl);
  286.     $tabControl->Begin(array("FORM_ACTION" => $formActionUrl));
  287.     $tabControl->BeginNextFormTab();
  288.     ?>
  289.  
  290.     <?$requiredFld = ' class="adm-detail-required-field"';?>
  291.  
  292.     <?$columns = Helper::getColumns('detail');?>
  293.     <?foreach($columns as $code => $field):?>
  294.  
  295.         <?if($code == 'ID' && !$id) continue; // new node or copied ?>
  296.         <?if(Helper::checkIsNameField($code)) continue; // we`ll output names in a different manner ?>
  297.  
  298.         <?$value = Helper::makeSafeDisplay($formData[$code], $code);?>
  299.  
  300.         <?$tabControl->BeginCustomField($code, $field['title']);?>
  301.  
  302.             <?if(!$geoHeadingShown && ($code == 'LATITUDE' || $code == 'LONGITUDE')):?>
  303.                 <tr class="heading">
  304.                     <td colspan="2"><?=Loc::getMessage('SALE_LOCATION_E_GEODATA')?></td>
  305.                 </tr>
  306.                 <?$geoHeadingShown = true;?>
  307.             <?endif?>
  308.  
  309.             <tr<?=($field['required'] || $code == 'ID' ? $requiredFld : '')?>>
  310.                 <td width="40%"><?=$field['title']?>:</td>
  311.                 <td width="60%">
  312.  
  313.                     <?if($code == 'ID'):?>
  314.  
  315.                         <?=$id?>
  316.                         <input type="hidden" name="element[ID]" value="<?=$id?>" />
  317.  
  318.                     <?elseif($code == 'TYPE_ID'):?>
  319.  
  320.                         <select name="element[TYPE_ID]">
  321.                             <?foreach(Helper::getTypeList() as $tId => $type):?>
  322.                                 <option value="<?=$tId?>"<?=($tId == $value ? ' selected' : '')?>><?=htmlspecialcharsbx($type)?></option>
  323.                             <?endforeach?>
  324.                         </select>
  325.  
  326.                     <?elseif($code == 'PARENT_ID'):?>
  327.  
  328.                         <div style="max-width: 500px">
  329.  
  330.                             <?/*$APPLICATION->IncludeComponent(
  331.                                 "bitrix:map.yandex.view",
  332.                                 "",
  333.                             Array(),
  334.                             false
  335.                             );*/?>
  336.  
  337.                             <?$APPLICATION->IncludeComponent("bitrix:sale.location.selector.".Helper::getWidgetAppearance(), "", array(
  338.                                 "ID" => $value,
  339.                                 "CODE" => "",
  340.                                 "INPUT_NAME" => "element[PARENT_ID]",
  341.                                 "PROVIDE_LINK_BY" => "id",
  342.                                 "SHOW_ADMIN_CONTROLS" => 'Y',
  343.                                 "SELECT_WHEN_SINGLE" => 'N',
  344.                                 "FILTER_BY_SITE" => 'N',
  345.                                 "SHOW_DEFAULT_LOCATIONS" => 'N',
  346.                                 "SEARCH_BY_PRIMARY" => 'Y',
  347.  
  348.                                 "EXCLUDE_SUBTREE" => $nodeId,
  349.                                 ),
  350.                                 false
  351.                             );?>
  352.  
  353.                         </div>
  354.  
  355.                     <?else:?>
  356.  
  357.                         <input type="text" name="element[<?=$code?>]" value="<?=$value?>" <?if($code == 'SORT'):?>size="7"<?endif?> />
  358.  
  359.                     <?endif?>
  360.  
  361.                 </td>
  362.             </tr>
  363.         <?$tabControl->EndCustomField($code, '');?>
  364.  
  365.     <?endforeach?>
  366.  
  367.     <?
  368.     $languages = Helper::getLanguageList();
  369.     $nameMap = Helper::getNameMap();
  370.     ?>
  371.     <?$tabControl->BeginCustomField('NAME', Loc::getMessage('SALE_LOCATION_E_HEADING_NAME_ALL'));?>
  372.     <?foreach($languages as $lang):?>
  373.  
  374.         <tr class="heading">
  375.             <td colspan="2"><?=Loc::getMessage('SALE_LOCATION_E_HEADING_NAME', array('#LANGUAGE_ID#' => htmlspecialcharsbx($lang)))?></td>
  376.         </tr>
  377.  
  378.         <?$lang = ToUpper($lang);?>
  379.  
  380.         <?foreach($nameMap as $code => $field):?>
  381.             <?$value = Helper::makeSafeDisplay($formData[$code.'_'.$lang], $code);?>
  382.             <tr<?=($field['required'] || $code == 'ID' ? $requiredFld : '')?>>
  383.                 <td width="40%"><?=$field['title']?></td>
  384.                 <td width="60%">
  385.                     <input type="text" name="element[<?=$code?>_<?=$lang?>]" value="<?=$value?>" size="20" maxlength="255" />
  386.                 </td>
  387.             </tr>
  388.         <?endforeach?>
  389.  
  390.     <?endforeach?>
  391.     <?$tabControl->EndCustomField('NAME', '');?>
  392.  
  393.     <?$tabControl->BeginNextFormTab();?>
  394.     <?$tabControl->BeginCustomField('EXTERNAL', Loc::getMessage('SALE_LOCATION_E_HEADING_EXTERNAL'));?>
  395.  
  396.         <?$services = Helper::getExternalServicesList();?>
  397.         <tr>
  398.             <td>
  399.  
  400.                 <?if(empty($services)):?>
  401.  
  402.                     <?=Loc::getMessage('SALE_LOCATION_E_NO_SERVICES')?> <a href="<?=ExternalServiceHelper::getListUrl()?>" target="blank_"><?=Loc::getMessage('SALE_LOCATION_E_NO_SERVICES_LIST_PAGE')?></a>
  403.  
  404.                 <?else:?>
  405.  
  406.                     <?$randTag = rand(99, 999);?>
  407.                     <?$externalMap = Helper::getExternalMap();?>
  408.                     <div id="ib_external_values_<?=$randTag?>">
  409.  
  410.                         <table class="internal" style="margin: 0 auto">
  411.                             <tbody class="bx-ui-dynamiclist-container">
  412.                                 <tr class="heading">
  413.                                     <?foreach($externalMap as $code => $field):?>
  414.                                         <td><?=$field['title']?></td>
  415.                                     <?endforeach?>
  416.                                     <td><?=Loc::getMessage('SALE_LOCATION_E_HEADER_EXT_REMOVE')?></td>
  417.                                 </tr>
  418.  
  419.                                 <?if(is_array($formData['EXTERNAL']) && !empty($formData['EXTERNAL'])):?>
  420.  
  421.                                     <?foreach($formData['EXTERNAL'] as $id => $ext):?>
  422.                                         <tr>
  423.                                             <?foreach($externalMap as $code => $field):?>
  424.                                                 <?$value = Helper::makeSafeDisplay($ext[$code], $code);?>
  425.                                                 <td>
  426.                                                     <?if($code == 'SERVICE_ID'):?>
  427.                                                         <select name="element[EXTERNAL][<?=$ext['ID']?>][<?=$code?>]">
  428.                                                             <?foreach($services as $sId => $serv):?>
  429.                                                                 <option value="<?=intval($serv['ID'])?>"<?=($serv['ID'] == $value ? ' selected' : '')?>><?=htmlspecialcharsbx($serv['CODE'])?></option>
  430.                                                             <?endforeach?>
  431.                                                         </select>
  432.                                                     <?elseif($code == 'ID'):?>
  433.                                                         <?if(intval($value)):?>
  434.                                                             <?=$value?>
  435.                                                         <?endif?>
  436.                                                     <?else:?>
  437.                                                         <input type="text" name="element[EXTERNAL][<?=$ext['ID']?>][<?=$code?>]" value="<?=$value?>" size="20" />
  438.                                                     <?endif?>
  439.                                                 </td>
  440.                                             <?endforeach?>
  441.  
  442.                                             <td style="text-align: center">
  443.                                                 <?if($ext['ID']):?>
  444.                                                     <input type="checkbox" name="element[EXTERNAL][<?=$ext['ID']?>][REMOVE]" value="1" />
  445.                                                 <?endif?>
  446.                                             </td>
  447.                                         </tr>
  448.                                     <?endforeach?>
  449.  
  450.                                 <?endif?>
  451.  
  452.                                 <script type="text/html" data-template-id="bx-ui-dynamiclist-row">
  453.                                     <tr>
  454.                                         <td></td>
  455.                                         <td>
  456.                                             <select name="element[EXTERNAL][n{{column_id}}][SERVICE_ID]">
  457.                                                 <?foreach($services as $sId => $serv):?>
  458.                                                     <option value="<?=intval($serv['ID'])?>"><?=htmlspecialcharsbx($serv['CODE'])?></option>
  459.                                                 <?endforeach?>
  460.                                             </select>
  461.                                         </td>
  462.                                         <td>
  463.                                             <input type="text" name="element[EXTERNAL][n{{column_id}}][XML_ID]" value="" size="20" />
  464.                                         </td>
  465.                                         <td></td>
  466.                                     </tr>
  467.                                 </script>
  468.                             </tbody>
  469.                         </table>
  470.  
  471.                         <div style="width: 100%; text-align: center; margin: 10px 0;">
  472.                             <input class="adm-btn-big bx-ui-dynamiclist-addmore" type="button" value="<?=Loc::getMessage('SALE_LOCATION_E_HEADER_EXT_MORE')?>" title="<?=Loc::getMessage('SALE_LOCATION_E_HEADER_EXT_MORE')?>">
  473.                         </div>
  474.  
  475.                     </div>
  476.  
  477.                     <script>
  478.                         new BX.ui.dynamicList({
  479.                             scope: 'ib_external_values_<?=$randTag?>',
  480.                             initiallyAdd: 3
  481.                         });
  482.                     </script>
  483.  
  484.                 <?endif?>
  485.  
  486.             </td>
  487.         </tr>
  488.     <?$tabControl->EndCustomField('EXTERNAL', '');?>
  489.  
  490.     <?
  491.     $tabControl->Buttons(array(
  492.         "disabled" => !$userIsAdmin,
  493.         "btnSaveAndAdd" => true,
  494.         "btnApply" => true,
  495.         "btnCancel" => true,
  496.         "back_url" => $listUrl,
  497.     ));
  498.  
  499.     $tabControl->Show();
  500.     ?>
  501.  
  502. <?endif?>
  503.  
  504. <?require($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/include/epilog_admin.php");?>
  505.  
Advertisement
Add Comment
Please, Sign In to add comment