PublicTemplateResponse.php 3.9 KB

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