-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate all element contents to single table #367
Open
yurabakhtin
wants to merge
17
commits into
develop
Choose a base branch
from
enh/22-migrate-elements
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e71fa3a
Migrate all element contents to single table
yurabakhtin e25cfdc
Separate class for record with dynamic attributes
yurabakhtin ee99b8c
Merge branch 'develop' into enh/22-migrate-elements
yurabakhtin c666ba0
Remove "dynAttributes" from safe attributes list
yurabakhtin 0ac9bb0
Migrate RichText element contents
yurabakhtin 8220c35
Migrate HumHub RichText element contents
yurabakhtin 3d5d9a4
Migrate File element contents
yurabakhtin 51fbcee
Fix tests for new elements
yurabakhtin 250d4b9
Migrate File Download element contents
yurabakhtin af2db34
Migrate Rss element contents
yurabakhtin 1fc5c80
Migrate Space/User element contents
yurabakhtin e2d0d4c
Migrate Spaces/Users element contents
yurabakhtin 3155594
Migrate Image element contents
yurabakhtin b1e8621
Migrate Container element contents
yurabakhtin 79eba77
Update element content forms
yurabakhtin b281b4e
Fix migration
yurabakhtin 86e715b
Fix migration
yurabakhtin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
modules/template/components/ActiveRecordDynamicAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?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\components; | ||
|
||
use humhub\components\ActiveRecord; | ||
use yii\validators\Validator; | ||
|
||
/** | ||
* 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 string|array $dynAttributes | ||
* | ||
* Dynamic attributes: | ||
* (List here all virtual/dynamic for the Record, | ||
* they all are stored in the property $dynAttributes as json encoded array) | ||
* | ||
* @todo Avoid mark `dynAttribute` model attribute as Safe attribute | ||
*/ | ||
abstract class ActiveRecordDynamicAttributes extends ActiveRecord | ||
{ | ||
/** | ||
* Get all possible dynamic attributes for this element content | ||
* | ||
* @return array Key - element index name, Value - default value | ||
*/ | ||
abstract protected function getDynamicAttributes(): array; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __get($name) | ||
{ | ||
if ($this->hasDynamicAttribute($name)) { | ||
return $this->dynAttributes[$name] ?? $this->getDynamicAttributeDefaultValue($name); | ||
} | ||
|
||
$value = parent::__get($name); | ||
|
||
if ($name === 'dynAttributes' && !is_array($value)) { | ||
$value = empty($value) ? [] : json_decode($value, true); | ||
$this->setAttribute($name, $value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __set($name, $value) | ||
{ | ||
if ($this->hasDynamicAttribute($name)) { | ||
$attrs = $this->dynAttributes; | ||
$attrs[$name] = $value; | ||
$this->setAttribute('dynAttributes', $attrs); | ||
} else { | ||
parent::__set($name, $value); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @noinspection PhpMissingReturnTypeInspection | ||
*/ | ||
public function beforeSave($insert) | ||
{ | ||
if (parent::beforeSave($insert)) { | ||
$this->dynAttributes = is_array($this->dynAttributes) ? json_encode($this->dynAttributes) : null; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function createValidators() | ||
{ | ||
$validators = parent::createValidators(); | ||
$validators->append(Validator::createValidator('safe', $this, 'dynAttributes')); | ||
|
||
return $validators; | ||
} | ||
|
||
private function hasDynamicAttribute(string $name): bool | ||
{ | ||
return array_key_exists($name, $this->getDynamicAttributes()); | ||
} | ||
|
||
private function getDynamicAttributeDefaultValue(string $name): mixed | ||
{ | ||
return $this->getDynamicAttributes()[$name] ?? null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yurabakhtin Is it really necessary to mark 'dynAttributes' as 'safe attributes'? Isn't that only necessary for attributes that are read in via 'load', for example, or for user input?
It is only used internally and shouldn't be necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@luke- Yes, you are right, I have removed that here.