在这一章节,我们来梳理一下之前几章的内容,更加完整性的走一遍,下面所列出步骤中的代码可以在这里下载,解压后直接复制进项目即可看出效果
下载: Magento-Backend-Module-Full.zip (16.0 KB)
空间名 / Namespace :
Company
模块名 / Module Name :
Web
步骤一、声明模块及其代码池
建立 /app/etc/modules/Company_Web.xml 文件 (你可以任意定义名字,也可以用一个文件来声明多个模块)
1
2
3
4
5
6
7
8
9
|
<?
xml
version
=
"1.0"
?>
<
config
>
<
modules
>
<
Company_Web
>
<
active
>
true
<
/
active
>
<
codePool
>
local
<
/
codePool
>
<
/
Company_Web
>
<
/
modules
>
<
/
config
>
|
步骤二、在 /app/code/local/ 目录下为该模块建立底层目录
1
2
3
4
5
6
7
8
9
|
Company
/
|–
Web
/
||–
Block
/
||–
controllers
/
||–
etc
/
||–
Helper
/
||–
sql
/
|
|
步骤三、为前台创建一个控制器
/app/code/local/Company/Web/controllers/IndexController.php
1
2
3
4
5
6
7
8
|
class
Company_Web_IndexController
extends
Mage_Core_Controller_Front_Action
{
public
function
indexAction
(
)
{
$this
->
loadLayout
(
)
;
$this
->
renderLayout
(
)
;
}
}
|
步骤四、 继续为后台创建一个控制器
app\code\local\Company\Web\controllers\Adminhtml\WebController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
class
Company_Web_Adminhtml_WebController
extends
Mage_Adminhtml_Controller_action
{
protected
function
_initAction
(
)
{
$this
->
loadLayout
(
)
->
_setActiveMenu
(
'web/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
(
'web/web'
)
->
load
(
$id
)
;
if
(
$model
->
getId
(
)
||
$id
==
0
)
{
$data
=
Mage::
getSingleton
(
'adminhtml/session'
)
->
getFormData
(
true
)
;
if
(
!
empty
(
$data
)
)
{
$model
->
setData
(
$data
)
;
}
Mage::
register
(
'web_data'
,
$model
)
;
$this
->
loadLayout
(
)
;
$this
->
_setActiveMenu
(
'web/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
(
'web/adminhtml_web_edit'
)
)
->
_addLeft
(
$this
->
getLayout
(
)
->
createBlock
(
'web/adminhtml_web_edit_tabs'
)
;
$this
->
renderLayout
(
)
;
}
else
{
Mage::
getSingleton
(
'adminhtml/session'
)
->
addError
(
Mage::
helper
(
'web'
)
->
__
(
'Item does not exist'
)
)
;
$this
->
_redirect
(
'*/*/'
)
;
}
}
public
function
newAction
(
)
{
$this
->
_forward
(
'edit'
)
;
}
public
function
saveAction
(
)
{
if
(
$data
=
$this
->
getRequest
(
)
->
getPost
(
)
)
{
if
(
isset
(
$_FILES
[
'filename'
]
[
'name'
]
)
&&
$_FILES
[
'filename'
]
[
'name'
]
!=
''
)
{
try
{
/* 开始上传 */
$uploader
=
new
Varien_File_Uploader
(
'filename'
)
;
/* 允许的扩展名 */
$uploader
->
setAllowedExtensions
(
array
(
'jpg'
,
'jpeg'
,
'gif'
,
'png'
)
)
;
$uploader
->
setAllowRenameFiles
(
false
)
;
// 设置文件上传模式
// false -> 从指定的文件夹直接获得文件
// true -> 获取文件像文件夹一样
// (file.jpg 上传后会像这样 /media/f/i/file.jpg)
$uploader
->
setFilesDispersion
(
false
)
;
// 我们设置 'media' 作为上传后的文件夹
$path
=
Mage::
getBaseDir
(
'media'
)
.
DS
;
$uploader
->
save
(
$path
,
$_FILES
[
'filename'
]
[
'name'
]
)
;
}
catch
(
Exception
$e
)
{
}
//这样我们把文件名存进了数据库
$data
[
'filename'
]
=
$_FILES
[
'filename'
]
[
'name'
]
;
}
$model
=
Mage::
getModel
(
'web/web'
)
;
$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
(
'web'
)
->
__
(
'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
(
'web'
)
->
__
(
'Unable to find item to save'
)
)
;
$this
->
_redirect
(
'*/*/'
)
;
}
public
function
deleteAction
(
)
{
if
(
$this
->
getRequest
(
)
->
getParam
(
'id'
)
>
0
)
{
try
{
$model
=
Mage::
getModel
(
'web/web'
)
;
$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
(
'web'
)
;
if
(
!
is_array
(
$webIds
)
)
{
Mage::
getSingleton
(
'adminhtml/session'
)
->
addError
(
Mage::
helper
(
'adminhtml'
)
->
__
(
'Please select item(s)'
)
)
;
}
else
{
try
{
foreach
(
$webIds
as
$webId
)
{
$web
=
Mage::
getModel
(
'web/web'
)
->
load
(
$webId
)
;
$web
->
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
(
'web'
)
;
if
(
!
is_array
(
$webIds
)
)
{
Mage::
getSingleton
(
'adminhtml/session'
)
->
addError
(
$this
->
__
(
'Please select item(s)'
)
)
;
}
else
{
try
{
foreach
(
$webIds
as
$webId
)
{
$web
=
Mage::
getSingleton
(
'web/web'
)
->
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 文件
app/code/local/Company/Web/Block/Web.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class
Company_Web_Block_Web
extends
Mage_Core_Block_Template
{
public
function
_prepareLayout
(
)
{
return
parent
::
_prepareLayout
(
)
;
}
public
function
getWeb
(
)
{
if
(
!
$this
->
hasData
(
'web'
)
)
{
$this
->
setData
(
'web'
,
Mage::
registry
(
'web'
)
)
;
}
return
$this
->
getData
(
'web'
)
;
}
}
|
步骤六、创建一个后台的 Block 文件
app/code/local/Company/Web/Block/Adminhtml/Web.php
1
2
3
4
5
6
7
8
9
10
11
12
|
class
Company_Web_Block_Adminhtml_Web
extends
Mage_Adminhtml_Block_Widget_Grid_Container
{
public
function
__construct
(
)
{
$this
->
_controller
=
'adminhtml_web'
;
$this
->
_blockGroup
=
'web'
;
$this
->
_headerText
=
Mage::
helper
(
'web'
)
->
__
(
'Item Manager'
)
;
$this
->
_addButtonLabel
=
Mage::
helper
(
'web'
)
->
__
(
'Add Item'
)
;
parent
::
__construct
(
)
;
}
}
|
步骤七、创建如下文件
app/code/local/Company/Web/Block/Adminhtml/Web/Grid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
class
Company_Web_Block_Adminhtml_Web_Grid
extends
Mage_Adminhtml_Block_Widget_Grid
{
public
function
__construct
(
)
{
parent
::
__construct
(
)
;
$this
->
setId
(
'webGrid'
)
;
$this
->
setDefaultSort
(
'web_id'
)
;
$this
->
setDefaultDir
(
'ASC'
)
;
$this
->
setSaveParametersInSession
(
true
)
;
}
protected
function
_prepareCollection
(
)
{
$collection
=
Mage::
getModel
(
'web/web'
)
->
getCollection
(
)
;
$this
->
setCollection
(
$collection
)
;
return
parent
::
_prepareCollection
(
)
;
}
protected
function
_prepareColumns
(
)
{
$this
->
addColumn
(
'web_id'
,
array
(
'header'
=
>
Mage::
helper
(
'web'
)
->
__
(
'ID'
)
,
'align'
=
>
'right'
,
'width'
=
>
'50px'
,
'index'
=
>
'web_id'
,
)
)
;
$this
->
addColumn
(
'title'
,
array
(
'header'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Title'
)
,
'align'
=
>
'left'
,
'index'
=
>
'title'
,
)
)
;
/*
$this->addColumn('content', array(
'header' => Mage::helper('web')->__('Item Content'),
'width' => '150px',
'index' => 'content',
));
*/
$this
->
addColumn
(
'status'
,
array
(
'header'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Status'
)
,
'align'
=
>
'left'
,
'width'
=
>
'80px'
,
'index'
=
>
'status'
,
'type'
=
>
'options'
,
'options'
=
>
array
(
1
=
>
'Enabled'
,
2
=
>
'Disabled'
,
)
,
)
)
;
$this
->
addColumn
(
'action'
,
array
(
'header'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Action'
)
,
'width'
=
>
'100'
,
'type'
=
>
'action'
,
'getter'
=
>
'getId'
,
'actions'
=
>
array
(
array
(
'caption'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Edit'
)
,
'url'
=
>
array
(
'base'
=
>
'*/*/edit'
)
,
'field'
=
>
'id'
)
)
,
'filter'
=
>
false
,
'sortable'
=
>
false
,
'index'
=
>
'stores'
,
'is_system'
=
>
true
,
)
)
;
$this
->
addExportType
(
'*/*/exportCsv'
,
Mage::
helper
(
'web'
)
->
__
(
'CSV'
)
)
;
$this
->
addExportType
(
'*/*/exportXml'
,
Mage::
helper
(
'web'
)
->
__
(
'XML'
)
)
;
return
parent
::
_prepareColumns
(
)
;
}
protected
function
_prepareMassaction
(
)
{
$this
->
setMassactionIdField
(
'web_id'
)
;
$this
->
getMassactionBlock
(
)
->
setFormFieldName
(
'web'
)
;
$this
->
getMassactionBlock
(
)
->
addItem
(
'delete'
,
array
(
'label'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Delete'
)
,
'url'
=
>
$this
->
getUrl
(
'*/*/massDelete'
)
,
'confirm'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Are you sure?'
)
)
)
;
$statuses
=
Mage::
getSingleton
(
'web/status'
)
->
getOptionArray
(
)
;
array_unshift
(
$statuses
,
array
(
'label'
=
>
''
,
'value'
=
>
''
)
)
;
$this
->
getMassactionBlock
(
)
->
addItem
(
'status'
,
array
(
'label'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Change status'
)
,
'url'
=
>
$this
->
getUrl
(
'*/*/massStatus'
,
array
(
'_current'
=
>
true
)
)
,
'additional'
=
>
array
(
'visibility'
=
>
array
(
'name'
=
>
'status'
,
'type'
=
>
'select'
,
'class'
=
>
'required-entry'
,
'label'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Status'
)
,
'values'
=
>
$statuses
)
)
)
)
;
return
$this
;
}
public
function
getRowUrl
(
$row
)
{
return
$this
->
getUrl
(
'*/*/edit'
,
array
(
'id'
=
>
$row
->
getId
(
)
)
)
;
}
}
|
步骤八、创建如下文件
app/code/local/Company/Web/Block/Adminhtml/Web/Edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
class
Company_Web_Block_Adminhtml_Web_Edit
extends
Mage_Adminhtml_Block_Widget_Form_Container
{
public
function
__construct
(
)
{
parent
::
__construct
(
)
;
$this
->
_objectId
=
'id'
;
$this
->
_blockGroup
=
'web'
;
$this
->
_controller
=
'adminhtml_web'
;
$this
->
_updateButton
(
'save'
,
'label'
,
Mage::
helper
(
'web'
)
->
__
(
'Save Item'
)
)
;
$this
->
_updateButton
(
'delete'
,
'label'
,
Mage::
helper
(
'web'
)
->
__
(
'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('web_content') == null) {
tinyMCE.execCommand('mceAddControl', false, 'web_content');
} else {
tinyMCE.execCommand('mceRemoveControl', false, 'web_content');
}
}
function saveAndContinueEdit(){
editForm.submit($('edit_form').action+'back/edit/');
}
"
;
}
public
function
getHeaderText
(
)
{
if
(
Mage::
registry
(
'web_data'
)
&&
Mage::
registry
(
'web_data'
)
->
getId
(
)
)
{
return
Mage::
helper
(
'web'
)
->
__
(
"Edit Item '%s'"
,
$this
->
htmlEscape
(
Mage::
registry
(
'web_data'
)
->
getTitle
(
)
)
)
;
}
else
{
return
Mage::
helper
(
'web'
)
->
__
(
'Add Item'
)
;
}
}
}
|
步骤九、创建如下文件
app/code/local/Company/Web/Block/Adminhtml/Web/Edit/Tabs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class
Company_Web_Block_Adminhtml_Web_Edit_Tabs
extends
Mage_Adminhtml_Block_Widget_Tabs
{
public
function
__construct
(
)
{
parent
::
__construct
(
)
;
$this
->
setId
(
'web_tabs'
)
;
$this
->
setDestElementId
(
'edit_form'
)
;
$this
->
setTitle
(
Mage::
helper
(
'web'
)
->
__
(
'Item Information'
)
)
;
}
protected
function
_beforeToHtml
(
)
{
$this
->
addTab
(
'form_section'
,
array
(
'label'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Item Information'
)
,
'title'
=
>
Mage::
helper
(
'web'
)
->
__
(
'Item Information'
)
,
'content'
=
>
$this
->
getLayout
(
)
->
createBlock
(
'web/adminhtml_web_edit_tab_form'
)
->
toHtml
(
)
,
)
)
;
return
parent
::
_beforeToHtml
(
)
;
}
}
|
步骤十、创建如下文件
app/code/local/Company/Web/Block/Adminhtml/Web/Edit/Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class
Company_Web_Block_Adminhtml_Web_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
(
)
;
}
}
|
步骤十一、创建如下文件
app/code/local/Company/Web/Block/Adminhtml/Web/Edit/Tab/Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|