View.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\UI;
  3. use \ModulesGarden\Servers\ProxmoxVps\Core\Helper;
  4. use \ModulesGarden\Servers\ProxmoxVps\Core\Http\Request;
  5. use \ModulesGarden\Servers\ProxmoxVps\Core\ModuleConstants;
  6. use \ModulesGarden\Servers\ProxmoxVps\Core\UI\Helpers\TemplateConstants;
  7. use \ModulesGarden\Servers\ProxmoxVps\Core\Traits\AppParams;
  8. use \ModulesGarden\Servers\ProxmoxVps\Core\Traits\IsDebugOn;
  9. /**
  10. * Main View Controller
  11. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  12. */
  13. class View
  14. {
  15. use Traits\AppLayouts;
  16. use Traits\CustomJsCode;
  17. use Traits\ViewBreadcrumb;
  18. use AppParams;
  19. use IsDebugOn;
  20. /**
  21. * Controler for all widgets inside of View
  22. * @var \ModulesGarden\Servers\ProxmoxVps\Core\UI\MainContainer
  23. */
  24. protected $mainContainer = null;
  25. protected $template = null;
  26. protected $name;
  27. protected $isBreadcrumbs = true;
  28. protected $templateDir = null;
  29. protected $defaultComponentsJs = null;
  30. protected $integration = false;
  31. public function __construct ($template = null)
  32. {
  33. $this->setTemplate($template);
  34. $this->mainContainer = new \ModulesGarden\Servers\ProxmoxVps\Core\UI\MainContainer();
  35. $this->initBreadcrumbs();
  36. $this->initCustomAssetFiles();
  37. }
  38. /**
  39. * Adds elements to the root element
  40. */
  41. public function addElement($element, $containerName = null)
  42. {
  43. $this->mainContainer->addElement($element, $containerName);
  44. return $this;
  45. }
  46. /**
  47. * Generates all responses for UI elements
  48. */
  49. public function getResponse()
  50. {
  51. $this->mainContainer->setTemplate($this->getAppLayoutTemplateDir(), $this->getAppLayout());
  52. $request = Request::build();
  53. if ($request->get('ajax', false))
  54. {
  55. return $this->mainContainer->getAjaxResponse();
  56. }
  57. return Helper\response([
  58. 'tpl' => $this->template,
  59. 'tplDir' => $this->templateDir,
  60. 'data' => [
  61. 'mainContainer' => $this->mainContainer,
  62. 'customJsCode' => $this->getCustomJsCode(),
  63. 'customCssCode' => $this->getCustomCssCode(),
  64. 'breadcrumbs' => $this->getBreadcrumbs()
  65. ]
  66. ])->setStatusCode(200)->setName($this->name)->setBreadcrumbs($this->isBreadcrumbs);
  67. }
  68. public function validateAcl ($isAdmin)
  69. {
  70. $this->mainContainer->valicateACL($isAdmin);
  71. return $this;
  72. }
  73. /**
  74. * Sets custom View template
  75. */
  76. public function setTemplate ($template = null)
  77. {
  78. if ($template === null)
  79. {
  80. $this->template = 'view';
  81. $this->templateDir = ModuleConstants::getTemplateDir()
  82. . DS . (Helper\isAdmin() ? TemplateConstants::ADMIN_PATH : TemplateConstants::CLIENT_PATH)
  83. . DS . TemplateConstants::MAIN_DIR;
  84. return;
  85. }
  86. $this->template = $template;
  87. $this->templateDir = null;
  88. }
  89. public function setName($name)
  90. {
  91. $this->name = $name;
  92. return $this;
  93. }
  94. public function disableBreadcrumbs()
  95. {
  96. $this->isBreadcrumbs = false;
  97. return $this;
  98. }
  99. /**
  100. * this function is deprecated
  101. * @return type
  102. */
  103. public function genResponse()
  104. {
  105. return $this->getResponse();
  106. }
  107. public function setIsIntegration($isIntegration = false)
  108. {
  109. if (is_bool($isIntegration))
  110. {
  111. $this->integration = $isIntegration;
  112. $this->initCustomAssetFiles();
  113. }
  114. }
  115. public function isIntegration()
  116. {
  117. return $this->integration;
  118. }
  119. }