views:

239

answers:

3

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,
Alex

Update


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, 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.

+1  A: 

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.

richsage
I can't use `batch_actions`, because the project's written in Symfony 1.0 and they aren't available. I'll try using `action` and see if I can get it to work.
Alex Ciminian
Ahh, unlucky :-( and no change of upgrading it I presume :-) I'll have a think/look but I started on 1.2 so I may be a while ;-)
richsage
+1  A: 

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.

Raise
Yep, I've done that, but it still isn't exactly what I need. The problem is that I need to send the filtered list to the export action and I haven't figured out a way to send parameters (the ids) using just the .yml. Currently I'm working around this using javascript, but I wish I weren't :).
Alex Ciminian
Ah, I missed the filter criteria. Yes, I'm afraid you can't do that without JS in sf1.0.
Raise
A: 

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

Alex Ciminian