PublicTemplateResponse.php 3.6 KB

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