Skip to content

Commit

Permalink
Refactor the show action, rename view => show, fix bug and missing im…
Browse files Browse the repository at this point in the history
…plementation
  • Loading branch information
Thomas Rabaix committed Aug 3, 2011
1 parent 2753079 commit 6b2e10a
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 295 deletions.
307 changes: 117 additions & 190 deletions Admin/Admin.php

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,9 @@ function getModelManager();

/**
*
* @param mixed $object
* @param array $options
* @return \Symfony\Component\Form\FormBuilder the form builder
*/
function getFormBuilder($object = null, $options = array());
function getFormBuilder();

/**
* @abstract
Expand Down
27 changes: 26 additions & 1 deletion Admin/FieldDescriptionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Sonata\AdminBundle\Admin\FieldDescriptionInterface;

class FieldDescriptionCollection
class FieldDescriptionCollection implements \ArrayAccess, \Countable
{
protected $elements = array();

Expand Down Expand Up @@ -66,4 +66,29 @@ public function remove($name)
unset($this->elements[$name]);
}
}

public function offsetExists($offset)
{
return $this->has($offset);
}

public function offsetGet($offset)
{
return $this->get($offset);
}

public function offsetSet($offset, $value)
{
throw new \RunTimeException('Cannot set value, use add');
}

public function offsetUnset($offset)
{
$this->remove($offset);
}

public function count()
{
return count($this->elements);
}
}
4 changes: 2 additions & 2 deletions Builder/ORM/DatagridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

class DatagridBuilder implements DatagridBuilderInterface
{

protected $formFactory;

/**
Expand Down Expand Up @@ -169,7 +168,7 @@ public function addFilter(DatagridInterface $datagrid, FieldDescriptionInterface
return false;
}

switch($fieldDescription->getType()) {
switch($fieldDescription->getMappingType()) {
case ClassMetadataInfo::MANY_TO_ONE:
$options = $fieldDescription->getOption('filter_field_options');
$filter = new \Sonata\AdminBundle\Filter\ORM\IntegerFilter($fieldDescription);
Expand All @@ -180,6 +179,7 @@ public function addFilter(DatagridInterface $datagrid, FieldDescriptionInterface
$options = $fieldDescription->getOption('filter_field_options');
$options['choices'] = $this->getChoices($fieldDescription);


$fieldDescription->setOption('filter_field_options', $options);

$filter = new \Sonata\AdminBundle\Filter\ORM\ChoiceFilter($fieldDescription);
Expand Down
3 changes: 2 additions & 1 deletion Builder/ORM/FormContractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ protected function getOneToOneFieldOptions($type, FieldDescriptionInterface $fie
{
$options = array();

$options['data_class'] = $fieldDescription->getTargetEntity();

if (!$fieldDescription->hasAssociationAdmin()) {
return $options;
}
Expand All @@ -65,7 +67,6 @@ protected function getOneToOneFieldOptions($type, FieldDescriptionInterface $fie
}

$options['class'] = $fieldDescription->getTargetEntity();
$options['data_class'] = $fieldDescription->getTargetEntity();
$options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();

if ($fieldDescription->getOption('edit') == 'list') {
Expand Down
30 changes: 22 additions & 8 deletions Builder/ORM/ViewBuilder.php → Builder/ORM/ShowBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
use Sonata\AdminBundle\Builder\ViewBuilderInterface;
use Sonata\AdminBundle\Builder\ShowBuilderInterface;

use Doctrine\ORM\Mapping\ClassMetadataInfo;

class ViewBuilder implements ViewBuilderInterface
class ShowBuilder implements ShowBuilderInterface
{
public function getBaseList(array $options = array())
{
Expand All @@ -28,7 +28,21 @@ public function getBaseList(array $options = array())

public function addField(FieldDescriptionCollection $list, FieldDescriptionInterface $fieldDescription)
{
return $list->add($fieldDescription);
if (!$fieldDescription->getType()) {
return false;
}

switch($fieldDescription->getMappingType()) {
case ClassMetadataInfo::MANY_TO_ONE:
case ClassMetadataInfo::MANY_TO_MANY:
case ClassMetadataInfo::ONE_TO_MANY:
case ClassMetadataInfo::ONE_TO_ONE:
// todo
return;
default:
$list->add($fieldDescription);
}

}

/**
Expand Down Expand Up @@ -67,22 +81,22 @@ public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInter

if (!$fieldDescription->getTemplate()) {

$fieldDescription->setTemplate(sprintf('SonataAdminBundle:CRUD:view_%s.html.twig', $fieldDescription->getType()));
$fieldDescription->setTemplate(sprintf('SonataAdminBundle:CRUD:show_%s.html.twig', $fieldDescription->getType()));

if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_ONE) {
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:view_orm_many_to_one.html.twig');
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:show_orm_many_to_one.html.twig');
}

if ($fieldDescription->getType() == ClassMetadataInfo::ONE_TO_ONE) {
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:view_orm_one_to_one.html.twig');
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:show_orm_one_to_one.html.twig');
}

if ($fieldDescription->getType() == ClassMetadataInfo::ONE_TO_MANY) {
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:view_orm_one_to_many.html.twig');
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:show_orm_one_to_many.html.twig');
}

if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_MANY) {
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:view_orm_many_to_many.html.twig');
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:show_orm_many_to_many.html.twig');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;

interface ViewBuilderInterface
interface ShowBuilderInterface
{
/**
* @abstract
Expand Down
15 changes: 10 additions & 5 deletions Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ public function editAction($id)

$this->admin->setSubject($object);

$form = $this->admin->getForm($object);
$form = $this->admin->getForm();
$form->setData($object);

if ($this->get('request')->getMethod() == 'POST') {
$form->bindRequest($this->get('request'));
Expand Down Expand Up @@ -345,9 +346,9 @@ public function createAction()
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function viewAction($id)
public function showAction($id)
{
if (false === $this->admin->isGranted('VIEW')) {
if (false === $this->admin->isGranted('SHOW')) {
throw new AccessDeniedException();
}

Expand All @@ -359,9 +360,13 @@ public function viewAction($id)

$this->admin->setSubject($object);

return $this->render($this->admin->getViewTemplate(), array(
'action' => 'view',
// build the show list
$elements = $this->admin->getShow();

return $this->render($this->admin->getShowTemplate(), array(
'action' => 'show',
'object' => $object,
'elements' => $this->admin->getShow(),
'admin' => $this->admin,
'base_template' => $this->getBaseTemplate(),
));
Expand Down
1 change: 1 addition & 0 deletions Datagrid/DatagridMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function add($name, array $fieldDescriptionOptions = array())
$fieldDescription->mergeOptions($fieldDescriptionOptions);

} else if (is_string($name) && !$this->admin->hasFormFieldDescription($name)) {

$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name,
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/AddDependencyCallsCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public function applyDefaults(Definition $definition, array $attributes = array(
$definition->addMethodCall('setFormContractor', array(new Reference(sprintf('sonata.admin.builder.%s_form', $manager_type))));
}

if (!$definition->hasMethodCall('setViewBuilder')) {
$definition->addMethodCall('setViewBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_view', $manager_type))));
if (!$definition->hasMethodCall('setShowBuilder')) {
$definition->addMethodCall('setShowBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_show', $manager_type))));
}

if (!$definition->hasMethodCall('setListBuilder')) {
Expand Down
3 changes: 1 addition & 2 deletions Resources/config/doctrine_orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

<service id="sonata.admin.builder.orm_list" class="Sonata\AdminBundle\Builder\ORM\ListBuilder" />

<service id="sonata.admin.builder.orm_view" class="Sonata\AdminBundle\Builder\ORM\ViewBuilder" />
<service id="sonata.admin.builder.orm_show" class="Sonata\AdminBundle\Builder\ORM\ShowBuilder" />

<service id="sonata.admin.builder.orm_datagrid" class="Sonata\AdminBundle\Builder\ORM\DatagridBuilder">
<argument type="service" id="form.factory" />
</service>

</services>

</container>
8 changes: 2 additions & 6 deletions Resources/views/CRUD/base_edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ file that was distributed with this source code.
{% block actions %}
<div class="sonata-actions">
<ul>
{% if admin.id(object) and admin.isGranted('VIEW') and admin.getViewFieldDescriptions is not empty %}
<li class="sonata-action-element"><a href="{{ admin.generateUrl('view', {'id' : admin.id(object)}) }}">{% trans from 'SonataAdminBundle' %}link_action_view{% endtrans %}</a></li>
{% if admin.id(object) and admin.isGranted('VIEW') and admin.show|length > 0 %}
<li class="sonata-action-element"><a href="{{ admin.generateUrl('show', {'id' : admin.id(object)}) }}">{% trans from 'SonataAdminBundle' %}link_action_show{% endtrans %}</a></li>
{% endif %}
{% if admin.isGranted('CREATE')%}
<li class="sonata-action-element"><a href="{{ admin.generateUrl('create') }}">{% trans from 'SonataAdminBundle' %}link_action_create{% endtrans %}</a></li>
Expand Down Expand Up @@ -58,13 +58,9 @@ file that was distributed with this source code.
<div class="sonata-ba-collapsed-fields">
{% endif %}


{% for field_name in form_group.fields %}
{% if admin.formfielddescriptions[field_name] is defined %}
{#{{ admin.formfielddescriptions[field_name]|render_form_element(form, object) }}#}

{{ form_row(form[field_name])}}

{% endif %}
{% endfor %}

Expand Down
4 changes: 2 additions & 2 deletions Resources/views/CRUD/base_list_field.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ file that was distributed with this source code.
#}

<td class="sonata-ba-list-field sonata-ba-list-field-{{ field_description.type }}" objectId="{{ admin.id(object) }}">
{% if field_description.options.identifier is defined and admin.isGranted(['EDIT', 'VIEW']) %}
{% if field_description.options.identifier is defined and admin.isGranted(['EDIT', 'SHOW']) %}

{% if admin.isGranted('EDIT') %}
<a href="{{ admin.generateUrl('edit', {'id': admin.id(object)}) }}">
{% else %}
<a href="{{ admin.generateUrl('view', {'id': admin.id(object)}) }}">
<a href="{{ admin.generateUrl('show', {'id': admin.id(object)}) }}">
{% endif %}

{% block field %}{{ value }}{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ file that was distributed with this source code.

{% block side_menu %}{{ admin.sidemenu(action).render|raw }}{% endblock %}

{% block view %}
{% block show %}
<div class="sonata-ba-view">
{% for name, view_group in admin.viewgroups %}
<table>
Expand All @@ -41,8 +41,8 @@ file that was distributed with this source code.

{% for field_name in view_group.fields %}
<tr class="sonata-ba-view-container">
{% if admin.viewfielddescriptions[field_name] is defined %}
{{ admin.viewfielddescriptions[field_name]|render_view_element(object) }}
{% if admin.show[field_name] is defined %}
{{ admin.show[field_name]|render_view_element(object) }}
{% endif %}
</tr>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/CRUD/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ file that was distributed with this source code.
#}

{% extends 'SonataAdminBundle:CRUD:base_list.html.twig' %}
{% extends 'SonataAdminBundle:CRUD:base_list.html.twig' %}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ file that was distributed with this source code.
#}

{% extends 'SonataAdminBundle:CRUD:base_view.html.twig' %}
{% extends 'SonataAdminBundle:CRUD:base_show.html.twig' %}

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ file that was distributed with this source code.
#}

{% extends 'SonataAdminBundle:CRUD:base_view_field.html.twig' %}
{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}

{% block value %}{{ value|date('F j, Y') }}{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ file that was distributed with this source code.
#}

{% extends 'SonataAdminBundle:CRUD:base_view_field.html.twig' %}
{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}

{% block value %}{{ value|date }}{% endblock %}

6 changes: 3 additions & 3 deletions Resources/views/standard_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ file that was distributed with this source code.
{# initialize block value #}
{% set preview = block('preview') %}
{% set form = block('form') %}
{% set view = block('view') %}
{% set show = block('show') %}
{% set list_table = block('list_table') %}
{% set list_filters = block('list_filters') %}
{% set side_menu = block('side_menu') %}
Expand Down Expand Up @@ -126,8 +126,8 @@ file that was distributed with this source code.
<div class="sonata-ba-content">{{ content|raw }}</div>
{% endif %}

{% if view is not empty %}
<div class="sonata-ba-view">{{ view|raw }}</div>
{% if show is not empty %}
<div class="sonata-ba-show">{{ show|raw }}</div>
{% endif %}

{% if form is not empty %}
Expand Down
4 changes: 2 additions & 2 deletions Security/Acl/Permission/AdminPermissionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
class AdminPermissionMap implements PermissionMapInterface
{
const PERMISSION_VIEW = 'VIEW';
const PERMISSION_SHOW = 'SHOW';
const PERMISSION_EDIT = 'EDIT';
const PERMISSION_CREATE = 'CREATE';
const PERMISSION_DELETE = 'DELETE';
Expand All @@ -38,7 +38,7 @@ class AdminPermissionMap implements PermissionMapInterface
MaskBuilder::MASK_LIST
),

self::PERMISSION_VIEW => array(
self::PERMISSION_SHOW => array(
MaskBuilder::MASK_VIEW,
),

Expand Down
6 changes: 3 additions & 3 deletions Show/ShowMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* file that was distributed with this source code.
*
*/
namespace Sonata\AdminBundle\Datagrid;
namespace Sonata\AdminBundle\Show;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
Expand Down Expand Up @@ -48,7 +48,7 @@ public function add($name, array $fieldDescriptionOptions = array())
$fieldDescription = $name;
$fieldDescription->mergeOptions($fieldDescriptionOptions);

} else if (is_string($name) && !$this->admin->hasListFieldDescription($name)) {
} else if (is_string($name) && !$this->admin->hasShowFieldDescription($name)) {

$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
Expand All @@ -57,7 +57,7 @@ public function add($name, array $fieldDescriptionOptions = array())
);

$this->showBuilder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
$this->admin->addListFieldDescription($name, $fieldDescription);
$this->admin->addShowFieldDescription($name, $fieldDescription);

} else if (is_string($name) && $this->admin->hasShowFieldDescription($name)) {
$fieldDescription = $this->admin->getShowFieldDescription($name);
Expand Down
Loading

0 comments on commit 6b2e10a

Please sign in to comment.