TemplateController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files\Controller;
  8. use OCA\Files\ResponseDefinitions;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\AppFramework\OCS\OCSForbiddenException;
  13. use OCP\AppFramework\OCSController;
  14. use OCP\Files\GenericFileException;
  15. use OCP\Files\Template\ITemplateManager;
  16. use OCP\Files\Template\TemplateFileCreator;
  17. use OCP\IRequest;
  18. /**
  19. * @psalm-import-type FilesTemplateFile from ResponseDefinitions
  20. * @psalm-import-type FilesTemplateFileCreator from ResponseDefinitions
  21. * @psalm-import-type FilesTemplateField from ResponseDefinitions
  22. */
  23. class TemplateController extends OCSController {
  24. public function __construct(
  25. $appName,
  26. IRequest $request,
  27. protected ITemplateManager $templateManager,
  28. ) {
  29. parent::__construct($appName, $request);
  30. }
  31. /**
  32. * List the available templates
  33. *
  34. * @return DataResponse<Http::STATUS_OK, array<FilesTemplateFileCreator>, array{}>
  35. *
  36. * 200: Available templates returned
  37. */
  38. #[NoAdminRequired]
  39. public function list(): DataResponse {
  40. return new DataResponse($this->templateManager->listTemplates());
  41. }
  42. /**
  43. * Create a template
  44. *
  45. * @param string $filePath Path of the file
  46. * @param string $templatePath Name of the template
  47. * @param string $templateType Type of the template
  48. * @param FilesTemplateField[] $templateFields Fields of the template
  49. *
  50. * @return DataResponse<Http::STATUS_OK, FilesTemplateFile, array{}>
  51. * @throws OCSForbiddenException Creating template is not allowed
  52. *
  53. * 200: Template created successfully
  54. */
  55. #[NoAdminRequired]
  56. public function create(
  57. string $filePath,
  58. string $templatePath = '',
  59. string $templateType = 'user',
  60. array $templateFields = [],
  61. ): DataResponse {
  62. try {
  63. return new DataResponse($this->templateManager->createFromTemplate(
  64. $filePath,
  65. $templatePath,
  66. $templateType,
  67. $templateFields));
  68. } catch (GenericFileException $e) {
  69. throw new OCSForbiddenException($e->getMessage());
  70. }
  71. }
  72. /**
  73. * Initialize the template directory
  74. *
  75. * @param string $templatePath Path of the template directory
  76. * @param bool $copySystemTemplates Whether to copy the system templates to the template directory
  77. *
  78. * @return DataResponse<Http::STATUS_OK, array{template_path: string, templates: FilesTemplateFileCreator[]}, array{}>
  79. * @throws OCSForbiddenException Initializing the template directory is not allowed
  80. *
  81. * 200: Template directory initialized successfully
  82. */
  83. #[NoAdminRequired]
  84. public function path(string $templatePath = '', bool $copySystemTemplates = false) {
  85. try {
  86. /** @var string $templatePath */
  87. $templatePath = $this->templateManager->initializeTemplateDirectory($templatePath, null, $copySystemTemplates);
  88. return new DataResponse([
  89. 'template_path' => $templatePath,
  90. 'templates' => array_map(fn (TemplateFileCreator $creator) => $creator->jsonSerialize(), $this->templateManager->listCreators()),
  91. ]);
  92. } catch (\Exception $e) {
  93. throw new OCSForbiddenException($e->getMessage());
  94. }
  95. }
  96. }