From e3e38b19e1f7a2921cbf4fc3d536cde2e4e2cf98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Gaillard?= Date: Wed, 13 Feb 2013 16:15:37 +0100 Subject: [PATCH] [*] BO: you can now export listing of some tabs in CSV --- classes/controller/AdminController.php | 45 +++++++++++++++++++ .../admin/AdminAddressesController.php | 23 +++++----- controllers/admin/AdminCartsController.php | 1 + .../admin/AdminCategoriesController.php | 1 + .../admin/AdminCustomersController.php | 2 + .../admin/AdminManufacturersController.php | 1 + controllers/admin/AdminProductsController.php | 1 + .../admin/AdminSuppliersController.php | 2 + 8 files changed, 63 insertions(+), 13 deletions(-) diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index bfda6b83d..1f9949518 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -170,6 +170,8 @@ class AdminControllerCore extends Controller /** @var bool boolean List content lines are clickable if true */ protected $list_no_link = false; + protected $allow_export = false; + /** @var array $cache_lang cache for traduction */ public static $cache_lang = array(); @@ -564,6 +566,39 @@ class AdminControllerCore extends Controller $this->errors[] = Tools::displayError('An error occurred during image deletion (cannot load object).'); return $object; } + + public function processExport() + { + // clean buffer + ob_clean(); + $this->getList($this->context->language->id); + if (!count($this->_list)) + return; + + header('Content-type: text/csv'); + header('Content-Type: application/force-download; charset=UTF-8'); + header('Cache-Control: no-store, no-cache'); + header('Content-disposition: attachment; filename="'.$this->table.'_'.date('Y-m-d_His').'.csv"'); + + $headers = array(); + foreach ($this->fields_list as $datas) + $headers[] = $datas['title']; + + $content = array(); + foreach ($this->_list as $i => $row) + { + $content[$i] = array(); + foreach ($this->fields_list as $key => $value) + $content[$i][] = $row[$key]; + } + $this->context->smarty->assign(array( + 'export_headers' => $headers, + 'export_content' => $content + ) + ); + + $this->layout = 'layout-export.tpl'; + } /** * Object Delete @@ -1025,6 +1060,11 @@ class AdminControllerCore extends Controller 'href' => self::$currentIndex.'&add'.$this->table.'&token='.$this->token, 'desc' => $this->l('Add new') ); + if ($this->allow_export) + $this->toolbar_btn['export'] = array( + 'href' => self::$currentIndex.'&export'.$this->table.'&token='.$this->token, + 'desc' => $this->l('Export') + ); } $this->addToolBarModulesListButton(); } @@ -1929,6 +1969,11 @@ class AdminControllerCore extends Controller else $this->errors[] = Tools::displayError('You do not have permission to view here.'); } + elseif (isset($_GET['export'.$this->table])) + { + if ($this->tabAccess['view'] === '1') + $this->action = 'export'; + } /* Cancel all filters for this tab */ elseif (isset($_POST['submitReset'.$this->table])) $this->action = 'reset_filters'; diff --git a/controllers/admin/AdminAddressesController.php b/controllers/admin/AdminAddressesController.php index e949ad4f3..223b0f12b 100644 --- a/controllers/admin/AdminAddressesController.php +++ b/controllers/admin/AdminAddressesController.php @@ -44,6 +44,8 @@ class AdminAddressesControllerCore extends AdminController $this->addRowAction('delete'); $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?'))); + $this->allow_export = true; + if (!Tools::getValue('realedit')) $this->deleted = true; @@ -61,8 +63,15 @@ class AdminAddressesControllerCore extends AdminController 'country' => array('title' => $this->l('Country'), 'width' => 100, 'type' => 'select', 'list' => $this->countries_array, 'filter_key' => 'cl!id_country')); parent::__construct(); + + $this->_select = 'cl.`name` as country'; + $this->_join = ' + LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (cl.`id_country` = a.`id_country` AND cl.`id_lang` = '.(int)$this->context->language->id.') + LEFT JOIN `'._DB_PREFIX_.'customer` c ON a.id_customer = c.id_customer + '; + $this->_where = 'AND a.id_customer != 0 '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER, 'c'); } - + public function initToolbar() { parent::initToolbar(); @@ -71,18 +80,6 @@ class AdminAddressesControllerCore extends AdminController 'desc' => $this->l('Import') ); } - - public function renderList() - { - $this->_select = 'cl.`name` as country'; - $this->_join = ' - LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (cl.`id_country` = a.`id_country` AND cl.`id_lang` = '.(int)$this->context->language->id.') - LEFT JOIN `'._DB_PREFIX_.'customer` c ON a.id_customer = c.id_customer - '; - $this->_where = 'AND a.id_customer != 0 '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER, 'c'); - - return parent::renderList(); - } public function renderForm() { diff --git a/controllers/admin/AdminCartsController.php b/controllers/admin/AdminCartsController.php index a0a85295e..9c8a0d299 100755 --- a/controllers/admin/AdminCartsController.php +++ b/controllers/admin/AdminCartsController.php @@ -35,6 +35,7 @@ class AdminCartsControllerCore extends AdminController $this->addRowAction('view'); $this->addRowAction('delete'); + $this->allow_export = true; $this->_select = 'CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) `customer`, a.id_cart total, ca.name carrier, o.id_order, IF(co.id_guest, 1, 0) id_guest'; $this->_join = 'LEFT JOIN '._DB_PREFIX_.'customer c ON (c.id_customer = a.id_customer) diff --git a/controllers/admin/AdminCategoriesController.php b/controllers/admin/AdminCategoriesController.php index 239cf281e..220d5f2d1 100644 --- a/controllers/admin/AdminCategoriesController.php +++ b/controllers/admin/AdminCategoriesController.php @@ -45,6 +45,7 @@ class AdminCategoriesControllerCore extends AdminController $this->lang = true; $this->deleted = false; $this->explicitSelect = true; + $this->allow_export = true; $this->context = Context::getContext(); diff --git a/controllers/admin/AdminCustomersController.php b/controllers/admin/AdminCustomersController.php index 727df43ec..29855a5a8 100644 --- a/controllers/admin/AdminCustomersController.php +++ b/controllers/admin/AdminCustomersController.php @@ -42,6 +42,8 @@ class AdminCustomersControllerCore extends AdminController $this->deleted = true; $this->explicitSelect = true; + $this->allow_export = true; + $this->addRowAction('edit'); $this->addRowAction('view'); $this->addRowAction('delete'); diff --git a/controllers/admin/AdminManufacturersController.php b/controllers/admin/AdminManufacturersController.php index cae9b74ee..c20657521 100644 --- a/controllers/admin/AdminManufacturersController.php +++ b/controllers/admin/AdminManufacturersController.php @@ -35,6 +35,7 @@ class AdminManufacturersControllerCore extends AdminController $this->className = 'Manufacturer'; $this->lang = false; $this->deleted = false; + $this->allow_export = true; $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?'))); diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index 8a26749cb..207a90841 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -71,6 +71,7 @@ class AdminProductsControllerCore extends AdminController $this->_defaultOrderBy = 'position'; $this->max_file_size = (int)(Configuration::get('PS_LIMIT_UPLOAD_FILE_VALUE') * 1000000); $this->max_image_size = (int)Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE'); + $this->allow_export = true; // @since 1.5 : translations for tabs $this->available_tabs_lang = array ( diff --git a/controllers/admin/AdminSuppliersController.php b/controllers/admin/AdminSuppliersController.php index b8644d642..95f9ebe4c 100644 --- a/controllers/admin/AdminSuppliersController.php +++ b/controllers/admin/AdminSuppliersController.php @@ -34,6 +34,8 @@ class AdminSuppliersControllerCore extends AdminController $this->addRowAction('view'); $this->addRowAction('edit'); $this->addRowAction('delete'); + $this->allow_export = true; + $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?'))); $this->_select = 'COUNT(DISTINCT ps.`id_product`) AS products';