0

I have a custom grid. Need to upload product images from admin form of the custom module.

Here is the grid Edit file at:

app\code\local\Crud\Banner\Block\Adminhtml\Banner\Edit\Form.php

    $fieldset = $form->addFieldset('banner_form', array('legend'=>Mage::helper('banner')->__('Banner information')));

    $fieldset->addField('title', 'text', array(
        'label' => Mage::helper('banner')->__('Title'),
        'class' => 'required-entry',
        //'required' => true,
        //'readonly' => true,
        'name' => 'title',
    ));

    $fieldset->addField('filename', 'image', array(
        'label' => Mage::helper('banner')->__('Upload Banner'),
        'class' => 'required-entry',
        //'required' => true,
        //'readonly' => true,
        //'renderer' => 'banner/adminhtml_banner_renderer_image',
        'name' => 'filename',
    ));

    $fieldset->addField('status', 'select', array(
        'label' => Mage::helper('banner')->__('Status'),
        'name' => 'status',
        'values' => array(
            array(
                'value' => 1,
                'label' => Mage::helper('banner')->__('Desabilitado'),
            ),

            array(
                'value' => 0,
                'label' => Mage::helper('banner')->__('Habilitado'),
            ),
        ),
    ));

    if ( Mage::getSingleton('adminhtml/session')->getBannerData() )
    {
        $form->setValues(Mage::getSingleton('adminhtml/session')->getBannerData());
        Mage::getSingleton('adminhtml/session')->setBannerData(null);
    } elseif ( Mage::registry('banner_data') ) {
        $form->setValues(Mage::registry('banner_data')->getData());
    }

    $this->setForm($form);

    return parent::_prepareForm();
}

Here is the controller SaveAction:

public function saveAction()
{
    if ( $this->getRequest()->getPost() ) {
        try {
            $postData = $this->getRequest()->getPost();
            $bannerModel = Mage::getModel('banner/banner');

            $bannerModel->setId($this->getRequest()->getParam('id'))
            ->setTitle($postData['title'])
            ->setStatus($postData['status'])
            ->save();

            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Data was successfully saved'));
            Mage::getSingleton('adminhtml/session')->setBannerData(false);

            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            Mage::getSingleton('adminhtml/session')->setBannerData($this->getRequest()->getPost());
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            return;
        }
    }
    $this->_redirect('*/*/');
}
0

I have updated the save controller with image save code, Check the below code

public function saveAction()
{
    if ( $this->getRequest()->getPost() ) {
        try {
            $postData = $this->getRequest()->getPost();
            $bannerModel = Mage::getModel('banner/banner');

            if(isset($_FILES['filename']['name])) {
                try {
                    $uploader = new Varien_File_Uploader('filename');
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything
                    $uploader->setAllowRenameFiles(false);
                    $uploader->setFilesDispersion(false);
                    $path = Mage::getBaseDir('media') . / ;
                    $uploader->save($path, $_FILES['filename']['name']);
                    $postData['filename'] = $path.$_FILES['filename']['name'];
                }catch(Exception $e) {
                    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                }
            }

            $bannerModel->setId($this->getRequest()->getParam('id'))
            ->setTitle($postData['title'])
            ->setStatus($postData['status'])
            ->setStatus($postData['filename'])
            ->save();

            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Data was successfully saved'));
            Mage::getSingleton('adminhtml/session')->setBannerData(false);

            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            Mage::getSingleton('adminhtml/session')->setBannerData($this->getRequest()->getPost());
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            return;
        }
    }
    $this->_redirect('*/*/');
}

Hope it will work for you.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.