magento新闻模块开发(四)

media目录要777权限,才能上传成功

修改 /app/code/local/Xinson/News/etc/config.xml

<modules>
    <Xinson_News>
        <version>0.1.1</version>
    </Xinson_News>
</modules>

数据库更新文件

  /app/code/local/Xinson/News/sql/news_setup/mysql4-upgrade-0.1.0-0.1.1.php

<?php
/* @var Mage_Core_Model_Resource_Setup $this */
$this->run("
ALTER TABLE `news` ADD `filename` VARCHAR (50) NOT NULL DEFAULT '' AFTER `title`;
");

修改编辑区域

/app/code/local/Xinson/News/Block/Adminhtml/News/Edit/Tab/Form.php

<?php

class Xinson_News_Block_Adminhtml_News_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $model = Mage::registry('news_data');

        $form = new Varien_Data_Form();

        $fieldset = $form->addFieldset('news_form', array('legend'=>Mage::helper('news')->__('News information')));

        if ($model->getNewsId()) {
            $fieldset->addField('news_id', 'hidden', array(
                'name' => 'news_id',
            ));
        }
        $fieldset->addField('title', 'text', array(
            'name' => 'title',
            'label' => Mage::helper('news')->__('News Title'),
            'title' => Mage::helper('news')->__('News Title'),
            'required' => true,
        ));

        $fieldset->addField('is_active', 'select', array(
            'name' => 'is_active',
            'label' => Mage::helper('news')->__('Status'),
            'title' => Mage::helper('news')->__('News Status'),
            'required' => true,
            'options' => $model->getAvailableStatuses(),
        ));
        
        //新加的
        //小图路径
        $fieldset->addType('image', Mage::getConfig()->getBlockClassName('news/adminhtml_news_helper_image'));
        //输入框
        $fieldset->addField('filename', 'image', array(
            'label'     => Mage::helper('news')->__('File'),
            'required'  => false,
            'name'      => 'filename',
        ));

        $fieldset->addField('content', 'editor', array(
            'name' => 'content',
            'label' => Mage::helper('news')->__('Content'),
            'title' => Mage::helper('news')->__('Content'),
            'required' => true,
            'style' => 'width:700px; height:300px;',
        ));


        $form->setValues($model->getData());
        $this->setForm($form);

        return parent::_prepareForm();
    }
}


修改config配置文件(上传文件保存的目录)

<config>
   ......

    <default>
        <news>
            <general>
                <image_folder>news</image_folder>
                <news>no_image.jpg</news>
            </general>
        </news>
    </default>

</config>


加Image文件重写原来Image,/app/code/local/Xinson/News/Block/Adminhtml/News/Helper/Image.php

<?php


class Xinson_News_Block_Adminhtml_News_Helper_Image extends Varien_Data_Form_Element_Image
{
    protected function _getUrl()
    {
        $url = false;
        if ($this->getValue()) {
            $url = Mage::getBaseUrl('media') . trim(Mage::getStoreConfig('news/general/image_folder'), ' /\\') . '/' . $this->getValue();
        }
        return $url;
    }
}


/app/code/local/Xinson/News/Model/Mysql4/News.php 模型添加保存方法

//在保存前操作
protected function _beforeSave(Mage_Core_Model_Abstract $object)
{

    /* image */
    $value = $object->getFilename();
    $path = Mage::getBaseDir('media') . DS . trim(Mage::getStoreConfig('news/general/image_folder'), ' /\\') . DS;

    if (is_array($value) && !empty($value['delete'])) {
        @unlink($path . $value['value']);
        $object->setFilename('');
        return $this;
    }

    if (empty($_FILES['filename']['name'])) {
        if (is_array($value)) {
            $object->setFilename($value['value']);
        }
        return $this;
    }

    try {
        $uploader = new Varien_File_Uploader('filename');
        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png', 'bmp'));
        $uploader->setAllowRenameFiles(true);
        $uploader->save($path);
        $object->setFilename($uploader->getUploadedFileName());
    } catch (Exception $e) {
        $object->unsFilename();
        throw $e;
        //return $this;
    }
    return $this;
}


源码 http://share.weiyun.com/f4bf2dbd350e9364839da01c18470cb9

你可能感兴趣的:(magento新闻模块开发(四))