This edition of the Magento Custom Development series is all about the controller. As mentioned in our previous Magento Custom Development blog post detailing adminhtml.xml reports, there is a unique controller created for the new report. You can also extend the existing Product Report controller and add the “Cost” functionality to create the controller. Either approach is acceptable. The class name of our controller is: Eyemagine_CostReport_Adminhtml_Report_Cost_ProductController.
Report controllers need to provide display and export functionality for CSV and Excel file formats. Included in the display functionality, the controller should define the page breadcrumbs and title, set the active menu and load the layout for the report.
Report Display Actions
This controller was modeled after the Mage_Adminhtml_Report_ProductController controller class, and I kept much of the functionality the same.
[cc lang="php-brief"]
public function _initAction()
{
parent::_initAction();
$this->_addBreadcrumb(
Mage::helper('reports')->__('Cost Analysis'),
Mage::helper('reports')->__('Cost Analysis')
);
return $this;
}
[/cc]
The function above is usually included in all reports. The purpose of this function is to add the Breadcrumbs to display, and if you look at the parent::_initAction() you will see that the “Reports” breadcrumb is added from the Mage_Adminhtml_Controller_Report_Abstract class. This function is not called automatically, and therefore needs to be referenced by the action function.
[cc lang="php-brief"]
public function costAction()
{
$this->_title($this->__('Reports'))
->_title($this->__('Cost Analysis'))
->_title($this->__('Products Cost'));
$this->_initAction()
->_setActiveMenu('report/costanalysis/product')
->_addBreadcrumb(
Mage::helper('reports')->__('Products Cost'),
Mage::helper('reports')->__('Products Cost')
)
->_addContent($this->getLayout()->createBlock(
'costreport/adminhtml_report_cost_product_cost'
))
->renderLayout();
}
[/cc]
After the title is set, notice how the _initAction() method is called before the active menu and final breadcrumb is set. Please be aware that the active menu must match the name and path of the menu item defined in the adminhtml.xml file. The last two actions in this function is to dynamicaly create a Block with the name “costreport/adminhtml_product_cost” and render the layout.
One of the reasons why the only the final breadcrumb is added in the costAction() is because a single report controller could handle multiple report actions. A good example is the Mage_Adminhtml_Report_ProductController which handles the Product Reports for “Ordered”, “Sold”, “Viewed”, “Low Stock” and “Downloaded”. By adding the prior level breadcrumbs in the _initAction() it simplifies the individual action functions, is easier to modify, and reduces code duplication.
I have no idea why Magento reports don’t also set the page title using the same _initAction() function. It is little inconsistencies that demonstrate how poorly planned the reports functionality was created.
The Block “costreport/adminhtml_report_cost_product_cost” name is standard class format and refers to Eyemagine_CostReport_Block_Adminhtml_Report_Cost_Product_Cost. There will be more info about this class later, but the role that it plays in the extension is as a container for the report grid.
Admin Export Actions
While viewing the report data on the screen is valuable, Magento also provides a way to export the report data to a CSV and Excel file format. For every display action, there should be two export actions; one for CSV and one for Excel. The name of the action should be in the following format: export[NAME]CsvAction() and export[NAME]ExcelAction().
Look at the function names for the Cost report displayed below:[cc lang="php-brief"]
public function exportCostCsvAction()
{
$fileName = 'product_costs.csv';
$content = $this->getLayout()
->createBlock('costreport/adminhtml_report_cost_product_cost_grid')
->getCsv();
$this->_prepareDownloadResponse($fileName, $content);
}
public function exportCostExcelAction()
{
$fileName = 'product_costs.xml';
$content = $this->getLayout()
->createBlock('costreport/adminhtml_report_cost_product_cost_grid')
->getExcel($fileName);
$this->_prepareDownloadResponse($fileName, $content);
}
[/cc]
While Magento provides two file formats, the controller actions which trigger the exports are essentially identical.
Notice that the Block loaded for the export action is different than the Block loaded for the display action. The reason for this difference is that the display action includes the container which also defines the Filter, whereas the export action references the Grid directly.
When the display action loads the report, the grid stores the current filter parameters and is able to duplicate the report data without rendering it to the screen. Then the CSV and Excel data is extracted directly from the Grid Block and downloaded as a file in the browser.
NOTE: there is a bug that prevents Excel exports from opening correctly in Excel when the export filename is too long. While this is an issue that could be resolved in Magento, the problem is actually a limitation of the Excel file format expecting a 218 max string length for file path and/or a 31 max string length for the worksheet name.
For best results try to keep the filename of the report limited to 31 characters.
Controller ACL
If the _isAllowed() function is not defined in the report controllers, then the functionality falls back to Mage_Adminhtml_Controller_Action::_isAllowed(), which always returns true. Because we are assigning unique ACL for each report action, we need the controller to check the ACL before allowing access.
[cc lang="php-brief"]
protected function _isAllowed()
{
switch ($this->getRequest()->getActionName()) {
case 'cost':
return Mage::getSingleton('admin/session')->isAllowed('report/costanalysis/product');
break;
default:
return Mage::getSingleton('admin/session')->isAllowed('report/costanalysis');
break;
}
}
[/cc]
SEE ALSO- MAGENTO CUSTOM DEVELOPMENT: ADMINHTML.XML
We hope this addition to our Magento Custom Development series has been helpful for you. Still need some assistance with report controllers? Contact one of our Magento specialists today.