-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.php
150 lines (133 loc) · 2.75 KB
/
element.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
<?php
/**
* element类 在视图中包含元件
* 用法:<!--{element name=name act=action}-->
*
* @author linln
* @version $Id$
*/
class Element
{
/**
* 视图实例
*
* @var object
*/
protected $view;
/**
* element实例名称
*
* @var string
*/
protected $element;
/**
* 执行的方法名称
*
* @var string
*/
protected $action;
public function setElement($name)
{
$this->element = $name;
$this->view = self::getView();
}
public function setAction($name)
{
$this->action = $name;
}
/**
* 得到视图的实例
*
* @return class
* @return Object (View)
*/
protected static function getView()
{
return View::getInstance();
}
/**
* 视图实例display方法的快捷方式
*
* @param string $tpl
* @return void
*/
public function display($tpl)
{
$this->view->display($tpl);
}
/**
* 视图实例fetch方法的快捷方式
*
* @param string $tpl
*/
public function fetch($tpl)
{
return $this->view->fetch($tpl);
}
/**
* 视图实例assign方法的快捷方式
*
* @param string $field
* @param string $$value
*/
public function assign($field, $value)
{
return $this->view->assign($field, $value);
}
/**
* 默认的方法,将参数赋值到视图并显示视图
*
* @param string $tpl 要显示的视图: 不传入则显示当前element目录中与当前action同名的视图
* @param array $params 视图中的参数: array('参数名称'=>'值')
*/
public function render($params = array(), $tpl = null)
{
$tpl = is_null($tpl) ? $this->action : $tpl;
$file = VIEW_DIR .DS. 'elements' .DS. $this->element .DS. $tpl .'.html';
if (file_exists($file))
{
if (!empty($params))
{
$keys = array_keys($params);
for ($i = 0; $i < count($params); $i++)
{
$this->assign($keys[$i], $params[$keys[$i]]);
}
}
$this->display($file);
}
}
public function request($ctl, $act, $params = array())
{
$dispatcher = new Dispatcher();
$ctl = $dispatcher->generateCtl($ctl);
Loader::controller($ctl);
$controller = new $ctl();
$controller->setController($ctl);
if (!method_exists($controller, $act))
{
Loader::exception('notfind');
throw new NotFindException();
}
return $controller->$act($params);
}
/**
* 取得当前登录信息
* 注意:返回的信息是当前用户登录时存放在SESSION中的信息
* 如果在登录后修改了这些信息,此方法中返回的内容不会
* 与之同步。
*/
public function getLoginInfo($id = null)
{
$name = 'fw_login';
if (!empty($id)) $name .= '_'.$id;
$login = @$_SESSION[$name.'_info'];
if (!is_null($login))
{
$login = str_replace('\"', '"', $login);
return json_decode($login, 1);
}
return null;
}
}
?>