Skip to content

Commit

Permalink
Update element content forms
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Dec 27, 2024
1 parent b1e8621 commit 79eba77
Show file tree
Hide file tree
Showing 35 changed files with 153 additions and 247 deletions.
2 changes: 1 addition & 1 deletion models/CustomPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public function afterSave($insert, $changedAttributes)
parent::afterSave($insert, $changedAttributes);

if (!$this->getContentType()->afterSave($this, $insert, $changedAttributes)) {
throw new LogicException('Could not save content type' . $this->getContentType()->getLabel());
throw new LogicException('Could not save content type ' . $this->getContentType()->getLabel());
}

if ($this->checkAbstract()) {
Expand Down
24 changes: 12 additions & 12 deletions modules/template/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,18 @@ public function actionEditUsage()
private function getContentTypes(): array
{
return [
TextElement::$label => TextElement::class,
RichtextElement::$label => RichtextElement::class,
HumHubRichtextElement::$label => HumHubRichtextElement::class,
ImageElement::$label => ImageElement::class,
FileElement::$label => FileElement::class,
FileDownloadElement::$label => FileDownloadElement::class,
ContainerElement::$label => ContainerElement::class,
RssElement::$label => RssElement::class,
UserElement::$label => UserElement::class,
SpaceElement::$label => SpaceElement::class,
UsersElement::$label => UsersElement::class,
SpacesElement::$label => SpacesElement::class,
TextElement::class,
RichtextElement::class,
HumHubRichtextElement::class,
ImageElement::class,
FileElement::class,
FileDownloadElement::class,
ContainerElement::class,
RssElement::class,
UserElement::class,
SpaceElement::class,
UsersElement::class,
SpacesElement::class,
];
}

Expand Down
17 changes: 2 additions & 15 deletions modules/template/elements/BaseContentContainerElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use humhub\libs\Html;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\ContentContainer;
use humhub\modules\custom_pages\modules\template\widgets\TemplateContentFormFields;
use yii\db\IntegrityException;

/**
Expand Down Expand Up @@ -46,14 +45,6 @@ public function rules()
];
}

/**
* @inheritdoc
*/
public function getLabel()
{
return static::$label;
}

/**
* @inheritdoc
*/
Expand All @@ -73,13 +64,9 @@ public function renderEmpty($options = [])
/**
* @inheritdoc
*/
public function renderForm($form)
public function getFormView(): string
{
return TemplateContentFormFields::widget([
'type' => strtolower(substr(strrchr(static::CONTAINER_CLASS, '\\'), 1)),
'form' => $form,
'model' => $this,
]);
return strtolower(substr(strrchr(static::CONTAINER_CLASS, '\\'), 1));
}

/**
Expand Down
20 changes: 3 additions & 17 deletions modules/template/elements/BaseRecordsElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
use humhub\components\ActiveRecord;
use humhub\libs\Html;
use humhub\modules\custom_pages\modules\template\models\TemplateContentIterable;
use humhub\modules\custom_pages\modules\template\widgets\TemplateContentFormFields;
use Yii;
use yii\db\ActiveQuery;
use yii\helpers\ArrayHelper;

/**
* Abstract class to manage content records of the elements with different object list (Spaces, Users)
Expand All @@ -35,7 +33,7 @@ abstract class BaseRecordsElement extends BaseTemplateElementContent implements
/**
* @var string Prefix for view file to render a widget with form fields
*/
public string $formView = '';
public string $subFormView = '';

/**
* Get query of the records depending on config
Expand Down Expand Up @@ -76,14 +74,6 @@ public function rules()
];
}

/**
* @inheritdoc
*/
public function getLabel()
{
return static::$label;
}

/**
* @inheritdoc
*/
Expand All @@ -103,13 +93,9 @@ public function renderEmpty($options = [])
/**
* @inheritdoc
*/
public function renderForm($form)
public function getFormView(): string
{
return TemplateContentFormFields::widget([
'type' => 'records',
'form' => $form,
'model' => $this,
]);
return 'records';
}

/**
Expand Down
18 changes: 11 additions & 7 deletions modules/template/elements/BaseTemplateElementContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,7 @@ abstract public function renderEmpty($options = []);
/**
* @return string the label of this content type
*/
abstract public function getLabel();

/**
* @param \yii\widgets\ActiveForm $form form instance
* @return string edit form of this content type
*/
abstract public function renderForm($form);
abstract public function getLabel(): string;

/**
* @inheritdoc
Expand Down Expand Up @@ -454,4 +448,14 @@ public function isCacheable(): bool
{
return true;
}

/**
* Get a view file name to render a form with fields for this Element Content
*
* @return string
*/
public function getFormView(): string
{
return lcfirst(substr(strrchr(static::class, '\\'), 1, -7));
}
}
35 changes: 16 additions & 19 deletions modules/template/elements/ContainerElement.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\modules\custom_pages\modules\template\models\Template;
use humhub\modules\custom_pages\modules\template\widgets\TemplateContentFormFields;
use Yii;
use yii\db\ActiveQuery;

Expand All @@ -22,7 +21,13 @@
*/
class ContainerElement extends BaseTemplateElementContent
{
public static $label = 'Container';
/**
* @inheritdoc
*/
public function getLabel(): string
{
return Yii::t('CustomPagesModule.template', 'Container');
}

/**
* @inheritdoc
Expand Down Expand Up @@ -50,6 +55,9 @@ public function getAllowedTemplates(): array
return $this->definition->allowedTemplates;
}

/**
* @inheritdoc
*/
public function beforeDelete()
{
if ($this->hasItems()) {
Expand All @@ -61,11 +69,9 @@ public function beforeDelete()
return parent::beforeDelete();
}

public function getLabel()
{
return self::$label;
}

/**
* @inheritdoc
*/
public function render($options = [])
{
$items = $this->items;
Expand All @@ -90,6 +96,9 @@ public function render($options = [])
}
}

/**
* @inheritdoc
*/
public function renderEmpty($options = [])
{
$options['jsWidget'] = 'custom_pages.template.TemplateContainer';
Expand Down Expand Up @@ -185,18 +194,6 @@ public function isSingleAllowedTemplate(): bool
return $this->definition->isSingleAllowedTemplate();
}

/**
* @inheritdoc
*/
public function renderForm($form)
{
return TemplateContentFormFields::widget([
'type' => 'container',
'form' => $form,
'model' => $this,
]);
}

/**
* @inheritdoc
*/
Expand Down
29 changes: 7 additions & 22 deletions modules/template/elements/FileDownloadElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

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

use humhub\modules\custom_pages\modules\template\widgets\TemplateContentFormFields;
use humhub\modules\file\models\File;
use humhub\modules\file\libs\FileHelper;
use Yii;
Expand All @@ -28,7 +27,13 @@
*/
class FileDownloadElement extends BaseTemplateElementContent
{
public static $label = 'File Download';
/**
* @inheritdoc
*/
public function getLabel(): string
{
return Yii::t('CustomPagesModule.template', 'File Download');
}

/**
* @inheritdoc
Expand Down Expand Up @@ -88,14 +93,6 @@ public function saveFiles()
$this->fileManager->attach($this->file_guid);
}

/**
* @inheritdoc
*/
public function getLabel()
{
return static::$label;
}

public function getFile(): ?File
{
return File::findOne(['guid' => $this->file_guid]);
Expand Down Expand Up @@ -161,16 +158,4 @@ public function renderEmpty($options = [])
{
return '';
}

/**
* @inheritdoc
*/
public function renderForm($form)
{
return TemplateContentFormFields::widget([
'type' => 'fileDownload',
'form' => $form,
'model' => $this,
]);
}
}
29 changes: 7 additions & 22 deletions modules/template/elements/FileElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

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

use humhub\modules\custom_pages\modules\template\widgets\TemplateContentFormFields;
use humhub\modules\file\models\File;
use Yii;

Expand All @@ -20,7 +19,13 @@
*/
class FileElement extends BaseTemplateElementContent
{
public static $label = 'File';
/**
* @inheritdoc
*/
public function getLabel(): string
{
return Yii::t('CustomPagesModule.template', 'File');
}

/**
* @inheritdoc
Expand Down Expand Up @@ -69,14 +74,6 @@ public function saveFiles()
$this->fileManager->attach($this->file_guid);
}

/**
* @inheritdoc
*/
public function getLabel()
{
return static::$label;
}

/**
* Get File
*
Expand Down Expand Up @@ -124,18 +121,6 @@ public function renderEmpty($options = [])
return '';
}

/**
* @inheritdoc
*/
public function renderForm($form)
{
return TemplateContentFormFields::widget([
'type' => 'file',
'form' => $form,
'model' => $this,
]);
}

/**
* @inheritdoc
*/
Expand Down
Loading

0 comments on commit 79eba77

Please sign in to comment.