J3.x

Developing a component frontend update function/Excel xlsx export: Difference between revisions

From Joomla! Documentation

Skyhigh (talk | contribs)
Excel xlsx export
Skyhigh (talk | contribs)
Excel xlsx export
Line 2: Line 2:
__NOTOC__
__NOTOC__


== 22 Excel export using PHPExcel ==
== 21 Improved csv export ==


=== 22.1 Description ===
=== 21.1 Description ===


As an example of a more elaborate export feature the HelloWorld version of the previous step has been enhanced with an option to download the data from the database as an xlsx Excel file. For the conversion the PHPExcel packages is used. The current [https://github.com/PHPOffice/PHPExcel PHPExcel] version can be [https://github.com/PHPOffice/PHPExcel/archive/1.8.zip downloaded here] .
As explained in the previous chapters, Joomla usually displays data in html format. However, it does support other formats as well, including the 'raw' format meaning that in that case Joomla does not perform any layout operations at all for the data.


=== 22.2 Install PHPExcel ===
That is exactly the way to go for downloading an export file as generally we have to assume that such file just contains 'binary' data.


The installation of PHPExcel is quite straightforward as explained in the [https://github.com/PHPOffice/PHPExcel/wiki PHPExcel wiki] .The download for the current version 1.8.1 consists of a zip archive, which contains a directory tree 'PHPExcel-1-8-1' that looks like:
=== 21.2 The changes ===
Classes/
 
  PHPExcel/
==== 21.2.1 Files: ====
    ...
* README.md
  PHPExcel.php
* helloworld.xml
Documentation/
* admin/button/index.html
Examples/
 
Again, have been updated to reflect version info. A new directory admin/button is created in this chapter, and therefore an additional'' admin/button/index.html ''placeholder file as well. ''helloworld.xml'' is updated accordingly.
 
==== 21.2.2 admin/button/rawformat.php ====
 
In Joomla processing of a page begins with starting the concerning controller. To indicate that processing should yield raw data rather than an html page, the controller must receive the'' 'format=raw' ''input parameter. These parameter is typically provided by adding an hidden input tag to the form that starts the controller. Like:
<form action="index.php?option=com_helloworld" method="post" name="adminForm">
  ...
  ...
We only need the Classes directory. Copy that directory to a suitable location in the web branch on your web server, for instance to the 'lib' directory in that branch. So if the root of your server web branch $_SERVER['DOCUMENT_ROOT'] is /var/www/html, then the Classes directory should be copied to /var/www/html/lib/Classes, making it look like:
<input type="hidden" name="format" value="raw"/>
  /var/www/html/
</form>
  administrator/
The HelloWorld database items are handled from the HelloWorld admin page with form:
  ...
  admin/components/com_helloworld/views/helloworlds/tmpl/default.php
  components/
or its frontend counterpart. The available functions like create new item, delete an item or update an item are offered to the user as buttons above this form. Our export button is here as well.
  ...
  lib/
    Classes/
      PHPExcel/
        ...
      PHPExcel.php
  libraries/
  ...
Obviously this list just shows a few directories that should be familiar to you after studying the Joomla tutorials.


=== 22.3 The changes ===
Pushing a button typically navigates to another web form, like the edit form to update a particular item. These forms are in html format, so including the 'format=raw' input tag permanently to the above form definition file tmpl/default.php would cause a conflict for these functions.


==== 22.3.1 Files: ====
So only pushing our export button should submit the 'format=raw' tag. As this tag is not a standard part of the form, the button function will add the tag on the fly as post variable. Joomla has no standard button function that allows this, so we define an additional button called'' 'RawFormat'.'' It is defined in ''admin/button/rawformat.php'' and basically just clones the Joomla 'Standard' button replacing the reference to the 'Joomla.submitbutton' javascript by a reference to the 'RawFormatSubmitbutton' javascript. This javascript submits (posts) the form data to the server, and this will schedule the controller.
* README.md
 
* helloworld.xml
==== 21.2.3 admin/views/helloworlds/submitbutton.js ====
 
This javascript injects the tag <tt>'&lt;input type="hidden" name="format" value="raw"/&gt;'</tt> in the form. Thereafter it submits the form using the'' 'Joomla.submitform(task)' ''call, just like the Joomla 'Standard' button would do. The input tag is reset to html format after this call for future form submissions.


Again, these files have been updated to reflect version info.
==== 21.2.4 admin/views/helloworlds/view.html.php ====


==== 22.3.2 admin/views/helloworlds/view.html.php ====
This php file prepares the display of the HelloWorld management page. It is adapted to include the above javascript 21.2.3 and display our new 'RawFormat' export button in stead of the previous one.


This view has been extended to show an additional button on the 'HelloWorld' administrator page. This button is to start the Excel export for the selected records (items). It works in the same way as the button for the csv export like explained in the previous section of this tutorial.
==== 21.2.5 admin/controllers/helloexport.raw.php ====


==== 22.3.3 admin/controllers/helloexport.raw.php ====
This is almost the same file as the previous 'admin/controllers/helloexport.php' except that its name has been changed in view of Joomla rules. Joomla expects a controller for raw format to have a filename &lt;controller&gt;.raw.php, although the class name is still &lt;component&gt;Controller&lt;controller&gt;, so 'HelloWorldControllerHelloExport' in this case.


Pushing the button results in a call to the 'exportxls()' function of this controller. It operates similarly to the exportcsv() function. The first steps are the same, up to and including retrieving the records from the database into the $content array.
The big difference with the previous controller version is that the application does not need to be forcibly closed as Joomla now understands from the 'format=raw' tag that the controller yields a raw format layout and embedding in html is not required anymore.


The remainder of the function embeds the data in an Excel layout, just like the data in the previous example was embedded in a csv layout. This is described in the PHPExcel documentation, and this tutorial is loosely based on the example 01simple-download-xlsx.php in the PHPExpress 'Examples' directory.
The final statements
$app->sendHeaders();
$app->close();
therefore have been removed as calling these functions will now be performed by Joomla in the standard way when the controller finishes the 'exportcsv' function.


Finally note that an include statement is added to the start of this controller:
=== 21.3 Source files for this step ===
require_once $_SERVER['DOCUMENT_ROOT'].'/lib/Classes/PHPExcel.php';
* [https://github.com/Hy-Fly/Joomla-Hello-Front-End/archive/v1-21.zip Download Joomla-Hello-Front-End v1-21]
This statement loads the PHPExcel base class from PHPExcel.php , and assumes that you indeed installed PHPExcel in the lib/Classes directory on your webserver.
* [https://github.com/Hy-Fly/Joomla-Hello-Front-End/commit/ef3fa05c04b68320f90658f264fba16bdf386777 View Joomla-Hello-Front-End v1-21]


=== 22.4 Source files for this step ===
=== 21.4 Testing the features of this step ===
* [https://github.com/Hy-Fly/Joomla-Hello-Front-End/archive/v1-22.zip Download Joomla-Hello-Front-End v1-22]
* Perform the same test as in the previous section 20.4 , and verify that the export works as it did before.
* [https://github.com/Hy-Fly/Joomla-Hello-Front-End/commit/08aab1a17111e4529f142b086ebc6265f68af680 View Joomla-Hello-Front-End v1-22]


<noinclude><div class="row">
<noinclude><div class="row">
<div class="large-6 columns">{{Basic button|J3.x:Developing_a_component_frontend_update_function/Improved_export_function|Prev: Improved export function|class=expand success}}</div>
<div class="large-6 columns">{{Basic button|J3.x:Developing_a_component_frontend_update_function/First_try_at_csv_export|Prev: First try at csv export|class=expand success}}</div>
<div class="large-6 columns">{{Basic button|J3.x:Developing_a_component_frontend_update_function/csv_and_xlsx_import|Next: csv and xlsx import|class=expand}}</div>
<div class="large-6 columns">{{Basic button|J3.x:Developing_a_component_frontend_update_function/Excel_xlsx_export|Next: Excel xlsx export|class=expand}}</div>
</div></noinclude>
</div></noinclude>

Revision as of 17:32, 22 April 2016

Joomla! 
3.x
Tutorial
Developing a component frontend update function


21 Improved csv export

21.1 Description

As explained in the previous chapters, Joomla usually displays data in html format. However, it does support other formats as well, including the 'raw' format meaning that in that case Joomla does not perform any layout operations at all for the data.

That is exactly the way to go for downloading an export file as generally we have to assume that such file just contains 'binary' data.

21.2 The changes

21.2.1 Files:

  • README.md
  • helloworld.xml
  • admin/button/index.html

Again, have been updated to reflect version info. A new directory admin/button is created in this chapter, and therefore an additional admin/button/index.html placeholder file as well. helloworld.xml is updated accordingly.

21.2.2 admin/button/rawformat.php

In Joomla processing of a page begins with starting the concerning controller. To indicate that processing should yield raw data rather than an html page, the controller must receive the 'format=raw' input parameter. These parameter is typically provided by adding an hidden input tag to the form that starts the controller. Like:

<form action="index.php?option=com_helloworld" method="post" name="adminForm">
...
<input type="hidden" name="format" value="raw"/>
</form>

The HelloWorld database items are handled from the HelloWorld admin page with form:

admin/components/com_helloworld/views/helloworlds/tmpl/default.php

or its frontend counterpart. The available functions like create new item, delete an item or update an item are offered to the user as buttons above this form. Our export button is here as well.

Pushing a button typically navigates to another web form, like the edit form to update a particular item. These forms are in html format, so including the 'format=raw' input tag permanently to the above form definition file tmpl/default.php would cause a conflict for these functions.

So only pushing our export button should submit the 'format=raw' tag. As this tag is not a standard part of the form, the button function will add the tag on the fly as post variable. Joomla has no standard button function that allows this, so we define an additional button called 'RawFormat'. It is defined in admin/button/rawformat.php and basically just clones the Joomla 'Standard' button replacing the reference to the 'Joomla.submitbutton' javascript by a reference to the 'RawFormatSubmitbutton' javascript. This javascript submits (posts) the form data to the server, and this will schedule the controller.

21.2.3 admin/views/helloworlds/submitbutton.js

This javascript injects the tag '<input type="hidden" name="format" value="raw"/>' in the form. Thereafter it submits the form using the 'Joomla.submitform(task)' call, just like the Joomla 'Standard' button would do. The input tag is reset to html format after this call for future form submissions.

21.2.4 admin/views/helloworlds/view.html.php

This php file prepares the display of the HelloWorld management page. It is adapted to include the above javascript 21.2.3 and display our new 'RawFormat' export button in stead of the previous one.

21.2.5 admin/controllers/helloexport.raw.php

This is almost the same file as the previous 'admin/controllers/helloexport.php' except that its name has been changed in view of Joomla rules. Joomla expects a controller for raw format to have a filename <controller>.raw.php, although the class name is still <component>Controller<controller>, so 'HelloWorldControllerHelloExport' in this case.

The big difference with the previous controller version is that the application does not need to be forcibly closed as Joomla now understands from the 'format=raw' tag that the controller yields a raw format layout and embedding in html is not required anymore.

The final statements

$app->sendHeaders();
$app->close();

therefore have been removed as calling these functions will now be performed by Joomla in the standard way when the controller finishes the 'exportcsv' function.

21.3 Source files for this step

21.4 Testing the features of this step

  • Perform the same test as in the previous section 20.4 , and verify that the export works as it did before.