1
0

TemplateResponse.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCP\AppFramework\Http;
  8. use OCP\AppFramework\Http;
  9. /**
  10. * Response for a normal template
  11. * @since 6.0.0
  12. *
  13. * @template S of int
  14. * @template H of array<string, mixed>
  15. * @template-extends Response<int, array<string, mixed>>
  16. */
  17. class TemplateResponse extends Response {
  18. /**
  19. * @since 20.0.0
  20. */
  21. public const RENDER_AS_GUEST = 'guest';
  22. /**
  23. * @since 20.0.0
  24. */
  25. public const RENDER_AS_BLANK = '';
  26. /**
  27. * @since 20.0.0
  28. */
  29. public const RENDER_AS_BASE = 'base';
  30. /**
  31. * @since 20.0.0
  32. */
  33. public const RENDER_AS_USER = 'user';
  34. /**
  35. * @since 20.0.0
  36. */
  37. public const RENDER_AS_ERROR = 'error';
  38. /**
  39. * @since 20.0.0
  40. */
  41. public const RENDER_AS_PUBLIC = 'public';
  42. /**
  43. * name of the template
  44. * @var string
  45. */
  46. protected $templateName;
  47. /**
  48. * parameters
  49. * @var array
  50. */
  51. protected $params;
  52. /**
  53. * rendering type (admin, user, blank)
  54. * @var string
  55. */
  56. protected $renderAs;
  57. /**
  58. * app name
  59. * @var string
  60. */
  61. protected $appName;
  62. /**
  63. * constructor of TemplateResponse
  64. * @param string $appName the name of the app to load the template from
  65. * @param string $templateName the name of the template
  66. * @param array $params an array of parameters which should be passed to the
  67. * template
  68. * @param string $renderAs how the page should be rendered, defaults to user
  69. * @param S $status
  70. * @param H $headers
  71. * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
  72. */
  73. public function __construct(string $appName, string $templateName, array $params = [], string $renderAs = self::RENDER_AS_USER, int $status = Http::STATUS_OK, array $headers = []) {
  74. parent::__construct($status, $headers);
  75. $this->templateName = $templateName;
  76. $this->appName = $appName;
  77. $this->params = $params;
  78. $this->renderAs = $renderAs;
  79. $this->setContentSecurityPolicy(new ContentSecurityPolicy());
  80. $this->setFeaturePolicy(new FeaturePolicy());
  81. }
  82. /**
  83. * Sets template parameters
  84. * @param array $params an array with key => value structure which sets template
  85. * variables
  86. * @return TemplateResponse Reference to this object
  87. * @since 6.0.0 - return value was added in 7.0.0
  88. */
  89. public function setParams(array $params) {
  90. $this->params = $params;
  91. return $this;
  92. }
  93. /**
  94. * Used for accessing the set parameters
  95. * @return array the params
  96. * @since 6.0.0
  97. */
  98. public function getParams() {
  99. return $this->params;
  100. }
  101. /**
  102. * @return string the app id of the used template
  103. * @since 25.0.0
  104. */
  105. public function getApp(): string {
  106. return $this->appName;
  107. }
  108. /**
  109. * Used for accessing the name of the set template
  110. * @return string the name of the used template
  111. * @since 6.0.0
  112. */
  113. public function getTemplateName() {
  114. return $this->templateName;
  115. }
  116. /**
  117. * Sets the template page
  118. * @param string $renderAs admin, user or blank. Admin also prints the admin
  119. * settings header and footer, user renders the normal
  120. * normal page including footer and header and blank
  121. * just renders the plain template
  122. * @return TemplateResponse Reference to this object
  123. * @since 6.0.0 - return value was added in 7.0.0
  124. */
  125. public function renderAs($renderAs) {
  126. $this->renderAs = $renderAs;
  127. return $this;
  128. }
  129. /**
  130. * Returns the set renderAs
  131. * @return string the renderAs value
  132. * @since 6.0.0
  133. */
  134. public function getRenderAs() {
  135. return $this->renderAs;
  136. }
  137. /**
  138. * Returns the rendered html
  139. * @return string the rendered html
  140. * @since 6.0.0
  141. */
  142. public function render() {
  143. $renderAs = self::RENDER_AS_USER;
  144. if ($this->renderAs === 'blank') {
  145. // Legacy fallback as \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
  146. $renderAs = self::RENDER_AS_BLANK;
  147. } elseif (in_array($this->renderAs, [
  148. self::RENDER_AS_GUEST,
  149. self::RENDER_AS_BLANK,
  150. self::RENDER_AS_BASE,
  151. self::RENDER_AS_ERROR,
  152. self::RENDER_AS_PUBLIC,
  153. self::RENDER_AS_USER], true)) {
  154. $renderAs = $this->renderAs;
  155. }
  156. $template = new \OCP\Template($this->appName, $this->templateName, $renderAs);
  157. foreach ($this->params as $key => $value) {
  158. $template->assign($key, $value);
  159. }
  160. return $template->fetchPage($this->params);
  161. }
  162. }