Magento后台模块示例(2)

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

<?xml version="1.0"?>
<layout version="0.1.0">
    <count_adminhtml_count_index>
        <reference name="content">
            <block type="count/adminhtml_count" name="count" />
        </reference>
    </count_adminhtml_count_index>
</layout>

贴图看下后台的效果:
图片描述

后台控制器的位置:
controllers/Adminhtml/CountController.php

<?php

class Message_Count_Adminhtml_CountController extends Mage_Adminhtml_Controller_action
{

    protected function _initAction() {
        $this->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

<?php
class Message_Count_Block_Adminhtml_Count extends Mage_Adminhtml_Block_Widget_Grid_Container
{
  public function __construct()
  {
    $this->_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

<?php
//此页面控制后台表单的信息输出
class Message_Count_Block_Adminhtml_Count_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
  public function __construct()
  {
      parent::__construct();
      $this->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

<?php
//Grid表单页面
class Message_Count_Block_Adminhtml_Count_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        parent::__construct();
                 
        $this->_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

<?php

class Message_Count_Block_Adminhtml_Count_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{

  public function __construct()
  {
      parent::__construct();
      $this->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

<?php

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

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

<?php
//这个页面是控制点开列表进行编辑的页面
class Message_Count_Block_Adminhtml_Count_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
  protected function _prepareForm()
  {
      $form = new Varien_Data_Form();
      $this->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();
  }
}

图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值