Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/files/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
'url' => '/api/v1/templates',
'verb' => 'GET'
],
[
'name' => 'Template#listTemplateFields',
'url' => '/api/v1/templates/fields/{fileId}',
'verb' => 'GET'
],
[
'name' => 'Template#create',
'url' => '/api/v1/templates/create',
Expand Down
18 changes: 18 additions & 0 deletions apps/files/lib/Controller/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ public function list(): DataResponse {
}, $this->templateManager->listTemplates()));
}

/**
* List the fields for the template specified by the given file ID
*
* @param int $fileId File ID of the template
* @return DataResponse<Http::STATUS_OK, array<string, FilesTemplateField>, array{}>
*
* 200: Fields returned
*/
#[NoAdminRequired]
public function listTemplateFields(int $fileId): DataResponse {
$fields = $this->templateManager->listTemplateFields($fileId);

return new DataResponse(
array_merge([], ...$fields),
Http::STATUS_OK
);
}

/**
* Create a template
*
Expand Down
74 changes: 74 additions & 0 deletions apps/files/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,80 @@
}
}
},
"/ocs/v2.php/apps/files/api/v1/templates/fields/{fileId}": {
"get": {
"operationId": "template-list-template-fields",
"summary": "List the fields for the template specified by the given file ID",
"tags": [
"template"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "fileId",
"in": "path",
"description": "File ID of the template",
"required": true,
"schema": {
"type": "integer",
"format": "int64"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "Fields returned",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/TemplateField"
}
}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/apps/files/api/v1/templates/create": {
"post": {
"operationId": "template-create",
Expand Down
5 changes: 5 additions & 0 deletions apps/files/src/services/Templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const getTemplates = async function() {
return response.data.ocs.data
}

export const getTemplateFields = async function(fileId) {
const response = await axios.get(generateOcsUrl(`apps/files/api/v1/templates/fields/${fileId}`))
return response.data.ocs.data
}

/**
* Create a new file from a specified template
*
Expand Down
11 changes: 7 additions & 4 deletions apps/files/src/views/TemplatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { translate as t } from '@nextcloud/l10n'
import { generateRemoteUrl } from '@nextcloud/router'
import { normalize, extname, join } from 'path'
import { defineComponent } from 'vue'
import { createFromTemplate, getTemplates } from '../services/Templates.js'
import { createFromTemplate, getTemplates, getTemplateFields } from '../services/Templates.js'

import NcEmptyContent from '@nextcloud/vue/components/NcEmptyContent'
import NcModal from '@nextcloud/vue/components/NcModal'
Expand Down Expand Up @@ -215,7 +215,7 @@ export default defineComponent({
}
},

async createFile(templateFields) {
async createFile(templateFields = []) {
const currentDirectory = new URL(window.location.href).searchParams.get('dir') || '/'

// If the file doesn't have an extension, add the default one
Expand Down Expand Up @@ -274,9 +274,12 @@ export default defineComponent({
},

async onSubmit() {
if (this.selectedTemplate?.fields?.length > 0) {
const fileId = this.selectedTemplate?.fileid
const fields = await getTemplateFields(fileId)

if (fields.length > 0) {
spawnDialog(TemplateFiller, {
fields: this.selectedTemplate.fields,
fields,
onSubmit: this.createFile,
})
} else {
Expand Down
2 changes: 2 additions & 0 deletions dist/4052-4052.js

Large diffs are not rendered by default.

File renamed without changes.
1 change: 1 addition & 0 deletions dist/4052-4052.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/4052-4052.js.map.license
2 changes: 0 additions & 2 deletions dist/7950-7950.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/7950-7950.js.map

This file was deleted.

1 change: 0 additions & 1 deletion dist/7950-7950.js.map.license

This file was deleted.

4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

65 changes: 63 additions & 2 deletions lib/private/Files/Template/TemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Template\BeforeGetTemplatesEvent;
use OCP\Files\Template\Field;
use OCP\Files\Template\FileCreatedFromTemplateEvent;
use OCP\Files\Template\ICustomTemplateProvider;
use OCP\Files\Template\ITemplateManager;
Expand Down Expand Up @@ -125,6 +126,19 @@ public function listTemplates(): array {
}, $this->listCreators()));
}

public function listTemplateFields(int $fileId): array {
foreach ($this->listCreators() as $creator) {
$fields = $this->getTemplateFields($creator, $fileId);
if (empty($fields)) {
continue;
}

return $fields;
}

return [];
}

/**
* @param string $filePath
* @param string $templateId
Expand Down Expand Up @@ -187,6 +201,20 @@ private function getTemplateFolder(): Folder {
* @return list<Template>
*/
private function getTemplateFiles(TemplateFileCreator $type): array {
$templates = array_merge(
$this->getProviderTemplates($type),
$this->getUserTemplates($type)
);

$this->eventDispatcher->dispatchTyped(new BeforeGetTemplatesEvent($templates, false));

return $templates;
}

/**
* @return list<Template>
*/
private function getProviderTemplates(TemplateFileCreator $type): array {
$templates = [];
foreach ($this->getRegisteredProviders() as $provider) {
foreach ($type->getMimetypes() as $mimetype) {
Expand All @@ -195,11 +223,22 @@ private function getTemplateFiles(TemplateFileCreator $type): array {
}
}
}

return $templates;
}

/**
* @return list<Template>
*/
private function getUserTemplates(TemplateFileCreator $type): array {
$templates = [];

try {
$userTemplateFolder = $this->getTemplateFolder();
} catch (\Exception $e) {
return $templates;
}

foreach ($type->getMimetypes() as $mimetype) {
foreach ($userTemplateFolder->searchByMime($mimetype) as $templateFile) {
$template = new Template(
Expand All @@ -212,11 +251,33 @@ private function getTemplateFiles(TemplateFileCreator $type): array {
}
}

$this->eventDispatcher->dispatchTyped(new BeforeGetTemplatesEvent($templates));

return $templates;
}

/*
* @return list<Field>
*/
private function getTemplateFields(TemplateFileCreator $type, int $fileId): array {
$providerTemplates = $this->getProviderTemplates($type);
$userTemplates = $this->getUserTemplates($type);

$matchedTemplates = array_filter(
array_merge($providerTemplates, $userTemplates),
function (Template $template) use ($fileId) {
return $template->jsonSerialize()['fileid'] === $fileId;
});

if (empty($matchedTemplates)) {
return [];
}

$this->eventDispatcher->dispatchTyped(new BeforeGetTemplatesEvent($matchedTemplates, true));

return array_values(array_map(function (Template $template) {
return $template->jsonSerialize()['fields'] ?? [];
}, $matchedTemplates));
}

/**
* @param Node|File $file
* @return array
Expand Down
14 changes: 13 additions & 1 deletion lib/public/Files/Template/BeforeGetTemplatesEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
class BeforeGetTemplatesEvent extends Event {
/** @var array<Template> */
private array $templates;
/** @var bool */
private bool $withFields;

/**
* @param array<Template> $templates
*
* @since 30.0.0
*/
public function __construct(array $templates) {
public function __construct(array $templates, bool $withFields = false) {
parent::__construct();

$this->templates = $templates;
$this->withFields = $withFields;
}

/**
Expand All @@ -37,4 +40,13 @@ public function __construct(array $templates) {
public function getTemplates(): array {
return $this->templates;
}

/**
* @return bool
*
* @since 32.0.0
*/
public function shouldGetFields(): bool {
return $this->withFields;
}
}
9 changes: 9 additions & 0 deletions lib/public/Files/Template/ITemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public function listCreators(): array;
*/
public function listTemplates(): array;

/**
* Get the fields for a given template
*
* @param int $fileId
* @return array
* @since 32.0.0
*/
public function listTemplateFields(int $fileId): array;

/**
* @return bool
* @since 21.0.0
Expand Down
Loading
Loading