Developing a component frontend update function/First try at csv export
From Joomla! Documentation
Articles in this Series
20 First try at csv file export
A browser in general is used for interactively retrieving and updating specific data. In many cases it is useful to import or export bulk data for offline processing. This chapter of the tutorial present a first example of exporting data in csv format.
Joomla is tuned for user interaction by means of a browser, so primarily uses html format. An exported data file, however, can be in any format, and typically can not be assumed to be html. This step in the tutorial therefore takes a rather blunt approach in inhibiting Joomla to embed the exported data in html. This is just a shortcut for this chapter, and a more sound approach is presented in the next one.
Just like in the previous steps of this tutorial, the functionality has been implemented both in the backend and the frontend, with (almost) equal code. The next chapter therefore only describes the backend files, but the description is equally valid for the frontend files.
20.2 The changes
Again, these files have been updated to reflect version info only.
This step extends the item list page with a button to start the export of the selected records (items) as a csv file for download. The 'view' method is therefore extended with the code to create that button.
The 'view' method only enables functions for which the user has permission. This is checked with the 'canDo→get()' methods. The export button is enabled if the user has edit permission ('core-edit').
The model has been extended with a method to retrieve a selected set of record items in one go in stead of fetching one at a time. Each records is stored in an array where each element contains one field. The record arrays in turn are collected in another array, so we have in fact a two dimensional array: $content.
The first row of $content contains the names of the fields. For simplicity of this example, only the fields id, uid, greeting and category are exported.
For large amounts of data a two dimensional array can become inefficient, both in memory usage and performance, and a more elaborate implementation may be needed. That however is not a Joomla specific issue, and for this tutorial we just keep it at this.
This is the tricky part. The normal data processing of Joomla consist of retrieving data, preparing it for display and then rendering the data in the proper format. These steps are typically implemented in files like controllers/helloworld.php, a 'view method' in helloworld/view.html.php and a layout template in helloworld/tmpl/default.php. In case of an export there is no display, so the last two steps are omitted.
So all the work of compiling data into an export file can be performed in the controller. To be fair, also preparing data for a specific export file layout constitutes an amount of formatting, so arguably that part of the processing could be deferred to a separate 'view' method. This might even be architecturally sounder and easier to implement. For this tutorial however we take the brute force approach of doing all the work in a controller.
As data collection for an export is a different task than collection for display, a separate controller 'helloexport.php' is defined in the 'controllers' directory. It performs a couple of steps: first it retrieves the data for the export from the view parameters ($pks) and database ($content array of records). Then with the 'foreach' loop it converts $content to lines for the csv file, and exports those lines with the 'print' statement.
Next, the controller transforms the 'print' data into a downloadable file by specifying a content type header. The concerning code starts with the comment '// write the header ...' .
For more background information see for example stackoverflow: joomla force file download csv export .
Although the data export is now complete, there is still one issue to tackle. Joomla by default expects all output to refer to a section of a web page, so embeds output with html for the other web page sections, like title, standard menus etc. Without further action this extra html code will be written to the downloaded export file when the 'exportcsv' function finishes and returns to Joomla. For this tutorial we prevent this by bluntly forcing the application to close, thereby inhibiting further processing of the output data by Joomla. This is the code after comment '// Close the application ...' .
This approach works well but is rather clumsy from an architectural point of view. Then next step in this tutorial will offer a cleaner approach.
20.3 Source files for this step
20.4 Testing the features of this step
- Open the overview list page of all message items. You can do so either using the backend by login in as administrator, or using the frontend by logging in as user with sufficient permissions to perform the export.
- Select the message items that you want to export.
- Perform the export and download the selected items by clicking Export csv.
- Inspect the downloaded file. It will contain the fields id, uid, greeting and category of the selected items.