Permalink
Browse files

Made form buttons configurable

  • Loading branch information...
1 parent 2ed4a2a commit ebadd378648957e73651b48b78086416774c1a9f @mgiagnoni committed Jun 15, 2012
View
@@ -229,6 +229,11 @@ public function getRoles()
return $this->roles;
}
+ public function hasIdParam()
+ {
+ return false !== strpos($this->routePattern, '{id}');
+ }
+
public function __toString()
{
return $this->name;
@@ -196,5 +196,12 @@ function setRoles($roles);
* @return array
*/
function getRoles();
+
+ /**
+ * Checks if action route pattern has an object id param.
+ *
+ * @return boolean
+ */
+ function hasIdParam();
}
@@ -79,6 +79,8 @@ private function addActionsSection(ArrayNodeDefinition $node)
'column' => null,
'order' => null
),
+ 'icon' => 'arrowthick-1-w',
+ 'text' => 'form.action.back',
'roles' => array()
),
'new' => array(
@@ -128,6 +130,12 @@ private function addActionsSection(ArrayNodeDefinition $node)
'action' => null
),
'roles' => array()
+ ),
+ 'save' => array(
+ 'icon' => 'disk',
+ 'text' => 'form.button.submit',
+ 'trans_domain' => 'LyraAdminBundle',
+ 'roles' => array()
)
);
@@ -327,16 +335,47 @@ private function addModelListActionsSection(ArrayNodeDefinition $node)
$node
->children()
->arrayNode('object_actions')
- ->defaultValue(array('show','edit','delete'))
- ->prototype('scalar')->end()
+ ->useAttributeAskey('name')
+ ->defaultValue(array('show' => array(),'edit' => array(),'delete' => array()))
+ ->prototype('array')
+ ->children()
+ ->scalarNode('text')->end()
+ ->scalarNode('icon')->end()
+ ->scalarNode('style')->end()
+ ->end()
+ ->end()
+ ->beforeNormalization()
+ ->always()
+ ->then($this->getNormalizeActionsFunc())
+ ->end()
->end()
->arrayNode('batch_actions')
- ->defaultValue(array('delete'))
- ->prototype('scalar')->end()
+ ->useAttributeAskey('name')
+ ->defaultValue(array('delete' => array()))
+ ->prototype('array')
+ ->children()
+ ->scalarNode('text')->end()
+ ->end()
+ ->end()
+ ->beforeNormalization()
+ ->always()
+ ->then($this->getNormalizeActionsFunc())
+ ->end()
->end()
->arrayNode('list_actions')
- ->defaultValue(array('new'))
- ->prototype('scalar')->end()
+ ->useAttributeAskey('name')
+ ->defaultValue(array('new' => array()))
+ ->prototype('array')
+ ->children()
+ ->scalarNode('text')->end()
+ ->scalarNode('icon')->end()
+ ->scalarNode('style')->end()
+ ->end()
+ ->end()
+ ->beforeNormalization()
+ ->always()
+ ->then($this->getNormalizeActionsFunc())
+ ->end()
->end()
->end();
}
@@ -347,7 +386,7 @@ private function addModelFormSection(ArrayNodeDefinition $node)
->children()
->arrayNode('form')
->addDefaultsIfNotSet()
- ->children()
+ ->children()
->scalarNode('template')->cannotBeEmpty()->defaultValue('LyraAdminBundle:Form:form.html.twig')->end()
->scalarNode('class')->cannotBeEmpty()->defaultValue('Lyra\AdminBundle\Form\Type\AdminFormType')->end()
->end();
@@ -381,6 +420,21 @@ private function addModelFormNewSection(ArrayNodeDefinition $node)
->addDefaultsIfNotSet()
->children()
->scalarNode('title')->defaultValue('New')->end()
+ ->arrayNode('actions')
+ ->useAttributeAskey('name')
+ ->defaultValue(array('index' => array(), 'save' => array()))
+ ->prototype('array')
+ ->children()
+ ->scalarNode('text')->end()
+ ->scalarNode('icon')->end()
+ ->scalarNode('style')->end()
+ ->end()
+ ->end()
+ ->beforeNormalization()
+ ->always()
+ ->then($this->getNormalizeActionsFunc())
+ ->end()
+ ->end()
->end();
$this->addModelFormGroupsSection($new);
}
@@ -393,6 +447,21 @@ private function addModelFormEditSection(ArrayNodeDefinition $node)
->addDefaultsIfNotSet()
->children()
->scalarNode('title')->defaultValue('Edit')->end()
+ ->arrayNode('actions')
+ ->useAttributeAskey('name')
+ ->defaultValue(array('index' => array(), 'save' => array(), 'delete' => array()))
+ ->prototype('array')
+ ->children()
+ ->scalarNode('text')->end()
+ ->scalarNode('icon')->end()
+ ->scalarNode('style')->end()
+ ->end()
+ ->end()
+ ->beforeNormalization()
+ ->always()
+ ->then($this->getNormalizeActionsFunc())
+ ->end()
+ ->end()
->end();
$this->addModelFormGroupsSection($edit);
}
@@ -433,4 +502,21 @@ private function addModelServicesSection(ArrayNodeDefinition $node)
->end()
->end();
}
+
+ private function getNormalizeActionsFunc()
+ {
+ return function($v)
+ {
+ $norm = array();
+ foreach ($v as $key => $val) {
+ if (is_int($key) && is_string($val)) {
+ $norm[$val] = array();
+ } else {
+ $norm[$key] = $val;
+ }
+ }
+
+ return $norm;
+ };
+ }
}
@@ -104,7 +104,7 @@ private function setActionsDefaults($model)
}
foreach ($actions as $action => $attrs) {
- if (!isset($attrs['route_name'])) {
+ if (isset($attrs['route_pattern']) && !isset($attrs['route_name'])) {
$actions[$action]['route_name'] = $this->config['models'][$model]['route_prefix'].'_'.$action;
}
}
@@ -125,17 +125,20 @@ private function setRouteDefaults($model)
private function setListDefaults($model)
{
- $this->config['models'][$model]['list']['other_actions'] = array('index', 'object');
- $types = array('list_actions', 'object_actions', 'batch_actions', 'other_actions');
- foreach ($types as $type) {
- $listActions =& $this->config['models'][$model]['list'][$type];
+ $modelOpts = $this->config['models'][$model];
+ $options =& $this->config['models'][$model]['list'];
+ $options['other_actions'] = array('index' => array(), 'object' => array());
+
+ foreach (array('list_actions', 'object_actions', 'batch_actions', 'other_actions') as $type) {
+ $listActions =& $options[$type];
$actions = array();
- foreach ($listActions as $action) {
- $actions[$action] = $this->setActionOptions($action, $model);
+ foreach ($listActions as $action => $attrs) {
+ $actions[$action] = $this->setActionOptions($action, $modelOpts['actions'], $attrs);
}
$listActions = $actions;
}
- $this->config['models'][$model]['list']['trans_domain'] = $this->config['models'][$model]['trans_domain'];
+
+ $options['trans_domain'] = $modelOpts['trans_domain'];
}
private function setColumnsDefaults($model)
@@ -178,10 +181,17 @@ private function setFormDefaults($model)
}
$options['groups'] = $groups;
+
+ foreach (array('new', 'edit') as $key) {
+ foreach ($options[$key]['actions'] as $action => $attrs) {
+ $options[$key]['actions'][$action] = $this->setActionOptions($action, $modelOpts['actions'], $attrs);
+ }
+ }
+
$actions = array();
- foreach (array('new', 'edit', 'index') as $action) {
- $actions[$action] = $this->setActionOptions($action, $model);
+ foreach (array('new', 'edit') as $action) {
+ $actions[$action] = $this->setActionOptions($action, $modelOpts['actions']);
}
$options['actions'] = $actions;
@@ -194,24 +204,23 @@ private function setFilterDefaults($model)
$options =& $this->config['models'][$model]['filter'];
$actions = array();
foreach (array('filter', 'index') as $action) {
- $actions[$action] = $this->setActionOptions($action, $model);
+ $actions[$action] = $this->setActionOptions($action, $this->config['models'][$model]['actions']);
}
$options['actions'] = $actions;
$options['trans_domain'] = $this->config['models'][$model]['trans_domain'];
}
- private function setActionOptions($action, $model)
+ private function setActionOptions($action, $actions, $curOpts = array())
{
- $actions = $this->config['models'][$model]['actions'];
-
$options['name'] = $action;
$keys = array('route_name', 'route_pattern', 'route_params', 'text', 'icon', 'style', 'dialog', 'trans_domain', 'template', 'roles');
foreach ($keys as $key) {
- if (!isset($actions[$action][$key])) {
- continue;
+ if (isset($curOpts[$key])) {
+ $options[$key] = $curOpts[$key];
+ } elseif (isset($actions[$action][$key])) {
+ $options[$key] = $actions[$action][$key];
}
- $options[$key] = $actions[$action][$key];
}
return $options;
@@ -538,6 +547,8 @@ private function createListDefinition($model, $options, ContainerBuilder $contai
private function createFormDefinition($model, $options, ContainerBuilder $container)
{
$this->createCollectionDefinition($model, 'form_actions', $options['actions'], $container);
+ $this->createCollectionDefinition($model, 'form_new_actions', $options['new']['actions'], $container);
+ $this->createCollectionDefinition($model, 'form_edit_actions', $options['edit']['actions'], $container);
$container->setDefinition(sprintf('lyra_admin.%s.form', $model), new DefinitionDecorator('lyra_admin.form.abstract'))
->addMethodCall('setModelName', array($model))
@@ -547,7 +558,10 @@ private function createFormDefinition($model, $options, ContainerBuilder $contai
->addMethodCall('setClass', array($options['class']))
->addMethodCall('setDataClass', array($options['data_class']))
->addMethodCall('setGroups', array($options['groups']))
- ->addMethodCall('setActions', array(new Reference(sprintf('lyra_admin.%s.form_actions.collection', $model))));
+ ->addMethodCall('setActions', array(new Reference(sprintf('lyra_admin.%s.form_actions.collection', $model))))
+ ->addMethodCall('setNewActions', array(new Reference(sprintf('lyra_admin.%s.form_new_actions.collection', $model))))
+ ->addMethodCall('setEditActions', array(new Reference(sprintf('lyra_admin.%s.form_edit_actions.collection', $model))))
+ ;
}
private function createFilterDefinition($model, $options, ContainerBuilder $container)
View
@@ -90,6 +90,19 @@ class Form implements FormInterface
*/
protected $actions;
+ /**
+ * @var \Lyra\AdminBundle\Action\ActionCollectionInterface
+ */
+ protected $newActions;
+
+ /**
+ * @var \Lyra\AdminBundle\Action\ActionCollectionInterface
+ */
+ protected $editActions;
+
+ /**
+ * @var mixed
+ */
protected $data;
/**
@@ -120,6 +133,26 @@ public function getActions()
return $this->actions;
}
+ public function setNewActions(ActionCollectionInterface $actions)
+ {
+ $this->newActions = $actions;
+ }
+
+ public function getNewActions()
+ {
+ return $this->newActions;
+ }
+
+ public function setEditActions(ActionCollectionInterface $actions)
+ {
+ $this->editActions = $actions;
+ }
+
+ public function getEditActions()
+ {
+ return $this->editActions;
+ }
+
public function setAction($actionName)
{
if (!$this->actions->has($actionName)) {
@@ -32,6 +32,8 @@ function getModelName();
/**
* Sets form actions.
*
+ * These are the actions executed when the form is submitted (new, edit).
+ *
* @param \Lyra\AdminBundle\Action\ActionCollectionInterface $actions
*/
function setActions(ActionCollectionInterface $actions);
@@ -44,6 +46,40 @@ function setActions(ActionCollectionInterface $actions);
function getActions();
/**
+ * Sets new form actions.
+ *
+ * These are the actions users execute through buttons
+ * at the top of a new form (default:index, save).
+ *
+ * @param \Lyra\AdminBundle\Action\ActionCollectionInterface $actions
+ */
+ function setNewActions(ActionCollectionInterface $actions);
+
+ /**
+ * Gets new form actions.
+ *
+ * @return \Lyra\AdminBundle\Action\ActionCollectionInterface
+ */
+ function getNewActions();
+
+ /**
+ * Sets edit form actions.
+ *
+ * These are the actions users execute through buttons
+ * at the top of an edit form (default:index, save, delete).
+ *
+ * @param \Lyra\AdminBundle\Action\ActionCollectionInterface $actions
+ */
+ function setEditActions(ActionCollectionInterface $actions);
+
+ /**
+ * Gets edit form actions.
+ *
+ * @return \Lyra\AdminBundle\Action\ActionCollectionInterface
+ */
+ function getEditActions();
+
+ /**
* Sets form action.
*
* This is the action executed when the form is submitted.
Oops, something went wrong.

0 comments on commit ebadd37

Please sign in to comment.