So I've been handed a custom administrator module which simply displays database table data in an HTML table. My task is to create a clickable button on the module that will generate the csv data on the fly and force the download of the same content as an excel document.
Andrew Eddie recommends these solutions, but I don't know how/where to implement them so that:
- I can authenticate that the downloading user is, in fact, the same logged in user with permission (I don't want someone to be able to learn the url and be able to directly access this private data. I assume a token is the best tool for this)
- I can enjoy the Joomla query builder methods to gather the necessary data
- I can avoid displaying any other administrator (menu dom) content with my generated data
It could divert to a different static method like this:
if (JFactory::getApplication()->input->get->get('export', 0, 'int'))
{
mod[modname]Helper::exportToExcel();
}
else
{
mod[modname]Helper::displayAsHTML();
}
to access:
public static function exportToExcel()
{
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=Some_Report_" . JFactory::getDate()->format('Y_m_d_H_i_s') . ".csv"); // @ https://docs.joomla.org/How_to_use_JDate it looks like someone got a bit spastic with the percent signs (bookmarked for upcoming Pizza, Bugs, & Fun)
header("Pragma: no-cache");
header("Expires: 0");
// get my db data and echo it as csv data
// Close the application gracefully.
JFactory::getApplication()->close();
}
But in doing so, my csv data is appended to the earlier loaded content -- no good. I wasn't able to ob_
the other content out; and I don't even know if this is a good approach to begin with.
The custom administrator module that I am looking at is as basic as it gets with files like:
- mod_[modname].php
- helper.php (previous dev does all processing and displaying from here)
- tmpl/default.php (previous dev didn't output anything from here)
My plan is to use javascript to append a button after the module's title:
<h3 class="module-title nav-header">[the module's title] [add my button here]</h3>
What I don't know is: Where do I park my new code and how should I craft the destination url to access it?
I'd like to learn how to write this custom code with the correct architecture and best practices in mind for my own education. I'm hoping someone is going to blow my mind with some elegant Joomla magic.
ob_end_clean();
to the top of your function?