Response.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\Http;
  3. use ModulesGarden\Servers\ProxmoxVps\Core\Http\View\Smarty;
  4. use ModulesGarden\Servers\ProxmoxVps\Core\ModuleConstants;
  5. use ModulesGarden\Servers\ProxmoxVps\Core\Traits\AppParams;
  6. use ModulesGarden\Servers\ProxmoxVps\Core\ServiceLocator;
  7. use ModulesGarden\Servers\ProxmoxVps\Core\DependencyInjection;
  8. use ModulesGarden\Servers\ProxmoxVps\Core\Http\View\MainMenu;
  9. use ModulesGarden\Servers\ProxmoxVps\Core\Helper\BuildUrl;
  10. use ModulesGarden\Servers\ProxmoxVps\Core\App\Controllers\Instances\Addon\Config;
  11. use ModulesGarden\Servers\ProxmoxVps\Core\Helper\WhmcsVersionComparator;
  12. use Symfony\Component\HttpFoundation\Response as SymfonyRespose;
  13. /**
  14. * Description of Response
  15. *
  16. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  17. */
  18. class Response extends SymfonyRespose
  19. {
  20. use \ModulesGarden\Servers\ProxmoxVps\Core\Traits\Template;
  21. use \ModulesGarden\Servers\ProxmoxVps\Core\Traits\IsAdmin;
  22. use \ModulesGarden\Servers\ProxmoxVps\Core\UI\Traits\RequestObjectHandler;
  23. use AppParams;
  24. protected $data = [];
  25. protected $lang;
  26. protected $staticName;
  27. protected $isBreadcrumbs = true;
  28. protected $isDebug = null;
  29. /*
  30. * determines data return type as HTML only
  31. */
  32. protected $forceHtml = false;
  33. public function setLang($lang)
  34. {
  35. $this->lang = $lang;
  36. return $this;
  37. }
  38. public function setBreadcrumbs($isBreadcrumbs)
  39. {
  40. $this->isBreadcrumbs = $isBreadcrumbs;
  41. return $this;
  42. }
  43. public function isBreadcrumbs()
  44. {
  45. return $this->isBreadcrumbs;
  46. }
  47. public function setName($name)
  48. {
  49. $this->staticName = $name;
  50. return $this;
  51. }
  52. public function getName()
  53. {
  54. return $this->staticName;
  55. }
  56. public function getLang()
  57. {
  58. if (empty($this->lang))
  59. {
  60. $this->lang = ServiceLocator::call('lang');
  61. }
  62. return $this->lang;
  63. }
  64. public function setData(array $data)
  65. {
  66. $this->data = $data;
  67. return $this;
  68. }
  69. public function getError()
  70. {
  71. $data = $this->getData();
  72. if (isset($data['status']) && $data['status'] == 'error')
  73. {
  74. return $data['message'];
  75. }
  76. return false;
  77. }
  78. public function getSuccess()
  79. {
  80. $data = $this->getData();
  81. if (isset($data['status']) && $data['status'] == 'success')
  82. {
  83. return $data['message'];
  84. }
  85. return false;
  86. }
  87. public function getData($key = null, $dafault = null)
  88. {
  89. if ($key == null)
  90. {
  91. return $this->data;
  92. }
  93. if (isset($this->data[$key]) || array_key_exists($key, $this->data))
  94. {
  95. return $this->data[$key];
  96. }
  97. return $dafault;
  98. }
  99. public function withSuccess($message = '')
  100. {
  101. $data = $this->getData();
  102. $data['status'] = 'success';
  103. $data['message'] = $message;
  104. $this->setData($data);
  105. return $this;
  106. }
  107. public function withError($message = '')
  108. {
  109. $data = $this->getData();
  110. $data['status'] = 'error';
  111. $data['message'] = $message;
  112. $this->setData($data);
  113. return $this;
  114. }
  115. public function getPageContext()
  116. {
  117. $tpl = $this->getData('tpl', 'home');
  118. return ServiceLocator::call('smarty')
  119. ->setLang($this->getLang())
  120. ->view($tpl, $this->getData('data', []), $this->getData('tplDir', false));
  121. }
  122. /**
  123. * @param $responseResolver \ModulesGarden\Servers\ProxmoxVps\Core\App\Controllers\ResponseResolver
  124. */
  125. public function getHtmlResponse($responseResolver)
  126. {
  127. $pageController = $responseResolver->getPageController();
  128. $path = $responseResolver->getTemplateDir();
  129. $fileName = $pageController->getTemplateName() ? : 'main';
  130. $controller = $pageController->getControllerClass(true);
  131. $action = $pageController->getControllerMethod();
  132. $mainMenu = DependencyInjection::create(MainMenu::class)->buildBreadcrumb($controller, $action, []);
  133. $menu = $mainMenu->getMenu();
  134. $addon = ServiceLocator::call(Config::class);
  135. $vars = [
  136. 'assetsURL' => BuildUrl::getAssetsURL(),
  137. 'customAssetsURL' => BuildUrl::getAssetsURL(true),
  138. 'isCustomIntegrationCss' => BuildUrl::isCustomIntegrationCss(),
  139. 'isCustomModuleCss' => BuildUrl::isCustomModuleCss(),
  140. 'mainURL' => BuildUrl::getUrl(),
  141. 'mainName' => ($this->staticName === null) ? $addon->getConfigValue('name') : $this->staticName,
  142. 'menu' => $menu,
  143. 'breadcrumbs' => ($this->isBreadcrumbs) ? $this->data['data']['breadcrumbs'] : null,
  144. 'JSONCurrentUrl' => BuildUrl::getUrl($controller),
  145. 'currentPageName' => $controller,
  146. 'mgWhmcsVersionComparator' => new WhmcsVersionComparator(),
  147. 'content' => $this->getPageContext(),
  148. 'moduleRequirementsErrors' => $this->checkModuleRequirements(),
  149. 'error' => $this->getData('status', false) == 'error' ? $this->getData('message', '') : false,
  150. 'success' => $this->getData('status', false) == 'success' ? $this->getData('message', '') : false,
  151. 'tagImageModule' => $addon->getConfigValue('moduleIcon'),
  152. 'isDebug' => (bool)((int)$addon->getConfigValue('debug', "0")),
  153. 'errorPageDetails' => $this->getErrorPageData($responseResolver)
  154. ];
  155. try
  156. {
  157. $this->loadLangContext();
  158. if ((!$responseResolver->isAdmin() && !$this->forceHtml))
  159. {
  160. $vars['MGLANG'] = $this->lang;
  161. if (strpos(trim(self::class, '\\'), 'ModulesGarden\Servers') === 0)
  162. {
  163. return $this->returnClientProvisioning($vars, $path, $fileName);
  164. }
  165. return $this->returnClientAddon($vars, $path, $fileName);
  166. }
  167. /**
  168. * @var Smarty $pageContent
  169. */
  170. $pageContent = ServiceLocator::call('smarty')
  171. ->setLang($this->lang)
  172. ->setTemplateDir($path)
  173. ->view($fileName, $vars);
  174. return $pageContent;
  175. }
  176. catch (\Exception $e)
  177. {
  178. ServiceLocator::call('errorManager')->addError(self::class, $e->getMessage(), $e->getTrace());
  179. }
  180. }
  181. public function returnClientAddon($vars, $path, $fileName)
  182. {
  183. return [
  184. 'vars' => $vars,
  185. //'pagetitle' => $this->lang->absoluteT("AddonCA","pagetitle","Reseller Area"), TODO!!!
  186. 'templatefile' => str_replace(\ModulesGarden\Servers\ProxmoxVps\Core\ModuleConstants::getTemplateDir() . DIRECTORY_SEPARATOR,
  187. '', $path . DIRECTORY_SEPARATOR . $fileName),
  188. 'requirelogin' => true,
  189. 'breadcrumb' => ($this->isBreadcrumbs) ? $this->data['data']['breadcrumbs'] : null
  190. ];
  191. }
  192. public function returnClientProvisioning($vars, $path, $fileName)
  193. {
  194. // $templateVarName = ($this->getRequestValue('a', false) === 'management') ? 'tabOverviewReplacementTemplate' : 'templatefile';
  195. $templateVarName = 'tabOverviewReplacementTemplate';
  196. return [
  197. $templateVarName => str_replace(\ModulesGarden\Servers\ProxmoxVps\Core\ModuleConstants::getTemplateDir() . DIRECTORY_SEPARATOR,
  198. '', $path . DIRECTORY_SEPARATOR . $fileName),
  199. 'templateVariables' => $vars
  200. ];
  201. }
  202. protected function checkModuleRequirements()
  203. {
  204. $requirementsHandler = new \ModulesGarden\Servers\ProxmoxVps\Core\App\Requirements\Checker();
  205. $requirementsErrors = $requirementsHandler->getUnfulfilledRequirements();
  206. if ($requirementsErrors)
  207. {
  208. return implode('<br>', $requirementsErrors);
  209. }
  210. return $this->getData('status', false) == 'error' ? $this->getData('message', '') : false;
  211. }
  212. /**
  213. * Set context lang ( AdminArea or ClientArea )
  214. */
  215. protected function loadLangContext()
  216. {
  217. $this->lang->setContext(($this->getType() . ($this->isAdmin() ? 'AA' : 'CA')));
  218. }
  219. /**
  220. * @return string
  221. */
  222. protected function getType()
  223. {
  224. return ModuleConstants::getModuleType();
  225. }
  226. public function setForceHtml()
  227. {
  228. $this->forceHtml = true;
  229. return $this;
  230. }
  231. public function unsetForceHtml()
  232. {
  233. $this->forceHtml = false;
  234. return $this;
  235. }
  236. public function isDebugOn()
  237. {
  238. if ($this->isDebug === null)
  239. {
  240. $addon = ServiceLocator::call(Config::class);
  241. $this->isDebug = (bool)((int)$addon->getConfigValue('debug', "0"));
  242. }
  243. return $this->isDebug;
  244. }
  245. public function getErrorPageData($responseResolver)
  246. {
  247. $pageController = $responseResolver->getPageController();
  248. $error = $pageController->getParam('mgErrorDetails');
  249. if (!$error)
  250. {
  251. return null;
  252. }
  253. $errorDetails = $error->getDetailsToDisplay();
  254. return $errorDetails;
  255. }
  256. }