From 5a38bc67c0ee7f0c14fdd159699f8bd7965bf43d Mon Sep 17 00:00:00 2001 From: Hy-Fly Date: Mon, 25 Apr 2016 12:32:16 +0200 Subject: [PATCH] add import with summary screen --- README.md | 5 +- admin/controllers/helloimport.php | 14 ++- admin/sql/updates/mysql/1.0.24.sql | 0 admin/views/helloimport/index.html | 1 + admin/views/helloimport/tmpl/default.php | 18 +++ admin/views/helloimport/tmpl/index.html | 1 + admin/views/helloimport/view.html.php | 135 +++++++++++++++++++++++ admin/views/helloworlds/view.html.php | 3 + helloworld.xml | 2 +- site/controllers/helloimport.php | 14 ++- site/views/helloimport/index.html | 1 + site/views/helloimport/tmpl/default.php | 18 +++ site/views/helloimport/tmpl/index.html | 1 + site/views/helloimport/view.html.php | 135 +++++++++++++++++++++++ site/views/helloworlds/view.html.php | 3 + 15 files changed, 347 insertions(+), 4 deletions(-) create mode 100644 admin/sql/updates/mysql/1.0.24.sql create mode 100644 admin/views/helloimport/index.html create mode 100644 admin/views/helloimport/tmpl/default.php create mode 100644 admin/views/helloimport/tmpl/index.html create mode 100644 admin/views/helloimport/view.html.php create mode 100644 site/views/helloimport/index.html create mode 100644 site/views/helloimport/tmpl/default.php create mode 100644 site/views/helloimport/tmpl/index.html create mode 100644 site/views/helloimport/view.html.php diff --git a/README.md b/README.md index 766cbed..39f82dc 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/admin/controllers/helloimport.php b/admin/controllers/helloimport.php index 5c1c863..7997ad6 100644 --- a/admin/controllers/helloimport.php +++ b/admin/controllers/helloimport.php @@ -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. @@ -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)) { diff --git a/admin/sql/updates/mysql/1.0.24.sql b/admin/sql/updates/mysql/1.0.24.sql new file mode 100644 index 0000000..e69de29 diff --git a/admin/views/helloimport/index.html b/admin/views/helloimport/index.html new file mode 100644 index 0000000..94906bc --- /dev/null +++ b/admin/views/helloimport/index.html @@ -0,0 +1 @@ + diff --git a/admin/views/helloimport/tmpl/default.php b/admin/views/helloimport/tmpl/default.php new file mode 100644 index 0000000..98dc369 --- /dev/null +++ b/admin/views/helloimport/tmpl/default.php @@ -0,0 +1,18 @@ + +
+ + + + Go Back + + +
diff --git a/admin/views/helloimport/tmpl/index.html b/admin/views/helloimport/tmpl/index.html new file mode 100644 index 0000000..94906bc --- /dev/null +++ b/admin/views/helloimport/tmpl/index.html @@ -0,0 +1 @@ + diff --git a/admin/views/helloimport/view.html.php b/admin/views/helloimport/view.html.php new file mode 100644 index 0000000..b224f6a --- /dev/null +++ b/admin/views/helloimport/view.html.php @@ -0,0 +1,135 @@ +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') + ) . "

"; + } + + 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 '' . "\n"; + foreach( $rows as $row ) + { + echo '' . PHP_EOL; + if (trim($row)) + { + $fields = explode($delimiter,$row); + array_push($data, $fields); + + foreach( $fields as $f ) + { + echo '' . PHP_EOL; + } + } + echo '' . PHP_EOL; + } + echo '
' + . $f .'
' . 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 '' . "\n"; + for ($row = 1; $row <= $highestRow; ++$row) { + echo '' . PHP_EOL; + for ($col = 0; $col < $highestColumnIndex; ++$col) { + echo '' . PHP_EOL; + } + echo '' . PHP_EOL; + } + echo '
' . + $sht->getCellByColumnAndRow($col, $row)->getValue() . + '
' . PHP_EOL; + + $rows = $sht->rangeToArray($range); + $model = $this->getModel('HelloImport'); //for db read/write + return $model->importItems($rows); + } +} diff --git a/admin/views/helloworlds/view.html.php b/admin/views/helloworlds/view.html.php index 0694622..c0c6bd9 100644 --- a/admin/views/helloworlds/view.html.php +++ b/admin/views/helloworlds/view.html.php @@ -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')) { diff --git a/helloworld.xml b/helloworld.xml index 331abed..a16fe53 100644 --- a/helloworld.xml +++ b/helloworld.xml @@ -10,7 +10,7 @@ Copyright Info License Info - 0.1.23 + 0.1.24 COM_HELLOWORLD_DESCRIPTION diff --git a/site/controllers/helloimport.php b/site/controllers/helloimport.php index 5c1c863..7997ad6 100644 --- a/site/controllers/helloimport.php +++ b/site/controllers/helloimport.php @@ -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. @@ -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)) { diff --git a/site/views/helloimport/index.html b/site/views/helloimport/index.html new file mode 100644 index 0000000..94906bc --- /dev/null +++ b/site/views/helloimport/index.html @@ -0,0 +1 @@ + diff --git a/site/views/helloimport/tmpl/default.php b/site/views/helloimport/tmpl/default.php new file mode 100644 index 0000000..98dc369 --- /dev/null +++ b/site/views/helloimport/tmpl/default.php @@ -0,0 +1,18 @@ + +
+ + + + Go Back + + +
diff --git a/site/views/helloimport/tmpl/index.html b/site/views/helloimport/tmpl/index.html new file mode 100644 index 0000000..94906bc --- /dev/null +++ b/site/views/helloimport/tmpl/index.html @@ -0,0 +1 @@ + diff --git a/site/views/helloimport/view.html.php b/site/views/helloimport/view.html.php new file mode 100644 index 0000000..b224f6a --- /dev/null +++ b/site/views/helloimport/view.html.php @@ -0,0 +1,135 @@ +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') + ) . "

"; + } + + 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 '' . "\n"; + foreach( $rows as $row ) + { + echo '' . PHP_EOL; + if (trim($row)) + { + $fields = explode($delimiter,$row); + array_push($data, $fields); + + foreach( $fields as $f ) + { + echo '' . PHP_EOL; + } + } + echo '' . PHP_EOL; + } + echo '
' + . $f .'
' . 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 '' . "\n"; + for ($row = 1; $row <= $highestRow; ++$row) { + echo '' . PHP_EOL; + for ($col = 0; $col < $highestColumnIndex; ++$col) { + echo '' . PHP_EOL; + } + echo '' . PHP_EOL; + } + echo '
' . + $sht->getCellByColumnAndRow($col, $row)->getValue() . + '
' . PHP_EOL; + + $rows = $sht->rangeToArray($range); + $model = $this->getModel('HelloImport'); //for db read/write + return $model->importItems($rows); + } +} diff --git a/site/views/helloworlds/view.html.php b/site/views/helloworlds/view.html.php index 4b4b7e2..cbdea16 100644 --- a/site/views/helloworlds/view.html.php +++ b/site/views/helloworlds/view.html.php @@ -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); } }