Skip to content

Commit

Permalink
Migrate Space/User element contents
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Dec 24, 2024
1 parent af2db34 commit 1fc5c80
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 43 deletions.
8 changes: 6 additions & 2 deletions migrations/m241220_101915_template_elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function safeUp()
$this->migrateElements('custom_pages_template_file_content', 'File', ['file_guid']);
$this->migrateElements('custom_pages_template_file_download_content', 'FileDownload', ['file_guid', 'title', 'style', 'cssClass', 'showFileinfo', 'showIcon']);
$this->migrateElements('custom_pages_template_rss_content', 'Rss', ['url', 'cache_time', 'limit']);
$this->migrateElements('custom_pages_template_contentcontainer_content', 'User', ['guid'], false);
$this->migrateElements('custom_pages_template_contentcontainer_content', 'Space', ['guid']);
}

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

private function migrateElements(string $oldTable, string $type, array $dynAttributes)
private function migrateElements(string $oldTable, string $type, array $dynAttributes, bool $deleteOldTable = true)
{
$oldContentType = 'humhub\\modules\\custom_pages\\modules\\template\\models\\' . $type . 'Content';
$newContentType = 'humhub\\modules\\custom_pages\\modules\\template\\elements\\' . $type . 'Element';
Expand Down Expand Up @@ -79,6 +81,8 @@ private function migrateElements(string $oldTable, string $type, array $dynAttri
);
}

$this->safeDropTable($oldTable);
if ($deleteOldTable) {
$this->safeDropTable($oldTable);
}
}
}
3 changes: 0 additions & 3 deletions migrations/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public function up()
$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_hh_richtext_content');
$this->safeDropTable('custom_pages_template_richtext_content');
$this->safeDropTable('custom_pages_template_file_download_content');
$this->safeDropTable('custom_pages_template_element');
$this->safeDropTable('custom_pages_template_owner_content');
$this->safeDropTable('custom_pages_template_instance');
Expand Down
8 changes: 4 additions & 4 deletions modules/template/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
use humhub\modules\custom_pages\modules\template\elements\HumHubRichtextElement;
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;
use humhub\modules\custom_pages\modules\template\elements\TextElement;
use humhub\modules\custom_pages\modules\template\elements\UserElement;
use humhub\modules\custom_pages\modules\template\models\ContainerContent;
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\SpaceContent;
use humhub\modules\custom_pages\modules\template\models\SpacesContent;
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;
use humhub\modules\custom_pages\modules\template\models\UserContent;
use humhub\modules\custom_pages\modules\template\models\UsersContent;
use humhub\modules\custom_pages\modules\template\services\ExportService;
use humhub\modules\custom_pages\modules\template\widgets\TemplateElementAdminRow;
Expand Down Expand Up @@ -172,8 +172,8 @@ private function getContentTypes()
FileDownloadElement::$label => FileDownloadElement::class,
ContainerContent::$label => ContainerContent::class,
RssElement::$label => RssElement::class,
UserContent::$label => UserContent::class,
SpaceContent::$label => SpaceContent::class,
UserElement::$label => UserElement::class,
SpaceElement::$label => SpaceElement::class,
UsersContent::$label => UsersContent::class,
SpacesContent::$label => SpacesContent::class,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license https://www.humhub.com/licences
*/

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

use humhub\libs\Html;
use humhub\modules\content\components\ContentContainerActiveRecord;
Expand All @@ -16,12 +16,12 @@
use yii\helpers\ArrayHelper;

/**
* Class ContentContainerContent
* Abstract class to manage content records of the User/Space elements
*
* Dynamic attributes:
* @property string $guid
* @property string $class
*/
abstract class ContentContainerContent extends TemplateContentActiveRecord
abstract class BaseContentContainerElement extends BaseTemplateElementContent
{
public const CONTAINER_CLASS = null;

Expand All @@ -30,20 +30,21 @@ abstract class ContentContainerContent extends TemplateContentActiveRecord
/**
* @inheritdoc
*/
public static function tableName()
protected function getDynamicAttributes(): array
{
return 'custom_pages_template_contentcontainer_content';
return [
'guid' => null,
];
}

/**
* @inheritdoc
*/
public function rules()
{
return array_merge(parent::rules(), [
[['guid', 'class'], 'string'],
[['class'], 'required'],
]);
return [
[['guid'], 'string'],
];
}

/**
Expand All @@ -52,7 +53,7 @@ public function rules()
public function scenarios()
{
return ArrayHelper::merge(parent::scenarios(), [
self::SCENARIO_CREATE => $attributes = ['guid', 'class'],
self::SCENARIO_CREATE => $attributes = ['guid'],
self::SCENARIO_EDIT_ADMIN => $attributes,
self::SCENARIO_EDIT => $attributes,
]);
Expand All @@ -66,17 +67,6 @@ public function getLabel()
return static::$label;
}

/**
* @inheritdoc
*/
public function copy()
{
$clone = new static();
$clone->guid = $this->guid;
$clone->class = $this->class;
return $clone;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -122,7 +112,6 @@ public function setAttributes($values, $safeOnly = true)
*/
public function beforeValidate()
{
$this->class = static::CONTAINER_CLASS;
return parent::beforeValidate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
* @license https://www.humhub.com/licences
*/

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

use humhub\modules\space\models\Space;
use Yii;

/**
* Class SpaceContent
* Class to manage content records of the Space elements
*/
class SpaceContent extends ContentContainerContent
class SpaceElement extends BaseContentContainerElement
{
public const CONTAINER_CLASS = Space::class;
public static $label = 'Space';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
* @license https://www.humhub.com/licences
*/

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

use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\user\models\User;
use Yii;

/**
* Class UserContent
* Class to manage content records of the User elements
*/
class UserContent extends ContentContainerContent
class UserElement extends BaseContentContainerElement
{
public const CONTAINER_CLASS = User::class;
public static $label = 'User';
Expand Down
3 changes: 2 additions & 1 deletion modules/template/models/OwnerContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\custom_pages\modules\template\elements\BaseTemplateElementContent;
use humhub\modules\custom_pages\modules\template\elements\UserElement;
use Yii;

/**
Expand Down Expand Up @@ -283,6 +284,6 @@ public function getItems(): iterable
public function getProfileField(string $field = null): string
{
$instance = $this->getInstance();
return $instance instanceof UserContent ? $instance->getProfileField($field) : '';
return $instance instanceof UserElement ? $instance->getProfileField($field) : '';
}
}
4 changes: 2 additions & 2 deletions modules/template/widgets/views/spaceContentFormFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* @license https://www.humhub.com/licences
*/

use humhub\modules\custom_pages\modules\template\models\SpaceContent;
use humhub\modules\custom_pages\modules\template\elements\SpaceElement;
use humhub\modules\space\widgets\SpacePickerField;
use humhub\modules\ui\form\widgets\ActiveForm;

/* @var SpaceContent $model */
/* @var SpaceElement $model */
/* @var ActiveForm $form */
?>
<?= $form->field($model, 'guid')->widget(SpacePickerField::class, [
Expand Down
5 changes: 3 additions & 2 deletions modules/template/widgets/views/userContentFormFields.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

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

use humhub\modules\custom_pages\modules\template\models\UserContent;
use humhub\modules\custom_pages\modules\template\elements\UserElement;
use humhub\modules\ui\form\widgets\ActiveForm;
use humhub\modules\user\widgets\UserPickerField;

/* @var UserContent $model */
/* @var UserElement $model */
/* @var ActiveForm $form */
?>
<?= $form->field($model, 'guid')->widget(UserPickerField::class, [
Expand Down

0 comments on commit 1fc5c80

Please sign in to comment.