-
-
Notifications
You must be signed in to change notification settings - Fork 325
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
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 */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
@@ -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')] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'])); | ||
} | ||
} | ||
} |
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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $this->getParameter has been removed in fork 5.0.0 |
||
|
||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $this->getParameter has been removed in fork 5.0.0 |
||
} 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'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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(); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
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.