This repository has been archived by the owner on Sep 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some code refactoring in the controllers
- Loading branch information
Showing
10 changed files
with
322 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
app/code/community/BL/CustomGrid/controllers/Blcg/Grid/ExportController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Open Software License (OSL 3.0) | ||
* that is bundled with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://opensource.org/licenses/osl-3.0.php | ||
* | ||
* @category BL | ||
* @package BL_CustomGrid | ||
* @copyright Copyright (c) 2015 Benoît Leulliette <[email protected]> | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
|
||
class BL_CustomGrid_Blcg_Grid_ExportController extends BL_CustomGrid_Controller_Grid_Action | ||
{ | ||
public function formAction() | ||
{ | ||
$this->_prepareWindowGridFormLayout( | ||
'export', | ||
array( | ||
'total_size' => $this->getRequest()->getParam('total_size'), | ||
'first_index' => $this->getRequest()->getParam('first_index'), | ||
'additional_params' => $this->getRequest()->getParam('additional_params', array()), | ||
), | ||
BL_CustomGrid_Model_Grid_Sentry::ACTION_EXPORT_RESULTS | ||
); | ||
$this->renderLayout(); | ||
} | ||
|
||
/** | ||
* Restore in the request the additional parameters from the given export config | ||
* | ||
* @param array $exportConfig Export config values | ||
*/ | ||
protected function _restoreExportAdditionalParams(array $exportConfig) | ||
{ | ||
if (isset($exportConfig['additional_params']) && is_array($exportConfig['additional_params'])) { | ||
foreach ($exportConfig['additional_params'] as $key => $value) { | ||
if (!$this->getRequest()->has($key)) { | ||
$this->getRequest()->setParam($key, $value); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Apply an export action for the given format and file name | ||
* | ||
* @param string $format Export format | ||
* @param string $fileName Exported file name | ||
*/ | ||
protected function _applyExportAction($format, $fileName) | ||
{ | ||
try { | ||
$gridModel = $this->_initGridModel(); | ||
$this->_initGridProfile(); | ||
|
||
if (!is_array($config = $this->getRequest()->getParam('export'))) { | ||
$config = null; | ||
} | ||
|
||
$this->_restoreExportAdditionalParams($config); | ||
|
||
if ($format == 'csv') { | ||
$exportOutput = $gridModel->getExporter()->exportToCsv($config); | ||
} elseif ($format == 'xml') { | ||
$exportOutput = $gridModel->getExporter()->exportToExcel($config); | ||
} else { | ||
$exportOutput = ''; | ||
} | ||
|
||
$this->_prepareDownloadResponse($fileName, $exportOutput); | ||
|
||
} catch (Mage_Core_Exception $e) { | ||
$this->_getSession()->addError($e->getMessage()); | ||
$this->_redirectReferer(); | ||
} catch (Exception $e) { | ||
Mage::logException($e); | ||
$this->_getSession()->addError($this->__('An error occurred while exporting grid results')); | ||
$this->_redirectReferer(); | ||
} | ||
} | ||
|
||
public function exportCsvAction() | ||
{ | ||
$this->_applyExportAction('csv', 'export.csv'); | ||
} | ||
|
||
public function exportExcelAction() | ||
{ | ||
$this->_applyExportAction('xml', 'export.xml'); | ||
} | ||
|
||
protected function _isAllowed() | ||
{ | ||
// Specific permissions are enforced by the models | ||
return true; | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
app/code/community/BL/CustomGrid/controllers/Blcg/Grid/MassController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
/** | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Open Software License (OSL 3.0) | ||
* that is bundled with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://opensource.org/licenses/osl-3.0.php | ||
* | ||
* @category BL | ||
* @package BL_CustomGrid | ||
* @copyright Copyright (c) 2015 Benoît Leulliette <[email protected]> | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
|
||
class BL_CustomGrid_Blcg_Grid_MassController extends BL_CustomGrid_Controller_Grid_Action | ||
{ | ||
/** | ||
* Apply a mass-action action with the given callback that will be used for each selected grid ID | ||
* | ||
* @param callback $callback Callback to use for each grid ID | ||
* @param string $defaultErrorMessage Default error message to display if a non-Magento exception is caught | ||
* @param string $successfulMessage Message that will be displayed with the number of successfully handled IDs | ||
* @param string $permissionErrorsMessage Message that will be displayed with the number of IDs that could not be | ||
* handled due to permission errors | ||
*/ | ||
protected function _applyMassactionAction( | ||
$callback, | ||
$defaultErrorMessage, | ||
$successfulMessage, | ||
$permissionErrorsMessage | ||
) { | ||
if (!$this->_validateMassActionValues('grid')) { | ||
return; | ||
} | ||
|
||
$gridsIds = $this->getRequest()->getParam('grid'); | ||
$successfulCount = 0; | ||
$permissionErrorsCount = 0; | ||
|
||
try { | ||
foreach ($gridsIds as $gridId) { | ||
try { | ||
call_user_func($callback, $gridId); | ||
++$successfulCount; | ||
} catch (BL_CustomGrid_Grid_Permission_Exception $e) { | ||
++$permissionErrorsCount; | ||
} | ||
} | ||
} catch (Mage_Core_Exception $e) { | ||
$this->_getSession()->addError($e->getMessage()); | ||
} catch (Exception $e) { | ||
Mage::logException($e); | ||
$this->_getSession()->addError($this->__($defaultErrorMessage)); | ||
} | ||
|
||
if ($successfulCount > 0) { | ||
$this->_getSession()->addSuccess($this->__($successfulMessage, $successfulCount)); | ||
} | ||
if ($permissionErrorsCount > 0) { | ||
$this->_getSession()->addError($this->__($permissionErrorsMessage, $permissionErrorsCount)); | ||
} | ||
|
||
$this->getResponse()->setRedirect($this->getUrl('*/*/')); | ||
} | ||
|
||
/** | ||
* Disable the grid model corresponding to the ID | ||
* | ||
* @param int $gridId Grid model ID | ||
*/ | ||
protected function _massDisableGrid($gridId) | ||
{ | ||
/** @var $gridModel BL_CustomGrid_Model_Grid */ | ||
$gridModel = Mage::getSingleton('customgrid/grid'); | ||
$gridModel->load($gridId)->setDisabled(true)->save(); | ||
} | ||
|
||
public function massDisableAction() | ||
{ | ||
$this->_applyMassactionAction( | ||
array($this, '_massDisableGrid'), | ||
'An error occurred while disabling a grid', | ||
'Total of %d grid(s) have been disabled', | ||
'You were not allowed to disable %d of the chosen grids' | ||
); | ||
} | ||
|
||
/** | ||
* Enable the grid model corresponding to the given ID | ||
* | ||
* @param int $gridId Grid model ID | ||
*/ | ||
protected function _massEnableGrid($gridId) | ||
{ | ||
/** @var $gridModel BL_CustomGrid_Model_Grid */ | ||
$gridModel = Mage::getSingleton('customgrid/grid'); | ||
$gridModel->load($gridId)->setDisabled(false)->save(); | ||
} | ||
|
||
public function massEnableAction() | ||
{ | ||
$this->_applyMassactionAction( | ||
array($this, '_massEnableGrid'), | ||
'An error occurred while enabling a grid', | ||
'Total of %d grid(s) have been enabled', | ||
'You were not allowed to enable %d of the chosen grids' | ||
); | ||
} | ||
|
||
/** | ||
* Delete the grid model corresponding to the given ID | ||
* | ||
* @param int $gridId Grid model ID | ||
*/ | ||
protected function _massDeleteGrid($gridId) | ||
{ | ||
/** @var $gridModel BL_CustomGrid_Model_Grid */ | ||
$gridModel = Mage::getSingleton('customgrid/grid'); | ||
$gridModel->load($gridId)->delete(); | ||
} | ||
|
||
public function massDeleteAction() | ||
{ | ||
$this->_applyMassactionAction( | ||
array($this, '_massDeleteGrid'), | ||
'An error occurred while deleting a grid', | ||
'Total of %d grid(s) have been deleted', | ||
'You were not allowed to delete %d of the chosen grids' | ||
); | ||
} | ||
|
||
protected function _isAllowed() | ||
{ | ||
return $this->_getAdminSession()->isAllowed('customgrid/administration/view_grids_list'); | ||
} | ||
} |
Oops, something went wrong.