Skip to content
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

Idea #368

Draft
wants to merge 1 commit into
base: enh/22-migrate-elements
Choose a base branch
from
Draft

Idea #368

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
91 changes: 91 additions & 0 deletions modules/template/components/ActiveRecordDynamicAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

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.
*
* @todo Avoid mark `dynAttribute` model attribute as Safe attribute
*/
abstract class ActiveRecordDynamicAttributes extends ActiveRecord
{
/**
* Get all possible fields 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)) {
$fields = $this->dynAttributes;
$fields[$name] = $value;
$this->setAttribute('dynAttributes', $fields);
} 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 parent::beforeSave($insert);
}

/**
* @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;
}

}
80 changes: 2 additions & 78 deletions modules/template/elements/BaseTemplateElementContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

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

use humhub\components\ActiveRecord;
use humhub\interfaces\ViewableInterface;
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\ContainerContentDefinition;
use humhub\modules\custom_pages\modules\template\models\OwnerContent;
use humhub\modules\custom_pages\permissions\ManagePages;
Expand All @@ -32,7 +32,7 @@
*
* @property-read OwnerContent $ownerContent
*/
abstract class BaseTemplateElementContent extends ActiveRecord implements ViewableInterface
abstract class BaseTemplateElementContent extends ActiveRecordDynamicAttributes implements ViewableInterface
{
public const SCENARIO_CREATE = 'create';
public const SCENARIO_EDIT = 'edit';
Expand Down Expand Up @@ -69,13 +69,6 @@ abstract class BaseTemplateElementContent extends ActiveRecord implements Viewab
*/
public $filesSaved = false;

/**
* Get all possible fields for this element content
*
* @return array Key - element index name, Value - default value
*/
abstract protected function getFields(): array;

/**
* @return string rendered content type by means of the given $options.
*/
Expand Down Expand Up @@ -106,70 +99,6 @@ public static function tableName()
return 'custom_pages_template_element_content';
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['fields'], 'safe'],
];
}

/**
* @inheritdoc
*/
public function __get($name)
{
if ($this->hasField($name)) {
return $this->fields[$name] ?? $this->getFieldDefaultValue($name);
}

$value = parent::__get($name);

if ($name === 'fields' && !is_array($value)) {
$value = empty($value) ? [] : json_decode($value, true);
$this->setAttribute($name, $value);
}

return $value;
}

/**
* @inheritdoc
*/
public function __set($name, $value)
{
if ($this->hasField($name)) {
$fields = $this->fields;
$fields[$name] = $value;
$this->setAttribute('fields', $fields);
} else {
parent::__set($name, $value);
}
}

/**
* Check if this Element Content has the requested field
*
* @param string $name
* @return bool
*/
protected function hasField(string $name): bool
{
return array_key_exists($name, $this->getFields());
}

/**
* Get default value of the requested field
*
* @param string $name
* @return mixed
*/
protected function getFieldDefaultValue(string $name): mixed
{
return $this->getFields()[$name] ?? null;
}

/**
* Copies the values of this content type instance.
Expand Down Expand Up @@ -328,11 +257,6 @@ public function beforeSave($insert)
return false;
}

if (parent::beforeSave($insert)) {
$this->fields = is_array($this->fields) ? json_encode($this->fields) : null;
return true;
}

return false;
}

Expand Down
31 changes: 20 additions & 11 deletions modules/template/elements/TextElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace humhub\modules\custom_pages\modules\template\elements;

use humhub\libs\Html;
use humhub\modules\custom_pages\modules\template\widgets\TemplateContentFormFields;
use Yii;

/**
Expand All @@ -26,7 +25,7 @@ class TextElement extends BaseTemplateElementContent
/**
* @inheritdoc
*/
protected function getFields(): array
protected function getDynamicAttributes(): array
{
return [
'content' => null,
Expand All @@ -39,11 +38,11 @@ protected function getFields(): array
*/
public function rules()
{
return array_merge(parent::rules(), [
return [
['content', 'trim'],
['inline_text', 'boolean'],
['content', 'string', 'length' => [1, 255]],
]);
];
}

/**
Expand Down Expand Up @@ -105,11 +104,21 @@ public function renderEmpty($options = [])
*/
public function renderForm($form)
{
return TemplateContentFormFields::widget([
'type' => 'text',
'form' => $form,
'model' => $this,
]);
$output = $form->field($this, 'content')->textInput(['maxlength' => 255])->label(false);

if (in_array($this->scenario, [$this::SCENARIO_EDIT_ADMIN, $this::SCENARIO_CREATE])) {
$output .= $form->field($this, 'inline_text')->checkbox()
->label(Yii::t('CustomPagesModule.template', 'Is inline text'));

$output .= Html::tag(
'div',
Yii::t(
'CustomPagesModule.base',
'Select this setting for visible text nodes only. Uncheck this setting in case this element is used for example as HTML attribute value.'
),
['class' => 'alert alert-info']
);
}
return $output;
}

}
}
23 changes: 0 additions & 23 deletions modules/template/widgets/views/textContentFormFields.php

This file was deleted.

Loading