FController.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. namespace KIF\Core;
  3. use Exception;
  4. abstract class FController {
  5. /**
  6. * @var 请求
  7. */
  8. protected $_setTemplate = false;
  9. /**
  10. * @var 模板类
  11. */
  12. protected $_view;
  13. /**
  14. *
  15. * 所有输出到模版的变量,都存放到这里面,以便统一管理
  16. * @var array
  17. */
  18. protected $output = array();
  19. protected $tpl;
  20. /**
  21. * 导航菜单
  22. *
  23. * @var Array
  24. */
  25. protected $navMenus = array();
  26. protected $telData ;
  27. public function __construct() {
  28. //这行一定放在最前面; *因为__set方法用到它(当$this->xx = xx 的时候调用此方法)*
  29. $this->_view = new CViewRender ();
  30. $this->telData = $this->includeTemplateData();
  31. $this->ctl = $this->getController ();
  32. $this->act = $this->getAction ();
  33. $this->exe = $this->getExecName ();
  34. }
  35. /**
  36. * 加载环境
  37. */
  38. abstract public function prepare($args);
  39. /**
  40. * 回显html内容
  41. */
  42. public function display() {
  43. if ($this->_setTemplate) {
  44. $content = $this->_view->display ();
  45. return $content;
  46. }
  47. }
  48. /**
  49. * 获取模板解析后的内容
  50. */
  51. public function fetch($file) {
  52. ///////////////增加/////////////////
  53. if(isset($this->telData[$file])){
  54. $this->tpl = $this->telData[$file];
  55. return $this->render(true);
  56. }
  57. ////////////////////////////////
  58. $this->setTemplate ( $file );
  59. $content = $this->_view->fetch ();
  60. return $content;
  61. }
  62. /**
  63. * 设置模板
  64. */
  65. public function setTemplate($file) {
  66. ///////////////增加/////////////////
  67. if(isset($this->telData[$file])){
  68. $this->tpl = $this->telData[$file];
  69. $this->render();
  70. return true;
  71. }
  72. ////////////////////////////////
  73. $this->_setTemplate = true;
  74. $this->_view->setTemplate ( $file );
  75. }
  76. /**
  77. * 魔术方法,自动给模板提供数据 *important!*
  78. */
  79. public function __set($key, $val) {
  80. $this->$key = $val;
  81. $this->setOutput($key, $val);
  82. //再分配给模板
  83. $this->_view->set ( $key, $val );
  84. }
  85. /**
  86. * 魔术方法,控制器中,调用没有的方法,直接到request中调用 *important!*
  87. * 如:$this->getController();
  88. */
  89. public function __call($method, $args) {
  90. //return call_user_func_array(array(CBase::$request, $method), $args);
  91. }
  92. /**
  93. * 消息提示,后续扩展
  94. */
  95. public function showMessage($msg, $url = '') {
  96. $str = '<script>';
  97. if ($msg) {
  98. $str .= "alert('$msg');";
  99. }
  100. if ($url) {
  101. $str .= "location.href ='$url';";
  102. } else {
  103. if ($second) {
  104. $str .= "top.location.reload();";
  105. } else {
  106. $str .= "history.go(-1);";
  107. }
  108. }
  109. $str .= '</script>';
  110. die ( $str );
  111. }
  112. /**
  113. * 表单提交后,映射到相应的处理方法
  114. */
  115. public function submitMap() {
  116. $funcName = "_" . strtolower ( $this->act ) . $this->exe;
  117. if (! method_exists ( $this, $funcName )) {
  118. $this->showMessage ( '参数错误,非法操作~' );
  119. }
  120. //处理
  121. $this->$funcName ();
  122. }
  123. /**
  124. * 获取控制器名称
  125. */
  126. public function getController()
  127. {
  128. $conName = ucfirst(strip_tags($_REQUEST['c']));
  129. return $conName ? $conName : 'Default';
  130. }
  131. /**
  132. * 获取动作
  133. */
  134. public function getAction()
  135. {
  136. return isset($_REQUEST['a']) ? ucfirst(strip_tags($_REQUEST['a'])) : 'Default';
  137. }
  138. /**
  139. * 获取动作
  140. */
  141. public function getExecName()
  142. {
  143. return isset($_REQUEST['e']) ? ucfirst(strip_tags($_REQUEST['e'])) : '';
  144. }
  145. /**
  146. *
  147. * 渲染结果
  148. * @param boolean $return 是否返回。true:返回渲染结果;false:直接输出结果
  149. * @return string | NULL
  150. */
  151. public function render($return = false) {
  152. if (!$this->tpl) {// 没有指定模文件
  153. return null;
  154. }
  155. $objView = new View();
  156. $this->output['navMenus'] = $this->navMenus;
  157. $config = Config::getInstance()->current();
  158. $this->output['web_cfg'] = $config['web_cfg'];
  159. $objView->assign($this->output);
  160. if ($return) {
  161. return $objView->r($this->tpl);
  162. } else {
  163. $objView->d($this->tpl);
  164. }
  165. }
  166. /**
  167. * 添加导航栏
  168. *
  169. * @param string $title
  170. * @param string $href
  171. * @param string $target
  172. * @param string $icon
  173. * @return \KIF\Core\Controller
  174. */
  175. protected function addNavMenu($title, $href = null, $target = '_self', $icon = null) {
  176. $this->navMenus[] = array(
  177. 'title' => $title,
  178. 'href' => $href,
  179. 'target' => $target,
  180. 'icon' => $icon,
  181. );
  182. return $this;
  183. }
  184. /**
  185. *
  186. * 设定指定的名、值,模版里可以使用到
  187. * @param string $name
  188. * @param mixed $value
  189. * @throws Exception
  190. */
  191. protected function setOutput($name, $value) {
  192. if (!is_string($name)) {
  193. throw new Exception('set output to template error, name not string !');
  194. }
  195. $this->output[$name] = $value;
  196. }
  197. /**
  198. * 获取专题数据文件
  199. *
  200. * @param string $name
  201. */
  202. static public function includeTemplateData() {
  203. //$appPath = Config::getInstance ()->get ( 'App_Path' );
  204. return include '/export/manager/product/app/product/kif/include/FControllerTemplate.data.php';
  205. }
  206. }
  207. class CViewRender extends CRender {
  208. /**
  209. * 构造函数
  210. *
  211. * @param string $templateFile 模板文件(扩展名为:tpl.php)
  212. * @param string $templateEngine 模板引擎
  213. */
  214. public function __construct($templateFile = null, $templateEngine = "php") {
  215. if (! $templateEngine)
  216. $templateEngine = "php";
  217. if (! is_null ( $templateFile ))
  218. $this->setTemplate ( $templateFile );
  219. }
  220. /**
  221. * 显示解析模板内容
  222. */
  223. public function display() {
  224. //$timer2 = new CTimer('模板渲染耗时:');
  225. $content = $this->render ();
  226. echo $content;
  227. return $content;
  228. //echo $timer2->mark('secs ', 6)."<br/>";
  229. }
  230. /**
  231. * 包含模板文件
  232. */
  233. public function embedFile($file) {
  234. if ($this->checkTemplateFile ( $file )) {
  235. if ($this->_viewData) {
  236. extract ( $this->_viewData );
  237. }
  238. include_once (APP_TEMPLATE . '/' . $file . '.tpl.php');
  239. }
  240. }
  241. /**
  242. * 获取模板内容
  243. */
  244. public function fetch() {
  245. return $this->render ();
  246. }
  247. /**
  248. * 设置$_viewData数组值
  249. */
  250. public function set($key, $val) {
  251. $this->_viewData [$key] = $val;
  252. }
  253. /**
  254. * 魔术函数 给类属性赋值
  255. */
  256. public function __set($key, $val) {
  257. $this->set ( $key, $val );
  258. }
  259. /**
  260. * 设置模板文件
  261. *
  262. * @param string $templateFile 缺损模板文件名
  263. */
  264. public function setTemplate($file) {
  265. $this->_templateFile = (defined ( 'APP_TEMPLATE' ) ? APP_TEMPLATE . '/' : "") . $file . ".tpl.php";
  266. }
  267. /**
  268. * 获取模板文件名
  269. */
  270. public function getTemplate() {
  271. return $this->_templateFile;
  272. }
  273. /**
  274. * 解析模板并返回内容
  275. *
  276. * @return string 解析内容
  277. */
  278. public function render() {
  279. //$timer2 = new CTimer($this->_templateFile.'渲染耗时内部调试:');
  280. if (! $this->checkTemplateFile ( $this->_templateFile )) {
  281. trigger_error ( "($this->_templateFile)模板文件不存在" );
  282. }
  283. if ($this->_viewData) {
  284. extract ( $this->_viewData );
  285. }
  286. //开始缓存
  287. ob_start ();
  288. include ($this->_templateFile);
  289. $content = ob_get_clean ();
  290. ob_flush ();
  291. flush ();
  292. //echo $timer2->mark('secs ', 6)."<br/>";
  293. return $content;
  294. }
  295. }
  296. abstract class CRender {
  297. /**
  298. * @var array 提取的内容数据
  299. */
  300. protected $_viewData;
  301. /**
  302. * @var string 模板文件
  303. */
  304. protected $_templateFile;
  305. /**
  306. * 解析模板文件并输出渲染内容
  307. */
  308. abstract function display();
  309. /**
  310. * 获取模板内容
  311. */
  312. abstract function fetch();
  313. /**
  314. * 渲染内容
  315. */
  316. abstract function render();
  317. /**
  318. * 验证文件是否存在
  319. *
  320. * @return boolean
  321. */
  322. public function checkTemplateFile() {
  323. if (! $this->_templateFile) {
  324. // throw new Exception("ddd");
  325. return false;
  326. }
  327. if (! is_file ( $this->_templateFile )) {
  328. // throw new Exception("模板文件不存在:".$this->_templateFile);
  329. return false;
  330. }
  331. return true;
  332. }
  333. }