Skip to content

Commit

Permalink
feat(condition): add condition to show or hide the item
Browse files Browse the repository at this point in the history
constant SHOW_CONDITION_REGEX
  • Loading branch information
aagz authored and btry committed Jan 18, 2021
1 parent bf18a48 commit 2681b9c
Show file tree
Hide file tree
Showing 27 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class PluginFormcreatorCondition extends CommonDBChild implements PluginFormcrea
const SHOW_CONDITION_GE = 6;
const SHOW_CONDITION_QUESTION_VISIBLE = 7;
const SHOW_CONDITION_QUESTION_INVISIBLE = 8;
const SHOW_CONDITION_REGEX = 9;

public static function getTypeName($nb = 0) {
return _n('Condition', 'Conditions', $nb, 'formcreator');
Expand Down Expand Up @@ -100,6 +101,7 @@ public static function getEnumShowCondition() : array {
self::SHOW_CONDITION_GE => '',
self::SHOW_CONDITION_QUESTION_VISIBLE => __('is visible', 'formcreator'),
self::SHOW_CONDITION_QUESTION_INVISIBLE => __('is not visible', 'formcreator'),
self::SHOW_CONDITION_REGEX => __('regular expression matches', 'formcreator'),
];
}

Expand Down
4 changes: 4 additions & 0 deletions inc/field/actorfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ public function lessThan($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ public function lessThan($value): bool {
return true;
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions inc/field/datefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use DateTime;
use Toolbox;
use Session;
use GlpiPlugin\Formcreator\Exception\ComparisonException;


class DateField extends PluginFormcreatorAbstractField
Expand Down Expand Up @@ -192,6 +193,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function parseAnswerValues($input, $nonDestructive = false): bool {
$key = 'formcreator_field_' . $this->question->getID();
if (!isset($input[$key])) {
Expand Down
5 changes: 5 additions & 0 deletions inc/field/datetimefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Session;
use Toolbox;
use DateTime;
use GlpiPlugin\Formcreator\Exception\ComparisonException;

class DatetimeField extends PluginFormcreatorAbstractField
{
Expand Down Expand Up @@ -194,6 +195,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function parseAnswerValues($input, $nonDestructive = false): bool {
$key = 'formcreator_field_' . $this->question->getID();
if (!isset($input[$key])) {
Expand Down
4 changes: 4 additions & 0 deletions inc/field/descriptionfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public function lessThan($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function regex($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function parseAnswerValues($input, $nonDestructive = false): bool {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function parseAnswerValues($input, $nonDestructive = false): bool {
$key = 'formcreator_field_' . $this->question->getID();
if (!isset($input[$key])) {
Expand Down
4 changes: 4 additions & 0 deletions inc/field/emailfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ public function lessThan($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/filefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ public function lessThan($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function regex($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions inc/field/floatfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Session;
use PluginFormcreatorQuestionRange;
use PluginFormcreatorQuestionRegex;
use GlpiPlugin\Formcreator\Exception\ComparisonException;

class FloatField extends PluginFormcreatorAbstractField
{
Expand Down Expand Up @@ -287,6 +288,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/glpiselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/hiddenfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/hostnamefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/integerfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public function greaterThan($value): bool {
return ((int) $this->value) > ((int) $value);
}

public function regex($value): bool {
return (preg_grep($value, (int) $this->value)) ? true : false;
}

public function getHtmlIcon() {
return '<i class="fas fa-square-root-alt" aria-hidden="true"></i>';
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/ipfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ public function lessThan($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/ldapselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/radiosfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions inc/field/requesttypefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Session;
use Ticket;
use Dropdown;
use GlpiPlugin\Formcreator\Exception\ComparisonException;

class RequestTypeField extends SelectField
{
Expand Down Expand Up @@ -218,6 +219,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/tagfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ public function lessThan($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function regex($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function isAnonymousFormCompatible(): bool {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/textareafield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/field/textfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
return preg_match($value, Toolbox::stripslashes_deep($this->value)) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions inc/field/timefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use PluginFormcreatorAbstractField;
use Session;
use Toolbox;
use GlpiPlugin\Formcreator\Exception\ComparisonException;

class TimeField extends PluginFormcreatorAbstractField
{
Expand Down Expand Up @@ -192,6 +193,12 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
throw new ComparisonException('Meaningless comparison');
}



public function parseAnswerValues($input, $nonDestructive = false): bool {

$key = 'formcreator_field_' . $this->question->getID();
Expand Down
5 changes: 5 additions & 0 deletions inc/field/urgencyfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Html;
use Session;
use Ticket;
use GlpiPlugin\Formcreator\Exception\ComparisonException;

class UrgencyField extends PluginFormcreatorAbstractField
{
Expand Down Expand Up @@ -222,6 +223,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
throw new ComparisonException('Meaningless comparison');
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions inc/fieldinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ public function greaterThan($value) : bool;
*/
public function LessThan($value) : bool;

/**
* Tests if the given value is match with regex
*
* @return boolean True if the value is match with regex
*/
public function regex($value) : bool;

/**
* Is the field compatible with anonymous form ?
*
Expand Down
12 changes: 12 additions & 0 deletions inc/fields.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ public static function isVisible(PluginFormcreatorConditionnableInterface $item,
$value = false;
}
break;

case PluginFormcreatorCondition::SHOW_CONDITION_REGEX:
if (!$conditionField->isPrerequisites()) {
self::$visibility[$itemtype][$itemId] = false;
return self::$visibility[$itemtype][$itemId];
}
try {
$value = $conditionField->regex($condition->fields['show_value']);
} catch (ComparisonException $e) {
$value = false;
}
break;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/3-unit/PluginFormcreatorCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function testGetEnumShowCondition() {
'6' => '',
'7' => 'is visible',
'8' => 'is not visible',
'9' => 'regular expression matches'
]);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/fixture/PluginFormcreatorDependentField.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ public function lessThan($value): bool {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function regex($value): bool {
return preg_match($value, $this->value) ? true : false;
}

public function isAnonymousFormCompatible(): bool {
return true;
}
Expand Down

0 comments on commit 2681b9c

Please sign in to comment.