From 8804720cb60a7bba2a3b814ee41eaf0e05b42248 Mon Sep 17 00:00:00 2001 From: Dinis Lage Date: Mon, 23 Sep 2013 20:37:46 +0100 Subject: [PATCH] Add action to export directly to Excel xlsx files. --- admin-dev/themes/default/css/admin.css | 1 + classes/controller/AdminController.php | 75 +++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/admin-dev/themes/default/css/admin.css b/admin-dev/themes/default/css/admin.css index a40b7e237..15602b4c9 100644 --- a/admin-dev/themes/default/css/admin.css +++ b/admin-dev/themes/default/css/admin.css @@ -232,6 +232,7 @@ a.module_toggle_all{color: #268CCD;} .toolbarBox .process-icon-save-and-preview { background-image: url('../img/process-icon-preview.png');} .toolbarBox .process-icon-import { background-image: url('../img/process-icon-export-csv.png');} .toolbarBox .process-icon-export { background-image: url('../img/process-icon-export-csv.png');} +.toolbarBox .process-icon-export-excel { background-image: url('../img/process-icon-export-csv.png');} .toolbarBox .process-icon-export-all { background-image: url('../img/process-icon-export-csv-details.png');} .toolbarBox .process-icon-export-stock-state-quantities-csv { background-image: url('../img/process-icon-export-csv.png');} .toolbarBox .process-icon-export-stock-state-prices-csv { background-image: url('../img/process-icon-export-csv-details.png');} diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index 84feddee9..4ae27476d 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -658,6 +658,69 @@ class AdminControllerCore extends Controller $this->layout = 'layout-export.tpl'; } + public function processExportExcel() + { + // clean buffer + if (ob_get_level() && ob_get_length() > 0) + ob_clean(); + $this->getList($this->context->language->id); + if (!count($this->_list)) + return; + + if (!extension_loaded('zip')) { + $this->displayWarning($this->l('Module php_zip must be enabled in order to use Excel export feature.')); + return; + } + + header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8'); + header('Cache-Control: no-store, no-cache'); + header('Content-disposition: attachment; filename="'.$this->table.'_'.date('Y-m-d_His').'.xlsx"'); + + $headers = array(); + foreach ($this->fields_list as $datas) + $headers[] = Tools::htmlentitiesDecodeUTF8($datas['title']); + $content = array(); + foreach ($this->_list as $i => $row) + { + $content[$i] = array(); + $path_to_image = false; + foreach ($this->fields_list as $key => $params) + { + $field_value = isset($row[$key]) ? Tools::htmlentitiesDecodeUTF8($row[$key]) : ''; + if ($key == 'image') + { + if ($params['image'] != 'p' || Configuration::get('PS_LEGACY_IMAGES')) + $path_to_image = Tools::getShopDomain(true)._PS_IMG_.$params['image'].'/'.$row['id_'.$this->table].(isset($row['id_image']) ? '-'.(int)$row['id_image'] : '').'.'.$this->imageType; + else + $path_to_image = Tools::getShopDomain(true)._PS_IMG_.$params['image'].'/'.Image::getImgFolderStatic($row['id_image']).(int)$row['id_image'].'.'.$this->imageType; + if ($path_to_image) + $field_value = $path_to_image; + } + $content[$i][] = $field_value; + } + } + + require_once(_PS_ROOT_DIR_.'/tools/phpexcel/PHPExcel.php'); + /** Create a new PHPExcel Object **/ + $objPHPExcel = new PHPExcel(); + $worksheet = $objPHPExcel->getSheet(0); + + $worksheet->fromArray( + $headers, // The data to set + NULL, // Array values with this value will not be set + 'A1' // Top left coordinate of the worksheet range where + // we want to set these values (default is A1) + ); + $worksheet->fromArray( + $content, + NULL, + 'A2' + ); + $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); + $objWriter->save('php://output'); + die(); + } + /** * Object Delete */ @@ -1126,11 +1189,16 @@ class AdminControllerCore extends Controller 'href' => self::$currentIndex.'&add'.$this->table.'&token='.$this->token, 'desc' => $this->l('Add new') ); - if ($this->allow_export) + if ($this->allow_export) { $this->toolbar_btn['export'] = array( 'href' => self::$currentIndex.'&export'.$this->table.'&token='.$this->token, 'desc' => $this->l('Export') ); + $this->toolbar_btn['export-excel'] = array( + 'href' => self::$currentIndex.'&export-excel'.$this->table.'&token='.$this->token, + 'desc' => $this->l('Export (Excel)') + ); + } } $this->addToolBarModulesListButton(); } @@ -2057,6 +2125,11 @@ class AdminControllerCore extends Controller if ($this->tabAccess['view'] === '1') $this->action = 'export'; } + elseif (isset($_GET['export-excel'.$this->table])) + { + if ($this->tabAccess['view'] === '1') + $this->action = 'exportExcel'; + } /* Cancel all filters for this tab */ elseif (isset($_POST['submitReset'.$this->list_id])) $this->action = 'reset_filters';