symfony2admingenerator/AdmingeneratorGeneratorBundle — pull request #508 by loostro (2013-06-15 23:06:37+00:00)
Title: Custom object actions stub generator

@sescandell @CayetanoSoriano @cedriclombardot This is ready to merge and tested, but before I merge this I'd like you to have a look at the code (in case I missed something).

In applications you often have many custom actions. For example, you can have an online shop with a customer ordering something. Your order will probably have multiple custom actions like:

* confirm -> confirms you received the order
* send -> set "is sent" to true
* cancel -> cancels order

To quickly manage many actions I've introduced a new builder called `custom` which main goal is to generate STUBs for custom actions.

Example config:
```yaml
generator: admingenerator.generator.doctrine
params:
model: Acme\ShopBundle\Entity\Order
namespace_prefix: Acme
bundle_name: ShopBundle
fields: ~
object_actions:
confirm:
label: Confirm
icon: icon-ok
route: Acme_ShopBundle_Order_custom
csrfProtected: true
params:
title: "Are you sure you want to confirm this order?"
pk: "{{ Order.id }}"
action: confirm
cancel:
label: Cancel
icon: icon-remove
route: Acme_ShopBundle_Order_custom
csrfProtected: true
params:
title: "Are you sure you want to cancel this order?"
pk: "{{ Order.id }}"
action: cancel

builders:
list:
params:
title: title.list
display: [ id, username, email, lastLogin, enabled, roles, groups ]
actions:
new: ~
# object_actions: ~
# removed object actions from here, so that list builder uses general params
batch_actions:
delete: ~

custom:
params:
# object_actions: ~
# removed object actions from here, so that list builder uses general params
```

Then you need to edit the custom controller in `Acme/ShopBundle/Controller/Order/CustomController.php` and customize `executeConfirm` and `executeCancel` functions like that:

```php
get('doctrine')->getManager();
$Order->setConfirmed(true);
$em->persist($Order);
$em->flush();
}

public function executeCancel($Order)
{
$em = $this->get('doctrine')->getManager();
$Order->setCanceled(true);
$em->persist($Order);
$em->flush();
}
}
```

And... thats all! It works!