@@ -38,174 +38,174 @@ |
||
| 38 | 38 | * @since 6.0.0 |
| 39 | 39 | */ |
| 40 | 40 | class TemplateResponse extends Response { |
| 41 | - /** |
|
| 42 | - * @since 20.0.0 |
|
| 43 | - */ |
|
| 44 | - public const RENDER_AS_GUEST = 'guest'; |
|
| 45 | - /** |
|
| 46 | - * @since 20.0.0 |
|
| 47 | - */ |
|
| 48 | - public const RENDER_AS_BLANK = ''; |
|
| 49 | - /** |
|
| 50 | - * @since 20.0.0 |
|
| 51 | - */ |
|
| 52 | - public const RENDER_AS_BASE = 'base'; |
|
| 53 | - /** |
|
| 54 | - * @since 20.0.0 |
|
| 55 | - */ |
|
| 56 | - public const RENDER_AS_USER = 'user'; |
|
| 57 | - /** |
|
| 58 | - * @since 20.0.0 |
|
| 59 | - */ |
|
| 60 | - public const RENDER_AS_ERROR = 'error'; |
|
| 61 | - /** |
|
| 62 | - * @since 20.0.0 |
|
| 63 | - */ |
|
| 64 | - public const RENDER_AS_PUBLIC = 'public'; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent |
|
| 68 | - */ |
|
| 69 | - public const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts'; |
|
| 70 | - /** |
|
| 71 | - * @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent |
|
| 72 | - */ |
|
| 73 | - public const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn'; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * name of the template |
|
| 77 | - * @var string |
|
| 78 | - */ |
|
| 79 | - protected $templateName; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * parameters |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 85 | - protected $params; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * rendering type (admin, user, blank) |
|
| 89 | - * @var string |
|
| 90 | - */ |
|
| 91 | - protected $renderAs; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * app name |
|
| 95 | - * @var string |
|
| 96 | - */ |
|
| 97 | - protected $appName; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * constructor of TemplateResponse |
|
| 101 | - * @param string $appName the name of the app to load the template from |
|
| 102 | - * @param string $templateName the name of the template |
|
| 103 | - * @param array $params an array of parameters which should be passed to the |
|
| 104 | - * template |
|
| 105 | - * @param string $renderAs how the page should be rendered, defaults to user |
|
| 106 | - * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0 |
|
| 107 | - */ |
|
| 108 | - public function __construct($appName, $templateName, array $params=[], |
|
| 109 | - $renderAs = self::RENDER_AS_USER) { |
|
| 110 | - parent::__construct(); |
|
| 111 | - |
|
| 112 | - $this->templateName = $templateName; |
|
| 113 | - $this->appName = $appName; |
|
| 114 | - $this->params = $params; |
|
| 115 | - $this->renderAs = $renderAs; |
|
| 116 | - |
|
| 117 | - $this->setContentSecurityPolicy(new ContentSecurityPolicy()); |
|
| 118 | - $this->setFeaturePolicy(new FeaturePolicy()); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Sets template parameters |
|
| 124 | - * @param array $params an array with key => value structure which sets template |
|
| 125 | - * variables |
|
| 126 | - * @return TemplateResponse Reference to this object |
|
| 127 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 128 | - */ |
|
| 129 | - public function setParams(array $params) { |
|
| 130 | - $this->params = $params; |
|
| 131 | - |
|
| 132 | - return $this; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Used for accessing the set parameters |
|
| 138 | - * @return array the params |
|
| 139 | - * @since 6.0.0 |
|
| 140 | - */ |
|
| 141 | - public function getParams() { |
|
| 142 | - return $this->params; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Used for accessing the name of the set template |
|
| 148 | - * @return string the name of the used template |
|
| 149 | - * @since 6.0.0 |
|
| 150 | - */ |
|
| 151 | - public function getTemplateName() { |
|
| 152 | - return $this->templateName; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Sets the template page |
|
| 158 | - * @param string $renderAs admin, user or blank. Admin also prints the admin |
|
| 159 | - * settings header and footer, user renders the normal |
|
| 160 | - * normal page including footer and header and blank |
|
| 161 | - * just renders the plain template |
|
| 162 | - * @return TemplateResponse Reference to this object |
|
| 163 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 164 | - */ |
|
| 165 | - public function renderAs($renderAs) { |
|
| 166 | - $this->renderAs = $renderAs; |
|
| 167 | - |
|
| 168 | - return $this; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Returns the set renderAs |
|
| 174 | - * @return string the renderAs value |
|
| 175 | - * @since 6.0.0 |
|
| 176 | - */ |
|
| 177 | - public function getRenderAs() { |
|
| 178 | - return $this->renderAs; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Returns the rendered html |
|
| 184 | - * @return string the rendered html |
|
| 185 | - * @since 6.0.0 |
|
| 186 | - */ |
|
| 187 | - public function render() { |
|
| 188 | - $renderAs = self::RENDER_AS_USER; |
|
| 189 | - if ($this->renderAs === 'blank') { |
|
| 190 | - // Legacy fallback as \OCP\Template needs an empty string instead of 'blank' for an unwrapped response |
|
| 191 | - $renderAs = self::RENDER_AS_BLANK; |
|
| 192 | - } elseif (in_array($this->renderAs, [ |
|
| 193 | - self::RENDER_AS_GUEST, |
|
| 194 | - self::RENDER_AS_BLANK, |
|
| 195 | - self::RENDER_AS_BASE, |
|
| 196 | - self::RENDER_AS_ERROR, |
|
| 197 | - self::RENDER_AS_PUBLIC, |
|
| 198 | - self::RENDER_AS_USER], true)) { |
|
| 199 | - $renderAs = $this->renderAs; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - \OCP\Util::addHeader('meta', ['name' => 'robots', 'content' => 'noindex, nofollow']); |
|
| 203 | - $template = new \OCP\Template($this->appName, $this->templateName, $renderAs); |
|
| 204 | - |
|
| 205 | - foreach ($this->params as $key => $value) { |
|
| 206 | - $template->assign($key, $value); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return $template->fetchPage($this->params); |
|
| 210 | - } |
|
| 41 | + /** |
|
| 42 | + * @since 20.0.0 |
|
| 43 | + */ |
|
| 44 | + public const RENDER_AS_GUEST = 'guest'; |
|
| 45 | + /** |
|
| 46 | + * @since 20.0.0 |
|
| 47 | + */ |
|
| 48 | + public const RENDER_AS_BLANK = ''; |
|
| 49 | + /** |
|
| 50 | + * @since 20.0.0 |
|
| 51 | + */ |
|
| 52 | + public const RENDER_AS_BASE = 'base'; |
|
| 53 | + /** |
|
| 54 | + * @since 20.0.0 |
|
| 55 | + */ |
|
| 56 | + public const RENDER_AS_USER = 'user'; |
|
| 57 | + /** |
|
| 58 | + * @since 20.0.0 |
|
| 59 | + */ |
|
| 60 | + public const RENDER_AS_ERROR = 'error'; |
|
| 61 | + /** |
|
| 62 | + * @since 20.0.0 |
|
| 63 | + */ |
|
| 64 | + public const RENDER_AS_PUBLIC = 'public'; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent |
|
| 68 | + */ |
|
| 69 | + public const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts'; |
|
| 70 | + /** |
|
| 71 | + * @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent |
|
| 72 | + */ |
|
| 73 | + public const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn'; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * name of the template |
|
| 77 | + * @var string |
|
| 78 | + */ |
|
| 79 | + protected $templateName; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * parameters |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | + protected $params; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * rendering type (admin, user, blank) |
|
| 89 | + * @var string |
|
| 90 | + */ |
|
| 91 | + protected $renderAs; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * app name |
|
| 95 | + * @var string |
|
| 96 | + */ |
|
| 97 | + protected $appName; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * constructor of TemplateResponse |
|
| 101 | + * @param string $appName the name of the app to load the template from |
|
| 102 | + * @param string $templateName the name of the template |
|
| 103 | + * @param array $params an array of parameters which should be passed to the |
|
| 104 | + * template |
|
| 105 | + * @param string $renderAs how the page should be rendered, defaults to user |
|
| 106 | + * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0 |
|
| 107 | + */ |
|
| 108 | + public function __construct($appName, $templateName, array $params=[], |
|
| 109 | + $renderAs = self::RENDER_AS_USER) { |
|
| 110 | + parent::__construct(); |
|
| 111 | + |
|
| 112 | + $this->templateName = $templateName; |
|
| 113 | + $this->appName = $appName; |
|
| 114 | + $this->params = $params; |
|
| 115 | + $this->renderAs = $renderAs; |
|
| 116 | + |
|
| 117 | + $this->setContentSecurityPolicy(new ContentSecurityPolicy()); |
|
| 118 | + $this->setFeaturePolicy(new FeaturePolicy()); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Sets template parameters |
|
| 124 | + * @param array $params an array with key => value structure which sets template |
|
| 125 | + * variables |
|
| 126 | + * @return TemplateResponse Reference to this object |
|
| 127 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 128 | + */ |
|
| 129 | + public function setParams(array $params) { |
|
| 130 | + $this->params = $params; |
|
| 131 | + |
|
| 132 | + return $this; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Used for accessing the set parameters |
|
| 138 | + * @return array the params |
|
| 139 | + * @since 6.0.0 |
|
| 140 | + */ |
|
| 141 | + public function getParams() { |
|
| 142 | + return $this->params; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Used for accessing the name of the set template |
|
| 148 | + * @return string the name of the used template |
|
| 149 | + * @since 6.0.0 |
|
| 150 | + */ |
|
| 151 | + public function getTemplateName() { |
|
| 152 | + return $this->templateName; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Sets the template page |
|
| 158 | + * @param string $renderAs admin, user or blank. Admin also prints the admin |
|
| 159 | + * settings header and footer, user renders the normal |
|
| 160 | + * normal page including footer and header and blank |
|
| 161 | + * just renders the plain template |
|
| 162 | + * @return TemplateResponse Reference to this object |
|
| 163 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 164 | + */ |
|
| 165 | + public function renderAs($renderAs) { |
|
| 166 | + $this->renderAs = $renderAs; |
|
| 167 | + |
|
| 168 | + return $this; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Returns the set renderAs |
|
| 174 | + * @return string the renderAs value |
|
| 175 | + * @since 6.0.0 |
|
| 176 | + */ |
|
| 177 | + public function getRenderAs() { |
|
| 178 | + return $this->renderAs; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Returns the rendered html |
|
| 184 | + * @return string the rendered html |
|
| 185 | + * @since 6.0.0 |
|
| 186 | + */ |
|
| 187 | + public function render() { |
|
| 188 | + $renderAs = self::RENDER_AS_USER; |
|
| 189 | + if ($this->renderAs === 'blank') { |
|
| 190 | + // Legacy fallback as \OCP\Template needs an empty string instead of 'blank' for an unwrapped response |
|
| 191 | + $renderAs = self::RENDER_AS_BLANK; |
|
| 192 | + } elseif (in_array($this->renderAs, [ |
|
| 193 | + self::RENDER_AS_GUEST, |
|
| 194 | + self::RENDER_AS_BLANK, |
|
| 195 | + self::RENDER_AS_BASE, |
|
| 196 | + self::RENDER_AS_ERROR, |
|
| 197 | + self::RENDER_AS_PUBLIC, |
|
| 198 | + self::RENDER_AS_USER], true)) { |
|
| 199 | + $renderAs = $this->renderAs; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + \OCP\Util::addHeader('meta', ['name' => 'robots', 'content' => 'noindex, nofollow']); |
|
| 203 | + $template = new \OCP\Template($this->appName, $this->templateName, $renderAs); |
|
| 204 | + |
|
| 205 | + foreach ($this->params as $key => $value) { |
|
| 206 | + $template->assign($key, $value); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return $template->fetchPage($this->params); |
|
| 210 | + } |
|
| 211 | 211 | } |