@@ -0,0 +1,8 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Context; | |||
interface SortableInterface | |||
{ | |||
} |
@@ -0,0 +1,8 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Context; | |||
interface StatusInterface | |||
{ | |||
} |
@@ -0,0 +1,22 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Context; | |||
interface TreeInterface | |||
{ | |||
/** | |||
* Retourne le parent d'une entité | |||
* | |||
* @return entity | |||
*/ | |||
public function getParent(); | |||
/** | |||
* Retourne les enfants d'une entité | |||
* | |||
* @return entity | |||
*/ | |||
public function getChildrens(); | |||
} |
@@ -6,8 +6,12 @@ use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController; | |||
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents; | |||
use FOS\UserBundle\Model\UserManagerInterface; | |||
use Lc\ShopBundle\Context\MerchantInterface; | |||
use Lc\ShopBundle\Context\SortableInterface; | |||
use Lc\ShopBundle\Context\StatusInterface; | |||
use Lc\ShopBundle\Context\TreeInterface; | |||
use Lc\ShopBundle\Form\AbstractEditPositionType; | |||
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | |||
use Symfony\Component\HttpFoundation\Request; | |||
use Symfony\Component\Security\Core\Security; | |||
class AdminController extends EasyAdminController | |||
@@ -19,14 +23,21 @@ class AdminController extends EasyAdminController | |||
{ | |||
$this->security = $security; | |||
$this->userManager = $userManager; | |||
} | |||
protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null) | |||
{ | |||
if ($pos = strpos($dqlFilter, 'currentMerchant')) { | |||
$dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter)); | |||
} | |||
if (new $entityClass instanceof StatusInterface) { | |||
if($dqlFilter) $dqlFilter .= sprintf(' AND entity.status > 0'); | |||
else $dqlFilter .= sprintf(' entity.status > 0'); | |||
} | |||
$queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter); | |||
if ($entityClass == 'App\Entity\PointSale') { | |||
@@ -61,6 +72,13 @@ class AdminController extends EasyAdminController | |||
]; | |||
} | |||
protected function removeEntity($entity) | |||
{ | |||
$entity->setStatus(-1); | |||
$this->em->persist($entity); | |||
$this->em->flush(); | |||
} | |||
public function updateEntity($entity) | |||
{ | |||
$this->setUpdated($entity); | |||
@@ -70,16 +88,21 @@ class AdminController extends EasyAdminController | |||
public function persistEntity($entity) | |||
{ | |||
if (method_exists($entity, 'setPosition')) { | |||
//Product Category avec parent | |||
if (method_exists($entity, 'getProductCategory') && $entity->getProductCategory()) { | |||
$total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'productCategory' => $entity->getProductCategory()->getId())); | |||
$entity->setPosition($entity->getProductCategory()->getId() . '_' . $total); | |||
if ($entity instanceof StatusInterface) { | |||
$entity->setStatus(1); | |||
} | |||
//set position | |||
if ($entity instanceof SortableInterface) { | |||
if ($entity instanceof TreeInterface) { | |||
if ($entity->getParent()) { | |||
$total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => $entity->getParent()->getId())); | |||
$entity->setPosition($entity->getParent()->getId() . '_' . $total); | |||
} else { | |||
$total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => null)); | |||
$entity->setPosition($total); | |||
} | |||
}else{ | |||
$total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'productCategory' => null )); | |||
$total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1)); | |||
$entity->setPosition($total); | |||
} | |||
} | |||
@@ -115,9 +138,7 @@ class AdminController extends EasyAdminController | |||
$this->setUpdated($entity); | |||
if (method_exists($entity, 'setStatus')) { | |||
$entity->setStatus(1); | |||
} | |||
parent::persistEntity($entity); | |||
} | |||
@@ -152,7 +173,7 @@ class AdminController extends EasyAdminController | |||
$this->dispatch(EasyAdminEvents::PRE_LIST); | |||
$id = $this->request->query->get('id'); | |||
$this->entity['list']['dql_filter'] = "entity.productCategory = " . $id; | |||
$this->entity['list']['dql_filter'] = "entity.parent = " . $id; | |||
$easyadmin = $this->request->attributes->get('easyadmin'); | |||
$entity = $easyadmin['item']; | |||
@@ -179,9 +200,9 @@ class AdminController extends EasyAdminController | |||
$this->dispatch(EasyAdminEvents::PRE_LIST); | |||
$entity = null; | |||
if($this->request->query->get('id')) { | |||
if ($this->request->query->get('id')) { | |||
$id = $this->request->query->get('id'); | |||
$this->entity['list']['dql_filter'] = "entity.productCategory = " . $id; | |||
$this->entity['list']['dql_filter'] = "entity.parent = " . $id; | |||
$easyadmin = $this->request->attributes->get('easyadmin'); | |||
$entity = $easyadmin['item']; | |||
@@ -191,21 +212,21 @@ class AdminController extends EasyAdminController | |||
$this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]); | |||
$positionForm =$this->createFormBuilder(array('entities', $paginator->getCurrentPageResults())) | |||
->add('entities', CollectionType::class ,array( | |||
'required' => true, | |||
'allow_add' => true, | |||
'entry_type' => AbstractEditPositionType::class, | |||
$positionForm = $this->createFormBuilder(array('entities', $paginator->getCurrentPageResults())) | |||
->add('entities', CollectionType::class, array( | |||
'required' => true, | |||
'allow_add' => true, | |||
'entry_type' => AbstractEditPositionType::class, | |||
)) | |||
->getForm(); | |||
$positionForm->handleRequest($this->request); | |||
if($positionForm->isSubmitted()&& $positionForm->isValid()){ | |||
if ($positionForm->isSubmitted() && $positionForm->isValid()) { | |||
$class = $this->entity['class']; | |||
$repo = $this->em->getRepository($class); | |||
foreach ($positionForm->get('entities')->getData() as $elm){ | |||
foreach ($positionForm->get('entities')->getData() as $elm) { | |||
$this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]); | |||
$entity = $repo->find($elm['id']); | |||
$entity->setPosition($elm['position']); | |||
@@ -215,7 +236,7 @@ class AdminController extends EasyAdminController | |||
} | |||
$this->em->flush(); | |||
$this->addFlash('success','Position modifié', array(), 'mweb'); | |||
$this->addFlash('success', 'Position modifié', array(), 'mweb'); | |||
return $this->redirectToReferrer(); | |||
@@ -228,7 +249,7 @@ class AdminController extends EasyAdminController | |||
'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(), | |||
'postion_form' => $positionForm->createView(), | |||
'entity' => $entity, | |||
'sortable'=> true | |||
'sortable' => true | |||
]; | |||
return $this->executeDynamicMethod('render<EntityName>Template', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]); | |||
} |
@@ -4,15 +4,19 @@ namespace Lc\ShopBundle\Model; | |||
use Doctrine\ORM\Mapping as ORM; | |||
use Gedmo\Mapping\Annotation as Gedmo; | |||
use Lc\ShopBundle\Context\SortableInterface; | |||
use Lc\ShopBundle\Context\StatusInterface; | |||
use Lc\ShopBundle\Model\AbstractEntity; | |||
/** | |||
* @ORM\MappedSuperclass | |||
*/ | |||
abstract class AbstractDocumentEntity extends AbstractEntity | |||
abstract class AbstractDocumentEntity extends AbstractEntity implements StatusInterface, SortableInterface | |||
{ | |||
use SortableTrait; | |||
use StatusTrait; | |||
/** | |||
* @ORM\Column(type="string", length=255) | |||
*/ | |||
@@ -34,11 +38,6 @@ abstract class AbstractDocumentEntity extends AbstractEntity | |||
*/ | |||
protected $image; | |||
/** | |||
* @ORM\Column(type="float") | |||
*/ | |||
protected $status; | |||
public function getTitle(): ?string | |||
{ | |||
@@ -88,16 +87,5 @@ abstract class AbstractDocumentEntity extends AbstractEntity | |||
return $this; | |||
} | |||
public function getStatus(): ?float | |||
{ | |||
return $this->status; | |||
} | |||
public function setStatus(float $status): self | |||
{ | |||
$this->status = $status; | |||
return $this; | |||
} | |||
} |
@@ -5,24 +5,23 @@ namespace Lc\ShopBundle\Model; | |||
use Doctrine\Common\Collections\ArrayCollection; | |||
use Doctrine\Common\Collections\Collection; | |||
use Doctrine\ORM\Mapping as ORM; | |||
use Orkestra\EaSortable\SortableTrait; | |||
use Lc\ShopBundle\Context\TreeInterface; | |||
/** | |||
* @ORM\MappedSuperclass() | |||
*/ | |||
abstract class ProductCategory extends AbstractDocumentEntity | |||
abstract class ProductCategory extends AbstractDocumentEntity implements TreeInterface | |||
{ | |||
/** | |||
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\ProductCategoryInterface", inversedBy="productCategories") | |||
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\ProductCategoryInterface", inversedBy="childrens") | |||
*/ | |||
protected $productCategory; | |||
protected $parent; | |||
/** | |||
* @ORM\OneToMany(targetEntity="Lc\ShopBundle\Context\ProductCategoryInterface", mappedBy="productCategory") | |||
* @ORM\OneToMany(targetEntity="Lc\ShopBundle\Context\ProductCategoryInterface", mappedBy="parent") | |||
*/ | |||
protected $productCategories; | |||
protected $childrens; | |||
public function __toString() | |||
{ | |||
@@ -35,14 +34,16 @@ abstract class ProductCategory extends AbstractDocumentEntity | |||
$this->productCategories = new ArrayCollection(); | |||
} | |||
public function getProductCategory(): ?self | |||
public function getParent(): ?self | |||
{ | |||
return $this->productCategory; | |||
return $this->parent; | |||
} | |||
public function setProductCategory(?self $productCategory): self | |||
public function setParent(?self $productCategory): self | |||
{ | |||
$this->productCategory = $productCategory; | |||
$this->parent = $productCategory; | |||
return $this; | |||
} | |||
@@ -50,28 +51,28 @@ abstract class ProductCategory extends AbstractDocumentEntity | |||
/** | |||
* @return Collection|self[] | |||
*/ | |||
public function getProductCategories(): Collection | |||
public function getChildrens(): Collection | |||
{ | |||
return $this->productCategories; | |||
return $this->childrens; | |||
} | |||
public function addProductCategory(self $productCategory): self | |||
public function addChildren(self $productCategory): self | |||
{ | |||
if (!$this->productCategories->contains($productCategory)) { | |||
$this->productCategories[] = $productCategory; | |||
$productCategory->setProductCategory($this); | |||
if (!$this->childrens->contains($productCategory)) { | |||
$this->childrens[] = $productCategory; | |||
$productCategory->setParent($this); | |||
} | |||
return $this; | |||
} | |||
public function removeProductCategory(self $productCategory): self | |||
public function removeChildren(self $productCategory): self | |||
{ | |||
if ($this->productCategories->contains($productCategory)) { | |||
$this->productCategories->removeElement($productCategory); | |||
if ($this->childrens->contains($productCategory)) { | |||
$this->childrens->removeElement($productCategory); | |||
// set the owning side to null (unless already changed) | |||
if ($productCategory->getProductCategory() === $this) { | |||
$productCategory->setProductCategory(null); | |||
if ($productCategory->getParent() === $this) { | |||
$productCategory->setParent(null); | |||
} | |||
} | |||
@@ -0,0 +1,26 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Model; | |||
use Doctrine\ORM\Mapping as ORM; | |||
use Lc\ShopBundle\Context\StatusInterface; | |||
trait StatusTrait | |||
{ | |||
/** | |||
* @ORM\Column(type="float") | |||
*/ | |||
protected $status; | |||
public function getStatus(): ?float | |||
{ | |||
return $this->status; | |||
} | |||
public function setStatus(float $status): self | |||
{ | |||
$this->status = $status; | |||
return $this; | |||
} | |||
} |
@@ -0,0 +1,12 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Model; | |||
use Doctrine\Common\Collections\ArrayCollection; | |||
use Doctrine\ORM\Mapping as ORM; | |||
use Lc\ShopBundle\Context\StatusInterface; | |||
trait TreeTrait | |||
{ | |||
} |
@@ -8,4 +8,5 @@ | |||
} | |||
.ui-sortable-helper{ display: table;} | |||
.ui-state-highlight{background: #eee} | |||
.ui-state-highlight{background: #eee} | |||
.lc-sortable div:last-child{display: none;} |
@@ -1,13 +1,12 @@ | |||
jQuery(document).ready(function() { | |||
custom_switch_merchants() ; | |||
jQuery(document).ready(function () { | |||
custom_switch_merchants(); | |||
setSortableList(); | |||
}) ; | |||
}); | |||
function custom_switch_merchants() { | |||
$('#switch-merchant select').change(function() { | |||
$('#switch-merchant form').submit() ; | |||
}) ; | |||
$('#switch-merchant select').change(function () { | |||
$('#switch-merchant form').submit(); | |||
}); | |||
} | |||
function setSortableList() { | |||
@@ -21,16 +20,20 @@ function setSortableList() { | |||
prototype = $('#form_entities').data('prototype'); | |||
$('.lc-sortable tr.lc-draggable').each(function (index, li) { | |||
// Replace '__name__' in the prototype's HTML to | |||
// instead be a number based on how many items we have | |||
var newForm = prototype.replace(/__name__/g, index); | |||
$(li).append(newForm); | |||
// Replace '__name__' in the prototype's HTML to | |||
//$(li).find('div:last-child').remove(); | |||
$(li).find('div:last-child').remove(); | |||
$(li).append(newForm); | |||
$(li).find('#form_entities_' + index + '_id').val($(li).data('id')); | |||
log($('.lc-sortable').data('parent-position')+'_'+index); | |||
$(li).find('#form_entities_' + index + '_position').val($('.lc-sortable').data('parent-position')+'_'+index); | |||
if ($('.lc-sortable').data('parent-position') !=='') val = $('.lc-sortable').data('parent-position') + '_' + index | |||
else val = index; | |||
log($(li).find('#form_entities_' + index + '_position')); | |||
$(li).find('#form_entities_' + index + '_position').val(val); | |||
}); | |||
}); |
@@ -1,6 +1,12 @@ | |||
{% extends '@EasyAdmin/default/list.html.twig' %} | |||
{% block content_title %} | |||
{% if 'action' in app.request.uri and app.request.get('action') == "listChildren" %} | |||
{% set param = _request_parameters|filter((v,k) => k != 'id') %} | |||
{% set action_href = path('easyadmin',param|merge({ action: 'list' })) %} | |||
<a href="{{ action_href }}">< Retour à la liste</a> | |||
{% endif %} | |||
{{ parent() }} | |||
{% if 'action' in app.request.uri and app.request.get('action') == "listChildren" %} | |||
<strong>: {{ entity.title }}</strong> |