Skip to content

Commit

Permalink
Refactor all snippets and container pages to single page class
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Nov 14, 2024
1 parent c90b2bb commit 6b21ded
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 140 deletions.
57 changes: 15 additions & 42 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +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\interfaces\CustomPagesService;
use humhub\modules\custom_pages\models\LinkType;
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 @@ -77,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 CustomPage[] $pages */
$pages = CustomPage::find()
->contentContainer($space)
->readable()
->andWhere(['target' => PageType::TARGET_SPACE_MENU])
->all();
foreach ($pages as $page) {
foreach (CustomPagesService::instance()->find(PageType::TARGET_SPACE_MENU, $space)->all() as $page) {
/* @var CustomPage $page */
if (!$page->canView()) {
continue;
}
Expand Down Expand Up @@ -118,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 @@ -145,7 +142,7 @@ public static function onTopMenuInit($event)
try {
Yii::$app->moduleManager->getModule('custom_pages')->checkOldGlobalContent();

foreach (self::findPagesByTarget(PageType::TARGET_TOP_MENU) as $page) {
foreach (CustomPagesService::instance()->find(PageType::TARGET_TOP_MENU)->all() as $page) {
if (!$page->canView()) {
continue;
}
Expand Down Expand Up @@ -202,7 +199,7 @@ public static function onAccountMenuInit($event)
try {
Yii::$app->moduleManager->getModule('custom_pages')->checkOldGlobalContent();

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

/* @var CustomPage[] $pages */
$pages = CustomPage::find()
->andWhere(['target' => PageType::TARGET_DASHBOARD_SIDEBAR])
->readable()
->all();
$canEdit = PagePermission::canEdit();
foreach ($pages as $page) {
foreach (CustomPagesService::instance()->find(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]);
}
Expand All @@ -262,15 +254,9 @@ public static function onSpaceSidebarInit($event)
$space = $event->sender->space;
$canEdit = PagePermission::canEdit();
if ($space->moduleManager->isEnabled('custom_pages')) {
/* @var CustomPage[] $pages */
$pages = CustomPage::find()
->contentContainer($space)
->readable()
//->filterByTargetType() TODO: Filter only by Snippet Targets
->andWhere(['target' => PageType::TARGET_SPACE_STREAM_SIDEBAR])
->all();
foreach ($pages as $page) {
if ($page->canView() && $page->isSnippet()) {
foreach (CustomPagesService::instance()->find(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]);
}
}
Expand All @@ -283,7 +269,7 @@ public static function onSpaceSidebarInit($event)
public static function onFooterMenuInit($event)
{
try {
foreach (self::findPagesByTarget(PageType::TARGET_FOOTER) as $page) {
foreach (CustomPagesService::instance()->find(PageType::TARGET_FOOTER)->all() as $page) {
if (!$page->canView()) {
continue;
}
Expand All @@ -305,7 +291,7 @@ public static function onPeopleHeadingButtonsInit($event)
try {
/* @var PeopleHeadingButtons $peopleHeadingButtons */
$peopleHeadingButtons = $event->sender;
foreach (self::findPagesByTarget(PageType::TARGET_PEOPLE) as $page) {
foreach (CustomPagesService::instance()->find(PageType::TARGET_PEOPLE)->all() as $page) {
if (!$page->canView()) {
continue;
}
Expand All @@ -322,17 +308,4 @@ public static function onPeopleHeadingButtonsInit($event)
Yii::error($e);
}
}

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

}
13 changes: 10 additions & 3 deletions controllers/AbstractCustomContainerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use humhub\modules\admin\permissions\ManageModules;
use humhub\modules\content\components\ContentContainerController;
use humhub\modules\custom_pages\helpers\Html;
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;
Expand Down Expand Up @@ -52,11 +53,17 @@ abstract protected function getPageType(): string;
* Returns a page by a given $id.
*
* @param int $id page id.
* @return CustomPage
* @return CustomPage|null
*/
protected function findById($id)
protected function findById($id): ?CustomPage
{
return CustomPage::findOne(['id' => $id]);
$targets = CustomPagesService::instance()->getTargets($this->getPageType(), $this->contentContainer);

return CustomPage::find()
->contentContainer($this->contentContainer)
->andWhere([CustomPage::tableName() . '.target' => array_column($targets, 'id')])
->andWhere([CustomPage::tableName() . '.id' => $id])
->one();
}

/**
Expand Down
42 changes: 0 additions & 42 deletions controllers/ContainerController.php

This file was deleted.

11 changes: 3 additions & 8 deletions controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,13 @@
*/
class PageController extends AbstractCustomContainerController
{
/**
* @var CustomPagesService
*/
public $customPagesService;

/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->customPagesService = new CustomPagesService();

if (!$this->contentContainer) {
$this->subLayout = "@humhub/modules/admin/views/layouts/main";
}
Expand Down Expand Up @@ -98,7 +93,7 @@ public function actionIndex()
public function actionOverview()
{
return $this->render('@custom_pages/views/common/list', [
'targets' => $this->customPagesService->getTargets($this->getPageType(), $this->contentContainer),
'targets' => CustomPagesService::instance()->getTargets($this->getPageType(), $this->contentContainer),
'pageType' => $this->getPageType(),
'subNav' => $this->getSubNav(),
]);
Expand All @@ -124,7 +119,7 @@ private function getSubNav()
*/
public function actionAdd($targetId, $type = null)
{
$target = $this->customPagesService->getTargetById($targetId, $this->getPageType(), $this->contentContainer);
$target = CustomPagesService::instance()->getTargetById($targetId, $this->getPageType(), $this->contentContainer);

if (!$target) {
throw new HttpException(404, 'Invalid target setting!');
Expand Down
44 changes: 14 additions & 30 deletions interfaces/CustomPagesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@
use humhub\modules\custom_pages\helpers\PageType;
use humhub\modules\custom_pages\models\Target;
use yii\base\Component;
use yii\base\StaticInstanceTrait;

/**
* Class CustomPagesService
* @package humhub\modules\custom_pages\interfaces
*/
class CustomPagesService extends Component
{
use StaticInstanceTrait;

public const EVENT_FETCH_TARGETS = 'fetchTargets';

/**
* Fetches all available navigations for a given container.
*
* @param string $type
* @param ?ContentContainerActiveRecord $container
* @return array
* @param ContentContainerActiveRecord|null $container
* @return Target[]
*/
public function getTargets(string $type, ?ContentContainerActiveRecord $container = null): array
{
Expand Down Expand Up @@ -56,7 +59,7 @@ public function getTargets(string $type, ?ContentContainerActiveRecord $containe
* @param ContentContainerActiveRecord|null $container
* @return Target
*/
public function getTargetById($targetId, $type, ContentContainerActiveRecord $container = null)
public function getTargetById($targetId, $type, ContentContainerActiveRecord $container = null): ?Target
{
$availableTargets = $this->getTargets($type, $container);
return array_key_exists($targetId, $availableTargets) ? $availableTargets[$targetId] : null;
Expand All @@ -82,15 +85,14 @@ public function getTargetByPage(CustomPage $page): ?Target
* to remove all pages and snippets of a given target.
*
* @param $targetId
* @param $type
* @param ContentContainerActiveRecord|null $container
* @throws \Throwable
* @throws \yii\base\Exception
* @throws \yii\db\StaleObjectException
*/
public function deleteByTarget($targetId, $type, ContentContainerActiveRecord $container = null)
public function deleteByTarget($targetId, ContentContainerActiveRecord $container = null): void
{
foreach ($this->findContentByTarget($targetId, $type, $container)->all() as $content) {
foreach ($this->find($targetId, $container)->each() as $content) {
$content->delete();
}
}
Expand All @@ -100,32 +102,32 @@ public function deleteByTarget($targetId, $type, ContentContainerActiveRecord $c
*
* @param $targetId
*/
public function deleteAllByTarget($targetId)
public function deleteAllByTarget($targetId): void
{
foreach (CustomPage::find()->where(['target' => $targetId])->all() as $page) {
foreach (CustomPage::find()->where(['target' => $targetId])->each() as $page) {
/* @var CustomPage $page */
$page->delete();
}
}

/**
* Returns all pages related to a given target.
* Returns a query to find all pages related to a given target and container/space.
*
* @param string|Target $targetId
* @param ContentContainerActiveRecord|null $container
* @return ActiveQueryContent
* @throws \yii\base\Exception
* @throws \Throwable
*/
public function findContentByTarget($targetId, $type, ContentContainerActiveRecord $container = null)
public function find($targetId, ?ContentContainerActiveRecord $container = null): ActiveQueryContent
{
if ($targetId instanceof Target) {
$container = $targetId->container;
$targetId = $targetId->id;
}

$query = CustomPage::find()
->where(['target' => $targetId])
->where([CustomPage::tableName() . '.target' => $targetId])
->contentContainer($container);

if ($container) {
Expand All @@ -136,30 +138,12 @@ public function findContentByTarget($targetId, $type, ContentContainerActiveReco
}

if (!CustomPage::canSeeAdminOnlyContent($container)) {
$query->andWhere(['admin_only' => 0]);
$query->andWhere([CustomPage::tableName() . '.admin_only' => 0]);
}

return $query->orderBy([
CustomPage::tableName() . '.sort_order' => SORT_ASC,
CustomPage::tableName() . '.id' => SORT_DESC,
]);
}

/**
* Should be called to search for a single custom content with a given id.
*
* @param int $id
* @param string $targetId
* @param string $type
* @param ContentContainerActiveRecord|null $container
* @return CustomPage|null
* @throws \yii\base\Exception
*/
public function getSingleContent($id, $targetId, $type, ContentContainerActiveRecord $container = null): ?CustomPage
{
return $this->findContentByTarget($targetId, $type, $container)
->andWhere([CustomPage::tableName() . '.id' => $id])
->one();
}

}
2 changes: 1 addition & 1 deletion models/CustomPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public function getPageType(): string
public function getTargetModel(): ?Target
{
if (!$this->_target) {
$this->_target = (new CustomPagesService())->getTargetByPage($this);
$this->_target = CustomPagesService::instance()->getTargetByPage($this);
}

return $this->_target;
Expand Down
Loading

0 comments on commit 6b21ded

Please sign in to comment.