Magento Adminhtml - 如何创建新的管理表单

时间:2015-02-11 21:03:37

标签: php magento adminhtml

我正在使用Magento 1.9.0.1,现在我正在开发一个新的magento扩展。

到目前为止,我已经使用从自定义MySQL表中获取数据的网格表创建了新的adminhtml页面:

以下是页面:

![在此处输入图片说明] [1]

此页面从自定义MySQL表VivasIndustries_SmsNotification获取数据,这里是它的结构:

![在此输入图片说明] [2]

让我告诉你我的扩展文件:

我在:/app/code/community/VivasIndustries/SmsNotification/etc/config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <VivasIndustries_SmsNotification>
      <version>0.1.0</version>
    </VivasIndustries_SmsNotification>
  </modules>
  <global>
    <models>
        <smsnotification>
            <class>VivasIndustries_SmsNotification_Model</class>
            <resourceModel>vivasindustries_smsnotification_resource</resourceModel>
        </smsnotification>
        <vivasindustries_smsnotification_resource>
        <class>VivasIndustries_SmsNotification_Model_Resource</class>
        <entities>
            <smsnotification>
            <table>VivasIndustries_SmsNotification</table>
            </smsnotification>
        </entities>
        </vivasindustries_smsnotification_resource>
    </models>
    <resources>
        <smsnotification_setup>
            <setup>
                <module>VivasIndustries_SmsNotification</module>
            </setup>
            <connection>
                 <use>core_setup</use>
             </connection>
        </smsnotification_setup>
        <smsnotification_read>
            <connection>
                <use>core_read</use>
            </connection>
        </smsnotification_read>
        <smsnotification_write>
            <connection>
                <use>core_write</use>
            </connection>
        </smsnotification_write>
    </resources>    
    <events>
        <sales_order_save_after>
            <observers>
                <vivasindustries_smsnotification>
                    <class>smsnotification/observer</class>
                    <method>orderSaved</method>
                </vivasindustries_smsnotification>
            </observers>
        </sales_order_save_after>
    </events>
    <helpers>
        <smsnotification>
            <class>VivasIndustries_SmsNotification_Helper</class>
        </smsnotification>
    </helpers>
    <blocks>
        <smsnotification>
             <class>VivasIndustries_SmsNotification_Block</class>
        </smsnotification>
    </blocks>
  </global>
  <adminhtml>
    <acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <vivas>
                                        <title>Vivas - All</title>
                                    </vivas>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <VivasIndustries_SmsNotification before="Mage_Adminhtml">VivasIndustries_SmsNotification_Adminhtml</VivasIndustries_SmsNotification>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>  

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status.php:

<?php

class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_blockGroup = 'smsnotification';
        $this->_controller = 'adminhtml_sms_status';
        $this->_headerText = Mage::helper('smsnotification')->__('Send SMS on Order Status Changes');
        $this->_addButtonLabel = Mage::helper('smsnotification')->__('Create new SMS Rule');
        parent::__construct();
    }

    protected function _prepareLayout()
    {
        $this->setChild( 'grid',
            $this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
                $this->_controller . '.grid')->setSaveParametersInSession(true) );
        return parent::_prepareLayout();
    }



}

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Grid.php:

<?php

class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('smsnotification_grid');
        $this->setDefaultSort('id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }


    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('smsnotification/smsnotification_collection');
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }


    protected function _prepareColumns()
    {
          $this->addColumn('id', array(
              'header'    => Mage::helper('smsnotification')->__('ID'),
              'align'     =>'right',
              'width'     => '50px',
              'index'     => 'id',
          ));

          $this->addColumn('Receiver', array(
              'header'    => Mage::helper('smsnotification')->__('Receiver'),
              'align'     =>'left',
              'index'     => 'Receiver',
          ));

        $this->addColumn('Phone', array(
            'header'    => Mage::helper('smsnotification')->__('Phone'),
            'align'     =>'left',
            'index'     => 'Phone',
        ));

        $this->addColumn('Date', array(
            'header'    => Mage::helper('smsnotification')->__('Date'),
            'align'     =>'left',
            'index'     => 'Date',

        ));


        return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array('id'=>$row->getId()));
    }
}

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/controllers/Adminhtml/SmsorderstatusesController.php:

<?php

class VivasIndustries_SmsNotification_Adminhtml_SmsorderstatusesController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('SMS Center'))->_title($this->__('SMS Center'));
        $this->loadLayout();
        $this->_setActiveMenu('vivassms');
        $this->_addContent($this->getLayout()->createBlock('smsnotification/adminhtml_sms_status'));
        $this->renderLayout();
    }

    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('smsnotification/adminhtml_sms_status_grid')->toHtml()
        );
    }

   public function newAction()
    {  
        $this->loadLayout();
        $this->_setActiveMenu('vivassms');
        $this->renderLayout();
    }  

    public function editAction()
    {  
        $this->_initAction();

        // Get id if available
        $id  = $this->getRequest()->getParam('id');
        $model = Mage::getModel('smsnotification/smsnotification');


        $this->_initAction()
            ->_addBreadcrumb($id ? $this->__('Edit Baz') : $this->__('New Baz'), $id ? $this->__('Edit Baz') : $this->__('New Baz'))
            ->_addContent($this->getLayout()->createBlock('smsnotification/adminhtml_sms_status_edit')->setData('action', $this->getUrl('*/*/save')))
            ->renderLayout();
    }

    protected function _initAction()
    {
        $this->loadLayout()
            // Make the active menu match the menu config nodes (without 'children' inbetween)
            ->_setActiveMenu('vivassms')
            ->_title($this->__('Sales'))->_title($this->__('Baz'))
            ->_addBreadcrumb($this->__('Sales'), $this->__('Sales'))
            ->_addBreadcrumb($this->__('Baz'), $this->__('Baz'));

        return $this;
    }


    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('sales/foo_bar_baz');
    }
}

现在当我点击Create new SMS Rule按钮时,我得到这样的空白页:

![在此输入图片说明] [3]

我想要达到的目标是:

  1. 我想在上面显示的空白页面中显示3个输入字段(接收者,电话,日期),我可以在其中插入数据。
  2. 我想要Save按钮,然后点击它。我希望将3个字段中输入的数据保存在MySQL表VivasIndustries_SmsNotification中。
  3. 为什么我点击Create new SMS Rule时会收到一个空白页面?如何在上述两点中制作出我想要的内容?

    提前致谢!

1 个答案:

答案 0 :(得分:4)

我为一个输入字段做了扩展,即 1.从管理员插入数据 2.展示收藏

请找到以下代码,可能对您有帮助。

<强> [根] /app/etc/modules/Mohit_Testmodule.xml

&#13;
&#13;
<?xml version="1.0" ?>
<config>
  <modules>
    <Mohit_Testmodule>
      <active>true</active>
      <codePool>local</codePool>
    </Mohit_Testmodule>
  </modules>
</config>
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/etc/config.xml

&#13;
&#13;
<config>
  <modules>
    <Mohit_Testmodule>
      <version>1.6.0.0</version>
      <!-- Version number of  module -->
    </Mohit_Testmodule>
  </modules>
  <global>
    <models>
      <testmodule>
        <class>Mohit_Testmodule_Model</class>
        <resourceModel>testmodule_resource</resourceModel>
      </testmodule>
      <testmodule_resource>
        <class>Mohit_Testmodule_Model_Resource</class>

        <entities>
          <testmodule>
            <table>testmodule</table>
          </testmodule>
        </entities>
      </testmodule_resource>
    </models>

    <blocks>
      <testmodule>
        <class>Mohit_Testmodule_Block</class>
      </testmodule>
    </blocks>

    <resources>
      <testmodule_setup>
        <setup>
          <module>Mohit_Testmodule</module>
        </setup>
      </testmodule_setup>
    </resources>

    <helpers>
      <testmodule>
        <class>Mohit_Testmodule_Helper</class>
      </testmodule>
    </helpers>

  </global>
  <admin>
    <routers>
      <testmodule>
        <use>admin</use>
        <args>
          <module>Mohit_Testmodule</module>
          <frontName>testmodule</frontName>
        </args>
      </testmodule>
    </routers>
  </admin>
</config>
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/etc/adminhtml.xml

&#13;
&#13;
<config>
  <menu>
    <testmodule module="testmodule">
      <title>Testmodule</title>
      <sort_order>100</sort_order>
      <title>Testmodule</title>
      <action>testmodule/manage_testmodule/index</action>
    </testmodule>
  </menu>
  <acl>
    <all>
      <title>Allow Everything</title>
    </all>
    <resources>
      <admin>
        <children>
          <testmodule>
            <title>Testmodule</title>
            <sort_order>100</sort_order>
          </testmodule>
          <system>
            <children>
              <config>
                <children>
                  <testmodule>
                    <title>Testmodule</title>
                  </testmodule>
                </children>
              </config>
            </children>
          </system>
        </children>
      </admin>
    </resources>
  </acl>
</config>
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/sql/testmodule_setup/install-1.6.0.0.php

&#13;
&#13;
<?php
$installer = $this;

$installer->startSetup();


$table = $installer->getConnection()
    ->newTable($installer->getTable('testmodule/testmodule'))
    ->addColumn('testmodule_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Testmodule Id')
    ->addColumn('testmodule_title', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
        ), 'Testmodule title');
  
$installer->getConnection()->createTable($table);

$installer->endSetup();
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/Model/Resource/Testmodule.php

&#13;
&#13;
<?php
 
class Mohit_Testmodule_Model_Resource_Testmodule extends Mage_Core_Model_Resource_Db_Abstract
{
    protected function _construct()
    {
        $this->_init('testmodule/testmodule', 'testmodule_id');
    }
}
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/Model/Resource/Testmodule/Collection.php

&#13;
&#13;
<?php
class Mohit_Testmodule_Model_Resource_Testmodule_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    public function _construct()
    {
    	parent::_construct();
        $this->_init('testmodule/testmodule');
    }
}
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/Helper/Data.php

&#13;
&#13;
<?php

class Mohit_Testmodule_Helper_Data extends Mage_Core_Helper_Abstract
{
}
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/controllers/Manage/TestmoduleController.php

&#13;
&#13;
<?php

class Mohit_Testmodule_Manage_TestmoduleController extends Mage_Adminhtml_Controller_Action
{
    
    public function indexAction()
    {
    
        $this->loadLayout();
         $this->_setActiveMenu('testmodule/testmodule');
        $this->_title("Add New Data");
       
   $this
                ->_addContent($this->getLayout()->createBlock('testmodule/manage_testmodule_edit'))
                ->_addLeft($this->getLayout()->createBlock('testmodule/manage_testmodule_edit_tabs'))
            ;

        $this->renderLayout();
    }

    public function saveAction()
	{
    $data = $this->getRequest()->getPost();
    if ($data) {
    $model = Mage::getModel('testmodule/testmodule');
    $title        = $this->getRequest()->getParam('title');
    $model->setTestmoduleTitle($title);
    $model->save();
    Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('testmodule')->__('We have saved your request'));
        $this->_redirect('*/*/');
    }
    else
    {
    	Mage::getSingleton('adminhtml/session')->addError(Mage::helper('testmodule')->__('Unable to save'));
        $this->_redirect('*/*/');
    }
	}

  
    
}
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/Block/Manage/Testmodule.php

&#13;
&#13;
<?php

class Mohit_Testmodule_Block_Manage_Testmodule extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_controller = 'manage_testmodule';
        $this->_blockGroup = 'testmodule';
        $this->_headerText = Mage::helper('testmodule')->__('Manager');
        parent::__construct();
        $this->setTemplate('testmodule/testmodule.phtml');
    }

    protected function _prepareLayout()
    {
        $addButtonBlock = $this->getLayout()->createBlock('adminhtml/widget_button')
            ->setData(
                array(
                     'label'   => Mage::helper('testmodule')->__('Testmodule'),
                     'onclick' => "setLocation('" . $this->getUrl('*/*/') . "')",
                     'class'   => 'add',
                )
            )
        ;
        $this->setChild('add_new_button', $addButtonBlock);

      
      
        return parent::_prepareLayout();
    }

    public function getAddNewButtonHtml()
    {
        return $this->getChildHtml('add_new_button');
    }

    
}
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/Block/Tesmodule/Edit.php

&#13;
&#13;
<?php

class Mohit_Testmodule_Block_Manage_Testmodule_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        parent::__construct();

        $this->_objectId = 'id';
        $this->_blockGroup = 'testmodule';
        $this->_controller = 'manage_testmodule';

        $this->_updateButton('save', 'label', Mage::helper('testmodule')->__('Save'));
        $this->_updateButton('delete', 'label', Mage::helper('testmodule')->__('Delete'));
     
    }

    public function getHeaderText()
    {
      
            return Mage::helper('testmodule')->__('Test Module');
       
    }
}
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/Block/Manage/Testmodule/Edit/Tabs.php

&#13;
&#13;
<?php

class Mohit_Testmodule_Block_Manage_Testmodule_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('testmodule_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle(Mage::helper('testmodule')->__('Information'));
    }

    protected function _beforeToHtml()
    {
        $this->addTab(
            'form_section',
            array(
                 'label'   => Mage::helper('testmodule')->__('Information'),
                 'title'   => Mage::helper('testmodule')->__('Information'),
                 'content' => $this->getLayout()->createBlock('testmodule/manage_testmodule_edit_tab_form')->toHtml(),
            )
        );

        $this->addTab(
            'collection_section',
            array(
                 'label'   => Mage::helper('testmodule')->__('Collection'),
                 'title'   => Mage::helper('testmodule')->__('Collection'),
                'content' => $this->getLayout()->createBlock('testmodule/manage_testmodule_edit_tab_collection')->toHtml(),
            )
        );

        return parent::_beforeToHtml();
    }
}
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/Block/Manage/Testmodule/Edit/Forms.php

&#13;
&#13;
<?php

class Mohit_Testmodule_Block_Manage_Testmodule_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',
            )
        );

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/Block/Manage/Testmodule/Edit/Tab/Form.php

&#13;
&#13;
<?php

class Mohit_Testmodule_Block_Manage_Testmodule_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('testmodule_form', array('legend' => Mage::helper('testmodule')->__('Information')));

        $fieldset->addField(
            'title',
            'text',
            array(
                 'label'    => Mage::helper('testmodule')->__('Title'),
                 'class'    => 'required-entry',
                 'required' => true,
                 'name'     => 'title',
            )
        );


        return parent::_prepareForm();
    }
}
&#13;
&#13;
&#13;

<强> [根] /app/code/local/Mohit/Block/Manage/Testmodule/Edit/Tab/Collection.php

&#13;
&#13;
<?php

class Mohit_Testmodule_Block_Manage_Testmodule_Edit_Tab_Collection extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('testmodule_collection');
        $this->setDefaultSort('id');
    }

   

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('testmodule/testmodule')->getCollection();
          
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
   
        $this->addColumn(
            'testmodule_id',
            array(
                 'header'   => Mage::helper('testmodule')->__('ID'),
                 'sortable' => true,
                 'width'    => '60px',
                 'index'    => 'testmodule_id',
            )
        );
        $this->addColumn(
            'testmodule_title',
            array(
                 'header' => Mage::helper('testmodule')->__('Title'),
                 'index'  => 'testmodule_title',
            )
        );
       
        return parent::_prepareColumns();
    }

 
}
&#13;
&#13;
&#13;