PublicTemplateResponse.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCP\AppFramework\Http\Template;
  26. use InvalidArgumentException;
  27. use OCP\AppFramework\Http\TemplateResponse;
  28. /**
  29. * Class PublicTemplateResponse
  30. *
  31. * @since 14.0.0
  32. */
  33. class PublicTemplateResponse extends TemplateResponse {
  34. private $headerTitle = '';
  35. private $headerDetails = '';
  36. private $headerActions = [];
  37. private $footerVisible = true;
  38. /**
  39. * PublicTemplateResponse constructor.
  40. *
  41. * @param string $appName
  42. * @param string $templateName
  43. * @param array $params
  44. * @since 14.0.0
  45. */
  46. public function __construct(string $appName, string $templateName, array $params = []) {
  47. parent::__construct($appName, $templateName, $params, 'public');
  48. \OC_Util::addScript('core', 'public/publicpage');
  49. }
  50. /**
  51. * @param string $title
  52. * @since 14.0.0
  53. */
  54. public function setHeaderTitle(string $title) {
  55. $this->headerTitle = $title;
  56. }
  57. /**
  58. * @return string
  59. * @since 14.0.0
  60. */
  61. public function getHeaderTitle(): string {
  62. return $this->headerTitle;
  63. }
  64. /**
  65. * @param string $details
  66. * @since 14.0.0
  67. */
  68. public function setHeaderDetails(string $details) {
  69. $this->headerDetails = $details;
  70. }
  71. /**
  72. * @return string
  73. * @since 14.0.0
  74. */
  75. public function getHeaderDetails(): string {
  76. return $this->headerDetails;
  77. }
  78. /**
  79. * @param array $actions
  80. * @since 14.0.0
  81. * @throws InvalidArgumentException
  82. */
  83. public function setHeaderActions(array $actions) {
  84. foreach ($actions as $action) {
  85. if ($actions instanceof IMenuAction) {
  86. throw new InvalidArgumentException('Actions must be of type IMenuAction');
  87. }
  88. $this->headerActions[] = $action;
  89. }
  90. usort($this->headerActions, function (IMenuAction $a, IMenuAction $b) {
  91. return $a->getPriority() <=> $b->getPriority();
  92. });
  93. }
  94. /**
  95. * @return IMenuAction
  96. * @since 14.0.0
  97. * @throws \Exception
  98. */
  99. public function getPrimaryAction(): IMenuAction {
  100. if ($this->getActionCount() > 0) {
  101. return $this->headerActions[0];
  102. }
  103. throw new \Exception('No header actions have been set');
  104. }
  105. /**
  106. * @return int
  107. * @since 14.0.0
  108. */
  109. public function getActionCount(): int {
  110. return count($this->headerActions);
  111. }
  112. /**
  113. * @return IMenuAction[]
  114. * @since 14.0.0
  115. */
  116. public function getOtherActions(): array {
  117. return array_slice($this->headerActions, 1);
  118. }
  119. /**
  120. * @since 14.0.0
  121. */
  122. public function setFooterVisible(bool $visible = false) {
  123. $this->footerVisible = $visible;
  124. }
  125. /**
  126. * @since 14.0.0
  127. */
  128. public function getFooterVisible(): bool {
  129. return $this->footerVisible;
  130. }
  131. /**
  132. * @return string
  133. * @since 14.0.0
  134. */
  135. public function render(): string {
  136. $params = array_merge($this->getParams(), [
  137. 'template' => $this,
  138. ]);
  139. $this->setParams($params);
  140. return parent::render();
  141. }
  142. }