PublicTemplateResponse.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCP\AppFramework\Http\Template;
  7. use InvalidArgumentException;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\TemplateResponse;
  10. use OCP\IInitialStateService;
  11. /**
  12. * Class PublicTemplateResponse
  13. *
  14. * @since 14.0.0
  15. * @template H of array<string, mixed>
  16. * @template S of int
  17. * @template-extends TemplateResponse<int, array<string, mixed>>
  18. */
  19. class PublicTemplateResponse extends TemplateResponse {
  20. private $headerTitle = '';
  21. private $headerDetails = '';
  22. /** @var IMenuAction[] */
  23. private $headerActions = [];
  24. private $footerVisible = true;
  25. /**
  26. * PublicTemplateResponse constructor.
  27. *
  28. * @param string $appName
  29. * @param string $templateName
  30. * @param array $params
  31. * @param S $status
  32. * @param H $headers
  33. * @since 14.0.0
  34. */
  35. public function __construct(
  36. string $appName,
  37. string $templateName,
  38. array $params = [],
  39. $status = Http::STATUS_OK,
  40. array $headers = [],
  41. ) {
  42. parent::__construct($appName, $templateName, $params, 'public', $status, $headers);
  43. \OCP\Util::addScript('core', 'public-page-menu');
  44. $state = \OCP\Server::get(IInitialStateService::class);
  45. $state->provideLazyInitialState('core', 'public-page-menu', function () {
  46. $response = [];
  47. foreach ($this->headerActions as $action) {
  48. // First try in it is a custom action that provides rendered HTML
  49. $rendered = $action->render();
  50. if ($rendered === '') {
  51. // If simple action, add the response data
  52. if ($action instanceof SimpleMenuAction) {
  53. $response[] = $action->getData();
  54. }
  55. } else {
  56. // custom action so add the rendered output
  57. $response[] = [
  58. 'id' => $action->getId(),
  59. 'label' => $action->getLabel(),
  60. 'html' => $rendered,
  61. ];
  62. }
  63. }
  64. return $response;
  65. });
  66. }
  67. /**
  68. * @param string $title
  69. * @since 14.0.0
  70. */
  71. public function setHeaderTitle(string $title) {
  72. $this->headerTitle = $title;
  73. }
  74. /**
  75. * @return string
  76. * @since 14.0.0
  77. */
  78. public function getHeaderTitle(): string {
  79. return $this->headerTitle;
  80. }
  81. /**
  82. * @param string $details
  83. * @since 14.0.0
  84. */
  85. public function setHeaderDetails(string $details) {
  86. $this->headerDetails = $details;
  87. }
  88. /**
  89. * @return string
  90. * @since 14.0.0
  91. */
  92. public function getHeaderDetails(): string {
  93. return $this->headerDetails;
  94. }
  95. /**
  96. * @param array $actions
  97. * @since 14.0.0
  98. * @throws InvalidArgumentException
  99. */
  100. public function setHeaderActions(array $actions) {
  101. foreach ($actions as $action) {
  102. if ($actions instanceof IMenuAction) {
  103. throw new InvalidArgumentException('Actions must be of type IMenuAction');
  104. }
  105. $this->headerActions[] = $action;
  106. }
  107. usort($this->headerActions, function (IMenuAction $a, IMenuAction $b) {
  108. return $a->getPriority() <=> $b->getPriority();
  109. });
  110. }
  111. /**
  112. * @return IMenuAction
  113. * @since 14.0.0
  114. * @throws \Exception
  115. */
  116. public function getPrimaryAction(): IMenuAction {
  117. if ($this->getActionCount() > 0) {
  118. return $this->headerActions[0];
  119. }
  120. throw new \Exception('No header actions have been set');
  121. }
  122. /**
  123. * @return int
  124. * @since 14.0.0
  125. */
  126. public function getActionCount(): int {
  127. return count($this->headerActions);
  128. }
  129. /**
  130. * @return IMenuAction[]
  131. * @since 14.0.0
  132. */
  133. public function getOtherActions(): array {
  134. return array_slice($this->headerActions, 1);
  135. }
  136. /**
  137. * @since 14.0.0
  138. */
  139. public function setFooterVisible(bool $visible = false) {
  140. $this->footerVisible = $visible;
  141. }
  142. /**
  143. * @since 14.0.0
  144. */
  145. public function getFooterVisible(): bool {
  146. return $this->footerVisible;
  147. }
  148. /**
  149. * @return string
  150. * @since 14.0.0
  151. */
  152. public function render(): string {
  153. $params = array_merge($this->getParams(), [
  154. 'template' => $this,
  155. ]);
  156. $this->setParams($params);
  157. return parent::render();
  158. }
  159. }