Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Empty entity support, override views
Browse files Browse the repository at this point in the history
Now, you can list an empty entity and override all views more easier.
See /README.md for more information.
  • Loading branch information
hugo082 committed Feb 22, 2017
1 parent a6a7335 commit ad63429
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 74 deletions.
62 changes: 33 additions & 29 deletions Controller/ManageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class ManageController extends Controller
public function indexAction()
{
$entities = $this->container->getParameter( 'db_manager.entities' );
return $this->render('DBManagerBundle:Manage:index.html.twig', array(
$settings = $this->container->getParameter('db_manager.views');
return $this->render($settings['indexView'], array(
'entities' => $entities
));
}
Expand All @@ -20,14 +21,14 @@ public function listAction(Request $request, $name)
{
$array = $this->container->getParameter( 'db_manager.entities' );
$settings = $this->container->getParameter('db_manager.views');
$einfo = $this->get('db.manager.checker')->getEntity($array, $name);
$eInfo = $this->get('db.manager.checker')->getEntity($array, $name);

$e = new $einfo['fullPath']();
$all = $this->getEntity($einfo);
$e = new $eInfo['fullPath']();
$all = $this->getEntity($eInfo);

$form = NULL;
if ($settings['list']['add'] && $einfo['permission']['add']) {
$form = $this->createForm($einfo['fullFormType'], $e);
if ($settings['list']['add'] && $eInfo['permission']['add']) {
$form = $this->createForm($eInfo['fullFormType'], $e);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
Expand All @@ -39,26 +40,27 @@ public function listAction(Request $request, $name)
$form = $form->createView();
}

return $this->render('DBManagerBundle:Manage:entity.html.twig', array(
return $this->render($eInfo['mainView'], array(
'name' => $name,
'einfo' => $einfo,
'eInfo' => $eInfo,
'all' => $all,
'form' => $form,
'action' => 'list'
'action' => array( 'name' => 'list', 'formType' => 'add'),
'settings' => $settings
));
}

public function addAction(Request $request, $name)
{
$array = $this->container->getParameter( 'db_manager.entities' );
$settings = $this->container->getParameter('db_manager.views');
$einfo = $this->get('db.manager.checker')->getEntity($array, $name);
$eInfo = $this->get('db.manager.checker')->getEntity($array, $name);

if ($settings['list']['add'] || !$einfo['permission']['add'])
if ($settings['list']['add'] || !$eInfo['permission']['add'])
return $this->redirectToRoute('db.manager.list', array('name' => $name));

$e = new $einfo['fullPath']();
$form = $this->createForm($einfo['fullFormType'], $e);
$e = new $eInfo['fullPath']();
$form = $this->createForm($eInfo['fullFormType'], $e);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
Expand All @@ -68,29 +70,30 @@ public function addAction(Request $request, $name)
return $this->redirectToRoute('db.manager.list', array('name' => $name));
}

return $this->render('DBManagerBundle:Manage:entity.html.twig', array(
return $this->render($eInfo['mainView'], array(
'name' => $name,
'einfo' => $einfo,
'eInfo' => $eInfo,
'all' => NULL,
'form' => $form->createView(),
'action' => 'add'
'action' => array( 'name' => 'add', 'formType' => 'add'),
'settings' => $settings
));
}

public function editAction(Request $request, $name, $id)
{
$array = $this->container->getParameter('db_manager.entities');
$settings = $this->container->getParameter('db_manager.views');
$einfo = $this->get('db.manager.checker')->getEntity($array, $name);
$this->get('db.manager.checker')->edit($einfo);
$eInfo = $this->get('db.manager.checker')->getEntity($array, $name);
$this->get('db.manager.checker')->edit($eInfo);

$all = ($settings['edit']['list']) ? $this->getEntity($einfo) : NULL;
$e = $this->getEntity($einfo, $id);
$all = ($settings['edit']['list']) ? $this->getEntity($eInfo) : NULL;
$e = $this->getEntity($eInfo, $id);
if (!$e) {
$this->addFlash('danger','Entity not found');
return $this->redirectToRoute('db.manager.list', array('name' => $name));
}
$form = $this->createForm($einfo['fullFormType'], $e);
$form = $this->createForm($eInfo['fullFormType'], $e);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
Expand All @@ -99,22 +102,23 @@ public function editAction(Request $request, $name, $id)
$this->addFlash('success','Your entity have updated');
return $this->redirectToRoute('db.manager.list', array('name' => $name));
}
return $this->render('DBManagerBundle:Manage:entity.html.twig', array(
return $this->render($eInfo['mainView'], array(
'name' => $name,
'einfo' => $einfo,
'eInfo' => $eInfo,
'all' => $all,
'form' => $form->createView(),
'action' => 'edit'
'action' => array( 'name' => 'edit', 'formType' => 'edit'),
'settings' => $settings
));
}

public function removeAction(Request $request, $name, $id)
{
$array = $this->container->getParameter( 'db_manager.entities' );
$einfo = $this->get('db.manager.checker')->getEntity($array, $name);
$this->get('db.manager.checker')->remove($einfo);
$eInfo = $this->get('db.manager.checker')->getEntity($array, $name);
$this->get('db.manager.checker')->remove($eInfo);

$e = $this->getEntity($einfo, $id);
$e = $this->getEntity($eInfo, $id);
if ($e) {
$em = $this->getDoctrine()->getManager();
$em->remove($e);
Expand All @@ -125,8 +129,8 @@ public function removeAction(Request $request, $name, $id)
return $this->redirectToRoute('db.manager.list', array('name' => $name));
}

private function getEntity($einfo, $id = NULL) {
$repo = $this->getDoctrine()->getRepository($einfo['bundle'].':'.$einfo['name']);
private function getEntity($eInfo, $id = NULL) {
$repo = $this->getDoctrine()->getRepository($eInfo['bundle'].':'.$eInfo['name']);
if ($id)
return $repo->find($id);
return $repo->findAll();
Expand Down
6 changes: 5 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private function addViewsSection(ArrayNodeDefinition $node)
->addDefaultsIfNotSet()
->canBeUnset()
->children()
->scalarNode('indexView')->defaultValue('DBManagerBundle:Manage:index.html.twig')->end() // Auto
->arrayNode('list')
->addDefaultsIfNotSet()
->children()
Expand All @@ -66,7 +67,7 @@ private function addEntitiesSection(ArrayNodeDefinition $node)
$node
->children()
->arrayNode('entities')
->prototype('array')
->prototype('array')->addDefaultsIfNotSet()
->children()
->scalarNode('fullName')->isRequired()->cannotBeEmpty()
->validate()
Expand All @@ -79,6 +80,9 @@ private function addEntitiesSection(ArrayNodeDefinition $node)
->scalarNode('fullPath')->end() // Auto
->scalarNode('formType')->end() // Auto
->scalarNode('fullFormType')->end() // Auto
->scalarNode('listView')->defaultValue('DBManagerBundle:Manage:list.html.twig')->end() // Auto
->scalarNode('formView')->defaultValue('DBManagerBundle:Manage:form.html.twig')->end() // Auto
->scalarNode('mainView')->defaultValue('DBManagerBundle:Manage:entity.html.twig')->end() // Auto
->arrayNode('permission')
->defaultValue($this->permissions)
->prototype('enum')
Expand Down
8 changes: 6 additions & 2 deletions DependencyInjection/DBManagerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public function load(array $configs, ContainerBuilder $container)
*/
private function loadViews(array $config, ContainerBuilder $container)
{
if (isset($config['views']))
$container->setParameter($this->getAlias().'.views', $config['views']);
$config['views']['edit']['add'] = true;
$config['views']['add']['add'] = true;
$config['views']['add']['list'] = false;
$config['views']['list']['list'] = true;

$container->setParameter($this->getAlias().'.views', $config['views']);
}

/**
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Features include:
* Remove
* Personalize interface

`v1.1` `21 FEV 17`
`v1.2` `22 FEV 17`

## Installation

Expand Down Expand Up @@ -88,14 +88,23 @@ You can configure different actions on each entity :

DisplayName:
fullName: YourBundle:RealName
listView: myFile_1.html.twig # Optional
formView: myFile_2.html.twig # Optional
mainView: myFile_3.html.twig # Optional
fullPath: YourBundle\Entity\Airport # Optional
formType: AirportEditType # Optional
fullFormType: AnotherBundle\Form\AirportEditType # Optional
permission: [ "edit" ] # Optional - add | edit | remove

By default, DBM load your entity in `YourBundle\Entity\RealName`, name the form with `RealNameType` and load your form type in
`YourBundle\Form\formType` (so `YourBundle\Form\RealNameType`)
`DisplayName` is used by DBM for display on template and in url, you can enter the same name of RealName.
- `DisplayName` is used by DBM for display on template and in url, you can enter the same name of RealName.
- `permission` is by default full authorized. This parameter is optional but recommended.
- You can call your custom views :
- `listView` is the view that list the entity.
- `formView` is the view that display the form
- `mainView` is the view that call `listview` and `formview`.


2. Configure views

Expand All @@ -109,9 +118,12 @@ set the options to `false`.

db_manager:
views:
indexView: index.html.twig
list:
add: false
edit:
list: false

If you do not specify an argument, the argument takes its default value (`true`)
You can also specify your custom index view with the option `indexView`.

6 changes: 1 addition & 5 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
Comin soon

Gérer bdd vide

template for listing in config
Comin soon
14 changes: 14 additions & 0 deletions Resources/translations/DBManagerBundle.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title: Database manager

entity:
title: Manage %name%
name: Name
listing: Listing
permissions: Permissions
empty: No data found.
action:
add: Add
edit: Edit
remove: Remove
form:
submit: Submit
14 changes: 14 additions & 0 deletions Resources/translations/DBManagerBundle.fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title: Manager de base de donnée

entity:
title: Manager %name%
name: Nom
listing: Liste
permissions: Permissions
empty: Aucune donnée trouvée.
action:
add: Ajouter
edit: Editer
remove: Supprimer
form:
submit: Soumettre
15 changes: 8 additions & 7 deletions Resources/views/Manage/entity.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends '::base.html.twig' %}
{% trans_default_domain 'DBManagerBundle' %}

{% block body %}

Expand All @@ -14,16 +15,16 @@
{% endfor %}
{% endif %}

<h1>Manage {{ name }}</h1>
<h1>{{ 'entity.title'|trans({'%name%': name}) }}</h1>

{% if all is not empty %}
{% include 'DBManagerBundle:Manage:list.html.twig' %}
{% if settings[action.name].list %}
{% include eInfo.listView %}
{% endif %}

{% if form is not empty %}
{% include 'DBManagerBundle:Manage:form.html.twig' %}
{% elseif action == 'list' and einfo.permission.add %}
<a href="{{ path('db.manager.add', {'name':name}) }}">Add</a>
{% if settings[action.name].add %}
{% include eInfo.formView %}
{% elseif action.name == 'list' and eInfo.permission.add %}
<a href="{{ path('db.manager.add', {'name':name}) }}">{{ 'entity.action.add'|trans }}</a>
{% endif %}

{% endblock %}
6 changes: 4 additions & 2 deletions Resources/views/Manage/form.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<h3>Add :</h3>
{% trans_default_domain 'DBManagerBundle' %}

<h3>{{ ('entity.action.'~action.formType)|trans }} :</h3>
{{ form_start(form) }}
{% for row in form %}
{{ form_row(row) }}
{% endfor %}
<button type="submit" name="button">Submit</button>
<button type="submit" name="button">{{ 'entity.form.submit'|trans }}</button>
{{ form_end(form) }}
15 changes: 8 additions & 7 deletions Resources/views/Manage/index.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends '::base.html.twig' %}
{% trans_default_domain 'DBManagerBundle' %}

{% block body %}

Expand All @@ -14,20 +15,20 @@
{% endfor %}
{% endif %}

<h1>Manage your database</h1>
<h1>{{ 'title'|trans }}</h1>

<h3>Listing :</h3>
<h3>{{ 'entity.listing'|trans }} :</h3>
<table>
<tr>
<th>Name</th>
<th colspan=3>Permissions</th>
<th>{{ 'entity.name'|trans }}</th>
<th colspan=3>{{ 'entity.permissions'|trans }}</th>
</tr>
{% for key, e in entities %}
<tr>
<td><a href="{{ path('db.manager.list', {'name':key})}}">{{ key }}</a></td>
<td>{{ e.permission.add ? 'Add' : '-' }}</td>
<td>{{ e.permission.edit ? 'Edit' : '-' }}</td>
<td>{{ e.permission.remove ? 'Remove' : '-' }}</td>
<td>{{ e.permission.add ? 'entity.action.add'|trans : '-' }}</td>
<td>{{ e.permission.edit ? 'entity.action.edit'|trans : '-' }}</td>
<td>{{ e.permission.remove ? 'entity.action.remove'|trans : '-' }}</td>
</tr>
{% endfor %}
</table>
Expand Down
Loading

0 comments on commit ad63429

Please sign in to comment.