Showing posts with label Magento Admin Grid. Show all posts
Showing posts with label Magento Admin Grid. Show all posts

Sunday, June 15, 2014

Add wysiwyg editor in Magento Custom Module

Considering custom module Bd_Demo

Add following function in Bd_Demo_Block_Adminhtml_Demo_Edit

protected function _prepareLayout() {
    parent::_prepareLayout();
    if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
        $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
    }
}

Then use as in grid from  Bd_Demo_Block_Adminhtml_Demo_Edit_Tab_Form
$fieldset->addField("wood_description", "editor", array(
"label" => Mage::helper("woodflooring")->__("Description"),
"class" => "required-entry",
"required" => true,
"name" => "wood_description",
'config'    => Mage::getSingleton('cms/wysiwyg_config')->getConfig(),
'wysiwyg'   => true,
));

Monday, February 25, 2013

MAgento : Add custom action to Sales Order Grid

Here we use an event core_block_abstract_prepare_layout_before
1. Add following script in config.xml
<events>
<core_block_abstract_prepare_layout_before>
        <observers>
                <bdaction_block_abstract_prepare_layout_before>
                        <type>singleton</type>
                        <class>Bd_Test_Model_Observer</class>
                        <method>addMassCustomAction</method>
                </bdaction_block_abstract_prepare_layout_before>
        </observers>
</core_block_abstract_prepare_layout_before>
</events>
2.Create a observer class like Bd_Test_Model_Observer
then add a method
public static function addMassCustomAction($observer)
{
    $block = $observer->getEvent()->getBlock();
    if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
        && $block->getRequest()->getControllerName() == 'sales_order')
    {
        $block->addItem('customaction', array(
        'label' => 'Custom Action',
        'url' => Mage::helper('adminhtml')->getUrl('module/controller/action'),
        ));
    }
}
3.At last create that controller and action method

Wednesday, October 3, 2012

Magento : add massDelete action in coupon code

First override Mage_Adminhtml_Block_Promo_Quote_Grid class and add following code
protected function _prepareMassaction()
    {
        $this->setMassactionIdField('rule_id');
        $this->getMassactionBlock()->setFormFieldName('rule');

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


then override Mage_Adminhtml_Promo_QuoteController class and add following code
public function massDeleteAction()
    { //exit;
        $rulesIds = $this->getRequest()->getParam('rule');
        if(!is_array($rulesIds)) {
             Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select rule(s).'));
        } else {
            try {
                $model = Mage::getModel('salesrule/rule');
                foreach ($rulesIds as $rulesId) {
                //echo $rulesId; exit;
                    $model->load($rulesId);
                    $model->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('adminhtml')->__(
                        'Total of %d record(s) were deleted.', count($rulesIds)
                    )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }

        $this->_redirect('*/*/index');
    }

Thursday, September 27, 2012

Magento : Add massdelete action in magento admin grid

Consider module Bd_Demo and model Test

Add following lines in _prepareMassaction() method of admin grid file
protected function _prepareMassaction()
{
    $this->setMassactionIdField('id');
    $this->getMassactionBlock()->setFormFieldName('test');
    $this->getMassactionBlock()->addItem('delete', array(
         'label'    => Mage::helper('demo')->__('Delete'),
         'url'      => $this->getUrl('*/*/massDelete'),
         'confirm'  => Mage::helper('demo')->__('Are you sure?')
    ));
           
    return $this;
}

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

Magento : Add export CSV/XML functionality in magento admin grid

Consider module Bd_Demo and model Test, then
Add following lines in _prepareColumns() method of admin grid file
$this->addExportType('*/*/exportCsv', Mage::helper('demo')->__('CSV'));                              
$this->addExportType('*/*/exportXml', Mage::helper('demo')->__('XML'));

And add following methods in admin controller
public function exportCsvAction()
{
        $fileName   = 'test.csv';
        $content    = $this->getLayout()->createBlock('demo/adminhtml_test_grid')
            ->getCsv();

        $this->_sendUploadResponse($fileName, $content);
}

public function exportXmlAction()
{
        $fileName   = 'test.xml';
        $content    = $this->getLayout()->createBlock('demo/adminhtml_test_grid')
            ->getXml();

        $this->_sendUploadResponse($fileName, $content);
}

protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream')
{
        $response = $this->getResponse();
        $response->setHeader('HTTP/1.1 200 OK','');
        $response->setHeader('Pragma', 'public', true);
        $response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
        $response->setHeader('Content-Disposition', 'attachment; filename='.$fileName);
        $response->setHeader('Last-Modified', date('r'));
        $response->setHeader('Accept-Ranges', 'bytes');
        $response->setHeader('Content-Length', strlen($content));
        $response->setHeader('Content-type', $contentType);
        $response->setBody($content);
        $response->sendResponse();
        die;
}

Magento : Add custome button and remove button in magento admin grid

Consider module 'Bd_Demo' and Model 'Test'
Edit Grid Container File Test.php
public function __construct()
{
        $this->_controller = 'adminhtml_test';
        $this->_blockGroup = 'test';
        $this->_headerText = Mage::helper('demo')->__('Test Manager');
        $this->_addButtonLabel = Mage::helper('demo')->__('Add Test');


        $this->_addButton('button1', array(
            'label'     => Mage::helper('demo')->__('Button Label1'),
        'onclick'   => "setLocation('".$this->getUrl('*/*/button1')."')",
           
        ));

    $this->_removeButton('add'); // remove Add test button

        parent::__construct();
}

Note : button1Action() method must be in admin controller

Magento : How to use ajax in admin grid

Searching/paging and other operations to all work in admin grid using ajax, is built in functionality, only follow following step
In constructor of Grid.php file, add following lines   
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);

And in Grid.php add the function   
public function getGridUrl()
{
    return $this->getUrl('*/*/grid', array('_current'=>true));
}

And in your ControllerNameContrller.php add the action   
public function gridAction()
{
    $this->loadLayout();
    $this->getResponse()->setBody(
        $this->getLayout()->createBlock('modulename/adminhtml_modelname_grid')->toHtml()
    );
}