5252use Glpi \RichText \UserMention ;
5353use Glpi \Search \Output \HTMLSearchOutput ;
5454use Glpi \Team \Team ;
55+ use Glpi \Urgency ;
5556use Safe \Exceptions \DatetimeException ;
5657
5758use function Safe \getimagesize ;
@@ -1768,17 +1769,6 @@ public function cleanDBonPurge()
17681769 protected function handleTemplateFields (array $ input , bool $ show_error_message = true )
17691770 {
17701771 //// check mandatory fields
1771- // First get ticket template associated: entity and type/category
1772- $ entid = $ input ['entities_id ' ] ?? $ this ->fields ['entities_id ' ];
1773-
1774- $ type = null ;
1775- if (isset ($ input ['type ' ])) {
1776- $ type = $ input ['type ' ];
1777- } elseif (isset ($ this ->fields ['type ' ])) {
1778- $ type = $ this ->fields ['type ' ];
1779- }
1780-
1781- $ categid = $ input ['itilcategories_id ' ] ?? $ this ->fields ['itilcategories_id ' ];
17821772
17831773 $ check_allowed_fields_for_template = false ;
17841774 $ allowed_fields = [];
@@ -1865,9 +1855,9 @@ class_exists($validation_class)
18651855 }
18661856 }
18671857
1868- $ tt = $ this -> getITILTemplateToUse ( 0 , $ type , $ categid , $ entid );
1869-
1870- if (count ($ tt ->mandatory )) {
1858+ // First get ticket template associated: entity and type/category
1859+ $ tt = $ this -> getITILTemplateFromInput ( $ input );
1860+ if ($ tt && count ($ tt ->mandatory )) {
18711861 $ mandatory_missing = [];
18721862 $ fieldsname = $ tt ->getAllowedFieldsNames (true );
18731863 foreach ($ tt ->mandatory as $ key => $ val ) {
@@ -2341,6 +2331,40 @@ public function prepareInputForUpdate($input)
23412331 return $ input ;
23422332 }
23432333
2334+ /**
2335+ * Processes readonly fields in the input array based on the ITIL template data.
2336+ *
2337+ * @param array $input The user input data to process (often $_POST).
2338+ * @param bool $isAdd true if we are in a creation, will force to apply the template predefined field.
2339+ *
2340+ * @return array The modified user input array after processing readonly fields.
2341+ *
2342+ * @since 11.0.2
2343+ */
2344+ public function enforceReadonlyFields (array $ input , bool $ isAdd = false ): array
2345+ {
2346+ $ tt = $ this ->getITILTemplateFromInput ($ input );
2347+ if (!$ tt ) {
2348+ return $ input ;
2349+ }
2350+
2351+ $ tt ->getFromDBWithData ($ tt ->getID ()); // We load the fields (predefined and readonly)
2352+
2353+ foreach (array_keys ($ tt ->readonly ) as $ read_only_field ) {
2354+ if ($ isAdd && array_key_exists ($ read_only_field , $ tt ->predefined )) {
2355+ $ input [$ read_only_field ] = $ tt ->predefined [$ read_only_field ];
2356+ continue ;
2357+ }
2358+
2359+ if (array_key_exists ($ read_only_field , $ this ->fields )) {
2360+ $ input [$ read_only_field ] = $ this ->fields [$ read_only_field ];
2361+ } else {
2362+ unset($ input [$ read_only_field ]);
2363+ }
2364+ }
2365+ return $ input ;
2366+ }
2367+
23442368 public function post_updateItem ($ history = true )
23452369 {
23462370 // Handle rich-text images and uploaded documents
@@ -2824,7 +2848,7 @@ public function prepareInputForAdd($input)
28242848 }
28252849
28262850 // save value before clean;
2827- $ title = ltrim ($ input ['name ' ]);
2851+ $ title = ltrim ($ input ['name ' ] ?? '' );
28282852
28292853 // Set default status to avoid notice
28302854 if (!isset ($ input ["status " ])) {
@@ -2835,7 +2859,7 @@ public function prepareInputForAdd($input)
28352859 !isset ($ input ["urgency " ])
28362860 || !($ CFG_GLPI ['urgency_mask ' ] & (1 << $ input ["urgency " ]))
28372861 ) {
2838- $ input ["urgency " ] = 3 ;
2862+ $ input ["urgency " ] = Urgency:: MEDIUM -> value ;
28392863 }
28402864 if (
28412865 !isset ($ input ["impact " ])
@@ -2886,8 +2910,8 @@ public function prepareInputForAdd($input)
28862910 }
28872911
28882912 // No name set name
2889- $ input ["name " ] = ltrim ($ input ["name " ]);
2890- $ input ['content ' ] = ltrim ($ input ['content ' ]);
2913+ $ input ["name " ] = ltrim ($ input ["name " ] ?? '' );
2914+ $ input ['content ' ] = ltrim ($ input ['content ' ] ?? '' );
28912915 if (empty ($ input ["name " ])) {
28922916 // Build name based on content
28932917
@@ -8252,6 +8276,33 @@ public function getITILTemplateToUse(
82528276 return $ tt ;
82538277 }
82548278
8279+ /**
8280+ * Get the template to use
8281+ * If the input is not defined, it will get it from the object fields datas
8282+ *
8283+ * @param array $input
8284+ * @return ITILTemplate|null
8285+ *
8286+ * @since 11.0.2
8287+ */
8288+ public function getITILTemplateFromInput (array $ input = []): ?ITILTemplate
8289+ {
8290+ $ entid = $ input ['entities_id ' ] ?? $ this ->fields ['entities_id ' ];
8291+
8292+ $ type = null ;
8293+ if (isset ($ input ['type ' ])) {
8294+ $ type = $ input ['type ' ];
8295+ } elseif (isset ($ this ->fields ['type ' ])) {
8296+ $ type = $ this ->fields ['type ' ];
8297+ }
8298+
8299+ $ categid = $ input ['itilcategories_id ' ] ?? $ this ->fields ['itilcategories_id ' ] ?? null ;
8300+ if (is_null ($ categid )) {
8301+ return null ;
8302+ }
8303+ return $ this ->getITILTemplateToUse (0 , $ type , $ categid , $ entid );
8304+ }
8305+
82558306 /**
82568307 * Get template field name
82578308 *
0 commit comments