J3.x

Developing a component frontend update function/csv and xlsx import

From Joomla! Documentation

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


23 csv and xlsx import[edit]

23.1 Description[edit]

If your Joomla application requires updates with a substantial amount of data, it may be more convenient to have the user collect the data offline and upload it in a file than to require the data to be entered interactively. Fortunately, it is quite easy to upload files using html and php facilities, but it does require carefully designed code to avoid upload problems or security issues. See for instance w3schools.com or php.net .

File upload is based on an html form that includes an <input type=”file”> tag. This <input> tag is displayed as a button, which pops up a file selection dialog when clicked. The data from the file will be posted to the server when the form is submitted, much like data that a user has entered by filling in an <input type="text"> text field.

The upload functionality is implemented by means of the following classes:

  • first, an upload button is defined that is attached to a form with the <input type=”file”> tag. When this button is clicked, a file selection dialog is displayed and the user can submit the file for upload.The button is defined in 'admin/button/importfile.php' .
  • The uploaded file is received as post data by controller 'admin/controllers/helloimport.php' .
  • The data is stored in the database by model class 'admin/models/helloimport.php' .

23.2 The changes[edit]

23.2.1 Files:[edit]

  • README.md
  • helloworld.xml

Again, these files have been updated to reflect version info.

23.2.2 admin/button/importfile.php[edit]

This file defines a button class of type 'importFile' which inserts a form with the <input type=”file”> button in the view presented to the user.

23.2.3 admin/views/helloworlds/view.html.php[edit]

This view is updated to include the above button.

23.2.4 admin/controllers/helloimport.php[edit]

This controller receives the uploaded data. It has two main entrypoints: importcsv() for processing an uploaded csv file, and importxls() for an uploaded Excel file. The Excel data is read using the PHPexcel package, as described in the export chapters 22 of this tutorial.

23.2.5 admin/models/helloimport.php[edit]

The database is updated with this model class. For simplicity of this tutorial, only the fields 'id', 'uid' and 'greeting' are imported.

23.3 Source files for this step[edit]

23.4 Testing the features of this step[edit]

  • Create a csv or Excel file to import. You can use a previously exported file and adapt that, or you can create a new file from scratch.
  • Log in either the backend or the frontend as a user with upload permission.
  • Upload the file by clicking 'Import csv' for a csv import file, or 'Import xls' for an Excel file.
  • Check the import by inspecting the item list overview page, and/or the item edit page.