123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- namespace KIF\Core;
- use KIF\String\Filter;
- use KIF\Core\View;
- use Exception;
- abstract class Controller {
- protected $action;
-
-
- protected $output = array();
-
- protected $tpl;
-
-
- protected $navMenus = array();
-
-
- abstract public function run();
-
-
- protected function setOutput($name, $value) {
- if (!is_string($name)) {
- throw new Exception('set output to template error, name not string !');
- }
- $this->output[$name] = $value;
- }
-
-
- public function setAction($action) {
- if (empty($action)) {
- $action = 'default';
- }
- $this->action = 'do' . ucfirst($action);
- }
-
-
- protected function addNavMenu($title, $href = null, $target = '_self', $icon = null) {
- $this->navMenus[] = array(
- 'title' => $title,
- 'href' => $href,
- 'target' => $target,
- 'icon' => $icon,
- );
- return $this;
- }
-
-
- protected function setOutputs(array $outputs) {
- $this->output = array_merge($this->output, $outputs);
- }
-
-
- public function render($return = false) {
- if (!$this->tpl) {
- return null;
- }
- $objView = new View();
-
- $this->output['navMenus'] = $this->navMenus;
-
- $config = Config::getInstance()->current();
- $this->output['web_cfg'] = $config['web_cfg'];
-
- $objView->assign($this->output);
-
- if ($return) {
- return $objView->r($this->tpl);
- } else {
- $objView->d($this->tpl);
- }
- }
-
-
- protected function ajax_fail_exit($msg) {
- $return = array(
- 'ok' => false,
- 'msg' => $msg,
- );
-
- $jsonp_cb = Request::r('jsonp_cb', Filter::TRIM);
- if ($jsonp_cb) {
- $jsonp_cb = Filter::htmlspecialchars($jsonp_cb);
- $this->echo_exit("{$jsonp_cb}(".json_encode($return).")");
- }
- $this->echo_exit(json_encode($return));
- }
-
-
- protected function ajax_success_exit($msg) {
- $return = array(
- 'ok' => true,
- 'msg' => $msg,
- );
-
- $jsonp_cb = Request::r('jsonp_cb', Filter::TRIM);
- if ($jsonp_cb) {
- $jsonp_cb = Filter::htmlspecialchars($jsonp_cb);
- $this->echo_exit("{$jsonp_cb}(".json_encode($return).")");
- }
- $this->echo_exit(json_encode($return));
- }
-
- protected function echo_exit($msg) {
- echo $msg;
- exit;
- }
-
- protected function echo_msg($msg) {
- echo '['.date('Y-m-d H:i:s').'] '. $msg;
- $this->newline();
- }
-
-
- protected function newline() {
- if (Request::isCLI()) {
- echo "\r\n";
- } else {
- echo "<br />";
- }
- }
-
-
- protected function redirect($url,$status ='') {
- if ($status == '301') {
- header("HTTP/1.1 301 Moved Permanently");
- }
- if (!empty($url)) {
- header("Location: ".$url."");
- }
- exit;
- }
-
-
- protected function fail_exit_bs($msg = null) {
- $permission_template_dir = Config::getInstance()->get('App_Path') . DS . 'template_dir';
- $this->tpl = $permission_template_dir . '/prompt_message';
- $this->setOutputs(array(
- 'type' => 'fail',
- 'msg' => $msg,
- 'referer' => Request::referer(),
- 'header_tpl'=> $permission_template_dir . '/header.html',
- 'bottom_tpl'=> $permission_template_dir . '/bottom.html',
- ));
- $this->render();
- exit;
- }
-
-
- protected function success_exit_bs($msg = null) {
- $permission_template_dir = Config::getInstance()->get('App_Path') . DS . 'template_dir';
- $this->tpl = $permission_template_dir . '/prompt_message';
- $this->setOutputs(array(
- 'type' => 'success',
- 'msg' => $msg,
- 'referer' => Request::referer(),
- 'header_tpl'=> $permission_template_dir . '/header.html',
- 'bottom_tpl'=> $permission_template_dir . '/bottom.html',
- ));
- $this->render();
- exit;
- }
- }
|