magento---后台grid加载过程分析(二)-------edit grid-----news插件为例!
http://www.g4qq.com/index.php/profile/adminhtml_profile/edit/profile_id/1/category_id/0/key/2e5cb07d908be9b66bd9f753824a7025/
当点击add a news Ariticle 或者编辑的时候,访问的URL大致就是上面的那个地址。
分析过程如下:仅供参考,无法保证正确性。
1带有key值-----后台URL
访问:RichardMason_Profile_Adminhtml_ProfileController的editAction()方法
/*
* Edit CMS page
*/
public function editAction()
{
// 1. Get ID and create model
$id = $this->getRequest()->getParam('profile_id');
$model = Mage::getModel('profile/profile');
// 2. Initial checking
if ($id) {
$model->load($id);
if (! $model->getId()) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('profile')->__('This profile no longer exists'));
$this->_redirect('*/*/');
return;
}
}
// 3. Set entered data if was error when we do save
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (! empty($data)) {
$model->setData($data);
}
if($this->getRequest()->getParam('category_id'))
$model->setData("category_id", $this->getRequest()->getParam('category_id'));
// 4. Register model to use later in blocks
Mage::register('profile_profile', $model);
// 5. Build edit form
$this->_initAction()
->_addBreadcrumb(Mage::helper('profile')->__('CMS'), Mage::helper('profile')->__('CMS'))
->_addContent($this->getLayout()->createBlock('profile/adminhtml_profile_edit'))
->_addLeft($this->getLayout()->createBlock('profile/adminhtml_profile_edit_tabs'));
$this->renderLayout();
}
1.1得到profile_id
当profile_id为零的时候,说明为new news,
当profile_id大于零的时候,说明为edit news
1.2
if($id),如果为edit的时候,执行if下面的语句。得到编辑的news的全部信息。
1.3
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (! empty($data)) {
$model->setData($data);
}
这块先一放,待会回来研究。
1.4
将$model注册到Mage。此时$model是编辑的该news的全部信息的collection。
1.5
content---profile/adminhtml_profile_edit
left ---profile/adminhtml_profile_edit_tabs
2(1.3)
layout---<profile_adminhtml_profile_edit>
<profile_adminhtml_profile_edit>
<update handle="editor"/>
<reference name="content">
<block type="profile/adminhtml_profile_edit" name="profile_edit"></block>
</reference>
<reference name="left">
<block type="profile/adminhtml_profile_edit_tabs" name="profile_edit_tabs">
<block type="profile/adminhtml_profile_edit_tab_main" name="profile_edit_tab_main" />
<block type="profile/adminhtml_profile_edit_tab_meta" name="profile_edit_tab_meta" />
<action method="addTab"><name>main_section</name><block>profile_edit_tab_main</block></action>
<action method="addTab"><name>meta_section</name><block>profile_edit_tab_meta</block></action>
</block>
</reference>
</profile_adminhtml_profile_edit>
2.1
<update handle="editor"/>
加入一个 editor layout handle
具体下面研究
2.2
content加入:block
type="profile/adminhtml_profile_edit"
2.3
left 加入:block
type="profile/adminhtml_profile_edit_tabs"
子block:
type="profile/adminhtml_profile_edit_tab_main"
type="profile/adminhtml_profile_edit_tab_meta"
增加方法action:method="addTab"
profile_edit_tab_main
profile_edit_tab_meta
tabs点击对应相应的
3(1.3--2.3)
接2.2
block
type="profile/adminhtml_profile_edit"
public function __construct()
{
//3.1
$this->_objectId = 'profile_id';
$this->_blockGroup = 'profile';
$this->_controller = 'adminhtml_profile';
//3/2
parent::__construct();
//3.3---back button
$url=Mage::registry('profile_profile')->getCategoryUrl(Mage::registry('profile_profile')->getData("category_id"));
//3.4
parent::addButton('back', array(
'label' => Mage::helper('adminhtml')->__('Back'),
'onclick' => 'setLocation(/'' . $this->getUrl("*/*/".$url."/") . '/')',
'class' => 'back',
), -1);
//3.5--save and continue edit
if ($this->_isAllowedAction('save')) {
$this->_updateButton('save', 'label', Mage::helper('profile')->__('Save'));
$this->_addButton('saveandcontinue', array(
'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save',
), -100);
} else {
$this->_removeButton('save');
}
//3.6--delete button
if ($this->_isAllowedAction('delete')) {
$this->_updateButton('delete', 'label', Mage::helper('profile')->__('Delete'));
} else {
$this->_removeButton('delete');
}
//3.7---js
$this->_formScripts[] = "
function toggleEditor() {
if (tinyMCE.getInstanceById('page_content') == null) {
tinyMCE.execCommand('mceAddControl', false, 'content');
} else {
tinyMCE.execCommand('mceRemoveControl', false, 'content');
}
}
function saveAndContinueEdit(){
editForm.submit($('edit_form').action+'back/edit/');
}
";
}
函数比较长,从标记一点一点的分析:
3.1
为block字段赋值。
$this->_objectId = 'profile_id'; news表的唯一索引。
$this->_blockGroup = 'profile';
$this->_controller = 'adminhtml_profile'; 生成block用的。
3.2
parent::__construct();
先一放
3.3-3.4
back button返回按钮的赋值
关于里面的如何写,改天再详细研究
3.5
--save and continue edit
3.6
delete button
3.7
js函数
譬如:
'onclick' => 'saveAndContinueEdit()',
对应:
function saveAndContinueEdit()
4(1.3--2.3)
下面研究3.2
public function __construct()
{
//4.1
parent::__construct();
//4.2
if (!$this->hasData('template')) {
$this->setTemplate('widget/form/container.phtml');
}
//4.3 返回按钮,已经被覆盖掉了
$this->_addButton('back', array(
'label' => Mage::helper('adminhtml')->__('Back'),
'onclick' => 'setLocation(/'' . $this->getBackUrl() . '/')',
'class' => 'back',
), -1);
//4.4 重置按钮
$this->_addButton('reset', array(
'label' => Mage::helper('adminhtml')->__('Reset'),
'onclick' => 'setLocation(window.location.href)',
), -1);
//4.5 如果edit,$objId不为空,如果new $objId为空,以此判断是否添加删除按钮
$objId = $this->getRequest()->getParam($this->_objectId);
if (! empty($objId)) {
$this->_addButton('delete', array(
'label' => Mage::helper('adminhtml')->__('Delete'),
'class' => 'delete',
'onclick' => 'deleteConfirm(/''. Mage::helper('adminhtml')->__('Are you sure you want to do this?')
.'/', /'' . $this->getDeleteUrl() . '/')',
));
}
//4.6 保存按钮
$this->_addButton('save', array(
'label' => Mage::helper('adminhtml')->__('Save'),
'onclick' => 'editForm.submit();',
'class' => 'save',
), 1);
}
有是一个好长的函数:
4.1
parent::__construct();
4.2
$this->setTemplate('widget/form/container.phtml');
5(1.3--2.3--4.2)
研究4.1
5.1
parent::__construct();为
public function __construct()
{
$args = func_get_args();
if (empty($args[0])) {
$args[0] = array();
}
$this->_data = $args[0];
$this->_construct();
}
前面已经研究过
6(1.3--2.3)
接4.2
$this->setTemplate('widget/form/container.phtml');
文件就不写了,该template用到的函数
$this->getFormInitScripts()
$this->getHeaderHtml()
$this->getButtonsHtml('header')
$this->getFormHtml()
$this->hasFooterButtons()
$this->getButtonsHtml('footer')
$this->getValidationUrl()
$this->getFormScripts()
最重要的就是
$this->getFormHtml();
public function getFormHtml()
{
$this->getChild('form')->setData('action', $this->getSaveUrl());
return $this->getChildHtml('form');
}
6.1
$this->getChild('form')->setData('action', $this->getSaveUrl());
//$this->_children[$name]->setData('action', $this->getSaveUrl());
还没打看明白,留下,继续往下走
6.2
$this->getChildHtml('form');
Mage_Adminhtml_Block_Widget_Form_Container里面有这么个函数:
protected function _prepareLayout()
{
if ($this->_blockGroup && $this->_controller && $this->_mode) {
$this->setChild('form', $this->getLayout()->createBlock($this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form'));
}
return parent::_prepareLayout();
}
故可以知道as="form" type="RichardMason/Profile_Block_Adminhtml_Profile_Edit_Form"
7(1.3--2.3)
type="RichardMason/Profile_Block_Adminhtml_Profile_Edit_Form"
该类下面有这么个函数。
protected function _prepareForm()
{
$form = new Varien_Data_Form(array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post', 'enctype' => 'multipart/form-data'));
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
这块还真没大看明白。太他妈的复杂,擦!
猜想:获取bar的值,也就是left,点击那个,就会显示相应的form,'action' => $this->getData('action')进而显示不同form。
$this->setForm($form);给block _data[form]赋值。
8(1.3--2.3)
接7
return parent::_prepareForm();
实例化,执行
protected function _construct()
{
parent::_construct();
$this->setTemplate('widget/form.phtml');
$this->setDestElementId('edit_form');
$this->setShowGlobalIcon(false);
}
故template="widget/form.phtml"
9(1.3--2.3)
template="widget/form.phtml"
<?php echo $this->getFormHtml();?>
--Mage_Adminhtml_Block_Widget_Form
public function getFormHtml()
{
if (is_object($this->getForm())) {
return $this->getForm()->getHtml();
}
return '';
}
$this->getForm()->getHtml();----->
public function toHtml()
{
Varien_Profiler::start('form/toHtml');
$html = '';
if ($useContainer = $this->getUseContainer()) {
$html .= '<form '.$this->serialize($this->getHtmlAttributes()).'>';
$html .= '<div>';
if (strtolower($this->getData('method')) == 'post') {
$html .= '<input name="form_key" type="hidden" value="'.Mage::getSingleton('core/session')->getFormKey().'" />';
}
$html .= '</div>';
}
foreach ($this->getElements() as $element) {
$html.= $element->toHtml();
}
if ($useContainer) {
$html.= '</form>';
}
Varien_Profiler::stop('form/toHtml');
return $html;
}
将对象form的子元素以form的形式显示出来。
10(2.3)
接1.3
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (! empty($data)) {
$model->setData($data);
}
Set entered data if was error when we do save
可能保存失败后,session就不存在相应的值了吧。还不是很懂
***********************************************************
left 代码
***********************************************************
11
接2.3
<reference name="left">
<block type="profile/adminhtml_profile_edit_tabs" name="profile_edit_tabs">
<block type="profile/adminhtml_profile_edit_tab_main" name="profile_edit_tab_main" />
<block type="profile/adminhtml_profile_edit_tab_meta" name="profile_edit_tab_meta" />
<action method="addTab"><name>main_section</name><block>profile_edit_tab_main</block></action>
<action method="addTab"><name>meta_section</name><block>profile_edit_tab_meta</block></action>
</block>
</reference>
-----------------------------------------------------------
left 加入:block
type="profile/adminhtml_profile_edit_tabs"
子block:
type="profile/adminhtml_profile_edit_tab_main"
type="profile/adminhtml_profile_edit_tab_meta"
增加方法action:method="addTab"
profile_edit_tab_main
profile_edit_tab_meta
tabs点击对应相应的
11.1
profile/adminhtml_profile_edit_tabs
RichardMason_Profile_Block_Adminhtml_Profile_Edit_Tabs
public function __construct()
{
parent::__construct();
$this->setId('profiles_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(Mage::helper('profile')->__('Information'));
}
11.1.1
赋值:
$this->setId('profiles_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(Mage::helper('profile')->__('Information'));
11.1.2
parent::__construct();
找这个函数找到,但是找不到template
在
它继承的类找到这个函数,不知道怎么执行,应该就是这个template吧
protected function _construct()
{
$this->setTemplate('widget/tabs.phtml');
}
12
widget/tabs.phtml
foreach ($tabs as $_tab):
<a href="<?php echo $this->getTabUrl($_tab) ?>"
$tabs应该就是
<action method="addTab"><name>main_section</name><block>profile_edit_tab_main</block></action>
<action method="addTab">的使用参看文章:tab的分析和使用!