Magento 新闻模块开发教程 (九) 建立Block

前面八章中我们已经先后建立了文件骨架,激活文件,前后端控制器,配置文件,辅助类,模型,SQL,模板,新闻模块开发的最后一步是创建block

在app\code\local\Voodoo\News\Block中创建News.php文件:

<?php
class Voodoo_News_Block_News extends Mage_Core_Block_Template
{
	public function _prepareLayout()
	{
		return parent::_prepareLayout();
	}
	public function getNews()
	{
		if (!$this->hasData('news')) {
			$this->setData('news', Mage::registry('news'));
		}
		return $this->getData('news');
	}
	public function getNewsList()
	{
		if (!$this->hasData('list')) {
			$this->setData('list', Mage::registry('list'));
		}
		return $this->getData('list');
	}
	function limitCharacter($string, $limit = 20, $suffix = ' . . .')
    {
		$string = strip_tags($string);
		if (strlen($string) < $limit) {
			return $string;
		}
		for ($i = $limit; $i >= 0; $i--) {
		$c = $string[$i];
		if ($c == ' ' OR $c == "\n") {
			return substr($string, 0, $i) . $suffix;
		}
		}
		return substr($string, 0, $limit) . $suffix;
	}
}

同样在app\code\local\Voodoo\News\Block\Adminhtml下,建立News.php文件:

class Voodoo_News_Block_Adminhtml_News extends Mage_Adminhtml_Block_Widget_Grid_Container
{
	public function __construct()
	{
		$this->_controller = 'adminhtml_news';
		$this->_blockGroup = 'news';
		$this->_headerText = Mage::helper('news')->__('Item Manager');
		$this->_addButtonLabel = Mage::helper('news')->__('Add Item');
		parent::__construct();
	}
}

app\code\local\Voodoo\News\Block\Adminhtml\News中建立Edit.php文件:

<?php
class Voodoo_News_Block_Adminhtml_News_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_blockGroup = 'news';
$this->_controller = 'adminhtml_news';
$this->_updateButton('save', 'label', Mage::helper('news')->__('Save Item'));
$this->_updateButton('delete', 'label',Mage::helper('news')->__('Delete Item'));
$this->_addButton('saveandcontinue', array('label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save',
), -100);
$this->_formScripts[] = "function toggleEditor() {
if (tinyMCE.getInstanceById('news_content') == null) {
tinyMCE.execCommand('mceAddControl', false, 'news_content');
} else {
tinyMCE.execCommand('mceRemoveControl', false, 'news_content');
}
}
function saveAndContinueEdit(){
editForm.submit($('edit_form').action+'back/edit/');
}
";
}
public function getHeaderText()
{
if (Mage::registry('news_data') && Mage::registry('news_data')->getId()) {
return Mage::helper('news')->__("Edit Item '%s'", $this->htmlEscape(Mage::registry('news_data')->getTitle()));
} else {
return Mage::helper('news')->__('Add Item');
}
}
}

同样目录下继续建立Grid.php文件:

<?php
class Voodoo_News_Block_Adminhtml_News_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('newsGrid');
$this->setDefaultSort('news_id');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getModel('news/news')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('news_id', array(
'header' => Mage::helper('news')->__('ID'),
'align' => 'right',
'width' => '50px',
'index' => 'news_id',
));
$this->addColumn('title', array(
'header' => Mage::helper('news')->__('Title'),
'align' => 'left',
'index' => 'title',
));
$this->addColumn('status', array(
'header' => Mage::helper('news')->__('Status'),
'align' => 'left',
'width' => '80px',
'index' => 'status',
'type' => 'options',
    'options' => array(
1 => 'Enabled',
2 => 'Disabled',
),
));
$this->addColumn('action',
array(
'header' => Mage::helper('news')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('news')->__('Edit'),
'url' => array('base' => '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
$this->addExportType('*/*/exportCsv',
Mage::helper('news')->__('CSV'));
$this->addExportType('*/*/exportXml',
Mage::helper('news')->__('XML'));
return parent::_prepareColumns();
}
protected function _prepareMassaction()
{
$this->setMassactionIdField('news_id');
$this->getMassactionBlock()->setFormFieldName('news');
$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('news')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('news')->__('Are you sure?')
        ));
$statuses = Mage::getSingleton('news/status')->getOptionArray();
array_unshift($statuses, array('label' => '', 'value' =>
''));
$this->getMassactionBlock()->addItem('status', array(
'label' => Mage::helper('news')->__('Change status'),
'url' => $this->getUrl('*/*/massStatus', array('_current'
=> true)),
'additional' => array(
'visibility' => array(
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('news')->__('Status'),
'values' => $statuses
)
)
));
return $this;
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
}

app\code\local\Voodoo\News\Block\Adminhtml\News\Edit下创建Form.php和Tabs.php:

<?php
class Voodoo_News_Block_Adminhtml_News_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$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);
return parent::_prepareForm();
}
}

<?php
class Voodoo_News_Block_Adminhtml_News_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
	public function __construct()
	{
	parent::__construct();
	$this->setId('news_tabs');
	$this->setDestElementId('edit_form');
	$this->setTitle(Mage::helper('news')->__('Item Information'));
	}
	
	protected function _beforeToHtml()
	{
	$this->addTab('form_section', array(
	'label' => Mage::helper('news')->__('Item Information'),
	'title' => Mage::helper('news')->__('Item Information'),
	'content' => $this->getLayout()->createBlock('news/adminhtml_news_edit_tab_form')->toHtml(),));
	return parent::_beforeToHtml();
	}
}

最后在app\code\local\Voodoo\News\Block\Adminhtml\News\Edit\Tab下建立Form.php

<?php
class Voodoo_News_Block_Adminhtml_News_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
	protected function _prepareForm()
	{
		$form = new Varien_Data_Form();
		$this->setForm($form);
		$fieldset = $form->addFieldset('news_form', array('legend'=> Mage::helper('news')->__('Item information')));
		$fieldset->addField('title', 'text', array('label' => Mage::helper('news')->__('Title'),'class' => 'required-entry','required' => true,'name' => 'title',));
		$fieldset->addField('filename', 'file', array('label' => Mage::helper('news')->__('File'),'required' => false,'name' => 'filename',));
		$fieldset->addField('status', 'select', array('label' => Mage::helper('news')->__('Status'),'name' => 'status','values' => array(array('value' => 1,'label' => Mage::helper('news')->__('Enabled'),),array('value' => 2,'label' => Mage::helper('news')->__('Disabled'),),),));
		$fieldset->addField('content', 'editor', array('name' => 'content','label' => Mage::helper('news')->__('Content'),'title' => Mage::helper('news')->__('Content'),'style' => 'width:700px; height:500px;','wysiwyg' => false,'required' => true,));
		if (Mage::getSingleton('adminhtml/session')->getNewsData())
		{
		$form->setValues(Mage::getSingleton('adminhtml/session')->getNewsData());
		Mage::getSingleton('adminhtml/session')->setNewsData(null);
		} elseif (Mage::registry('news_data')) {
		$form->setValues(Mage::registry('news_data')->getData());
	}
		return parent::_prepareForm();
	}
}

这样就完成了模块的创建。block是Magento的重要部分,这里我们创建了7个新闻模块功能,我们写了一些方法,并且将数据传递到布局。

这样我们便完成了新闻模块的开发,这是后台截图:

这是前台:


好了,新闻模块的教材就到这里,有什么问题可以和我交流,大家共同进步!


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值