View.php 3.1 KB

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