Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
some code refactoring in the controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
mage-eag committed Mar 8, 2016
1 parent 7132d65 commit e88b74c
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 283 deletions.
6 changes: 3 additions & 3 deletions app/code/community/BL/CustomGrid/Block/Grid/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,23 @@ protected function _prepareMassaction()
'mass_enable',
array(
'label' => $this->__('Enable'),
'url' => $this->getUrl('*/*/massEnable', array('_current' => true)),
'url' => $this->getUrl('*/blcg_grid_mass/massEnable', array('_current' => true)),
'confirm' => $this->__('Are you sure?'),
)
)
->addItem(
'mass_disable',
array(
'label' => $this->__('Disable'),
'url' => $this->getUrl('*/*/massDisable', array('_current' => true)),
'url' => $this->getUrl('*/blcg_grid_mass/massDisable', array('_current' => true)),
'confirm' => $this->__('Are you sure?'),
)
)
->addItem(
'mass_delete',
array(
'label' => $this->__('Delete'),
'url' => $this->getUrl('*/*/massDelete', array('_current' => true)),
'url' => $this->getUrl('*/blcg_grid_mass/massDelete', array('_current' => true)),
'confirm' => $this->__('Are you sure?'),
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function getDefaultParamsFormUrl()
*/
public function getExportFormUrl()
{
return $this->getUrl('adminhtml/blcg_grid/exportForm', $this->getBaseUrlParams());
return $this->getUrl('adminhtml/blcg_grid_export/form', $this->getBaseUrlParams());
}

/**
Expand Down
66 changes: 66 additions & 0 deletions app/code/community/BL/CustomGrid/Controller/Grid/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,72 @@ protected function _initWindowFormLayout(
return $this;
}

/**
* Initialize the current grid model and profile, check the given permissions,
* then prepare the layout for the given window grid form type
*
* @param string $formType Form type
* @param array $formData Form data
* @param string|array $permissions Required user permission(s)
* @param bool $anyPermission Whether all the given permissions are required, or just any of them
* @param array $handles Layout handles
* @return BL_CustomGrid_Blcg_GridController
*/
protected function _prepareWindowGridFormLayout(
$formType,
array $formData,
$permissions = null,
$anyPermission = true,
array $handles = array('blcg_empty')
) {
$this->_initWindowFormLayout(
'adminhtml_blcg_grid_form_window_action',
'adminhtml_blcg_grid_form_window_error',
'blcg.grid.form_error',
$permissions,
$anyPermission,
$handles
);

if ($containerBlock = $this->getLayout()->getBlock('blcg.grid.form_container')) {
/** @var $containerBlock BL_CustomGrid_Block_Grid_Form_Container */
$containerBlock->setFormData($formData)->setFormType($formType);
}

return $this;
}

/**
* Initialize the current grid model and profile, check the given permissions,
* then prepare the layout for the given profile action
*
* @param string $actionCode Profile action
* @param string|array $permissions Required user permission(s)
* @param bool $anyPermission Whether all the given permissions are required, or just one of them
* @return BL_CustomGrid_Blcg_Grid_ProfileController
*/
protected function _prepareWindowProfileFormLayout($actionCode, $permissions = null, $anyPermission = true)
{
$this->_initWindowFormLayout(
'adminhtml_blcg_grid_profile_form_window_action',
'adminhtml_blcg_grid_profile_form_window_error',
'blcg.grid_profile.form_error',
$permissions,
$anyPermission
);

if (($gridProfile = Mage::registry('blcg_grid_profile'))
&& ($containerBlock = $this->getLayout()->getBlock('blcg.grid_profile.form_container'))) {
/**
* @var BL_CustomGrid_Model_Grid_Profile $gridProfile
* @var BL_CustomGrid_Block_Grid_Profile_Form_Container $containerBlock
*/
$containerBlock->setProfileId($gridProfile->getId())->setActionCode($actionCode);
}

return $this;
}

/**
* Validate that one or more values were selected for a mass-action,
* otherwise force a redirect to the index action
Expand Down
4 changes: 2 additions & 2 deletions app/code/community/BL/CustomGrid/Model/Grid/Type/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,13 @@ protected function _getExportTypes($blockType)
return array(
'csv' => new BL_CustomGrid_Object(
array(
'route' => 'adminhtml/blcg_grid/exportCsv',
'route' => 'adminhtml/blcg_grid_export/exportCsv',
'label' => $this->getBaseHelper()->__('CSV'),
)
),
'xml' => new BL_CustomGrid_Object(
array(
'route' => 'adminhtml/blcg_grid/exportExcel',
'route' => 'adminhtml/blcg_grid_export/exportExcel',
'label' => $this->getBaseHelper()->__('Excel'),
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function formAction()

if (!$helper->isAjaxRequest()
|| ($formBlock instanceof Mage_Core_Block_Abstract)) {
$this->_prepareFormLayout($formBlock, $errorMessage);
$this->_prepareWindowProfileFormLayout($formBlock, $errorMessage);
} elseif (!is_null($errorMessage)) {
$this->_setActionErrorJsonResponse($errorMessage);
} else {
Expand Down
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 <benoit.leulliette@gmail.com>
* @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;
}
}
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 <benoit.leulliette@gmail.com>
* @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');
}
}

0 comments on commit e88b74c

Please sign in to comment.