(.+)#', $value, $results)) {
- $value = str_replace($results[0][0], '', $value);
+ if (!isset($params['subtype'])) {
+ $params['subtype'] = null;
}
- }
- if (!isset($params['subtype'])) {
- $params['subtype'] = null;
+ $out .= Dropdown::showFromArray(
+ 'subtype',
+ $tabs,
+ ['value' => $params['subtype'],
+ 'width' => '100%',
+ 'display' => false,
+ ],
+ );
+ $out .= "";
}
-
- $out .= Dropdown::showFromArray(
- 'subtype',
- $tabs,
- ['value' => $params['subtype'],
- 'width' => '100%',
- 'display' => false,
- ],
- );
- $out .= "";
}
}
}
@@ -1090,7 +1168,7 @@ public static function getEntries($type = 'tab', $full = false): array
continue;
}
- $jsonitemtypes = json_decode($item['itemtypes']);
+ $jsonitemtypes = PluginFieldsToolbox::decodeJSONItemtypes($item['itemtypes']);
//show more info or not
foreach ($jsonitemtypes as $v) {
if ($full) {
@@ -1130,8 +1208,10 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false)
]);
foreach ($iterator as $data) {
- $jsonitemtype = json_decode($data['itemtypes']);
- $itemtypes = array_merge($itemtypes, $jsonitemtype);
+ $jsonitemtype = PluginFieldsToolbox::decodeJSONItemtypes($data['itemtypes']);
+ if (is_array($jsonitemtype)) {
+ $itemtypes = array_merge($itemtypes, $jsonitemtype);
+ }
}
return $itemtypes;
@@ -1161,7 +1241,7 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
if (!$item->isEntityAssign() || in_array($item->fields['entities_id'], $entities)) {
$display_condition = new PluginFieldsContainerDisplayCondition();
if ($display_condition->computeDisplayContainer($item, $data['id'])) {
- $tabs_entries[$tab_name] = $data['label'];
+ $tabs_entries[$tab_name] = self::createTabEntry($data['label'], 0, null, PluginFieldsContainer::getIcon());
}
}
}
@@ -1183,7 +1263,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
//retrieve container for current tab
$container = new self();
- $found_c = $container->find(['type' => 'tab', 'name' => Sanitizer::sanitize($tabnum), 'is_active' => 1]);
+ $found_c = $container->find(['type' => 'tab', 'name' => $tabnum, 'is_active' => 1]);
foreach ($found_c as $data) {
$dataitemtypes = json_decode($data['itemtypes']);
if (in_array(get_class($item), $dataitemtypes) != false) {
@@ -1252,28 +1332,41 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false)
}
}
- if ($exist === false) {
- // add fields data
- $obj->add($data);
- } else {
- // update fields data
- $data['id'] = $obj->fields['id'];
- $obj->update($data);
- }
+ $container_obj = new PluginFieldsContainer();
+ $container_obj->getFromDB($data['plugin_fields_containers_id']);
- // Add files and images for richtext fields
- $this->addRichTextFiles($obj);
+ $items_id = $data['items_id'];
+ $classname = self::getClassname($itemtype, $container_obj->fields['name']);
- //construct history on itemtype object (Historical tab)
- self::constructHistory(
- $obj->input['plugin_fields_containers_id'],
- $items_id,
- $itemtype,
- $obj->input,
- $obj,
- );
+ $dbu = new DbUtils();
+ $obj = $dbu->getItemForItemtype($classname);
- return true;
+ if ($obj !== false) {
+ if ($obj->getFromDBByCrit(['items_id' => $items_id]) === false) {
+ // add fields data
+ $obj->add($data);
+ } else {
+ // update fields data
+ $data['id'] = $obj->fields['id'];
+ $obj->update($data);
+ }
+
+ // Add files and images for richtext fields
+ $this->addRichTextFiles($obj);
+
+ //construct history on itemtype object (Historical tab)
+ self::constructHistory(
+ $obj->input['plugin_fields_containers_id'],
+ $items_id,
+ $itemtype,
+ $obj->input,
+ $obj,
+ );
+
+ return true;
+ }
+
+ return false;
}
private function addRichTextFiles(CommonDBTM $object): void
@@ -1325,7 +1418,8 @@ public static function constructHistory(
$field_obj
) {
// Don't log few itemtypes
- $obj = new $itemtype();
+ $dbu = new DbUtils();
+ $obj = $dbu->getItemForItemtype($itemtype);
if ($obj->dohistory == false) {
return;
}
@@ -1477,7 +1571,8 @@ public static function validateValues($data, $itemtype, $massiveaction)
if ($container->fields['type'] === 'dom') {
$status_value = $data[$status_field_name] ?? null;
} else {
- $relatedItem = new $itemtype();
+ $dbu = new DbUtils();
+ $relatedItem = $dbu->getItemForItemtype($itemtype);
$status_value = $relatedItem->fields[$status_field_name] ?? null;
}
// Apply status overrides
@@ -1600,7 +1695,7 @@ public static function findContainer($itemtype, $type = 'tab', $subtype = '')
}
foreach ($itemtypes as $data) {
- $dataitemtypes = json_decode($data['itemtypes']);
+ $dataitemtypes = PluginFieldsToolbox::decodeJSONItemtypes($data['itemtypes']);
if (in_array($itemtype, $dataitemtypes) != false) {
$id = $data['id'];
}
@@ -1906,7 +2001,7 @@ public static function getAddSearchOptions($itemtype, $containers_id = false)
'glpi_plugin_fields_containers.label AS container_label',
(
Session::isCron()
- ? new QueryExpression(sprintf('%s AS %s', READ + CREATE, $DB->quoteName('right')))
+ ? new \Glpi\DBAL\QueryExpression(sprintf('%s AS %s', READ + CREATE, $DB->quoteName('right')))
: 'glpi_plugin_fields_profiles.right'
),
],
@@ -2156,7 +2251,7 @@ public function prepareInputForClone($input)
if (array_key_exists('itemtypes', $input) && !empty($input['itemtypes'])) {
// $input has been transformed with `Toolbox::addslashes_deep()`, and `self::prepareInputForAdd()`
// is expecting an array, so it have to be unslashed then json decoded.
- $input['itemtypes'] = json_decode(Sanitizer::dbUnescape($input['itemtypes']));
+ $input['itemtypes'] = json_decode($input['itemtypes']);
} else {
unset($input['itemtypes']);
}
diff --git a/inc/containerdisplaycondition.class.php b/inc/containerdisplaycondition.class.php
index bd108987..3ce821ff 100644
--- a/inc/containerdisplaycondition.class.php
+++ b/inc/containerdisplaycondition.class.php
@@ -77,7 +77,7 @@ public static function installBaseData(Migration $migration, $version)
PRIMARY KEY (`id`),
KEY `plugin_fields_containers_id_itemtype` (`plugin_fields_containers_id`, `itemtype`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
- $DB->doQuery($query) or die($DB->error());
+ $DB->doQuery($query);
}
return true;
@@ -163,6 +163,8 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
return self::createTabEntry(
self::getTypeName(Session::getPluralNumber()),
countElementsInTable(self::getTable(), ['plugin_fields_containers_id' => $item->getID()]),
+ null,
+ 'ti ti-eye-off',
);
}
@@ -232,6 +234,9 @@ public static function getFieldName($so_id, $itemtype)
public static function showItemtypeFieldForm($itemtype)
{
+ /** @var array $CFG_GLPI */
+ global $CFG_GLPI;
+
$rand = mt_rand();
$out = '';
$out .= Dropdown::showFromArray('search_option', self::removeBlackListedOption(Search::getOptions($itemtype), $itemtype), ['display_emptychoice' => true, 'display' => false, 'rand' => $rand]);
@@ -239,7 +244,7 @@ public static function showItemtypeFieldForm($itemtype)
$out .= Ajax::updateItemOnSelectEvent(
'dropdown_search_option' . $rand,
'results_condition',
- Plugin::getWebDir('fields') . '/ajax/container_display_condition.php',
+ $CFG_GLPI['root_doc'] . '/plugins/fields/ajax/container_display_condition.php',
[
'search_option_id' => '__VALUE__',
'itemtype' => $itemtype,
@@ -292,7 +297,8 @@ public static function showSearchOptionCondition($searchoption_id, $itemtype, ?s
case 'impact':
case 'urgency':
case 'priority':
- $twig_params['item'] = new $itemtype();
+ $dbu = new DbUtils();
+ $twig_params['item'] = $dbu->getItemForItemtype($itemtype);
$twig_params['itemtype_field'] = $so['field'];
break;
case 'global_validation':
@@ -316,7 +322,8 @@ public static function getRawValue($searchoption_id, $itemtype, $value)
if ($so['datatype'] == 'dropdown' || ($so['datatype'] == 'itemlink' && $so['table'] !== $itemtypetable)) {
$dropdown_itemtype = getItemTypeForTable($so['table']);
- $dropdown = new $dropdown_itemtype();
+ $dbu = new DbUtils();
+ $dropdown = $dbu->getItemForItemtype($dropdown_itemtype);
if ($dropdown->getFromDB($value)) {
$raw_value = $dropdown->fields['name'];
}
@@ -350,7 +357,8 @@ public static function getRawValue($searchoption_id, $itemtype, $value)
public static function removeBlackListedOption($array, $itemtype_class)
{
- $itemtype_object = new $itemtype_class();
+ $dbu = new DbUtils();
+ $itemtype_object = $dbu->getItemForItemtype($itemtype_class);
$allowed_so = [];
//remove "Common"
@@ -457,7 +465,6 @@ public function checkCondition($item)
case self::SHOW_CONDITION_REGEX:
//'regex';
if (self::checkRegex($value)) {
- $value = Sanitizer::unsanitize($value);
if (preg_match_all($value . 'i', $fields[$searchOption['linkfield']]) > 0) {
return false;
}
diff --git a/inc/field.class.php b/inc/field.class.php
index 7f5383d6..1acb2c88 100644
--- a/inc/field.class.php
+++ b/inc/field.class.php
@@ -92,24 +92,26 @@ public static function installBaseData(Migration $migration, $version)
KEY `is_active` (`is_active`),
KEY `is_readonly` (`is_readonly`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
- $DB->doQuery($query) or die($DB->error());
+ if (!$DB->doQuery($query)) {
+ throw new \RuntimeException('Error creating plugin_fields_fields table: ' . $DB->error());
+ }
}
$migration->displayMessage("Updating $table");
if (!$DB->fieldExists($table, 'is_active')) {
- $migration->addField($table, 'is_active', 'bool', ['value' => 1]);
+ $migration->addField($table, 'is_active', 'bool', ['value' => '1']);
$migration->addKey($table, 'is_active', 'is_active');
}
if (!$DB->fieldExists($table, 'is_readonly')) {
- $migration->addField($table, 'is_readonly', 'bool', ['default' => false]);
+ $migration->addField($table, 'is_readonly', 'bool', ['default' => 'false']);
$migration->addKey($table, 'is_readonly', 'is_readonly');
}
if (!$DB->fieldExists($table, 'mandatory')) {
- $migration->addField($table, 'mandatory', 'bool', ['value' => 0]);
+ $migration->addField($table, 'mandatory', 'bool', ['value' => '0']);
}
if (!$DB->fieldExists($table, 'multiple')) {
- $migration->addField($table, 'multiple', 'bool', ['value' => 0]);
+ $migration->addField($table, 'multiple', 'bool', ['value' => '0']);
}
//increase the size of column 'type' (25 to 255)
@@ -313,7 +315,7 @@ public function prepareInputForAdd($input)
}
if (isset($input['allowed_values'])) {
- $input['allowed_values'] = Sanitizer::dbEscape(json_encode($input['allowed_values']));
+ $input['allowed_values'] = json_encode($input['allowed_values']);
}
return $input;
@@ -431,7 +433,7 @@ public function post_purgeItem()
$DB->update(
$table,
[
- 'ranking' => new QueryExpression($DB->quoteName('ranking') . ' - 1'),
+ 'ranking' => new \Glpi\DBAL\QueryExpression($DB->quoteName('ranking') . ' - 1'),
],
[
'plugin_fields_containers_id' => $old_container,
@@ -510,7 +512,7 @@ public function getNextRanking()
global $DB;
$iterator = $DB->request([
- 'SELECT' => new \QueryExpression(
+ 'SELECT' => new \Glpi\DBAL\QueryExpression(
'max(' . $DB->quoteName('ranking') . ') AS ' . $DB->quoteName('rank'),
),
'FROM' => self::getTable(),
@@ -547,6 +549,8 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
self::getTable(),
['plugin_fields_containers_id' => $item->getID()],
),
+ null,
+ 'ti ti-forms',
);
}
@@ -636,7 +640,7 @@ public function showSummary($container)
echo '| ';
$label = !empty($this->fields['label']) ? $this->fields['label'] : NOT_AVAILABLE;
- echo "getID()}'>{$label}";
+ echo "getID()}'>{$label}";
echo ' | ';
echo '' . $fields_type[$this->fields['type']] . ' | ';
echo '' ;
@@ -815,6 +819,9 @@ public function showForm($ID, $options = [])
public static function showForTabContainer($c_id, $item)
{
+ /** @var array $CFG_GLPI */
+ global $CFG_GLPI;
+
//profile restriction
$right = PluginFieldsProfile::getRightOnContainer($_SESSION['glpiactiveprofile']['id'], $c_id);
if ($right < READ) {
@@ -825,7 +832,7 @@ public static function showForTabContainer($c_id, $item)
//get fields for this container
$field_obj = new self();
$fields = $field_obj->find(['plugin_fields_containers_id' => $c_id, 'is_active' => 1], 'ranking');
- echo " |