Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 51 additions & 29 deletions ProcessMaker/ImportExport/Dependent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function __construct(
public string $type,
public string $uuid,
public Manifest $manifest,
public $meta,
public mixed $meta,
public string $exporterClass,
public string $modelClass,
public array $fallbackMatches,
Expand Down Expand Up @@ -45,45 +45,67 @@ public static function fromArray(array $array, Manifest $manifest)
}, $array);
}

public function __get($property)
public function __get(string $property)
{
$asset = $this->manifest->get($this->uuid);
$value = null;

if ($property === 'model' && !$asset) {
// Attempt to reconstruct discarded model if it exists on the target instance
$assetInfo = [
'model' => $this->modelClass,
'attributes' => $this->fallbackMatches,
];

list($_, $model) = Manifest::getModel($this->uuid, $assetInfo, 'discard', $this->exporterClass, false);

// Only return the model if it is persisted in the database
if ($model && $model->exists) {
return $model;
}
$value = $this->getDiscardedModel();
} elseif ($property === 'mode') {
$value = $this->getMode($asset);
} elseif ($property === 'name') {
$value = $this->getName($asset);
} elseif ($asset) {
$value = $asset->$property;
}

if ($property === 'mode') {
if ($asset) {
return $asset->mode;
} else {
return 'discard';
}
return $value;
}

private function getDiscardedModel()
{
if ($this->canUseDiscardedDependentFinder()) {
return $this->exporterClass::findDiscardedDependentModel($this);
}

if ($property === 'name') {
if ($asset) {
return $asset->getName($this->model);
} else {
return '';
}
return $this->findPersistedDiscardedModel();
}

private function canUseDiscardedDependentFinder(): bool
{
if (!method_exists($this->exporterClass, 'findDiscardedDependentModel')) {
return false;
}

if (!$asset) {
return null;
if (!method_exists($this->exporterClass, 'shouldFindDiscardedDependentModel')) {
return true;
}

return $asset->$property;
return $this->exporterClass::shouldFindDiscardedDependentModel($this);
}

private function findPersistedDiscardedModel()
{
// Attempt to reconstruct discarded model if it exists on the target instance
$assetInfo = [
'model' => $this->modelClass,
'attributes' => $this->fallbackMatches,
];

[, $model] = Manifest::getModel($this->uuid, $assetInfo, 'discard', $this->exporterClass, false);

// Only return the model if it is persisted in the database
return $model && $model->exists ? $model : null;
}

private function getMode(mixed $asset): string
{
return $asset ? $asset->mode : 'discard';
}

private function getName(mixed $asset): string
{
return $asset ? $asset->getName($this->model) : '';
}
}
19 changes: 19 additions & 0 deletions ProcessMaker/ImportExport/Exporters/GroupExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace ProcessMaker\ImportExport\Exporters;

use Illuminate\Support\Facades\Log;
use ProcessMaker\ImportExport\Dependent;
use ProcessMaker\ImportExport\DependentType;
use ProcessMaker\Models\Group;
use ProcessMaker\Models\Permission;

class GroupExporter extends ExporterBase
Expand All @@ -13,6 +16,22 @@ class GroupExporter extends ExporterBase

public $discard = true;

public static function shouldFindDiscardedDependentModel(Dependent $dependent): bool
{
return $dependent->type === DependentType::GROUP_ASSIGNMENT;
}

public static function findDiscardedDependentModel(Dependent $dependent): ?Group
{
$name = $dependent->fallbackMatches['name'] ?? null;

if (!$name) {
return null;
}

return Group::where('name', $name)->first();
}

public function export() : void
{
// Skipping user expansion to avoid exporting entire group membership (can be tens of thousands).
Expand Down
Loading
Loading