From 2681b9c366e3e50ad7691c7bce0bb08ce329f45f Mon Sep 17 00:00:00 2001 From: aagz Date: Mon, 18 Jan 2021 00:21:51 +0300 Subject: [PATCH] feat(condition): add condition to show or hide the item constant SHOW_CONDITION_REGEX --- inc/condition.class.php | 2 ++ inc/field/actorfield.class.php | 4 ++++ inc/field/checkboxesfield.class.php | 4 ++++ inc/field/datefield.class.php | 5 +++++ inc/field/datetimefield.class.php | 5 +++++ inc/field/descriptionfield.class.php | 4 ++++ inc/field/dropdownfield.class.php | 4 ++++ inc/field/emailfield.class.php | 4 ++++ inc/field/filefield.class.php | 4 ++++ inc/field/floatfield.class.php | 5 +++++ inc/field/glpiselectfield.class.php | 4 ++++ inc/field/hiddenfield.class.php | 4 ++++ inc/field/hostnamefield.class.php | 4 ++++ inc/field/integerfield.class.php | 4 ++++ inc/field/ipfield.class.php | 4 ++++ inc/field/ldapselectfield.class.php | 4 ++++ inc/field/radiosfield.class.php | 4 ++++ inc/field/requesttypefield.class.php | 5 +++++ inc/field/tagfield.class.php | 4 ++++ inc/field/textareafield.class.php | 4 ++++ inc/field/textfield.class.php | 4 ++++ inc/field/timefield.class.php | 7 +++++++ inc/field/urgencyfield.class.php | 5 +++++ inc/fieldinterface.class.php | 7 +++++++ inc/fields.class.php | 12 ++++++++++++ tests/3-unit/PluginFormcreatorCondition.php | 1 + tests/fixture/PluginFormcreatorDependentField.php | 4 ++++ 27 files changed, 122 insertions(+) diff --git a/inc/condition.class.php b/inc/condition.class.php index 2db95dd14..c08847510 100644 --- a/inc/condition.class.php +++ b/inc/condition.class.php @@ -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'); @@ -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'), ]; } diff --git a/inc/field/actorfield.class.php b/inc/field/actorfield.class.php index 8ee5e6267..778c5f9ec 100644 --- a/inc/field/actorfield.class.php +++ b/inc/field/actorfield.class.php @@ -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; } diff --git a/inc/field/checkboxesfield.class.php b/inc/field/checkboxesfield.class.php index a17c8ab60..8199bfda9 100644 --- a/inc/field/checkboxesfield.class.php +++ b/inc/field/checkboxesfield.class.php @@ -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; } diff --git a/inc/field/datefield.class.php b/inc/field/datefield.class.php index 1ebdcf558..1307b0c34 100644 --- a/inc/field/datefield.class.php +++ b/inc/field/datefield.class.php @@ -37,6 +37,7 @@ use DateTime; use Toolbox; use Session; +use GlpiPlugin\Formcreator\Exception\ComparisonException; class DateField extends PluginFormcreatorAbstractField @@ -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])) { diff --git a/inc/field/datetimefield.class.php b/inc/field/datetimefield.class.php index 2677cfe93..009c1a1d8 100644 --- a/inc/field/datetimefield.class.php +++ b/inc/field/datetimefield.class.php @@ -37,6 +37,7 @@ use Session; use Toolbox; use DateTime; +use GlpiPlugin\Formcreator\Exception\ComparisonException; class DatetimeField extends PluginFormcreatorAbstractField { @@ -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])) { diff --git a/inc/field/descriptionfield.class.php b/inc/field/descriptionfield.class.php index a77fa98b9..ec8836a89 100644 --- a/inc/field/descriptionfield.class.php +++ b/inc/field/descriptionfield.class.php @@ -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; } diff --git a/inc/field/dropdownfield.class.php b/inc/field/dropdownfield.class.php index 7247b0e92..7062ecf30 100644 --- a/inc/field/dropdownfield.class.php +++ b/inc/field/dropdownfield.class.php @@ -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])) { diff --git a/inc/field/emailfield.class.php b/inc/field/emailfield.class.php index 252db49f6..a33dcfbc1 100644 --- a/inc/field/emailfield.class.php +++ b/inc/field/emailfield.class.php @@ -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; } diff --git a/inc/field/filefield.class.php b/inc/field/filefield.class.php index 32d6d4d9f..c618893e3 100644 --- a/inc/field/filefield.class.php +++ b/inc/field/filefield.class.php @@ -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; } diff --git a/inc/field/floatfield.class.php b/inc/field/floatfield.class.php index 26dfb5ce2..a652a3d17 100644 --- a/inc/field/floatfield.class.php +++ b/inc/field/floatfield.class.php @@ -38,6 +38,7 @@ use Session; use PluginFormcreatorQuestionRange; use PluginFormcreatorQuestionRegex; +use GlpiPlugin\Formcreator\Exception\ComparisonException; class FloatField extends PluginFormcreatorAbstractField { @@ -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; } diff --git a/inc/field/glpiselectfield.class.php b/inc/field/glpiselectfield.class.php index 12adc237d..a5265c218 100644 --- a/inc/field/glpiselectfield.class.php +++ b/inc/field/glpiselectfield.class.php @@ -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; } diff --git a/inc/field/hiddenfield.class.php b/inc/field/hiddenfield.class.php index bedd7cf07..f456c992a 100644 --- a/inc/field/hiddenfield.class.php +++ b/inc/field/hiddenfield.class.php @@ -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; } diff --git a/inc/field/hostnamefield.class.php b/inc/field/hostnamefield.class.php index 6ad306633..07747c42d 100644 --- a/inc/field/hostnamefield.class.php +++ b/inc/field/hostnamefield.class.php @@ -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; } diff --git a/inc/field/integerfield.class.php b/inc/field/integerfield.class.php index 695719296..7dfe9ad59 100644 --- a/inc/field/integerfield.class.php +++ b/inc/field/integerfield.class.php @@ -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 ''; } diff --git a/inc/field/ipfield.class.php b/inc/field/ipfield.class.php index 9d7b32116..87a78647e 100644 --- a/inc/field/ipfield.class.php +++ b/inc/field/ipfield.class.php @@ -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; } diff --git a/inc/field/ldapselectfield.class.php b/inc/field/ldapselectfield.class.php index 1b2b12384..095ede70a 100644 --- a/inc/field/ldapselectfield.class.php +++ b/inc/field/ldapselectfield.class.php @@ -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; } diff --git a/inc/field/radiosfield.class.php b/inc/field/radiosfield.class.php index 4aad4dac6..58c5ee2d1 100644 --- a/inc/field/radiosfield.class.php +++ b/inc/field/radiosfield.class.php @@ -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; } diff --git a/inc/field/requesttypefield.class.php b/inc/field/requesttypefield.class.php index 526109d7b..f1bbec33c 100644 --- a/inc/field/requesttypefield.class.php +++ b/inc/field/requesttypefield.class.php @@ -36,6 +36,7 @@ use Session; use Ticket; use Dropdown; +use GlpiPlugin\Formcreator\Exception\ComparisonException; class RequestTypeField extends SelectField { @@ -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; } diff --git a/inc/field/tagfield.class.php b/inc/field/tagfield.class.php index 0b9e8985e..680e9ed95 100644 --- a/inc/field/tagfield.class.php +++ b/inc/field/tagfield.class.php @@ -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; } diff --git a/inc/field/textareafield.class.php b/inc/field/textareafield.class.php index 43f433f47..e753097e4 100644 --- a/inc/field/textareafield.class.php +++ b/inc/field/textareafield.class.php @@ -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; } diff --git a/inc/field/textfield.class.php b/inc/field/textfield.class.php index fb80de811..bd533f440 100644 --- a/inc/field/textfield.class.php +++ b/inc/field/textfield.class.php @@ -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; } diff --git a/inc/field/timefield.class.php b/inc/field/timefield.class.php index 15160399f..78e9d9d4b 100644 --- a/inc/field/timefield.class.php +++ b/inc/field/timefield.class.php @@ -37,6 +37,7 @@ use PluginFormcreatorAbstractField; use Session; use Toolbox; +use GlpiPlugin\Formcreator\Exception\ComparisonException; class TimeField extends PluginFormcreatorAbstractField { @@ -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(); diff --git a/inc/field/urgencyfield.class.php b/inc/field/urgencyfield.class.php index 940a13ba5..1fcaf5edb 100644 --- a/inc/field/urgencyfield.class.php +++ b/inc/field/urgencyfield.class.php @@ -36,6 +36,7 @@ use Html; use Session; use Ticket; +use GlpiPlugin\Formcreator\Exception\ComparisonException; class UrgencyField extends PluginFormcreatorAbstractField { @@ -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; } diff --git a/inc/fieldinterface.class.php b/inc/fieldinterface.class.php index cb42bab7a..1deaabcd3 100644 --- a/inc/fieldinterface.class.php +++ b/inc/fieldinterface.class.php @@ -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 ? * diff --git a/inc/fields.class.php b/inc/fields.class.php index 3e9e63d46..59cc1880e 100644 --- a/inc/fields.class.php +++ b/inc/fields.class.php @@ -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; } } } diff --git a/tests/3-unit/PluginFormcreatorCondition.php b/tests/3-unit/PluginFormcreatorCondition.php index d6936df3c..da390125f 100644 --- a/tests/3-unit/PluginFormcreatorCondition.php +++ b/tests/3-unit/PluginFormcreatorCondition.php @@ -59,6 +59,7 @@ public function testGetEnumShowCondition() { '6' => '≥', '7' => 'is visible', '8' => 'is not visible', + '9' => 'regular expression matches' ]); } diff --git a/tests/fixture/PluginFormcreatorDependentField.php b/tests/fixture/PluginFormcreatorDependentField.php index c5c16533f..e1dd37149 100644 --- a/tests/fixture/PluginFormcreatorDependentField.php +++ b/tests/fixture/PluginFormcreatorDependentField.php @@ -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; }