Skip to content

Commit

Permalink
Add sort option
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rabaix committed Apr 15, 2011
1 parent 74086f3 commit 6509540
Show file tree
Hide file tree
Showing 17 changed files with 351 additions and 52 deletions.
42 changes: 27 additions & 15 deletions Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,23 @@ abstract class Admin implements AdminInterface
protected $translationDomain = 'AdminBundle';

/**
* options to set to the form (ie, validation_groups)
* Options to set to the form (ie, validation_groups)
*
* @var array
*/
protected $formOptions = array(
'validation_groups' => 'Default'
);

/**
* Default values to the datagrid
*
* @var array
*/
protected $datagridValues = array(
'_page' => 1,
);

/**
* The code related to the admin
*
Expand Down Expand Up @@ -323,6 +332,7 @@ protected function configureFormFields(FormMapper $form)
*/
protected function configureListFields(ListMapper $list)
{

}

/**
Expand All @@ -331,6 +341,7 @@ protected function configureListFields(ListMapper $list)
*/
protected function configureDatagridFilters(DatagridMapper $filter)
{

}

/**
Expand All @@ -349,6 +360,7 @@ public function configureSideMenu(MenuItem $menu, $action, Admin $childAdmin = n
}

/**
* @param string $code
* @param string $class
* @param string $baseControllerName
*/
Expand All @@ -361,7 +373,6 @@ public function __construct($code, $class, $baseControllerName)

public function configure()
{

$this->uniqid = uniqid();

if (!$this->classnameLabel) {
Expand Down Expand Up @@ -429,7 +440,6 @@ public function postRemove($object)
*/
protected function buildListFieldDescriptions()
{

if ($this->loaded['list_fields']) {
return;
}
Expand All @@ -445,9 +455,10 @@ protected function buildListFieldDescriptions()

if (!isset($this->listFieldDescriptions['_batch'])) {
$fieldDescription = $this->modelManager->getNewFieldDescriptionInstance($this->getClass(), 'batch', array(
'label' => 'batch',
'code' => '_batch',
'type' => 'batch',
'label' => 'batch',
'code' => '_batch',
'type' => 'batch',
'sortable' => false
));

$fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__batch.html.twig');
Expand All @@ -464,7 +475,6 @@ protected function buildListFieldDescriptions()
*/
public function buildFilterFieldDescriptions()
{

if ($this->loaded['filter_fields']) {
return;
}
Expand Down Expand Up @@ -502,7 +512,6 @@ public function getParentAssociationMapping()
*/
protected function buildFormFieldDescriptions()
{

if ($this->loaded['form_fields']) {
return;
}
Expand Down Expand Up @@ -956,7 +965,7 @@ public function getList(array $options = array())

foreach ($this->getListFieldDescriptions() as $fieldDescription) {

// do not add field already set in the configureFormField method
// do not add field already set in the configureListFields method
if ($mapper->has($fieldDescription->getFieldName())) {
continue;
}
Expand All @@ -975,19 +984,25 @@ public function getList(array $options = array())
public function getDatagrid()
{
if (!$this->datagrid) {
// retrieve the parameters
$parameters = $this->request->query->all();
// build the values array
$parameters = array_merge(
$this->getModelManager()->getDefaultSortValues($this->getClass()),
$this->datagridValues,
$this->request->query->all()
);

// always force the parent value
if ($this->isChild() && $this->getParentAssociationMapping()) {
$parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
}

// build the datagrid filter
// initialize the datagrid
$this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
$this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);

$mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);

// build the datagrid filter
$this->buildFilterFieldDescriptions();
$this->configureDatagridFilters($mapper);

Expand Down Expand Up @@ -1236,7 +1251,6 @@ public function removeFormFieldDescription($name)
*/
public function getListFieldDescriptions()
{

$this->buildListFieldDescriptions();

return $this->listFieldDescriptions;
Expand All @@ -1250,7 +1264,6 @@ public function getListFieldDescriptions()
*/
public function getListFieldDescription($name)
{

return $this->hasListFieldDescription($name) ? $this->listFieldDescriptions[$name] : null;
}

Expand Down Expand Up @@ -1298,7 +1311,6 @@ public function removeListFieldDescription($name)
*/
public function getFilterFieldDescription($name)
{

return $this->hasFilterFieldDescription($name) ? $this->filterFieldDescriptions[$name] : null;
}

Expand Down
14 changes: 14 additions & 0 deletions Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,18 @@ function getClass();
* @return void
*/
function attachAdminClass(FieldDescriptionInterface $fieldDescription);

/**
* @abstract
* @return \Sonata\AdminBundle\Datagrid\DatagridInterface
*/
function getDatagrid();

/**
* @abstract
* @param string $name
* @param array $parameters
* @return void
*/
function generateUrl($name, array $parameters = array());
}
7 changes: 6 additions & 1 deletion Builder/ORM/DatagridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\Datagrid;
use Sonata\AdminBundle\Datagrid\ORM\Pager;
use Sonata\AdminBundle\Datagrid\ORM\ProxyQuery;
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;

use Doctrine\ORM\Mapping\ClassMetadataInfo;
Expand Down Expand Up @@ -192,8 +193,12 @@ public function addFilter(DatagridInterface $datagrid, FieldDescriptionInterface
*/
public function getBaseDatagrid(AdminInterface $admin, array $values = array())
{
$queryBuilder = $admin->getModelManager()->createQuery($admin->getClass());

$query = new ProxyQuery($queryBuilder);

return new Datagrid(
$admin->getModelManager()->createQuery($admin->getClass()),
$query,
$admin->getList(),
new Pager,
$values
Expand Down
11 changes: 7 additions & 4 deletions Builder/ORM/ListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,30 @@ public function addField(ListCollection $list, FieldDescriptionInterface $fieldD
*/
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription, array $options = array())
{
if ($fieldDescription->getName() == '_action')
{
if ($fieldDescription->getName() == '_action') {
$this->buildActionFieldDescription($fieldDescription);
}

$fieldDescription->mergeOptions($options);
$fieldDescription->setAdmin($admin);

if($admin->getModelManager()->hasMetadata($admin->getClass()))
{
if($admin->getModelManager()->hasMetadata($admin->getClass())) {
$metadata = $admin->getModelManager()->getMetadata($admin->getClass());

// set the default field mapping
if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
$fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
if ($fieldDescription->getOption('sortable') !== false) {
$fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', $fieldDescription->getName()));
}
}

// set the default association mapping
if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
$fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
}

$fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC'));
}

if (!$fieldDescription->getType()) {
Expand Down
5 changes: 1 addition & 4 deletions Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\Form;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class CRUDController extends Controller
{

/**
* The related Admin class
*
* @var Admin
* @var \Sonata\AdminBundle\Admin\AdminInterface
*/
protected $admin;

Expand Down
21 changes: 20 additions & 1 deletion Datagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sonata\AdminBundle\Datagrid;

use Sonata\AdminBundle\Datagrid\PagerInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Filter\FilterInterface;

class Datagrid implements DatagridInterface
Expand All @@ -31,19 +32,25 @@ class Datagrid implements DatagridInterface

protected $bound = false;

public function __construct($query, ListCollection $columns, PagerInterface $pager, array $values = array())
protected $query;

public function __construct(ProxyQueryInterface $query, ListCollection $columns, PagerInterface $pager, array $values = array())
{
$this->pager = $pager;
$this->query = $query;
$this->values = $values;
$this->columns = $columns;
}

/**
* @return \Sonata\AdminBundle\Datagrid\PagerInterface
*/
public function getPager()
{
return $this->pager;
}


public function getResults()
{
$this->buildPager();
Expand All @@ -64,13 +71,20 @@ public function buildPager()
);
}

$this->query->setSortBy(isset($this->values['_sort_by']) ? $this->values['_sort_by'] : null);
$this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);

$this->pager->setPage(isset($this->values['_page']) ? $this->values['_page'] : 1);
$this->pager->setQuery($this->query);
$this->pager->init();

$this->bound = true;
}

/**
* @param \Sonata\AdminBundle\Filter\FilterInterface $filter
* @return \Sonata\AdminBundle\Filter\FilterInterface
*/
public function addFilter(FilterInterface $filter)
{
return $this->filters[$filter->getName()] = $filter;
Expand All @@ -90,4 +104,9 @@ public function getColumns()
{
return $this->columns;
}

public function getQuery()
{
return $this->query;
}
}
35 changes: 35 additions & 0 deletions Datagrid/DatagridInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,52 @@
interface DatagridInterface
{

/**
* @abstract
* @return \Sonata\AdminBundle\Datagrid\PagerInterface
*/
function getPager();

/**
* @abstract
* @return \Sonata\AdminBundle\Datagrid\ProxyQueryInterface
*/
function getQuery();

/**
* @abstract
* @return array
*/
function getResults();

/**
* @abstract
* @return void
*/
function buildPager();

/**
* @abstract
* @param \Sonata\AdminBundle\Filter\FilterInterface $filter
* @return \Sonata\AdminBundle\Filter\FilterInterface
*/
function addFilter(FilterInterface $filter);

/**
* @abstract
* @return array
*/
function getFilters();

/**
* @abstract
* @return array
*/
function getValues();

/**
* @abstract
* @return array
*/
function getColumns();
}
1 change: 0 additions & 1 deletion Datagrid/ListCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
namespace Sonata\AdminBundle\Datagrid;


use Sonata\AdminBundle\Admin\FieldDescriptionInterface;

class ListCollection
Expand Down
2 changes: 1 addition & 1 deletion Datagrid/ListMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function add($name, array $fieldDescriptionOptions = array())

$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$field->getKey(),
$name,
$fieldDescriptionOptions
);

Expand Down
Loading

0 comments on commit 6509540

Please sign in to comment.