-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAction.php
190 lines (163 loc) · 5.22 KB
/
Action.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
<?php
class Links_Action extends Typecho_Widget implements Widget_Interface_Do
{
/**
* @param $link
* @return mixed
* @throws Typecho_Db_Exception
*/
public static function insertOf($link)
{
$db = Typecho_Db::get();
$link['order'] = $db->fetchObject($db->select(array('MAX(order)' => 'maxOrder'))
->from('table.links'))->maxOrder + 1;
$link['lid'] = $db->query($db
->insert('table.links')
->rows($link));
return $link;
}
/**
* @param $link
* @return mixed
* @throws Typecho_Db_Exception
*/
public static function modifyOf($link)
{
$db = Typecho_Db::get();
$db->query($db->update('table.links')
->rows($link)
->where('lid = ?', $link['lid']));
return $link;
}
/**
* @param $list
* @return int
* @throws Typecho_Db_Exception
*/
public static function deleteOf($list)
{
$db = Typecho_Db::get();
$deleteCount = 0;
foreach ($list as $lid) {
if ($db->query($db->delete('table.links')->where('lid = ?', $lid))) {
$deleteCount++;
}
}
return $deleteCount;
}
/**
* @param int $limit
* @param null $sort
* @return array
* @throws Typecho_Db_Exception
*/
public static function selectOf($limit = 0, $sort = NULL)
{
$db = Typecho_Db::get();
$select = $db->select()->from('table.links');
if (isset($sort)) {
$select->where('table.links.sort = ?', $sort);
}
$select->order('table.links.order');
if ($limit > 0) {
$select->limit($limit);
}
return $db->fetchAll($select);
}
/**
* @param $list
* @throws Typecho_Db_Exception
*/
public static function orderOf($list)
{
$db = Typecho_Db::get();
foreach ($list as $sort => $lid) {
$db->query($db->update('table.links')
->rows(array('order' => $sort + 1))
->where('lid = ?', $lid));
}
}
/**
* @throws Typecho_Db_Exception
* @throws Typecho_Exception
* @throws Typecho_Widget_Exception
*/
public function addOf()
{
if (Links_Plugin::form()->validate()) {
$this->response->goBack();
}
/** 取出数据 */
$link = $this->request->from('name', 'mail', 'url', 'sort', 'image', 'desc', 'user');
/** 插入数据 */
$link = self::insertOf($link);
/** 设置高亮 */
$this->widget('Widget_Notice')->highlight('link-' . $link['lid']);
/** 提示信息 */
$this->widget('Widget_Notice')->set(_t('链接 <a href="%s">%s</a> 已经被增加',
$link['url'], $link['name']), NULL, 'success');
/** 转向原页 */
$this->response->redirect(Typecho_Common::url('extending.php?panel=Links%2FManage.php', Helper::options()->adminUrl));
}
/**
* @throws Typecho_Db_Exception
* @throws Typecho_Exception
* @throws Typecho_Widget_Exception
*/
public function saveOf()
{
if (Links_Plugin::form($this->request->lid)->validate()) {
$this->response->goBack();
}
/** 取出数据 */
$link = $this->request->from('lid', 'name', 'mail', 'sort', 'image', 'url', 'desc', 'user');
/** 更新数据 */
self::modifyOf($link);
/** 设置高亮 */
$this->widget('Widget_Notice')->highlight('link-' . $link['lid']);
/** 提示信息 */
$this->widget('Widget_Notice')->set(_t('链接 <a href="%s">%s</a> 已经被更新',
$link['url'], $link['name']), NULL, 'success');
/** 转向原页 */
$this->response->redirect(Typecho_Common::url('extending.php?panel=Links%2FManage.php', Helper::options()->adminUrl));
}
/**
* @throws Typecho_Db_Exception
* @throws Typecho_Exception
*/
public function removeOf()
{
$lids = $this->request->filter('int')->getArray('lid');
$deleteCount = self::deleteOf($lids);
/** 提示信息 */
$this->widget('Widget_Notice')->set($deleteCount > 0 ? _t('链接已经删除') : _t('没有链接被删除'), NULL,
$deleteCount > 0 ? 'success' : 'notice');
/** 转向原页 */
$this->response->redirect(Typecho_Common::url('extending.php?panel=Links%2FManage.php', Helper::options()->adminUrl));
}
/**
* @throws Typecho_Db_Exception
*/
public function sortOf()
{
$links = $this->request->filter('int')->getArray('lid');
self::orderOf($links);
}
/**
* 接口需要实现的入口函数
*
* @access public
* @return void
* @throws Typecho_Exception
*/
public function action()
{
Typecho_Widget::widget('Widget_User')
->pass('administrator');
$this->on($this->request->is('do=insert'))->addOf();
$this->on($this->request->is('do=update'))->saveOf();
$this->on($this->request->is('do=delete'))->removeOf();
$this->on($this->request->is('do=sort'))->sortOf();
$this->response->redirect(Helper::options()->adminUrl);
}
}