Skip to content

Commit 3555e97

Browse files
update: findContainers method to use entityId instead of itemId
update: showForTab method fix: dropdown field
1 parent c69f103 commit 3555e97

File tree

3 files changed

+143
-188
lines changed

3 files changed

+143
-188
lines changed

inc/container.class.php

Lines changed: 134 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,85 +1588,87 @@ public static function findContainer($itemtype, $type = 'tab', $subtype = '')
15881588
return $id;
15891589
}
15901590

1591-
public static function findContainers($itemtype, $type = 'tab', $subtype = '', $itemId = '')
1591+
/**
1592+
* Find containers for a specific itemtype, type, subtype and entity id
1593+
*
1594+
* @param string $itemtype Itemtype GLPI
1595+
* @param string $type Type of container (tab, dom, domtab)
1596+
* @param string $subtype
1597+
* @param integer $entityId Entity ID default is 0 (root entity)
1598+
*
1599+
* @return array List of container IDs
1600+
*/
1601+
public static function findContainers($itemtype, $type = 'tab', $subtype = '', $entityId = 0): array
15921602
{
15931603
/** @var DBmysql $DB */
15941604
global $DB;
1595-
$ids = [];
15961605

1597-
if (!empty($itemtype) && !empty($itemId) && class_exists($itemtype)) {
1598-
1599-
$obj = new $itemtype();
1600-
if ($obj->getFromDB($itemId)) {
1601-
1602-
$entityId = $obj->fields['entities_id'] ?? 0;
1603-
$entityIds = getAncestorsOf("glpi_entities", $entityId);
1604-
$entityIds[] = $entityId; // Add entity obj itself to the list
1605-
$glpiActiveEntities = $_SESSION['glpiactiveentities'] ?? 0;
1606+
if ($itemtype === '') {
1607+
return [];
1608+
}
16061609

1607-
$entityRestriction = getEntitiesRestrictCriteria('', '', $glpiActiveEntities, true, true);
1610+
$entitiesIds = getAncestorsOf("glpi_entities", (string) $entityId);
1611+
$entitiesIds[] = $entityId; // Add entity active itself to the list
16081612

1609-
$where = [
1610-
'is_active' => 1,
1611-
'type' => $type,
1612-
new \QueryExpression("JSON_CONTAINS(itemtypes, " . $DB->quote('"' . $itemtype . '"') . ")"),
1613-
'AND' => [
1614-
'OR' => [
1615-
[
1616-
'is_recursive' => 1,
1617-
'entities_id' => $entityIds,
1618-
],
1619-
[
1620-
'is_recursive' => 0,
1621-
],
1622-
],
1613+
$where = [
1614+
'is_active' => 1,
1615+
'type' => $type,
1616+
new \QueryExpression("JSON_CONTAINS(itemtypes, " . $DB->quote('"' . $itemtype . '"') . ")"),
1617+
'AND' => [
1618+
'OR' => [
1619+
[
1620+
'is_recursive' => 1,
1621+
'entities_id' => $entitiesIds,
16231622
],
1624-
];
1623+
[
1624+
'is_recursive' => 0,
1625+
'entities_id' => $entityId,
1626+
],
1627+
],
1628+
],
1629+
];
16251630

1626-
if ($subtype !== '') {
1627-
if ($subtype === $itemtype . '$main') {
1628-
$where['type'] = 'dom';
1629-
} else {
1630-
$where['type'] = ['!=', 'dom'];
1631-
$where['subtype'] = $subtype;
1632-
}
1633-
} else {
1634-
$where['type'] = $type;
1635-
}
1631+
if ($subtype !== '') {
1632+
if ($subtype === $itemtype . '$main') {
1633+
$where['type'] = 'dom';
1634+
} else {
1635+
$where['type'] = ['!=', 'dom'];
1636+
$where['subtype'] = $subtype;
1637+
}
1638+
} else {
1639+
$where['type'] = $type;
1640+
}
16361641

1637-
if (!empty($entityRestriction)) {
1638-
$allowedEntities = [];
1639-
foreach ($entityRestriction as $restriction) {
1640-
if (isset($restriction['entities_id']) && is_array($restriction['entities_id'])) {
1641-
$allowedEntities = array_merge($allowedEntities, $restriction['entities_id']);
1642-
}
1643-
}
1644-
if (!empty($allowedEntities)) {
1645-
$where['entities_id'] = $allowedEntities;
1646-
}
1642+
$entityRestriction = getEntitiesRestrictCriteria('', '', $entityId, true, true);
1643+
if (!empty($entityRestriction)) {
1644+
$allowedEntities = [];
1645+
foreach ($entityRestriction as $restriction) {
1646+
if (isset($restriction['entities_id']) && is_array($restriction['entities_id'])) {
1647+
$allowedEntities = array_merge($allowedEntities, $restriction['entities_id']);
16471648
}
1649+
}
1650+
if (!empty($allowedEntities)) {
1651+
$where['entities_id'] = $allowedEntities;
1652+
}
1653+
}
16481654

1649-
$iterator = $DB->request([
1650-
'SELECT' => 'id',
1651-
'FROM' => self::getTable(),
1652-
'WHERE' => $where,
1653-
]);
1654-
1655-
foreach ($iterator as $row) {
1656-
$containerId = (int) $row['id'];
1657-
1658-
//profiles restriction
1659-
if (isset($_SESSION['glpiactiveprofile']['id'])) {
1660-
$profileId = $_SESSION['glpiactiveprofile']['id'];
1661-
$right = PluginFieldsProfile::getRightOnContainer($profileId, $containerId);
1662-
if ($right < READ) {
1663-
continue;
1664-
}
1665-
}
1655+
$iterator = $DB->request([
1656+
'SELECT' => 'id',
1657+
'FROM' => self::getTable(),
1658+
'WHERE' => $where,
1659+
]);
16661660

1667-
$ids[] = $containerId;
1661+
$ids = [];
1662+
foreach ($iterator as $row) {
1663+
$containerId = (int) $row['id'];
1664+
1665+
if (isset($_SESSION['glpiactiveprofile']['id'])) {
1666+
$profileId = $_SESSION['glpiactiveprofile']['id'];
1667+
if (PluginFieldsProfile::getRightOnContainer($profileId, $containerId) < READ) {
1668+
continue;
16681669
}
16691670
}
1671+
$ids[] = $containerId;
16701672
}
16711673

16721674
return $ids;
@@ -1741,35 +1743,29 @@ public static function preItemUpdate(CommonDBTM $item)
17411743
*/
17421744
public static function preItem(CommonDBTM $item)
17431745
{
1744-
$type = 'dom';
1745-
if (isset($_REQUEST['_plugin_fields_type'])) {
1746-
$type = $_REQUEST['_plugin_fields_type'];
1747-
}
1748-
$subtype = '';
1749-
if ($type == 'domtab') {
1750-
$subtype = $_REQUEST['_plugin_fields_subtype'];
1751-
}
1746+
$type = $_REQUEST['_plugin_fields_type'] ?? 'dom';
1747+
$subtype = ($type === 'domtab') ? ($_REQUEST['_plugin_fields_subtype'] ?? '') : '';
1748+
1749+
$itemEntityId = $item->getEntityID();
1750+
$entityId = ($itemEntityId === -1) ? ($_SESSION['glpiactive_entity'] ?? 0) : $itemEntityId;
17521751

1753-
$containers = self::findContainers($item->getType(), $type, $subtype, $item->getID());
1752+
$containers = self::findContainers($item->getType(), $type, $subtype, $entityId);
17541753

17551754
$all_data = [];
17561755

17571756
foreach ($containers as $c_id) {
17581757

1759-
$loc_c = new PluginFieldsContainer();
1760-
$loc_c->getFromDB($c_id);
1761-
17621758
// check rights on $c_id
1763-
1764-
if (isset($_SESSION['glpiactiveprofile']['id']) && $_SESSION['glpiactiveprofile']['id'] != null && $c_id > 0) {
1759+
if (isset($_SESSION['glpiactiveprofile']['id'])) {
17651760
$right = PluginFieldsProfile::getRightOnContainer($_SESSION['glpiactiveprofile']['id'], $c_id);
1766-
if ($right > READ === false) { // Si le droit est insuffisant, on passe au container suivant
1767-
continue;
1761+
if ($right < READ) {
1762+
continue; // insufficient rights
17681763
}
1769-
} else {
1770-
continue;
17711764
}
17721765

1766+
$loc_c = new self();
1767+
$loc_c->getFromDB($c_id);
1768+
17731769
// need to check if container is usable on this object entity
17741770
$entities = [$loc_c->fields['entities_id']];
17751771
if ($loc_c->fields['is_recursive']) {
@@ -1781,11 +1777,12 @@ public static function preItem(CommonDBTM $item)
17811777
}
17821778

17831779
if ($item->isEntityAssign() && !in_array($item->getEntityID(), $entities)) {
1784-
continue;
1780+
continue; // not the right entity
17851781
}
17861782

17871783
if (false !== ($data = self::populateData($c_id, $item))) {
1788-
if (self::validateValues($data, $item->getType(), isset($_REQUEST['massiveaction'])) === false) {
1784+
if (!self::validateValues($data, $item->getType(), isset($_REQUEST['massiveaction']))) {
1785+
// if validation fails, we need to remove the data from the item input
17891786
$item->input = [];
17901787

17911788
return false;
@@ -1828,20 +1825,16 @@ public static function populateData($c_id, CommonDBTM $item)
18281825
];
18291826

18301827
// Add status so it can be used with status overrides
1831-
$status_field_name = PluginFieldsStatusOverride::getStatusFieldName($item->getType());
1832-
$data[$status_field_name] = null;
1833-
if (array_key_exists($status_field_name, $item->input) && $item->input[$status_field_name] !== '') {
1834-
$data[$status_field_name] = (int) $item->input[$status_field_name];
1835-
} elseif (array_key_exists($status_field_name, $item->fields) && $item->fields[$status_field_name] !== '') {
1836-
$data[$status_field_name] = (int) $item->fields[$status_field_name];
1837-
}
1828+
$statusField = PluginFieldsStatusOverride::getStatusFieldName($item->getType());
1829+
$data[$statusField] = $item->input[$statusField] ?? $item->fields[$statusField] ?? null;
18381830

18391831
$has_fields = false;
18401832
// Prefix for input names
18411833
$prefix = "plugin_fields_{$c_id}_";
18421834

18431835
foreach ($fields as $field) {
18441836
$base_name = $field['name'];
1837+
$isMulti = (bool) $field['multiple'];
18451838
if ($field['type'] == 'glpi_item') {
18461839
$itemtype_key = "itemtype_{$base_name}";
18471840
$items_id_key = "items_id_{$base_name}";
@@ -1850,9 +1843,9 @@ public static function populateData($c_id, CommonDBTM $item)
18501843
continue; // not a valid input
18511844
}
18521845

1853-
$has_fields = true;
18541846
$data[$itemtype_key] = $item->input[$itemtype_key];
18551847
$data[$items_id_key] = $item->input[$items_id_key];
1848+
$has_fields = true;
18561849

18571850
continue; // bypass unique field handling
18581851
}
@@ -1861,86 +1854,68 @@ public static function populateData($c_id, CommonDBTM $item)
18611854
// "plugin_fields_{$c_id}_{$base_name}"
18621855
if ($field['type'] === 'dropdown') {
18631856
// For dropdown fields, the input name is "plugin_fields_{$c_id}_{$base_name}dropdowns_id"
1864-
$input = $prefix . $base_name . "dropdowns_id";
1865-
if (isset($item->input[$input])) {
1866-
$has_fields = true;
1867-
$data[$base_name . "_dropdowns_id"] = $item->input[$input];
1868-
}
1869-
// If the field is a dropdown with multiple selection, we need to check if the input name is defined
1870-
elseif ($field['multiple']) {
1871-
$multiple_key = $input;
1872-
$multiple_defined = '_' . $multiple_key . '_defined';
1873-
if (isset($item->input[$multiple_key])) {
1874-
$has_fields = true;
1875-
$data[$base_name . "_dropdowns_id"] = $item->input[$multiple_key];
1876-
} elseif (isset($item->input[$multiple_defined]) && $item->input[$multiple_defined]) {
1877-
$has_fields = true;
1878-
$data[$base_name . "_dropdowns_id"] = [];
1857+
$htmlKeyWithId = $prefix . $base_name . "dropdowns_id"; // html key in POST data with id
1858+
$htmlKeyNoId = "plugin_fields_{$base_name}dropdowns_id"; // html key in POST data without id
1859+
$colKey = 'plugin_fields_' . $base_name . 'dropdowns_id'; // column key in DB
1860+
1861+
if (array_key_exists($htmlKeyWithId, $item->input)) {
1862+
$data[$colKey] = $item->input[$htmlKeyWithId];
1863+
$has_fields = true;
1864+
} elseif (array_key_exists($htmlKeyNoId, $item->input)) {
1865+
$data[$colKey] = $item->input[$htmlKeyNoId];
1866+
$has_fields = true;
1867+
} elseif ($isMulti) {
1868+
$definedKeyWithId = '_' . $htmlKeyWithId . '_defined';
1869+
$definedKeyNoId = '_' . $htmlKeyNoId . '_defined';
1870+
if (!empty($item->input[$definedKeyWithId]) || !empty($item->input[$definedKeyNoId])) {
1871+
$data[$colKey] = [];
1872+
$has_fields = true;
18791873
}
18801874
}
18811875
continue;
18821876
}
18831877

18841878
// For fields standard, the input name is "plugin_fields_{$c_id}_{$base_name}"
1885-
$input = $prefix . $base_name;
1886-
if (isset($item->input[$input])) {
1887-
$has_fields = true;
1888-
// Before is_number check, help user to have a number correct, during a massive action of a number field
1889-
if ($field['type'] == 'number') {
1890-
$item->input[$input] = str_replace(',', '.', $item->input[$input]);
1879+
$htmlKeyWithId = $prefix . $base_name;
1880+
$htmlKeyNoId = "plugin_fields_{$base_name}";
1881+
1882+
$valuePresent = false;
1883+
$value = null;
1884+
if (array_key_exists($htmlKeyWithId, $item->input)) {
1885+
$value = $item->input[$htmlKeyWithId];
1886+
$valuePresent = true;
1887+
} elseif (array_key_exists($htmlKeyNoId, $item->input)) {
1888+
$value = $item->input[$htmlKeyNoId];
1889+
$valuePresent = true;
1890+
} elseif ($isMulti) {
1891+
$definedKeyWithId = '_' . $htmlKeyWithId . '_defined';
1892+
$definedKeyNoId = '_' . $htmlKeyNoId . '_defined';
1893+
if (!empty($item->input[$definedKeyWithId]) || !empty($item->input[$definedKeyNoId])) {
1894+
$value = [];
1895+
$valuePresent = true;
18911896
}
1892-
$data[$base_name] = $item->input[$input];
1897+
}
18931898

1894-
if ($field['type'] === 'richtext') {
1895-
$filename_input = "_" . $input;
1896-
$prefix_input = "_prefix_" . $input;
1897-
$tag_input = "_tag_" . $input;
1899+
if (!$valuePresent) {
1900+
continue; // not a valid input
1901+
}
18981902

1899-
$data[$filename_input] = $item->input[$filename_input] ?? [];
1900-
$data[$prefix_input] = $item->input[$prefix_input] ?? [];
1901-
$data[$tag_input] = $item->input[$tag_input] ?? [];
1902-
}
1903-
} else {
1904-
//the absence of the field in the input may be due to the fact that the input allows multiple selection
1905-
// ex my_dom[]
1906-
//in these conditions, the input is never sent by the browser
1907-
if ($field['multiple']) {
1908-
//handle multi dropdown field
1909-
if ($field['type'] == 'dropdown') {
1910-
$multiple_key = $prefix . $base_name . "dropdowns_id";
1911-
$multiple_defined = '_' . $multiple_key . '_defined';
1912-
//values are defined by user
1913-
if (isset($item->input[$multiple_key])) {
1914-
$data[$base_name . "_dropdowns_id"] = $item->input[$multiple_key];
1915-
$has_fields = true;
1916-
} elseif (
1917-
isset($item->input[$multiple_defined])
1918-
&& $item->input[$multiple_defined]
1919-
) { //multi dropdown is empty or has been emptied
1920-
$data[$base_name . "_dropdowns_id"] = [];
1921-
$has_fields = true;
1922-
}
1923-
}
1903+
if ($field['type'] === 'number') {
1904+
$value = str_replace(',', '.', $value);
1905+
}
19241906

1925-
//managed multi GLPI item dropdown field
1926-
if (preg_match('/^dropdown-(?<type>.+)$/', $field['type'], $match) === 1) {
1927-
//values are defined by user
1928-
if (isset($item->input[$field['name']])) {
1929-
$data[$base_name] = $item->input[$field['name']];
1930-
$has_fields = true;
1931-
} else { //multi dropdown is empty or has been emptied
1932-
$data[$base_name] = [];
1933-
}
1934-
}
1907+
$data[$base_name] = $value;
1908+
$has_fields = true;
1909+
1910+
// If the field is a richtext
1911+
if ($field['type'] === 'richtext') {
1912+
foreach (['_' . $htmlKeyWithId, '_prefix_' . $htmlKeyWithId, '_tag_' . $htmlKeyWithId] as $extra) {
1913+
$data[$extra] = $item->input[$extra];
19351914
}
19361915
}
19371916
}
19381917

1939-
if ($has_fields === true) {
1940-
return $data;
1941-
} else {
1942-
return false;
1943-
}
1918+
return $has_fields ? $data : false;
19441919
}
19451920

19461921
public static function getAddSearchOptions($itemtype, $containers_id = false)

0 commit comments

Comments
 (0)