diff options
-rw-r--r-- | appinfo/routes.php | 1 | ||||
-rw-r--r-- | js/viewer/viewer.js | 52 | ||||
-rw-r--r-- | lib/Controller/DocumentController.php | 55 |
3 files changed, 82 insertions, 26 deletions
diff --git a/appinfo/routes.php b/appinfo/routes.php index e2fe270d..30baf711 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -29,6 +29,7 @@ return [ 'routes' => [ //documents ['name' => 'document#index', 'url' => 'index', 'verb' => 'GET'], + ['name' => 'document#template', 'url' => 'indexTemplate', 'verb' => 'GET'], ['name' => 'document#publicPage', 'url' => '/public', 'verb' => 'GET'], ['name' => 'document#create', 'url' => 'ajax/documents/create', 'verb' => 'POST'], diff --git a/js/viewer/viewer.js b/js/viewer/viewer.js index 2196c02b..8fdad39f 100644 --- a/js/viewer/viewer.js +++ b/js/viewer/viewer.js @@ -61,6 +61,7 @@ var odfViewer = { if(context) { var fileDir = context.dir; var fileId = context.fileId || context.$file.attr('data-id'); + var templateId = context.templateId; } var viewer; @@ -75,14 +76,27 @@ var odfViewer = { } ); } else { - viewer = OC.generateUrl( - 'apps/richdocuments/index?fileId={fileId}&requesttoken={requesttoken}', - { - fileId: fileId, - dir: fileDir, - requesttoken: OC.requestToken - } - ); + // We are dealing with a template + if (typeof(templateId) !== 'undefined') { + viewer = OC.generateUrl( + 'apps/richdocuments/indexTemplate?templateId={templateId}&fileName={fileName}&dir={dir}&requesttoken={requesttoken}', + { + templateId: templateId, + fileName: fileName, + dir: fileDir, + requesttoken: OC.requestToken + } + ); + } else { + viewer = OC.generateUrl( + 'apps/richdocuments/index?fileId={fileId}&requesttoken={requesttoken}', + { + fileId: fileId, + dir: fileDir, + requesttoken: OC.requestToken + } + ); + } } if(context) { @@ -223,23 +237,11 @@ var odfViewer = { OCA.Files.Files.isFileNameValid(filename); filename = FileList.getUniqueName(filename); - // TODO: use templateId to create the file from the template - console.log('TODO: Create a new file from template ' + templateId); - $.post( - OC.generateUrl('apps/richdocuments/ajax/documents/create'), - { mimetype : mimetype, filename: filename, dir: $('#dir').val() }, - function(response){ - if (response && response.status === 'success'){ - FileList.add(response.data, {animate: true, scrollTo: true}); - odfViewer.onEdit(filename, { - fileId: response.data.id, - fileDir: $('#dir').val() - }); - } else { - OC.dialogs.alert(response.data.message, t('core', 'Could not create file')); - } - } - ); + odfViewer.onEdit(filename, { + fileId: -1, + dir: $('#dir').val(), + templateId: templateId + }); }, _openTemplatePicker: function(type, mimetype, filename) { diff --git a/lib/Controller/DocumentController.php b/lib/Controller/DocumentController.php index 7219114a..a11399d3 100644 --- a/lib/Controller/DocumentController.php +++ b/lib/Controller/DocumentController.php @@ -52,9 +52,12 @@ class DocumentController extends Controller { private $session; /** @var IRootFolder */ private $rootFolder; + /** @var \OCA\Richdocuments\TemplateManager */ + private $templateManager; const ODT_TEMPLATE_PATH = '/assets/odttemplate.odt'; + /** * @param string $appName * @param IRequest $request @@ -78,7 +81,8 @@ class DocumentController extends Controller { IRootFolder $rootFolder, ISession $session, $UserId, - ILogger $logger) { + ILogger $logger, + \OCA\Richdocuments\TemplateManager $templateManager) { parent::__construct($appName, $request); $this->uid = $UserId; $this->l10n = $l10n; @@ -89,6 +93,7 @@ class DocumentController extends Controller { $this->rootFolder = $rootFolder; $this->session = $session; $this->logger = $logger; + $this->templateManager = $templateManager; } /** @@ -225,6 +230,54 @@ class DocumentController extends Controller { } /** + * @NoAdminRequired + * + * @param int $templateId + * @param string $fileName + * @param string $dir + * @return TemplateResponse + */ + public function template($templateId, $fileName, $dir) { + if (!$this->templateManager->isTemplate($templateId)) { + return new TemplateResponse('core', '403', [], 'guest'); + } + + $userFolder = $this->rootFolder->getUserFolder($this->uid); + try { + $folder = $userFolder->get($dir); + } catch (NotFoundException $e) { + return new TemplateResponse('core', '403', [], 'guest'); + } + + if ((!$folder instanceof Folder)) { + return new TemplateResponse('core', '403', [], 'guest'); + } + + $file = $folder->newFile($fileName); + + $template = $this->templateManager->get($templateId); + list($urlSrc, $token) = $this->tokenManager->getTokenForTemplate($template, $this->uid, $file->getId()); + + $params = [ + 'permissions' => $template->getPermissions(), + 'title' => $template->getName(), + 'fileId' => $template->getId() . '_' . $this->settings->getSystemValue('instanceid'), + 'token' => $token, + 'urlsrc' => $urlSrc, + 'path' => $userFolder->getRelativePath($file->getPath()), + 'instanceId' => $this->settings->getSystemValue('instanceid'), + 'canonical_webroot' => $this->appConfig->getAppValue('canonical_webroot'), + ]; + + $response = new TemplateResponse('richdocuments', 'documents', $params, 'empty'); + $policy = new ContentSecurityPolicy(); + $policy->addAllowedFrameDomain($this->domainOnly($this->appConfig->getAppValue('wopi_url'))); + $policy->allowInlineScript(true); + $response->setContentSecurityPolicy($policy); + return $response; + } + + /** * @PublicPage * * @param string $shareToken |