HttpController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\App\Controllers\Instances;
  3. use \ThurData\Servers\KerioEmail\Core\App\Controllers\Interfaces\AdminArea;
  4. use \ThurData\Servers\KerioEmail\Core\App\Controllers\Interfaces\ClientArea;
  5. use \ThurData\Servers\KerioEmail\Core\App\Controllers\Interfaces\DefaultController;
  6. use ThurData\Servers\KerioEmail\Core\App\Controllers\ResponseResolver;
  7. use ThurData\Servers\KerioEmail\Core\App\Controllers\Router;
  8. use \ThurData\Servers\KerioEmail\Core\DependencyInjection;
  9. use ThurData\Servers\KerioEmail\Core\ModuleConstants;
  10. abstract class HttpController implements DefaultController
  11. {
  12. use \ThurData\Servers\KerioEmail\Core\Traits\Lang;
  13. use \ThurData\Servers\KerioEmail\Core\Traits\Smarty;
  14. use \ThurData\Servers\KerioEmail\Core\Traits\OutputBuffer;
  15. use \ThurData\Servers\KerioEmail\Core\Traits\IsAdmin;
  16. use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
  17. use \ThurData\Servers\KerioEmail\Core\Traits\ErrorCodesLibrary;
  18. use \ThurData\Servers\KerioEmail\Core\Traits\Params;
  19. use \ThurData\Servers\KerioEmail\Core\Traits\AppParams;
  20. const ADMIN = 'admin';
  21. const CLIENT = 'client';
  22. protected $templateName = 'main';
  23. protected $templateContext = 'default';
  24. protected $templateDir = null;
  25. protected $controllerResult = null;
  26. /**
  27. * @var Router|null
  28. *
  29. */
  30. protected $router = null;
  31. protected $responseResolver = null;
  32. public function __construct()
  33. {
  34. $this->loadSmarty();
  35. $this->isAdmin();
  36. $this->router = new Router();
  37. $this->responseResolver = new ResponseResolver();
  38. }
  39. public function execute($params = null)
  40. {
  41. $this->setParams($params);
  42. logModuleCall(
  43. 'kerioEmail',
  44. __FUNCTION__,
  45. $params,
  46. 'Debug Controller',
  47. $this->router
  48. );
  49. if (!$this->router->isControllerCallable() || !$this->isAdminContextValid())
  50. {
  51. return $this->controllerResult = $this->getPageNotFound();
  52. }
  53. else
  54. {
  55. $this->setAppParam('HttpControlerName', $this->router->getControllerClass());
  56. $this->setAppParam('HttpControlerMethod', $this->router->getControllerMethod());
  57. $this->controllerResult = $this->getControllerResponse();
  58. }
  59. return $this->resolveResponse();
  60. }
  61. public function resolveResponse()
  62. {
  63. return $this->responseResolver->setResponse($this->controllerResult)
  64. ->setTemplateName($this->getTemplateName())
  65. ->setTemplateDir($this->getTemplateDir())
  66. ->setPageController($this)
  67. ->resolve();
  68. }
  69. public function isAdminContextValid()
  70. {
  71. if ($this->isAdmin() && !($this instanceof AdminArea))
  72. {
  73. return false;
  74. }
  75. if (!$this->isAdmin() && !($this instanceof ClientArea))
  76. {
  77. return false;
  78. }
  79. return true;
  80. }
  81. public function getPageNotFound()
  82. {
  83. $notFound = new Http\PageNotFound();
  84. return $notFound->execute();
  85. }
  86. protected function getControllerResponse()
  87. {
  88. $this->loadLang();
  89. $this->lang->setContext(($this->getType() . ($this->isAdmin() ? 'AA' : 'CA')), lcfirst($this->getControllerClass(true)));
  90. $result = DependencyInjection::create(
  91. $this->router->getControllerClass(),
  92. $this->router->getControllerMethod()
  93. );
  94. return $result;
  95. }
  96. public function getTemplateName()
  97. {
  98. return $this->templateName;
  99. }
  100. public function getTemplateDir()
  101. {
  102. if ($this->templateDir === null)
  103. {
  104. $this->templateDir = ModuleConstants::getTemplateDir() . DIRECTORY_SEPARATOR .
  105. ($this->isAdmin() ? self::ADMIN : (self::CLIENT . DIRECTORY_SEPARATOR . $this->getTemplateContext()))
  106. . DIRECTORY_SEPARATOR . 'controlers';
  107. }
  108. return $this->templateDir;
  109. }
  110. public function getTemplateContext()
  111. {
  112. return $this->templateContext;
  113. }
  114. public function getControllerClass($raw = false)
  115. {
  116. if ($raw)
  117. {
  118. $namespaceParts = explode('\\', $this->getControllerClass());
  119. return end($namespaceParts);
  120. }
  121. return $this->router->getControllerClass();
  122. }
  123. public function getControllerMethod()
  124. {
  125. return $this->router->getControllerMethod();
  126. }
  127. /**
  128. * @param null $controllerResult
  129. */
  130. public function setControllerResult ($controllerResult)
  131. {
  132. $this->controllerResult = $controllerResult;
  133. }
  134. /**
  135. * @return null
  136. */
  137. public function getControllerResult ()
  138. {
  139. return $this->controllerResult;
  140. }
  141. /**
  142. * @return string
  143. */
  144. protected function getType()
  145. {
  146. return 'addon';
  147. }
  148. public function runExecuteProcess($params = null)
  149. {
  150. return $this->execute($params);
  151. }
  152. }