forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImporter.php
More file actions
118 lines (94 loc) · 3.71 KB
/
Importer.php
File metadata and controls
118 lines (94 loc) · 3.71 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace ProcessMaker\ImportExport;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Session;
use ProcessMaker\Exception\ImportPasswordException;
use ProcessMaker\Models\Script;
class Importer
{
public $options;
public $payload;
public $manifest;
public $newScriptId;
public $logger;
public function __construct(array $payload, Options $options, $logger = null)
{
$this->payload = $payload;
$this->options = $options;
$this->logger = $logger ?? new Logger();
$this->manifest = $this->loadManifest();
}
public function previewImport()
{
return $this->manifest->toArray(true);
}
public function loadManifest()
{
return Manifest::fromArray($this->payload['export'], $this->options, $this->logger);
}
public function doImport($existingAssetInDatabase = null, $importingFromTemplate = false)
{
$this->logger->log('Starting Transaction');
DB::transaction(function () use ($existingAssetInDatabase, $importingFromTemplate) {
// First, we save the model so we have IDs set for all assets
Schema::disableForeignKeyConstraints();
$count = count(Arr::where($this->manifest->all(), fn ($exporter) => $exporter->mode !== 'discard'));
$this->logger->log("Importing $count assets");
foreach ($this->manifest->all() as $exporter) {
if ($exporter->mode !== 'discard') {
$this->logger->log('Importing ' . get_class($exporter->model));
if ($exporter->disableEventsWhenImporting) {
$exporter->model->saveQuietly();
} else {
$exporter->model->save();
}
$exporter->log('newId', $exporter->model->id);
}
}
Schema::enableForeignKeyConstraints();
// Now, run the import method in each Exporter class
foreach ($this->manifest->all() as $exporter) {
if ($exporter->mode !== 'discard') {
$this->logger->log('Associating ' . get_class($exporter->model));
$exporter->runImport($existingAssetInDatabase, $importingFromTemplate);
}
}
$this->manifest->runAfterImport();
});
$manifest = $this->manifest->all();
$newProcessId = $manifest[$this->payload['root']]->log['newId'];
$this->logger->log('Done Importing', ['processId' => $newProcessId, 'message' => self::getMessages()]);
return $manifest;
}
public static function getMessages()
{
$message = null;
if (Session::get('_alert')) {
$message = Session::get('_alert');
}
return $message;
}
public static function handlePasswordDecrypt(array $payload, string|null $password)
{
if (isset($payload['encrypted']) && $payload['encrypted']) {
if (!$password) {
throw new ImportPasswordException('password required');
}
$payload = (new ExportEncrypted($password))->decrypt($payload);
if ($payload['export'] === null) {
throw new ImportPasswordException('incorrect password');
}
}
return $payload;
}
public static function cleanOldFiles()
{
collect(Storage::listContents('import', true))->each(function ($file) {
if ($file['type'] == 'file' && $file['timestamp'] < now()->subDays(15)->getTimestamp()) {
Storage::disk('public')->delete($file['path']);
}
});
}
}