-
-
Save weyandch/9782538 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
services: | |
majax.portal.site_list_builder: | |
class: Majax\PortalBundle\Builder\SiteListBuilder | |
arguments: [ @doctrine ] | |
majax.portal.admin.site: | |
class: Majax\PortalBundle\Admin\SiteAdmin | |
tags: | |
- { name: sonata.admin, manager_type: orm, group: sites, label: site } | |
arguments: [null, Majax\PortalBundle\Entity\Site, MajaxPortalBundle:SiteAdmin] | |
majax.portal.admin.user: | |
class: Majax\PortalBundle\Admin\UserAdmin | |
tags: | |
- { name: sonata.admin, manager_type: orm, group: users, label: user } | |
arguments: [null, Majax\PortalBundle\Entity\User, MajaxPortalBundle:UserAdmin] | |
calls: | |
- [ setSiteListBuilder, [ @majax.portal.site_list_builder ] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Majax\PortalBundle\Builder; | |
use Symfony\Bundle\DoctrineBundle\Registry; | |
class SiteListBuilder { | |
/** @var \Symfony\Bundle\DoctrineBundle\Registry */ | |
private $orm; | |
public function __construct($orm) | |
{ | |
$this->orm = $orm; | |
} | |
public function getList() | |
{ | |
$repo = $this->orm->getRepository('MajaxPortalBundle:Site'); | |
$items = $repo->findAll(); | |
$ret = array(); | |
foreach($items as $item) | |
{ | |
$ret[$item->getId()] = $item->getName(); | |
} | |
return $ret; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Majax\PortalBundle\Admin; | |
use Majax\PortalBundle\Builder\SiteListBuilder; | |
use Sonata\AdminBundle\Admin\Admin; | |
use Sonata\AdminBundle\Form\FormMapper; | |
use Sonata\AdminBundle\Datagrid\DatagridMapper; | |
use Sonata\AdminBundle\Datagrid\ListMapper; | |
use Sonata\AdminBundle\Show\ShowMapper; | |
class UserAdmin extends Admin | |
{ | |
/** @var \Majax\PortalBundle\Builder\SiteListBuilder */ | |
private $site_list_builder; | |
public function setSiteListBuilder(SiteListBuilder $slb) | |
{ | |
$this->site_list_builder = $slb; | |
} | |
public function configureShowFields(ShowMapper $showMapper) | |
{ | |
$showMapper | |
->add('username') | |
->add('email') | |
; | |
} | |
public function configureFormFields(FormMapper $formMapper) | |
{ | |
$formMapper | |
->with('General') | |
->add('username') | |
->add('email') | |
->end() | |
; | |
} | |
public function configureListFields(ListMapper $listMapper) | |
{ | |
$listMapper | |
->addIdentifier('username') | |
->add('email') | |
->add('lastLogin') | |
; | |
} | |
public function configureDatagridFilters(DatagridMapper $datagridMapper) | |
{ | |
$datagridMapper | |
->add('username') | |
->add('email') | |
->add('applied_sites', 'callback', | |
array( | |
'filter_field_options' => array('choices' => $this->site_list_builder->getList(), 'expanded' => false, 'multiple' => true), | |
'filter_options' => array( | |
'filter' => array($this, 'handleSitesFilter'), | |
'type' => 'choice', | |
'multiple' => 'true', | |
) | |
) | |
) | |
; | |
} | |
public function handleSitesFilter($queryBuilder, $alias, $field, $value) | |
{ | |
if ($value == null) | |
return; | |
$queryBuilder->leftJoin('o.sites', 's'); | |
$queryBuilder->andWhere($queryBuilder->expr()->in(sprintf('%s.%s', 's', 'id'), $value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment