PublicTemplateResponse.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /**
  11. * Class PublicTemplateResponse
  12. *
  13. * @since 14.0.0
  14. * @template H of array<string, mixed>
  15. * @template S of int
  16. * @template-extends TemplateResponse<int, array<string, mixed>>
  17. */
  18. class PublicTemplateResponse extends TemplateResponse {
  19. private $headerTitle = '';
  20. private $headerDetails = '';
  21. private $headerActions = [];
  22. private $footerVisible = true;
  23. /**
  24. * PublicTemplateResponse constructor.
  25. *
  26. * @param string $appName
  27. * @param string $templateName
  28. * @param array $params
  29. * @param S $status
  30. * @param H $headers
  31. * @since 14.0.0
  32. */
  33. public function __construct(string $appName, string $templateName, array $params = [], $status = Http::STATUS_OK, array $headers = []) {
  34. parent::__construct($appName, $templateName, $params, 'public', $status, $headers);
  35. \OC_Util::addScript('core', 'public/publicpage');
  36. }
  37. /**
  38. * @param string $title
  39. * @since 14.0.0
  40. */
  41. public function setHeaderTitle(string $title) {
  42. $this->headerTitle = $title;
  43. }
  44. /**
  45. * @return string
  46. * @since 14.0.0
  47. */
  48. public function getHeaderTitle(): string {
  49. return $this->headerTitle;
  50. }
  51. /**
  52. * @param string $details
  53. * @since 14.0.0
  54. */
  55. public function setHeaderDetails(string $details) {
  56. $this->headerDetails = $details;
  57. }
  58. /**
  59. * @return string
  60. * @since 14.0.0
  61. */
  62. public function getHeaderDetails(): string {
  63. return $this->headerDetails;
  64. }
  65. /**
  66. * @param array $actions
  67. * @since 14.0.0
  68. * @throws InvalidArgumentException
  69. */
  70. public function setHeaderActions(array $actions) {
  71. foreach ($actions as $action) {
  72. if ($actions instanceof IMenuAction) {
  73. throw new InvalidArgumentException('Actions must be of type IMenuAction');
  74. }
  75. $this->headerActions[] = $action;
  76. }
  77. usort($this->headerActions, function (IMenuAction $a, IMenuAction $b) {
  78. return $a->getPriority() <=> $b->getPriority();
  79. });
  80. }
  81. /**
  82. * @return IMenuAction
  83. * @since 14.0.0
  84. * @throws \Exception
  85. */
  86. public function getPrimaryAction(): IMenuAction {
  87. if ($this->getActionCount() > 0) {
  88. return $this->headerActions[0];
  89. }
  90. throw new \Exception('No header actions have been set');
  91. }
  92. /**
  93. * @return int
  94. * @since 14.0.0
  95. */
  96. public function getActionCount(): int {
  97. return count($this->headerActions);
  98. }
  99. /**
  100. * @return IMenuAction[]
  101. * @since 14.0.0
  102. */
  103. public function getOtherActions(): array {
  104. return array_slice($this->headerActions, 1);
  105. }
  106. /**
  107. * @since 14.0.0
  108. */
  109. public function setFooterVisible(bool $visible = false) {
  110. $this->footerVisible = $visible;
  111. }
  112. /**
  113. * @since 14.0.0
  114. */
  115. public function getFooterVisible(): bool {
  116. return $this->footerVisible;
  117. }
  118. /**
  119. * @return string
  120. * @since 14.0.0
  121. */
  122. public function render(): string {
  123. $params = array_merge($this->getParams(), [
  124. 'template' => $this,
  125. ]);
  126. $this->setParams($params);
  127. return parent::render();
  128. }
  129. }