From 91c5dfd0bb980385a153761a8bcc9aaae03bcb25 Mon Sep 17 00:00:00 2001 From: JohnKrovitch Date: Fri, 9 Dec 2016 16:22:44 +0100 Subject: [PATCH 1/5] fix typo in documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f33516154..706db4fc6 100644 --- a/README.md +++ b/README.md @@ -237,7 +237,7 @@ Your admin is now ready! routing_url_pattern: custom/planet/{admin}/{action} routing_name_pattern: tauntaun.{admin}.{action} # Your custom controller (can extends CRUDController to ease Admin management) - controller: MyLittleTaunTaunBundle:MyController + controller: MyLittleTaunTaunBundle:TaunTaun max_per_page: 5 # Should implements DataProviderInterface data_provider: 'my.custom.data_provider.service' From cb260780e9ba4c4778ebdeca0114813478b76b65 Mon Sep 17 00:00:00 2001 From: JohnKrovitch Date: Sun, 11 Dec 2016 21:53:34 +0100 Subject: [PATCH 2/5] fix bug when loading entities without pagination --- Admin/Admin.php | 136 ++++++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 73 deletions(-) diff --git a/Admin/Admin.php b/Admin/Admin.php index cc9523954..92a26b28c 100644 --- a/Admin/Admin.php +++ b/Admin/Admin.php @@ -21,71 +21,72 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Security\Core\User\UserInterface; +use Traversable; class Admin implements AdminInterface { use AdminTrait; - + /** * Entities collection. * * @var ArrayCollection */ protected $entities; - + /** * @var MessageHandlerInterface */ protected $messageHandler; - + /** * @var EntityManagerInterface */ protected $entityManager; - + /** * @var DataProviderInterface */ protected $dataProvider; - + /** * Admin configuration object * * @var AdminConfiguration */ protected $configuration; - + /** * Admin configured actions * * @var ActionInterface[] */ protected $actions = []; - + /** * Admin current action. It will be set after calling the handleRequest() * * @var ActionInterface */ protected $currentAction; - + /** * Admin name * * @var string */ protected $name; - + /** * @var EventDispatcherInterface */ protected $eventDispatcher; - + /** * @var RequestFilterInterface */ protected $requestFilter; - + /** * Admin constructor. * @@ -112,7 +113,7 @@ public function __construct( $this->entities = new ArrayCollection(); $this->requestFilter = $requestFilter; } - + /** * Load entities and set current action according to request. * @@ -125,14 +126,14 @@ public function handleRequest(Request $request, $user = null) { // set current action $this->currentAction = $this->getAction($request->get('_route_params')['_action']); - + // check if user is logged have required permissions to get current action $this->checkPermissions($user); - + $actionConfiguration = $this ->currentAction ->getConfiguration(); - + // configure the request filter with the action and admin configured parameters $this ->requestFilter @@ -141,12 +142,12 @@ public function handleRequest(Request $request, $user = null) $actionConfiguration->getParameter('order'), $this->configuration->getParameter('max_per_page') ); - + // filter the request with the configured criteria, order and max_per_page parameter $this ->requestFilter ->filter($request); - + // load entities according to action and request $this->load( $this->requestFilter->getCriteria(), @@ -155,7 +156,7 @@ public function handleRequest(Request $request, $user = null) $this->requestFilter->getCurrentPage() ); } - + /** * Check if user is allowed to be here * @@ -174,19 +175,19 @@ public function checkPermissions($user) $actionName = $this ->getCurrentAction() ->getName(); - + if (!$this->isActionGranted($actionName, $roles)) { $rolesStringArray = []; - + foreach ($roles as $role) { - + if ($role instanceof Role) { $rolesStringArray[] = $role->getRole(); } else { $rolesStringArray[] = $role; } } - + $message = sprintf('User with roles %s not allowed for action "%s"', implode(', ', $rolesStringArray), $actionName @@ -194,7 +195,7 @@ public function checkPermissions($user) throw new NotFoundHttpException($message); } } - + /** * Create and return a new entity. * @@ -206,15 +207,15 @@ public function create() $entity = $this ->dataProvider ->create(); - + // add it to the collection $this ->entities ->add($entity); - + return $entity; } - + /** * Save entity via admin manager. Error are catch, logged and a flash message is added to session * @@ -244,7 +245,7 @@ public function save() } return $success; } - + /** * Remove an entity with data provider * @@ -274,7 +275,7 @@ public function remove() } return $success; } - + /** * Generate a route for admin and action name (like lag.admin.my_admin) * @@ -299,10 +300,10 @@ public function generateRouteName($actionName) // replace admin and action name in pattern $routeName = str_replace('{admin}', Container::underscore($this->getName()), $routingPattern); $routeName = str_replace('{action}', $actionName, $routeName); - + return $routeName; } - + /** * Load entities according to the given criteria and the current action configuration. * @@ -316,16 +317,16 @@ public function load(array $criteria, array $orderBy = [], $limit = 25, $offset { $currentAction = $this->getCurrentAction(); $currentActionConfiguration = $currentAction->getConfiguration(); - + // some action, such as create, does not require the entities to be loaded if (!$currentAction->isLoadingRequired()) { return; } $pager = $currentActionConfiguration->getParameter('pager'); - + if ($currentAction->isPaginationRequired() && $pager) { $loadStrategy = $currentActionConfiguration->getParameter('load_strategy'); - + // only pagerfanta adapter is yet supported if ('pagerfanta' !== $pager) { throw new AdminException( @@ -345,10 +346,10 @@ public function load(array $criteria, array $orderBy = [], $limit = 25, $offset // load entities using a pager $entities = $this->loadPaginate($criteria, $orderBy, $limit, $offset); } else { - // load using the data provider - $entities = $this->loadWithoutPagination($criteria, $orderBy, $limit, $offset); + // load using the data provider without pagination data + $entities = $this->loadWithoutPagination($criteria, $orderBy); } - + // the data provider should return an array or a collection of entities. if (!is_array($entities) && !$entities instanceof Collection) { throw new AdminException( @@ -358,16 +359,16 @@ public function load(array $criteria, array $orderBy = [], $limit = 25, $offset $this ); } - + // if an array is provided, transform it to a collection to be more convenient if (is_array($entities)) { $entities = new ArrayCollection($entities); } - + // load the entities into the Admin $this->entities = $entities; } - + /** * Return loaded entities * @@ -377,7 +378,7 @@ public function getEntities() { return $this->entities; } - + /** * Return entity for current admin. If entity does not exist, it throws an exception. * @@ -397,7 +398,7 @@ public function getUniqueEntity() } return $this->entities->first(); } - + /** * Return admin name * @@ -407,7 +408,7 @@ public function getName() { return $this->name; } - + /** * Return true if current action is granted for user. * @@ -419,7 +420,7 @@ public function getName() public function isActionGranted($actionName, array $roles) { $isGranted = array_key_exists($actionName, $this->actions); - + // if action exists if ($isGranted) { $isGranted = false; @@ -427,7 +428,7 @@ public function isActionGranted($actionName, array $roles) $action = $this->actions[$actionName]; // checking roles permissions foreach ($roles as $role) { - + if ($role instanceof Role) { $role = $role->getRole(); } @@ -436,10 +437,10 @@ public function isActionGranted($actionName, array $roles) } } } - + return $isGranted; } - + /** * @return ActionInterface[] */ @@ -447,7 +448,7 @@ public function getActions() { return $this->actions; } - + /** * @return integer[] */ @@ -455,7 +456,7 @@ public function getActionNames() { return array_keys($this->actions); } - + /** * @param $name * @return ActionInterface @@ -468,10 +469,10 @@ public function getAction($name) "Invalid action name \"{$name}\" for admin '{$this->getName()}'. Check your configuration" ); } - + return $this->actions[$name]; } - + /** * Return if an action with specified name exists form this admin. * @@ -482,7 +483,7 @@ public function hasAction($name) { return array_key_exists($name, $this->actions); } - + /** * @param ActionInterface $action * @return void @@ -491,7 +492,7 @@ public function addAction(ActionInterface $action) { $this->actions[$action->getName()] = $action; } - + /** * Return the current action or an exception if it is not set. * @@ -506,10 +507,10 @@ public function getCurrentAction() 'Current action is null. You should initialize it (with handleRequest method for example)' ); } - + return $this->currentAction; } - + /** * Return if the current action has been initialized and set. * @@ -519,7 +520,7 @@ public function isCurrentActionDefined() { return ($this->currentAction instanceof ActionInterface); } - + /** * Return admin configuration object. * @@ -529,7 +530,7 @@ public function getConfiguration() { return $this->configuration; } - + /** * Return a translation key for a message according to the Admin's translation pattern. * @@ -544,7 +545,7 @@ protected function generateMessageTranslationKey($message) $this->name ); } - + /** * Load entities using PagerFanta. * @@ -553,7 +554,7 @@ protected function generateMessageTranslationKey($message) * @param int $limit * @param int $offset * - * @return array|\Traversable + * @return array|Traversable */ protected function loadPaginate(array $criteria, array $orderBy, $limit, $offset) { @@ -563,35 +564,24 @@ protected function loadPaginate(array $criteria, array $orderBy, $limit, $offset $this->pager = new Pagerfanta($adapter); $this->pager->setMaxPerPage($limit); $this->pager->setCurrentPage($offset); - + return $this ->pager ->getCurrentPageResults(); } - + /** * Load entities using to configured data provider. * * @param array $criteria * @param array $orderBy - * @param int $limit - * @param int $offset * * @return array|Collection */ - protected function loadWithoutPagination(array $criteria, $orderBy, $limit, $offset) + protected function loadWithoutPagination(array $criteria, $orderBy) { - $currentAction = $this->getCurrentAction(); - $currentActionConfiguration = $currentAction->getConfiguration(); - - // if the current action should retrieve only one entity, the offset should be zero - if ($currentActionConfiguration->getParameter('load_strategy') !== AdminInterface::LOAD_STRATEGY_MULTIPLE) { - $offset = 0; - $limit = 1; - } - return $this ->dataProvider - ->findBy($criteria, $orderBy, $limit, $offset); + ->findBy($criteria, $orderBy, null, null); } } From 4e3b98bc4ad88bea5b15bcf1a3aa4bf2c962915a Mon Sep 17 00:00:00 2001 From: JohnKrovitch Date: Sun, 11 Dec 2016 22:02:43 +0100 Subject: [PATCH 3/5] fix wrong method in delete template --- Resources/views/CRUD/delete.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/views/CRUD/delete.html.twig b/Resources/views/CRUD/delete.html.twig index 9d9f13bf0..bc7683e4e 100644 --- a/Resources/views/CRUD/delete.html.twig +++ b/Resources/views/CRUD/delete.html.twig @@ -6,7 +6,7 @@
- {{ 'lag.admin.deleting' | trans({'%entity%' : admin.entityLabel, '%id%': admin.uniqueEntity.id}) }} + {{ 'lag.admin.deleting' | trans({'%entity%' : admin.uniqueEntityLabel, '%id%': admin.uniqueEntity.id}) }}
{{ form_rest(form) }}
From e1bdfa55b2451bf0e50ee9dac039b17d8c4268e7 Mon Sep 17 00:00:00 2001 From: JohnKrovitch Date: Sun, 11 Dec 2016 22:47:06 +0100 Subject: [PATCH 4/5] fix error message on deletion using translation pattern --- Admin/Admin.php | 2 +- Controller/CRUDController.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Admin/Admin.php b/Admin/Admin.php index 92a26b28c..c28671a1b 100644 --- a/Admin/Admin.php +++ b/Admin/Admin.php @@ -268,7 +268,7 @@ public function remove() $this ->messageHandler ->handleError( - $this->generateMessageTranslationKey('lag.admin.deleted_errors'), + $this->generateMessageTranslationKey('deleted_errors'), "An error has occurred while deleting an entity : {$e->getMessage()}, stackTrace: {$e->getTraceAsString()} " ); $success = false; diff --git a/Controller/CRUDController.php b/Controller/CRUDController.php index eb1f932fc..4d5ea290b 100644 --- a/Controller/CRUDController.php +++ b/Controller/CRUDController.php @@ -12,6 +12,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\Security\Core\Role\Role; @@ -28,7 +29,7 @@ class CRUDController extends Controller * * @Template("LAGAdminBundle:CRUD:list.html.twig") * @param Request $request - * @return array + * @return array|Response */ public function listAction(Request $request) { From 5245bd3cb3dcab08c330bacbd36be8bfbb704f2e Mon Sep 17 00:00:00 2001 From: JohnKrovitch Date: Thu, 15 Dec 2016 13:10:46 +0100 Subject: [PATCH 5/5] improve AdminInterface command --- Admin/AdminInterface.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Admin/AdminInterface.php b/Admin/AdminInterface.php index 90274384b..d26c14b8d 100644 --- a/Admin/AdminInterface.php +++ b/Admin/AdminInterface.php @@ -5,6 +5,7 @@ use Exception; use LAG\AdminBundle\Action\ActionInterface; use LAG\AdminBundle\Admin\Configuration\AdminConfiguration; +use LAG\AdminBundle\Field\Field\Collection; use Pagerfanta\Pagerfanta; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\User\UserInterface; @@ -83,7 +84,7 @@ public function create(); public function getName(); /** - * @return mixed + * @return Collection|mixed */ public function getEntities();