7676use Glpi \Form \Destination \FormDestinationTicket ;
7777use Glpi \Form \Migration \TypesConversionMapper ;
7878use Glpi \Form \QuestionType \QuestionTypesManager ;
79+ use Glpi \Plugin \Hooks ;
7980use Symfony \Component \Yaml \Yaml ;
8081
8182/**
@@ -99,116 +100,123 @@ function plugin_init_fields()
99100 $ pluginfields_autoloader = new PluginFieldsAutoloader ([PLUGINFIELDS_CLASS_PATH ]);
100101 $ pluginfields_autoloader ->register ();
101102
102- if ((Session:: getLoginUserID () || isCommandLine ()) && Plugin::isPluginActive ('fields ' )) {
103- // Init hook about itemtype(s) for plugin fields
104- if (! isset ( $ PLUGIN_HOOKS [' plugin_fields ' ])) {
105- $ PLUGIN_HOOKS [ ' plugin_fields ' ] = [];
106- }
103+ if (Plugin::isPluginActive ('fields ' )) {
104+ // This API integration cannot be done inside a login check since the plugin is initialized before the Router handles authentication
105+ $ PLUGIN_HOOKS [Hooks:: REDEFINE_API_SCHEMAS ][ ' fields ' ] = ' plugin_fields_redefine_api_schemas ' ;
106+
107+ if ((Session:: getLoginUserID () || isCommandLine ())) {
107108
108- // When a Category is changed during ticket creation
109- if (
110- $ _POST !== []
111- && isset ($ _POST ['_plugin_fields_type ' ])
112- && ($ _SERVER ['REQUEST_URI ' ] == Ticket::getFormURL ())
113- ) {
114- foreach ($ _POST as $ key => $ value ) {
115- if (!is_array ($ value )) {
116- $ _SESSION ['plugin ' ]['fields ' ]['values_sent ' ][$ key ] = $ value ;
109+ // Init hook about itemtype(s) for plugin fields
110+ if (!isset ($ PLUGIN_HOOKS ['plugin_fields ' ])) {
111+ $ PLUGIN_HOOKS ['plugin_fields ' ] = [];
112+ }
113+
114+ // When a Category is changed during ticket creation
115+ if (
116+ $ _POST !== []
117+ && isset ($ _POST ['_plugin_fields_type ' ])
118+ && ($ _SERVER ['REQUEST_URI ' ] == Ticket::getFormURL ())
119+ ) {
120+ foreach ($ _POST as $ key => $ value ) {
121+ if (!is_array ($ value )) {
122+ $ _SESSION ['plugin ' ]['fields ' ]['values_sent ' ][$ key ] = $ value ;
123+ }
117124 }
118125 }
119- }
120126
121- if (Plugin::isPluginActive ('fusioninventory ' )) {
122- $ PLUGIN_HOOKS ['fusioninventory_inventory ' ]['fields ' ]
123- = ['PluginFieldsInventory ' , 'updateInventory ' ];
124- }
127+ if (Plugin::isPluginActive ('fusioninventory ' )) {
128+ $ PLUGIN_HOOKS ['fusioninventory_inventory ' ]['fields ' ]
129+ = ['PluginFieldsInventory ' , 'updateInventory ' ];
130+ }
125131
126- // complete rule engine
127- $ PLUGIN_HOOKS ['use_rules ' ]['fields ' ] = ['PluginFusioninventoryTaskpostactionRule ' ];
128- $ PLUGIN_HOOKS ['rule_matched ' ]['fields ' ] = 'plugin_fields_rule_matched ' ;
132+ // complete rule engine
133+ $ PLUGIN_HOOKS ['use_rules ' ]['fields ' ] = ['PluginFusioninventoryTaskpostactionRule ' ];
134+ $ PLUGIN_HOOKS ['rule_matched ' ]['fields ' ] = 'plugin_fields_rule_matched ' ;
129135
130- if (isset ($ _SESSION ['glpiactiveentities ' ])) {
131- // add link in plugin page
132- $ PLUGIN_HOOKS ['config_page ' ]['fields ' ] = 'front/container.php ' ;
136+ if (isset ($ _SESSION ['glpiactiveentities ' ])) {
137+ // add link in plugin page
138+ $ PLUGIN_HOOKS ['config_page ' ]['fields ' ] = 'front/container.php ' ;
133139
134- // add entry to configuration menu (only if user has read access to config)
135- if (Session::haveRight ('config ' , READ )) {
136- $ PLUGIN_HOOKS ['menu_toadd ' ]['fields ' ] = ['config ' => PluginFieldsMenu::class];
137- }
140+ // add entry to configuration menu (only if user has read access to config)
141+ if (Session::haveRight ('config ' , READ )) {
142+ $ PLUGIN_HOOKS ['menu_toadd ' ]['fields ' ] = ['config ' => PluginFieldsMenu::class];
143+ }
138144
139- // add tabs to itemtypes
140- $ itemtypes = array_unique (PluginFieldsContainer::getEntries ());
141- if ($ itemtypes !== []) {
142- Plugin::registerClass (
143- 'PluginFieldsContainer ' ,
144- ['addtabon ' => $ itemtypes ],
145- );
146- }
145+ // add tabs to itemtypes
146+ $ itemtypes = array_unique (PluginFieldsContainer::getEntries ());
147+ if ($ itemtypes !== []) {
148+ Plugin::registerClass (
149+ 'PluginFieldsContainer ' ,
150+ ['addtabon ' => $ itemtypes ],
151+ );
152+ }
147153
148- //include js and css
149- $ debug = (isset ($ _SESSION ['glpi_use_mode ' ])
150- && $ _SESSION ['glpi_use_mode ' ] == Session::DEBUG_MODE );
151- if (!$ debug && file_exists (__DIR__ . '/public/css/fields.min.css ' )) {
152- $ PLUGIN_HOOKS ['add_css ' ]['fields ' ][] = 'css/fields.min.css ' ;
153- } else {
154- $ PLUGIN_HOOKS ['add_css ' ]['fields ' ][] = 'css/fields.scss ' ;
155- }
154+ //include js and css
155+ $ debug = (isset ($ _SESSION ['glpi_use_mode ' ])
156+ && $ _SESSION ['glpi_use_mode ' ] == Session::DEBUG_MODE );
157+ if (!$ debug && file_exists (__DIR__ . '/public/css/fields.min.css ' )) {
158+ $ PLUGIN_HOOKS ['add_css ' ]['fields ' ][] = 'css/fields.min.css ' ;
159+ } else {
160+ $ PLUGIN_HOOKS ['add_css ' ]['fields ' ][] = 'css/fields.scss ' ;
161+ }
156162
157- // Add/delete profiles to automaticaly to container
158- $ PLUGIN_HOOKS ['item_add ' ]['fields ' ]['Profile ' ] = ['PluginFieldsProfile ' , 'addNewProfile ' ];
159- $ PLUGIN_HOOKS ['pre_item_purge ' ]['fields ' ]['Profile ' ] = ['PluginFieldsProfile ' , 'deleteProfile ' ];
163+ // Add/delete profiles to automaticaly to container
164+ $ PLUGIN_HOOKS ['item_add ' ]['fields ' ]['Profile ' ] = ['PluginFieldsProfile ' , 'addNewProfile ' ];
165+ $ PLUGIN_HOOKS ['pre_item_purge ' ]['fields ' ]['Profile ' ] = ['PluginFieldsProfile ' , 'deleteProfile ' ];
160166
161- //load drag and drop javascript library on Package Interface
167+ //load drag and drop javascript library on Package Interface
162168
163- if (
164- plugin_fields_script_endswith ('container.form.php ' )
165- ) {
166- $ PLUGIN_HOOKS ['add_javascript ' ]['fields ' ][] = 'lib/redips-drag-min.js ' ;
167- if (!$ debug && file_exists (__DIR__ . '/public/js/drag-field-row.min.js ' )) {
168- $ PLUGIN_HOOKS ['add_javascript ' ]['fields ' ][] = 'js/drag-field-row.min.js ' ;
169- } else {
170- $ PLUGIN_HOOKS ['add_javascript ' ]['fields ' ][] = 'js/drag-field-row.js ' ;
169+ if (
170+ plugin_fields_script_endswith ('container.form.php ' )
171+ ) {
172+ $ PLUGIN_HOOKS ['add_javascript ' ]['fields ' ][] = 'lib/redips-drag-min.js ' ;
173+ if (!$ debug && file_exists (__DIR__ . '/public/js/drag-field-row.min.js ' )) {
174+ $ PLUGIN_HOOKS ['add_javascript ' ]['fields ' ][] = 'js/drag-field-row.min.js ' ;
175+ } else {
176+ $ PLUGIN_HOOKS ['add_javascript ' ]['fields ' ][] = 'js/drag-field-row.js ' ;
177+ }
171178 }
172179 }
173- }
174180
175- // Add Fields to Datainjection
176- if (Plugin::isPluginActive ('datainjection ' )) {
177- $ PLUGIN_HOOKS ['plugin_datainjection_populate ' ]['fields ' ] = 'plugin_datainjection_populate_fields ' ;
178- }
181+ // Add Fields to Datainjection
182+ if (Plugin::isPluginActive ('datainjection ' )) {
183+ $ PLUGIN_HOOKS ['plugin_datainjection_populate ' ]['fields ' ] = 'plugin_datainjection_populate_fields ' ;
184+ }
179185
180- //Retrieve dom container
181- $ itemtypes = PluginFieldsContainer::getUsedItemtypes ();
182- if ($ itemtypes !== false ) {
183- foreach ($ itemtypes as $ itemtype ) {
184- $ PLUGIN_HOOKS ['pre_item_update ' ]['fields ' ][$ itemtype ] = [
185- 'PluginFieldsContainer ' ,
186- 'preItemUpdate ' ,
187- ];
188- $ PLUGIN_HOOKS ['pre_item_add ' ]['fields ' ][$ itemtype ] = [
189- 'PluginFieldsContainer ' ,
190- 'preItem ' ,
191- ];
192- $ PLUGIN_HOOKS ['item_add ' ]['fields ' ][$ itemtype ] = [
193- 'PluginFieldsContainer ' ,
194- 'postItemAdd ' ,
195- ];
196- $ PLUGIN_HOOKS ['pre_item_purge ' ] ['fields ' ][$ itemtype ] = [
197- 'PluginFieldsContainer ' ,
198- 'preItemPurge ' ,
199- ];
186+ //Retrieve dom container
187+ $ itemtypes = PluginFieldsContainer::getUsedItemtypes ();
188+ if ($ itemtypes !== false ) {
189+ foreach ($ itemtypes as $ itemtype ) {
190+ $ PLUGIN_HOOKS ['pre_item_update ' ]['fields ' ][$ itemtype ] = [
191+ 'PluginFieldsContainer ' ,
192+ 'preItemUpdate ' ,
193+ ];
194+ $ PLUGIN_HOOKS ['pre_item_add ' ]['fields ' ][$ itemtype ] = [
195+ 'PluginFieldsContainer ' ,
196+ 'preItem ' ,
197+ ];
198+ $ PLUGIN_HOOKS ['item_add ' ]['fields ' ][$ itemtype ] = [
199+ 'PluginFieldsContainer ' ,
200+ 'postItemAdd ' ,
201+ ];
202+ $ PLUGIN_HOOKS ['pre_item_purge ' ] ['fields ' ][$ itemtype ] = [
203+ 'PluginFieldsContainer ' ,
204+ 'preItemPurge ' ,
205+ ];
206+ }
200207 }
201- }
202208
203- // Display fields in any existing tab
204- $ PLUGIN_HOOKS ['post_item_form ' ]['fields ' ] = [
205- 'PluginFieldsField ' ,
206- 'showForTab ' ,
207- ];
209+ // Display fields in any existing tab
210+ $ PLUGIN_HOOKS ['post_item_form ' ]['fields ' ] = [
211+ 'PluginFieldsField ' ,
212+ 'showForTab ' ,
213+ ];
208214
209- // Register fields question type
210- plugin_fields_register_plugin_types ();
215+ // Register fields question type
216+ plugin_fields_register_plugin_types ();
217+ }
211218 }
219+
212220}
213221
214222
0 commit comments