@@ -65,7 +65,7 @@ public function handle()
6565 'is_system ' => 1 ,
6666 ])->getKey ();
6767
68- // Fetch the default template list from Github
68+ // Fetch the guided template list from Github
6969 $ response = Http::get ($ url );
7070
7171 // Check if the request was successful
@@ -82,8 +82,12 @@ public function handle()
8282 continue ;
8383 }
8484
85- foreach ($ wizardTemplates as $ template ) {
86- $ this ->importTemplate ($ template , $ config , $ wizardTemplateCategoryId );
85+ try {
86+ foreach ($ wizardTemplates as $ template ) {
87+ $ this ->importTemplate ($ template , $ config , $ wizardTemplateCategoryId );
88+ }
89+ } catch (Exception $ e ) {
90+ Log::error ("Error Importing Guided Templates: {$ e ->getMessage ()}" );
8791 }
8892 }
8993 } catch (Exception $ e ) {
@@ -101,22 +105,19 @@ public function handle()
101105 */
102106 private function importTemplate ($ template , $ config , $ wizardTemplateCategoryId )
103107 {
104- // Configure URLs for the helper process, process template, and collection
108+ // Configure URLs for the helper process, process template
105109 $ helperProcessUrl = $ this ->buildTemplateUrl ($ config , $ template ['helper_process ' ]);
106110 $ processTemplateUrl = $ this ->buildTemplateUrl ($ config , $ template ['template_process ' ]);
107- $ configCollectionUrl = $ this ->buildTemplateUrl ($ config , $ template ['config_collection ' ]);
108111
109- // Get manifests of the exported helper process, process template, and collection
112+ // Get manifests of the exported helper process, process template
110113 $ helperProcessPayload = $ this ->fetchPayload ($ helperProcessUrl );
111114 $ templateProcessPayload = $ this ->fetchPayload ($ processTemplateUrl );
112- $ configCollectionPayload = $ this ->fetchPayload ($ configCollectionUrl );
113115
114116 // Update process categories for the helper process and process template
115117 $ this ->updateProcessCategories ($ helperProcessPayload , $ templateProcessPayload , $ wizardTemplateCategoryId );
116118
117119 // Import the helper process and get the new ID
118120 $ newHelperProcessId = $ this ->importProcess ($ helperProcessPayload , 'WIZARD_HELPER_PROCESS ' );
119-
120121 // Import the process template and get the new ID
121122 $ newProcessTemplateId = $ this ->importProcess ($ templateProcessPayload , 'WIZARD_PROCESS_TEMPLATE ' );
122123
@@ -129,22 +130,6 @@ private function importTemplate($template, $config, $wizardTemplateCategoryId)
129130 // Import template assets and associate with the media collection
130131 $ this ->importTemplateAssets ($ template , $ config , $ mediaCollectionName , $ wizardTemplate );
131132
132- // Import the config collection and update associations
133- $ this ->importConfigCollection ($ configCollectionUrl , $ configCollectionPayload , $ wizardTemplate );
134- // Update the wizard template with the config collection ID
135-
136- // TODO: An issue occurs at this point as there is currently no direct way to update an existing collection when importing a collection.
137- // When importing a collection from the wizard template, a new collection is created each time.
138- // As a temporary solution, we could check if the wizard template already has a config_collection_id associated.
139- // If so, we need to delete the existing collection along with all its assets (screens, process signals, etc.).
140- // Subsequently, we import a new collection and associate it with the wizard template.
141- // This approach prevents the database from being cluttered with orphaned collections and collection assets.
142- // Considerations:
143- // - Could this cause issues with already configured wizard templates?
144- // - Could this cause issues with the helper process/process template if they reference a specific collection?
145- // - Does deleting a collection automatically remove all associated assets?
146- // - How can we implement an import method similar to processes for collections, and what is the expected duration of implementation?
147- $ wizardTemplate ->config_collection_id = $ this ->newConfigCollectionId ;
148133 $ wizardTemplate ->media_collection = $ mediaCollectionName ;
149134 $ wizardTemplate ->save ();
150135 }
@@ -211,9 +196,6 @@ private function importProcess($payload, $assetType)
211196 private function updateOrCreateWizardTemplate ($ template , $ newHelperProcessId , $ newProcessTemplateId )
212197 {
213198 // Update or create the wizard template in the database
214- // TODO: Currently, the index.json in the GitHub repository contains identical wizard template test data for each category,
215- // resulting in a single record creation that gets updated for each template within the category.
216- // This behavior will be corrected once we ensure that each category has only one instance of the test data.
217199
218200 return WizardTemplate::updateOrCreate ([
219201 'name ' => $ template ['template_details ' ]['card-title ' ],
@@ -222,7 +204,6 @@ private function updateOrCreateWizardTemplate($template, $newHelperProcessId, $n
222204 'helper_process_id ' => $ newHelperProcessId ,
223205 'process_template_id ' => $ newProcessTemplateId ,
224206 'media_collection ' => '' ,
225- 'config_collection_id ' => null ,
226207 'template_details ' => json_encode ($ template ['template_details ' ]),
227208 ]);
228209 }
@@ -258,116 +239,4 @@ private function importMedia($assetUrl, $customProperty, $mediaCollectionName, $
258239 // Import a media asset and associate it with the media collection
259240 $ wizardTemplate ->addMediaFromUrl ($ assetUrl )->withCustomProperties (['media_type ' => $ customProperty ])->toMediaCollection ($ mediaCollectionName );
260241 }
261-
262- private function importConfigCollection ($ configCollectionUrl , $ configCollectionPayload , $ wizardTemplate )
263- {
264- // Import the config collection and update associations
265- if (class_exists ('ProcessMaker\Plugins\Collections\Jobs\ImportCollection ' )) {
266- // Obtain the user ID for the admin user
267- $ adminUserId = User::where ('username ' , 'admin ' )->first ()->value ('id ' );
268- // Create a temporary file to store the JSON payload
269- $ tempFileName = $ this ->createTempFile ($ configCollectionUrl , $ configCollectionPayload );
270-
271- // Store the temporary file in the storage directory
272- $ pathInStorage = $ this ->storeInStorage ($ tempFileName );
273-
274- // Import the config collection using the Collection package
275- $ importedData = $ this ->importCollection ($ pathInStorage , $ adminUserId );
276-
277- // Process the imported data and update asset types and associations
278- $ this ->processImportedData ($ importedData );
279- } else {
280- Log::debug ('Error Syncing Guided Templates: The Collection package is not installed. Please install the Collection package to enable full functionality for Guided Templates. ' );
281- }
282- }
283-
284- /**
285- * Create a temporary file and store the JSON payload.
286- *
287- * @param string $configCollectionUrl
288- * @param array $configCollectionPayload
289- * @return string
290- */
291- private function createTempFile ($ configCollectionUrl , $ configCollectionPayload )
292- {
293- $ info = pathinfo ($ configCollectionUrl );
294- $ tempFileName = '/tmp/ ' . $ info ['basename ' ];
295- file_put_contents ($ tempFileName , json_encode ($ configCollectionPayload ));
296-
297- return $ tempFileName ;
298- }
299-
300- /**
301- * Store the temporary file in the storage directory.
302- *
303- * @param string $tempFileName
304- * @return string
305- */
306- private function storeInStorage ($ tempFileName )
307- {
308- return Storage::putFile ('imports ' , $ tempFileName );
309- }
310-
311- /**
312- * Import the config collection using the Collection package.
313- *
314- * @param string $pathInStorage
315- * @param int $adminUserId
316- * @return array
317- */
318- private function importCollection ($ pathInStorage , $ adminUserId )
319- {
320- $ collectionImporterClass = 'ProcessMaker\Plugins\Collections\Jobs\ImportCollection ' ;
321-
322- return (new $ collectionImporterClass ($ pathInStorage , $ adminUserId ))->handle ();
323- }
324-
325- /**
326- * Process the imported data and update asset types and associations.
327- *
328- * @param array $importedData
329- * @return void
330- */
331- private function processImportedData ($ importedData )
332- {
333- foreach ($ importedData as $ element => $ value ) {
334- if (isset ($ value ['uuids ' ])) {
335- foreach ($ value ['uuids ' ] as $ uuid ) {
336- if ($ element === 'collections ' ) {
337- $ this ->updateCollectionAssetType ($ uuid );
338- } elseif ($ element === 'screens ' ) {
339- $ this ->updateScreenAssetType ($ uuid );
340- }
341- }
342- }
343- }
344- }
345-
346- /**
347- * Update the asset type on the imported collection.
348- *
349- * @param string $uuid
350- * @return void
351- */
352- private function updateCollectionAssetType ($ uuid )
353- {
354- $ collectionClass = 'ProcessMaker\Plugins\Collections\Models\Collection ' ;
355- $ importedCollection = (new $ collectionClass )->where ('uuid ' , $ uuid )->first ();
356- $ importedCollection ->asset_type = 'WIZARD_CONFIG_COLLECTION ' ;
357- $ importedCollection ->save ();
358- $ this ->newConfigCollectionId = $ importedCollection ->id ;
359- }
360-
361- /**
362- * Update the asset type for imported collection screens.
363- *
364- * @param string $uuid
365- * @return void
366- */
367- private function updateScreenAssetType ($ uuid )
368- {
369- $ importedCollectionScreen = Screen::where ('uuid ' , $ uuid )->first ();
370- $ importedCollectionScreen ->asset_type = 'WIZARD_CONFIG_COLLECTION_SCREEN ' ;
371- $ importedCollectionScreen ->save ();
372- }
373242}
0 commit comments