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

fix(display): check data after reload from self-service #543

Merged
23 changes: 23 additions & 0 deletions ajax/container_display_condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@
$status_override = new PluginFieldsContainerDisplayCondition();
$status_override->getFromDB($_GET['id']);
$status_override->showForm($_GET['id'], $_GET);
} else if ($_GET['action'] === 'get_html_fields') {
$c_id = $_GET['c_id'];
$itemtype = $_GET['itemtype'];
$items_id = $_GET['items_id'];
$type = $_GET['type'];
$subtype = $_GET['subtype'];
$input = $_GET['values'];

$item = new $itemtype();
$item->getFromDB($items_id);

$display_condition = new PluginFieldsContainerDisplayCondition();
if ($display_condition->computeDisplayContainer($item, $c_id, $input)) {
PluginFieldsField::showDomContainer(
$c_id,
$itemtype,
$items_id,
$type,
$subtype
);
} else {
echo "";
}
}
} else if (isset($_POST['action'])) {
if ($_POST['action'] === 'get_itemtype_so') {
Expand Down
23 changes: 15 additions & 8 deletions inc/containerdisplaycondition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public static function removeBlackListedOption($array, $itemtype_class)
}


public function computeDisplayContainer($item, $container_id)
public function computeDisplayContainer($item, $container_id, $params = null)
stonebuzz marked this conversation as resolved.
Show resolved Hide resolved
{
//load all condition for itemtype and container
$displayCondition = new self();
Expand All @@ -372,7 +372,7 @@ public function computeDisplayContainer($item, $container_id)
$display = true;
foreach ($found_dc as $data) {
$displayCondition->getFromDB($data['id']);
$result = $displayCondition->checkCondition($item);
$result = $displayCondition->checkCondition($item, $params);
stonebuzz marked this conversation as resolved.
Show resolved Hide resolved
if (!$result) {
return $result;
}
Expand All @@ -386,42 +386,49 @@ public function computeDisplayContainer($item, $container_id)
}


public function checkCondition($item)
public function checkCondition($item, $params = null)
stonebuzz marked this conversation as resolved.
Show resolved Hide resolved
{
$value = $this->fields['value'];
$condition = $this->fields['condition'];
$searchOption = Search::getOptions(get_class($item))[$this->fields['search_option']];

//if from self-service form
if ($params != null) {
$object_fields = $params;
} else {
$object_fields = $item->fields;
}
stonebuzz marked this conversation as resolved.
Show resolved Hide resolved

switch ($condition) {
case self::SHOW_CONDITION_EQ:
// '='
if ($value == $item->fields[$searchOption['linkfield']]) {
if ($value == $object_fields[$searchOption['linkfield']]) {
return false;
}
break;
case self::SHOW_CONDITION_NE:
// '≠'
if ($value != $item->fields[$searchOption['linkfield']]) {
if ($value != $object_fields[$searchOption['linkfield']]) {
return false;
}
break;
case self::SHOW_CONDITION_LT:
// '<';
if ($item->fields[$searchOption['linkfield']] > $value) {
if ($object_fields[$searchOption['linkfield']] > $value) {
return false;
}
break;
case self::SHOW_CONDITION_GT:
//'>';
if ($item->fields[$searchOption['linkfield']] > $value) {
if ($object_fields[$searchOption['linkfield']] > $value) {
return false;
}
break;
case self::SHOW_CONDITION_REGEX:
//'regex';
if (self::checkRegex($value)) {
$value = Sanitizer::unsanitize($value);
if (preg_match_all($value . "i", $item->fields[$searchOption['linkfield']]) > 0) {
if (preg_match_all($value . "i", $object_fields[$searchOption['linkfield']]) > 0) {
return false;
}
}
Expand Down
42 changes: 40 additions & 2 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ public static function showForTabContainer($c_id, $items_id, $itemtype)
*
* @return void
*/
private static function showDomContainer($c_id, $itemtype, $items_id, $type = "dom", $subtype = "")
public static function showDomContainer($c_id, $itemtype, $items_id, $type = "dom", $subtype = "")
{

if ($c_id !== false) {
Expand Down Expand Up @@ -750,8 +750,45 @@ public static function showForTab($params)
return false;
}

//JS to trigger any change and check if container need to be display or not
$ajax_url = Plugin::getWebDir('fields') . '/ajax/container_display_condition.php';
$items_id = $item->getID() ? $item->getID() : 0;
stonebuzz marked this conversation as resolved.
Show resolved Hide resolved
echo Html::scriptBlock(<<<JAVASCRIPT

function checkContainerVisibility(){
var data = $('#itil-form').serializeArray().reduce(function(obj, item) {
cedric-anne marked this conversation as resolved.
Show resolved Hide resolved
obj[item.name] = item.value;
return obj;
}, {});

$.ajax({
url : '{$ajax_url}',
type : 'GET',
data : {
'action' : 'get_html_fields',
'c_id' : {$c_id},
'itemtype' : '{$item::getType()}',
'items_id' : {$items_id},
'type' : '{$type}',
'subtype' : '{$subtype}',
'values' : data
},
success : function(data) {
$("#fields_container").html(data);
}
});
}

//do not check select DOM -> reload page is triggered on change
$('#itil-form').on('keyup change paste', 'input', function(){
checkContainerVisibility();
});
JAVASCRIPT
);

echo "<div id='fields_container'>";
$display_condition = new PluginFieldsContainerDisplayCondition();
if ($display_condition->computeDisplayContainer($item, $c_id)) {
if ($display_condition->computeDisplayContainer($item, $c_id, $params['options'])) {
self::showDomContainer(
$c_id,
$item::getType(),
Expand All @@ -760,6 +797,7 @@ public static function showForTab($params)
$subtype
);
}
echo "</div>";
}

public static function prepareHtmlFields(
Expand Down