1
0

TemplateResponse.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Thomas Tanghus <thomas@tanghus.net>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. /**
  26. * Public interface of ownCloud for apps to use.
  27. * AppFramework\HTTP\TemplateResponse class
  28. */
  29. namespace OCP\AppFramework\Http;
  30. /**
  31. * Response for a normal template
  32. * @since 6.0.0
  33. */
  34. class TemplateResponse extends Response {
  35. const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts';
  36. const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn';
  37. /**
  38. * name of the template
  39. * @var string
  40. */
  41. protected $templateName;
  42. /**
  43. * parameters
  44. * @var array
  45. */
  46. protected $params;
  47. /**
  48. * rendering type (admin, user, blank)
  49. * @var string
  50. */
  51. protected $renderAs;
  52. /**
  53. * app name
  54. * @var string
  55. */
  56. protected $appName;
  57. /**
  58. * constructor of TemplateResponse
  59. * @param string $appName the name of the app to load the template from
  60. * @param string $templateName the name of the template
  61. * @param array $params an array of parameters which should be passed to the
  62. * template
  63. * @param string $renderAs how the page should be rendered, defaults to user
  64. * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
  65. */
  66. public function __construct($appName, $templateName, array $params=array(),
  67. $renderAs='user') {
  68. parent::__construct();
  69. $this->templateName = $templateName;
  70. $this->appName = $appName;
  71. $this->params = $params;
  72. $this->renderAs = $renderAs;
  73. $this->setContentSecurityPolicy(new ContentSecurityPolicy());
  74. }
  75. /**
  76. * Sets template parameters
  77. * @param array $params an array with key => value structure which sets template
  78. * variables
  79. * @return TemplateResponse Reference to this object
  80. * @since 6.0.0 - return value was added in 7.0.0
  81. */
  82. public function setParams(array $params){
  83. $this->params = $params;
  84. return $this;
  85. }
  86. /**
  87. * Used for accessing the set parameters
  88. * @return array the params
  89. * @since 6.0.0
  90. */
  91. public function getParams(){
  92. return $this->params;
  93. }
  94. /**
  95. * Used for accessing the name of the set template
  96. * @return string the name of the used template
  97. * @since 6.0.0
  98. */
  99. public function getTemplateName(){
  100. return $this->templateName;
  101. }
  102. /**
  103. * Sets the template page
  104. * @param string $renderAs admin, user or blank. Admin also prints the admin
  105. * settings header and footer, user renders the normal
  106. * normal page including footer and header and blank
  107. * just renders the plain template
  108. * @return TemplateResponse Reference to this object
  109. * @since 6.0.0 - return value was added in 7.0.0
  110. */
  111. public function renderAs($renderAs){
  112. $this->renderAs = $renderAs;
  113. return $this;
  114. }
  115. /**
  116. * Returns the set renderAs
  117. * @return string the renderAs value
  118. * @since 6.0.0
  119. */
  120. public function getRenderAs(){
  121. return $this->renderAs;
  122. }
  123. /**
  124. * Returns the rendered html
  125. * @return string the rendered html
  126. * @since 6.0.0
  127. */
  128. public function render(){
  129. // \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
  130. $renderAs = $this->renderAs === 'blank' ? '' : $this->renderAs;
  131. $template = new \OCP\Template($this->appName, $this->templateName, $renderAs);
  132. foreach($this->params as $key => $value){
  133. $template->assign($key, $value);
  134. }
  135. return $template->fetchPage($this->params);
  136. }
  137. }