TemplateController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Template;
  17. use OCP\Files\Template\TemplateFileCreator;
  18. use OCP\IRequest;
  19. /**
  20. * @psalm-import-type FilesTemplateFile from ResponseDefinitions
  21. * @psalm-import-type FilesTemplateFileCreator from ResponseDefinitions
  22. * @psalm-import-type FilesTemplateFileCreatorWithTemplates from ResponseDefinitions
  23. * @psalm-import-type FilesTemplateField from ResponseDefinitions
  24. * @psalm-import-type FilesTemplate from ResponseDefinitions
  25. */
  26. class TemplateController extends OCSController {
  27. public function __construct(
  28. $appName,
  29. IRequest $request,
  30. protected ITemplateManager $templateManager,
  31. ) {
  32. parent::__construct($appName, $request);
  33. }
  34. /**
  35. * List the available templates
  36. *
  37. * @return DataResponse<Http::STATUS_OK, list<FilesTemplateFileCreatorWithTemplates>, array{}>
  38. *
  39. * 200: Available templates returned
  40. */
  41. #[NoAdminRequired]
  42. public function list(): DataResponse {
  43. /* Convert embedded Template instances to arrays to match return type */
  44. return new DataResponse(array_map(static function (array $templateFileCreator) {
  45. $templateFileCreator['templates'] = array_map(static fn (Template $template) => $template->jsonSerialize(), $templateFileCreator['templates']);
  46. return $templateFileCreator;
  47. }, $this->templateManager->listTemplates()));
  48. }
  49. /**
  50. * Create a template
  51. *
  52. * @param string $filePath Path of the file
  53. * @param string $templatePath Name of the template
  54. * @param string $templateType Type of the template
  55. * @param list<FilesTemplateField> $templateFields Fields of the template
  56. *
  57. * @return DataResponse<Http::STATUS_OK, FilesTemplateFile, array{}>
  58. * @throws OCSForbiddenException Creating template is not allowed
  59. *
  60. * 200: Template created successfully
  61. */
  62. #[NoAdminRequired]
  63. public function create(
  64. string $filePath,
  65. string $templatePath = '',
  66. string $templateType = 'user',
  67. array $templateFields = [],
  68. ): DataResponse {
  69. try {
  70. return new DataResponse($this->templateManager->createFromTemplate(
  71. $filePath,
  72. $templatePath,
  73. $templateType,
  74. $templateFields));
  75. } catch (GenericFileException $e) {
  76. throw new OCSForbiddenException($e->getMessage());
  77. }
  78. }
  79. /**
  80. * Initialize the template directory
  81. *
  82. * @param string $templatePath Path of the template directory
  83. * @param bool $copySystemTemplates Whether to copy the system templates to the template directory
  84. *
  85. * @return DataResponse<Http::STATUS_OK, array{template_path: string, templates: list<FilesTemplateFileCreator>}, array{}>
  86. * @throws OCSForbiddenException Initializing the template directory is not allowed
  87. *
  88. * 200: Template directory initialized successfully
  89. */
  90. #[NoAdminRequired]
  91. public function path(string $templatePath = '', bool $copySystemTemplates = false) {
  92. try {
  93. /** @var string $templatePath */
  94. $templatePath = $this->templateManager->initializeTemplateDirectory($templatePath, null, $copySystemTemplates);
  95. return new DataResponse([
  96. 'template_path' => $templatePath,
  97. 'templates' => array_values(array_map(fn (TemplateFileCreator $creator) => $creator->jsonSerialize(), $this->templateManager->listCreators())),
  98. ]);
  99. } catch (\Exception $e) {
  100. throw new OCSForbiddenException($e->getMessage());
  101. }
  102. }
  103. }