Response.php 8.7 KB

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