From 8bc23b707c4296caf6a476e4276a02cbef4960a6 Mon Sep 17 00:00:00 2001 From: Sergey Gripinskiy Date: Thu, 9 Jun 2022 14:42:42 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20"=D0=9F=D1=80=D0=B8=D0=B2=D1=8F=D0=B7?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BA=20=D0=BC=D0=B5=D1=81=D1=82=D0=BE=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8E"=20LocationTyp?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Исправлено: - свойство "Привязка к местоположению" LocationType работало со множеством ошибок во множественном режиме: нельзя было добавить новые поля, не поддерживалось поле описания значения. --- CHANGELOG.md | 8 + .../Abstraction/IblockPropertyTypeBase.php | 9 + .../Exception/ModuleNotFoundException.php | 9 + src/main/LocationType.php | 188 ++++++++++++++++-- 4 files changed, 194 insertions(+), 20 deletions(-) create mode 100644 src/main/Exception/ModuleNotFoundException.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 207407e..48b96de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ Change Log ========== +2.0.2 +----- + +### Исправлено: + +- свойство "Привязка к местоположению" `LocationType` работало со множеством ошибок во множественном режиме: нельзя было + добавить новые поля, не поддерживалось поле описания значения. + 2.0.1 ----- diff --git a/src/main/Abstraction/IblockPropertyTypeBase.php b/src/main/Abstraction/IblockPropertyTypeBase.php index 76ab71e..77752d8 100644 --- a/src/main/Abstraction/IblockPropertyTypeBase.php +++ b/src/main/Abstraction/IblockPropertyTypeBase.php @@ -375,4 +375,13 @@ public function getUIFilterProperty(array $property, array $control, array &$fil throw new NotImplementedMethodException('getUIFilterProperty', static::class); } + /** + * @return array|bool|mixed + */ + protected function includeIblockElementAdminLangFile() + { + return IncludeModuleLangFile( + $_SERVER["DOCUMENT_ROOT"] . BX_ROOT . '/modules/iblock/admin/iblock_element_admin.php' + ); + } } diff --git a/src/main/Exception/ModuleNotFoundException.php b/src/main/Exception/ModuleNotFoundException.php new file mode 100644 index 0000000..f99a617 --- /dev/null +++ b/src/main/Exception/ModuleNotFoundException.php @@ -0,0 +1,9 @@ + [$this, 'getAdminListViewHTML'], - 'GetPropertyFieldHtml' => [$this, 'getPropertyFieldHtml'], + 'GetAdminListViewHTML' => [$this, 'getAdminListViewHTML'], + 'GetPropertyFieldHtml' => [$this, 'getPropertyFieldHtml'], + 'GetPropertyFieldHtmlMulty' => [$this, 'getPropertyFieldHtmlMulty'], ]; } /** * @inheritDoc + */ + public function getPropertyFieldHtml(array $property, array $value, array $control) { + try { + $this->includeIblockElementAdminLangFile(); + $descInput = ''; + if (key_exists('WITH_DESCRIPTION', $property) && 'Y' === $property['WITH_DESCRIPTION']) { + $descInput = $this->getDescriptionInput( + $control['DESCRIPTION'], + $value['DESCRIPTION'] + ); + } + + return $this->getValueInputWithAutoComplete($value['VALUE'], $control['VALUE']) . $descInput; + } catch (LoaderException|ModuleNotFoundException $exception) { + return $this->getErrorSpan($exception->getMessage()); + } + } + + /** + * @inheritDoc + * @noinspection PhpUndefinedMethodInspection + */ + public function getPropertyFieldHtmlMulty(array $property, array $valueList, array $control) + { + try { + $this->includeIblockElementAdminLangFile(); + $rowList = []; + // Существующие значения + foreach ($valueList as $valueId => $singleValue) { + $rowList[] = $this->getMultiRow( + $valueId, + $property, + $singleValue, + $control + ); + } + // Новые значения + for ($n = 0; $n < ($property['MULTIPLE_CNT'] ?? 1); $n++) { + $rowList[] = $this->getMultiRow( + 'n' . $n, + $property, + ['VALUE' => '', 'DESCRIPTION' => ''], + $control + ); + } + + /** + * К этой таблице НЕЛЬЗЯ добавлять кнопку "Добавить"/"Ещё", т.к. крайне сложно + * скопировать поле поиска местоположения, не сломав его. + */ + return Element::table( + implode('', $rowList), + ['width' => '100%'] + ); + } catch (LoaderException|ModuleNotFoundException $exception) { + return $this->getErrorSpan($exception->getMessage()); + } + } + + /** + * @inheritDoc + */ + public function getAdminListViewHTML(array $property, array $value, array $control) + { + try { + $this->assertSaleModuleIncluded(); + + return LocationHelper::getLocationStringByCode($property['VALUE']); + } catch (LoaderException|ModuleNotFoundException $exception) { + return $this->getErrorSpan($exception->getMessage()); + } + } + + /** + * @param string $valueId + * @param array $property + * @param array $singleValue + * @param array $control + * * @throws LoaderException + * @throws ModuleNotFoundException + * @return Element + * @noinspection PhpUndefinedMethodInspection */ - public function getPropertyFieldHtml( - array $property, - array $value, - array $control - ) { + private function getMultiRow(string $valueId, array $property, array $singleValue, array $control): Element + { + $descriptionCell = ''; + if (key_exists('WITH_DESCRIPTION', $property) && 'Y' === $property['WITH_DESCRIPTION']) { + $descriptionCell = $this->getDescriptionInput( + $control['VALUE'] . '[' . $valueId . '][DESCRIPTION]', + $singleValue['DESCRIPTION'] + ); + } + + return Element::tr( + Element::td( + $this->getValueInputWithAutoComplete( + $singleValue['VALUE'], + $control['VALUE'] . '[' . $valueId . '][VALUE]' + ) + . $descriptionCell + ) + ); + } + + /** + * @param null|string $valueValue + * @param string $inputName + * + * @throws LoaderException + * @throws ModuleNotFoundException + * @return false|string + */ + private function getValueInputWithAutoComplete(?string $valueValue, string $inputName): string + { global $APPLICATION; - if (!Loader::includeModule('sale')) { - return $value['VALUE']; - } + $this->assertSaleModuleIncluded(); ob_start(); $APPLICATION->IncludeComponent( @@ -63,8 +176,8 @@ public function getPropertyFieldHtml( [ 'COMPONENT_TEMPLATE' => 'search', 'ID' => '', - 'CODE' => htmlspecialcharsbx($value['VALUE']), - 'INPUT_NAME' => htmlspecialcharsbx($control['VALUE']), + 'CODE' => htmlspecialcharsbx($valueValue), + 'INPUT_NAME' => htmlspecialcharsbx($inputName), 'PROVIDE_LINK_BY' => 'code', 'JSCONTROL_GLOBAL_ID' => '', 'JS_CALLBACK' => '', @@ -82,19 +195,54 @@ public function getPropertyFieldHtml( } /** - * @param array $property - * @param array $value - * @param array $control + * @param string $name + * @param null|string $value * + * @return Element + * @noinspection PhpUndefinedMethodInspection + */ + private function getDescriptionInput(string $name, ?string $value): Element + { + return Element::label( + GetMessage('IBLOCK_ELEMENT_EDIT_PROP_DESC_1') + . Element::input( + null, + [ + 'name' => $name, + 'value' => (string)$value, + ] + ) + ); + } + + /** * @throws LoaderException - * @return string + * @throws ModuleNotFoundException + * @return void */ - public function getAdminListViewHTML(array $property, array $value, array $control) + private function assertSaleModuleIncluded(): void { - if (!Loader::includeModule('sale')) { - return $value['VALUE']; + if (!Loader::includeModule(self::MODULE_SALE)) { + throw new ModuleNotFoundException( + sprintf( + 'Модуль %s не установлен.', + self::MODULE_SALE + ) + ); } + } - return LocationHelper::getLocationStringByCode($value['VALUE']); + /** + * @param string $message + * + * @return Element + * @noinspection PhpUndefinedMethodInspection + */ + private function getErrorSpan(string $message): Element + { + return Element::span( + $message, + ['style' => 'color: red; font-weight: bold;'] + ); } }