Skip to content

Commit

Permalink
Fix export/import elements(Container, Image) with definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Jan 15, 2025
1 parent 1f74d7b commit b492329
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
19 changes: 17 additions & 2 deletions modules/template/services/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace humhub\modules\custom_pages\modules\template\services;

use humhub\modules\custom_pages\modules\template\models\ContainerContentDefinition;
use humhub\modules\custom_pages\modules\template\models\ContentDefinition;
use humhub\modules\custom_pages\modules\template\models\OwnerContent;
use humhub\modules\custom_pages\modules\template\models\Template;
use humhub\modules\custom_pages\modules\template\models\TemplateContentActiveRecord;
Expand Down Expand Up @@ -52,7 +54,18 @@ public function export(): self

$contentObject = $defaultContent->getInstance();
if ($contentObject instanceof TemplateContentActiveRecord) {
$this->data['elements'][$e]['ownerContent']['contentObject'] = $contentObject->attributes;
$contentData = $contentObject->attributes;

if ($contentObject->hasDefinition()) {
$definition = $contentObject->getDefinition();
if ($definition instanceof ContentDefinition) {
$contentData['definitionClass'] = get_class($definition);
$contentData['definitionObject'] = $definition->attributes;
if ($definition instanceof ContainerContentDefinition) {
$contentData['definitionTemplates'] = array_column($definition->getTemplates(), 'id');
}
}
}

// Attach files
$files = [];
Expand All @@ -68,8 +81,10 @@ public function export(): self
}
}
if ($files !== []) {
$this->data['elements'][$e]['ownerContent']['contentObject']['attachedFiles'] = $files;
$contentData['attachedFiles'] = $files;
}

$this->data['elements'][$e]['ownerContent']['contentObject'] = $contentData;
}
}
}
Expand Down
24 changes: 20 additions & 4 deletions modules/template/services/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace humhub\modules\custom_pages\modules\template\services;

use humhub\components\ActiveRecord;
use humhub\modules\custom_pages\modules\template\models\ContainerContentDefinition;
use humhub\modules\custom_pages\modules\template\models\ContainerContentTemplate;
use humhub\modules\custom_pages\modules\template\models\OwnerContent;
use humhub\modules\custom_pages\modules\template\models\Template;
use humhub\modules\custom_pages\modules\template\models\TemplateContentActiveRecord;
Expand All @@ -17,7 +18,7 @@
use humhub\modules\file\models\FileContent;
use yii\base\InvalidConfigException;
use Yii;
use yii\db\ActiveRecord as BaseActiveRecord;
use yii\db\ActiveRecord;

class ImportService
{
Expand Down Expand Up @@ -79,7 +80,7 @@ public function run(): bool
return !$this->hasErrors();
}

private function saveRecord(BaseActiveRecord $record): ?BaseActiveRecord
private function saveRecord(ActiveRecord $record): ?ActiveRecord
{
if ($record->validate() && $record->save()) {
return $record;
Expand Down Expand Up @@ -198,12 +199,27 @@ private function createObjectByData(string $class, array $data): ?ActiveRecord
}

foreach ($data as $name => $value) {
if ($name === 'id' || is_array($value)) {
if ($name === 'id' || $name === 'definitionClass' || is_array($value)) {
continue;
}
$object->$name = $value;
}

if (isset($data['definition_id'], $data['definitionClass'], $data['definitionObject']) && is_array($data['definitionObject'])) {
$definition = $this->createObjectByData($data['definitionClass'], $data['definitionObject']);
$object->definition_id = $definition?->id;

if ($definition instanceof ContainerContentDefinition && !empty($data['definitionTemplates'])) {
$definitionTemplates = Template::findAll(['id' => $data['definitionTemplates']]);
foreach ($definitionTemplates as $definitionTemplate) {
$allowedTemplate = new ContainerContentTemplate();
$allowedTemplate->template_id = $definitionTemplate->id;
$allowedTemplate->definition_id = $definition->id;
$allowedTemplate->save();
}
}
}

$object = $this->saveRecord($object);
if (!$object) {
return null;
Expand Down

0 comments on commit b492329

Please sign in to comment.