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

UI Disabled Inputs #1305

Merged
merged 7 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/UI/Component/Input/Field/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ public function isRequired();
public function withRequired($is_required);


/**
* Is this input disabled?
*
* @return bool
*/
public function isDisabled();


/**
* Get an input like this, but set it to a disabled state.
*
* @param bool $is_disabled
*
* @return Input
*/
public function withDisabled($is_disabled);


/**
* Get the value that is displayed in the input client side.
*
Expand Down
11 changes: 9 additions & 2 deletions src/UI/Implementation/Component/Input/Field/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ public function withInput(PostData $post_input) {
throw new \LogicException("Can only collect if input has a name.");
}

$value = $post_input->getOr($this->getName(), "");
$clone = $this->withValue($value);
if (!$this->isDisabled()) {
$value = $post_input->getOr($this->getName(), "");
$clone = $this->withValue($value);
}
else {
$value = $this->getValue();
$clone = $this;
}

$clone->content = $this->applyOperationsTo($value);
if ($clone->content->isError()) {
return $clone->withError("" . $clone->content->error());
Expand Down
10 changes: 10 additions & 0 deletions src/UI/Implementation/Component/Input/Field/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public function __construct(
$this->inputs = $inputs;
}

public function withDisabled($is_disabled) {
$clone = parent::withDisabled($is_disabled);
$inputs = [];
foreach ($this->inputs as $key => $input)
{
$inputs[$key] = $input->withDisabled($is_disabled);
}
$clone->inputs = $inputs;
return $clone;
}
}
35 changes: 33 additions & 2 deletions src/UI/Implementation/Component/Input/Field/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ abstract class Input implements C\Input\Field\Input, InputInternal {
* @var bool
*/
protected $is_required = false;
/**
* @var bool
*/
protected $is_disabled = false;
/**
* This is the value contained in the input as displayed
* client side.
Expand Down Expand Up @@ -176,6 +180,26 @@ public function withRequired($is_required) {
abstract protected function getConstraintForRequirement();


/**
* @inheritdoc
*/
public function isDisabled() {
return $this->is_disabled;
}


/**
* @inheritdoc
*/
public function withDisabled($is_disabled) {
$this->checkBoolArg("is_disabled", $is_disabled);
$clone = clone $this;
$clone->is_disabled = $is_disabled;

return $clone;
}


/**
* Get the value that is displayed in the input client side.
*
Expand Down Expand Up @@ -362,8 +386,15 @@ public function withInput(PostData $input) {

//TODO: Discuss, is this correct here. If there is no input contained in this post
//We assign null. Note that unset checkboxes are not contained in POST.
$value = $input->getOr($this->getName(), null);
$clone = $this->withValue($value);
if (!$this->isDisabled()) {
$value = $input->getOr($this->getName(), null);
$clone = $this->withValue($value);
}
else {
$value = $this->getValue();
$clone = $this;
}

$clone->content = $this->applyOperationsTo($value);
if ($clone->content->isError()) {
return $clone->withError("" . $clone->content->error());
Expand Down
18 changes: 14 additions & 4 deletions src/UI/Implementation/Component/Input/Field/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,26 @@ public function withInput(PostData $post_input) {
if ($this->getName() === null) {
throw new \LogicException("Can only collect if input has a name.");
}
$value = $post_input->getOr($this->getName(), "");

$clone = $this->withValue($value);
if (!$this->isDisabled()) {
$value = $post_input->getOr($this->getName(), "");
$clone = $this->withValue($value);
}
else {
$value = $this->getValue();
$clone = $this;
}

$clone->content = $this->applyOperationsTo($value);
if ($clone->content->isError()) {
return $clone->withError("" . $clone->content->error());
}

$dep_fields = $this->getDependantFieldsFor($value);
if (is_null($value)) {
$dep_fields = $this->getDependantFieldsFor("");
}
else {
$dep_fields = $this->getDependantFieldsFor($value);
}
if(is_null($dep_fields)) {
$clone->content = $this->applyOperationsTo($value);
} else {
Expand Down
23 changes: 22 additions & 1 deletion src/UI/Implementation/Component/Input/Field/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ protected function renderInputField(Template $tpl, Input $input, $id) {
$tpl->setVariable("VALUE", $input->getValue());
$tpl->parseCurrentBlock();
}
if ($input->isDisabled()) {
$tpl->setCurrentBlock("disabled");
$tpl->setVariable("DISABLED", 'disabled="disabled"');
$tpl->parseCurrentBlock();
}
if ($id) {
$tpl->setCurrentBlock("id");
$tpl->setVariable("ID", $id);
Expand Down Expand Up @@ -323,6 +328,11 @@ function ($id) use ($configuration) {
*/
$tpl->setVariable("ID", $id);
$tpl->setVariable("NAME", $input->getName());
if ($input->isDisabled()) {
$tpl->setCurrentBlock("disabled");
$tpl->setVariable("DISABLED", "disabled");
$tpl->parseCurrentBlock();
}
if ($input->getValue()) {
$value = $input->getValue();
$tpl->setVariable("VALUE_COMMA_SEPARATED", implode(",", $value));
Expand All @@ -342,14 +352,19 @@ function ($id) use ($configuration) {

public function renderSelectInput(Template $tpl, Select $input)
{
if ($input->isDisabled()) {
$tpl->setCurrentBlock("disabled");
$tpl->setVariable("DISABLED", 'disabled="disabled"');
$tpl->parseCurrentBlock();
}
$value = $input->getValue();
//disable first option if required.
$tpl->setCurrentBlock("options");
if(!$value) {
$tpl->setVariable("SELECTED", "selected");
}
if($input->isRequired()) {
$tpl->setVariable("DISABLED", "disabled");
$tpl->setVariable("DISABLED_OPTION", "disabled");
$tpl->setVariable("HIDDEN", "hidden");
}
$tpl->setVariable("VALUE", NULL);
Expand Down Expand Up @@ -383,6 +398,9 @@ public function renderMultiSelectInput(Template $tpl, MultiSelect $input) : Temp
if($value && in_array($opt_value, $value)) {
$tpl->setVariable("CHECKED", 'checked="checked"');
}
if ($input->isDisabled()) {
$tpl->setVariable("DISABLED", 'disabled="disabled"');
}

$tpl->parseCurrentBlock();
}
Expand Down Expand Up @@ -488,6 +506,9 @@ protected function renderRadioField(Component\Input\Field\Radio $input, Renderer
if ($input->getValue() !== null && $input->getValue()===$value) {
$input_tpl->setVariable("CHECKED", 'checked="checked"');
}
if ($input->isDisabled()) {
$input_tpl->setVariable("DISABLED", 'disabled="disabled"');
}

//dependant fields
$dependant_group_html = '';
Expand Down
40 changes: 40 additions & 0 deletions src/UI/examples/Input/Field/Checkbox/disabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Example showing how to plug a disabled checkbox into a form
*/
function disabled() {
//Step 0: Declare dependencies
global $DIC;
$ui = $DIC->ui()->factory();
$renderer = $DIC->ui()->renderer();
$request = $DIC->http()->request();

//Step 1: define the checkbox, and making it disabled
$checkbox_input = $ui->input()->field()->checkbox("Checkbox", "Cannot check.")
->withDisabled(true);

//Step 2: define form and form actions
$DIC->ctrl()->setParameterByClass(
'ilsystemstyledocumentationgui',
'example_name',
'checkbox_disabled'
);
$form_action = $DIC->ctrl()->getFormActionByClass('ilsystemstyledocumentationgui');
$form = $ui->input()->container()->form()->standard($form_action, [ $checkbox_input]);

//Step 3: implement some form data processing. Note, the value of the checkbox will
// be 'checked' if checked and null if unchecked.
if ($request->getMethod() == "POST"
&& $request->getQueryParams()['example_name'] =='checkbox_disabled') {
$form = $form->withRequest($request);
$result = $form->getData();
}
else {
$result = "No result yet.";
}

//Step 4: Render the checkbox with the enclosing form.
return
"<pre>".print_r($result, true)."</pre><br/>".
$renderer->render($form);
}
49 changes: 49 additions & 0 deletions src/UI/examples/Input/Field/DependantGroup/disabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Example showing how a disabled dependant group (aka sub form) might be attached to a checkbox.
*/
function disabled() {
//Step 0: Declare dependencies
global $DIC;
$ui = $DIC->ui()->factory();
$renderer = $DIC->ui()->renderer();
$request = $DIC->http()->request();

//Step 1: Define the dependent group (aka sub section)
$dependant_field1 = $ui->input()->field()->text("Item 1", "Just some disabled dependent group field");
$dependant_field2 = $ui->input()->field()->text("Item 2", "Just another disabled dependent group field");

$dependant_group = $ui->input()->field()->dependantGroup([ "Sub Part 1"=>$dependant_field1, "Sub Part 2"=>$dependant_field2])
->withDisabled(true);

//Step 2: Define input and attach sub section
$checkbox_input = $ui->input()->field()->checkbox("Checkbox", "Check to display disabled dependant field.")
->withValue(true)
->withDependantGroup($dependant_group);

//Step 3: Define form and form actions
$DIC->ctrl()->setParameterByClass(
'ilsystemstyledocumentationgui',
'example_name',
'checkbox_disabled'
);

$form_action = $DIC->ctrl()->getFormActionByClass('ilsystemstyledocumentationgui');
$form = $ui->input()->container()->form()->standard($form_action, [ "checkbox"=>$checkbox_input]);


//Step 4: Implement some form data processing.
if ($request->getMethod() == "POST"
&& $request->getQueryParams()['example_name'] =='checkbox_disabled') {
$form = $form->withRequest($request);
$result = $form->getData();
}
else {
$result = "No result yet.";
}

//Step 5: Render the checkbox with the enclosing form.
return
"<pre>".print_r($result, true)."</pre><br/>".
$renderer->render($form);
}
68 changes: 68 additions & 0 deletions src/UI/examples/Input/Field/Group/disabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Example showing how disabled groups can be used.
*/
function disabled() {
//Step 0: Declare dependencies
global $DIC;
$ui = $DIC->ui()->factory();
$lng = $DIC->language();
$renderer = $DIC->ui()->renderer();
$request = $DIC->http()->request();
$data = new \ILIAS\Data\Factory();
$validation = new \ILIAS\Validation\Factory($data, $lng);
$trafo = new \ILIAS\Transformation\Factory();

//Step 1: Implement transformation and constraints
$sum = $trafo->custom(function($vs) {
list($l, $r) = $vs;
$s = $l + $r;
return $s;
});
$equal_ten = $validation->custom(function($v) {
return $v==10;
}, "The sum must equal ten.");

//Step 2: Define inputs
$number_input = $ui->input()->field()->numeric("number", "Cannot put in a number.")->withValue(5);

//Step 3: Define the group, add the inputs to the group and attach the
//transformation and constraint
$group = $ui->input()->field()->group(
[ $number_input->withLabel("Left"), $number_input->withLabel("Right")])->withDisabled(true)
->withAdditionalTransformation($sum)
->withAdditionalConstraint($equal_ten);

//Step 4: define form and form actions, attach the group to the form
$DIC->ctrl()->setParameterByClass(
'ilsystemstyledocumentationgui',
'example_name',
'numeric_inputs_disabled'
);
$form_action = $DIC->ctrl()->getFormActionByClass('ilsystemstyledocumentationgui');
$form = $ui->input()->container()->form()->standard($form_action, ["custom_group"=>$group]);

//Step 4: Implement some form data processing.
if ($request->getMethod() == "POST"
&& $request->getQueryParams()['example_name'] =='numeric_inputs_disabled') {
//Step 4.1: Device some context dependant logic to display the potential
// constraint error on the group.
$form = $form->withRequest($request);
$group = $form->getInputs()["custom_group"];
if($group->getError()){
$result = $group->getError();
}else{
//The result is sumarized through the transformation
$result = $form->getData();
}

}
else {
$result = "No result yet.";
}

//Step 5: Return the rendered form
return
"<pre>".print_r($result, true)."</pre><br/>".
$renderer->render($form);
}
Loading