diff --git a/css/onoffice-address-detail.css b/css/onoffice-address-detail.css index 2cad9c625..44072f57f 100644 --- a/css/onoffice-address-detail.css +++ b/css/onoffice-address-detail.css @@ -13,20 +13,22 @@ .oo-address-fieldlist { display: flex; flex-direction: column; + flex-wrap: wrap; } -.oo-address-placefieldlist { +.oo-address-field { display: flex; - flex-direction: column; + flex-wrap: wrap; + font-size: 1rem; margin-top: 0.25rem; margin-bottom: 0.25rem; } -.oo-address-placefield { - font-size: 1rem; +.oo-address-field-label { + margin-right: 0.5rem; + font-weight: bold; + word-break: break-word; } -.oo-address-field { - font-size: 1rem; - margin-top: 0.25rem; - margin-bottom: 0.25rem; +.oo-address-field-label { + word-break: break-word; } .oo-address-estatelist { margin-bottom: 4rem; diff --git a/plugin/AddressList.php b/plugin/AddressList.php index 7a19ba3fa..f66f098d6 100644 --- a/plugin/AddressList.php +++ b/plugin/AddressList.php @@ -43,6 +43,8 @@ use onOffice\WPlugin\Utility\__String; use onOffice\WPlugin\ViewFieldModifier\ViewFieldModifierHandler; use function esc_html; +use onOffice\WPlugin\Field\UnknownFieldException; +use onOffice\WPlugin\DataView\DataAddressDetailView; /** * @@ -139,6 +141,10 @@ class AddressList /** @var array */ private $_countEstates = []; + + /** @var FieldsCollection */ + private $_pFieldsCollection = []; + /** * * @param DataViewAddress $pDataViewAddress @@ -185,6 +191,7 @@ public function loadAddressesById(array $addressIds, array $fields) $this->_pDataViewAddress->setFields($fields); $this->addRawRecordsByAPICall(clone $this->_pApiClientAction, $parametersRaw); + $this->buildFieldsCollectionForAddressCustomLabel(); } /** @@ -227,6 +234,7 @@ public function loadAddresses(int $inputPage = 1) $multipage = $numpages > 1; $more = true; $page = $newPage; + $this->buildFieldsCollectionForAddressCustomLabel(); } /** @@ -404,10 +412,27 @@ public function getDataViewAddress(): DataViewAddress * @param bool $raw * @return string */ - public function getFieldLabel($field, bool $raw = false): string + public function getFieldLabel(string $field, bool $raw = false): string { - $label = $this->_pEnvironment->getFieldnames() - ->getFieldLabel($field, onOfficeSDK::MODULE_ADDRESS); + $recordType = onOfficeSDK::MODULE_ADDRESS; + + try { + $label = $this->_pFieldsCollection->getFieldByModuleAndName($recordType, $field)->getLabel(); + } catch (UnknownFieldException $pE) { + $label = $this->_pEnvironment->getFieldnames()->getFieldLabel($field, $recordType); + } + if ($this->_pDataViewAddress instanceof DataAddressDetailView) { + try { + $pLanguage = $this->_pEnvironment->getContainer()->get(Language::class)->getLocale(); + } catch (DependencyException | NotFoundException $e) { + return $raw ? $label : esc_html($label); + } + $dataView = $this->_pDataViewAddress->getCustomLabels(); + if (!empty( $dataView[ $field ][ $pLanguage ])) { + $label = $dataView[ $field ][ $pLanguage ]; + } + } + return $raw ? $label : esc_html($label); } @@ -424,6 +449,20 @@ public function getFieldType($field): string return $fieldInformation['type']; } + /** + * + */ + private function buildFieldsCollectionForAddressCustomLabel() + { + $this->_pFieldsCollection = new FieldsCollection(); + $pFieldBuilderShort = $this->_pEnvironment->getFieldsCollectionBuilderShort(); + $pFieldBuilderShort->addFieldsAddressEstate($this->_pFieldsCollection); + + if ($this->_pDataViewAddress instanceof DataListViewAddress && !empty($this->_pDataViewAddress->getName())) { + $pFieldBuilderShort->addCustomLabelFieldsAddressFrontend($this->_pFieldsCollection, $this->_pDataViewAddress->getName()); + } + } + /** * @return string[] An array of visible fields * @throws Field\UnknownFieldException @@ -448,6 +487,7 @@ public function getVisibleFilterableFields(): array ->getAsRow(); $result[$field]['name'] = $field; $result[$field]['value'] = $value; + $result[$field]['label'] = $this->getFieldLabel($field); } return $result; } diff --git a/plugin/Field/Collection/FieldsCollectionBuilderShort.php b/plugin/Field/Collection/FieldsCollectionBuilderShort.php index 4328930d1..24fd46b15 100644 --- a/plugin/Field/Collection/FieldsCollectionBuilderShort.php +++ b/plugin/Field/Collection/FieldsCollectionBuilderShort.php @@ -42,6 +42,7 @@ use onOffice\WPlugin\Field\FieldModuleCollectionDecoratorCustomLabelEstate; use onOffice\WPlugin\Field\FieldModuleCollectionDecoratorGeoPositionBackend; use onOffice\WPlugin\Field\FieldModuleCollectionDecoratorReadAddress; +use onOffice\WPlugin\Field\FieldModuleCollectionDecoratorCustomLabelAddress; /** @@ -341,4 +342,21 @@ public function addFieldSupervisorForSearchCriteria(FieldsCollection $pFieldsCol $pFieldsCollection->merge($pFieldCollectionSupervisor); return $this; } + + /** + * + * @param FieldsCollection $pFieldsCollection + * @param string $formName + * @return $this + * @throws DependencyException + * @throws NotFoundException + * @throws UnknownFormException + */ + + public function addCustomLabelFieldsAddressFrontend(FieldsCollection $pFieldsCollection, string $formName): self + { + $pFieldsCollectionTmp = new FieldModuleCollectionDecoratorCustomLabelAddress($pFieldsCollection, $formName); + $pFieldsCollection->merge($pFieldsCollectionTmp); + return $this; + } } diff --git a/plugin/Field/FieldModuleCollectionDecoratorCustomLabelAddress.php b/plugin/Field/FieldModuleCollectionDecoratorCustomLabelAddress.php new file mode 100644 index 000000000..6ea29f020 --- /dev/null +++ b/plugin/Field/FieldModuleCollectionDecoratorCustomLabelAddress.php @@ -0,0 +1,133 @@ +. + * + */ + +declare(strict_types=1); + +namespace onOffice\WPlugin\Field; + +use DI\Container; +use DI\ContainerBuilder; +use onOffice\SDK\onOfficeSDK; +use onOffice\WPlugin\Language; +use onOffice\WPlugin\Types\Field; +use onOffice\WPlugin\Record\RecordManager; + +use onOffice\WPlugin\Field\CustomLabel\CustomLabelRead; +use onOffice\WPlugin\Record\RecordManagerReadListViewAddress; + +/** + * + * @url http://www.onoffice.de + * @copyright 2003-2024, onOffice(R) GmbH + * + */ +class FieldModuleCollectionDecoratorCustomLabelAddress + extends FieldModuleCollectionDecoratorAbstract +{ + /** @var Container */ + private $_pContainer; + + /** @var array */ + private $_fieldCustomLabels = []; + + /** + * @param FieldModuleCollection $pFieldModuleCollection + * @param mixed $formName + * @param Container|null $pContainer + * @return void + */ + public function __construct(FieldModuleCollection $pFieldModuleCollection, string $formName, Container $pContainer = null) + { + parent::__construct($pFieldModuleCollection); + $this->_pContainer = $pContainer ?? $this->buildContainer(); + $recordManagerReadAddress = $this->_pContainer->get(RecordManagerReadListViewAddress::class); + + try { + $results = $recordManagerReadAddress->getRowByName($formName); + } catch (\Exception $e) { + return; + } + + if (empty($results['listview_address_id'])) { + return; + } + + $fieldsByFormIds = $recordManagerReadAddress->readFieldconfigByListviewId(intval($results['listview_address_id'])); + + foreach ($fieldsByFormIds as $fieldsByFormId) { + $lang = $this->_pContainer->get(Language::class); + $customLabelRead = $this->_pContainer->get(CustomLabelRead::class); + $query = $customLabelRead->readCustomLabelByFormIdAndFieldName(intval($results['listview_address_id']), + $fieldsByFormId['fieldname'], $lang->getLocale(), + RecordManager::TABLENAME_FIELDCONFIG_ADDRESS_CUSTOMS_LABELS, RecordManager::TABLENAME_FIELDCONFIG_ADDRESS_TRANSLATED_LABELS); + if (empty($query[0]->value)) { + continue; + } + $this->_fieldCustomLabels[onOfficeSDK::MODULE_ADDRESS][$fieldsByFormId['fieldname']] = $query[0]->value; + } + } + + /** + * + * @return Field[] + * + */ + + public function getAllFields(): array + { + $fields = parent::getAllFields(); + $cloneFields = array(); + foreach ($fields as $key => $field) { + $cloneFields[$key] = clone $field; + $module = $cloneFields[$key]->getModule(); + $name = $cloneFields[$key]->getName(); + $label = $this->_fieldCustomLabels[$module][$name] ?? null; + + if ($label !== null) { + $cloneFields[$key]->setLabel($label); + } + } + return $cloneFields; + } + + /** + * @return Container + * @throws \Exception + */ + private function buildContainer(): Container + { + $pContainerBuilder = new ContainerBuilder; + $pContainerBuilder->addDefinitions(ONOFFICE_DI_CONFIG_PATH); + return $pContainerBuilder->build(); + } + + /** + * + * @return array + * + */ + + public function getFieldCustomLabels(): array + { + return $this->_fieldCustomLabels; + } + +} diff --git a/plugin/Gui/AdminPageAddressDetail.php b/plugin/Gui/AdminPageAddressDetail.php index 99d5de92e..d6a3bb5b3 100644 --- a/plugin/Gui/AdminPageAddressDetail.php +++ b/plugin/Gui/AdminPageAddressDetail.php @@ -347,11 +347,11 @@ public function doExtraEnqueues() wp_register_script('onoffice-custom-form-label-js', plugin_dir_url(ONOFFICE_PLUGIN_DIR.'/index.php').'dist/onoffice-custom-form-label.min.js', ['onoffice-multiselect'], '', true); wp_enqueue_script('onoffice-custom-form-label-js'); - $pluginPath = ONOFFICE_PLUGIN_DIR.'/index.php'; - wp_register_script('onoffice-multiselect', plugins_url('dist/onoffice-multiselect.min.js', $pluginPath)); - wp_register_style('onoffice-multiselect', plugins_url('css/onoffice-multiselect.css', $pluginPath)); - wp_enqueue_script('onoffice-multiselect'); - wp_enqueue_style('onoffice-multiselect'); + $pluginPath = ONOFFICE_PLUGIN_DIR.'/index.php'; + wp_register_script('onoffice-multiselect', plugins_url('dist/onoffice-multiselect.min.js', $pluginPath)); + wp_register_style('onoffice-multiselect', plugins_url('css/onoffice-multiselect.css', $pluginPath)); + wp_enqueue_script('onoffice-multiselect'); + wp_enqueue_style('onoffice-multiselect'); wp_register_script('oo-unsaved-changes-message', plugin_dir_url(ONOFFICE_PLUGIN_DIR.'/index.php').'dist/onoffice-unsaved-changes-message.min.js', ['jquery'], '', true); diff --git a/plugin/Gui/AdminPageAddressListSettings.php b/plugin/Gui/AdminPageAddressListSettings.php index 91a19a2fb..43772514b 100644 --- a/plugin/Gui/AdminPageAddressListSettings.php +++ b/plugin/Gui/AdminPageAddressListSettings.php @@ -41,6 +41,15 @@ use function __; use function wp_enqueue_script; use onOffice\WPlugin\DataView\DataAddressDetailView; +use onOffice\WPlugin\Field\CustomLabel\CustomLabelRead; +use onOffice\WPlugin\Language; +use onOffice\WPlugin\Field\CustomLabel\CustomLabelDelete; +use onOffice\WPlugin\Field\CustomLabel\ModelToOutputConverter\CustomLabelRowSaver; +use onOffice\WPlugin\Field\Collection\FieldsCollectionBuilderFromNamesForm; +use onOffice\WPlugin\Field\CustomLabel\Exception\CustomLabelDeleteException; +use DI\DependencyException; +use DI\NotFoundException; +use onOffice\WPlugin\Field\UnknownFieldException; /** * @@ -55,6 +64,12 @@ class AdminPageAddressListSettings /** @var FormModelBuilderDBAddress */ private $_pFormModelBuilderAddress = null; + /** @var FieldsCollection */ + private $_pFieldsCollection = null; + + /** */ + const CUSTOM_LABELS = 'customlabels'; + /** * * @param string $pageSlug @@ -107,6 +122,7 @@ protected function buildForms() $this->addSortableFieldsList(array(onOfficeSDK::MODULE_ADDRESS), $this->_pFormModelBuilderAddress, InputModelBase::HTML_TYPE_COMPLEX_SORTABLE_DETAIL_LIST); $this->addSearchFieldForFieldLists(onOfficeSDK::MODULE_ADDRESS, $this->_pFormModelBuilderAddress); + $this->_pFieldsCollection = $pFieldsCollection; $this->addFormModelName(); $this->addFormModelPictureTypes(); @@ -293,6 +309,8 @@ protected function updateValues(array $row, stdClass $pResult, $recordId = null) $row = [ RecordManager::TABLENAME_FIELDCONFIG_ADDRESS => $this->prepareRelationValues (RecordManager::TABLENAME_FIELDCONFIG_ADDRESS, 'listview_address_id', $row, $recordId), + RecordManager::TABLENAME_FIELDCONFIG_ADDRESS_TRANSLATED_LABELS => $this->prepareRelationValues + (RecordManager::TABLENAME_FIELDCONFIG_ADDRESS_TRANSLATED_LABELS, 'listview_address_id', $row, $recordId), ]; $pRecordManagerInsert->insertAdditionalValues($row); $result = true; @@ -302,6 +320,9 @@ protected function updateValues(array $row, stdClass $pResult, $recordId = null) } } + if ($result) { + $this->saveCustomLabels($recordId, $row, RecordManager::TABLENAME_FIELDCONFIG_ADDRESS_CUSTOMS_LABELS, RecordManager::TABLENAME_FIELDCONFIG_ADDRESS_TRANSLATED_LABELS); + } $pResult->result = $result; $pResult->record_id = $recordId; } @@ -376,6 +397,8 @@ public function getEnqueueData(): array AdminPageSettingsBase::POST_RECORD_ID => $this->getListViewId(), self::VIEW_UNSAVED_CHANGES_MESSAGE => __('Your changes have not been saved yet! Do you want to leave the page without saving?', 'onoffice-for-wp-websites'), self::VIEW_LEAVE_WITHOUT_SAVING_TEXT => __('Leave without saving', 'onoffice-for-wp-websites'), + self::CUSTOM_LABELS => $this->readCustomLabels(), + 'label_custom_label' => __('Custom Label: %s', 'onoffice-for-wp-websites'), ); } @@ -391,6 +414,77 @@ public function doExtraEnqueues() wp_localize_script('oo-sanitize-shortcode-name', 'shortcode', ['name' => 'oopluginlistviewsaddress-name']); wp_enqueue_script('oo-sanitize-shortcode-name'); wp_enqueue_script( 'oo-copy-shortcode'); + wp_register_script('onoffice-custom-form-label-js', + plugin_dir_url(ONOFFICE_PLUGIN_DIR.'/index.php').'dist/onoffice-custom-form-label.min.js', ['onoffice-multiselect'], '', true); + wp_enqueue_script('onoffice-custom-form-label-js'); + $pluginPath = ONOFFICE_PLUGIN_DIR.'/index.php'; + wp_register_script('onoffice-multiselect', plugins_url('dist/onoffice-multiselect.min.js', $pluginPath)); + wp_register_style('onoffice-multiselect', plugins_url('css/onoffice-multiselect.css', $pluginPath)); + wp_enqueue_script('onoffice-multiselect'); + wp_enqueue_style('onoffice-multiselect'); + } + + /** + * @return array + * @throws DependencyException + * @throws NotFoundException + * @throws UnknownFieldException + */ + private function readCustomLabels(): array + { + $result = []; + /** @var CustomLabelRead $pCustomLabelRead*/ + $pCustomLabelRead = $this->getContainer()->get(CustomLabelRead::class); + $pLanguage = $this->getContainer()->get(Language::class); + $currentLocale = $pLanguage->getLocale(); + if (is_null($this->_pFieldsCollection)) { + return []; + } + + foreach (array_chunk($this->_pFieldsCollection->getAllFields(), 100) as $pField) { + $pCustomLabelModel = $pCustomLabelRead->getCustomLabelsFieldsForAdmin + ((int)$this->getListViewId(), $pField, $currentLocale, RecordManager::TABLENAME_FIELDCONFIG_ADDRESS_CUSTOMS_LABELS, RecordManager::TABLENAME_FIELDCONFIG_ADDRESS_TRANSLATED_LABELS); + if (count($pCustomLabelModel)) $result = array_merge($result, $pCustomLabelModel); + } + + return $result; + } + + /** + * @param int $recordId + * @param array $row + * @param string $pCustomsLabelConfigurationField + * @param string $pTranslateLabelConfigurationField + * @throws CustomLabelDeleteException + * @throws DependencyException + * @throws NotFoundException + * @throws RecordManagerInsertException + * @throws UnknownFieldException + */ + + private function saveCustomLabels(int $recordId, array $row ,string $pCustomsLabelConfigurationField, string $pTranslateLabelConfigurationField) + { + $fields = $row[RecordManager::TABLENAME_FIELDCONFIG_ADDRESS] ?? []; + $fieldNamesSelected = array_column($fields, 'fieldname'); + + foreach ($fieldNamesSelected as $key => $name) { + if (!$this->_pFieldsCollection->containsFieldByModule(onOfficeSDK::MODULE_ADDRESS, $name)) { + unset($fieldNamesSelected[$key]); + unset($row['oo_plugin_fieldconfig_address_translated_labels'][$name]); + } + } + /** @var FieldsCollectionBuilderFromNamesForm $pFieldsCollectionBuilder */ + $pFieldsCollectionBuilder = $this->getContainer()->get(FieldsCollectionBuilderFromNamesForm::class); + $pFieldsCollectionCurrent = $pFieldsCollectionBuilder->buildFieldsCollectionFromBaseCollection + ($fieldNamesSelected, $this->_pFieldsCollection); + + /** @var CustomLabelDelete $pCustomLabelDelete */ + $pCustomLabelDelete = $this->getContainer()->get(CustomLabelDelete::class); + $pCustomLabelDelete->deleteByFormIdAndFieldNames($recordId, $fieldNamesSelected, $pCustomsLabelConfigurationField, $pTranslateLabelConfigurationField); + + $pCustomLabelSave = $this->getContainer()->get(CustomLabelRowSaver::class); + $pCustomLabelSave->saveCustomLabels($recordId, + $row['oo_plugin_fieldconfig_address_translated_labels'] ?? [], $pFieldsCollectionCurrent, $pCustomsLabelConfigurationField, $pTranslateLabelConfigurationField); } /** diff --git a/plugin/Gui/AdminPageSettingsBase.php b/plugin/Gui/AdminPageSettingsBase.php index dace6fc58..fd5f8ab21 100644 --- a/plugin/Gui/AdminPageSettingsBase.php +++ b/plugin/Gui/AdminPageSettingsBase.php @@ -257,6 +257,9 @@ public function ajax_action() $row['oo_plugin_fieldconfig_estate_translated_labels'] = (array)($row['oo_plugin_fieldconfig_form_translated_labels']['value'] ?? []) + (array)($values->{'customlabel-lang'}) ?? []; + $row['oo_plugin_fieldconfig_address_translated_labels'] = + (array)($row['oo_plugin_fieldconfig_form_translated_labels']['value'] ?? []) + + (array)($values->{'customlabel-lang'}) ?? []; if ($checkResult) { $this->updateValues($row, $pResultObject, $recordId); @@ -320,6 +323,9 @@ public function save_form() $row['oo_plugin_fieldconfig_estate_translated_labels'] = (array)($row['oo_plugin_fieldconfig_form_translated_labels']['value'] ?? []) + (array)($values->{'customlabel-lang'} ?? [] ); + $row['oo_plugin_fieldconfig_address_translated_labels'] = + (array)($row['oo_plugin_fieldconfig_form_translated_labels']['value'] ?? []) + + (array)($values->{'customlabel-lang'}) ?? []; if ( $checkResult ) { $this->updateValues( $row, $pResultObject, $recordId ); diff --git a/plugin/Installer/DatabaseChanges.php b/plugin/Installer/DatabaseChanges.php index 83389be53..fa3592cca 100644 --- a/plugin/Installer/DatabaseChanges.php +++ b/plugin/Installer/DatabaseChanges.php @@ -41,7 +41,7 @@ class DatabaseChanges implements DatabaseChangesInterface { /** @var int */ - const MAX_VERSION = 49; + const MAX_VERSION = 50; /** @var WPOptionWrapperBase */ private $_pWpOption; @@ -335,6 +335,12 @@ public function install() $dbversion = 49; } + if ($dbversion == 49) { + dbDelta($this->getCreateQueryFieldConfigAddressCustomsLabels()); + dbDelta($this->getCreateQueryFieldConfigAddressTranslatedLabels()); + $dbversion = 50; + } + $this->_pWpOption->updateOption( 'oo_plugin_db_version', $dbversion, true ); } @@ -967,6 +973,8 @@ public function deinstall() $prefix."oo_plugin_fieldconfig_estate_customs_labels", $prefix."oo_plugin_fieldconfig_estate_translated_labels", $prefix."oo_plugin_contacttypes", + $prefix."oo_plugin_fieldconfig_address_customs_labels", + $prefix."oo_plugin_fieldconfig_address_translated_labels", ); foreach ($tables as $table) { @@ -1220,4 +1228,41 @@ public function updateValueGeoFieldsForEsateList() $this->_pWPDB->query($sql); } + + /** + * @return string + */ + private function getCreateQueryFieldConfigAddressCustomsLabels(): string + { + $prefix = $this->getPrefix(); + $charsetCollate = $this->getCharsetCollate(); + $tableName = $prefix . "oo_plugin_fieldconfig_address_customs_labels"; + $sql = "CREATE TABLE $tableName ( + `customs_labels_id` bigint(20) NOT NULL AUTO_INCREMENT, + `form_id` bigint(20) NOT NULL, + `fieldname` tinytext NOT NULL, + PRIMARY KEY (`customs_labels_id`) + ) $charsetCollate;"; + + return $sql; + } + + /** + * @return string + */ + private function getCreateQueryFieldConfigAddressTranslatedLabels(): string + { + $prefix = $this->getPrefix(); + $charsetCollate = $this->getCharsetCollate(); + $tableName = $prefix . "oo_plugin_fieldconfig_address_translated_labels"; + $sql = "CREATE TABLE $tableName ( + `translated_label_id` bigint(20) NOT NULL AUTO_INCREMENT, + `input_id` bigint(20) NOT NULL, + `locale` tinytext NULL DEFAULT NULL, + `value` text, + PRIMARY KEY (`translated_label_id`) + ) $charsetCollate;"; + + return $sql; + } } diff --git a/plugin/Model/FormModelBuilder/FormModelBuilderDBAddress.php b/plugin/Model/FormModelBuilder/FormModelBuilderDBAddress.php index 983e81344..80f1e04c8 100644 --- a/plugin/Model/FormModelBuilder/FormModelBuilderDBAddress.php +++ b/plugin/Model/FormModelBuilder/FormModelBuilderDBAddress.php @@ -36,6 +36,12 @@ use onOffice\WPlugin\Record\RecordManagerReadListViewAddress; use onOffice\WPlugin\Types\FieldsCollection; use function __; +use DI\ContainerBuilder; +use onOffice\WPlugin\Model\InputModelBuilder\InputModelBuilderCustomLabel; +use onOffice\WPlugin\WP\InstalledLanguageReader; +use onOffice\WPlugin\Types\Field; +use DI\DependencyException; +use DI\NotFoundException; /** * @@ -229,10 +235,19 @@ public function createSortableFieldList($module, $htmlType) $pSortableFieldsList = parent::createSortableFieldList($module, $htmlType); $pInputModelIsFilterable = $this->getInputModelIsFilterable(); $pInputModelIsHidden = $this->getInputModelIsHidden(); + + $pFieldsCollectionUsedFields = new FieldsCollection; + foreach ($pSortableFieldsList->getValuesAvailable() as $key => $pField) { + $field = Field::createByRow($key, $pField); + $pFieldsCollectionUsedFields->addField($field); + } + $pInputModelConvertInputTextToSelectField = $this->getInputModelConvertInputTextToSelectField(); $pSortableFieldsList->addReferencedInputModel($pInputModelIsFilterable); $pSortableFieldsList->addReferencedInputModel($pInputModelIsHidden); $pSortableFieldsList->addReferencedInputModel($pInputModelConvertInputTextToSelectField); + $pSortableFieldsList->addReferencedInputModel($this->getInputModelCustomLabel($pFieldsCollectionUsedFields)); + $pSortableFieldsList->addReferencedInputModel($this->getInputModelCustomLabelLanguageSwitch()); return $pSortableFieldsList; } @@ -384,4 +399,40 @@ public function callbackValueInputModelConvertInputTextToSelectForField(InputMod $pInputModel->setValue($value); $pInputModel->setValuesAvailable($key); } + + /** + * @param FieldsCollection $pFieldsCollection + * @return InputModelDB + * @throws DependencyException + * @throws NotFoundException + */ + private function getInputModelCustomLabel(FieldsCollection $pFieldsCollection): InputModelDB + { + $pDIContainerBuilder = new ContainerBuilder(); + $pDIContainerBuilder->addDefinitions(ONOFFICE_DI_CONFIG_PATH); + $pContainer = $pDIContainerBuilder->build(); + $pInputModelBuilder = $pContainer->get(InputModelBuilderCustomLabel::class); + return $pInputModelBuilder->createInputModelCustomLabel($pFieldsCollection, $this->getValue('customlabel', [])); + } + + /** + * @return InputModelDB + */ + public function getInputModelCustomLabelLanguageSwitch(): InputModelDB + { + $pInputModel = new InputModelDB('customlabel_newlang', + __('Add custom label language', 'onoffice-for-wp-websites')); + $pInputModel->setTable('language-custom-label'); + $pInputModel->setField('language'); + + $pLanguageReader = new InstalledLanguageReader; + $languages = ['' => __('Choose Language', 'onoffice-for-wp-websites')] + + $pLanguageReader->readAvailableLanguageNamesUsingNativeName(); + $pInputModel->setValuesAvailable(array_diff_key($languages, [get_locale() => []])); + $pInputModel->setValueCallback(function (InputModelDB $pInputModel) { + $pInputModel->setHtmlType(InputModelBase::HTML_TYPE_SELECT); + $pInputModel->setLabel(__('Add custom label language', 'onoffice-for-wp-websites')); + }); + return $pInputModel; + } } diff --git a/plugin/Record/RecordManager.php b/plugin/Record/RecordManager.php index caad9fb04..d4bfda6b9 100644 --- a/plugin/Record/RecordManager.php +++ b/plugin/Record/RecordManager.php @@ -80,6 +80,12 @@ abstract class RecordManager /** */ const TABLENAME_CONTACT_TYPES = 'oo_plugin_contacttypes'; + /** */ + const TABLENAME_FIELDCONFIG_ADDRESS_CUSTOMS_LABELS = 'oo_plugin_fieldconfig_address_customs_labels'; + + /** */ + const TABLENAME_FIELDCONFIG_ADDRESS_TRANSLATED_LABELS = 'oo_plugin_fieldconfig_address_translated_labels'; + /** * * @deprecated get wpdb via DI diff --git a/templates.dist/address/default_detail.php b/templates.dist/address/default_detail.php index a87023a37..79f143906 100644 --- a/templates.dist/address/default_detail.php +++ b/templates.dist/address/default_detail.php @@ -22,7 +22,6 @@ use onOffice\WPlugin\AddressList; $addressName = array('Anrede', 'Titel', 'Vorname', 'Name'); -$addressPlace = array('Strasse', 'Plz', 'Ort', 'Plz-Ort'); /* @var $pAddressList AddressList */ $currentAddressArr = $pAddressList->getRows(); foreach ($currentAddressArr as $addressId => $escapedValues) { @@ -52,23 +51,16 @@ ?>
$value) { if (in_array($field, $addressName) || empty($value)) { continue; } - if (in_array($field, $addressPlace)) { - $addressPlaceDiv .= '
' - . (is_array($value) ? esc_html(implode(', ', $value)) : esc_html($value)) - . '
'; - continue; - } - echo '
' + echo '
'; + echo '
' . esc_html($pAddressList->getFieldLabel($field)) . '
'; + echo '
' . (is_array($value) ? esc_html(implode(', ', $value)) : esc_html($value)) . '
'; - } - if(!empty($addressPlaceDiv)) { - echo '
'.$addressPlaceDiv.'
'; + echo '
'; } ?>
diff --git a/tests/TestClassAddressList.php b/tests/TestClassAddressList.php index 535d3828b..711e290db 100644 --- a/tests/TestClassAddressList.php +++ b/tests/TestClassAddressList.php @@ -228,7 +228,7 @@ public function prepare() ->getMock(); $pFieldsCollectionNewFields = new FieldsCollection; - $pFieldsCollectionNewFields->addField(new Field('KdNr', onOfficeSDK::MODULE_ADDRESS)); + $pFieldsCollectionNewFields->addField(new Field('KdNr', onOfficeSDK::MODULE_ADDRESS, 'Kundennummer')); $pFieldsCollectionNewFields->addField(new Field('Vorname', onOfficeSDK::MODULE_ADDRESS)); $pFieldsCollectionNewFields->addField(new Field('Name', onOfficeSDK::MODULE_ADDRESS)); @@ -364,7 +364,7 @@ public function testGetVisibleFilterableFields() 'KdNr' => [ 'type' => 'varchar', 'value' => 4, - 'label' => '', + 'label' => 'Kundennummer', 'default' => null, 'length' => null, 'permittedvalues' => Array (), diff --git a/tests/TestClassDatabaseChanges.php b/tests/TestClassDatabaseChanges.php index c1f5f94d6..a3f0d04e0 100644 --- a/tests/TestClassDatabaseChanges.php +++ b/tests/TestClassDatabaseChanges.php @@ -118,7 +118,7 @@ public function testInstall(): array $this->assertGreaterThanOrEqual(self::NUM_NEW_TABLES, count($this->_createQueries)); $dbversion = $this->_pDbChanges->getDbVersion(); - $this->assertEquals(49, $dbversion); + $this->assertEquals(50, $dbversion); return $this->_createQueries; } @@ -271,7 +271,7 @@ public function testDeleteMessageFieldApplicantSearchForm() */ public function testMaxVersion() { - $this->assertEquals(49, DatabaseChanges::MAX_VERSION); + $this->assertEquals(50, DatabaseChanges::MAX_VERSION); } diff --git a/tests/TestClassFieldModuleCollectionDecoratorCustomLabelAddress.php b/tests/TestClassFieldModuleCollectionDecoratorCustomLabelAddress.php new file mode 100644 index 000000000..7d53fa4fc --- /dev/null +++ b/tests/TestClassFieldModuleCollectionDecoratorCustomLabelAddress.php @@ -0,0 +1,196 @@ +. + * + */ + +declare (strict_types=1); + +namespace onOffice\tests; + +use DI\Container; +use DI\ContainerBuilder; +use onOffice\SDK\onOfficeSDK; +use onOffice\WPlugin\Field\CustomLabel\CustomLabelRead; +use onOffice\WPlugin\Field\FieldModuleCollection; +use onOffice\WPlugin\Field\FieldModuleCollectionDecoratorCustomLabelAddress; +use onOffice\WPlugin\Types\Field; +use onOffice\WPlugin\Types\FieldsCollection; +use WP_UnitTestCase; +use wpdb; +use onOffice\WPlugin\Record\RecordManagerReadListViewAddress; + +/** + * + * @url http://www.onoffice.de + * @copyright 2003-2018, onOffice(R) GmbH + * + */ +class TestClassFieldModuleCollectionDecoratorCustomLabelAddress + extends WP_UnitTestCase +{ + /** @var RecordManagerReadListViewAddress */ + private $_pRecordManagerReadListViewAddress = null; + + /** @var CustomLabelRead */ + private $_pCustomLabelRead = null; + + /** @var FieldModuleCollection */ + private $_pFieldModuleCollection = null; + + /** @var Container */ + private $_pContainer = null; + + /** @var wpdb */ + private $_pWPDBMock = null; + + /** + * @before + */ + + public function prepare() + { + $rows = [ + (object)[ + 'customs_labels_id' => 33, + 'locale' => 'de_DE', + 'value' => 'Deutschland', + ] + ]; + $this->_pWPDBMock = $this->getMockBuilder(\wpdb::class) + ->disableOriginalConstructor() + ->setMethods(['get_results']) + ->getMock(); + $pContainerBuilder = new ContainerBuilder; + $pContainerBuilder->addDefinitions(ONOFFICE_DI_CONFIG_PATH); + $this->_pContainer = $pContainerBuilder->build(); + + $this->_pRecordManagerReadListViewAddress = $this->getMockBuilder(RecordManagerReadListViewAddress::class) + ->getMock(); + $this->_pRecordManagerReadListViewAddress->method('readFieldconfigByListviewId')->will($this->returnValueMap([ + [1, $this->getBasicFieldsArray(1)] + ])); + $this->_pRecordManagerReadListViewAddress->method('getRowByName')->will($this->returnValue( + $this->getBaseRow(1) + )); + $this->_pContainer->set(RecordManagerReadListViewAddress::class, $this->_pRecordManagerReadListViewAddress); + $pFieldsCollectionByFormIds = $this->_pContainer->get(RecordManagerReadListViewAddress::class)->readFieldconfigByListviewId(1); + + $this->_pFieldModuleCollection = new FieldsCollection(); + foreach ($pFieldsCollectionByFormIds as $pFieldsCollectionByFormId) { + $this->_pFieldModuleCollection->addField(new Field($pFieldsCollectionByFormId['fieldname'], + onOfficeSDK::MODULE_ADDRESS)); + $this->_pWPDBMock->method('get_results')->will($this->returnValue($rows)); + $this->_pCustomLabelRead = new CustomLabelRead($this->_pWPDBMock); + $this->_pCustomLabelRead->readCustomLabelByFormIdAndFieldName(1, $pFieldsCollectionByFormId['fieldname'], + 'de_DE','oo_plugin_fieldconfig_address_customs_labels','oo_plugin_fieldconfig_address_translated_labels'); + } + $this->_pContainer->set(CustomLabelRead::class, $this->_pCustomLabelRead); + } + + + /** + * + */ + + public function testGetAllFields() + { + $pDecoratorCustomLabel = new FieldModuleCollectionDecoratorCustomLabelAddress($this->_pFieldModuleCollection, + 'testListViewId1', $this->_pContainer); + $fieldCustomLabels = $pDecoratorCustomLabel->getFieldCustomLabels(); + $cloneFields = array(); + + foreach ($pDecoratorCustomLabel->getAllFields() as $key => $field) { + $cloneFields[$key] = clone $field; + $module = $cloneFields[$key]->getModule(); + $name = $cloneFields[$key]->getName(); + $label = $fieldCustomLabels[$module][$name] ?? null; + + if ($label !== null) { + $this->assertEquals($label, $cloneFields[$key]->getLabel()); + } + } + } + + /** + * + * @param int $addressId + * @return array + * + */ + + private function getBasicFieldsArray(int $addressId): array + { + $fields = [ + [ + 'fieldconfig_id' => '1', + 'listview_id' => $addressId, + 'order' => '1', + 'fieldname' => 'Test 1', + 'fieldlabel' => 'Field label 1', + 'hidden' => '0', + 'availableOptions' => '0', + ], + [ + 'fieldconfig_id' => '1', + 'listview_id' => $addressId, + 'order' => '1', + 'fieldname' => 'Test 3', + 'fieldlabel' => 'Field label 3', + 'hidden' => '0', + 'availableOptions' => '0', + ], + [ + 'fieldconfig_id' => '1', + 'listview_id' => $addressId, + 'order' => '1', + 'fieldname' => 'Test 3', + 'fieldlabel' => 'Field label 3', + 'hidden' => '0', + 'availableOptions' => '0', + ], + ]; + + return $fields; + } + + /** + * + * @param int $addressId + * @return array + * + */ + + private function getBaseRow(int $listviewId): array + { + return [ + 'listview_address_id' => $listviewId, + 'name' => 'testListViewId' . $listviewId, + 'filterId' => '0', + 'template' => 'testtemplate.php', + 'expose' => '', + 'radius' => '10', + 'fields' => [ + 'test1', + 'test2' + ], + 'filterable' => [], + 'hidden' => [], + ]; + } +} diff --git a/tests/TestClassFormModelBuilderDBAddress.php b/tests/TestClassFormModelBuilderDBAddress.php index 32a7a8cfe..1b96691a7 100644 --- a/tests/TestClassFormModelBuilderDBAddress.php +++ b/tests/TestClassFormModelBuilderDBAddress.php @@ -142,4 +142,26 @@ public function testGetInputModelConvertInputTextToSelectField() $this->assertEquals(InputModelBase::HTML_TYPE_CHECKBOX, $pInputModelDB->getHtmlType()); $this->assertEquals([$pInstance, 'callbackValueInputModelConvertInputTextToSelectForField'], $pInputModelDB->getValueCallback()); } + + /** + * @covers onOffice\WPlugin\Model\FormModelBuilder\FormModelBuilderDBAddress::getInputModelCustomLabelLanguageSwitch + */ + public function testGetInputModelCustomLabelLanguageSwitch() + { + $pInstance = $this->getMockBuilder(FormModelBuilderDBAddress::class) + ->disableOriginalConstructor() + ->setMethods(['readAvailableLanguageNamesUsingNativeName']) + ->getMock(); + + $inputModel = $pInstance->getInputModelCustomLabelLanguageSwitch(); + $this->assertInstanceOf(InputModelDB::class, $inputModel); + $this->assertEquals('Add custom label language', $inputModel->getLabel()); + $this->assertEquals('language-custom-label', $inputModel->getTable()); + $this->assertEquals('language', $inputModel->getField()); + + $values = $inputModel->getValuesAvailable(); + + $this->assertContains('Choose Language', $values); + $this->assertNotContains(get_locale(), $values); + } }