Getting batch_action functionality in Symfony 1.0
I'm currently working on a web app written in Symfony. I'm supposed to add an "export to CSV" feature in the backend/administration part of the app for some modules. In the list view, there should be an "Export" button which should provide the user with a csv file of the elements that are displayed (considering filtering criteria).
I've created a method in the actions class of the module that takes a comma separated list of ids and generates the CSV, but I'm not really sure how to add the link to it in the view. The problem is that the view doesn't exist anywhere, it's generated on the fly from the data in the generator.yml
configuration file. I've posted the relevant part of the file below.
I'm new to Symfony, so any help would be appreciated :).
Thanks,
AlexUpdate
list:
display: [id, =name, indemn, _status, _participants, _approved_, created_at]
title: Lista acţiuni
object_actions:
_edit: ~
_delete: ~
actions:
_create: ~
export_csv:
name: Export to CSV
action: CSVExport
params: id=csvActionSubmit
filters: [name, county_id, _status_filter, activity_id]
fields:
id:
name: Nr. crt.
...
Thanks to your advice,开发者_JAVA百科 I've managed to add a button that is linked to my action. The problem is that I also need to send some parameters to the action, because I may not want all the elements - filters may have been used. Unfortunately, the project is using Symfony 1.0, which doesn't support batch_actions
.
Currently, I'm working around this with Javascript (I parse the DOM to get the numeric ids (from the display table) and then build the link for the button. I really think there could be a better way for this.
If you're looking to keep it within the admin generator, then what you're looking for is a "batch action" (see the Symfony docs for details). Basically this will appear in the dropdown menu (if you're using the default admin theme), and will let you tick the rows you're interested in, select the option from the dropdown menu and click 'Go'.
Your generator.yml would look something like the following:
list:
display: [=name, indemn, _status, _participants, _approved_, created_at]
title: Lista acţiuni
object_actions:
_edit: ~
_delete: ~
batch_actions:
exportcsv:
label: "Export to CSV
...
and this will look for an executeBatchExportcsv()
method in your actions.class.php. It passes your method an array of ids in the sfWebRequest
object, so you should be able to slot it in pretty easily to your existing code.
As you were mentioning, add this to your list
params:
actions:
exportToCsv:
name: Export to CSV
action: exportToCsv
create a executeExportToCsv()
action in the module's action.class.php file
, and you're done. If you want a nice icon for the button, you can add:
icon: /images/icons/page_excel.png
or somesuch to the action params.
I've managed to achieve what I wanted in the end.
The problem with the Javascript solution was that I could only export the displayed results on the page. If pagination was needed, only the displayed results (the current page) was exported.
I added the following to the layout template:
<?php
if (isset($filters['csv-export-link'])) {
echo '<a href="'$filters['csv-export-link']).'">Export to CSV</a>';
}
?>
And I modified the already overriden addFiltersCriteria
:
public function addFiltersCriteria($c) {
// [...]
parent::addFiltersCriteria($c);
if (isset($this->filters['csv-export'])) {
$idList = array();
$results = ActionnPeer::doSelect($c);
foreach ($results as $result) {
$idList[] = $result->getId();
}
$this->filters['csv-export-link'] = '[...]/CSVExport/idList/'.implode(',', $idList);
}
}
Last, but not least :), I removed the "general" action as it is not needed anymore and added the csv-export "filter" to the generator.yml.
filters: [..., _csv_export_filter]
The filter template had the following contents:
<input type="hidden" name="filters[csv-export]" value="true" /> Active
This solution is almost reusable :), no to mention that it actually works as intended. There is little overhead in adapting it for other modules.
Thank you all for your help.
Cheers!
Alex
精彩评论