Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2021-01-19 18:38:51 +0300
committerJulius Härtl <jus@bitgrid.net>2021-01-28 14:00:20 +0300
commit4f90766ba314171bbfc78d1e988307c50633e7f3 (patch)
treef5f910ff0f3dd2ff8fa5a05c8fd1f905ffc21ba5 /lib
parent7e6d69d166cbc92fb457fc72efc9abe850a0bbe4 (diff)
Skip template picker if none available
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Files/Template/TemplateManager.php95
-rw-r--r--lib/private/legacy/OC_Util.php6
-rw-r--r--lib/public/Files/Template/ITemplateManager.php4
-rw-r--r--lib/public/Files/Template/TemplateFileCreator.php19
4 files changed, 90 insertions, 34 deletions
diff --git a/lib/private/Files/Template/TemplateManager.php b/lib/private/Files/Template/TemplateManager.php
index 614440327e8..808c0fbb999 100644
--- a/lib/private/Files/Template/TemplateManager.php
+++ b/lib/private/Files/Template/TemplateManager.php
@@ -34,6 +34,7 @@ use OCP\Files\GenericFileException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
+use OCP\Files\NotPermittedException;
use OCP\Files\Template\CreatedFromTemplateEvent;
use OCP\Files\Template\ICustomTemplateProvider;
use OCP\Files\Template\ITemplateManager;
@@ -103,7 +104,15 @@ class TemplateManager implements ITemplateManager {
return $this->providers;
}
- public function listCreators(): array {
+ public function listCreators():? array {
+ if ($this->types === null) {
+ return null;
+ }
+
+ usort($this->types, function (TemplateFileCreator $a, TemplateFileCreator $b) {
+ return $a->getOrder() - $b->getOrder();
+ });
+
return array_map(function (TemplateFileCreator $entry) {
return array_merge($entry->jsonSerialize(), [
'templates' => $this->getTemplateFiles($entry)
@@ -154,7 +163,10 @@ class TemplateManager implements ITemplateManager {
* @throws \OC\User\NoUserException
*/
private function getTemplateFolder(): Node {
- return $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath());
+ if ($this->getTemplatePath() !== '') {
+ return $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath());
+ }
+ throw new NotFoundException();
}
private function getTemplateFiles(TemplateFileCreator $type): array {
@@ -220,10 +232,10 @@ class TemplateManager implements ITemplateManager {
}
public function getTemplatePath(): string {
- return $this->config->getUserValue($this->userId, 'core', 'templateDirectory', $this->l10n->t('Templates') . '/');
+ return $this->config->getUserValue($this->userId, 'core', 'templateDirectory', '');
}
- public function initializeTemplateDirectory(string $path = null, string $userId = null): void {
+ public function initializeTemplateDirectory(string $path = null, string $userId = null, $copyTemplates = true): string {
if ($userId !== null) {
$this->userId = $userId;
}
@@ -232,6 +244,8 @@ class TemplateManager implements ITemplateManager {
$defaultTemplateDirectory = \OC::$SERVERROOT . '/core/skeleton/Templates';
$skeletonPath = $this->config->getSystemValue('skeletondirectory', $defaultSkeletonDirectory);
$skeletonTemplatePath = $this->config->getSystemValue('templatedirectory', $defaultTemplateDirectory);
+ $isDefaultSkeleton = $skeletonPath === $defaultSkeletonDirectory;
+ $isDefaultTemplates = $skeletonTemplatePath === $defaultTemplateDirectory;
$userLang = $this->l10nFactory->getUserLanguage();
try {
@@ -239,39 +253,64 @@ class TemplateManager implements ITemplateManager {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$userTemplatePath = $path ?? $l10n->t('Templates') . '/';
- // All locations are default so we just need to rename the directory to the users language
- if ($skeletonPath === $defaultSkeletonDirectory && $skeletonTemplatePath === $defaultTemplateDirectory && $userFolder->nodeExists('Templates')) {
- $newPath = $userFolder->getPath() . '/' . $userTemplatePath;
- if ($newPath !== $userFolder->get('Templates')->getPath()) {
- $userFolder->get('Templates')->move($newPath);
+ // Initial user setup without a provided path
+ if ($path === null) {
+ // All locations are default so we just need to rename the directory to the users language
+ if ($isDefaultSkeleton && $isDefaultTemplates && $userFolder->nodeExists('Templates')) {
+ $newPath = $userFolder->getPath() . '/' . $userTemplatePath;
+ if ($newPath !== $userFolder->get('Templates')->getPath()) {
+ $userFolder->get('Templates')->move($newPath);
+ }
+ $this->setTemplatePath($userTemplatePath);
+ return $userTemplatePath;
}
- $this->setTemplatePath($userTemplatePath);
- return;
- }
- // A custom template directory is specified
- if (!empty($skeletonTemplatePath) && $skeletonTemplatePath !== $defaultTemplateDirectory) {
- // In case the shipped template files are in place we remove them
- if ($skeletonPath === $defaultSkeletonDirectory && $userFolder->nodeExists('Templates')) {
+ if ($isDefaultSkeleton && !empty($skeletonTemplatePath) && !$isDefaultTemplates && $userFolder->nodeExists('Templates')) {
$shippedSkeletonTemplates = $userFolder->get('Templates');
$shippedSkeletonTemplates->delete();
}
- try {
- $userFolder->get($userTemplatePath);
- } catch (NotFoundException $e) {
- $folder = $userFolder->newFolder($userTemplatePath);
-
- $localizedSkeletonTemplatePath = $this->getLocalizedTemplatePath($skeletonTemplatePath, $userLang);
- if (!empty($localizedSkeletonTemplatePath) && file_exists($localizedSkeletonTemplatePath)) {
- \OC_Util::copyr($localizedSkeletonTemplatePath, $folder);
- $userFolder->getStorage()->getScanner()->scan($userTemplatePath, Scanner::SCAN_RECURSIVE);
- }
- }
+ }
+
+ try {
+ $folder = $userFolder->newFolder($userTemplatePath);
+ } catch (NotPermittedException $e) {
+ $folder = $userFolder->get($userTemplatePath);
+ }
+
+ $folderIsEmpty = count($folder->getDirectoryListing()) === 0;
+
+ if (!$copyTemplates) {
$this->setTemplatePath($userTemplatePath);
+ return $userTemplatePath;
+ }
+
+ if (!$isDefaultTemplates && $folderIsEmpty) {
+ $localizedSkeletonTemplatePath = $this->getLocalizedTemplatePath($skeletonTemplatePath, $userLang);
+ if (!empty($localizedSkeletonTemplatePath) && file_exists($localizedSkeletonTemplatePath)) {
+ \OC_Util::copyr($localizedSkeletonTemplatePath, $folder);
+ $userFolder->getStorage()->getScanner()->scan($userTemplatePath, Scanner::SCAN_RECURSIVE);
+ $this->setTemplatePath($userTemplatePath);
+ return $userTemplatePath;
+ }
}
+
+ if ($path !== null && $isDefaultSkeleton && $isDefaultTemplates && $folderIsEmpty) {
+ $localizedSkeletonPath = $this->getLocalizedTemplatePath($skeletonPath . '/Templates', $userLang);
+ if (!empty($localizedSkeletonPath) && file_exists($localizedSkeletonPath)) {
+ \OC_Util::copyr($localizedSkeletonPath, $folder);
+ $userFolder->getStorage()->getScanner()->scan($userTemplatePath, Scanner::SCAN_RECURSIVE);
+ $this->setTemplatePath($userTemplatePath);
+ return $userTemplatePath;
+ }
+ }
+
+ $this->setTemplatePath($path ?? '');
+ return $this->getTemplatePath();
} catch (\Throwable $e) {
- $this->logger->error('Failed to rename templates directory to user language ' . $userLang . ' for ' . $userId, ['app' => 'files_templates']);
+ $this->logger->error('Failed to initialize templates directory to user language ' . $userLang . ' for ' . $userId, ['app' => 'files_templates', 'exception' => $e]);
}
+ $this->setTemplatePath('');
+ return $this->getTemplatePath();
}
private function getLocalizedTemplatePath(string $skeletonTemplatePath, string $userLang) {
diff --git a/lib/private/legacy/OC_Util.php b/lib/private/legacy/OC_Util.php
index 05d54cf84e6..f1e88166e97 100644
--- a/lib/private/legacy/OC_Util.php
+++ b/lib/private/legacy/OC_Util.php
@@ -449,9 +449,9 @@ class OC_Util {
// update the file cache
$userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
- /** @var ITemplateManager $templateManaer */
- $templateManaer = \OC::$server->get(ITemplateManager::class);
- $templateManaer->initializeTemplateDirectory(null, $userId);
+ /** @var ITemplateManager $templateManager */
+ $templateManager = \OC::$server->get(ITemplateManager::class);
+ $templateManager->initializeTemplateDirectory(null, $userId);
}
}
diff --git a/lib/public/Files/Template/ITemplateManager.php b/lib/public/Files/Template/ITemplateManager.php
index 28d57a8b94c..58b5b6c4846 100644
--- a/lib/public/Files/Template/ITemplateManager.php
+++ b/lib/public/Files/Template/ITemplateManager.php
@@ -56,7 +56,7 @@ interface ITemplateManager {
* @return array
* @since 21.0.0
*/
- public function listCreators(): array;
+ public function listCreators():? array;
/**
* @return bool
@@ -82,7 +82,7 @@ interface ITemplateManager {
* @param string|null $userId
* @since 21.0.0
*/
- public function initializeTemplateDirectory(string $path = null, string $userId = null): void;
+ public function initializeTemplateDirectory(string $path = null, string $userId = null, $copyTemplates = true): string;
/**
* @param string $filePath
diff --git a/lib/public/Files/Template/TemplateFileCreator.php b/lib/public/Files/Template/TemplateFileCreator.php
index c41a6514ee5..e40fa8e91b3 100644
--- a/lib/public/Files/Template/TemplateFileCreator.php
+++ b/lib/public/Files/Template/TemplateFileCreator.php
@@ -35,6 +35,7 @@ final class TemplateFileCreator implements \JsonSerializable {
protected $fileExtension;
protected $iconClass;
protected $ratio = null;
+ protected $order = 100;
/**
* @since 21.0.0
@@ -80,12 +81,28 @@ final class TemplateFileCreator implements \JsonSerializable {
/**
* @since 21.0.0
*/
- public function setRatio(float $ratio) {
+ public function setRatio(float $ratio): TemplateFileCreator {
$this->ratio = $ratio;
return $this;
}
/**
+ * @param int $order order in which the create action shall be listed
+ * @since 21.0.0
+ */
+ public function setOrder(int $order): TemplateFileCreator {
+ $this->order = $order;
+ return $this;
+ }
+
+ /**
+ * @since 21.0.0
+ */
+ public function getOrder(): int {
+ return $this->order;
+ }
+
+ /**
* @since 21.0.0
*/
public function jsonSerialize() {