From 0bf5bc5c09463d84d266685231832f2b662169a7 Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Wed, 8 Jul 2020 16:31:13 +0200 Subject: [PATCH] Enh: Added ActiveField widget for content forms --- protected/humhub/docs/CHANGELOG_DEV.md | 1 + .../modules/ui/form/widgets/ActiveForm.php | 5 +- .../form/widgets/ContentVisibilitySelect.php | 158 ++++++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 protected/humhub/modules/ui/form/widgets/ContentVisibilitySelect.php diff --git a/protected/humhub/docs/CHANGELOG_DEV.md b/protected/humhub/docs/CHANGELOG_DEV.md index 5c4c22d594..673ee88fd7 100644 --- a/protected/humhub/docs/CHANGELOG_DEV.md +++ b/protected/humhub/docs/CHANGELOG_DEV.md @@ -13,3 +13,4 @@ HumHub Change Log - Chg #4158: Cleanup post table removed unused column - Fix #4182: Native edge password reveal icons interferes with custom one - Fix #4173: Notification overview HTML compliant issue +- Enh: Added `ContentVisibilitySelect` ActiveField widget for content forms diff --git a/protected/humhub/modules/ui/form/widgets/ActiveForm.php b/protected/humhub/modules/ui/form/widgets/ActiveForm.php index dc0f0ffd75..0d3dddcc4f 100644 --- a/protected/humhub/modules/ui/form/widgets/ActiveForm.php +++ b/protected/humhub/modules/ui/form/widgets/ActiveForm.php @@ -30,11 +30,14 @@ class ActiveForm extends \yii\bootstrap\ActiveForm public $acknowledge = false; + /** + * @inheritdoc + */ public function init() { parent::init(); - if($this->acknowledge) { + if ($this->acknowledge) { $this->options['data-ui-addition'] = 'acknowledgeForm'; } } diff --git a/protected/humhub/modules/ui/form/widgets/ContentVisibilitySelect.php b/protected/humhub/modules/ui/form/widgets/ContentVisibilitySelect.php new file mode 100644 index 0000000000..3c118bccb8 --- /dev/null +++ b/protected/humhub/modules/ui/form/widgets/ContentVisibilitySelect.php @@ -0,0 +1,158 @@ +field($model, $attribute)->widget(ContentVisibilitySelect::class, [ + * // configure additional widget properties here + * ]) ?> + * ``` + * + * The specified model can either be a ContentActiveRecord or directly a Content record. + * + * @since 1.6 + * @package humhub\modules\ui\form\widgets + */ +class ContentVisibilitySelect extends InputWidget +{ + /** + * @var bool Automatically hide the field when no mulitple visibility modes available + */ + public $autoHide = true; + + /** + * @var bool|null Readonly mode (automatically determined if null) + */ + public $readonly = null; + + /** + * @var Content + */ + private $_content; + + /** + * @inheritDoc + */ + public function run() + { + $this->field->label(false); + + if ($this->autoHide && $this->shouldHide()) { + return ''; + } + + $this->options['label'] = Yii::t('ContentModule.base', 'Public'); + + if ($this->getContentContainer() instanceof Space) { + $this->options['label'] .= ' ' . + Yii::t('ContentModule.base', '(Also visible to non-members of this space)'); + } + + $this->options['title'] = + Yii::t('ContentModule.base', 'Specify who can see this content.'); + + if ($this->readonly) { + $this->options['disabled'] = true; + } + + $this->setDefaultValue(); + + return + '
' . + Html::activeCheckbox($this->model, $this->attribute, $this->options) . + 'model; + $attribute = $this->attribute; + + if ($model->$attribute === null) { + $contentContainer = $this->getContentContainer(); + if ($contentContainer instanceof Space) { + /** @var Space $contentContainer */ + $model->$attribute = $contentContainer->default_content_visibility; + } + } + } + + /** + * @return bool + */ + private function shouldHide() + { + $contentContainer = $this->getContentContainer(); + + // Should hide on private spaces (Only provide private content visibility option) + if ($contentContainer instanceof Space) { + /** @var Space $contentContainer */ + if ($contentContainer->visibility == Space::VISIBILITY_NONE) { + return true; + } + } + + return false; + } + + /** + * @return ContentContainerActiveRecord|null + */ + private function getContentContainer() + { + $content = $this->getContent(); + if ($content !== null) { + return $content->getContainer(); + } + + return null; + } + + /** + * @return Content|null + */ + private function getContent() + { + if ($this->_content !== null) { + return $this->_content; + } + + if ($this->model instanceof ContentActiveRecord) { + $this->_content = $this->model->content; + } elseif ($this->model->page instanceof ContentActiveRecord) { + $this->_content = $this->model->page->content; + } elseif ($this->model instanceof Content) { + $this->_content = $this->model; + } + + return $this->_content; + + } + +}