Skip to content

Commit

Permalink
45380 multiple contact types in form specific options (#770)
Browse files Browse the repository at this point in the history
* 45380 multiple contact types in form specific options

* 45380 update unit test

* 45380 update logic migrate contact types

* 45380 update unit test

* 45380 refactor code

* 45380 refactor code

* 45380 update unit test

* 45380 fix conflict

* 45380 remove input field renderer

* 45380 update unit test

* Small refactoring for unused styling and readability
#4116037

* Remove unused select2 call
#4116037

---------

Co-authored-by: Frederic Alpers <88546396+fredericalpers@users.noreply.github.com>
Co-authored-by: andernath <chibineko@gmail.com>
  • Loading branch information
3 people authored Aug 15, 2024
1 parent 47a6f6b commit b47552a
Show file tree
Hide file tree
Showing 24 changed files with 194 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dist/onoffice-custom-select.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions js/onoffice-custom-select.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const custom_select2 = (typeof custom_select2_translation !== 'undefined') ? custom_select2_translation : {};
jQuery(document).ready(function ($) {
const $adminSelect = $('.oo-poststuff .custom-input-field .oo-custom-select2');
const $multiSelectAdminSorting = $('#viewrecordssorting .oo-custom-select2.oo-custom-select2--multiple');
const $singleSelectAdminSorting = $("#viewrecordssorting .oo-custom-select2.oo-custom-select2--single");

$('.custom-multiple-select, .custom-single-select').select2({
width: '100%'
});

if ($adminSelect.length > 0) {
$adminSelect.select2({
width: '50%'
});
}

if ($multiSelectAdminSorting.length) {
$multiSelectAdminSorting.select2({
placeholder: custom_select2.multipleSelectOptions,
Expand Down
12 changes: 6 additions & 6 deletions plugin/DataFormConfiguration/DataFormConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class DataFormConfiguration
/** @var bool */
private $_showEstateContext = false;

/** @var bool */
private $_contactType = '';
/** @var array */
private $_contactType = [];

/** @var string */
private $_recipient = '';
Expand Down Expand Up @@ -142,8 +142,8 @@ public function addAvailableOptionsField(string $availableOptionsField)
public function setShowEstateContext(bool $showEstateContext)
{ $this->_showEstateContext = $showEstateContext; }

/** @param string $contactTypeField */
public function setContactTypeField(string $contactTypeField)
/** @param array $contactTypeField */
public function setContactTypeField(array $contactTypeField)
{ $this->_contactType = $contactTypeField; }

/** @return array */
Expand Down Expand Up @@ -219,8 +219,8 @@ public function setId(int $id)
public function getShowEstateContext(): bool
{ return $this->_showEstateContext; }

/** @return string */
public function getContactType(): string
/** @return array */
public function getContactType(): array
{ return $this->_contactType; }

/** @return bool */
Expand Down
6 changes: 3 additions & 3 deletions plugin/DataFormConfiguration/DataFormConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private function configureContact(array $row, DataFormConfigurationContact $pCon
$pConfig->setCheckDuplicateOnCreateAddress((bool)$row['checkduplicates']);
$pConfig->setNewsletterCheckbox((bool)$row['newsletter']);
$pConfig->setShowEstateContext((bool)$row['show_estate_context']);
$pConfig->setContactTypeField($row['contact_type'] ?? '');
$pConfig->setContactTypeField($row['contact_type'] ?? []);
}


Expand Down Expand Up @@ -336,7 +336,7 @@ private function configureOwner(array $row, DataFormConfigurationOwner $pConfig)
$pConfig->setPages($row['pages']);
$pConfig->setCreateOwner((bool)$row['createaddress']);
$pConfig->setCheckDuplicateOnCreateAddress((bool)$row['checkduplicates']);
$pConfig->setContactTypeField($row['contact_type'] ?? '');
$pConfig->setContactTypeField($row['contact_type'] ?? []);
}


Expand All @@ -354,7 +354,7 @@ private function configureInterest(array $row, DataFormConfigurationInterest $pC
$pConfig->setSubject($row['subject']);
$pConfig->setCreateInterest((bool)$row['createaddress']);
$pConfig->setCheckDuplicateOnCreateAddress($row['checkduplicates']);
$pConfig->setContactTypeField($row['contact_type'] ?? '');
$pConfig->setContactTypeField($row['contact_type'] ?? []);
}


Expand Down
4 changes: 2 additions & 2 deletions plugin/Form/FormAddressCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(
/**
* @param FormData $pFormData
* @param bool $mergeExisting
* @param string $contactType
* @param array $contactType
* @param int|null $estateId
* @return int the new (or updated) address ID
* @throws ApiClientException
Expand All @@ -73,7 +73,7 @@ public function __construct(
* @throws NotFoundException
*/
public function createOrCompleteAddress(
FormData $pFormData, bool $mergeExisting = false, string $contactType = '', int $estateId = null): int
FormData $pFormData, bool $mergeExisting = false, array $contactType = [], int $estateId = null): int
{
$requestParams = $this->getAddressDataForApiCall($pFormData);
$requestParams['checkDuplicate'] = $mergeExisting;
Expand Down
12 changes: 12 additions & 0 deletions plugin/Gui/AdminPageFormSettingsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ protected function updateValues(array $row, stdClass $pResult, $recordId = null)
$result = $result && $pRecordManagerUpdateForm->updateFieldConfigByRow
($row[RecordManager::TABLENAME_FIELDCONFIG_FORMS]);
}

if (array_key_exists(RecordManager::TABLENAME_CONTACT_TYPES, $row)) {
$result = $result && $pRecordManagerUpdateForm->updateContactTypeByRow($row[RecordManager::TABLENAME_CONTACT_TYPES]);
}
} else {
$action = RecordManagerFactory::ACTION_INSERT;
// insert
Expand All @@ -225,6 +229,8 @@ protected function updateValues(array $row, stdClass $pResult, $recordId = null)
$rowFieldConfig = $this->prepareRelationValues
(RecordManager::TABLENAME_FIELDCONFIG_FORMS, 'form_id', $row, $recordId);
$row[RecordManager::TABLENAME_FIELDCONFIG_FORMS] = $rowFieldConfig;
$row[RecordManager::TABLENAME_CONTACT_TYPES] =
$this->prepareRelationValues(RecordManager::TABLENAME_CONTACT_TYPES, 'form_id', $row, $recordId);
$pRecordManagerInsertForm->insertAdditionalValues($row);
$result = true;
} catch (RecordManagerInsertException $pException) {
Expand Down Expand Up @@ -700,6 +706,12 @@ public function doExtraEnqueues()
wp_localize_script('oo-sanitize-shortcode-name', 'shortcode', ['name' => 'oopluginforms-name']);
wp_enqueue_script('oo-sanitize-shortcode-name');
wp_enqueue_script('oo-copy-shortcode');

if ($this->getType() !== Form::TYPE_APPLICANT_SEARCH) {
wp_enqueue_script('select2', plugin_dir_url( ONOFFICE_PLUGIN_DIR . '/index.php' ) . 'vendor/select2/select2/dist/js/select2.min.js');
wp_enqueue_style('select2', plugin_dir_url( ONOFFICE_PLUGIN_DIR . '/index.php' ) . 'vendor/select2/select2/dist/css/select2.min.css');
wp_enqueue_script('onoffice-custom-select', plugins_url('/dist/onoffice-custom-select.min.js', $pluginPath));
}
}


Expand Down
48 changes: 47 additions & 1 deletion plugin/Installer/DatabaseChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
class DatabaseChanges implements DatabaseChangesInterface
{
/** @var int */
const MAX_VERSION = 46;
const MAX_VERSION = 47;

/** @var WPOptionWrapperBase */
private $_pWpOption;
Expand Down Expand Up @@ -319,6 +319,12 @@ public function install()
$dbversion = 46;
}

if ($dbversion == 46) {
dbDelta($this->getCreateQueryContactTypes());
$this->migrateContactTypes();
$dbversion = 47;
}

$this->_pWpOption->updateOption( 'oo_plugin_db_version', $dbversion, true );
}

Expand Down Expand Up @@ -607,6 +613,45 @@ private function getCreateQuerySortByUserValues()
return $sql;
}

/**
* @return string
*/
private function getCreateQueryContactTypes()
{
$prefix = $this->getPrefix();
$charsetCollate = $this->getCharsetCollate();
$tableName = $prefix."oo_plugin_contacttypes";
$sql = "CREATE TABLE $tableName (
`contacttype_id` bigint(20) NOT NULL AUTO_INCREMENT,
`form_id` int(11) NOT NULL,
`contact_type` varchar(100) NOT NULL,
PRIMARY KEY (`contacttype_id`)
) $charsetCollate;";

return $sql;
}

/**
* @return void
*/
private function migrateContactTypes()
{
$prefix = $this->getPrefix();
$tableForm = $prefix."oo_plugin_forms";
$tableContactTypes = $prefix."oo_plugin_contacttypes";
$contactTypes = $this->_pWPDB->get_results("SELECT form_id, contact_type
FROM $tableForm
WHERE contact_type IS NOT NULL
AND contact_type != '' ", ARRAY_A);

if (!empty($contactTypes) && is_array($contactTypes)) {
foreach ($contactTypes as $contactType) {
$formId = esc_sql((int) $contactType['form_id']);
$value = esc_sql($contactType['contact_type']);
$this->_pWPDB->insert($tableContactTypes, ['form_id' => $formId, 'contact_type' => $value]);
}
}
}

/**
*
Expand Down Expand Up @@ -907,6 +952,7 @@ public function deinstall()
$prefix."oo_plugin_fieldconfig_form_translated_labels",
$prefix."oo_plugin_fieldconfig_estate_customs_labels",
$prefix."oo_plugin_fieldconfig_estate_translated_labels",
$prefix."oo_plugin_contacttypes",
);

foreach ($tables as $table) {
Expand Down
8 changes: 6 additions & 2 deletions plugin/Model/FormModelBuilder/FormModelBuilderDBForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,14 @@ public function createInputModelContactType()
$pInputModelFormContactType = $this->getInputModelDBFactory()->create
(InputModelDBFactoryConfigForm::INPUT_FORM_CONTACT_TYPE, $labelContactType);
$field = $pInputModelFormContactType->getField();
$pInputModelFormContactType->setHtmlType(InputModelOption::HTML_TYPE_SELECT);
$availableContactType = array('' => 'No contact type') + $this->getDataContactType(onOfficeSDK::MODULE_ADDRESS);
$pInputModelFormContactType->setHtmlType(InputModelOption::HTML_TYPE_SELECT_TWO);
$availableContactType = $this->getDataContactType(onOfficeSDK::MODULE_ADDRESS);
$pInputModelFormContactType->setValuesAvailable($availableContactType);
$pInputModelFormContactType->setIsMulti(true);
$selectedValue = $this->getValue($field);
if (is_null($selectedValue)) {
$selectedValue = [];
}
$pInputModelFormContactType->setValue($selectedValue);

return $pInputModelFormContactType;
Expand Down
2 changes: 1 addition & 1 deletion plugin/Model/InputModel/InputModelDBFactoryConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class InputModelDBFactoryConfigForm
self::KEY_FIELD => 'show_estate_context',
],
self::INPUT_FORM_CONTACT_TYPE => [
self::KEY_TABLE => 'oo_plugin_forms',
self::KEY_TABLE => 'oo_plugin_contacttypes',
self::KEY_FIELD => 'contact_type',
],

Expand Down
5 changes: 5 additions & 0 deletions plugin/Model/InputModelDBAdapterRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class InputModelDBAdapterRow
'sortbyvalue_id' => null,
'listview_id' => ['oo_plugin_listviews', 'listview_id'],
],

'oo_plugin_contacttypes' => [
'contacttype_id' => null,
'form_id' => ['oo_plugin_forms', 'form_id'],
],
];


Expand Down
3 changes: 3 additions & 0 deletions plugin/Record/RecordManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ abstract class RecordManager
/** */
const TABLENAME_FIELDCONFIG_ESTATE_TRANSLATED_LABELS = 'oo_plugin_fieldconfig_estate_translated_labels';

/** */
const TABLENAME_CONTACT_TYPES = 'oo_plugin_contacttypes';

/**
*
* @deprecated get wpdb via DI
Expand Down
9 changes: 9 additions & 0 deletions plugin/Record/RecordManagerDuplicateListViewForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ public function duplicateByName(string $name)
$tableFieldFormTranslatedLabel, 'input_id', 'translated_label_id' );
}
}

$contactTypes = $listViewRoot['contact_type'];
foreach ($contactTypes as $contactType) {
$this->_pWPDB->insert($prefix.'oo_plugin_contacttypes', [
'form_id' => esc_sql((int) $duplicateListViewId),
'contact_type' => esc_sql($contactType)
]
);
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions plugin/Record/RecordManagerRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,31 @@ public function getRowById(int $recordId): array
WHERE `".esc_sql($this->getIdColumnMain())."` = ".esc_sql((int)$recordId);

$result = $pWpDb->get_row($sql, ARRAY_A);
if ($result !== null) {
$result['contact_type'] = $this->readContactTypesByFormId($recordId);
}

return $result;
}

/**
* @param int $formId
*
* @return array
*/
public function readContactTypesByFormId(int $formId): array
{
$prefix = $this->getTablePrefix();
$pWpDb = $this->getWpdb();
$sql = "SELECT `contact_type`
FROM {$prefix}oo_plugin_contacttypes
WHERE `form_id` = ".esc_sql($formId);

$contactTypes = $pWpDb->get_col($sql);

return is_array($contactTypes) ? $contactTypes : [];
}


/** @return int */
public function getCountOverall(): int
Expand Down
2 changes: 2 additions & 0 deletions plugin/Record/RecordManagerReadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public function getRowByName($formName)
$result['filterable'] = array_keys(array_filter(array_column($resultFieldConfig, 'filterable', 'fieldname')));
$result['hidden'] = array_keys(array_filter(array_column($resultFieldConfig, 'hidden', 'fieldname')));

$result['contact_type'] = $this->readContactTypesByFormId($result['form_id']);

return $result;
}

Expand Down
23 changes: 23 additions & 0 deletions plugin/Record/RecordManagerUpdateForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,27 @@ public function updateFieldConfigByRow(array $fieldConfig): bool

return $result;
}

/**
* @param array $contactTypes
*
* @return bool
*/
public function updateContactTypeByRow(array $contactTypes): bool
{
$prefix = $this->getTablePrefix();
$pWpDb = $this->getWpdb();
$pWpDb->delete($prefix.self::TABLENAME_CONTACT_TYPES, ['form_id' => $this->getRecordId()]);

$result = true;
foreach ($contactTypes as $row) {
if (empty($row)) {
continue;
}

$result = $result && $pWpDb->insert($pWpDb->prefix.self::TABLENAME_CONTACT_TYPES, $row);
}

return $result;
}
}
6 changes: 6 additions & 0 deletions plugin/Record/RecordStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ class RecordStructure
self::EMPTY_VALUE => '',
),
],
RecordManager::TABLENAME_CONTACT_TYPES => array(
'contact_type' => array(
self::NULL_ALLOWED => false,
self::EMPTY_VALUE => '',
),
),
);


Expand Down
1 change: 1 addition & 0 deletions plugin/Renderer/InputModelRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ private function getHtmlElementName(InputModelBase $pInputModel): string
case InputModelOption::HTML_TYPE_BUTTON_FIELD:
case InputModelOption::HTML_SEARCH_FIELD_FOR_FIELD_LISTS:
case InputModelOption::HTML_TYPE_TOGGLE_SWITCH:
case InputModelOption::HTML_TYPE_SELECT_TWO:
if ($pInputModel->getIsMulti()) {
$name .= '[]';
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestClassDataFormConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ private function getBaseRow(int $formId, string $formType): array
'newsletter' => '1',
'availableOptions' => '1',
'show_estate_context' => '0',
'contact_type' => '',
'contact_type' => ['Owner', 'Investor'],
'default_recipient' => 'default@my-onoffice.com'
];
}
Expand Down
Loading

0 comments on commit b47552a

Please sign in to comment.