action_class.php
2.67 KB
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
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* 常用的action封装处理类
*
* @author Tiny
* @class Action
*/
class Action extends BaseAction
{
/**
* action 运行入口
*
* @access public
* @return mixed
*/
public function run()
{
$controller = $this->getController();
$methodName = preg_split("/_(?=(save|del|edit)$)/i",$this->getId());
if(count($methodName)==2)
{
$op = $methodName[1];
$modelName = $methodName[0];
}
else
{
$op = $methodName[0];
$modelName = $controller->getId();
}
$operator = array('save'=>'save','del'=>'delete','edit'=>'find');
//如果配制文件存在curd函数自动进行处理
if($controller->getAutoActionRight() && array_key_exists($op,$operator))
{
if(($op=='save'))
{
$pre_validator = $modelName.'_validator';
if(method_exists($controller,$pre_validator)){
$validator = $controller->$pre_validator();
if(is_array($validator))
{
$data = Req::args()+array('validator'=>$validator);
$controller->redirect($modelName.'_edit',false,$data);
exit;
}
}
}
$model = new Model($modelName);
$opAction = $operator[$op];
$data=$model->data(Req::args())->$opAction();
switch($op)
{
case 'save':
{
if($data!==false)
{
$controller->redirect($modelName.'_list');
}
else
{
$controller->redirect($modelName.'_edit',null,false,array('form'=>$model->find()));
}
break;
}
case 'del':
{
$controller->redirect($modelName.'_list');
break;
}
case 'edit':
{
$data = isset($data)?$data:array();
$data['ref_route'] = Url::getRefRoute();
$controller->redirect($modelName.'_edit',false,$data);
break;
}
}
}
else
{
$action = new ViewAction($controller, $this->getId());
$action->run();
//exit;
}
}
}