From 98f033c7228ee73cfd201407f5c43935d955fc55 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 5 Mar 2015 22:45:11 +0100 Subject: [PATCH 1/2] Added click-to-toggle for boolean columns --- Controller/AdminController.php | 45 ++++++++++++++++++++++- Resources/public/stylesheet/admin.css | 10 ++++-- Resources/views/list.html.twig | 51 +++++++++++++++++++++++++-- 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/Controller/AdminController.php b/Controller/AdminController.php index 51fa4e02ef..0112153497 100644 --- a/Controller/AdminController.php +++ b/Controller/AdminController.php @@ -20,6 +20,7 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\ClassMetadataInfo; use Symfony\Component\Form\Form; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -33,7 +34,7 @@ */ class AdminController extends Controller { - protected $allowedActions = array('list', 'edit', 'new', 'show', 'search', 'delete'); + protected $allowedActions = array('list', 'edit', 'new', 'show', 'search', 'delete', 'toggle'); protected $config; protected $entity = array(); @@ -267,6 +268,48 @@ protected function searchAction() )); } + protected function toggleAction() + { + if (!$entity = $this->em->getRepository($this->entity['class'])->find($this->request->query->get('id'))) { + throw new \Exception('The entity does not exist.'); + } + + $propertyName = $this->request->query->get('property'); + + // get the current property value + $getter = 'get'.ucfirst($propertyName); + $isser = 'is'.ucfirst($propertyName); + + if (method_exists($entity, $getter)) { + $value = $entity->{$getter}(); + } elseif (method_exists($entity, $isser)) { + $value = $entity->{$isser}(); + } elseif (property_exists($entity, $propertyName)) { + $value = $entity->{$propertyName}; + } else { + throw new \Exception(sprintf('It\'s not possible to get the current value of the "%s" boolean property of the "%s" entity.', $propertyName, $this->entity['name'])); + } + + // toggle the property value + $newValue = !$value; + $setter = 'set'.ucfirst($propertyName); + $isser = 'setIs'.ucfirst($propertyName); + + if (method_exists($entity, $setter)) { + $entity->{$setter}($newValue); + } elseif (method_exists($entity, $isser)) { + $entity->{$isser}($newValue); + } elseif (property_exists($entity, $propertyName)) { + $entity->{$propertyName} = $newValue; + } else { + throw new \Exception(sprintf('It\'s not possible to toggle the value of the "%s" boolean property of the "%s" entity.', $propertyName, $this->entity['name'])); + } + + $this->em->flush(); + + return new JsonResponse(array('success' => true)); + } + /** * Allows applications to modify the entity associated with the item being * edited before persisting it. diff --git a/Resources/public/stylesheet/admin.css b/Resources/public/stylesheet/admin.css index 6a021a1d77..03002d848c 100644 --- a/Resources/public/stylesheet/admin.css +++ b/Resources/public/stylesheet/admin.css @@ -202,17 +202,21 @@ ul.pager .next { /* Labels ------------------------------------------------------------------------- */ -span.label { +.label { background: #222; color: #FFF; font-size: 11px; padding: 2px 4px; text-transform: uppercase; } -span.label-success { +.label-success { background: #00994D; } -span.label-danger { +.label-warning { + background: #FFFF61; + color: #222; +} +.label-danger { background: #D44542; } diff --git a/Resources/views/list.html.twig b/Resources/views/list.html.twig index 697d45a641..55fb6cbfab 100644 --- a/Resources/views/list.html.twig +++ b/Resources/views/list.html.twig @@ -49,7 +49,7 @@ {% for field, metadata in fields %} {% set isSortingField = metadata.property == app.request.get('sortField') %} - + {% if isSortingField %} {% set sortDirection = ('DESC' == app.request.get('sortDirection')) ? 'ASC' : 'DESC' %} {% set request_attributes = request_attributes|merge({ sortField: metadata.property }) %} @@ -91,7 +91,7 @@ {% for item in paginator.currentPageResults %} - + {% for field, metadata in fields %} {% set isSortingField = metadata.property == app.request.get('sortField') %} @@ -124,3 +124,50 @@ {% endblock %} + +{% block body_javascript %} + {{ parent() }} + + +{% endblock %} From 73081275bd8e696de879680e3edf7413116a379c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 6 Mar 2015 22:00:40 +0100 Subject: [PATCH 2/2] Use the bootstrap-toggle library to handle click-to-toggle booleans --- Controller/AdminController.php | 6 +- .../public/javascript/bootstrap-toggle.min.js | 8 +++ Resources/public/stylesheet/admin.css | 39 +++++++++++ .../stylesheet/bootstrap-toggle.min.css | 28 ++++++++ Resources/views/list.html.twig | 70 +++++++++---------- Twig/EasyAdminTwigExtension.php | 5 +- 6 files changed, 116 insertions(+), 40 deletions(-) create mode 100644 Resources/public/javascript/bootstrap-toggle.min.js create mode 100644 Resources/public/stylesheet/bootstrap-toggle.min.css diff --git a/Controller/AdminController.php b/Controller/AdminController.php index 0112153497..1a51afa4a7 100644 --- a/Controller/AdminController.php +++ b/Controller/AdminController.php @@ -275,6 +275,10 @@ protected function toggleAction() } $propertyName = $this->request->query->get('property'); + if (!isset($this->entity['properties'][$propertyName]) + || 'boolean' != $this->entity['properties'][$propertyName]['type']) { + throw new \Exception(sprintf('The "%s" property is not boolean.', $propertyName)); + } // get the current property value $getter = 'get'.ucfirst($propertyName); @@ -307,7 +311,7 @@ protected function toggleAction() $this->em->flush(); - return new JsonResponse(array('success' => true)); + return new Response(true === $newValue ? 'on' : 'off'); } /** diff --git a/Resources/public/javascript/bootstrap-toggle.min.js b/Resources/public/javascript/bootstrap-toggle.min.js new file mode 100644 index 0000000000..5bed0ff7e2 --- /dev/null +++ b/Resources/public/javascript/bootstrap-toggle.min.js @@ -0,0 +1,8 @@ +/*! ======================================================================== + * Bootstrap Toggle: bootstrap-toggle.js v2.2.0 + * http://www.bootstraptoggle.com + * ======================================================================== + * Copyright 2014 Min Hur, The New York Times Company + * Licensed under MIT + * ======================================================================== */ ++function(a){"use strict";function b(b){return this.each(function(){var d=a(this),e=d.data("bs.toggle"),f="object"==typeof b&&b;e||d.data("bs.toggle",e=new c(this,f)),"string"==typeof b&&e[b]&&e[b]()})}var c=function(b,c){this.$element=a(b),this.options=a.extend({},this.defaults(),c),this.render()};c.VERSION="2.2.0",c.DEFAULTS={on:"On",off:"Off",onstyle:"primary",offstyle:"default",size:"normal",style:"",width:null,height:null},c.prototype.defaults=function(){return{on:this.$element.attr("data-on")||c.DEFAULTS.on,off:this.$element.attr("data-off")||c.DEFAULTS.off,onstyle:this.$element.attr("data-onstyle")||c.DEFAULTS.onstyle,offstyle:this.$element.attr("data-offstyle")||c.DEFAULTS.offstyle,size:this.$element.attr("data-size")||c.DEFAULTS.size,style:this.$element.attr("data-style")||c.DEFAULTS.style,width:this.$element.attr("data-width")||c.DEFAULTS.width,height:this.$element.attr("data-height")||c.DEFAULTS.height}},c.prototype.render=function(){this._onstyle="btn-"+this.options.onstyle,this._offstyle="btn-"+this.options.offstyle;var b="large"===this.options.size?"btn-lg":"small"===this.options.size?"btn-sm":"mini"===this.options.size?"btn-xs":"",c=a('