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

Using Domain structure for ContentBlocks module #2091

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,22 @@
use Backend\Core\Engine\Authentication;
use Backend\Core\Engine\Base\ActionAdd as BackendBaseActionAdd;
use Backend\Core\Engine\Model as BackendModel;
use Backend\Modules\ContentBlocks\Command\CreateContentBlock;
use Backend\Modules\ContentBlocks\Event\ContentBlockCreated;
use Backend\Modules\ContentBlocks\Form\ContentBlockType;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\Command\CreateContentBlock;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\Event\ContentBlockCreated;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\ContentBlockType;
use Symfony\Component\Form\Form;

/**
* This is the add-action, it will display a form to create a new item
*/
class Add extends BackendBaseActionAdd
class ContentBlockAdd extends BackendBaseActionAdd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case renaming this might be a bit overkill, but if you do it you need to make sure that all the changes needed to the database are added to the upgrade guide

Goes for all the other actions in this case as well.

{
public function execute(): void
{
parent::execute();

$form = $this->createForm(
ContentBlockType::class,
null,
['theme' => $this->get('fork.settings')->get('Core', 'theme', 'Core')]
);

$form->handleRequest($this->get('request'));
/** @var Form $form */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need this phpdoc since the return type is already the form

$form = $this->getForm();

if (!$form->isValid()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking if a form is valid before checking if a form is submitted is deprecated in symfony 3.3 and removed in symfony 4, since we will need to upgrade for fork 5 this will need to be changed here as well

$this->tpl->assign('form', $form->createView());
Expand All @@ -43,27 +39,55 @@ public function execute(): void
}

/** @var CreateContentBlock $createContentBlock */
$createContentBlock = $form->getData();
$createContentBlock->userId = Authentication::getUser()->getUserId();

// The command bus will handle the saving of the content block in the database.
$this->get('command_bus')->handle($createContentBlock);
$createContentBlock = $this->createContentBlock($form);

$this->get('event_dispatcher')->dispatch(
ContentBlockCreated::EVENT_NAME,
new ContentBlockCreated($createContentBlock->contentBlock)
new ContentBlockCreated($createContentBlock->getContentBlockEntity())
);

$this->redirect(
BackendModel::createURLForAction(
'Index',
null,
null,
$this->getBackLink(
[
'report' => 'added',
'report' => 'content-block-added',
'var' => $createContentBlock->title,
]
)
);
}

private function createContentBlock(Form $form): CreateContentBlock
{
/** @var CreateContentBlock $createContentBlock */
$createContentBlock = $form->getData();
$createContentBlock->userId = Authentication::getUser()->getUserId();

// The command bus will handle the saving of the content block in the database.
$this->get('command_bus')->handle($createContentBlock);

return $createContentBlock;
}

private function getBackLink(array $parameters = []): string
{
return BackendModel::createURLForAction(
'ContentBlockIndex',
null,
null,
$parameters
);
}

private function getForm(): Form
{
$form = $this->createForm(
ContentBlockType::class,
new CreateContentBlock(),
['theme' => $this->get('fork.settings')->get('Core', 'theme', 'Core')]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be $this->get('fork.settings')->get('Core', 'theme', 'Fork')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this has/will be fixed in another PR, so I won't change this. OK?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That PR has already been made and has been merged, so you should make sure you have the latests version of 5.0.0-dev, since git saw your changes as delete and add instead of move that change would've been lost

);

$form->handleRequest($this->get('request'));

return $form;
}
}
69 changes: 69 additions & 0 deletions src/Backend/Modules/ContentBlocks/Actions/ContentBlockDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Backend\Modules\ContentBlocks\Actions;

/*
* This file is part of Fork CMS.
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/

use Backend\Core\Engine\Base\ActionDelete as BackendBaseActionDelete;
use Backend\Core\Engine\Model as BackendModel;
use Backend\Core\Language\Locale;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\Command\DeleteContentBlock;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\ContentBlock;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\Event\ContentBlockDeleted;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\Exception\ContentBlockNotFound;

/**
* This is the delete-action, it will delete an item.
*/
class ContentBlockDelete extends BackendBaseActionDelete
{
public function execute(): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if #2090 gets merged before this one the logic here needs to be updated since it won't get merged because you changed everything in one big commit and thereby are breaking the git history

{
/** @var ContentBlock $contentBlock */
$contentBlock = $this->getContentBlock();

// The command bus will handle the saving of the content block in the database.
$this->get('command_bus')->handle(new DeleteContentBlock($contentBlock));

$this->get('event_dispatcher')->dispatch(
ContentBlockDeleted::EVENT_NAME,
new ContentBlockDeleted($contentBlock)
);

$this->redirect(
$this->getBackLink(
[
'report' => 'content-block-deleted',
'var' => $contentBlock->getTitle(),
]
)
);
}

private function getBackLink(array $parameters = []): string
{
return BackendModel::createURLForAction(
'ContentBlockIndex',
null,
null,
$parameters
);
}

private function getContentBlock(): ContentBlock
{
try {
return $this->get('content_blocks.repository.content_block')->findOneByIdAndLocale(
$this->getParameter('id', 'int'),
Locale::workingLocale()
);
} catch (ContentBlockNotFound $e) {
$this->redirect($this->getBackLink(['error' => 'non-existing']));
}
}
}
131 changes: 131 additions & 0 deletions src/Backend/Modules/ContentBlocks/Actions/ContentBlockEdit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Backend\Modules\ContentBlocks\Actions;

/*
* This file is part of Fork CMS.
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/

use Backend\Core\Engine\Authentication;
use Backend\Core\Engine\Base\ActionEdit as BackendBaseActionEdit;
use Backend\Core\Engine\Model as BackendModel;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\Command\UpdateContentBlock;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\ContentBlock;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\Event\ContentBlockUpdated;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\ContentBlockRepository;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\ContentBlockType;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\ContentBlockRevisionDataGrid;
use Backend\Core\Language\Locale;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\Exception\ContentBlockNotFound;
use Symfony\Component\Form\Form;

/**
* This is the edit-action, it will display a form to edit an existing item
*/
class ContentBlockEdit extends BackendBaseActionEdit
{
public function execute(): void
{
parent::execute();

/** @var ContentBlock $contentBlock */
$contentBlock = $this->getContentBlock();

/** @var Form $form */
$form = $this->getForm($contentBlock);

if (!$form->isValid()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking if a form is valid before checking if a form is submitted is deprecated in symfony 3.3 and removed in symfony 4, since we will need to upgrade for fork 5 this will need to be changed here as well

$this->tpl->assign('form', $form->createView());
$this->tpl->assign('contentBlock', $contentBlock);
$this->tpl->assign('revisions', ContentBlockRevisionDataGrid::getHtml($contentBlock, Locale::workingLocale()));

$this->parse();
$this->display();

return;
}

/** @var UpdateContentBlock $updateContentBlock */
$updateContentBlock = $this->updateContentBlock($form);

$this->get('event_dispatcher')->dispatch(
ContentBlockUpdated::EVENT_NAME,
new ContentBlockUpdated($updateContentBlock->getContentBlockEntity())
);

$this->redirect(
$this->getBackLink(
[
'report' => 'content-block-edited',
'var' => $updateContentBlock->title,
'highlight' => 'row-' . $contentBlock->getId(),
]
)
);
}

private function getBackLink(array $parameters = []): string
{
return BackendModel::createURLForAction(
'ContentBlockIndex',
null,
null,
$parameters
);
}

private function getContentBlock(): ContentBlock
{
/** @var ContentBlockRepository $contentBlockRepository */
$contentBlockRepository = $this->get('content_blocks.repository.content_block');

// specific revision?
$revisionId = $this->getParameter('revision', 'int');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->getParameter has been removed in fork 5.0.0
use $this->getRequest()->query->getInt('revision') instead


if ($revisionId !== null) {
$this->tpl->assign('usingRevision', true);

try {
return $contentBlockRepository->findOneByRevisionIdAndLocale($revisionId, Locale::workingLocale());
} catch (ContentBlockNotFound $e) {
$this->redirect($this->getBackLink(['error' => 'non-existing']));
}
}

try {
return $contentBlockRepository->findOneByIdAndLocale($this->getParameter('id', 'int'), Locale::workingLocale());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->getParameter has been removed in fork 5.0.0
use $this->getRequest()->query->getInt('id') instead

} catch (ContentBlockNotFound $e) {
$this->redirect($this->getBackLink(['error' => 'non-existing']));
}
}

private function getForm(ContentBlock $contentBlock): Form
{
$form = $this->createForm(
ContentBlockType::class,
new UpdateContentBlock($contentBlock),
[
'theme' => $this->get('fork.settings')->get('Core', 'theme', 'Core'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be $this->get('fork.settings')->get('Core', 'theme', 'Fork')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this has/will be fixed in another PR, so I won't change this. OK?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same answer here

]
);

$form->handleRequest($this->get('request'));

return $form;
}

private function updateContentBlock(Form $form): UpdateContentBlock
{
/** @var UpdateContentBlock $updateContentBlock */
$updateContentBlock = $form->getData();
$updateContentBlock->userId = Authentication::getUser()->getUserId();

// The command bus will handle the saving of the content block in the database.
$this->get('command_bus')->handle($updateContentBlock);

return $updateContentBlock;
}
}
28 changes: 28 additions & 0 deletions src/Backend/Modules/ContentBlocks/Actions/ContentBlockIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Backend\Modules\ContentBlocks\Actions;

/*
* This file is part of Fork CMS.
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/

use Backend\Core\Engine\Base\ActionIndex as BackendBaseActionIndex;
use Backend\Core\Language\Locale;
use Backend\Modules\ContentBlocks\Domain\ContentBlock\ContentBlockDataGrid;

/**
* This is the index-action (default), it will display the overview
*/
class ContentBlockIndex extends BackendBaseActionIndex
{
public function execute(): void
{
parent::execute();
$this->tpl->assign('dataGrid', ContentBlockDataGrid::getHtml(Locale::workingLocale()));
$this->parse();
$this->display();
}
}
60 changes: 0 additions & 60 deletions src/Backend/Modules/ContentBlocks/Actions/Delete.php

This file was deleted.

Loading