@@ -1770,8 +1770,6 @@ public function cleanDBonPurge()
17701770 protected function handleTemplateFields (array $ input , bool $ show_error_message = true )
17711771 {
17721772 //// check mandatory fields
1773- // First get ticket template associated: entity and type/category
1774- $ tt = $ this ->getITILTemplateFromInput ($ input );
17751773
17761774 $ check_allowed_fields_for_template = false ;
17771775 $ allowed_fields = [];
@@ -1858,7 +1856,9 @@ class_exists($validation_class)
18581856 }
18591857 }
18601858
1861- if (count ($ tt ->mandatory )) {
1859+ // First get ticket template associated: entity and type/category
1860+ $ tt = $ this ->getITILTemplateFromInput ($ input );
1861+ if ($ tt && count ($ tt ->mandatory )) {
18621862 $ mandatory_missing = [];
18631863 $ fieldsname = $ tt ->getAllowedFieldsNames (true );
18641864 foreach ($ tt ->mandatory as $ key => $ val ) {
@@ -1967,7 +1967,6 @@ public function prepareInputForUpdate($input)
19671967 if (!$ this ->checkFieldsConsistency ($ input )) {
19681968 return false ;
19691969 }
1970- $ input = $ this ->handleReadonlyFields ($ input );
19711970
19721971 // Add document if needed
19731972 $ this ->getFromDB ($ input ["id " ]); // entities_id field required
@@ -2332,9 +2331,25 @@ public function prepareInputForUpdate($input)
23322331
23332332 return $ input ;
23342333 }
2335- private function handleReadonlyFields (array $ input , bool $ isAdd = false ): array
2334+
2335+ /**
2336+ * Processes readonly fields in the input array based on the ITIL template data.
2337+ *
2338+ * @param array $input The user input data to process (often $_POST).
2339+ * @param bool $isAdd true if we are in a creation, will force to apply the template predefined field.
2340+ *
2341+ * @return array The modified user input array after processing readonly fields.
2342+ *
2343+ * @since 11.0.2
2344+ */
2345+ public function enforceReadonlyFields (array $ input , bool $ isAdd = false ): array
23362346 {
23372347 $ tt = $ this ->getITILTemplateFromInput ($ input );
2348+ if (!$ tt ) {
2349+ dump ('Template not found ' );
2350+ return $ input ;
2351+ }
2352+
23382353 $ tt ->getFromDBWithData ($ tt ->getID ()); // We load the fields (predefined and readonly)
23392354
23402355 foreach (array_keys ($ tt ->readonly ) as $ read_only_field ) {
@@ -2824,7 +2839,6 @@ public function prepareInputForAdd($input)
28242839 if (!$ this ->checkFieldsConsistency ($ input )) {
28252840 return false ;
28262841 }
2827- $ input = $ this ->handleReadonlyFields ($ input , true );
28282842
28292843 $ input = $ this ->transformActorsInput ($ input );
28302844
@@ -8269,13 +8283,16 @@ public function getITILTemplateToUse(
82698283 * If the input is not defined, it will get it from the object fields datas
82708284 *
82718285 * @param array $input
8272- * @return ITILTemplate
8286+ * @return ITILTemplate|null
82738287 *
82748288 * @since 11.0.2
82758289 */
8276- public function getITILTemplateFromInput (array $ input = []): ITILTemplate
8290+ public function getITILTemplateFromInput (array $ input = []): ? ITILTemplate
82778291 {
8278- $ entid = $ input ['entities_id ' ] ?? $ this ->fields ['entities_id ' ];
8292+ $ entid = $ input ['entities_id ' ] ?? $ this ->fields ['entities_id ' ] ?? $ input ['id ' ] ?? null ;
8293+ if (is_null ($ entid )) {
8294+ return null ;
8295+ }
82798296
82808297 $ type = null ;
82818298 if (isset ($ input ['type ' ])) {
@@ -8285,6 +8302,9 @@ public function getITILTemplateFromInput(array $input = []): ITILTemplate
82858302 }
82868303
82878304 $ categid = $ input ['itilcategories_id ' ] ?? $ this ->fields ['itilcategories_id ' ];
8305+ if (is_null ($ categid )) {
8306+ return null ;
8307+ }
82888308 return $ this ->getITILTemplateToUse (0 , $ type , $ categid , $ entid );
82898309 }
82908310
0 commit comments