forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncDefaultTemplates.php
More file actions
93 lines (82 loc) · 3.54 KB
/
SyncDefaultTemplates.php
File metadata and controls
93 lines (82 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace ProcessMaker\Jobs;
use Exception;
use Facades\ProcessMaker\JsonColumnIndex;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use ProcessMaker\ImportExport\Importer;
use ProcessMaker\ImportExport\Options;
use ProcessMaker\Models\ProcessCategory;
use ProcessMaker\Models\ProcessTemplates;
class SyncDefaultTemplates implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
* Function to handle the execution of this job when it is run.
* Here the function fetches default templates list from Github and saves them to the database.
* @return void
*/
public function handle()
{
$config = config('services.github');
$url = $config['base_url'] . $config['template_repo'] . '/' . $config['template_branch'] . '/index.json';
// If there are multiple categories of templates defined in the .env then separate them into an array.
$categories = (strpos($config['template_categories'], ',') !== false) ? explode(',', $config['template_categories']) : [$config['template_categories']];
$processCategoryId = ProcessCategory::firstOrCreate(
['name' => 'Default Templates'],
[
'name' => 'Default Templates',
'status' => 'ACTIVE',
'is_system' => 0,
]
)->getKey();
// Get the default template list from Github.
$response = Http::get($url);
if (!$response->successful()) {
throw new Exception('Unable to fetch default template list.');
}
// Extract the json data from the response and iterate over the categories and templates to retrieve them.
$data = $response->json();
foreach ($data as $templateCategory => $templates) {
if (!in_array($templateCategory, $categories) && !in_array('all', $categories)) {
continue;
}
foreach ($templates as $template) {
$existingTemplate = ProcessTemplates::where('uuid', $template['uuid'])->first();
// If the template already exists in the database with a user then skip it, since we don't want to overwrite their changes.
if (!is_null($existingTemplate) && !is_null($existingTemplate->user_id)) {
continue;
}
$url = $config['base_url'] . $config['template_repo'] . '/' . $config['template_branch'] . '/' . $template['relative_path'];
$response = Http::get($url);
if (!$response->successful()) {
throw new Exception("Unable to fetch default template {$template['name']}.");
}
$payload = $response->json();
data_set($payload, 'export.' . $payload['root'] . '.attributes.process_category_id', $processCategoryId);
$options = new Options([
'mode' => 'update',
'isTemplate' => true,
'saveAssetsMode' => 'saveAllAssets',
]);
$importer = new Importer($payload, $options);
$manifest = $importer->doImport();
}
}
}
}