HttpController.php 5.7 KB

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