Skip to content

Commit

Permalink
EnumField: propertyName to use the identifier provided in the form (n…
Browse files Browse the repository at this point in the history
…o more prefix)

The migration adjust existing form so the new identifier is the previously calculated one
  • Loading branch information
seballot committed Jul 2, 2024
1 parent 16d7362 commit c4fa524
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use YesWiki\Bazar\Field\EnumField;
use YesWiki\Bazar\Service\FieldFactory;
use YesWiki\Bazar\Service\FormManager;
use YesWiki\Core\YesWikiMigration;

class RefactorEnumFieldPropertyName extends YesWikiMigration
{
public function run()
{
$formManager = $this->getService(FormManager::class);
$fieldFactory = $this->getService(FieldFactory::class);
$forms = $formManager->getAll();
foreach ($forms as $form) {
$newTemplate = [];
foreach ($form['template'] as $fieldArray) {
$field = $fieldFactory->create($fieldArray);
if ($field instanceof EnumField) {
$fieldArray[EnumField::FIELD_NAME] = $field->getType() . $field->getLinkedObjectName() . $field->getName();
}
$newTemplate[] = $fieldArray;
}
$form['bn_template'] = $formManager->encodeTemplate($newTemplate);
$formManager->update($form);
}
}
}
19 changes: 8 additions & 11 deletions tools/bazar/fields/EnumField.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@ abstract class EnumField extends BazarField
protected $optionsUrls; // only for loadOptionsFromJson
protected $optionsTree = null; // only for list with multi levels

protected $listLabel; // Allows to differentiate two enums using the same list
protected $linkedObjectName;
protected $keywords;
protected $queries;

protected const FIELD_LIST_LABEL = 6;
protected const FIELD_LINKED_OBJECT = 1;
public const FIELD_NAME = 6;
protected const FIELD_KEYWORDS = 13;
protected const FIELD_QUERIES = 15;

public function __construct(array $values, ContainerInterface $services)
{
parent::__construct($values, $services);

$this->listLabel = $values[self::FIELD_LIST_LABEL];
$this->name = $values[self::FIELD_NAME];
$this->linkedObjectName = $values[self::FIELD_LINKED_OBJECT];
$this->keywords = $values[self::FIELD_KEYWORDS];
$this->queries = $values[self::FIELD_QUERIES];

$this->options = [];
$this->optionsUrls = [];

$this->propertyName = $this->type . $this->name . $this->listLabel;
$this->propertyName = $this->name;
}

public function loadOptionsFromList()
{
if (!empty($this->getLinkedObjectName())) {
$list = $this->getService(ListManager::class)->getOne($this->getLinkedObjectName());
$this->options = [];
foreach ($list['nodes'] ?? [] as $node) {
$this->loadOptionsFromListNode($node);
if (isset($node['children']) && count($node['children']) > 0) {
Expand Down Expand Up @@ -152,7 +155,6 @@ public function loadOptionsFromEntries()
*/
protected function prepareJSONEntryField()
{
$this->propertyName = $this->type . removeAccents(preg_replace('/--+/u', '-', preg_replace('/[[:punct:]]/', '-', $this->name))) . $this->listLabel;
$this->loadOptionsFromJson();
if (
preg_match('/^(.*\/\??)' // catch baseUrl
Expand Down Expand Up @@ -193,14 +195,9 @@ protected function getEntriesOptions()
return $this->options;
}

public function getName()
{
return $this->listLabel;
}

public function getLinkedObjectName()
{
return $this->name;
return $this->linkedObjectName;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
attrs: { type: 'checkbox-group' },
icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check2-square" viewBox="0 0 16 16"><path d="M3 14.5A1.5 1.5 0 0 1 1.5 13V3A1.5 1.5 0 0 1 3 1.5h8a.5.5 0 0 1 0 1H3a.5.5 0 0 0-.5.5v10a.5.5 0 0 0 .5.5h10a.5.5 0 0 0 .5-.5V8a.5.5 0 0 1 1 0v5a1.5 1.5 0 0 1-1.5 1.5H3z"/><path d="m8.354 10.354 7-7a.5.5 0 0 0-.708-.708L8 9.293 5.354 6.646a.5.5 0 1 0-.708.708l3 3a.5.5 0 0 0 .708 0z"/></svg>'
},
defaultIdentifier: 'bf_checkboxes',
attributes: {
...selectConf,
...{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default {
}
}
},
defaultIdentifier: 'bf_choice',
advancedAttributes: ['read', 'write', 'semantic', 'queries', 'fillingMode', 'options'],
// disabledAttributes: [],
attributesMapping: { ...listsMapping, ...{ 7: 'fillingMode' } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ function initializeFormbuilder() {
const fieldType = $(this).closest('.form-field').attr('type')

// Make the default names easier to read
if (['radio_group', 'checkbox_group', 'select'].includes(fieldType)) {
name = ''
} else if (!name.includes('bf_')) {
if (!name.includes('bf_')) {
name = defaultFieldsName[fieldType] || `bf_${fieldType}`
if (existingFieldsNames.includes(name)) {
// If name already exist, we add a number (bf_address, bf_address1, bf_address2...)
Expand Down

0 comments on commit c4fa524

Please sign in to comment.