Magento - Add Custom Image Field To Cms Page Edit Form

1. rewrite adminhtml edit form blocks in config file

 

<global>
	<blocks>
	  <mycms>
		<class>Bysoft_Mycms_Block</class>
	  </mycms>
	  <adminhtml>
		<rewrite>
                    <cms_page_edit_form>Bysoft_Mycms_Block_Adminhtml_Cms_Page_Edit_Form</cms_page_edit_form>
		</rewrite>
	  </adminhtml>
	</blocks>

 2. write rewrite block to change form post type: ( just extends Mage_Adminhtml_Block_Widget_Form)

<?php
class Bysoft_Mycms_Block_Adminhtml_Cms_Page_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
	protected function _prepareForm()
	{
		
		$form = new Varien_Data_Form(array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post', 'enctype' => 'multipart/form-data'));
		$form->setUseContainer(true);
		$this->setForm($form);
		
		return parent::_prepareForm();
	}
}

 3. set two event method for prepare form and save upload file in config.xml

 <global>
<models>
	  <mycms>
		<class>Bysoft_Mycms_Model</class>
		<resourceModel>mycms_mysql4</resourceModel>
	  </mycms>
	  <mycms_mysql4>
		<class>Bysoft_Mycms_Model_Mysql4</class>
	  </mycms_mysql4>
	</models>
	<resources>
	  <mycms_setup>
		<setup>
		  <module>Bysoft_Mycms</module>
		</setup>
		<connection>
		  <use>core_setup</use>
		</connection>
	  </mycms_setup>
	  <mycms_write>
		<connection>
		  <use>core_write</use>
		</connection>
	  </mycms_write>
	  <mycms_read>
		<connection>
		  <use>core_read</use>
		</connection>
	  </mycms_read>
	</resources> 
  </global>
<adminhtml>
     <events>
       <cms_page_prepare_save>
        <observers>
            <bysoft_mycms_save_page>
                <type>singleton</type>
                <class>mycms/observer</class>
                <method>saveSearchImage</method>
            </bysoft_mycms_save_page>
        </observers>
      </cms_page_prepare_save>
      <adminhtml_cms_page_edit_tab_main_prepare_form>
        <observers>
            <bysoft_mycms_prepare_form>
                <type>singleton</type>
                <class>mycms/observer</class>
                <method>prepareSearchImageForm</method>
            </bysoft_mycms_prepare_form>
        </observers>
      </adminhtml_cms_page_edit_tab_main_prepare_form>
    </events>
</adminhtml>

 

4. add custom image column to core cms page table by setup script

<?php
$installer = $this;
$installer->startSetup();
$sql=<<<SQLTEXT

SQLTEXT;

$installer->run($sql);
$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
 
$installer->run(' ALTER TABLE `cms_page` ADD `search_image` VARCHAR(255) NULL; ');
 
$installer->endSetup();
$installer->endSetup();
	 

 5. two function in observer script:

    1) prepare edit form, add image field set and field

    2) save image data

<?php
class Bysoft_Mycms_Model_Observer
{
	public function saveSearchImage(Varien_Event_Observer $observer)
	{
		try {
			$model = $observer->getEvent()->getPage();
			$request = $observer->getEvent()->getRequest();
			
			if (isset($_FILES['search_image']['name']) && $_FILES['search_image']['name'] != '') {
				$uploader = new Varien_File_Uploader('search_image');
			
				$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
				$uploader->setAllowRenameFiles(false);
				$uploader->setFilesDispersion(false);
			
				// Set media as the upload dir
				$media_path  = Mage::getBaseDir('media') . DS . 'search_image' . DS;
			
				// Set thumbnail name
				$file_name = 'cms_';
				
				$rand_file_name = $file_name . $this->get_rand_filename($_FILES['search_image']['name']);
			
				// Upload the image
				$uploader->save($media_path, $rand_file_name);
			
				$data['search_image'] = 'search_image' . '/' . $rand_file_name;
			
				// Set thumbnail name
				$data['search_image'] = $data['search_image'];
				$model->setData('search_image',$data['search_image']);
			} else {
				$data = $request->getPost();
				if(isset($data['search_image']['delete']) && $data['search_image']['delete']== 1) {
					$data['search_image'] = '';
					$model->setData('search_image',$data['search_image']);
				} else {
					unset($data['search_image']);
					$model->setData('search_image',implode($request->getPost('search_image')));
				}
			}
		}catch (Exception $e) {
			echo $e->getMessage();
		}
	}
		
	public function prepareSearchImageForm(Varien_Event_Observer $observer)
	{ 
		
		$form = $observer->getEvent()->getForm();
		
		$fieldset = $form->addFieldset(
				'image_fieldset',
				array(
						'legend' => 'Image',
						'class' => 'fieldset-wide'
				)
		);
		
		$fieldset->addField('search_image', 'image', array(
				'name' => 'search_image',
				'label' => 'Search image',
				'title' => 'Search image'
		));
		
	}
	
	public function get_rand_filename($base_file_name) {
		$ext = pathinfo($base_file_name, PATHINFO_EXTENSION);
		return Mage::getModel('core/date')->timestamp(time()) . '.' . $ext;
	}
			
	
		
 }

 

你可能感兴趣的:(image)