[Taxon] Implement creating taxon for parent logic

This commit is contained in:
Mateusz Zalewski 2016-11-04 13:49:25 +01:00
parent 84f96b1d8a
commit ce368cfab4
9 changed files with 177 additions and 6 deletions

View file

@ -9,7 +9,7 @@ Feature: Adding a new taxon for parent
And the store has "Category" taxonomy
And I am logged in as an administrator
@ui
@ui @javascript
Scenario: Adding a new taxon for specific parent taxon
Given I want to create a new taxon for "Category"
And I specify its code as "guns"

View file

@ -19,3 +19,19 @@ sylius_admin_taxon_index:
_controller: FrameworkBundle:Redirect:redirect
route: sylius_admin_taxon_create
permanent: true
sylius_admin_taxon_create_for_parent:
path: /taxons/new/{id}
defaults:
_controller: sylius.controller.taxon:createAction
_sylius:
section: admin
template: 'SyliusAdminBundle:Taxon:create.html.twig'
redirect: sylius_admin_taxon_update
factory:
method: createForParent
arguments: ['expr:service("sylius.repository.taxon").find($id)']
vars:
subheader: sylius.ui.manage_categorization_of_your_product
templates:
form: @SyliusAdmin/Taxon/_form.html.twig

View file

@ -5,7 +5,7 @@
{% import '@SyliusUi/Macro/buttons.html.twig' as buttons %}
{% import _self as tree %}
{% for taxon in taxons %}
{% for taxon in taxons if taxon.id != null %}
<div class="item">
<i class="folder icon"></i>
<div class="content">
@ -15,6 +15,7 @@
</a>
</div>
<div class="ui mini buttons">
{{ buttons.create(path('sylius_admin_taxon_create_for_parent', { 'id': taxon.id })) }}
{{ buttons.edit(path('sylius_admin_taxon_update', { 'id': taxon.id })) }}
{{ buttons.delete(path('sylius_admin_taxon_delete', { 'id': taxon.id }), 'sylius.ui.delete'|trans) }}
</div>
@ -29,7 +30,7 @@
{{ buttons.create(path('sylius_admin_taxon_create')) }}
<div class="ui segment">
<div class="ui list">
<div id="taxons-list" class="ui list">
{{ tree.render(taxons) }}
</div>
</div>

View file

@ -2,7 +2,11 @@
{{ form_errors(form) }}
<div class="two fields">
{{ form_row(form.code) }}
{{ form_row(form.parent) }}
{% if taxon.id != null %}
{{ form_row(form.parent) }}
{% else %}
{{ form_widget(form.parent, {'attr': {'style': 'display: none;'}}) }}
{% endif %}
</div>
</div>
<div class="ui styled fluid accordion">

View file

@ -17,5 +17,10 @@
<argument type="service" id="sylius.repository.taxon" />
<tag name="form.type" alias="sylius_taxon_choice" />
</service>
<service id="sylius.factory.taxon.decorated" class="Sylius\Component\Taxonomy\Factory\TaxonFactory"
decorates="sylius.factory.taxon" public="false">
<argument type="service" id="sylius.factory.taxon.decorated.inner" />
</service>
</services>
</container>

View file

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Taxonomy\Factory;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Taxonomy\Model\TaxonInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
final class TaxonFactory implements TaxonFactoryInterface
{
/**
* @var FactoryInterface
*/
private $factory;
/**
* @param FactoryInterface $factory
*/
public function __construct(FactoryInterface $factory)
{
$this->factory = $factory;
}
/**
* {@inheritdoc}
*/
public function createNew()
{
return $this->factory->createNew();
}
/**
* {@inheritdoc}
*/
public function createForParent(TaxonInterface $parent)
{
$taxon = $this->createNew();
$taxon->setParent($parent);
return $taxon;
}
}

View file

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Taxonomy\Factory;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Taxonomy\Model\TaxonInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
interface TaxonFactoryInterface extends FactoryInterface
{
/**
* @param TaxonInterface $parent
*
* @return TaxonInterface
*/
public function createForParent(TaxonInterface $parent);
}

View file

@ -134,6 +134,9 @@ class Taxon implements TaxonInterface
public function setParent(TaxonInterface $parent = null)
{
$this->parent = $parent;
if (null !== $parent) {
$parent->addChild($this);
}
}
/**
@ -176,10 +179,12 @@ class Taxon implements TaxonInterface
public function addChild(TaxonInterface $taxon)
{
if (!$this->hasChild($taxon)) {
$taxon->setParent($this);
$this->children->add($taxon);
}
if ($this !== $taxon->getParent()) {
$taxon->setParent($this);
}
}
/**

View file

@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Component\Taxonomy\Factory;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Taxonomy\Factory\TaxonFactory;
use Sylius\Component\Taxonomy\Factory\TaxonFactoryInterface;
use Sylius\Component\Taxonomy\Model\TaxonInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
final class TaxonFactorySpec extends ObjectBehavior
{
function let(FactoryInterface $factory)
{
$this->beConstructedWith($factory);
}
function it_is_initializable()
{
$this->shouldHaveType(TaxonFactory::class);
}
function it_implements_taxon_factory_interface()
{
$this->shouldImplement(TaxonFactoryInterface::class);
}
function it_uses_decorated_factory_to_create_new_taxon(
FactoryInterface $factory,
TaxonInterface $taxon
) {
$factory->createNew()->willReturn($taxon);
$this->createNew()->shouldReturn($taxon);
}
function it_creates_taxon_for_given_parent_taxon(
FactoryInterface $factory,
TaxonInterface $parent,
TaxonInterface $taxon
) {
$factory->createNew()->willReturn($taxon);
$taxon->setParent($parent)->shouldBeCalled();
$this->createForParent($parent)->shouldReturn($taxon);
}
}