Skip to content

Commit

Permalink
Migrate Image element contents
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Dec 26, 2024
1 parent e2d0d4c commit 3155594
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 257 deletions.
56 changes: 55 additions & 1 deletion migrations/m241220_101915_template_elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ public function safeUp()
'id' => $this->primaryKey(),
'element_id' => $this->integer()->notNull(),
'dynAttributes' => $this->text(),
'definition_id' => $this->integer(),
]);
$this->safeCreateTable('custom_pages_template_element_content_definition', [
'id' => $this->primaryKey(),
'dynAttributes' => $this->text(),
'is_default' => $this->boolean()->notNull()->defaultValue(0),
]);
$this->safeAddForeignKey('fk-element_id', 'custom_pages_template_element_content', 'element_id', 'custom_pages_template_element', 'id', 'CASCADE');
$this->safeAddForeignKey('fk-definition_id', 'custom_pages_template_element_content', 'definition_id', 'custom_pages_template_element_content_definition', 'id', 'CASCADE');

$this->migrateElements('custom_pages_template_text_content', 'Text', ['content', 'inline_text']);
$this->migrateElements('custom_pages_template_richtext_content', 'Richtext', ['content']);
Expand All @@ -27,6 +34,7 @@ public function safeUp()
$this->migrateElements('custom_pages_template_contentcontainer_content', 'Space', ['guid']);
$this->migrateElements('custom_pages_template_records_content', 'Users', ['type', 'options' => 'jsonMerge'], false);
$this->migrateElements('custom_pages_template_records_content', 'Spaces', ['type', 'options' => 'jsonMerge']);
$this->migrateElements('custom_pages_template_image_content', 'Image', ['file_guid', 'alt'], true, 'custom_pages_template_image_content_definition', ['height', 'width', 'style']);
}

/**
Expand All @@ -39,7 +47,7 @@ public function safeDown()
return false;
}

private function migrateElements(string $oldTable, string $type, array $dynAttributes, bool $deleteOldTable = true)
private function migrateElements(string $oldTable, string $type, array $dynAttributes, bool $deleteOldTable = true, ?string $oldDefinitionTable = null, ?array $definitionDynAttributes = null)
{
$oldContentType = 'humhub\\modules\\custom_pages\\modules\\template\\models\\' . $type . 'Content';
$newContentType = 'humhub\\modules\\custom_pages\\modules\\template\\elements\\' . $type . 'Element';
Expand All @@ -50,7 +58,18 @@ private function migrateElements(string $oldTable, string $type, array $dynAttri
->innerJoin('custom_pages_template_owner_content AS oc', 'ot.id = oc.content_id AND oc.content_type = :contentType', ['contentType' => $oldContentType])
->innerJoin('custom_pages_template_element AS e', 'e.content_type = oc.content_type AND e.name = oc.element_name');

// Map between old and new definition Ids; Key - old, Value - new.
$definitionIds = [];

foreach ($elements->each() as $element) {
$definitionId = null;
if (!empty($element['definition_id'])) {
if (!isset($definitionIds[$element['definition_id']])) {
$definitionIds[$element['definition_id']] = $this->migrateDefinition($element['definition_id'], $oldDefinitionTable, $definitionDynAttributes);
}
$definitionId = $definitionIds[$element['definition_id']];
}

$dynValues = [];
foreach ($dynAttributes as $attrKey => $attrName) {
if ($attrName === 'jsonMerge') {
Expand All @@ -68,6 +87,7 @@ private function migrateElements(string $oldTable, string $type, array $dynAttri
$this->insertSilent('custom_pages_template_element_content', [
'element_id' => $element['elementId'],
'dynAttributes' => json_encode($dynValues),
'definition_id' => $definitionId,
]);
$newElementId = $this->db->getLastInsertID();

Expand All @@ -92,6 +112,40 @@ private function migrateElements(string $oldTable, string $type, array $dynAttri

if ($deleteOldTable) {
$this->safeDropTable($oldTable);
if ($oldDefinitionTable !== null) {
$this->safeDropTable($oldDefinitionTable);
}
}
}

private function migrateDefinition(?int $definition_id, ?string $oldDefinitionTable = null, ?array $definitionDynAttributes = null): ?int
{
if ($oldDefinitionTable === null || $definitionDynAttributes === null || empty($definition_id)) {
return null;
}

$definition = (new Query())
->select('*')
->from($oldDefinitionTable)
->where(['id' => $definition_id])
->one();

if (!$definition) {
return null;
}

$definitionDynValues = [];
foreach ($definitionDynAttributes as $attrName) {
if (isset($definition[$attrName])) {
$definitionDynValues[$attrName] = $definition[$attrName];
}
}

$this->insertSilent('custom_pages_template_element_content_definition', [
'dynAttributes' => json_encode($definitionDynValues),
'is_default' => $definition['is_default'],
]);

return $this->db->getLastInsertID();
}
}
8 changes: 2 additions & 6 deletions migrations/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ public function up()
{
$this->safeDropTable('custom_pages_template_container_content_item');
$this->safeDropTable('custom_pages_template_container_content_template');
$this->safeDropTable('custom_pages_template_file_content');
$this->safeDropTable('custom_pages_template_container_content');
$this->safeDropTable('custom_pages_template_image_content');
$this->safeDropTable('custom_pages_template_element');
$this->safeDropTable('custom_pages_template_element_content');
$this->safeDropTable('custom_pages_template_element_content_definition');
$this->safeDropTable('custom_pages_template_owner_content');
$this->safeDropTable('custom_pages_template_instance');
$this->safeDropTable('custom_pages_template_container_content_definition');
$this->safeDropTable('custom_pages_template_image_content_definition');
$this->safeDropTable('custom_pages_template');

$this->safeDropTable('custom_pages_page');
$this->safeDropTable('custom_pages_snippet');
$this->safeDropTable('custom_pages_container_page');
$this->safeDropTable('custom_pages_container_snippet');
}

public function down()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Abstract ActiveRecord which allows you to use not only the regular AR attributes but also other dynamic attributes.
* These are stored in a JSON field.
*
* @property int $id
* @property string|array $dynAttributes
*
* Dynamic attributes:
Expand Down
4 changes: 2 additions & 2 deletions modules/template/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use humhub\modules\custom_pages\modules\template\elements\FileDownloadElement;
use humhub\modules\custom_pages\modules\template\elements\FileElement;
use humhub\modules\custom_pages\modules\template\elements\HumHubRichtextElement;
use humhub\modules\custom_pages\modules\template\elements\ImageElement;
use humhub\modules\custom_pages\modules\template\elements\RichtextElement;
use humhub\modules\custom_pages\modules\template\elements\RssElement;
use humhub\modules\custom_pages\modules\template\elements\SpaceElement;
Expand All @@ -22,7 +23,6 @@
use humhub\modules\custom_pages\modules\template\models\forms\AddElementForm;
use humhub\modules\custom_pages\modules\template\models\forms\EditElementForm;
use humhub\modules\custom_pages\modules\template\models\forms\ImportForm;
use humhub\modules\custom_pages\modules\template\models\ImageContent;
use humhub\modules\custom_pages\modules\template\models\TemplateSearch;
use humhub\modules\custom_pages\modules\template\models\Template;
use humhub\modules\custom_pages\modules\template\models\TemplateElement;
Expand Down Expand Up @@ -167,7 +167,7 @@ private function getContentTypes()
TextElement::$label => TextElement::class,
RichtextElement::$label => RichtextElement::class,
HumHubRichtextElement::$label => HumHubRichtextElement::class,
ImageContent::$label => ImageContent::class,
ImageElement::$label => ImageElement::class,
FileElement::$label => FileElement::class,
FileDownloadElement::$label => FileDownloadElement::class,
ContainerContent::$label => ContainerContent::class,
Expand Down
27 changes: 20 additions & 7 deletions modules/template/elements/BaseTemplateElementContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\custom_pages\models\CustomPage;
use humhub\modules\custom_pages\modules\template\components\ActiveRecordDynamicAttributes;
use humhub\modules\custom_pages\modules\template\models\ContainerContent;
use humhub\modules\custom_pages\modules\template\models\ContainerContentDefinition;
use humhub\modules\custom_pages\modules\template\models\ContainerContentItem;
use humhub\modules\custom_pages\modules\template\models\OwnerContent;
use humhub\modules\custom_pages\modules\template\models\PagePermission;
use humhub\modules\custom_pages\modules\template\models\Template;
use humhub\modules\custom_pages\modules\template\models\TemplateContentOwner;
use humhub\modules\custom_pages\modules\template\models\TemplateInstance;
use humhub\modules\custom_pages\modules\template\widgets\TemplateEditorElement;
use humhub\modules\custom_pages\permissions\ManagePages;
use humhub\modules\user\components\PermissionManager;
use Yii;
Expand All @@ -25,8 +31,10 @@
*
* @property int $id
* @property int $element_id
* @property int $definition_id
*
* @property-read OwnerContent $ownerContent
* @property-read BaseTemplateElementContentDefinition $definition
*/
abstract class BaseTemplateElementContent extends ActiveRecordDynamicAttributes implements ViewableInterface
{
Expand All @@ -40,7 +48,7 @@ abstract class BaseTemplateElementContent extends ActiveRecordDynamicAttributes
private $formName;

/**
* @var ContainerContentDefinition instance of this template
* @var BaseTemplateElementContentDefinition|null instance of the definition
*/
private $definitionInstance;

Expand Down Expand Up @@ -75,7 +83,6 @@ abstract public function render($options = []);
*/
abstract public function renderEmpty($options = []);


/**
* @return string the label of this content type
*/
Expand Down Expand Up @@ -107,6 +114,7 @@ public function copy(): static
$clone = new static();
$clone->element_id = $this->element_id;
$clone->dynAttributes = $this->dynAttributes;
$clone->definition_id = $this->definition_id;
return $clone;
}

Expand Down Expand Up @@ -192,14 +200,14 @@ public function formName()
/**
* Returns the ContainerContentDefinition instance of this instance. +
* This function will create an empty definition instance if this content type has an definitionModel and
* does not have an related definition_id.
* does not have a related definition_id.
*
* @return ContainerContentDefinition the definition instance.
* @return BaseTemplateElementContentDefinition|null the definition instance.
*/
public function getDefinition()
public function getDefinition(): ?BaseTemplateElementContentDefinition
{
if (!$this->isDefinitionContent()) {
return;
return null;
}

if ($this->definitionInstance) {
Expand Down Expand Up @@ -310,7 +318,7 @@ protected function wrap($type, $content, $options = [], $attributes = [])
$options['template-content-id'] = $this->getPrimaryKey();
}

return \humhub\modules\custom_pages\modules\template\widgets\TemplateEditorElement::widget([
return TemplateEditorElement::widget([
'container' => $type,
'templateContent' => $this,
'content' => $content,
Expand Down Expand Up @@ -357,6 +365,11 @@ public function getOption($options, $key, $default = null)
return isset($options[$key]) ? strval($options[$key]) : $default;
}

/**
* Check if the Element is empty
*
* @return bool
*/
public function isEmpty(): bool
{
return false;
Expand Down
53 changes: 53 additions & 0 deletions modules/template/elements/BaseTemplateElementContentDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\custom_pages\modules\template\elements;

use humhub\modules\custom_pages\modules\template\components\ActiveRecordDynamicAttributes;

/**
* Class for template element content definition
*
* @property bool $is_default
*/
abstract class BaseTemplateElementContentDefinition extends ActiveRecordDynamicAttributes
{
private $formName;

/**
* @inheritdoc
*/
public static function tableName()
{
return 'custom_pages_template_element_content_definition';
}

public function setFormName($formName): void
{
$this->formName = $formName;
}

public function hasValues(): bool
{
foreach ($this->attributes() as $key) {
if ($this->getAttribute($key) !== null && $key !== 'id' && $key !== 'is_default') {
return true;
}
}

return false;
}

/**
* @inheritdoc
*/
public function formName()
{
return $this->formName ?? parent::formName();
}
}
2 changes: 1 addition & 1 deletion modules/template/elements/FileDownloadElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function rules()
*/
public function attributeLabels()
{
return [
return [
'file_guid' => Yii::t('CustomPagesModule.base', 'File'),
'title' => Yii::t('CustomPagesModule.base', 'Title'),
'style' => Yii::t('CustomPagesModule.base', 'Style'),
Expand Down
Loading

0 comments on commit 3155594

Please sign in to comment.