N°1318 - multiple choice in a template - WIP

This commit is contained in:
acognet
2023-06-14 10:49:40 +02:00
parent 413a798510
commit 2896357078
10 changed files with 178 additions and 86 deletions

View File

@@ -82,16 +82,11 @@ abstract class MultipleChoicesField extends Field
*/
public function SetCurrentValue($currentValue)
{
if (is_array($currentValue))
{
if (is_array($currentValue)) {
$this->currentValue = $currentValue;
}
elseif (is_null($currentValue))
{
} elseif (is_null($currentValue)) {
$this->currentValue = array();
}
else
{
} else {
$this->currentValue = array($currentValue);
}
return $this;

View File

@@ -8,6 +8,12 @@
namespace Combodo\iTop\Form\Field;
use BinaryExpression;
use DBObjectSet;
use FieldExpression;
use ScalarExpression;
use utils;
/**
* Description of MultipleSelectObjectField
*
@@ -16,6 +22,8 @@ namespace Combodo\iTop\Form\Field;
*/
class MultipleSelectObjectField extends SelectObjectField
{
const DEFAULT_MULTIPLE_VALUES_ENABLED = true;
function SetCurrentValue($currentValue)
{
if ($currentValue != null) {
@@ -26,4 +34,73 @@ class MultipleSelectObjectField extends SelectObjectField
return $this;
}
public function Validate()
{
$this->SetValid(true);
$this->EmptyErrorMessages();
if ($this->GetReadOnly() === false) {
if (count($this->currentValue) > 0) {
foreach ($this->currentValue as $sCode => $value) {
if (utils::IsNullOrEmptyString($value) || ($value == 0)) {
continue;
}
$oSearchForExistingCurrentValue = $this->oSearch->DeepClone();
$oSearchForExistingCurrentValue->AddCondition('id', $value, '=');
$oCheckIdAgainstCurrentValueExpression = new BinaryExpression(
new FieldExpression('id', $oSearchForExistingCurrentValue->GetClassAlias()), '=', new ScalarExpression($value)
);
$oSearchForExistingCurrentValue->AddConditionExpression($oCheckIdAgainstCurrentValueExpression);
$oSetForExistingCurrentValue = new DBObjectSet($oSearchForExistingCurrentValue);
$iObjectsCount = $oSetForExistingCurrentValue->CountWithLimit(1);
if ($iObjectsCount === 0) {
$this->SetValid(false);
$this->AddErrorMessage("Value $value does not match the corresponding filter set");
return $this->GetValid();
}
}
}
}
foreach ($this->GetValidators() as $oValidator) {
foreach ($this->currentValue as $value) {
if (!preg_match($oValidator->GetRegExp(true), $value)) {
$this->SetValid(false);
$this->AddErrorMessage($oValidator->GetErrorMessage());
}
}
}
return $this->GetValid();
}
/**
* Resets current value if not among allowed ones.
* By default, reset is done ONLY when the field is not read-only.
*
* @param boolean $bAlways Set to true to verify even when the field is read-only.
*
* @throws \CoreException
*/
public function VerifyCurrentValue(bool $bAlways = false)
{
if (!$this->GetReadOnly() || $bAlways) {
if (count($this->currentValue) > 0) {
foreach ($this->currentValue as $sCode => $value) {
$oValuesScope = $this->GetSearch()->DeepClone();
$oBinaryExp = new BinaryExpression(new FieldExpression('id', $oValuesScope->GetClassAlias()), '=',
new ScalarExpression($value));
$oValuesScope->AddConditionExpression($oBinaryExp);
$oValuesSet = new DBObjectSet($oValuesScope);
}
} else {
$this->currentValue = [];
}
}
}
}

View File

@@ -36,7 +36,7 @@ use utils;
* @author Romain Quetiez <romain.quetiez@combodo.com>
* @since 2.3.0
*/
class SelectObjectField extends Field
class SelectObjectField extends MultipleChoicesField
{
/** @var int CONTROL_SELECT */
const CONTROL_SELECT = 1;

View File

@@ -26,6 +26,7 @@ namespace Combodo\iTop\Form\Field;
*/
class TagSetObjectField extends MultipleSelectObjectField
{
const DEFAULT_MULTIPLE_VALUES_ENABLED = true;
function SetCurrentValue($currentValue)
{

View File

@@ -185,10 +185,7 @@ EOF
* $oPage->add_ready_script($sScript);*/
$oValue = UIContentBlockUIBlockFactory::MakeStandard("", ["form-field-content"]);
$bVertical = true;
$idx = 0;
$aValues = $this->oField->GetCurrentValue();
$sId = $this->oField->GetGlobalId();
$aFieldsToLoad = [];
$aComplementAttributeSpec = MetaModel::GetNameSpec($oAllowedValues->GetClass(), FriendlyNameType::COMPLEMENTARY);
@@ -209,49 +206,60 @@ EOF
}
$oAllowedValues->OptimizeColumnLoad($aFieldsToLoad);
//MAYBE USE INPUT SET ?
$oSelect = SelectUIBlockFactory::MakeForSelectWithLabel($this->oField->GetLabel(), $this->oField->GetDescription(), $this->oField->GetGlobalId());
$oSelect->SetIsMultiple(true);
while ($oObj = $oAllowedValues->Fetch()) {
$oSelect->AddSubBlock(SelectOptionUIBlockFactory::MakeForSelectOption(
$oObj->GetKey(),
$oObj->GetName(),
is_null($aValues) ? false : in_array($oObj->GetKey(), $aValues, true) === true)
(is_null($aValues) || !is_array($aValues)) ? false : in_array($oObj->GetKey(), $aValues, true) === true)
);
}
$oOutput->AddJs(
<<<JS
$oOutput->AddJs(
<<<JS
$("#{$this->oField->GetGlobalId()}").selectize({
maxItems: null,
sortField: 'text',
onChange: function(value){
console.warn('chane');
var me = this.\$input;
me.trigger("field_change", {
id: me.attr("id"),
name: me.closest(".form_field").attr("data-field-id"),
value: me.val()
})
.closest('.form_handler').trigger('value_change');
console.warn('chane');
var me = this.\$input;
me.trigger("field_change", {
id: me.attr("id"),
name: me.closest(".form_field").attr("data-field-id"),
value: me.val()
})
.closest('.form_handler').trigger('change');
$(me).closest(".field_set").trigger("field_change", {
id: $(me).attr("id"),
name: $(me).closest(".form_field").attr("data-field-id"),
value: $(me).val()
})
.closest('.form_handler').trigger('value_change');
},
loadingClass: '',
itemClass: 'item attribute-set-item',
inputClass: 'attribute-set ibo-input ibo-input-selectize',
render: {
option: function(option) {
return CombodoGlobalToolbox.RenderTemplate('#{$this->oField->GetGlobalId()}_options_template', option, this.settings.optionClass)[0].outerHTML;
},
item: function (item) {
return CombodoGlobalToolbox.RenderTemplate('#{$this->oField->GetGlobalId()}_items_template', item, this.settings.itemClass)[0].outerHTML;
},
},
inputClass: 'ibo-input-vanilla ibo-input ibo-input-selectize',
});
$("#{$this->oField->GetGlobalId()}").closest('div').addClass('ibo-input-select-wrapper--with-buttons');
$("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
var me = this;
$(this).closest(".field_set").trigger("field_change", {
id: $(me).attr("id"),
name: $(me).closest(".form_field").attr("data-field-id"),
value: $(me).val()
})
.closest('.form_handler').trigger('value_change');
});
JS
);
$oValue->AddSubBlock($oSelect);
$oValue->AddSubBlock(new Html('<span class="form_validation"></span>'));
$oBlock->AddSubBlock($oValue);
break;
@@ -387,8 +395,19 @@ EOF
value: me.val()
})
.closest('.form_handler').trigger('value_change');
},
inputClass: 'ibo-input-vanilla ibo-input ibo-input-selectize',
},
loadingClass: '',
itemClass: 'item attribute-set-item',
inputClass: 'ibo-input-vanilla ibo-input ibo-input-selectize',
plugins: {
'combodo_update_operations' : {
initial: ["5"],
},
'combodo_auto_position' : {
maxDropDownHeight: 300,
},
'remove_button' : {},
},
});
$("#{$this->oField->GetGlobalId()}").closest('div').addClass('ibo-input-select-wrapper--with-buttons');
JS
@@ -396,6 +415,7 @@ JS
}
}
$oBlock->AddDataAttribute("input-type", $sFieldClass);
$oOutput->AddHtml((BlockRenderer::RenderBlockTemplates($oBlock)));
// JS Form field widget construct
$aValidators = array();

View File

@@ -118,9 +118,11 @@ class ConsoleSimpleFieldRenderer extends FieldRenderer
break;
case 'Combodo\\iTop\\Form\\Field\\LabelField':
$oValue = UIContentBlockUIBlockFactory::MakeStandard("",[""]);
$oValue = UIContentBlockUIBlockFactory::MakeStandard("", [""]);
$oBlock->AddSubBlock($oValue);
$oValue->AddSubBlock(new Text($this->oField->GetCurrentValue()));
if (utils::IsNotNullOrEmptyString($this->oField->GetCurrentValue())) {
$oValue->AddSubBlock(new Text($this->oField->GetCurrentValue()));
}
$oValue->AddSubBlock(new Html('<span class="form_validation"></span>'));
break;
@@ -241,6 +243,7 @@ EOF
.closest('.form_handler').trigger('value_change');
console.warn('chanegement');
},
itemClass: 'attribute-set-item',
});
$("#{$this->oField->GetGlobalId()}").closest('div').addClass('ibo-input-select-wrapper--with-buttons');
JS
@@ -485,18 +488,19 @@ EOF
$oOutput->AddJs(
<<<EOF
$("#{$this->oField->GetGlobalId()}").selectize({
sortField: 'text',
onChange: function(value){
sortField: 'text',
onChange: function(value){
console.warn('lala');
var me = this.\$input;
me.closest(".field_set").trigger("field_change", {
id: me.attr("id"),
name: me.closest(".form_field").attr("data-field-id"),
value: me.val()
})
.closest('.form_handler').trigger('value_change');
}
var me = this.\$input;
me.closest(".field_set").trigger("field_change", {
id: me.attr("id"),
name: me.closest(".form_field").attr("data-field-id"),
value: me.val()
})
.closest('.form_handler').trigger('value_change');
},
itemClass: 'attribute-set-item'
});
EOF
);