3232 *
3333 * ---------------------------------------------------------------------
3434 */
35-
3635use Glpi \Application \View \TemplateRenderer ;
3736use Glpi \ContentTemplates \Parameters \CommonITILObjectParameters ;
3837use Glpi \DBAL \QueryExpression ;
@@ -1770,8 +1769,6 @@ public function cleanDBonPurge()
17701769 protected function handleTemplateFields (array $ input , bool $ show_error_message = true )
17711770 {
17721771 //// check mandatory fields
1773- // First get ticket template associated: entity and type/category
1774- $ tt = $ this ->getITILTemplateFromInput ($ input );
17751772
17761773 $ check_allowed_fields_for_template = false ;
17771774 $ allowed_fields = [];
@@ -1858,7 +1855,9 @@ class_exists($validation_class)
18581855 }
18591856 }
18601857
1861- 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 )) {
18621861 $ mandatory_missing = [];
18631862 $ fieldsname = $ tt ->getAllowedFieldsNames (true );
18641863 foreach ($ tt ->mandatory as $ key => $ val ) {
@@ -1967,7 +1966,6 @@ public function prepareInputForUpdate($input)
19671966 if (!$ this ->checkFieldsConsistency ($ input )) {
19681967 return false ;
19691968 }
1970- $ input = $ this ->handleReadonlyFields ($ input );
19711969
19721970 // Add document if needed
19731971 $ this ->getFromDB ($ input ["id " ]); // entities_id field required
@@ -2332,9 +2330,25 @@ public function prepareInputForUpdate($input)
23322330
23332331 return $ input ;
23342332 }
2335- private function handleReadonlyFields (array $ input , bool $ isAdd = false ): array
2333+
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
23362345 {
23372346 $ tt = $ this ->getITILTemplateFromInput ($ input );
2347+ if (!$ tt ) {
2348+ dump ('Template not found ' );
2349+ return $ input ;
2350+ }
2351+
23382352 $ tt ->getFromDBWithData ($ tt ->getID ()); // We load the fields (predefined and readonly)
23392353
23402354 foreach (array_keys ($ tt ->readonly ) as $ read_only_field ) {
@@ -2824,7 +2838,6 @@ public function prepareInputForAdd($input)
28242838 if (!$ this ->checkFieldsConsistency ($ input )) {
28252839 return false ;
28262840 }
2827- $ input = $ this ->handleReadonlyFields ($ input , true );
28282841
28292842 $ input = $ this ->transformActorsInput ($ input );
28302843
@@ -8269,13 +8282,16 @@ public function getITILTemplateToUse(
82698282 * If the input is not defined, it will get it from the object fields datas
82708283 *
82718284 * @param array $input
8272- * @return ITILTemplate
8285+ * @return ITILTemplate|null
82738286 *
82748287 * @since 11.0.2
82758288 */
8276- public function getITILTemplateFromInput (array $ input = []): ITILTemplate
8289+ public function getITILTemplateFromInput (array $ input = []): ? ITILTemplate
82778290 {
8278- $ entid = $ input ['entities_id ' ] ?? $ this ->fields ['entities_id ' ];
8291+ $ entid = $ input ['entities_id ' ] ?? $ this ->fields ['entities_id ' ] ?? $ input ['id ' ] ?? null ;
8292+ if (is_null ($ entid )) {
8293+ return null ;
8294+ }
82798295
82808296 $ type = null ;
82818297 if (isset ($ input ['type ' ])) {
@@ -8285,6 +8301,9 @@ public function getITILTemplateFromInput(array $input = []): ITILTemplate
82858301 }
82868302
82878303 $ categid = $ input ['itilcategories_id ' ] ?? $ this ->fields ['itilcategories_id ' ];
8304+ if (is_null ($ categid )) {
8305+ return null ;
8306+ }
82888307 return $ this ->getITILTemplateToUse (0 , $ type , $ categid , $ entid );
82898308 }
82908309
0 commit comments