@@ -155,95 +155,122 @@ public function addAsset(ProcessMakerModel $asset)
155155 public function addSettings ($ setting , $ newId , $ type = null )
156156 {
157157 $ existingSetting = $ this ->settings ()->where ('setting ' , $ setting )->first ();
158- //If $newId is null, set config to null
159- if (is_null ($ newId ) && is_null ($ type )) {
160- if ($ existingSetting ) {
161- $ existingSetting ->update (['config ' => null ]);
162- } else {
163- BundleSetting::create ([
164- 'bundle_id ' => $ this ->id ,
165- 'setting ' => $ setting ,
166- 'config ' => null ,
167- ]);
168- }
169158
170- return ;
159+ if ($ this ->shouldSetNullConfig ($ newId , $ type )) {
160+ return $ this ->handleNullConfig ($ existingSetting , $ setting );
171161 }
172- // verify if newId is a json with id key
162+
163+ $ decodedNewId = $ this ->parseNewId ($ newId );
164+
165+ if ($ existingSetting ) {
166+ return $ this ->updateExistingSetting ($ existingSetting , $ decodedNewId );
167+ }
168+
169+ $ this ->createNewSetting ($ setting , $ decodedNewId , $ type );
170+ }
171+
172+ private function shouldSetNullConfig ($ newId , $ type )
173+ {
174+ return is_null ($ newId ) && is_null ($ type );
175+ }
176+
177+ private function handleNullConfig ($ existingSetting , $ setting )
178+ {
179+ if ($ existingSetting ) {
180+ $ existingSetting ->update (['config ' => null ]);
181+ } else {
182+ BundleSetting::create ([
183+ 'bundle_id ' => $ this ->id ,
184+ 'setting ' => $ setting ,
185+ 'config ' => null ,
186+ ]);
187+ }
188+ }
189+
190+ private function parseNewId ($ newId )
191+ {
173192 $ decodedNewId = json_decode ($ newId , true );
193+
174194 if (json_last_error () === JSON_ERROR_NONE ) {
175195 if (is_array ($ decodedNewId )) {
176- $ newId = $ decodedNewId ;
177- if ($ existingSetting ) {
178- if ($ decodedNewId ['id ' ] === []) {
179- $ existingSetting ->delete ();
180-
181- return ;
182- }
183- $ existingSetting ->update (['config ' => json_encode ($ newId )]);
184-
185- return ;
186- }
196+ return $ decodedNewId ;
187197 } elseif (isset ($ decodedNewId ['id ' ])) {
188- $ newId = [$ decodedNewId ['id ' ]];
189- } else {
190- $ newId = ['id ' => [$ decodedNewId ]];
198+ return ['id ' => [$ decodedNewId ['id ' ]]];
191199 }
192- } else {
193- $ newId = ['id ' => [$ newId ]];
200+
201+ return ['id ' => [$ decodedNewId ]];
194202 }
195203
196- if ($ existingSetting ) {
197- // Decode the existing JSON
198- $ config = json_decode ($ existingSetting ->config , true );
199- // Ensure 'id' is an array
200- if (!isset ($ config ['id ' ]) || !is_array ($ config ['id ' ])) {
201- $ config ['id ' ] = [];
202- }
203- // Add the new ID
204+ return ['id ' => [$ newId ]];
205+ }
206+
207+ private function updateExistingSetting ($ existingSetting , $ decodedNewId )
208+ {
209+ if (isset ($ decodedNewId ['id ' ]) && $ decodedNewId ['id ' ] === []) {
210+ $ existingSetting ->delete ();
211+
212+ return ;
213+ }
214+
215+ $ config = json_decode ($ existingSetting ->config , true ) ?: ['id ' => []];
216+
217+ if (!isset ($ config ['id ' ]) || !is_array ($ config ['id ' ])) {
218+ $ config ['id ' ] = [];
219+ }
220+
221+ foreach ($ decodedNewId ['id ' ] as $ id ) {
222+ $ config ['id ' ][] = $ id ;
223+ }
224+
225+ $ config ['id ' ] = array_values (array_unique ($ config ['id ' ]));
226+ $ existingSetting ->update (['config ' => json_encode ($ config )]);
227+ }
228+
229+ private function createNewSetting ($ setting , $ newId , $ type )
230+ {
231+ $ config = ['id ' => []];
232+
233+ if ($ type === 'settings ' ) {
234+ $ config = $ this ->getSettingsConfig ($ setting );
235+ } elseif ($ type === 'ui_settings ' ) {
236+ $ config = $ this ->getUiSettingsConfig ();
237+ } elseif ($ newId && $ type !== 'settings ' ) {
204238 foreach ($ newId ['id ' ] as $ id ) {
205239 $ config ['id ' ][] = $ id ;
206240 }
207- // Remove duplicates
208- $ config ['id ' ] = array_unique ($ config ['id ' ]);
209- //reindex the array
210- $ config ['id ' ] = array_values ($ config ['id ' ]);
211- // Update the config
212- $ existingSetting ->update ([
213- 'config ' => json_encode ($ config ),
214- ]);
215- } else {
216- // Create a new BundleSetting with the initial ID
217- $ config = ['id ' => []];
218- if ($ newId && $ type !== 'settings ' ) {
219- foreach ($ newId ['id ' ] as $ id ) {
220- $ config ['id ' ][] = $ id ;
221- }
222- }
241+ }
223242
224- if ($ type === 'settings ' ) {
225- $ settingsMenu = SettingsMenus::where ('menu_group ' , $ setting )->first ();
226- $ settingsKeys = Setting::where ([
227- ['group_id ' , '= ' , $ settingsMenu ->id ],
228- ['hidden ' , '= ' , false ],
229- ])->pluck ('key ' )->toArray ();
230- if ($ settingsMenu ) {
231- $ config ['id ' ] = $ settingsKeys ;
232- $ config ['type ' ] = $ type ;
233- }
234- }
243+ BundleSetting::create ([
244+ 'bundle_id ' => $ this ->id ,
245+ 'setting ' => $ setting ,
246+ 'config ' => json_encode ($ config ),
247+ ]);
248+ }
235249
236- if ( $ type === ' ui_settings ' ) {
237- $ config [ ' id ' ] = [ ' css-override ' , ' login-footer ' , ' logo-alt-text ' ];
238- $ config[ ' type ' ] = $ type ;
239- }
250+ private function getSettingsConfig ( $ setting )
251+ {
252+ $ config = [ ' id ' => []] ;
253+ $ settingsMenu = SettingsMenus:: where ( ' menu_group ' , $ setting )-> first ();
240254
241- BundleSetting::create ([
242- 'bundle_id ' => $ this ->id ,
243- 'setting ' => $ setting ,
244- 'config ' => json_encode ($ config ),
245- ]);
255+ if ($ settingsMenu ) {
256+ $ settingsKeys = Setting::where ([
257+ ['group_id ' , '= ' , $ settingsMenu ->id ],
258+ ['hidden ' , '= ' , false ],
259+ ])->pluck ('key ' )->toArray ();
260+
261+ $ config ['id ' ] = $ settingsKeys ;
262+ $ config ['type ' ] = 'settings ' ;
246263 }
264+
265+ return $ config ;
266+ }
267+
268+ private function getUiSettingsConfig ()
269+ {
270+ return [
271+ 'id ' => ['css-override ' , 'login-footer ' , 'logo-alt-text ' ],
272+ 'type ' => 'ui_settings ' ,
273+ ];
247274 }
248275
249276 public function addAssetToBundles (ProcessMakerModel $ asset )
@@ -349,7 +376,7 @@ public function installSettingsPayloads(array $payloads, $mode, $logger = null)
349376 foreach ($ payloads as $ payload ) {
350377 if (isset ($ payload [0 ]['export ' ])) {
351378 $ logger ->status ('Installing bundle settings on the this instance ' );
352- $ logger ->setSteps ($ payloads [0 ]);
379+ $ logger ->setSteps ($ payload [0 ]);
353380 $ assets [] = DevLink::import ($ payload [0 ], $ options , $ logger );
354381 } elseif (isset ($ payload [0 ]['setting_type ' ])) {
355382 foreach ($ payload as $ setting ) {
0 commit comments