PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/library/base/PwErrorController.php

https://github.com/cuijinquan/nextwind
PHP | 101 lines | 61 code | 8 blank | 32 comment | 13 complexity | 7c41c02778377f80b4456d33a0dd3457 MD5 | raw file
  1. <?php
  2. defined('WEKIT_VERSION') || exit('Forbidden');
  3. Wind::import('WIND:utility.WindJson');
  4. /**
  5. * 错误处理
  6. *
  7. * @author xiaoxia.xu <xiaoxia.xuxx@aliyun-inc.com>
  8. * @copyright ©2003-2103 phpwind.com
  9. * @license http://www.phpwind.com
  10. * @version $Id: PwErrorController.php 1532 2011-12-15上午11:00:42 xiaoxiao $
  11. * @package library.base
  12. */
  13. class PwErrorController extends WindErrorHandler {
  14. protected $state = 'fail';
  15. /*
  16. * (non-PHPdoc) @see WindErrorHandler::beforeAction()
  17. */
  18. public function beforeAction($handlerAdapter) {
  19. parent::beforeAction($handlerAdapter);
  20. $this->setOutput($this->error['referer'], 'referer');
  21. $this->setOutput($this->error['refresh'], 'refresh');
  22. isset($this->error['state']) && $this->state = $this->error['state'];
  23. unset($this->error['referer'], $this->error['refresh'], $this->error['state']);
  24. }
  25. /**
  26. * 错误提示
  27. *
  28. * @see WindErrorHandler::run()
  29. */
  30. public function run() {
  31. $this->setOutput($this->state, 'state');
  32. if (isset($this->error['data'])) {
  33. $this->setOutput($this->error['data'], 'data');
  34. unset($this->error['data']);
  35. }
  36. $this->setOutput($this->error, "message");
  37. $this->setTemplate('TPL:common.error');
  38. // set layout for common request
  39. if (!$this->getRequest()->getIsAjaxRequest()) {
  40. $this->setLayout('TPL:common.layout_error');
  41. $lang = Wind::getComponent('i18n');
  42. Wekit::setGlobal(NEXT_VERSION, 'version');
  43. Wekit::setGlobal(array('title' => strtr($lang->getMessage('SEO:' . $this->state . '.page.title'), array('{sitename}' => Wekit::C('site', 'info.name')))), 'seo');
  44. }
  45. }
  46. /*
  47. * (non-PHPdoc) @see WindSimpleController::afterAction()
  48. */
  49. public function afterAction($handlerAdapter) {
  50. parent::afterAction($handlerAdapter);
  51. $debug = Wekit::C('site', 'debug') || !Wekit::C('site', 'css.compress');
  52. Wekit::setGlobal(array('debug' => $debug ? '/dev' : '/build'), 'theme');
  53. $this->setTheme('site', null);
  54. /* @var $resource WindLangResource */
  55. $resource = Wind::getComponent('i18n');
  56. $_error = $this->getForward()->getVars('message');
  57. if ($resource !== null) {
  58. foreach ($_error as $key => $value) {
  59. if (is_array($value))
  60. list($value, $var) = $value;
  61. else
  62. $var = array();
  63. $message = $resource->getMessage($value, $var);
  64. $message && $_error[$key] = $message;
  65. }
  66. }
  67. $this->getForward()->setVars(array('message' => $_error, '__error' => ''));
  68. $type = $this->getRequest()->getAcceptTypes();
  69. // 如果是含有上传的递交,不能采用ajax的方式递交,需要以html的方式递交,并且返回的结果需要是json格式,将以json=1传递过来标志
  70. $json = $this->getInput('_json');
  71. $requestJson = $this->getRequest()->getIsAjaxRequest() && strpos(strtolower($type), "application/json") !== false;
  72. if ($requestJson || $json == 1) {
  73. $this->getResponse()->setHeader('Content-type', 'application/json; charset=' . Wekit::V('charset'));
  74. echo Pw::jsonEncode($this->getForward()->getVars());
  75. exit();
  76. }
  77. }
  78. /**
  79. * 风格设置
  80. *
  81. * 设置当前页面风格,需要两个参数,$type风格类型,$theme该类型下风格
  82. *
  83. * @see WindSimpleController::setTheme()
  84. * @param string $type 风格类型(site,space,area...)
  85. * @param string $theme 风格别名
  86. */
  87. protected function setTheme($type, $theme) {
  88. $themePack = Wekit::C('site', 'theme.' . $type . '.pack');
  89. $themePack = 'THEMES:' . $themePack;
  90. if (!$theme) $theme = Wekit::C('site', 'theme.' . $type . '.default');
  91. parent::setTheme($theme, $themePack);
  92. }
  93. }