@@ -1690,85 +1690,87 @@ public static function findContainer($itemtype, $type = 'tab', $subtype = '')
16901690 return $ id ;
16911691 }
16921692
1693- public static function findContainers ($ itemtype , $ type = 'tab ' , $ subtype = '' , $ itemId = '' )
1693+ /**
1694+ * Find containers for a specific itemtype, type, subtype and entity id
1695+ *
1696+ * @param string $itemtype Itemtype GLPI
1697+ * @param string $type Type of container (tab, dom, domtab)
1698+ * @param string $subtype
1699+ * @param integer $entityId Entity ID default is 0 (root entity)
1700+ *
1701+ * @return array List of container IDs
1702+ */
1703+ public static function findContainers ($ itemtype , $ type = 'tab ' , $ subtype = '' , $ entityId = 0 ): array
16941704 {
16951705 /** @var DBmysql $DB */
16961706 global $ DB ;
1697- $ ids = [];
16981707
1699- if (!empty ($ itemtype ) && !empty ($ itemId ) && class_exists ($ itemtype )) {
1700-
1701- $ obj = new $ itemtype ();
1702- if ($ obj ->getFromDB ($ itemId )) {
1703-
1704- $ entityId = $ obj ->fields ['entities_id ' ] ?? 0 ;
1705- $ entityIds = getAncestorsOf ("glpi_entities " , $ entityId );
1706- $ entityIds [] = $ entityId ; // Add entity obj itself to the list
1707- $ glpiActiveEntities = $ _SESSION ['glpiactiveentities ' ] ?? 0 ;
1708+ if ($ itemtype === '' ) {
1709+ return [];
1710+ }
17081711
1709- $ entityRestriction = getEntitiesRestrictCriteria ('' , '' , $ glpiActiveEntities , true , true );
1712+ $ entitiesIds = getAncestorsOf ("glpi_entities " , (string ) $ entityId );
1713+ $ entitiesIds [] = $ entityId ; // Add entity active itself to the list
17101714
1711- $ where = [
1712- 'is_active ' => 1 ,
1713- 'type ' => $ type ,
1714- new \QueryExpression ("JSON_CONTAINS(itemtypes, " . $ DB ->quote ('" ' . $ itemtype . '" ' ) . ") " ),
1715- 'AND ' => [
1716- 'OR ' => [
1717- [
1718- 'is_recursive ' => 1 ,
1719- 'entities_id ' => $ entityIds ,
1720- ],
1721- [
1722- 'is_recursive ' => 0 ,
1723- ],
1724- ],
1715+ $ where = [
1716+ 'is_active ' => 1 ,
1717+ 'type ' => $ type ,
1718+ new \QueryExpression ("JSON_CONTAINS(itemtypes, " . $ DB ->quote ('" ' . $ itemtype . '" ' ) . ") " ),
1719+ 'AND ' => [
1720+ 'OR ' => [
1721+ [
1722+ 'is_recursive ' => 1 ,
1723+ 'entities_id ' => $ entitiesIds ,
17251724 ],
1726- ];
1725+ [
1726+ 'is_recursive ' => 0 ,
1727+ 'entities_id ' => $ entityId ,
1728+ ],
1729+ ],
1730+ ],
1731+ ];
17271732
1728- if ($ subtype !== '' ) {
1729- if ($ subtype === $ itemtype . '$main ' ) {
1730- $ where ['type ' ] = 'dom ' ;
1731- } else {
1732- $ where ['type ' ] = ['!= ' , 'dom ' ];
1733- $ where ['subtype ' ] = $ subtype ;
1734- }
1735- } else {
1736- $ where ['type ' ] = $ type ;
1737- }
1733+ if ($ subtype !== '' ) {
1734+ if ($ subtype === $ itemtype . '$main ' ) {
1735+ $ where ['type ' ] = 'dom ' ;
1736+ } else {
1737+ $ where ['type ' ] = ['!= ' , 'dom ' ];
1738+ $ where ['subtype ' ] = $ subtype ;
1739+ }
1740+ } else {
1741+ $ where ['type ' ] = $ type ;
1742+ }
17381743
1739- if (!empty ($ entityRestriction )) {
1740- $ allowedEntities = [];
1741- foreach ($ entityRestriction as $ restriction ) {
1742- if (isset ($ restriction ['entities_id ' ]) && is_array ($ restriction ['entities_id ' ])) {
1743- $ allowedEntities = array_merge ($ allowedEntities , $ restriction ['entities_id ' ]);
1744- }
1745- }
1746- if (!empty ($ allowedEntities )) {
1747- $ where ['entities_id ' ] = $ allowedEntities ;
1748- }
1744+ $ entityRestriction = getEntitiesRestrictCriteria ('' , '' , $ entityId , true , true );
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 ' ]);
17491750 }
1751+ }
1752+ if (!empty ($ allowedEntities )) {
1753+ $ where ['entities_id ' ] = $ allowedEntities ;
1754+ }
1755+ }
17501756
1751- $ iterator = $ DB ->request ([
1752- 'SELECT ' => 'id ' ,
1753- 'FROM ' => self ::getTable (),
1754- 'WHERE ' => $ where ,
1755- ]);
1756-
1757- foreach ($ iterator as $ row ) {
1758- $ containerId = (int ) $ row ['id ' ];
1759-
1760- //profiles restriction
1761- if (isset ($ _SESSION ['glpiactiveprofile ' ]['id ' ])) {
1762- $ profileId = $ _SESSION ['glpiactiveprofile ' ]['id ' ];
1763- $ right = PluginFieldsProfile::getRightOnContainer ($ profileId , $ containerId );
1764- if ($ right < READ ) {
1765- continue ;
1766- }
1767- }
1757+ $ iterator = $ DB ->request ([
1758+ 'SELECT ' => 'id ' ,
1759+ 'FROM ' => self ::getTable (),
1760+ 'WHERE ' => $ where ,
1761+ ]);
1762+
1763+ $ ids = [];
1764+ foreach ($ iterator as $ row ) {
1765+ $ containerId = (int ) $ row ['id ' ];
17681766
1769- $ ids [] = $ containerId ;
1767+ if (isset ($ _SESSION ['glpiactiveprofile ' ]['id ' ])) {
1768+ $ profileId = $ _SESSION ['glpiactiveprofile ' ]['id ' ];
1769+ if (PluginFieldsProfile::getRightOnContainer ($ profileId , $ containerId ) < READ ) {
1770+ continue ;
17701771 }
17711772 }
1773+ $ ids [] = $ containerId ;
17721774 }
17731775
17741776 return $ ids ;
@@ -1843,35 +1845,29 @@ public static function preItemUpdate(CommonDBTM $item)
18431845 */
18441846 public static function preItem (CommonDBTM $ item )
18451847 {
1846- $ type = 'dom ' ;
1847- if (isset ($ _REQUEST ['_plugin_fields_type ' ])) {
1848- $ type = $ _REQUEST ['_plugin_fields_type ' ];
1849- }
1850- $ subtype = '' ;
1851- if ($ type == 'domtab ' ) {
1852- $ subtype = $ _REQUEST ['_plugin_fields_subtype ' ];
1853- }
1848+ $ type = $ _REQUEST ['_plugin_fields_type ' ] ?? 'dom ' ;
1849+ $ subtype = ($ type === 'domtab ' ) ? ($ _REQUEST ['_plugin_fields_subtype ' ] ?? '' ) : '' ;
18541850
1855- $ containers = self ::findContainers ($ item ->getType (), $ type , $ subtype , $ item ->getID ());
1851+ $ itemEntityId = $ item ->getEntityID ();
1852+ $ entityId = ($ itemEntityId === -1 ) ? ($ _SESSION ['glpiactive_entity ' ] ?? 0 ) : $ itemEntityId ;
1853+
1854+ $ containers = self ::findContainers ($ item ->getType (), $ type , $ subtype , $ entityId );
18561855
18571856 $ all_data = [];
18581857
18591858 foreach ($ containers as $ c_id ) {
18601859
1861- $ loc_c = new PluginFieldsContainer ();
1862- $ loc_c ->getFromDB ($ c_id );
1863-
18641860 // check rights on $c_id
1865-
1866- if (isset ($ _SESSION ['glpiactiveprofile ' ]['id ' ]) && $ _SESSION ['glpiactiveprofile ' ]['id ' ] != null && $ c_id > 0 ) {
1861+ if (isset ($ _SESSION ['glpiactiveprofile ' ]['id ' ])) {
18671862 $ right = PluginFieldsProfile::getRightOnContainer ($ _SESSION ['glpiactiveprofile ' ]['id ' ], $ c_id );
1868- if ($ right > READ === false ) { // Si le droit est insuffisant, on passe au container suivant
1869- continue ;
1863+ if ($ right < READ ) {
1864+ continue ; // insufficient rights
18701865 }
1871- } else {
1872- continue ;
18731866 }
18741867
1868+ $ loc_c = new self ();
1869+ $ loc_c ->getFromDB ($ c_id );
1870+
18751871 // need to check if container is usable on this object entity
18761872 $ entities = [$ loc_c ->fields ['entities_id ' ]];
18771873 if ($ loc_c ->fields ['is_recursive ' ]) {
@@ -1883,11 +1879,12 @@ public static function preItem(CommonDBTM $item)
18831879 }
18841880
18851881 if ($ item ->isEntityAssign () && !in_array ($ item ->getEntityID (), $ entities )) {
1886- continue ;
1882+ continue ; // not the right entity
18871883 }
18881884
18891885 if (false !== ($ data = self ::populateData ($ c_id , $ item ))) {
1890- if (self ::validateValues ($ data , $ item ->getType (), isset ($ _REQUEST ['massiveaction ' ])) === false ) {
1886+ if (!self ::validateValues ($ data , $ item ->getType (), isset ($ _REQUEST ['massiveaction ' ]))) {
1887+ // if validation fails, we need to remove the data from the item input
18911888 $ item ->input = [];
18921889
18931890 return false ;
@@ -1930,20 +1927,16 @@ public static function populateData($c_id, CommonDBTM $item)
19301927 ];
19311928
19321929 // Add status so it can be used with status overrides
1933- $ status_field_name = PluginFieldsStatusOverride::getStatusFieldName ($ item ->getType ());
1934- $ data [$ status_field_name ] = null ;
1935- if (array_key_exists ($ status_field_name , $ item ->input ) && $ item ->input [$ status_field_name ] !== '' ) {
1936- $ data [$ status_field_name ] = (int ) $ item ->input [$ status_field_name ];
1937- } elseif (array_key_exists ($ status_field_name , $ item ->fields ) && $ item ->fields [$ status_field_name ] !== '' ) {
1938- $ data [$ status_field_name ] = (int ) $ item ->fields [$ status_field_name ];
1939- }
1930+ $ statusField = PluginFieldsStatusOverride::getStatusFieldName ($ item ->getType ());
1931+ $ data [$ statusField ] = $ item ->input [$ statusField ] ?? $ item ->fields [$ statusField ] ?? null ;
19401932
19411933 $ has_fields = false ;
19421934 // Prefix for input names
19431935 $ prefix = "plugin_fields_ {$ c_id }_ " ;
19441936
19451937 foreach ($ fields as $ field ) {
19461938 $ base_name = $ field ['name ' ];
1939+ $ isMulti = (bool ) $ field ['multiple ' ];
19471940 if ($ field ['type ' ] == 'glpi_item ' ) {
19481941 $ itemtype_key = "itemtype_ {$ base_name }" ;
19491942 $ items_id_key = "items_id_ {$ base_name }" ;
@@ -1952,9 +1945,9 @@ public static function populateData($c_id, CommonDBTM $item)
19521945 continue ; // not a valid input
19531946 }
19541947
1955- $ has_fields = true ;
19561948 $ data [$ itemtype_key ] = $ item ->input [$ itemtype_key ];
19571949 $ data [$ items_id_key ] = $ item ->input [$ items_id_key ];
1950+ $ has_fields = true ;
19581951
19591952 continue ; // bypass unique field handling
19601953 }
@@ -1963,87 +1956,68 @@ public static function populateData($c_id, CommonDBTM $item)
19631956 // "plugin_fields_{$c_id}_{$base_name}"
19641957 if ($ field ['type ' ] === 'dropdown ' ) {
19651958 // For dropdown fields, the input name is "plugin_fields_{$c_id}_{$base_name}dropdowns_id"
1966- $ input = $ prefix . $ base_name . "dropdowns_id " ;
1967- if (isset ($ item ->input [$ input ])) {
1968- $ has_fields = true ;
1969- $ data [$ base_name . "_dropdowns_id " ] = $ item ->input [$ input ];
1970- }
1971- // If the field is a dropdown with multiple selection, we need to check if the input name is defined
1972- elseif ($ field ['multiple ' ]) {
1973- $ multiple_key = $ input ;
1974- $ multiple_defined = '_ ' . $ multiple_key . '_defined ' ;
1975- if (isset ($ item ->input [$ multiple_key ])) {
1976- $ has_fields = true ;
1977- $ data [$ base_name . "_dropdowns_id " ] = $ item ->input [$ multiple_key ];
1978- } elseif (isset ($ item ->input [$ multiple_defined ]) && $ item ->input [$ multiple_defined ]) {
1979- $ has_fields = true ;
1980- $ data [$ base_name . "_dropdowns_id " ] = [];
1959+ $ htmlKeyWithId = $ prefix . $ base_name . "dropdowns_id " ; // html key in POST data with id
1960+ $ htmlKeyNoId = "plugin_fields_ {$ base_name }dropdowns_id " ; // html key in POST data without id
1961+ $ colKey = 'plugin_fields_ ' . $ base_name . 'dropdowns_id ' ; // column key in DB
1962+
1963+ if (array_key_exists ($ htmlKeyWithId , $ item ->input )) {
1964+ $ data [$ colKey ] = $ item ->input [$ htmlKeyWithId ];
1965+ $ has_fields = true ;
1966+ } elseif (array_key_exists ($ htmlKeyNoId , $ item ->input )) {
1967+ $ data [$ colKey ] = $ item ->input [$ htmlKeyNoId ];
1968+ $ has_fields = true ;
1969+ } elseif ($ isMulti ) {
1970+ $ definedKeyWithId = '_ ' . $ htmlKeyWithId . '_defined ' ;
1971+ $ definedKeyNoId = '_ ' . $ htmlKeyNoId . '_defined ' ;
1972+ if (!empty ($ item ->input [$ definedKeyWithId ]) || !empty ($ item ->input [$ definedKeyNoId ])) {
1973+ $ data [$ colKey ] = [];
1974+ $ has_fields = true ;
19811975 }
19821976 }
19831977 continue ;
19841978 }
19851979
19861980 // For fields standard, the input name is "plugin_fields_{$c_id}_{$base_name}"
1987- $ input = $ prefix . $ base_name ;
1988- if (isset ($ item ->input [$ input ])) {
1989- $ has_fields = true ;
1990- // Before is_number check, help user to have a number correct, during a massive action of a number field
1991- if ($ field ['type ' ] == 'number ' ) {
1992- $ item ->input [$ input ] = str_replace (', ' , '. ' , $ item ->input [$ input ]);
1981+ $ htmlKeyWithId = $ prefix . $ base_name ;
1982+ $ htmlKeyNoId = "plugin_fields_ {$ base_name }" ;
1983+
1984+ $ valuePresent = false ;
1985+ $ value = null ;
1986+ if (array_key_exists ($ htmlKeyWithId , $ item ->input )) {
1987+ $ value = $ item ->input [$ htmlKeyWithId ];
1988+ $ valuePresent = true ;
1989+ } elseif (array_key_exists ($ htmlKeyNoId , $ item ->input )) {
1990+ $ value = $ item ->input [$ htmlKeyNoId ];
1991+ $ valuePresent = true ;
1992+ } elseif ($ isMulti ) {
1993+ $ definedKeyWithId = '_ ' . $ htmlKeyWithId . '_defined ' ;
1994+ $ definedKeyNoId = '_ ' . $ htmlKeyNoId . '_defined ' ;
1995+ if (!empty ($ item ->input [$ definedKeyWithId ]) || !empty ($ item ->input [$ definedKeyNoId ])) {
1996+ $ value = [];
1997+ $ valuePresent = true ;
19931998 }
1994- $ data [ $ base_name ] = $ item -> input [ $ input ];
1999+ }
19952000
1996- if ($ field ['type ' ] === 'richtext ' ) {
1997- $ filename_input = "_ " . $ input ;
1998- $ prefix_input = "_prefix_ " . $ input ;
1999- $ tag_input = "_tag_ " . $ input ;
2001+ if (!$ valuePresent ) {
2002+ continue ; // not a valid input
2003+ }
20002004
2001- $ data [$ filename_input ] = $ item ->input [$ filename_input ] ?? [];
2002- $ data [$ prefix_input ] = $ item ->input [$ prefix_input ] ?? [];
2003- $ data [$ tag_input ] = $ item ->input [$ tag_input ] ?? [];
2004- }
2005- } elseif ($ field ['multiple ' ]) {
2006- //the absence of the field in the input may be due to the fact that the input allows multiple selection
2007- // ex my_dom[]
2008- //in these conditions, the input is never sent by the browser
2009- if ($ field ['multiple ' ]) {
2010- $ data ['multiple_dropdown_action ' ] = $ _POST ['multiple_dropdown_action ' ] ?? 'assign ' ;
2011- //handle multi dropdown field
2012- if ($ field ['type ' ] == 'dropdown ' ) {
2013- $ multiple_key = $ prefix . $ base_name . "dropdowns_id " ;
2014- $ multiple_defined = '_ ' . $ multiple_key . '_defined ' ;
2015- //values are defined by user
2016- if (isset ($ item ->input [$ multiple_key ])) {
2017- $ data [$ base_name . "_dropdowns_id " ] = $ item ->input [$ multiple_key ];
2018- $ has_fields = true ;
2019- } elseif (
2020- isset ($ item ->input [$ multiple_defined ])
2021- && $ item ->input [$ multiple_defined ]
2022- ) { //multi dropdown is empty or has been emptied
2023- $ data [$ base_name . "_dropdowns_id " ] = [];
2024- $ has_fields = true ;
2025- }
2026- }
2005+ if ($ field ['type ' ] === 'number ' ) {
2006+ $ value = str_replace (', ' , '. ' , $ value );
2007+ }
20272008
2028- //managed multi GLPI item dropdown field
2029- if (preg_match ('/^dropdown-(?<type>.+)$/ ' , $ field ['type ' ], $ match ) === 1 ) {
2030- //values are defined by user
2031- if (isset ($ item ->input [$ field ['name ' ]])) {
2032- $ data [$ base_name ] = $ item ->input [$ field ['name ' ]];
2033- $ has_fields = true ;
2034- } else { //multi dropdown is empty or has been emptied
2035- $ data [$ base_name ] = [];
2036- }
2037- }
2009+ $ data [$ base_name ] = $ value ;
2010+ $ has_fields = true ;
2011+
2012+ // If the field is a richtext
2013+ if ($ field ['type ' ] === 'richtext ' ) {
2014+ foreach (['_ ' . $ htmlKeyWithId , '_prefix_ ' . $ htmlKeyWithId , '_tag_ ' . $ htmlKeyWithId ] as $ extra ) {
2015+ $ data [$ extra ] = $ item ->input [$ extra ];
20382016 }
20392017 }
20402018 }
20412019
2042- if ($ has_fields === true ) {
2043- return $ data ;
2044- } else {
2045- return false ;
2046- }
2020+ return $ has_fields ? $ data : false ;
20472021 }
20482022
20492023 public static function getAddSearchOptions ($ itemtype , $ containers_id = false )
0 commit comments