GetGroupRight('sale'); if ($saleModulePermissions < 'W') $APPLICATION->AuthForm(GetMessage('ACCESS_DENIED')); require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/sale/prolog.php'); require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/sale/include.php'); ClearVars(); ClearVars('f_'); ClearVars('l_'); use Bitrix\Sale\Internals\Input, Bitrix\Sale\Internals\OrderPropsTable, Bitrix\Sale\Internals\PersonTypeTable, Bitrix\Main\Localization\Loc; Loc::loadMessages(__FILE__); $propertyId = $ID; $personTypeId = $PERSON_TYPE_ID; unset($ID, $PERSON_TYPE_ID); // load person types $personTypes = array(); //$result = PersonTypeTable::getList(array( // TODO LIDS // 'select' => array('ID', 'NAME', 'LID', 'DOMAIN' => 'SALE.DOMAIN'), // 'order' => array('LID', 'SORT', 'NAME'), //)); $result = CSalePersonType::GetList(array('SORT' => 'ASC', 'NAME' => 'ASC'), array()); while ($row = $result->Fetch()) $personTypes[$row['ID']] = array( 'ID' => $row['ID'], 'NAME' => htmlspecialcharsex($row['NAME']), 'LID' => htmlspecialcharsex(implode(", ", $row['LIDS'])), ); $errors = array(); $reload = 'reloadForm()'; $variants = array(); // PREPARE PROPERTY, RELATIONS ///////////////////////////////////////////////////////////////////////////////////////// // 1. load property from database if exists $existentProperty = $propertyId ? OrderPropsTable::getById($propertyId)->fetch() : null; // 1. get property from post if ($_SERVER['REQUEST_METHOD'] == 'POST') // get property from post { $_POST = Input\File::getPostWithFiles($_POST, $_FILES); // // MULTIPLE_DEBUG // if ($_POST['TYPE'] != 'ENUM' && $_POST['TYPE'] != 'FILE') // $_POST['MULTIPLE'] = 'N'; if ($_POST['TYPE'] == $_POST['PREVIOUS-TYPE']) { if ($_POST['TYPE'] == 'ENUM') foreach ($_POST['VARIANTS'] as $row) if (($row = array_filter($row, 'strlen')) && $row != array('SORT' => 100)) $variants []= $row; } else { $resetInputSettings = true; } $property = $_POST; $relations = $_POST['RELATIONS']; } else { $relations = array(); // 2. load property from database if ($property = $existentProperty) { $personTypeId = $property['PERSON_TYPE_ID']; $property += $property['SETTINGS']; // load relations $result = CSaleOrderProps::GetOrderPropsRelations(array('PROPERTY_ID' => $propertyId)); while ($row = $result->Fetch()) $relations[$row['ENTITY_TYPE']][] = $row['ENTITY_ID']; } // 3. make new property else { $propertyId = null; $property = array( 'TYPE' => 'STRING', 'PERSON_TYPE_ID' => $personTypeId, ); } } // 4. check requested person type if (! $personType = $personTypes[$personTypeId]) LocalRedirect('sale_order_props.php?lang='.LANG.GetFilterParams('filter_', false)); // SETTINGS //////////////////////////////////////////////////////////////////////////////////////////////////////////// // input settings $inputSettings = Input\Manager::getSettings($property, $reload); if (isset($resetInputSettings)) { unset($property['DEFAULT_VALUE'], $property['SETTINGS']); $property = array_diff_key($property, $inputSettings); if ($propertyId && $existentProperty && $property['TYPE'] == $existentProperty['TYPE']) { $property['MULTIPLE' ] = $existentProperty['MULTIPLE' ]; $property['DEFAULT_VALUE'] = $existentProperty['DEFAULT_VALUE']; $property += $existentProperty['SETTINGS']; } } // load property metadata switch ($property['TYPE']) { case 'ENUM': if (! $variants) { $result = CSaleOrderPropsVariant::GetList(($b='SORT'), ($o='ASC'), Array('ORDER_PROPS_ID' => $propertyId)); while ($row = $result->GetNext()) $variants []= $row; } break; case 'FILE': $property['DEFAULT_VALUE'] = Input\File::loadInfo($property['DEFAULT_VALUE']); break; } // variant settings $variantSettings = array( 'VALUE' => array('TYPE' => 'STRING', 'LABEL' => Loc::getMessage('SALE_VARIANTS_CODE' ), 'SIZE' => '5', 'MAXLENGTH' => 255, 'REQUIRED' => 'Y'), 'NAME' => array('TYPE' => 'STRING', 'LABEL' => Loc::getMessage('SALE_VARIANTS_NAME' ), 'SIZE' => '20', 'MAXLENGTH' => 255, 'REQUIRED' => 'Y'), 'SORT' => array('TYPE' => 'NUMBER', 'LABEL' => Loc::getMessage('SALE_VARIANTS_SORT' ), 'MIN' => 0, 'STEP' => 1, 'VALUE' => 100), 'DESCRIPTION' => array('TYPE' => 'STRING', 'LABEL' => Loc::getMessage('SALE_VARIANTS_DESCR'), 'SIZE' => '30', 'MAXLENGTH' => 255), 'ID' => array('TYPE' => 'NUMBER', 'MIN' => 0, 'STEP' => 1, 'HIDDEN' => 'Y'), ); // common settings $groupOptions = array(); $result = \CSaleOrderPropsGroup::GetList(($b="NAME"), ($o="ASC"), Array('PERSON_TYPE_ID' => $personTypeId)); while ($row = $result->Fetch()) $groupOptions[$row['ID']] = $row['NAME']; $commonSettings = array( 'PERSON_TYPE_ID' => array('TYPE' => 'NUMBER', 'LABEL' => Loc::getMessage('SALE_PERS_TYPE' ), 'MIN' => 0, 'STEP' => 1, 'HIDDEN' => 'Y', 'REQUIRED' => 'Y', 'RLABEL' => "[$personTypeId] {$personType['NAME']} ({$personType['LID']})"), 'PROPS_GROUP_ID' => array('TYPE' => 'ENUM' , 'LABEL' => Loc::getMessage('F_PROPS_GROUP_ID'), 'OPTIONS' => $groupOptions, 'RLABEL' => '  '.Loc::getMessage('SALE_PROPS_GROUP').''), 'NAME' => array('TYPE' => 'STRING', 'LABEL' => Loc::getMessage('F_NAME' ), 'MAXLENGTH' => 255, 'REQUIRED' => 'Y'), 'CODE' => array('TYPE' => 'STRING', 'LABEL' => Loc::getMessage('F_CODE' ), 'MAXLENGTH' => 50), 'ACTIVE' => array('TYPE' => 'Y/N' , 'LABEL' => Loc::getMessage('F_ACTIVE' ), 'VALUE' => 'Y'), 'UTIL' => array('TYPE' => 'Y/N' , 'LABEL' => Loc::getMessage('F_UTIL' )), 'USER_PROPS' => array('TYPE' => 'Y/N' , 'LABEL' => Loc::getMessage('F_USER_PROPS' )), 'IS_FILTERED' => array('TYPE' => 'Y/N' , 'LABEL' => Loc::getMessage('F_IS_FILTERED' ), 'DESCRIPTION' => Loc::getMessage('MULTIPLE_DESCRIPTION')), 'SORT' => array('TYPE' => 'NUMBER', 'LABEL' => Loc::getMessage('F_SORT' ), 'MIN' => 0, 'STEP' => 1, 'VALUE' => 100), 'DESCRIPTION' => array('TYPE' => 'STRING', 'LABEL' => Loc::getMessage('F_DESCRIPTION' ), 'MULTILINE' => 'Y', 'ROWS' => 3, 'COLS' => 40), ); if ($propertyId > 0) { $commonSettings = array_merge( array( 'ID' => array( 'TYPE' => 'NUMBER', 'LABEL' => 'ID', 'MIN' => 0, 'STEP' => 1, 'HIDDEN' => 'Y', 'RLABEL' => &$propertyId ) ), $commonSettings ); } $commonSettings += Input\Manager::getCommonSettings($property, $reload); $commonSettings['MULTIPLE']['DESCRIPTION'] = Loc::getMessage('MULTIPLE_DESCRIPTION'); unset($commonSettings['VALUE']); $commonSettings['DEFAULT_VALUE'] = array( 'REQUIRED' => 'N', 'DESCRIPTION' => null, 'VALUE' => $property['DEFAULT_VALUE'], 'LABEL' => Loc::getMessage('F_DEFAULT_VALUE'), ) + $property; if ($property['TYPE'] == 'ENUM') { $defaultOptions = $property['MULTIPLE'] == 'Y' ? array() : array('' => Loc::getMessage('NO_DEFAULT_VALUE')); foreach ($variants as $row) $defaultOptions[$row['VALUE']] = $row['NAME']; $commonSettings['DEFAULT_VALUE']['OPTIONS'] = &$defaultOptions; } // string settings $stringSettings = array( 'IS_PROFILE_NAME' => array('TYPE' => 'Y/N', 'LABEL' => Loc::getMessage('F_IS_PROFILE_NAME'), 'DESCRIPTION' => Loc::getMessage('F_IS_PROFILE_NAME_DESCR')), 'IS_PAYER' => array('TYPE' => 'Y/N', 'LABEL' => Loc::getMessage('F_IS_PAYER' ), 'DESCRIPTION' => Loc::getMessage('F_IS_PAYER_DESCR' )), 'IS_EMAIL' => array('TYPE' => 'Y/N', 'LABEL' => Loc::getMessage('F_IS_EMAIL' ), 'DESCRIPTION' => Loc::getMessage('F_IS_EMAIL_DESCR' )), 'IS_PHONE' => array('TYPE' => 'Y/N', 'LABEL' => Loc::getMessage('F_IS_PHONE' )), 'IS_ZIP' => array('TYPE' => 'Y/N', 'LABEL' => Loc::getMessage('F_IS_ZIP' ), 'DESCRIPTION' => Loc::getMessage('F_IS_ZIP_DESCR' )), 'IS_ADDRESS' => array('TYPE' => 'Y/N', 'LABEL' => Loc::getMessage('F_IS_ADDRESS' )), ); // location settings $locationOptions = array('' => Loc::getMessage('NULL_ANOTHER_LOCATION')); $result = CSaleOrderProps::GetList(array(), array('PERSON_TYPE_ID' => $personTypeId, 'TYPE' => 'STRING', 'ACTIVE' => 'Y'), false, false, array('ID', 'NAME')); while ($row = $result->Fetch()) $locationOptions[$row['ID']] = $row['NAME']; $locationSettings = array( 'IS_LOCATION' => array('TYPE' => 'Y/N' , 'LABEL' => Loc::getMessage('F_IS_LOCATION' ), 'DESCRIPTION' => Loc::getMessage('F_IS_LOCATION_DESCR' ), 'ONCLICK' => $reload), 'INPUT_FIELD_LOCATION' => array('TYPE' => 'ENUM', 'LABEL' => Loc::getMessage('F_ANOTHER_LOCATION'), 'DESCRIPTION' => Loc::getMessage('F_INPUT_FIELD_DESCR' ), 'OPTIONS' => $locationOptions, 'VALUE' => 0), 'IS_LOCATION4TAX' => array('TYPE' => 'Y/N' , 'LABEL' => Loc::getMessage('F_IS_LOCATION4TAX' ), 'DESCRIPTION' => Loc::getMessage('F_IS_LOCATION4TAX_DESCR')), ); // prepare property settings for view $propertySettings = $commonSettings + $inputSettings; //// MULTIPLE_DEBUG //if ($property['TYPE'] != 'ENUM' && $property['TYPE'] != 'FILE') // unset($propertySettings['MULTIPLE']); //elseif ($property['MULTIPLE'] == 'Y') // unset($propertySettings['IS_FILTERED']); if ($property['MULTIPLE'] == 'Y') { $propertySettings['IS_FILTERED']['DISABLED'] = 'Y'; unset($property['IS_FILTERED']); } if ($property['TYPE'] == 'STRING') { $propertySettings += $stringSettings; } elseif ($property['TYPE'] == 'LOCATION') { $propertySettings += $locationSettings; if ($property['IS_LOCATION'] != 'Y' || $property['MULTIPLE'] == 'Y') // TODO unset($propertySettings['INPUT_FIELD_LOCATION']); } // RELATION SETTINGS /////////////////////////////////////////////////////////////////////////////////////////////////// // payment system options $paymentOptions = array('' => Loc::getMessage('SALE_PROPERTY_SELECT_ALL')); $result = CSalePaySystem::GetList( array("SORT"=>"ASC", "NAME"=>"ASC"), array("ACTIVE" => "Y"), false, false, array("ID", "NAME", "ACTIVE", "SORT", "LID") ); while ($row = $result->Fetch()) $paymentOptions[$row['ID']] = $row['NAME'] . ($row['LID'] ? " ({$row['LID']}) " : ' ') . "[{$row['ID']}]"; // delivery system options $deliveryOptions = array('' => Loc::getMessage('SALE_PROPERTY_SELECT_ALL')); $result = CSaleDelivery::GetList( array("SORT"=>"ASC", "NAME"=>"ASC"), array("ACTIVE" => "Y"), false, false, array("ID", "NAME", "ACTIVE", "SORT") ); while ($row = $result->Fetch()) $deliveryOptions[$row['ID']] = $row['NAME']." [".$row['ID']."]"; $result = CSaleDeliveryHandler::GetList( array("SORT" => "ASC"), array("SITE_ID" => trim($personType['LID'])) ); while ($row = $result->GetNext()) { $dsName = $row['LID'] ? " ({$row['LID']})" : ''; foreach ($row['PROFILES'] as $profileId => $arDeliveryProfile) if ($arDeliveryProfile['ACTIVE'] == 'Y') { $id = $row['SID'].':'.$profileId; $deliveryOptions[$id] = $row['NAME']." ({$arDeliveryProfile["TITLE"]}) [$id] $dsName"; } } $relationsSettings = array( 'P' => array('TYPE' => 'ENUM', 'LABEL' => Loc::getMessage('SALE_PROPERTY_PAYSYSTEM'), 'OPTIONS' => $paymentOptions , 'MULTIPLE' => 'Y', 'SIZE' => '5'), 'D' => array('TYPE' => 'ENUM', 'LABEL' => Loc::getMessage('SALE_PROPERTY_DELIVERY' ), 'OPTIONS' => $deliveryOptions, 'MULTIPLE' => 'Y', 'SIZE' => '5'), ); // VALIDATE AND SAVE POST ////////////////////////////////////////////////////////////////////////////////////////////// if ($_SERVER['REQUEST_METHOD'] == 'POST' && (isset($_POST["apply"]) || isset($_POST["save"])) && bitrix_sessid()) { // validate property foreach ($propertySettings as $name => $input) { if ($error = Input\Manager::getError($input, $property[$name])) { if ($input['MULTIPLE'] && $input['MULTIPLE'] == 'Y') // for DEFAULT_VALUE { $errorString = ''; foreach ($error as $k => $v) $errorString .= ' '.(++$k).': '.implode(', ', $v).';'; $errors []= $input['LABEL'].$errorString; } else { $errors []= $input['LABEL'].': '.implode(', ', $error); } } } // validate variants if ($property['TYPE'] == 'ENUM') { $index = 0; foreach ($variants as $row) { ++ $index; if ($row['DELETE']) { unset($defaultOptions[$row['VALUE']]); } else { $hasError = false; foreach ($variantSettings as $name => $input) if ($error = Input\Manager::getError($input, $row[$name])) { $errors []= Loc::getMessage('INPUT_ENUM')." $index: ".$input['LABEL'].': '.implode(', ', $error); $hasError = true; } if ($hasError) unset($defaultOptions[$row['VALUE']]); } } } // validate relations $hasRelations = false; foreach ($relationsSettings as $name => $input) { if (($value = $relations[$name]) && $value != array('')) { $hasRelations = true; if ($error = Input\Manager::getError($input, $value)) $errors [] = $input['LABEL'].': '.implode(', ', $error); } else { $relations[$name] = array(); } } if ($hasRelations) { if ($property['IS_LOCATION4TAX'] == 'Y') $errors []= Loc::getMessage('ERROR_LOCATION4TAX_RELATION_NOT_ALLOWED'); if ($property['IS_EMAIL'] == 'Y') $errors []= Loc::getMessage('ERROR_EMAIL_RELATION_NOT_ALLOWED'); if ($property['IS_PROFILE_NAME'] == 'Y') $errors []= Loc::getMessage('ERROR_PROFILE_NAME_RELATION_NOT_ALLOWED'); } // insert/update database if (! $errors && ($_POST['save'] || $_POST['apply']) && $saleModulePermissions == 'W' && check_bitrix_sessid()) { // save uploaded files if ($property['TYPE'] == 'FILE') { $savedFiles = array(); $files = Input\File::asMultiple($property['DEFAULT_VALUE']); foreach ($files as $i => $file) { if (Input\File::isDeletedSingle($file)) { unset($files[$i]); } else { if (Input\File::isUploadedSingle($file) && ($fileId = \CFile::SaveFile(array('MODULE_ID' => 'sale') + $file, 'sale/order/properties/default')) && is_numeric($fileId)) { $file = $fileId; $savedFiles []= $fileId; } $files[$i] = Input\File::loadInfoSingle($file); } } $property['DEFAULT_VALUE'] = $files; } // prepare property for database & set defaults $propertyForDB = array(); foreach ($commonSettings + $inputSettings + $stringSettings + $locationSettings as $name => $input) $propertyForDB[$name] = Input\Manager::getValue($input, $property[$name]); $propertyForDB['SETTINGS'] = array_intersect_key($propertyForDB, $inputSettings); $propertyForDB = array_diff_key($propertyForDB, $propertyForDB['SETTINGS']); // 1. update property if ($propertyId) { $update = OrderPropsTable::update($propertyId, array_diff_key($propertyForDB, array('ID'=>1))); if ($update->isSuccess()) { $propertyCode = ($v = $property['CODE']) ? $v : false; $result = CSaleOrderPropsValue::GetList( // TODO modernize ($b = 'ID'), ($o = 'ASC'), array( 'ORDER_PROPS_ID' => $propertyId, '!CODE' => $propertyCode, ) ); while ($row = $result->Fetch()) { CSaleOrderPropsValue::Update($row['ID'], array('CODE' => $propertyCode)); } } else { $errors []= loc::getMessage('ERROR_EDIT_PROP').': '.implode(', ', $update->getErrorMessages()); } } // 2. insert property else { $insert = OrderPropsTable::add($propertyForDB); if ($insert->isSuccess()) $propertyId = $property['ID'] = $insert->getId(); else $errors []= loc::getMessage('ERROR_ADD_PROP').': '.implode(', ', $insert->getErrorMessages()); } // cleanup files if ($errors) { if (isset($savedFiles)) $filesToDelete = $savedFiles; } else { if ($existentProperty && $existentProperty['TYPE'] == 'FILE') { $filesToDelete = Input\File::asMultiple(Input\File::getValue($existentProperty, $existentProperty['DEFAULT_VALUE'])); if (isset($files)) $filesToDelete = array_diff($filesToDelete, Input\File::asMultiple(Input\File::getValue($property, $files))); } } if (isset($filesToDelete)) { foreach ($filesToDelete as $fileId) if (is_numeric($fileId)) \CFile::Delete($fileId); } // $filesToDelete = array(); // // if ($v = $property['DEFAULT_VALUE']) // $filesToDelete = is_array($v) ? $v : array($v); // // if (! $errors) // { // $filesToDelete = ($existentProperty && $existentProperty['TYPE'] == 'FILE' && ($v = $existentProperty['DEFAULT_VALUE'])) // ? array_diff((is_array($v) ? $v : array($v)), $filesToDelete) // : array(); // } // save associated data if (! $errors) { // save property variants if ($property['TYPE'] == 'ENUM') { $index = 0; foreach ($variants as $key => $row) { if ($row['DELETE']) { if ($row['ID']) CSaleOrderPropsVariant::Delete($row['ID']); // TODO modernize unset($variants[$key]); } else { ++ $index; $variantId = $row['ID']; $row = array_intersect_key($row, $variantSettings); if ($variantId) { unset($row['ID']); if (! CSaleOrderPropsVariant::Update($variantId, $row)) $errors []= Loc::getMessage('ERROR_EDIT_VARIANT')." $index"; } else { $row['ORDER_PROPS_ID'] = $propertyId; if ($variantId = CSaleOrderPropsVariant::Add($row)) $variants[$key]['ID'] = $variantId; else $errors []= Loc::getMessage('ERROR_ADD_VARIANT')." $index"; } } } } // cleanup variants elseif ($existentProperty && $existentProperty['TYPE'] == 'ENUM') { CSaleOrderPropsVariant::DeleteAll($propertyId); } // save property relations foreach ($relationsSettings as $name => $input) CSaleOrderProps::UpdateOrderPropsRelations($propertyId, $relations[$name], $name); } if ($_POST['save'] && ! $errors) LocalRedirect("sale_order_props.php?lang=".LANG.GetFilterParams("filter_", false)); if ($_POST['apply'] && ! $errors) LocalRedirect("sale_order_props_edit.php?lang=".LANG."&ID=".$propertyId.GetFilterParams("filter_", false)); } } // RENDER VIEW ///////////////////////////////////////////////////////////////////////////////////////////////////////// $APPLICATION->SetTitle($propertyId ? Loc::getMessage('SALE_EDIT_RECORD', array('#ID#' => $propertyId)) : Loc::getMessage('SALE_NEW_RECORD')); require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_after.php'); $aMenu = array( array( "TEXT" => Loc::getMessage('SOPEN_2FLIST'), "ICON" => "btn_list", "LINK" => "/bitrix/admin/sale_order_props.php?lang=".LANG.GetFilterParams("filter_") ) ); if ($propertyId && $saleModulePermissions >= "W") { $aMenu[] = array("SEPARATOR" => "Y"); $arDDMenu = array(); $arDDMenu[] = array( "TEXT" => "".Loc::getMessage('SOPEN_4NEW_PROMT')."", "ACTION" => false ); foreach($personTypes as $row) $arDDMenu[] = array( 'TEXT' => "[{$row['ID']}] {$row['NAME']} ({$row['LID']})", 'ACTION' => "window.location = 'sale_order_props_edit.php?lang=".LANG."&PERSON_TYPE_ID={$row['ID']}';" ); $aMenu[] = array( "TEXT" => Loc::getMessage('SOPEN_NEW_PROPS'), "ICON" => "btn_new", "MENU" => $arDDMenu ); $aMenu[] = array( "TEXT" => Loc::getMessage('SOPEN_DELETE_PROPS'), "LINK" => "javascript:if(confirm('".Loc::getMessage('SOPEN_DELETE_PROPS_CONFIRM')."')) window.location='/bitrix/admin/sale_order_props.php?action=delete&ID[]=".$propertyId."&lang=".LANG."&".bitrix_sessid_get()."#tb';", "ICON" => "btn_delete", ); } $context = new CAdminContextMenu($aMenu); $context->Show(); if ($errors) { $message = ''; foreach ($errors as $v) $message .= $v.'
'; $m = new CAdminMessage($message); echo $m->Show(); } ?>