@@ -1588,85 +1588,87 @@ public static function findContainer($itemtype, $type = 'tab', $subtype = '')
1588
1588
return $ id ;
1589
1589
}
1590
1590
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
1592
1602
{
1593
1603
/** @var DBmysql $DB */
1594
1604
global $ DB ;
1595
- $ ids = [];
1596
1605
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
+ }
1606
1609
1607
- $ entityRestriction = getEntitiesRestrictCriteria ('' , '' , $ glpiActiveEntities , true , true );
1610
+ $ entitiesIds = getAncestorsOf ("glpi_entities " , (string ) $ entityId );
1611
+ $ entitiesIds [] = $ entityId ; // Add entity active itself to the list
1608
1612
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 ,
1623
1622
],
1624
- ];
1623
+ [
1624
+ 'is_recursive ' => 0 ,
1625
+ 'entities_id ' => $ entityId ,
1626
+ ],
1627
+ ],
1628
+ ],
1629
+ ];
1625
1630
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
+ }
1636
1641
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 ' ]);
1647
1648
}
1649
+ }
1650
+ if (!empty ($ allowedEntities )) {
1651
+ $ where ['entities_id ' ] = $ allowedEntities ;
1652
+ }
1653
+ }
1648
1654
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
+ ]);
1666
1660
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 ;
1668
1669
}
1669
1670
}
1671
+ $ ids [] = $ containerId ;
1670
1672
}
1671
1673
1672
1674
return $ ids ;
@@ -1741,35 +1743,29 @@ public static function preItemUpdate(CommonDBTM $item)
1741
1743
*/
1742
1744
public static function preItem (CommonDBTM $ item )
1743
1745
{
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 ;
1752
1751
1753
- $ containers = self ::findContainers ($ item ->getType (), $ type , $ subtype , $ item -> getID () );
1752
+ $ containers = self ::findContainers ($ item ->getType (), $ type , $ subtype , $ entityId );
1754
1753
1755
1754
$ all_data = [];
1756
1755
1757
1756
foreach ($ containers as $ c_id ) {
1758
1757
1759
- $ loc_c = new PluginFieldsContainer ();
1760
- $ loc_c ->getFromDB ($ c_id );
1761
-
1762
1758
// 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 ' ])) {
1765
1760
$ 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
1768
1763
}
1769
- } else {
1770
- continue ;
1771
1764
}
1772
1765
1766
+ $ loc_c = new self ();
1767
+ $ loc_c ->getFromDB ($ c_id );
1768
+
1773
1769
// need to check if container is usable on this object entity
1774
1770
$ entities = [$ loc_c ->fields ['entities_id ' ]];
1775
1771
if ($ loc_c ->fields ['is_recursive ' ]) {
@@ -1781,11 +1777,12 @@ public static function preItem(CommonDBTM $item)
1781
1777
}
1782
1778
1783
1779
if ($ item ->isEntityAssign () && !in_array ($ item ->getEntityID (), $ entities )) {
1784
- continue ;
1780
+ continue ; // not the right entity
1785
1781
}
1786
1782
1787
1783
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
1789
1786
$ item ->input = [];
1790
1787
1791
1788
return false ;
@@ -1828,20 +1825,16 @@ public static function populateData($c_id, CommonDBTM $item)
1828
1825
];
1829
1826
1830
1827
// 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 ;
1838
1830
1839
1831
$ has_fields = false ;
1840
1832
// Prefix for input names
1841
1833
$ prefix = "plugin_fields_ {$ c_id }_ " ;
1842
1834
1843
1835
foreach ($ fields as $ field ) {
1844
1836
$ base_name = $ field ['name ' ];
1837
+ $ isMulti = (bool ) $ field ['multiple ' ];
1845
1838
if ($ field ['type ' ] == 'glpi_item ' ) {
1846
1839
$ itemtype_key = "itemtype_ {$ base_name }" ;
1847
1840
$ items_id_key = "items_id_ {$ base_name }" ;
@@ -1850,9 +1843,9 @@ public static function populateData($c_id, CommonDBTM $item)
1850
1843
continue ; // not a valid input
1851
1844
}
1852
1845
1853
- $ has_fields = true ;
1854
1846
$ data [$ itemtype_key ] = $ item ->input [$ itemtype_key ];
1855
1847
$ data [$ items_id_key ] = $ item ->input [$ items_id_key ];
1848
+ $ has_fields = true ;
1856
1849
1857
1850
continue ; // bypass unique field handling
1858
1851
}
@@ -1861,86 +1854,68 @@ public static function populateData($c_id, CommonDBTM $item)
1861
1854
// "plugin_fields_{$c_id}_{$base_name}"
1862
1855
if ($ field ['type ' ] === 'dropdown ' ) {
1863
1856
// 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 ;
1879
1873
}
1880
1874
}
1881
1875
continue ;
1882
1876
}
1883
1877
1884
1878
// 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 ;
1891
1896
}
1892
- $ data [ $ base_name ] = $ item -> input [ $ input ];
1897
+ }
1893
1898
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
+ }
1898
1902
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
+ }
1924
1906
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 ];
1935
1914
}
1936
1915
}
1937
1916
}
1938
1917
1939
- if ($ has_fields === true ) {
1940
- return $ data ;
1941
- } else {
1942
- return false ;
1943
- }
1918
+ return $ has_fields ? $ data : false ;
1944
1919
}
1945
1920
1946
1921
public static function getAddSearchOptions ($ itemtype , $ containers_id = false )
0 commit comments