Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

38234 Each form leads to many queries for default values #604

Merged
115 changes: 25 additions & 90 deletions plugin/Field/DefaultValue/DefaultValueRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

namespace onOffice\WPlugin\Field\DefaultValue;

use onOffice\WPlugin\Types\Field;
use wpdb;
use const OBJECT;
use function esc_sql;
Expand Down Expand Up @@ -51,104 +50,40 @@ public function __construct(wpdb $pWPDB)

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelSingleselect
* @param array $pFields
* @return array
*/
public function readDefaultValuesSingleselect(int $formId, Field $pField): DefaultValueModelSingleselect
public function readDefaultMultiValuesSingleSelect(int $formId, array $pFields): array
{
$query = $this->createBaseQuery($formId, $pField);
$row = $this->_pWPDB->get_row($query, ARRAY_A);
$pDataModel = new DefaultValueModelSingleselect($formId, $pField);
$pDataModel->setDefaultsId(!empty($row['defaults_id']) ? (int)$row['defaults_id'] : 0);
$pDataModel->setValue(!empty($row['value']) ? $row['value'] : '');
return $pDataModel;
$pFieldNames = array_map(function ($field) {
return $field->getName();
}, $pFields);
$query = $this->createMultiBaseQuery($formId, $pFieldNames);
return $this->_pWPDB->get_results($query, OBJECT);
}

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelMultiselect
*/
public function readDefaultValuesMultiSelect(int $formId, Field $pField): DefaultValueModelMultiselect
{
$query = $this->createBaseQuery($formId, $pField);
$rows = $this->_pWPDB->get_results($query, ARRAY_A);
$values = array_column($rows, 'value');
$pDataModel = new DefaultValueModelMultiselect($formId, $pField);
$pDataModel->setValues($values);
return $pDataModel;
}

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelBool
*/
public function readDefaultValuesBool(int $formId, Field $pField): DefaultValueModelBool
{
$query = $this->createBaseQuery($formId, $pField);
$row = $this->_pWPDB->get_row($query, ARRAY_A);
$pDataModel = new DefaultValueModelBool($formId, $pField);
$pDataModel->setDefaultsId(!empty($row['defaults_id']) ? (int)$row['defaults_id'] : 0);
$pDataModel->setValue(!empty($row['value']) ? (bool)intval($row['value']) : false);

return $pDataModel;
}

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelText
*/
public function readDefaultValuesText(int $formId, Field $pField): DefaultValueModelText
{
$query = $this->createBaseQuery($formId, $pField);
$rows = $this->_pWPDB->get_results($query, OBJECT);
$pDataModel = new DefaultValueModelText($formId, $pField);

foreach ($rows as $pRow) {
$pDataModel->addValueByLocale($pRow->locale, $pRow->value);
}

return $pDataModel;
}

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelNumericRange
*/
public function readDefaultValuesNumericRange(int $formId, Field $pField): DefaultValueModelNumericRange
{
$query = $this->createBaseQuery($formId, $pField);
$rows = $this->_pWPDB->get_results($query, OBJECT);
$pDataModel = new DefaultValueModelNumericRange($formId, $pField);

if (count($rows) === 2) {
$pDataModel->setValueFrom((float)$rows[0]->value);
$pDataModel->setValueTo((float)$rows[1]->value);
}
return $pDataModel;
}

/**
* @param int $formId
* @param Field $pField
* @param array $pFieldNames
* @return string
*/
private function createBaseQuery(int $formId, Field $pField): string
private function createMultiBaseQuery(int $formId, array $pFieldNames): string
{
$names = array_map(function ($v) {
return "'" . esc_sql($v) . "'";
}, $pFieldNames);
$names = implode(',', $names);

$prefix = $this->_pWPDB->prefix;
$query = "SELECT {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id,"
."{$prefix}oo_plugin_fieldconfig_form_defaults_values.locale,\n"
."{$prefix}oo_plugin_fieldconfig_form_defaults_values.value\n"
."FROM {$prefix}oo_plugin_fieldconfig_form_defaults\n"
."INNER JOIN {$prefix}oo_plugin_fieldconfig_form_defaults_values\n"
."ON {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id = "
." {$prefix}oo_plugin_fieldconfig_form_defaults_values.defaults_id\n"
."WHERE {$prefix}oo_plugin_fieldconfig_form_defaults.fieldname = '".esc_sql($pField->getName())."' AND\n"
." {$prefix}oo_plugin_fieldconfig_form_defaults.form_id = ".esc_sql($formId);
return $query;
return "SELECT {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id,"
. "{$prefix}oo_plugin_fieldconfig_form_defaults_values.locale,\n"
. "{$prefix}oo_plugin_fieldconfig_form_defaults_values.value,\n"
. "{$prefix}oo_plugin_fieldconfig_form_defaults.fieldname\n"
. "FROM {$prefix}oo_plugin_fieldconfig_form_defaults\n"
. "INNER JOIN {$prefix}oo_plugin_fieldconfig_form_defaults_values\n"
. "ON {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id = "
. " {$prefix}oo_plugin_fieldconfig_form_defaults_values.defaults_id\n"
. "WHERE {$prefix}oo_plugin_fieldconfig_form_defaults.fieldname IN (" . $names . ") AND\n"
. " {$prefix}oo_plugin_fieldconfig_form_defaults.form_id = " . esc_sql($formId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

use DI\DependencyException;
use DI\NotFoundException;
use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelBool;
use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelMultiselect;
use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelNumericRange;
use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelSingleselect;
use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelText;
use onOffice\WPlugin\Field\DefaultValue\DefaultValueRead;
use onOffice\WPlugin\Types\Field;
use onOffice\WPlugin\Types\FieldTypes;
Expand Down Expand Up @@ -57,112 +62,197 @@ public function __construct(
$this->_pDefaultValueReader = $pDefaultValueReader;
}


/**
* @param int $formId
* @param Field $pField
* @param array $pFields
* @return array
* @throws DependencyException
* @throws NotFoundException
*/
public function getConvertedField(int $formId, Field $pField): array
public function getConvertedMultiFields(int $formId, array $pFields): array
{
$isSingleValue = FieldTypes::isDateOrDateTime($pField->getType()) ||
FieldTypes::isNumericType($pField->getType()) ||
$pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT;
$isMultiSelect = $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT;
$isBoolean = $pField->getType() === FieldTypes::FIELD_TYPE_BOOLEAN;
$isStringType = FieldTypes::isStringType($pField->getType());
$isRegZusatz = FieldTypes::isRegZusatzSearchcritTypes($pField->getType());

if ($pField->getIsRangeField()) {
return $this->convertNumericRange($formId, $pField);
} elseif ($isSingleValue) {
return $this->convertGeneric($formId, $pField);
} elseif ($isMultiSelect) {
return $this->convertMultiSelect($formId, $pField);
} elseif ($isBoolean) {
return $this->convertBoolean($formId, $pField);
} elseif ($isStringType) {
return $this->convertText($formId, $pField);
} elseif ($isRegZusatz) {
$pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT);
return $this->convertMultiSelect($formId, $pField);
$pDataModels = [];
$rows = $this->_pDefaultValueReader->readDefaultMultiValuesSingleSelect($formId, $pFields);

foreach ($pFields as $pField) {
$rowData = array_values(array_filter($rows, function ($row) use ($pField) {
return $pField->getName() == $row->fieldname;
}));

if (count($rowData)) {
switch ($pField) {
case $pField->getIsRangeField():
$pDataModel = $this->convertNumericRange($formId, $pField, $rowData);
$pDataModels[ $pField->getName() ] = $pDataModel[0] ?? '';
$pDataModels[ $pField->getName() . '__von' ] = $pDataModel['min'] ?? '';
$pDataModels[ $pField->getName() . '__bis' ] = $pDataModel['max'] ?? '';
break;
case FieldTypes::isDateOrDateTime($pField->getType()):
case FieldTypes::isNumericType($pField->getType()):
case $pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT:
$pDataModel = $this->convertGeneric($formId, $pField, $rowData);
$pDataModels[ $pField->getName() ] = $pDataModel[0] ?? '';
break;
case $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT;
$pDataModel = $this->convertMultiSelect($formId, $pField, $rowData);
$pDataModels[ $pField->getName() ] = $pDataModel[0] ?? '';
$pDataModels[ $pField->getName() ] = $pDataModel;
break;
case $pField->getType() === FieldTypes::FIELD_TYPE_BOOLEAN;
$pDataModel = $this->convertBoolean($formId, $pField, $rowData);
$pDataModels[ $pField->getName() ] = $pDataModel[0] ?? '';
break;
case FieldTypes::isStringType($pField->getType());
$pDataModel = $this->convertText($formId, $pField, $rowData);
$pDataModels[ $pField->getName() ] = $pDataModel[0] ?? '';
$pDataModels[ $pField->getName() ] = ($pDataModel['native'] ?? '') ?: (array_shift($pDataModel) ?? '');
break;
case FieldTypes::isRegZusatzSearchcritTypes($pField->getType());
$pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT);
$pDataModel = $this->convertMultiSelect($formId, $pField, $rowData);
$pDataModels[ $pField->getName() ] = $pDataModel[0] ?? '';
break;
}
}
}
return [];

return $pDataModels;
}

/**
* @throws DependencyException
* @throws NotFoundException
*/
public function getConvertedMultiFieldsForAdmin(int $formId, array $pFields): array
{
$pDataModels = [];
$rows = $this->_pDefaultValueReader->readDefaultMultiValuesSingleSelect($formId, $pFields);

foreach ($pFields as $pField) {
$rowData = array_values(array_filter($rows, function ($row) use ($pField) {
return $pField->getName() == $row->fieldname;
}));
switch ($pField) {
case $pField->getIsRangeField():
$pDataModel = $this->convertNumericRange($formId, $pField, $rowData);
break;
case FieldTypes::isDateOrDateTime($pField->getType()):
case FieldTypes::isNumericType($pField->getType()):
case $pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT:
$pDataModel = $this->convertGeneric($formId, $pField, $rowData);
break;
case $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT;
$pDataModel = $this->convertMultiSelect($formId, $pField, $rowData);
break;
case $pField->getType() === FieldTypes::FIELD_TYPE_BOOLEAN;
$pDataModel = $this->convertBoolean($formId, $pField, $rowData);
break;
case FieldTypes::isStringType($pField->getType());
$pDataModel = $this->convertText($formId, $pField, $rowData);
break;
case FieldTypes::isRegZusatzSearchcritTypes($pField->getType());
$pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT);
$pDataModel = $this->convertMultiSelect($formId, $pField, $rowData);
break;
}
if (isset($pDataModel)) $pDataModels[ $pField->getName() ] = $pDataModel;
}

return $pDataModels;
}

/**
*
* @param int $formId
* @param Field $pField
* @param array $rows
* @return array
* @throws DependencyException
* @throws NotFoundException
*
*/

private function convertGeneric(int $formId, Field $pField): array
private function convertNumericRange(int $formId, Field $pField, array $rows): array
{
$pModel = $this->_pDefaultValueReader->readDefaultValuesSingleselect($formId, $pField);
$pConverter = $this->_pOutputConverterFactory->createForSingleSelect();
return $pConverter->convertToRow($pModel);
$pDataModel = new DefaultValueModelNumericRange($formId, $pField);

if (count($rows) === 2) {
$pDataModel->setValueFrom((float) $rows[0]->value);
$pDataModel->setValueTo((float) $rows[1]->value);
}

$pConverter = $this->_pOutputConverterFactory->createForNumericRange();
return $pConverter->convertToRow($pDataModel);
}

/**
* @param int $formId
* @param Field $pField
* @param array $rows
* @return array
* @throws DependencyException
* @throws NotFoundException
*/
private function convertMultiSelect(int $formId, Field $pField): array
private function convertGeneric(int $formId, Field $pField, array $rows): array
{
$pModel = $this->_pDefaultValueReader->readDefaultValuesMultiSelect($formId, $pField);
$pConverter = $this->_pOutputConverterFactory->createForMultiSelect();
return $pConverter->convertToRow($pModel);
$pDataModel = new DefaultValueModelSingleselect($formId, $pField);
$pDataModel->setDefaultsId(isset($rows[0]->defaults_id) ? (int) $rows[0]->defaults_id : 0);
$pDataModel->setValue($rows[0]->value ?? '');

$pConverter = $this->_pOutputConverterFactory->createForSingleSelect();
return $pConverter->convertToRow($pDataModel);
}

/**
* @param int $formId
* @param Field $pField
* @param array $rows
* @return array
* @throws DependencyException
* @throws NotFoundException
*/
private function convertText(int $formId, Field $pField): array
private function convertMultiSelect(int $formId, Field $pField, array $rows): array
{
$pModel = $this->_pDefaultValueReader->readDefaultValuesText($formId, $pField);
$pConverter = $this->_pOutputConverterFactory->createForText();
return $pConverter->convertToRow($pModel);
$values = array_column($rows, 'value');
$pDataModel = new DefaultValueModelMultiselect($formId, $pField);
$pDataModel->setValues($values);

$pConverter = $this->_pOutputConverterFactory->createForMultiSelect();
return $pConverter->convertToRow($pDataModel);
}

/**
* @param int $formId
* @param Field $pField
* @param array $rows
* @return array
* @throws DependencyException
* @throws NotFoundException
*/
private function convertNumericRange(int $formId, Field $pField): array
private function convertBoolean(int $formId, Field $pField, array $rows): array
{
$pModel = $this->_pDefaultValueReader->readDefaultValuesNumericRange($formId, $pField);
$pConverter = $this->_pOutputConverterFactory->createForNumericRange();
return $pConverter->convertToRow($pModel);
$pDataModel = new DefaultValueModelBool($formId, $pField);
$pDataModel->setDefaultsId(isset($rows[0]->defaults_id) ? (int)$rows[0]->defaults_id : 0);
$pDataModel->setValue(isset($rows[0]->value) && !empty($rows[0]->value) && (bool)intval($rows[0]->value));

$pConverter = $this->_pOutputConverterFactory->createForBool();
return $pConverter->convertToRow($pDataModel);
}

/**
* @param int $formId
* @param Field $pField
* @param array $rows
* @return array
* @throws DependencyException
* @throws NotFoundException
*/
private function convertBoolean(int $formId, Field $pField): array
private function convertText(int $formId, Field $pField, array $rows): array
{
$pModel = $this->_pDefaultValueReader->readDefaultValuesBool($formId, $pField);
$pConverter = $this->_pOutputConverterFactory->createForBool();
return $pConverter->convertToRow($pModel);
$pDataModel = new DefaultValueModelText($formId, $pField);

foreach ($rows as $pRow) {
$pDataModel->addValueByLocale($pRow->locale, $pRow->value);
}

$pConverter = $this->_pOutputConverterFactory->createForText();
return $pConverter->convertToRow($pDataModel);
}
}
Loading
Loading