Skip to content

Commit

Permalink
Merge pull request #7468 from NeverResponse/form-menu-builder
Browse files Browse the repository at this point in the history
[UI][Admin] Form menu builders for Product and Product Variant edits
  • Loading branch information
pjedrzejewski committed Feb 17, 2017
2 parents 9de44cb + 420cb62 commit cc28ed2
Show file tree
Hide file tree
Showing 17 changed files with 416 additions and 58 deletions.
48 changes: 48 additions & 0 deletions src/Sylius/Bundle/AdminBundle/Event/ProductMenuBuilderEvent.php
@@ -0,0 +1,48 @@
<?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\Bundle\AdminBundle\Event;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;
use Sylius\Component\Product\Model\ProductInterface;

/**
* @author Jan Góralski <jan.goralski@lakion.com>
*/
class ProductMenuBuilderEvent extends MenuBuilderEvent
{
/**
* @var ProductInterface
*/
private $product;

/**
* @param FactoryInterface $factory
* @param ItemInterface $menu
* @param ProductInterface $product
*/
public function __construct(FactoryInterface $factory, ItemInterface $menu, ProductInterface $product)
{
parent::__construct($factory, $menu);

$this->product = $product;
}

/**
* @return ProductInterface
*/
public function getProduct()
{
return $this->product;
}
}
@@ -0,0 +1,48 @@
<?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\Bundle\AdminBundle\Event;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;
use Sylius\Component\Core\Model\ProductVariantInterface;

/**
* @author Jan Góralski <jan.goralski@lakion.com>
*/
class ProductVariantMenuBuilderEvent extends MenuBuilderEvent
{
/**
* @var ProductVariantInterface
*/
private $productVariant;

/**
* @param FactoryInterface $factory
* @param ItemInterface $menu
* @param ProductVariantInterface $productVariant
*/
public function __construct(FactoryInterface $factory, ItemInterface $menu, ProductVariantInterface $productVariant)
{
parent::__construct($factory, $menu);

$this->productVariant = $productVariant;
}

/**
* @return ProductVariantInterface
*/
public function getProductVariant()
{
return $this->productVariant;
}
}
98 changes: 98 additions & 0 deletions src/Sylius/Bundle/AdminBundle/Menu/ProductFormMenuBuilder.php
@@ -0,0 +1,98 @@
<?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\Bundle\AdminBundle\Menu;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Sylius\Bundle\AdminBundle\Event\ProductMenuBuilderEvent;
use Sylius\Component\Core\Model\ProductInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @author Jan Góralski <jan.goralski@lakion.com>
*/
final class ProductFormMenuBuilder
{
const EVENT_NAME = 'sylius.menu.admin.product.form';

/**
* @var FactoryInterface
*/
private $factory;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* @param FactoryInterface $factory
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
{
$this->factory = $factory;
$this->eventDispatcher = $eventDispatcher;
}

/**
* @param array $options
*
* @return ItemInterface
*/
public function createMenu(array $options = [])
{
$menu = $this->factory->createItem('root');

if (!array_key_exists('product', $options) || !$options['product'] instanceof ProductInterface) {
return $menu;
}

$menu
->addChild('details')
->setAttribute('template', '@SyliusAdmin/Product/Tab/_details.html.twig')
->setLabel('sylius.ui.details')
->setCurrent(true)
;

$menu
->addChild('taxonomy')
->setAttribute('template', '@SyliusAdmin/Product/Tab/_taxonomy.html.twig')
->setLabel('sylius.ui.taxonomy')
;

$menu
->addChild('attributes')
->setAttribute('template', '@SyliusAdmin/Product/Tab/_attributes.html.twig')
->setLabel('sylius.ui.attributes')
;

$menu
->addChild('associations')
->setAttribute('template', '@SyliusAdmin/Product/Tab/_associations.html.twig')
->setLabel('sylius.ui.associations')
;

$menu
->addChild('media')
->setAttribute('template', '@SyliusAdmin/Product/Tab/_media.html.twig')
->setLabel('sylius.ui.media')
;

$this->eventDispatcher->dispatch(
self::EVENT_NAME,
new ProductMenuBuilderEvent($this->factory, $menu, $options['product'])
);

return $menu;
}
}
@@ -0,0 +1,80 @@
<?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\Bundle\AdminBundle\Menu;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Sylius\Bundle\AdminBundle\Event\ProductVariantMenuBuilderEvent;
use Sylius\Component\Product\Model\ProductVariantInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @author Jan Góralski <jan.goralski@lakion.com>
*/
final class ProductVariantFormMenuBuilder
{
const EVENT_NAME = 'sylius.menu.admin.product_variant.form';

/**
* @var FactoryInterface
*/
private $factory;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* @param FactoryInterface $factory
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
{
$this->factory = $factory;
$this->eventDispatcher = $eventDispatcher;
}

/**
* @param array $options
*
* @return ItemInterface
*/
public function createMenu(array $options = [])
{
$menu = $this->factory->createItem('root');

if (!array_key_exists('product_variant', $options) || !$options['product_variant'] instanceof ProductVariantInterface) {
return $menu;
}

$menu
->addChild('details')
->setAttribute('template', '@SyliusAdmin/ProductVariant/Tab/_details.html.twig')
->setLabel('sylius.ui.details')
->setCurrent(true)
;

$menu
->addChild('taxes')
->setAttribute('template', '@SyliusAdmin/ProductVariant/Tab/_taxes.html.twig')
->setLabel('sylius.ui.taxes')
;

$this->eventDispatcher->dispatch(
self::EVENT_NAME,
new ProductVariantMenuBuilderEvent($this->factory, $menu, $options['product_variant'])
);

return $menu;
}
}
12 changes: 12 additions & 0 deletions src/Sylius/Bundle/AdminBundle/Resources/config/services/menu.xml
Expand Up @@ -31,5 +31,17 @@
<argument type="service" id="sm.factory" />
<tag name="knp_menu.menu_builder" method="createMenu" alias="sylius.admin.order.show" />
</service>

<service id="sylius.admin.menu_builder.product_form" class="Sylius\Bundle\AdminBundle\Menu\ProductFormMenuBuilder">
<argument type="service" id="knp_menu.factory" />
<argument type="service" id="event_dispatcher" />
<tag name="knp_menu.menu_builder" method="createMenu" alias="sylius.admin.product_form" />
</service>

<service id="sylius.admin.menu_builder.product_variant_form" class="Sylius\Bundle\AdminBundle\Menu\ProductVariantFormMenuBuilder">
<argument type="service" id="knp_menu.factory" />
<argument type="service" id="event_dispatcher" />
<tag name="knp_menu.menu_builder" method="createMenu" alias="sylius.admin.product_variant_form" />
</service>
</services>
</container>
3 changes: 3 additions & 0 deletions src/Sylius/Bundle/AdminBundle/Resources/private/js/app.js
Expand Up @@ -48,6 +48,9 @@
}, 50);
});

$('.sylius-tabular-form').addTabErrors();
$('.ui.accordion').addAccordionErrors();

$(document).productSlugGenerator();
$(document).taxonSlugGenerator();
});
Expand Down
@@ -0,0 +1,47 @@
/*
* 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.
*/

(function ($) {
'use strict';

$.fn.extend({
addTabErrors: function () {
var element = $(this);
var tabElements = element.find('.ui.segment');

$(tabElements).each(function () {
var errors = $(this).find('.sylius-validation-error');

if(0 !== errors.length) {
var tabName = $(this).find('.ui.tab').attr('data-tab');
var tabWithErrors = $(element).find('a.item[data-tab="' + tabName + '"]');

var label = tabWithErrors.html();
var newLabel = label + '<span class="ui small horizontal circular label" style="background-color: #DB2828">' + errors.length + '</span>';

tabWithErrors.html(newLabel);
}
});
},
addAccordionErrors: function () {
var element = $(this);
var accordionElements = element.find('.ui.content');

$(accordionElements).each(function () {
var errors = $(this).find('.sylius-validation-error');

if(0 !== errors.length) {
var ribWithErrors = $(this).closest('[data-locale]').find('.title');

ribWithErrors.css('color', '#DB2828');
}
});
}
})
})( jQuery );
Expand Up @@ -12,7 +12,7 @@
{% for association in product.associations %}
{% if association.type.code == associationForm.vars.name %}
{% for associatedProduct in association.associatedProducts %}
<div class="item" data-value="{{ associatedProduct.code }}">{{ associatedProduct }}</div>
<div class="item" data-value="{{ associatedProduct.code }}">{{ associatedProduct.name|default(associatedProduct.code) }}</div>
{% endfor %}
{% endif %}
{% endfor %}
Expand Down
@@ -1,12 +1,2 @@
{{ form_errors(form) }}

<div class="ui stackable grid">
<div class="three wide column">
{% include '@SyliusAdmin/Product/_menu.html.twig' %}
</div>
<div class="thirteen wide column">
<div class="ui segment">
{% include '@SyliusAdmin/Product/_tabs.html.twig' %}
</div>
</div>
</div>
{% set menu = knp_menu_get('sylius.admin.product_form', [], {'product': product}) %}
{{ knp_menu_render(menu, {'template': 'SyliusAdminBundle:Product:_menu.html.twig', 'form': form, 'product': product}) }}

0 comments on commit cc28ed2

Please sign in to comment.