Kunstmaan / KunstmaanBundlesStandardEdition

The "Kunstmaan Bundles Standard Edition" distribution
http://bundles.kunstmaan.be
MIT License
89 stars 39 forks source link

BulkDelete ListAction for AdminListBundle - [IMPLEMENTED!-SOLUTION FOUND] #31

Closed kabasakalis closed 8 years ago

kabasakalis commented 10 years ago

Any plans to implement BulkDelete ListAction ?.Thanks in advance

kabasakalis commented 10 years ago

capture

OK,so I did some digging into the code and managed to implement this.I 'll give a brief outline in case someone is interested in.I am following this example: http://bundles.kunstmaan.be/getting-started/creating-an-adminlist

First step,in src/Sandbox/WebsiteBundle/AdminList/EmployeeAdminListConfigurator.php ,in buildFields() we include the id property with a checkbox template and add buildListActions() and canBulkDelete() functions as follows...

    /**
     * Configure the visible columns
     */
    public function buildFields()
    {
         $this->addField('id','Select',false,'SandboxWebsiteBundle:AdminList\Employee:check.html.twig'); 
         $this->addField('firstName', 'First name', true);
        $this->addField('lastName', 'Last name', true);
        $this->addField('twitterHandle', 'Twitter handle', true);
        $this->addField('picture', 'Picture', false, 'SandboxWebsiteBundle:AdminList\Employee:picture.html.twig');
    }

      /**
     * Configure if it's possible to bulkdelete items
     *
     * @return bool
     */
    public function canBulkDelete()
    {
        return true;
    }

    public function buildListActions(){
        $this->addListAction(new BulkDeleteListAction);
    }

\src\Sandbox\WebsiteBundle\Resources\views\AdminList\Employee\check.html.twig is simply

  <input class="select_item" type="checkbox" id="{{object}}">

Next we declare a custom ListAction.We create a new folder src\Sandbox\WebsiteBundle\AdminList\ListAction and in it we create a new BulkDeleteListAction class

 <?php

namespace Sandbox\WebsiteBundle\AdminList\ListAction;
use Kunstmaan\AdminListBundle\AdminList\ListAction\ListActionInterface;

class BulkDeleteListAction implements ListActionInterface {

    /**
     * @return array
     */
    public function getUrl()
    {
        return array(
            'path'      => 'sandboxwebsitebundle_admin_employee_bulkdelete'
        );
    }

    /**
     * @return string
     */
    public function getLabel()
    {
        return 'Delete Selected';
    }

    /**
     * @return null
     */
    public function getIcon()
    {
        return null;
    }

    /**
     * @return null
     */
    public function getTemplate()
    {
        return 'SandboxWebsiteBundle:AdminList\Employee:bulkdelete.html.twig';
    }

}

The template src\Sandbox\WebsiteBundle\Resources\views\AdminList\Employee\bulkdelete.html.twig is

 <script type="text/javascript">

    var selected_ids = new Array();
    $(".select_item").bind("click", function (e) {
        selected_ids=[];
        $(".select_item:checked").each(function () {
            var id=$(this).attr('id')
            console.log('id',id);
            if($.inArray(id, selected_ids) == -1){
            selected_ids.push(id);
            }
        });
        var ids = JSON.stringify(selected_ids);
        console.log('ids',ids);
        $('#ids').attr('value',selected_ids);
        //console.log('HIDDEN',$('#ids').attr('value'));
    });

</script>

{% if adminlist.configurator.canBulkDelete() %}
   <button  data-toggle="modal" data-target="#sure-modal-bulkdelete" class="btn-danger" type="submit"><i class="icon-remove-sign"></i> {{action.getLabel()}}</button>

{% endif %}

    <div id="sure-modal-bulkdelete" class="modal hide fade">
        <form action="{{ path(action.getUrl()["path"]) }}" method="POST">
            <div class="modal-header">
                <button class="close" data-dismiss="modal">&times;</button>
                <h3>Are you sure you want to delete these  items?</h3>
            </div>
            <div class="modal-footer">
                <input type="hidden" id="ids" name="ids"  >
                <div class="btn_group">
                    <button type="submit" class="btn btn-danger" value="delete" name="delete" >{{ 'form.delete' | trans }}</button>
                    <button class="btn" data-dismiss="modal">Cancel</button>
                </div>
            </div>
        </form>
    </div>

the javascript aggregates selected row ids and passes them as a comma delimited string in a hidden input of a form, that is submitted to the controller action after the Delete Selected button is pressed (and after the modal dialog )

Last,the controller action sk\src\Sandbox\WebsiteBundle\Controller\EmployeeAdminListController.php

  /**
     * The bulkdelete action
     *
     * @param string $ids
     *
     * @Route("/bulkdelete", name="sandboxwebsitebundle_admin_employee_bulkdelete")
     * @Method({ "POST"})
     *
     * @return array
     */

    public function bulkdeleteAction()
    {

        $request = $this->get('request');
        $configurator=$this->getAdminListConfigurator();
        $ids_string = $request->request->get('ids');
        $ids=explode(',',$ids_string);

        $repository = $this->getDoctrine()->getRepository('SandboxWebsiteBundle:Employee');
        $indexUrl = $configurator->getIndexUrl();
        if ('POST' == $request->getMethod()) {
            $qb= $repository->createQueryBuilder('e');
            $qb ->delete('Sandbox\WebsiteBundle\Entity\Employee','e');
            $qb->where($qb->expr()->in('e.id',':ids'));
            $qb ->setParameter(':ids',$ids);
            $q = $qb->getQuery();
            $results = $q->execute();
            if($ids_string=='')
                $this->get('session')->getFlashBag()->add('info','No Employees were selected.');else
            $this->get('session')->getFlashBag()->add('success',count($ids).' Employees have  been deleted!');
        }
        return new RedirectResponse($this->generateUrl($indexUrl['path'], isset($indexUrl['params']) ? $indexUrl['params'] : array()));
    }

That's it! I think this should be a core feature. Cheers,hope this helps someone.

wimvds commented 10 years ago

:+1: Thanks!

kabasakalis commented 10 years ago

Thank you,Kunstmaan CMS is amazing!

roderik commented 10 years ago

@drumaddict care to submit this guide as a pull request in https://github.com/Kunstmaan/KunstmaanBundlesDocs ? This way we can add it to the advanced section on the bundles site: http://bundles.kunstmaan.be/getting-started/advanced

jockri commented 8 years ago

Bulk actions were added to the CMS about a year ago. Closing this issue...