TemplateController.php 3.2 KB

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