Skip to content

Commit

Permalink
Merge pull request #514 from BitBagCommerce/feature/OP-461
Browse files Browse the repository at this point in the history
OP-461: Page templates
  • Loading branch information
senghe authored Aug 2, 2024
2 parents 6cfa6eb + 27901a9 commit 7567d5a
Show file tree
Hide file tree
Showing 76 changed files with 1,721 additions and 57 deletions.
15 changes: 15 additions & 0 deletions features/admin/adding_page.feature
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,18 @@ Feature: Adding new page
Then I should be notified that the page has been created
And I should see newly created "Single media" content element in Content elements section
And I should see newly created "Multiple media" content element in Content elements section

@ui @javascript
Scenario: Adding page with template
Given there is an existing template named "Homepage" with "Page" type that contains "Textarea, Single media" content elements
When I go to the create page page
And I fill the code with "my_page"
And I fill the slug with "my_page"
And I fill the name with "My page"
And I select "Homepage" template
And I click button to use this template
And I confirm that I want to use this template
And I add it
Then I should be notified that the page has been created
And I should see newly created "Textarea" content element in Content elements section
And I should see newly created "Single media" content element in Content elements section
59 changes: 59 additions & 0 deletions features/admin/adding_template.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@managing_templates
Feature: Adding cms templates
In order to create templates
As an Administrator
I want to be able to add new templates

Background:
Given the store operates on a single channel in "United States"
And I am logged in as an administrator

@ui
Scenario: Creating template
When I go to the create template page
And I fill the name with "Test template"
And I choose "Page" in Type field
And I add it
Then I should be notified that the template has been created

@ui @javascript
Scenario: Creating template with content elements
When I go to the create template page
And I fill the name with "Test template"
And I choose "Page" in Type field
And I click on Add button in Content elements section
And I select "Textarea" content element
And I click on Add button in Content elements section
And I select "Single media" content element
And I add it
Then I should be notified that the template has been created
And I should see newly created "Textarea" content element in Content elements section
And I should see newly created "Single media" content element in Content elements section

@ui
Scenario: Trying to add template with existing name
Given there is a template in the store with "New template" name
When I go to the create template page
And I fill the name with "New template"
And I try to add it
Then I should be notified that there is already existing template with provided name

@ui
Scenario: Adding new template with blank data
When I go to the create template page
And I add it
And I should be notified that "Name" field cannot be blank

@ui
Scenario: Trying to add a template with too short data
When I go to the create template page
And I fill the name with "X"
And I try to add it
Then I should be notified that "Name" field is too short

@ui
Scenario: Trying to add a template with too long data
When I go to the create template page
And I fill "Name" field with 251 characters
And I try to add it
Then I should be notified that "Name" field is too long
35 changes: 35 additions & 0 deletions features/admin/managing_templates.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@managing_templates
Feature: Managing cms templates
In order to manage existing templates
As an Administrator
I want to be able to edit and remove existing templates

Background:
Given the store operates on a single channel in "United States"
And I am logged in as an administrator

@ui
Scenario: Deleting template
Given there is a template in the store with "Test template" name
When I go to the templates page
And I delete this template
Then I should be notified that the template has been deleted
And I should see empty list of templates

@ui
Scenario: Updating template
Given there is a template in the store with "Test template" name
When I go to the update "Test template" template page
And I fill the name with "New template"
And I update it
Then I should be notified that the template has been successfully updated

@ui @javascript
Scenario: Updating template with content elements
Given there is a template in the store with "Test template" name
And there are "Textarea" and "Single media" content elements in this template
When I go to the update "Test template" template page
And I delete "Textarea" content element
And I update it
Then I should be notified that the template has been successfully updated
And I should see only "Single media" content element in Content elements section
7 changes: 7 additions & 0 deletions spec/Menu/ContentManagementMenuBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public function it_build_menu(
$cmsRootMenuItem->setLabel('bitbag_sylius_cms_plugin.ui.collections')->willReturn($cmsRootMenuItem);
$cmsRootMenuItem->setLabelAttribute('icon', 'grid layout')->shouldBeCalled();

$cmsRootMenuItem
->addChild('templates', ['route' => 'bitbag_sylius_cms_plugin_admin_template_index'])
->willReturn($cmsRootMenuItem)
;
$cmsRootMenuItem->setLabel('bitbag_sylius_cms_plugin.ui.templates')->willReturn($cmsRootMenuItem);
$cmsRootMenuItem->setLabelAttribute('icon', 'clone')->shouldBeCalled();

$cmsRootMenuItem
->addChild('media', ['route' => 'bitbag_sylius_cms_plugin_admin_media_index'])
->willReturn($cmsRootMenuItem)
Expand Down
34 changes: 34 additions & 0 deletions src/Controller/TemplateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Controller;

use BitBag\SyliusCmsPlugin\Entity\TemplateInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\JsonResponse;

final class TemplateController extends ResourceController
{
public function getContentElementsAction(int $id): JsonResponse
{
$template = $this->getDoctrine()->getRepository(TemplateInterface::class)->find($id);
if (null === $template) {
return new JsonResponse([
'status' => 'error',
'message' => 'Template not found',
]);
}

return new JsonResponse([
'status' => 'success',
'content' => $template->getContentElements(),
]);
}
}
4 changes: 2 additions & 2 deletions src/Entity/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use BitBag\SyliusCmsPlugin\Entity\Trait\ChannelsAwareTrait;
use BitBag\SyliusCmsPlugin\Entity\Trait\CollectibleTrait;
use BitBag\SyliusCmsPlugin\Entity\Trait\ContentConfigurationAwareTrait;
use BitBag\SyliusCmsPlugin\Entity\Trait\ContentElementsAwareTrait;
use BitBag\SyliusCmsPlugin\Entity\Trait\LocaleAwareTrait;
use BitBag\SyliusCmsPlugin\Entity\Trait\ProductsAwareTrait;
use BitBag\SyliusCmsPlugin\Entity\Trait\ProductsInTaxonsAwareTrait;
Expand All @@ -24,7 +24,7 @@ class Block implements BlockInterface
use ToggleableTrait;
use CollectibleTrait;
use ChannelsAwareTrait;
use ContentConfigurationAwareTrait;
use ContentElementsAwareTrait;
use LocaleAwareTrait;
use ProductsAwareTrait;
use TaxonAwareTrait;
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/BlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface BlockInterface extends
ToggleableInterface,
CollectibleInterface,
ChannelsAwareInterface,
ContentConfigurationAwareInterface,
ContentElementsAwareInterface,
LocaleAwareInterface,
ProductsAwareInterface,
TaxonAwareInterface,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Doctrine\Common\Collections\Collection;

interface ContentConfigurationAwareInterface
interface ContentElementsAwareInterface
{
public function initializeContentElementsCollection(): void;

Expand Down
4 changes: 2 additions & 2 deletions src/Entity/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use BitBag\SyliusCmsPlugin\Entity\Trait\ChannelsAwareTrait;
use BitBag\SyliusCmsPlugin\Entity\Trait\CollectibleTrait;
use BitBag\SyliusCmsPlugin\Entity\Trait\ContentConfigurationAwareTrait;
use BitBag\SyliusCmsPlugin\Entity\Trait\ContentElementsAwareTrait;
use Sylius\Component\Resource\Model\TimestampableTrait;
use Sylius\Component\Resource\Model\ToggleableTrait;
use Sylius\Component\Resource\Model\TranslatableTrait;
Expand All @@ -24,7 +24,7 @@ class Page implements PageInterface
use CollectibleTrait;
use TimestampableTrait;
use ChannelsAwareTrait;
use ContentConfigurationAwareTrait;
use ContentElementsAwareTrait;
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/PageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface PageInterface extends
TimestampableInterface,
ChannelsAwareInterface,
SlugAwareInterface,
ContentConfigurationAwareInterface
ContentElementsAwareInterface
{
public function getCode(): ?string;

Expand Down
57 changes: 57 additions & 0 deletions src/Entity/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Entity;

class Template implements TemplateInterface
{
protected ?int $id;

protected ?string $name;

protected ?string $type;

protected array $contentElements = [];

public function getId(): ?int
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): void
{
$this->name = $name;
}

public function getType(): ?string
{
return $this->type;
}

public function setType(?string $type): void
{
$this->type = $type;
}

public function getContentElements(): array
{
return $this->contentElements;
}

public function setContentElements(array $contentElements): void
{
$this->contentElements = $contentElements;
}
}
28 changes: 28 additions & 0 deletions src/Entity/TemplateInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Entity;

use Sylius\Component\Resource\Model\ResourceInterface;

interface TemplateInterface extends ResourceInterface
{
public function getName(): ?string;

public function setName(?string $name): void;

public function getType(): ?string;

public function setType(?string $type): void;

public function getContentElements(): array;

public function setContentElements(array $contentElements): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* @property Collection $contentElements
*/
trait ContentConfigurationAwareTrait
trait ContentElementsAwareTrait
{
protected Collection $contentElements;

Expand Down
43 changes: 43 additions & 0 deletions src/Form/Type/AbstractTemplateAutocompleteChoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Form\Type;

use Sylius\Bundle\ResourceBundle\Form\Type\ResourceAutocompleteChoiceType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

abstract class AbstractTemplateAutocompleteChoiceType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'resource' => 'bitbag_sylius_cms_plugin.template',
'choice_name' => 'name',
'choice_value' => 'id',
]);
}

public function buildView(
FormView $view,
FormInterface $form,
array $options,
): void {
$view->vars['remote_criteria_type'] = 'contains';
$view->vars['remote_criteria_name'] = 'phrase';
}

public function getParent(): string
{
return ResourceAutocompleteChoiceType::class;
}
}
Loading

0 comments on commit 7567d5a

Please sign in to comment.