Skip to content

Commit 4a06ac1

Browse files
committed
Added display conditions for fields
They work same as conditions for containers, I just took their implementation and added it to fields!
1 parent 8510362 commit 4a06ac1

10 files changed

+1106
-17
lines changed

ajax/field_display_condition.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Fields plugin for GLPI
6+
* -------------------------------------------------------------------------
7+
*
8+
* LICENSE
9+
*
10+
* This file is part of Fields.
11+
*
12+
* Fields is free software; you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation; either version 2 of the License, or
15+
* (at your option) any later version.
16+
*
17+
* Fields is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU General Public License
23+
* along with Fields. If not, see <http://www.gnu.org/licenses/>.
24+
* -------------------------------------------------------------------------
25+
* @copyright Copyright (C) 2013-2023 by Fields plugin team.
26+
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
27+
* @link https://github.com/pluginsGLPI/fields
28+
* -------------------------------------------------------------------------
29+
*/
30+
31+
include('../../../inc/includes.php');
32+
Session::checkLoginUser();
33+
34+
if (isset($_GET['action'])) {
35+
if ($_GET['action'] === 'get_add_form') {
36+
$status_override = new PluginFieldsFieldDisplayCondition();
37+
$status_override->showForm(0, $_GET);
38+
} elseif ($_GET['action'] === 'get_edit_form') {
39+
$status_override = new PluginFieldsFieldDisplayCondition();
40+
$status_override->getFromDB($_GET['id']);
41+
$status_override->showForm($_GET['id'], $_GET);
42+
}
43+
} elseif (isset($_POST['action'])) {
44+
if ($_POST['action'] === 'get_itemtype_so') {
45+
if (isset($_POST['itemtype']) && class_exists($_POST['itemtype'])) {
46+
echo PluginFieldsFieldDisplayCondition::showItemtypeFieldForm($_POST['itemtype']) ;
47+
} else {
48+
echo '';
49+
}
50+
} elseif ($_POST['action'] === 'get_condition_switch_so') {
51+
if (isset($_POST['search_option_id']) && (isset($_POST['itemtype']) && class_exists($_POST['itemtype']))) {
52+
echo PluginFieldsFieldDisplayCondition::showSearchOptionCondition($_POST['search_option_id'], $_POST['itemtype']);
53+
} else {
54+
echo '';
55+
}
56+
}
57+
} else {
58+
http_response_code(400);
59+
die();
60+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Fields plugin for GLPI
6+
* -------------------------------------------------------------------------
7+
*
8+
* LICENSE
9+
*
10+
* This file is part of Fields.
11+
*
12+
* Fields is free software; you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation; either version 2 of the License, or
15+
* (at your option) any later version.
16+
*
17+
* Fields is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU General Public License
23+
* along with Fields. If not, see <http://www.gnu.org/licenses/>.
24+
* -------------------------------------------------------------------------
25+
* @copyright Copyright (C) 2013-2023 by Fields plugin team.
26+
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
27+
* @link https://github.com/pluginsGLPI/fields
28+
* -------------------------------------------------------------------------
29+
*/
30+
31+
include('../../../inc/includes.php');
32+
Session::checkRight('config', READ);
33+
34+
$status_override = new PluginFieldsFieldDisplayCondition();
35+
if (isset($_POST['add'])) {
36+
$_POST["entities_id"] = 0;
37+
$_POST["is_recursive"] = 0;
38+
$status_override->check(-1, CREATE, $_POST);
39+
$status_override->add($_POST);
40+
Html::back();
41+
} elseif (isset($_POST['update'])) {
42+
$status_override->check($_POST['id'], UPDATE);
43+
$status_override->update($_POST);
44+
Html::back();
45+
} elseif (isset($_POST['delete'])) {
46+
$status_override->check($_POST['id'], PURGE);
47+
$status_override->delete([
48+
'id' => $_POST['id'],
49+
]);
50+
Html::back();
51+
}
52+
Html::back();

hook.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function plugin_fields_install()
6767
PluginFieldsContainerDisplayCondition::class,
6868
PluginFieldsDropdown::class,
6969
PluginFieldsField::class,
70+
PluginFieldsFieldDisplayCondition::class,
7071
PluginFieldsLabelTranslation::class,
7172
PluginFieldsProfile::class,
7273
PluginFieldsStatusOverride::class,
@@ -132,6 +133,7 @@ function plugin_fields_uninstall()
132133
'PluginFieldsProfile',
133134
'PluginFieldsStatusOverride',
134135
'PluginFieldsContainerDisplayCondition',
136+
'PluginFieldsFieldDisplayCondition',
135137
];
136138

137139
foreach ($classesToUninstall as $class) {

inc/container.class.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -715,12 +715,18 @@ public function pre_deleteItem()
715715
'plugin_fields_containers_id' => $this->fields['id'],
716716
]);
717717

718-
//delete display condition
718+
//delete container display condition
719719
$field_obj = new PluginFieldsContainerDisplayCondition();
720720
$field_obj->deleteByCriteria([
721721
'plugin_fields_containers_id' => $this->fields['id'],
722722
]);
723723

724+
//delete field display condition
725+
$field_obj = new PluginFieldsFieldDisplayCondition();
726+
$field_obj->deleteByCriteria([
727+
'plugin_fields_fields_id' => $this->fields['id'],
728+
]);
729+
724730
//delete profiles
725731
$profile_obj = new PluginFieldsProfile();
726732
$profile_obj->deleteByCriteria([
@@ -1102,7 +1108,7 @@ public static function getEntries($type = 'tab', $full = false): array
11021108
}
11031109
}
11041110
}
1105-
1111+
11061112
return $itemtypes;
11071113
}
11081114

@@ -1202,12 +1208,12 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
12021208
*
12031209
* @return boolean
12041210
*/
1205-
public function updateFieldsValues($data, $itemtype, $massiveaction = false)
1211+
public function updateFieldsValues($data, $item, $massiveaction = false)
12061212
{
12071213
/** @var DBmysql $DB */
12081214
global $DB;
12091215

1210-
if (self::validateValues($data, $itemtype, $massiveaction) === false) {
1216+
if (self::validateValues($data, $item, $massiveaction) === false) {
12111217
return false;
12121218
}
12131219

@@ -1236,7 +1242,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false)
12361242
$container_obj->getFromDB($data['plugin_fields_containers_id']);
12371243

12381244
$items_id = $data['items_id'];
1239-
$classname = self::getClassname($itemtype, $container_obj->fields['name']);
1245+
$classname = self::getClassname($item->getType(), $container_obj->fields['name']);
12401246

12411247
$obj = new $classname();
12421248
if ($obj->getFromDBByCrit(['items_id' => $items_id]) === false) {
@@ -1255,7 +1261,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false)
12551261
self::constructHistory(
12561262
$obj->input['plugin_fields_containers_id'],
12571263
$items_id,
1258-
$itemtype,
1264+
$item->getType(),
12591265
$obj->input,
12601266
$obj,
12611267
);
@@ -1442,7 +1448,7 @@ public static function constructHistory(
14421448
*
14431449
* @return boolean
14441450
*/
1445-
public static function validateValues($data, $itemtype, $massiveaction)
1451+
public static function validateValues($data, $item, $massiveaction)
14461452
{
14471453
/** @var DBmysql $DB */
14481454
global $DB;
@@ -1460,16 +1466,15 @@ public static function validateValues($data, $itemtype, $massiveaction)
14601466
]);
14611467

14621468
$status_value = null;
1463-
$status_field_name = PluginFieldsStatusOverride::getStatusFieldName($itemtype);
1469+
$status_field_name = PluginFieldsStatusOverride::getStatusFieldName($item->getType());
14641470
if ($container->fields['type'] === 'dom') {
14651471
$status_value = $data[$status_field_name] ?? null;
14661472
} else {
1467-
$relatedItem = new $itemtype();
1468-
$status_value = $relatedItem->fields[$status_field_name] ?? null;
1473+
$status_value = $item->fields[$status_field_name] ?? null;
14691474
}
14701475
// Apply status overrides
14711476
$status_overrides = $status_value !== null
1472-
? PluginFieldsStatusOverride::getOverridesForItemtypeAndStatus($container->getID(), $itemtype, $status_value)
1477+
? PluginFieldsStatusOverride::getOverridesForItemtypeAndStatus($container->getID(), $item->getType(), $status_value)
14731478
: [];
14741479

14751480
foreach ($status_overrides as $status_override) {
@@ -1516,10 +1521,13 @@ public static function validateValues($data, $itemtype, $massiveaction)
15161521
//translate label
15171522
$field['itemtype'] = PluginFieldsField::getType();
15181523
$field['label'] = PluginFieldsLabelTranslation::getLabelFor($field);
1519-
1524+
1525+
$dc = new PluginFieldsFieldDisplayCondition();
1526+
15201527
// Check mandatory fields
15211528
if (
15221529
$field['mandatory'] == 1
1530+
&& $dc->computeDisplayField($item, $field['id'])
15231531
&& (
15241532
empty($value)
15251533
|| (($field['type'] === 'dropdown' || preg_match('/^dropdown-.+/i', $field['type'])) && $value == 0)
@@ -1620,7 +1628,7 @@ public static function postItemAdd(CommonDBTM $item)
16201628
$data['entities_id'] = $item->isEntityAssign() ? $item->getEntityID() : 0;
16211629
//update data
16221630
$container = new self();
1623-
if ($container->updateFieldsValues($data, $item->getType(), isset($_REQUEST['massiveaction']))) {
1631+
if ($container->updateFieldsValues($data, $item, isset($_REQUEST['massiveaction']))) {
16241632
return true;
16251633
}
16261634

@@ -1724,7 +1732,7 @@ public static function preItem(CommonDBTM $item)
17241732
}
17251733

17261734
if (false !== ($data = self::populateData($c_id, $item))) {
1727-
if (self::validateValues($data, $item::getType(), isset($_REQUEST['massiveaction'])) === false) {
1735+
if (self::validateValues($data, $item, isset($_REQUEST['massiveaction'])) === false) {
17281736
$item->input = [];
17291737

17301738
return false;

inc/containerdisplaycondition.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ public function showForm($ID, array $options = [])
562562
? []
563563
: self::removeBlackListedOption(Search::getOptions($this->fields['itemtype']), $this->fields['itemtype']),
564564
];
565+
565566
TemplateRenderer::getInstance()->display('@fields/forms/container_display_condition.html.twig', $twig_params);
566567

567568
return true;

inc/field.class.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ public function defineTabs($options = [])
563563
$ong = [];
564564
$this->addDefaultFormTab($ong);
565565
$this->addStandardTab('PluginFieldsLabelTranslation', $ong, $options);
566+
$this->addStandardTab('PluginFieldsFieldDisplayCondition', $ong, $options);
566567

567568
return $ong;
568569
}
@@ -704,7 +705,7 @@ public function showSummary($container)
704705
echo '</div>';
705706
echo Html::scriptBlock('$(document).ready(function() {
706707
redipsInit()
707-
});');
708+
});');
708709
}
709710

710711
public function showForm($ID, $options = [])
@@ -809,7 +810,7 @@ public function showForm($ID, $options = [])
809810
echo '</tr>';
810811

811812
$this->showFormButtons($options);
812-
813+
813814
return true;
814815
}
815816

@@ -841,7 +842,7 @@ public static function showForTabContainer($c_id, $item)
841842

842843
echo '</table>';
843844
Html::closeForm();
844-
845+
845846
return true;
846847
}
847848

@@ -857,6 +858,7 @@ public static function showForTabContainer($c_id, $item)
857858
*/
858859
public static function showDomContainer($id, $item, $type = 'dom', $subtype = '', $field_options = [])
859860
{
861+
global $DB;
860862
if ($id != 0) {
861863
//get fields for this container
862864
$field_obj = new self();
@@ -867,6 +869,31 @@ public static function showDomContainer($id, $item, $type = 'dom', $subtype = ''
867869
],
868870
'ranking',
869871
);
872+
873+
$dc = new PluginFieldsFieldDisplayCondition();
874+
$fields_to_pass = [];
875+
876+
foreach($fields as $field)
877+
{
878+
$id = $field['id'];
879+
880+
$iterator = $DB->request([
881+
'SELECT' => '*',
882+
'FROM' => PluginFieldsFieldDisplayCondition::getTable(),
883+
'WHERE' => [
884+
'plugin_fields_fields_id' => $id,
885+
],
886+
]);
887+
888+
$res = $dc->computeDisplayField($item, $id, true);
889+
//if res is true - we display field
890+
if($res)
891+
{
892+
array_push($fields_to_pass, $field);
893+
}
894+
}
895+
896+
$fields = $fields_to_pass;
870897
} else {
871898
$fields = [];
872899
}

0 commit comments

Comments
 (0)