Skip to content

Commit

Permalink
add View mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rabaix committed Jun 14, 2011
1 parent fe17ef9 commit 58fa7ef
Show file tree
Hide file tree
Showing 17 changed files with 649 additions and 26 deletions.
195 changes: 189 additions & 6 deletions Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
use Sonata\AdminBundle\Datagrid\DatagridMapper;

use Sonata\AdminBundle\Admin\Pool;

use Sonata\AdminBundle\Builder\FormContractorInterface;
use Sonata\AdminBundle\Builder\ListBuilderInterface;
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
use Sonata\AdminBundle\Builder\ViewBuilderInterface;

use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Model\ModelManagerInterface;
Expand All @@ -50,12 +53,27 @@ abstract class Admin implements AdminInterface, DomainObjectInterface

/**
* The list FieldDescription constructed from the $list property
* and the the configureListField method
* and the configureListField method
*
* @var array
*/
protected $listFieldDescriptions = array();

/**
* The view field definitions (quick property definition)
*
* @var array
*/
protected $view = array();

/**
* The view FieldDescription constructed from the $view property
* and the configureListField method
*
* @var array
*/
protected $viewFieldDescriptions = array();

/**
* The form field definition (quick property definition)
*
Expand Down Expand Up @@ -121,6 +139,13 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
*/
protected $formGroups = false;

/**
* The view group disposition
*
* @var array|boolean
*/
protected $viewGroups = false;

/**
* The label class name (used in the title/breadcrumb ...)
*
Expand Down Expand Up @@ -267,6 +292,13 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
*/
protected $listBuilder;

/**
* The related view builder
*
* @var \Sonata\AdminBundle\View\ViewBuilderInterface
*/
protected $viewBuilder;

/**
* The related datagrid builder
*
Expand Down Expand Up @@ -311,6 +343,8 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
'form_groups' => false,
'list_fields' => false,
'filter_fields' => false,
'view_fields' => false,
'view_groups' => false,
'routes' => false,
'side_menu' => false,
);
Expand Down Expand Up @@ -344,6 +378,15 @@ protected function configureDatagridFilters(DatagridMapper $filter)

}

/**
*
* @param DatagridMapper
*/
protected function configureViewFields(DatagridMapper $filter)
{

}

/**
* configure the Admin routes
*
Expand Down Expand Up @@ -433,6 +476,29 @@ public function postRemove($object)

}

/**
* build the view FieldDescription array
*
* @return void
*/
protected function buildViewFieldDescriptions()
{
if ($this->loaded['view_fields']) {
return;
}

$this->loaded['view_fields'] = true;

$this->viewFieldDescriptions = $this->getBaseFields($this->view);

// normalize field
foreach ($this->viewFieldDescriptions as $fieldDescription) {
$this->getViewBuilder()->fixFieldDescription($this, $fieldDescription);
}

return $this->viewFieldDescriptions;
}

/**
* build the list FieldDescription array
*
Expand Down Expand Up @@ -744,6 +810,7 @@ public function buildRoutes()
$collection->add('batch');
$collection->add('edit', $this->getRouterIdParameter().'/edit');
$collection->add('delete', $this->getRouterIdParameter().'/delete');
$collection->add('view', $this->getRouterIdParameter().'/view');

// add children urls
foreach ($this->getChildren() as $children) {
Expand Down Expand Up @@ -854,6 +921,16 @@ public function getEditTemplate()
return 'SonataAdminBundle:CRUD:edit.html.twig';
}

/**
* Returns the view template
*
* @return string the view template
*/
public function getViewTemplate()
{
return 'SonataAdminBundle:CRUD:view.html.twig';
}

/**
* Returns an instance of the related classname
*
Expand Down Expand Up @@ -968,6 +1045,33 @@ public function buildFormGroups()
}
}

/**
* build the view group array
*
* @return void
*/
public function buildViewGroups()
{
if ($this->loaded['view_groups']) {
return;
}

$this->loaded['view_groups'] = true;

if (!$this->viewGroups) {
$this->viewGroups = array(
false => array('fields' => array_keys($this->getViewFieldDescriptions()))
);
}

// normalize array
foreach ($this->viewGroups as $name => $group) {
if (!isset($this->viewGroups[$name]['collapsed'])) {
$this->viewGroups[$name]['collapsed'] = false;
}
}
}

/**
* Returns a form depend on the given $object
*
Expand Down Expand Up @@ -1131,18 +1235,20 @@ public function getMaxPerPage()
return $this->maxPerPage;
}

public function setFormGroups($formGroups)
{
$this->formGroups = $formGroups;
}

public function getFormGroups()
{
$this->buildFormGroups();

return $this->formGroups;
}

public function getViewGroups()
{
$this->buildViewGroups();

return $this->viewGroups;
}

/**
* set the parent FieldDescription
*
Expand Down Expand Up @@ -1266,6 +1372,65 @@ public function removeFormFieldDescription($name)
unset($this->formFieldDescriptions[$name]);
}

/**
* build and return the collection of form FieldDescription
*
* @return array collection of form FieldDescription
*/
public function getViewFieldDescriptions()
{
$this->buildViewFieldDescriptions();

return $this->viewFieldDescriptions;
}

/**
* Returns the form FieldDescription with the given $name
*
* @param string $name
* @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
*/
public function getViewFieldDescription($name)
{
return $this->hasViewFieldDescription($name) ? $this->viewFieldDescriptions[$name] : null;
}

/**
* Returns true if the admin has a FieldDescription with the given $name
*
* @param string $name
* @return bool
*/
public function hasViewFieldDescription($name)
{
$this->buildViewFieldDescriptions();

return array_key_exists($name, $this->viewFieldDescriptions);
}

/**
* add a FieldDescription
*
* @param string $name
* @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
* @return void
*/
public function addViewFieldDescription($name, FieldDescriptionInterface $fieldDescription)
{
$this->viewFieldDescriptions[$name] = $fieldDescription;
}

/**
* remove a FieldDescription
*
* @param string $name
* @return void
*/
public function removeViewFieldDescription($name)
{
unset($this->viewFieldDescriptions[$name]);
}

/**
* Returns the collection of list FieldDescriptions
*
Expand Down Expand Up @@ -1770,6 +1935,23 @@ public function getListBuilder()
return $this->listBuilder;
}

/**
* @param \Sonata\AdminBundle\Builder\ViewBuilderInterface $viewBuilder
* @return void
*/
public function setViewBuilder(ViewBuilderInterface $viewBuilder)
{
$this->viewBuilder = $viewBuilder;
}

/**
* @return \Sonata\AdminBundle\Builder\ViewBuilderInterface
*/
public function getViewBuilder()
{
return $this->viewBuilder;
}

/**
* @param Pool $configurationPool
* @return void
Expand Down Expand Up @@ -1854,6 +2036,7 @@ public function getSecurityInformation()
'EDIT' => array('EDIT'),
'LIST' => array('LIST'),
'CREATE' => array('CREATE'),
'VIEW' => array('VIEW'),
'DELETE' => array('DELETE'),
'OPERATOR' => array('OPERATOR')
);
Expand Down
Loading

0 comments on commit 58fa7ef

Please sign in to comment.