Skip to content

Commit

Permalink
add import with summary screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Hy-Fly committed Apr 25, 2016
1 parent c84f0dc commit 5a38bc6
Show file tree
Hide file tree
Showing 15 changed files with 347 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -3,9 +3,12 @@ This tutorial provides a guideline for development of a Joomla component that en

This guide continues from the tutorial 'Developing an MVC Component' (aka the 'Hello World' tutorial) from this Joomla site: https://docs.joomla.org/J3.x:Developing_an_MVC_Component.

See also [the wiki](https://github.com/Hy-Fly/Joomla-Hello-Front-End/wiki) for documentation.

Version history:

v1-24 2016-04 add import with summary screen

v1-23 2016-04 add csv and xlsx import

v1-22 2016-04 add excel xlsx export, based on the PHPExcel package
Expand All @@ -22,4 +25,4 @@ v1-17 2016-04 clone the back-end record item editing page to the front-end

v1-16 2016-04 simple front-end data entry

v1-15 2016-04 initial version based on the Joomla3 tutorial: Developing an MVC Component
v1-15 2016-04 initial version based on the Joomla3 tutorial: Developing an MVC Component
14 changes: 13 additions & 1 deletion admin/controllers/helloimport.php
Expand Up @@ -20,6 +20,18 @@
*/
class HelloWorldControllerHelloImport extends JControllerAdmin
{
/**
* import
* Defer to the viewer where all the work is done.
*/
public function import_check()
{
$view = $this->getView( 'HelloImport', 'html'); //display user form
$model = $this->getModel('HelloImport'); //for db read/write
$view->setModel($model, true); //define as default model for view
$view->display(); //call default template
}

/**
* Helper functions
* Begin and end are the same, so are combined in separate helpers.
Expand Down Expand Up @@ -82,7 +94,7 @@ private function cvs2array($content, $delimiter)
$content = str_replace("\n\n","\n", $content);
$rows = explode("\n",trim($content));

foreach( $rows as $row )
foreach( $rows as $row )
{
if (trim($row))
{
Expand Down
Empty file.
1 change: 1 addition & 0 deletions admin/views/helloimport/index.html
@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>
18 changes: 18 additions & 0 deletions admin/views/helloimport/tmpl/default.php
@@ -0,0 +1,18 @@
<?php
/**
* @subpackage com_helloworld
*
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die('Restricted access'); // No direct access
JHtml::_('behavior.formvalidation');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&view=helloimport'); ?>"
method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">

<input type="file" name="importfile" id="importfile" />
<input type="submit" value="Upload" name="submit" />
<a href="<?php echo JRoute::_('index.php?option=com_helloworld&view=helloworlds'); ?>" class="btn btn-default" style="margin-left:100px;">Go Back</a>

<?php echo JHtml::_('form.token'); ?>
</form>
1 change: 1 addition & 0 deletions admin/views/helloimport/tmpl/index.html
@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>
135 changes: 135 additions & 0 deletions admin/views/helloimport/view.html.php
@@ -0,0 +1,135 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/** Include PHPExcel */
require_once $_SERVER['DOCUMENT_ROOT'].'/lib/Classes/PHPExcel.php';

/**
* HelloWorld View
*/
class HelloWorldViewHelloImport extends JViewLegacy
{
protected $form;
protected $item;
protected $script;
protected $canDo;

/**
* Display the HelloWorld view
*
* @param string $tpl The name of the template file to parse; automatically searches through the template paths.
* @return void
*/
public function display($tpl = null)
{
// check if form has sent a file
$files = JFactory::getApplication()->input->files;
$file = $files->get('importfile'); //Joomla way of $_FILES['importfile']

if( !empty($file['name']) )
{
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); //check for form tampering

switch (pathinfo($file['name'], PATHINFO_EXTENSION)) {
case "csv":
$ok = $this->import_csv($file);
break;
case "xls":
case "xlsx":
$ok = $this->import_xls($file);
break;
default:
echo "something else";
}
echo ( $ok
? JText::_('COM_HELLOWORLD_IMPORT_SUCCESS')
: JText::_('COM_HELLOWORLD_IMPORT_ERRORS')
) . "<br><br>";
}

parent::display($tpl); // Display the template
}

/**
* import csv
*/
public function import_csv($file)
{
// Reads the content which has been uploaded to the tmp file into a text var
$content = file_get_contents($file['tmp_name']);
unlink($file['tmp_name']); //delete tmp file

$rows = $this->cvs2array($content, ';');
$model = $this->getModel('HelloImport'); //for db read/write
return $model->importItems($rows);
}

public function cvs2array($content, $delimiter)
{
$data = array();
$content = str_replace("\r","\n", $content);
$content = str_replace("\n\n","\n", $content);
$rows = explode("\n",trim($content));

echo '<table style="margin:10px;">' . "\n";
foreach( $rows as $row )
{
echo '<tr>' . PHP_EOL;
if (trim($row))
{
$fields = explode($delimiter,$row);
array_push($data, $fields);

foreach( $fields as $f )
{
echo '<td style="border: 1px solid black; padding: 5px;">'
. $f .'</td>' . PHP_EOL;
}
}
echo '</tr>' . PHP_EOL;
}
echo '</table>' . PHP_EOL;

return $data;
}

/**
* import xls
*/
public function import_xls($file)
{
// Reads the content which has been uploaded to the tmp file into a text var
$xls = $file['tmp_name'];
$objPHPExcel = PHPExcel_IOFactory::load($xls);
$sht = $objPHPExcel->getActiveSheet();

$highestColumn = $sht->getHighestDataColumn();
$highestRow = $sht->getHighestDataRow();
$range = "A1:".$highestColumn.$highestRow;

$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
echo '<table style="margin:10px;">' . "\n";
for ($row = 1; $row <= $highestRow; ++$row) {
echo '<tr>' . PHP_EOL;
for ($col = 0; $col < $highestColumnIndex; ++$col) {
echo '<td style="border: 1px solid black; padding: 5px;">' .
$sht->getCellByColumnAndRow($col, $row)->getValue() .
'</td>' . PHP_EOL;
}
echo '</tr>' . PHP_EOL;
}
echo '</table>' . PHP_EOL;

$rows = $sht->rangeToArray($range);
$model = $this->getModel('HelloImport'); //for db read/write
return $model->importItems($rows);
}
}
3 changes: 3 additions & 0 deletions admin/views/helloworlds/view.html.php
Expand Up @@ -108,6 +108,9 @@ protected function addToolBar()
$toolbar->appendButton('RawFormat', 'download', 'Export xls', 'helloexport.exportxls');
$toolbar->appendButton('ImportFile', 'upload', 'Import csv', 'helloimport.importcsv', 'importcsv');
$toolbar->appendButton('ImportFile', 'upload', 'Import xls', 'helloimport.importxls', 'importxls');

// an ordinary standard button for import. listmode=false as no list is needed
JToolBarHelper::custom('helloimport.import_check', 'upload', '', 'Import Check', false);
}
if ($this->canDo->get('core.admin'))
{
Expand Down
2 changes: 1 addition & 1 deletion helloworld.xml
Expand Up @@ -10,7 +10,7 @@
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.1.23</version>
<version>0.1.24</version>
<!-- The description is optional and defaults to the name -->
<description>COM_HELLOWORLD_DESCRIPTION</description>

Expand Down
14 changes: 13 additions & 1 deletion site/controllers/helloimport.php
Expand Up @@ -20,6 +20,18 @@
*/
class HelloWorldControllerHelloImport extends JControllerAdmin
{
/**
* import
* Defer to the viewer where all the work is done.
*/
public function import_check()
{
$view = $this->getView( 'HelloImport', 'html'); //display user form
$model = $this->getModel('HelloImport'); //for db read/write
$view->setModel($model, true); //define as default model for view
$view->display(); //call default template
}

/**
* Helper functions
* Begin and end are the same, so are combined in separate helpers.
Expand Down Expand Up @@ -82,7 +94,7 @@ private function cvs2array($content, $delimiter)
$content = str_replace("\n\n","\n", $content);
$rows = explode("\n",trim($content));

foreach( $rows as $row )
foreach( $rows as $row )
{
if (trim($row))
{
Expand Down
1 change: 1 addition & 0 deletions site/views/helloimport/index.html
@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>
18 changes: 18 additions & 0 deletions site/views/helloimport/tmpl/default.php
@@ -0,0 +1,18 @@
<?php
/**
* @subpackage com_helloworld
*
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die('Restricted access'); // No direct access
JHtml::_('behavior.formvalidation');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&view=helloimport'); ?>"
method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">

<input type="file" name="importfile" id="importfile" />
<input type="submit" value="Upload" name="submit" />
<a href="<?php echo JRoute::_('index.php?option=com_helloworld&view=helloworlds'); ?>" class="btn btn-default" style="margin-left:100px;">Go Back</a>

<?php echo JHtml::_('form.token'); ?>
</form>
1 change: 1 addition & 0 deletions site/views/helloimport/tmpl/index.html
@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>

0 comments on commit 5a38bc6

Please sign in to comment.