Wednesday, August 8, 2012

Magento : MassDelete & Status action in admin grid

Consider Package: Bd, Module : Test & Model : Test

1.Override _prepareMassaction() method in Grid.php with following code

protected function _prepareMassaction()
    {
        $this->setMassactionIdField('test_id');
        $this->getMassactionBlock()->setFormFieldName('test');

        $this->getMassactionBlock()->addItem('delete', array(
             'label'    => Mage::helper('test')->__('Delete'),
             'url'      => $this->getUrl('*/*/massDelete'),
             'confirm'  => Mage::helper('test')->__('Are you sure?')
        ));

        $statuses = Mage::getSingleton('test/status')->getOptionArray();

        array_unshift($statuses, array('label'=>'', 'value'=>''));
        $this->getMassactionBlock()->addItem('status', array(
             'label'=> Mage::helper('test')->__('Change status'),
             'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
             'additional' => array(
                    'visibility' => array(
                         'name' => 'status',
                         'type' => 'select',
                         'class' => 'required-entry',
                         'label' => Mage::helper('test')->__('Status'),
                         'values' => $statuses
                     )
             )
        ));
        return $this;
    }


2.Create two action in admin controller with following code
public function massDeleteAction() {
        $testIds = $this->getRequest()->getParam('test');
        if(!is_array($testIds)) {
   Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
        } else {
            try {
                foreach ($testIds as $testId) {
                    $test = Mage::getModel('test/test')->load($testId);
                    $test->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('adminhtml')->__(
                        'Total of %d record(s) were successfully deleted', count($testIds)
                    )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }

    public function massStatusAction()
    {
        $testIds = $this->getRequest()->getParam('test');
        if(!is_array($testIds)) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
        } else {
            try {
                foreach ($testIds as $testId) {
                    $test = Mage::getSingleton('test/test')
                        ->load($testId)
                        ->setStatus($this->getRequest()->getParam('status'))
                        ->setIsMassupdate(true)
                        ->save();
                }
                $this->_getSession()->addSuccess(
                    $this->__('Total of %d record(s) were successfully updated', count($testIds))
                );
            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }

No comments:

Post a Comment