Skip to content

Commit

Permalink
Merge pull request #357 from humhub/enh/20-refactor-pages
Browse files Browse the repository at this point in the history
Refactor all snippets and container pages to single page class
  • Loading branch information
luke- authored Nov 23, 2024
2 parents 70a9772 + e759303 commit f5e3426
Show file tree
Hide file tree
Showing 85 changed files with 1,674 additions and 2,215 deletions.
66 changes: 21 additions & 45 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
use humhub\modules\admin\widgets\AdminMenu;
use humhub\modules\content\helpers\ContentContainerHelper;
use humhub\modules\custom_pages\helpers\Url;
use humhub\modules\custom_pages\models\ContainerPage;
use humhub\modules\custom_pages\models\ContainerSnippet;
use humhub\modules\custom_pages\interfaces\CustomPagesService;
use humhub\modules\custom_pages\models\LinkType;
use humhub\modules\custom_pages\models\Page;
use humhub\modules\custom_pages\models\Snippet;
use humhub\modules\custom_pages\models\CustomPage;
use humhub\modules\custom_pages\helpers\PageType;
use humhub\modules\custom_pages\modules\template\models\PagePermission;
use humhub\modules\custom_pages\permissions\ManagePages;
use humhub\modules\custom_pages\widgets\SnippetWidget;
use humhub\modules\space\models\Space;
use humhub\modules\ui\menu\MenuLink;
use humhub\modules\user\widgets\PeopleHeadingButtons;
use humhub\widgets\TopMenu;
Expand Down Expand Up @@ -79,16 +79,11 @@ public static function onSpaceMenuInit($event)
try {
Yii::$app->moduleManager->getModule('custom_pages')->checkOldGlobalContent();

/* @var $space \humhub\modules\space\models\Space */
/* @var $space Space */
$space = $event->sender->space;
if ($space->moduleManager->isEnabled('custom_pages')) {
/* @var Page[] $pages */
$pages = ContainerPage::find()
->contentContainer($space)
->readable()
->andWhere(['target' => ContainerPage::NAV_CLASS_SPACE_NAV])
->all();
foreach ($pages as $page) {
foreach (CustomPagesService::instance()->findByTarget(PageType::TARGET_SPACE_MENU, $space)->all() as $page) {
/* @var CustomPage $page */
if (!$page->canView()) {
continue;
}
Expand Down Expand Up @@ -120,7 +115,7 @@ public static function onSpaceAdminMenuInit($event)
try {
Yii::$app->moduleManager->getModule('custom_pages')->checkOldGlobalContent();

/* @var $space \humhub\modules\space\models\Space */
/* @var $space Space */
$space = $event->sender->space;
if ($space->moduleManager->isEnabled('custom_pages') && $space->isAdmin() && $space->isMember()) {
$event->sender->addItem([
Expand All @@ -147,7 +142,7 @@ public static function onTopMenuInit($event)
try {
Yii::$app->moduleManager->getModule('custom_pages')->checkOldGlobalContent();

foreach (self::findPagesByTarget(Page::NAV_CLASS_TOPNAV) as $page) {
foreach (CustomPagesService::instance()->findByTarget(PageType::TARGET_TOP_MENU)->all() as $page) {
if (!$page->canView()) {
continue;
}
Expand All @@ -174,7 +169,7 @@ public static function onTopMenuInit($event)
}
}

private static function isCurrentTargetUrl(Page $page): bool
private static function isCurrentTargetUrl(CustomPage $page): bool
{
if ($page->type === LinkType::ID && $page->page_content) {
$targetUrl = strpos($page->page_content, 'http') === 0 ?
Expand Down Expand Up @@ -204,7 +199,7 @@ public static function onAccountMenuInit($event)
try {
Yii::$app->moduleManager->getModule('custom_pages')->checkOldGlobalContent();

foreach (self::findPagesByTarget(Page::NAV_CLASS_ACCOUNTNAV) as $page) {
foreach (CustomPagesService::instance()->findByTarget(PageType::TARGET_ACCOUNT_MENU)->all() as $page) {
if (!$page->canView()) {
continue;
}
Expand Down Expand Up @@ -239,15 +234,12 @@ public static function onDashboardSidebarInit($event)
{
try {
Yii::$app->moduleManager->getModule('custom_pages')->checkOldGlobalContent();

/* @var Snippet[] $snippets */
$snippets = Snippet::find()->where(['target' => Snippet::SIDEBAR_DASHBOARD])->readable()->all();
$canEdit = PagePermission::canEdit();
foreach ($snippets as $snippet) {
if (!$snippet->canView()) {
continue;
foreach (CustomPagesService::instance()->findByTarget(PageType::TARGET_DASHBOARD_SIDEBAR)->all() as $page) {
/* @var CustomPage $page */
if ($page->canView()) {
$event->sender->addWidget(SnippetWidget::class, ['model' => $page, 'canEdit' => $canEdit], ['sortOrder' => $page->sort_order]);
}
$event->sender->addWidget(SnippetWidget::class, ['model' => $snippet, 'canEdit' => $canEdit], ['sortOrder' => $snippet->sort_order]);
}
} catch (Throwable $e) {
Yii::error($e);
Expand All @@ -262,14 +254,11 @@ public static function onSpaceSidebarInit($event)
$space = $event->sender->space;
$canEdit = PagePermission::canEdit();
if ($space->moduleManager->isEnabled('custom_pages')) {
/* @var Snippet[] $snippets */
$snippets = ContainerSnippet::find()->contentContainer($space)->readable()->all();
foreach ($snippets as $snippet) {
if (!$snippet->canView()) {
continue;
foreach (CustomPagesService::instance()->findByTarget(PageType::TARGET_SPACE_STREAM_SIDEBAR, $space)->all() as $page) {
/* @var CustomPage $page */
if ($page->canView()) {
$event->sender->addWidget(SnippetWidget::class, ['model' => $page, 'canEdit' => $canEdit], ['sortOrder' => $page->sort_order]);
}

$event->sender->addWidget(SnippetWidget::class, ['model' => $snippet, 'canEdit' => $canEdit], ['sortOrder' => $snippet->sort_order]);
}
}
} catch (Throwable $e) {
Expand All @@ -280,7 +269,7 @@ public static function onSpaceSidebarInit($event)
public static function onFooterMenuInit($event)
{
try {
foreach (self::findPagesByTarget(Page::NAV_CLASS_FOOTER) as $page) {
foreach (CustomPagesService::instance()->findByTarget(PageType::TARGET_FOOTER)->all() as $page) {
if (!$page->canView()) {
continue;
}
Expand All @@ -302,7 +291,7 @@ public static function onPeopleHeadingButtonsInit($event)
try {
/* @var PeopleHeadingButtons $peopleHeadingButtons */
$peopleHeadingButtons = $event->sender;
foreach (self::findPagesByTarget(Page::NAV_CLASS_PEOPLE) as $page) {
foreach (CustomPagesService::instance()->findByTarget(PageType::TARGET_PEOPLE)->all() as $page) {
if (!$page->canView()) {
continue;
}
Expand All @@ -319,17 +308,4 @@ public static function onPeopleHeadingButtonsInit($event)
Yii::error($e);
}
}

/**
* @param string $target
* @return Page[]
*/
private static function findPagesByTarget(string $target): array
{
return Page::find()
->where(['target' => $target])
->readable()
->all();
}

}
35 changes: 6 additions & 29 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
use humhub\modules\content\components\ContentContainerModule;
use humhub\modules\content\models\Content;
use humhub\modules\custom_pages\helpers\Url;
use humhub\modules\custom_pages\models\ContainerPage;
use humhub\modules\custom_pages\models\Page;
use humhub\modules\custom_pages\models\Snippet;
use humhub\modules\custom_pages\models\CustomPage;
use humhub\modules\space\models\Space;
use Yii;

Expand Down Expand Up @@ -51,22 +49,17 @@ class Module extends ContentContainerModule

public function checkOldGlobalContent()
{

if (!Yii::$app->user->isAdmin()) {
return;
}

if (!$this->settings->get(static::SETTING_MIGRATION_KEY, 0)) {
foreach (Page::find()->all() as $page) {
foreach (CustomPage::find()->all() as $page) {
/* @var CustomPage $page */
$page->content->visibility = $page->admin_only ? Content::VISIBILITY_PRIVATE : Content::VISIBILITY_PUBLIC;
$page->content->save();
}

foreach (Snippet::find()->all() as $snippet) {
$snippet->content->visibility = $snippet->admin_only ? Content::VISIBILITY_PRIVATE : Content::VISIBILITY_PUBLIC;
$snippet->content->save();
}

$this->settings->set(static::SETTING_MIGRATION_KEY, 1);
}
}
Expand All @@ -84,19 +77,7 @@ public function getConfigUrl()
*/
public function disable()
{
foreach (Page::find()->all() as $page) {
$page->hardDelete();
}

foreach (ContainerPage::find()->all() as $page) {
$page->hardDelete();
}

foreach (models\Snippet::find()->all() as $page) {
$page->hardDelete();
}

foreach (models\ContainerSnippet::find()->all() as $page) {
foreach (CustomPage::find()->all() as $page) {
$page->hardDelete();
}

Expand All @@ -118,7 +99,7 @@ public function getContentContainerTypes()
*/
public function getContentClasses(): array
{
return [Page::class, ContainerPage::class];
return [CustomPage::class];
}

/**
Expand All @@ -143,11 +124,7 @@ public function disableContentContainer(ContentContainerActiveRecord $container)
{
parent::disableContentContainer($container);

foreach (ContainerPage::find()->contentContainer($container)->all() as $page) {
$page->hardDelete();
}

foreach (models\ContainerSnippet::find()->contentContainer($container)->all() as $page) {
foreach (CustomPage::find()->contentContainer($container)->all() as $page) {
$page->hardDelete();
}
}
Expand Down
6 changes: 3 additions & 3 deletions components/PageUrlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use yii\base\Component;
use yii\web\UrlRuleInterface;
use humhub\modules\custom_pages\models\Page;
use humhub\modules\custom_pages\models\CustomPage;

/**
* CustomPages URL Rule
Expand Down Expand Up @@ -66,7 +66,7 @@ public function parseRequest($manager, $request)
if (substr($pathInfo, 0, 2) == "p/") {
$parts = explode('/', $pathInfo, 3);
if (isset($parts[1])) {
$page = Page::find()->where(['id' => $parts[1]])->orWhere(['url' => $parts[1]])->one();
$page = CustomPage::find()->where(['id' => $parts[1]])->orWhere(['url' => $parts[1]])->one();
if ($page !== null) {
if (!isset($parts[2]) || $parts[2] == "") {
$parts[2] = $this->defaultRoutes[0];
Expand Down Expand Up @@ -94,7 +94,7 @@ public static function getUrlByPageId($id)
return static::$pageUrlMap[$id];
}

$page = Page::findOne(['id' => $id]);
$page = CustomPage::findOne(['id' => $id]);
if ($page !== null) {
static::$pageUrlMap[$page->id] = !empty($page->url) ? $page->url : $page->id;
return static::$pageUrlMap[$page->id];
Expand Down
10 changes: 5 additions & 5 deletions components/PhpPageContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace humhub\modules\custom_pages\components;

use HttpException;
use humhub\modules\custom_pages\models\CustomContentContainer;
use humhub\modules\custom_pages\models\forms\SettingsForm;
use humhub\modules\custom_pages\models\CustomPage;
use humhub\modules\custom_pages\models\PhpType;
use humhub\modules\file\libs\FileHelper;
use Yii;
use yii\helpers\Html;

/**
* @used-by CustomContentContainer
* @used-by CustomPage
*/
trait PhpPageContainer
{
Expand All @@ -31,7 +31,7 @@ public function validatePhpType($attribute, $params)
}

if (!$this->validatePhpViewFile()) {
$this->addError($this->getPageContentProperty(), Yii::t('CustomPagesModule.base', 'Invalid view file selection!'));
$this->addError('page_content', Yii::t('CustomPagesModule.base', 'Invalid view file selection!'));
}
}
}
Expand All @@ -45,7 +45,7 @@ public function validatePhpType($attribute, $params)
public function validatePhpViewFile()
{
$allowedFiles = $this->getAllowedPhpViewFileSelection();
return array_key_exists(Html::getAttributeValue($this, $this->getPageContentProperty()), $allowedFiles);
return array_key_exists(Html::getAttributeValue($this, 'page_content'), $allowedFiles);
}

/**
Expand All @@ -57,7 +57,7 @@ public function getPhpViewFilePath()
{
if (PhpType::isType($this->type)) {
$viewFiles = $this->getAllowedPhpViewFileSelection(true);
$viewName = Html::getAttributeValue($this, $this->getPageContentProperty());
$viewName = Html::getAttributeValue($this, 'page_content');

if (array_key_exists($viewName, $viewFiles)) {
return $this->getPhpViewPathByView(basename($viewFiles[$viewName]), true);
Expand Down
47 changes: 17 additions & 30 deletions controllers/AbstractCustomContainerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@
use humhub\modules\admin\permissions\ManageModules;
use humhub\modules\content\components\ContentContainerController;
use humhub\modules\custom_pages\helpers\Html;
use humhub\modules\custom_pages\models\ContainerPage;
use humhub\modules\custom_pages\models\ContainerSnippet;
use humhub\modules\custom_pages\models\CustomContentContainer;
use humhub\modules\custom_pages\models\Page;
use humhub\modules\custom_pages\models\PageType;
use humhub\modules\custom_pages\models\Snippet;
use humhub\modules\custom_pages\interfaces\CustomPagesService;
use humhub\modules\custom_pages\models\CustomPage;
use humhub\modules\custom_pages\helpers\PageType;
use humhub\modules\custom_pages\modules\template\components\TemplateCache;
use humhub\modules\custom_pages\modules\template\models\TemplateInstance;
use humhub\modules\custom_pages\modules\template\models\PagePermission;
Expand Down Expand Up @@ -50,40 +47,30 @@ public function init()
* @return string
* @see PageType
*/
abstract protected function getPageType();

/**
* Returns the actual class for this type of page.
*
* @return string
*/
protected function getPageClassName()
{
if ($this->getPageType() === PageType::Snippet) {
return $this->contentContainer ? ContainerSnippet::class : Snippet::class;
}

return $this->contentContainer ? ContainerPage::class : Page::class;
}
abstract protected function getPageType(): string;

/**
* Returns a page by a given $id.
*
* @param int $id page id.
* @return CustomContentContainer
* @return CustomPage|null
*/
protected function findById($id)
protected function findById($id): ?CustomPage
{
return call_user_func($this->getPageClassName() . '::findOne', ['id' => $id]);
return CustomPagesService::instance()
->findByPageType($this->getPageType(), $this->contentContainer)
->andWhere([CustomPage::tableName() . '.id' => $id])
->one();
}

/**
* Render the given template page
*
* @param \humhub\modules\custom_pages\models\CustomContentContainer $page
* @param CustomPage $page
* @return string rendered template page
* @throws \yii\web\HttpException in case the page is protected from non admin access
*/
public function viewTemplatePage(CustomContentContainer $page, $view)
public function viewTemplatePage(CustomPage $page, $view): string
{
$html = $this->renderTemplate($page);
$canEdit = $this->isCanEdit();
Expand All @@ -101,14 +88,14 @@ public function viewTemplatePage(CustomContentContainer $page, $view)
}

/**
* @param $page
* @param null $editMode
* @param CustomPage $page
* @param bool $editMode
* @return string
* @throws HttpException
*/
public function renderTemplate($page, $editMode = null)
public function renderTemplate(CustomPage $page, $editMode = false)
{
$templateInstance = TemplateInstance::findOne(['object_model' => get_class($page) ,'object_id' => $page->id]);
$templateInstance = TemplateInstance::findOne(['page_id' => $page->id]);

if (!$templateInstance) {
throw new HttpException(404);
Expand Down
Loading

0 comments on commit f5e3426

Please sign in to comment.