HttpController.php 5.2 KB

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