magento 1.9编辑表单不显示缩略图图像

时间:2015-02-27 05:30:29

标签: magento-1.9

我正在尝试在管理员自定义模块中的EDIT表单上显示缩略图图像。 我收到复选框以删除图像。 我正在得到一个应该显示图像的方框。 但它没有显示图像。

请建议我解决方案

我在控制器中的save()动作是

public function saveAction()
    {
        if ($data = $this->getRequest()->getPost())
        {
				if(isset($_FILES['shop_image']['name']) && $_FILES['shop_image']['name'] != '') 
				{ 
					try {   
							/* Starting upload */   
							$uploader = new Varien_File_Uploader('shop_image');
		
							// Any extention would work
							$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
							$uploader->setAllowRenameFiles(false);
		
							
							$uploader->setFilesDispersion(false);
							
							echo $path = Mage::getBaseDir('media').DS.'ud'.DS;
							
							
							
							$uploader->save($path, $_FILES['shop_image']['name'] );
		
						} catch (Exception $e) {
		
						}
				  		//  echo $path;exit;
						//this way the name is saved in DB

            			$data['shop_image'] = $path.$_FILES['shop_image']['name'];
        		}
				else if((isset($data['shop_image']['delete']) && $data['shop_image']['delete'] == 1)){
						 .'ud'.DS.$data['shop_image']['value'];
						unlink($data['shop_image']['value']);
						//set path to null and save to database
						$data['shop_image'] = '';
					
						
				}
				$model = Mage::getModel('shop/shop');
            $id = $this->getRequest()->getParam('id');
            if ($id) {
                $model->load($id);
            }
            $model->setData($data);
 
			 Mage::getSingleton('adminhtml/session')->setFormData($data);
					
            try {
                if ($id) {
                    $model->setId($id);
                }
                $model->save();
 
                if (!$model->getId()) {
                    Mage::throwException(Mage::helper('shop')->__('Error saving shop'));
                }
 
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('shop')->__('Shop was successfully saved.'));
                Mage::getSingleton('adminhtml/session')->setFormData(false);
 
                // The following line decides if it is a "save" or "save and continue"
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/');
                }
 
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                if ($model && $model->getId()) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/');
                }
            }
 
            return;
		
           
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('shop')->__('No data found to save'));
        $this->_redirect('*/*/');
    }

我的form.php是

<?php
 
class Sample_Shop_Block_Adminhtml_Shop_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        if (Mage::getSingleton('adminhtml/session')->getExampleData())
        {
            $data = Mage::getSingleton('adminhtml/session')->getExamplelData();
            Mage::getSingleton('adminhtml/session')->getExampleData(null);
        }
        elseif (Mage::registry('example_data'))
        {
            $data = Mage::registry('example_data')->getData();
        }
        else
        {
            $data = array();
        }
 
        $form = new Varien_Data_Form(array(
                'id' => 'edit_form',
                'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                'method' => 'post',
                'enctype' => 'multipart/form-data',
        ));
 
        $form->setUseContainer(true);
 
        $this->setForm($form);
 
        $fieldset = $form->addFieldset('example_form', array(
             'legend' =>Mage::helper('shop')->__('Shop Information')
        ));
 
        $fieldset->addField('shopname', 'text', array(
             'label'     => Mage::helper('shop')->__('Shop Name'),
             'class'     => 'required-entry',
             'required'  => true,
             'name'      => 'shopname',
             'note'     => Mage::helper('shop')->__('The name of the shop.'),
        ));
 
        $fieldset->addField('logo', 'text', array(
             'label'     => Mage::helper('shop')->__('Logo'),
             'class'     => 'required-entry',
             'required'  => true,
             'name'      => 'logo',
        ));
 
        $fieldset->addField('productimage', 'text', array(
             'label'     => Mage::helper('shop')->__('Product Image'),
             'class'     => 'required-entry',
             'required'  => true,
             'name'      => 'productimage',
        ));
		
		$fieldset->addField('state', 'text', array(
             'label'     => Mage::helper('shop')->__('State'),
             'class'     => 'required-entry',
             'required'  => true,
             'name'      => 'state',
        ));
		
		$fieldset->addField('shop_image','image', array(
		'label' => Mage::helper('shop')->__('Shop Image'),
		'required' => false,
		'name' => 'shop_image', 
		));
		
		 


        $form->setValues($data);
 
        return parent::_prepareForm();
    }
}

1 个答案:

答案 0 :(得分:0)

找到以下简单步骤来显示缩略图图像。 在您的编辑 - &gt; tab-&gt;表单文件中添加以下代码 例如:Package_Campaign_Block_Adminhtml_Campaign_Edit_Tab_Form

$fieldset->addType('image','Package_Campaign_Block_Adminhtml_Campaign_Helper_Image');

创建下面的帮助程序类文件以显示缩略图图像。

class Package_Campaign_Block_Adminhtml_Campaign_Edit_Tab_Form extends Varien_Data_Form_Element_Abstract{
    public function __construct($data) {
    parent::__construct($data);
    $this->setType('file');
}


public function getElementHtml() {
    $html = '';
    if ($this->getValue()) {           
        $media = Mage::getBaseUrl('media').'campaign/';
        $html = '<a onclick="imagePreview(\''.$this->getHtmlId().'_image\'); return false;" href="'.$this->getValue().'"><img id="'.$this->getHtmlId().'_image" style="border: 1px solid #d6d6d6;" title="'.$this->getValue().'" src="'.$media.$this->getValue().'" alt="'.$this->getValue().'" width="30"></a> ';
    }
    $this->setClass('input-file');
    $html.= parent::getElementHtml();
    return $html;
} 
}