@@ -1696,85 +1696,87 @@ public static function findContainer($itemtype, $type = 'tab', $subtype = '')
16961696 return $ id ;
16971697 }
16981698
1699- public static function findContainers ($ itemtype , $ type = 'tab ' , $ subtype = '' , $ itemId = '' )
1699+ /**
1700+ * Find containers for a specific itemtype, type, subtype and entity id
1701+ *
1702+ * @param string $itemtype Itemtype GLPI
1703+ * @param string $type Type of container (tab, dom, domtab)
1704+ * @param string $subtype
1705+ * @param integer $entityId Entity ID default is 0 (root entity)
1706+ *
1707+ * @return array List of container IDs
1708+ */
1709+ public static function findContainers ($ itemtype , $ type = 'tab ' , $ subtype = '' , $ entityId = 0 ): array
17001710 {
17011711 /** @var DBmysql $DB */
17021712 global $ DB ;
1703- $ ids = [];
17041713
1705- if (!empty ($ itemtype ) && !empty ($ itemId ) && class_exists ($ itemtype )) {
1706-
1707- $ obj = new $ itemtype ();
1708- if ($ obj ->getFromDB ($ itemId )) {
1709-
1710- $ entityId = $ obj ->fields ['entities_id ' ] ?? 0 ;
1711- $ entityIds = getAncestorsOf ("glpi_entities " , $ entityId );
1712- $ entityIds [] = $ entityId ; // Add entity obj itself to the list
1713- $ glpiActiveEntities = $ _SESSION ['glpiactiveentities ' ] ?? 0 ;
1714+ if ($ itemtype === '' ) {
1715+ return [];
1716+ }
17141717
1715- $ entityRestriction = getEntitiesRestrictCriteria ('' , '' , $ glpiActiveEntities , true , true );
1718+ $ entitiesIds = getAncestorsOf ("glpi_entities " , (string ) $ entityId );
1719+ $ entitiesIds [] = $ entityId ; // Add entity active itself to the list
17161720
1717- $ where = [
1718- 'is_active ' => 1 ,
1719- 'type ' => $ type ,
1720- new \QueryExpression ("JSON_CONTAINS(itemtypes, " . $ DB ->quote ('" ' . $ itemtype . '" ' ) . ") " ),
1721- 'AND ' => [
1722- 'OR ' => [
1723- [
1724- 'is_recursive ' => 1 ,
1725- 'entities_id ' => $ entityIds ,
1726- ],
1727- [
1728- 'is_recursive ' => 0 ,
1729- ],
1730- ],
1721+ $ where = [
1722+ 'is_active ' => 1 ,
1723+ 'type ' => $ type ,
1724+ new \QueryExpression ("JSON_CONTAINS(itemtypes, " . $ DB ->quote ('" ' . $ itemtype . '" ' ) . ") " ),
1725+ 'AND ' => [
1726+ 'OR ' => [
1727+ [
1728+ 'is_recursive ' => 1 ,
1729+ 'entities_id ' => $ entitiesIds ,
17311730 ],
1732- ];
1731+ [
1732+ 'is_recursive ' => 0 ,
1733+ 'entities_id ' => $ entityId ,
1734+ ],
1735+ ],
1736+ ],
1737+ ];
17331738
1734- if ($ subtype !== '' ) {
1735- if ($ subtype === $ itemtype . '$main ' ) {
1736- $ where ['type ' ] = 'dom ' ;
1737- } else {
1738- $ where ['type ' ] = ['!= ' , 'dom ' ];
1739- $ where ['subtype ' ] = $ subtype ;
1740- }
1741- } else {
1742- $ where ['type ' ] = $ type ;
1743- }
1739+ if ($ subtype !== '' ) {
1740+ if ($ subtype === $ itemtype . '$main ' ) {
1741+ $ where ['type ' ] = 'dom ' ;
1742+ } else {
1743+ $ where ['type ' ] = ['!= ' , 'dom ' ];
1744+ $ where ['subtype ' ] = $ subtype ;
1745+ }
1746+ } else {
1747+ $ where ['type ' ] = $ type ;
1748+ }
17441749
1745- if (!empty ($ entityRestriction )) {
1746- $ allowedEntities = [];
1747- foreach ($ entityRestriction as $ restriction ) {
1748- if (isset ($ restriction ['entities_id ' ]) && is_array ($ restriction ['entities_id ' ])) {
1749- $ allowedEntities = array_merge ($ allowedEntities , $ restriction ['entities_id ' ]);
1750- }
1751- }
1752- if (!empty ($ allowedEntities )) {
1753- $ where ['entities_id ' ] = $ allowedEntities ;
1754- }
1750+ $ entityRestriction = getEntitiesRestrictCriteria ('' , '' , $ entityId , true , true );
1751+ if (!empty ($ entityRestriction )) {
1752+ $ allowedEntities = [];
1753+ foreach ($ entityRestriction as $ restriction ) {
1754+ if (isset ($ restriction ['entities_id ' ]) && is_array ($ restriction ['entities_id ' ])) {
1755+ $ allowedEntities = array_merge ($ allowedEntities , $ restriction ['entities_id ' ]);
17551756 }
1757+ }
1758+ if (!empty ($ allowedEntities )) {
1759+ $ where ['entities_id ' ] = $ allowedEntities ;
1760+ }
1761+ }
17561762
1757- $ iterator = $ DB ->request ([
1758- 'SELECT ' => 'id ' ,
1759- 'FROM ' => self ::getTable (),
1760- 'WHERE ' => $ where ,
1761- ]);
1762-
1763- foreach ($ iterator as $ row ) {
1764- $ containerId = (int ) $ row ['id ' ];
1765-
1766- //profiles restriction
1767- if (isset ($ _SESSION ['glpiactiveprofile ' ]['id ' ])) {
1768- $ profileId = $ _SESSION ['glpiactiveprofile ' ]['id ' ];
1769- $ right = PluginFieldsProfile::getRightOnContainer ($ profileId , $ containerId );
1770- if ($ right < READ ) {
1771- continue ;
1772- }
1773- }
1763+ $ iterator = $ DB ->request ([
1764+ 'SELECT ' => 'id ' ,
1765+ 'FROM ' => self ::getTable (),
1766+ 'WHERE ' => $ where ,
1767+ ]);
17741768
1775- $ ids [] = $ containerId ;
1769+ $ ids = [];
1770+ foreach ($ iterator as $ row ) {
1771+ $ containerId = (int ) $ row ['id ' ];
1772+
1773+ if (isset ($ _SESSION ['glpiactiveprofile ' ]['id ' ])) {
1774+ $ profileId = $ _SESSION ['glpiactiveprofile ' ]['id ' ];
1775+ if (PluginFieldsProfile::getRightOnContainer ($ profileId , $ containerId ) < READ ) {
1776+ continue ;
17761777 }
17771778 }
1779+ $ ids [] = $ containerId ;
17781780 }
17791781
17801782 return $ ids ;
@@ -1849,35 +1851,29 @@ public static function preItemUpdate(CommonDBTM $item)
18491851 */
18501852 public static function preItem (CommonDBTM $ item )
18511853 {
1852- $ type = 'dom ' ;
1853- if (isset ($ _REQUEST ['_plugin_fields_type ' ])) {
1854- $ type = $ _REQUEST ['_plugin_fields_type ' ];
1855- }
1856- $ subtype = '' ;
1857- if ($ type == 'domtab ' ) {
1858- $ subtype = $ _REQUEST ['_plugin_fields_subtype ' ];
1859- }
1854+ $ type = $ _REQUEST ['_plugin_fields_type ' ] ?? 'dom ' ;
1855+ $ subtype = ($ type === 'domtab ' ) ? ($ _REQUEST ['_plugin_fields_subtype ' ] ?? '' ) : '' ;
1856+
1857+ $ itemEntityId = $ item ->getEntityID ();
1858+ $ entityId = ($ itemEntityId === -1 ) ? ($ _SESSION ['glpiactive_entity ' ] ?? 0 ) : $ itemEntityId ;
18601859
1861- $ containers = self ::findContainers ($ item ->getType (), $ type , $ subtype , $ item -> getID () );
1860+ $ containers = self ::findContainers ($ item ->getType (), $ type , $ subtype , $ entityId );
18621861
18631862 $ all_data = [];
18641863
18651864 foreach ($ containers as $ c_id ) {
18661865
1867- $ loc_c = new PluginFieldsContainer ();
1868- $ loc_c ->getFromDB ($ c_id );
1869-
18701866 // check rights on $c_id
1871-
1872- if (isset ($ _SESSION ['glpiactiveprofile ' ]['id ' ]) && $ _SESSION ['glpiactiveprofile ' ]['id ' ] != null && $ c_id > 0 ) {
1867+ if (isset ($ _SESSION ['glpiactiveprofile ' ]['id ' ])) {
18731868 $ right = PluginFieldsProfile::getRightOnContainer ($ _SESSION ['glpiactiveprofile ' ]['id ' ], $ c_id );
1874- if ($ right > READ === false ) { // Si le droit est insuffisant, on passe au container suivant
1875- continue ;
1869+ if ($ right < READ ) {
1870+ continue ; // insufficient rights
18761871 }
1877- } else {
1878- continue ;
18791872 }
18801873
1874+ $ loc_c = new self ();
1875+ $ loc_c ->getFromDB ($ c_id );
1876+
18811877 // need to check if container is usable on this object entity
18821878 $ entities = [$ loc_c ->fields ['entities_id ' ]];
18831879 if ($ loc_c ->fields ['is_recursive ' ]) {
@@ -1889,11 +1885,12 @@ public static function preItem(CommonDBTM $item)
18891885 }
18901886
18911887 if ($ item ->isEntityAssign () && !in_array ($ item ->getEntityID (), $ entities )) {
1892- continue ;
1888+ continue ; // not the right entity
18931889 }
18941890
18951891 if (false !== ($ data = self ::populateData ($ c_id , $ item ))) {
1896- if (self ::validateValues ($ data , $ item ->getType (), isset ($ _REQUEST ['massiveaction ' ])) === false ) {
1892+ if (!self ::validateValues ($ data , $ item ->getType (), isset ($ _REQUEST ['massiveaction ' ]))) {
1893+ // if validation fails, we need to remove the data from the item input
18971894 $ item ->input = [];
18981895
18991896 return false ;
@@ -1936,20 +1933,16 @@ public static function populateData($c_id, CommonDBTM $item)
19361933 ];
19371934
19381935 // Add status so it can be used with status overrides
1939- $ status_field_name = PluginFieldsStatusOverride::getStatusFieldName ($ item ->getType ());
1940- $ data [$ status_field_name ] = null ;
1941- if (array_key_exists ($ status_field_name , $ item ->input ) && $ item ->input [$ status_field_name ] !== '' ) {
1942- $ data [$ status_field_name ] = (int ) $ item ->input [$ status_field_name ];
1943- } elseif (array_key_exists ($ status_field_name , $ item ->fields ) && $ item ->fields [$ status_field_name ] !== '' ) {
1944- $ data [$ status_field_name ] = (int ) $ item ->fields [$ status_field_name ];
1945- }
1936+ $ statusField = PluginFieldsStatusOverride::getStatusFieldName ($ item ->getType ());
1937+ $ data [$ statusField ] = $ item ->input [$ statusField ] ?? $ item ->fields [$ statusField ] ?? null ;
19461938
19471939 $ has_fields = false ;
19481940 // Prefix for input names
19491941 $ prefix = "plugin_fields_ {$ c_id }_ " ;
19501942
19511943 foreach ($ fields as $ field ) {
19521944 $ base_name = $ field ['name ' ];
1945+ $ isMulti = (bool ) $ field ['multiple ' ];
19531946 if ($ field ['type ' ] == 'glpi_item ' ) {
19541947 $ itemtype_key = "itemtype_ {$ base_name }" ;
19551948 $ items_id_key = "items_id_ {$ base_name }" ;
@@ -1958,9 +1951,9 @@ public static function populateData($c_id, CommonDBTM $item)
19581951 continue ; // not a valid input
19591952 }
19601953
1961- $ has_fields = true ;
19621954 $ data [$ itemtype_key ] = $ item ->input [$ itemtype_key ];
19631955 $ data [$ items_id_key ] = $ item ->input [$ items_id_key ];
1956+ $ has_fields = true ;
19641957
19651958 continue ; // bypass unique field handling
19661959 }
@@ -1969,87 +1962,68 @@ public static function populateData($c_id, CommonDBTM $item)
19691962 // "plugin_fields_{$c_id}_{$base_name}"
19701963 if ($ field ['type ' ] === 'dropdown ' ) {
19711964 // For dropdown fields, the input name is "plugin_fields_{$c_id}_{$base_name}dropdowns_id"
1972- $ input = $ prefix . $ base_name . "dropdowns_id " ;
1973- if (isset ($ item ->input [$ input ])) {
1974- $ has_fields = true ;
1975- $ data [$ base_name . "_dropdowns_id " ] = $ item ->input [$ input ];
1976- }
1977- // If the field is a dropdown with multiple selection, we need to check if the input name is defined
1978- elseif ($ field ['multiple ' ]) {
1979- $ multiple_key = $ input ;
1980- $ multiple_defined = '_ ' . $ multiple_key . '_defined ' ;
1981- if (isset ($ item ->input [$ multiple_key ])) {
1982- $ has_fields = true ;
1983- $ data [$ base_name . "_dropdowns_id " ] = $ item ->input [$ multiple_key ];
1984- } elseif (isset ($ item ->input [$ multiple_defined ]) && $ item ->input [$ multiple_defined ]) {
1985- $ has_fields = true ;
1986- $ data [$ base_name . "_dropdowns_id " ] = [];
1965+ $ htmlKeyWithId = $ prefix . $ base_name . "dropdowns_id " ; // html key in POST data with id
1966+ $ htmlKeyNoId = "plugin_fields_ {$ base_name }dropdowns_id " ; // html key in POST data without id
1967+ $ colKey = 'plugin_fields_ ' . $ base_name . 'dropdowns_id ' ; // column key in DB
1968+
1969+ if (array_key_exists ($ htmlKeyWithId , $ item ->input )) {
1970+ $ data [$ colKey ] = $ item ->input [$ htmlKeyWithId ];
1971+ $ has_fields = true ;
1972+ } elseif (array_key_exists ($ htmlKeyNoId , $ item ->input )) {
1973+ $ data [$ colKey ] = $ item ->input [$ htmlKeyNoId ];
1974+ $ has_fields = true ;
1975+ } elseif ($ isMulti ) {
1976+ $ definedKeyWithId = '_ ' . $ htmlKeyWithId . '_defined ' ;
1977+ $ definedKeyNoId = '_ ' . $ htmlKeyNoId . '_defined ' ;
1978+ if (!empty ($ item ->input [$ definedKeyWithId ]) || !empty ($ item ->input [$ definedKeyNoId ])) {
1979+ $ data [$ colKey ] = [];
1980+ $ has_fields = true ;
19871981 }
19881982 }
19891983 continue ;
19901984 }
19911985
19921986 // For fields standard, the input name is "plugin_fields_{$c_id}_{$base_name}"
1993- $ input = $ prefix . $ base_name ;
1994- if (isset ($ item ->input [$ input ])) {
1995- $ has_fields = true ;
1996- // Before is_number check, help user to have a number correct, during a massive action of a number field
1997- if ($ field ['type ' ] == 'number ' ) {
1998- $ item ->input [$ input ] = str_replace (', ' , '. ' , $ item ->input [$ input ]);
1987+ $ htmlKeyWithId = $ prefix . $ base_name ;
1988+ $ htmlKeyNoId = "plugin_fields_ {$ base_name }" ;
1989+
1990+ $ valuePresent = false ;
1991+ $ value = null ;
1992+ if (array_key_exists ($ htmlKeyWithId , $ item ->input )) {
1993+ $ value = $ item ->input [$ htmlKeyWithId ];
1994+ $ valuePresent = true ;
1995+ } elseif (array_key_exists ($ htmlKeyNoId , $ item ->input )) {
1996+ $ value = $ item ->input [$ htmlKeyNoId ];
1997+ $ valuePresent = true ;
1998+ } elseif ($ isMulti ) {
1999+ $ definedKeyWithId = '_ ' . $ htmlKeyWithId . '_defined ' ;
2000+ $ definedKeyNoId = '_ ' . $ htmlKeyNoId . '_defined ' ;
2001+ if (!empty ($ item ->input [$ definedKeyWithId ]) || !empty ($ item ->input [$ definedKeyNoId ])) {
2002+ $ value = [];
2003+ $ valuePresent = true ;
19992004 }
2000- $ data [ $ base_name ] = $ item -> input [ $ input ];
2005+ }
20012006
2002- if ($ field ['type ' ] === 'richtext ' ) {
2003- $ filename_input = "_ " . $ input ;
2004- $ prefix_input = "_prefix_ " . $ input ;
2005- $ tag_input = "_tag_ " . $ input ;
2007+ if (!$ valuePresent ) {
2008+ continue ; // not a valid input
2009+ }
20062010
2007- $ data [$ filename_input ] = $ item ->input [$ filename_input ] ?? [];
2008- $ data [$ prefix_input ] = $ item ->input [$ prefix_input ] ?? [];
2009- $ data [$ tag_input ] = $ item ->input [$ tag_input ] ?? [];
2010- }
2011- } else {
2012- //the absence of the field in the input may be due to the fact that the input allows multiple selection
2013- // ex my_dom[]
2014- //in these conditions, the input is never sent by the browser
2015- if ($ field ['multiple ' ]) {
2016- $ data ['multiple_dropdown_action ' ] = $ _POST ['multiple_dropdown_action ' ] ?? 'assign ' ;
2017- //handle multi dropdown field
2018- if ($ field ['type ' ] == 'dropdown ' ) {
2019- $ multiple_key = $ prefix . $ base_name . "dropdowns_id " ;
2020- $ multiple_defined = '_ ' . $ multiple_key . '_defined ' ;
2021- //values are defined by user
2022- if (isset ($ item ->input [$ multiple_key ])) {
2023- $ data [$ base_name . "_dropdowns_id " ] = $ item ->input [$ multiple_key ];
2024- $ has_fields = true ;
2025- } elseif (
2026- isset ($ item ->input [$ multiple_defined ])
2027- && $ item ->input [$ multiple_defined ]
2028- ) { //multi dropdown is empty or has been emptied
2029- $ data [$ base_name . "_dropdowns_id " ] = [];
2030- $ has_fields = true ;
2031- }
2032- }
2011+ if ($ field ['type ' ] === 'number ' ) {
2012+ $ value = str_replace (', ' , '. ' , $ value );
2013+ }
20332014
2034- //managed multi GLPI item dropdown field
2035- if (preg_match ('/^dropdown-(?<type>.+)$/ ' , $ field ['type ' ], $ match ) === 1 ) {
2036- //values are defined by user
2037- if (isset ($ item ->input [$ field ['name ' ]])) {
2038- $ data [$ base_name ] = $ item ->input [$ field ['name ' ]];
2039- $ has_fields = true ;
2040- } else { //multi dropdown is empty or has been emptied
2041- $ data [$ base_name ] = [];
2042- }
2043- }
2015+ $ data [$ base_name ] = $ value ;
2016+ $ has_fields = true ;
2017+
2018+ // If the field is a richtext
2019+ if ($ field ['type ' ] === 'richtext ' ) {
2020+ foreach (['_ ' . $ htmlKeyWithId , '_prefix_ ' . $ htmlKeyWithId , '_tag_ ' . $ htmlKeyWithId ] as $ extra ) {
2021+ $ data [$ extra ] = $ item ->input [$ extra ];
20442022 }
20452023 }
20462024 }
20472025
2048- if ($ has_fields === true ) {
2049- return $ data ;
2050- } else {
2051- return false ;
2052- }
2026+ return $ has_fields ? $ data : false ;
20532027 }
20542028
20552029 public static function getAddSearchOptions ($ itemtype , $ containers_id = false )
0 commit comments