博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Magento后台模块示例(2)
阅读量:7071 次
发布时间:2019-06-28

本文共 15572 字,大约阅读时间需要 51 分钟。

写好前台部分,现在要做的就是要写后台部分,配置文件在前面已经提前写出来了,但还需要说明的是,后台部分的配置文件,在design/adminhtml/default/default/layout/下,还需多写一个配置文件,count.xml

贴图看下后台的效果:

图片描述

后台控制器的位置:

controllers/Adminhtml/CountController.php

loadLayout() ->_setActiveMenu('count/items') ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager')); return $this; } public function indexAction() { $this->_initAction() ->renderLayout(); } public function editAction() { $id = $this->getRequest()->getParam('id'); $model = Mage::getModel('count/count')->load($id); if ($model->getId() || $id == 0) { $data = Mage::getSingleton('adminhtml/session')->getFormData(true); if (!empty($data)) { $model->setData($data); } Mage::register('count_data', $model); $this->loadLayout(); $this->_setActiveMenu('count/items'); $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager')); $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News')); $this->getLayout()->getBlock('head')->setCanLoadExtJs(true); $this->_addContent($this->getLayout()->createBlock('count/adminhtml_count_edit')) ->_addLeft($this->getLayout()->createBlock('count/adminhtml_count_edit_tabs')); $this->renderLayout(); } else { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('count')->__('Item does not exist')); $this->_redirect('*/*/'); } } public function newAction() { $this->_forward('edit'); } //存储数据 public function saveAction() { if ($data = $this->getRequest()->getPost()) { $model = Mage::getModel('count/count'); $model->setData($data) ->setId($this->getRequest()->getParam('id')); try { if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) { $model->setCreatedTime(now()) ->setUpdateTime(now()); } else { $model->setUpdateTime(now()); } $model->save(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('count')->__('Item was successfully saved')); Mage::getSingleton('adminhtml/session')->setFormData(false); if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('id' => $model->getId())); return; } $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); Mage::getSingleton('adminhtml/session')->setFormData($data); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); return; } } Mage::getSingleton('adminhtml/session')->addError(Mage::helper('count')->__('Unable to find item to save')); $this->_redirect('*/*/'); } public function deleteAction() { if( $this->getRequest()->getParam('id') > 0 ) { try { $model = Mage::getModel('count/count'); $model->setId($this->getRequest()->getParam('id')) ->delete(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted')); $this->_redirect('*/*/'); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); } } $this->_redirect('*/*/'); } public function massDeleteAction() { $webIds = $this->getRequest()->getParam('count'); if(!is_array($webIds)) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)')); } else { try { foreach ($webIds as $webId) { $count = Mage::getModel('count/count')->load($webId); $count->delete(); } Mage::getSingleton('adminhtml/session')->addSuccess( Mage::helper('adminhtml')->__( 'Total of %d record(s) were successfully deleted', count($webIds) ) ); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } $this->_redirect('*/*/index'); } public function massStatusAction() { $webIds = $this->getRequest()->getParam('count'); if(!is_array($webIds)) { Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)')); } else { try { foreach ($webIds as $webId) { $count = Mage::getSingleton('count/count') ->load($webId) ->setStatus($this->getRequest()->getParam('status')) ->setIsMassupdate(true) ->save(); } $this->_getSession()->addSuccess( $this->__('Total of %d record(s) were successfully updated', count($webIds)) ); } catch (Exception $e) { $this->_getSession()->addError($e->getMessage()); } } $this->_redirect('*/*/index'); }}

控制器写好之后,来看看后台的block文件夹怎么写,

Block |---Count.php |---Adminhtml        |----Count.php        |----Count               |----Edit.php               |----Grid.php               |----Edit                      |---Tabs.php                      |---Form.php                      |---Tab                           |---Form.php

Block/Adminhtml/Count.php

_controller = 'adminhtml_count'; $this->_blockGroup = 'count'; $this->_headerText = Mage::helper('count')->__('Count Manager'); $this->_addButtonLabel = Mage::helper('count')->__('Add Item'); parent::__construct(); }}

Block/Adminhtml/Count/Grid.php

setId('countGrid'); $this->setDefaultSort('count_id'); $this->setDefaultDir('ASC'); $this->setSaveParametersInSession(true); } protected function _prepareCollection() { $collection = Mage::getModel('count/count')->getCollection(); $this->setCollection($collection); return parent::_prepareCollection(); } protected function _prepareColumns() { $this->addColumn('count_id', array( 'header' => Mage::helper('count')->__('ID'), 'align' =>'right', //'width' => '50px', 'index' => 'count_id', )); $this->addColumn('Model_Name', array( 'header' => Mage::helper('count')->__('Model Name'), 'align' =>'left', 'index' => 'Model_Name', )); $this->addColumn('Name', array( 'header' => Mage::helper('count')->__('Name'), 'align' =>'left', 'index' => 'Name', )); $this->addColumn('Serial_Number', array( 'header' => Mage::helper('count')->__('Serial Number'), 'align' =>'left', 'index' => 'Serial_Number', )); $this->addColumn('Email', array( 'header' => Mage::helper('count')->__('Email'), 'align' =>'left', 'index' => 'Email', )); $this->addColumn('Date_Purchased', array( 'header' => Mage::helper('count')->__('Data Purchased'), 'align' =>'left', 'index' => 'Date_Purchased', )); $this->addColumn('Messages_Source', array( 'header' => Mage::helper('count')->__('Messages Source'), 'align' =>'left', 'index' => 'Messages_Source', )); $this->addColumn('Content', array( 'header' => Mage::helper('count')->__('Content'), 'align' =>'left', 'index' => 'Content', )); $this->addColumn('action', array( 'header' => Mage::helper('count')->__('Action'), 'width' => '100', 'type' => 'action', 'getter' => 'getId', 'actions' => array( array( 'caption' => Mage::helper('count')->__('Edit'), 'url' => array('base'=> '*/*/edit'), 'field' => 'id' ) ), 'filter' => false, 'sortable' => false, 'index' => 'stores', 'is_system' => true, )); return parent::_prepareColumns(); } protected function _prepareMassaction() { $this->setMassactionIdField('count_id'); $this->getMassactionBlock()->setFormFieldName('count'); $this->getMassactionBlock()->addItem('delete', array( 'label' => Mage::helper('count')->__('Delete'), 'url' => $this->getUrl('*/*/massDelete'), 'confirm' => Mage::helper('count')->__('Are you sure?') )); return $this; } public function getRowUrl($row) { return $this->getUrl('*/*/edit', array('id' => $row->getId())); }}

图片描述

Block/Adminhtml/Count/Edit.php

_objectId = 'id'; $this->_blockGroup = 'count'; $this->_controller = 'adminhtml_count'; $this->_updateButton('save', 'label', Mage::helper('count')->__('Save Item')); $this->_updateButton('delete', 'label', Mage::helper('count')->__('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('count_content') == null) { tinyMCE.execCommand('mceAddControl', false, 'count_content'); } else { tinyMCE.execCommand('mceRemoveControl', false, 'count_content'); } } function saveAndContinueEdit(){ editForm.submit($('edit_form').action+'back/edit/'); } "; } public function getHeaderText() { if( Mage::registry('count_data') && Mage::registry('count_data')->getId() ) { return Mage::helper('count')->__("Edit Item '%s'", $this->htmlEscape(Mage::registry('count_data')->getTitle())); } else { return Mage::helper('count')->__('Add Item'); } }}

Block/Adminhtml/Count/Edit/Tabs.php

setId('count_tabs'); $this->setDestElementId('edit_form'); $this->setTitle(Mage::helper('count')->__('Item Information')); } protected function _beforeToHtml() { $this->addTab('form_section', array( 'label' => Mage::helper('count')->__('Item Information'), 'title' => Mage::helper('count')->__('Item Information'), 'content' => $this->getLayout()->createBlock('count/adminhtml_count_edit_tab_form')->toHtml(), )); return parent::_beforeToHtml(); }}

Block/Adminhtml/Count/Edit/Form.php

'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(); }}

Block/Adminhtml/Count/Edit/Tab/Form.php

setForm($form); $fieldset = $form->addFieldset('count_form', array('legend'=>Mage::helper('count')->__('Item information'))); $fieldset->addField('Model_Name', 'text', array( 'label' => Mage::helper('count')->__('Model_Name'), // 'class' => 'required-entry', 'required' => true, 'name' => 'Model_Name', )); $fieldset->addField('Name', 'text', array( 'label' => Mage::helper('count')->__('Name'), // 'class' => 'required-entry', 'required' => true, 'name' => 'Name', )); $fieldset->addField('Serial_Number', 'text', array( 'label' => Mage::helper('count')->__('Serial_Number'), // 'class' => 'required-entry', 'required' => true, 'name' => 'Serial_Number', )); $fieldset->addField('Email', 'text', array( 'label' => Mage::helper('count')->__('Email'), //'class' => 'required-entry', 'required' => true, 'name' => 'Email', )); $fieldset->addField('Date_Purchased', 'text', array( 'label' => Mage::helper('count')->__('Date_Purchased'), // 'class' => 'required-entry', 'required' => true, 'name' => 'Data_Purchased', )); $fieldset->addField('Messages_Source', 'select', array( 'label' => Mage::helper('count')->__('Messages_Source'), //'class' => 'required-entry', 'required' => true, 'name' => 'Messages_Source', 'values' => array( array( 'value' => 1, 'label' => Mage::helper('count')->__('web Search'), ), array( 'value' => 2, 'label' => Mage::helper('count')->__('Online Review'), ), array( 'value' => 3, 'label' => Mage::helper('count')->__('Online Ad'), ), array( 'value' => 4, 'label' => Mage::helper('count')->__('Other'), ), ), )); $fieldset->addField('Content', 'text', array( 'label' => Mage::helper('count')->__('Content'), 'title' => Mage::helper('count')->__('Content'), 'class' => 'required-entry', 'required' => true, 'style' => 'width:278px; height:60px;', 'name' => 'Content', 'wysiwyg' => false, )); if ( Mage::getSingleton('adminhtml/session')->getcountData() ) { $form->setValues(Mage::getSingleton('adminhtml/session')->getcountData()); Mage::getSingleton('adminhtml/session')->setcountData(null); } elseif ( Mage::registry('count_data') ) { $form->setValues(Mage::registry('count_data')->getData()); } return parent::_prepareForm(); }}

图片描述

转载地址:http://muell.baihongyu.com/

你可能感兴趣的文章
JAVA使用POI读取EXCEL文件的简单model
查看>>
Docker创建centos的LNMP镜像
查看>>
lintcode:Wiggle Sort
查看>>
iOS彩票项目--第二天,自定义蒙版、封装活动菜单、自定义pop菜单
查看>>
[Unity3D]Unity3D游戏开发之角色控制漫谈
查看>>
git branch merge到master
查看>>
EJB--事务管理 .
查看>>
在vmware里面免费安装纯净的xp虚拟机
查看>>
什么是RESTfull?理解RESTfull架构【转】
查看>>
linux lsof命令详解
查看>>
MySQL中concat函数
查看>>
代理模式
查看>>
Linux命令 cat命令
查看>>
poj1007 逆序数 排序
查看>>
周末轻松话卷积(上)
查看>>
【转】对C# 中堆栈,堆,值类型,引用类型的理解
查看>>
perl脚本调用
查看>>
gcc 0长数组学习
查看>>
经方时方接轨之――茵陈蒿汤合甘露饮
查看>>
MATLAB中取整函数(fix, floor, ceil, round)的使用
查看>>