2018-09-18 11:45:45 +03:00
|
|
|
<?php
|
2018-09-19 09:23:39 +03:00
|
|
|
declare (strict_types = 1);
|
2018-09-18 11:45:45 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Richdocuments;
|
|
|
|
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\IAppData;
|
2018-09-18 19:47:14 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\Files\Node;
|
2018-09-18 11:45:45 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IPreview;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OC\Files\AppData\Factory;
|
|
|
|
|
|
|
|
class TemplateManager {
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $appName;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $userId;
|
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/** @var IAppData */
|
|
|
|
private $appData;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
|
|
|
|
/** @var ISimpleFolder */
|
|
|
|
private $folder;
|
|
|
|
|
|
|
|
/** @var Folder */
|
|
|
|
private $userFolder;
|
|
|
|
|
|
|
|
/** @var IPreview */
|
|
|
|
private $previewManager;
|
|
|
|
|
|
|
|
/** Accepted templates mime types */
|
2018-09-19 18:00:05 +03:00
|
|
|
const MIMES_DOCUMENTS = [
|
|
|
|
'application/vnd.oasis.opendocument.text'
|
2018-09-18 11:45:45 +03:00
|
|
|
];
|
2018-09-19 18:00:05 +03:00
|
|
|
const MIMES_SHEETS = [
|
|
|
|
'application/vnd.oasis.opendocument.spreadsheet'
|
2018-09-18 11:45:45 +03:00
|
|
|
];
|
2018-09-19 18:00:05 +03:00
|
|
|
const MIMES_PRESENTATIONS = [
|
|
|
|
'application/vnd.oasis.opendocument.presentation'
|
2018-09-18 11:45:45 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
/** @var array Template mime types match */
|
|
|
|
static public $tplTypes = [
|
2018-09-19 18:00:05 +03:00
|
|
|
'document' => self::MIMES_DOCUMENTS,
|
|
|
|
'spreadsheet' => self::MIMES_SHEETS,
|
|
|
|
'presentation' => self::MIMES_PRESENTATIONS
|
2018-09-18 11:45:45 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Template manager
|
|
|
|
*
|
|
|
|
* @param string $appName
|
|
|
|
* @param string $userId
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param Factory $appDataFactory
|
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param IRootFolder $rootFolder
|
|
|
|
* @param IPreview $previewManager
|
|
|
|
*/
|
|
|
|
public function __construct(string $appName,
|
2018-09-19 18:00:05 +03:00
|
|
|
string $userId,
|
|
|
|
IConfig $config,
|
|
|
|
Factory $appDataFactory,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
IRootFolder $rootFolder,
|
|
|
|
IPreview $previewManager) {
|
2018-09-18 11:45:45 +03:00
|
|
|
$this->appName = $appName;
|
|
|
|
$this->userId = $userId;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->appData = $appDataFactory->get($appName);
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->userFolder = $rootFolder->getUserFolder($userId);
|
|
|
|
$this->previewManager = $previewManager;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->folder = $this->appData->getFolder('templates');
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
$this->folder = $this->appData->newFolder('templates');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-19 09:23:39 +03:00
|
|
|
* Get template ISimpleFile|Node
|
2018-09-18 11:45:45 +03:00
|
|
|
*
|
|
|
|
* @param string $templateName
|
2018-09-18 20:31:29 +03:00
|
|
|
* @return ISimpleFile|Node
|
2018-09-18 11:45:45 +03:00
|
|
|
*/
|
|
|
|
public function get(string $templateName) {
|
|
|
|
try {
|
|
|
|
// is this a global template ?
|
2018-09-18 19:47:14 +03:00
|
|
|
$templateFile = $this->folder->getFile($templateName);
|
2018-09-18 11:45:45 +03:00
|
|
|
} catch (NotFoundException $e) {
|
2018-09-18 19:47:14 +03:00
|
|
|
$templateDir = $this->getUserTemplateDir();
|
|
|
|
// finally get the template file
|
2018-09-18 11:45:45 +03:00
|
|
|
try {
|
2018-09-18 19:47:14 +03:00
|
|
|
$templateFile = $templateDir->get($templateName);
|
2018-09-18 11:45:45 +03:00
|
|
|
} catch (NotFoundException $e) {
|
2018-09-19 09:23:39 +03:00
|
|
|
throw new NotFoundException($e->getMessage());
|
2018-09-18 11:45:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 20:14:05 +03:00
|
|
|
return $templateFile;
|
2018-09-18 11:45:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all global templates
|
2018-09-19 09:23:39 +03:00
|
|
|
*
|
|
|
|
* @return array
|
2018-09-18 11:45:45 +03:00
|
|
|
*/
|
2018-09-19 09:23:39 +03:00
|
|
|
public function getSystem(): array{
|
2018-09-18 11:45:45 +03:00
|
|
|
$templateFiles = $this->folder->getDirectoryListing();
|
|
|
|
|
|
|
|
return array_map(function (ISimpleFile $templateFile) {
|
2018-09-18 19:47:14 +03:00
|
|
|
return $this->formatNodeReturn($templateFile);
|
2018-09-18 11:45:45 +03:00
|
|
|
}, $templateFiles);
|
|
|
|
}
|
|
|
|
|
2018-09-19 09:23:39 +03:00
|
|
|
/**
|
|
|
|
* Get all user templates
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getUser(): array{
|
|
|
|
$templateDir = $this->getUserTemplateDir();
|
|
|
|
$templateFiles = $templateDir->getDirectoryListing();
|
|
|
|
|
|
|
|
return array_map(function (Node $templateFile) {
|
|
|
|
return $this->formatNodeReturn($templateFile);
|
|
|
|
}, $templateFiles);
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:27:18 +03:00
|
|
|
/**
|
|
|
|
* Get all templates
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAll(string $type = 'document'): array{
|
|
|
|
$system = $this->getSystem();
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
2018-09-19 18:00:05 +03:00
|
|
|
return array_filter(array_merge($user, $system), function ($template) use ($type) {
|
|
|
|
return $template['ext'] === $type;
|
2018-09-19 10:27:18 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-18 11:45:45 +03:00
|
|
|
/**
|
|
|
|
* Add a template to the global template folder
|
|
|
|
*
|
|
|
|
* @param string $templateName
|
|
|
|
* @param string $templateFile
|
2018-09-19 09:23:39 +03:00
|
|
|
* @return array
|
2018-09-18 11:45:45 +03:00
|
|
|
*/
|
2018-09-19 09:23:39 +03:00
|
|
|
public function add(string $templateName, string $templateFile): array{
|
2018-09-18 11:45:45 +03:00
|
|
|
try {
|
|
|
|
$template = $this->folder->getFile($templateName);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
$template = $this->folder->newFile($templateName);
|
|
|
|
}
|
|
|
|
$template->putContent($templateFile);
|
|
|
|
|
2018-09-18 20:14:05 +03:00
|
|
|
return $this->formatNodeReturn($this->get($templateName));
|
2018-09-18 19:47:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a template to the global template folder
|
|
|
|
*
|
|
|
|
* @param string $templateName
|
2018-09-19 09:23:39 +03:00
|
|
|
* @return boolean
|
|
|
|
* @throws NotFoundException
|
2018-09-18 19:47:14 +03:00
|
|
|
*/
|
2018-09-19 09:23:39 +03:00
|
|
|
public function delete(string $templateName): bool {
|
2018-09-18 19:47:14 +03:00
|
|
|
try {
|
2018-09-18 20:31:29 +03:00
|
|
|
$template = $this->get($templateName);
|
2018-09-18 20:14:05 +03:00
|
|
|
$template->delete();
|
2018-09-18 19:47:14 +03:00
|
|
|
} catch (NotFoundException $e) {
|
2018-09-19 09:23:39 +03:00
|
|
|
throw new NotFoundException($e->getMessage());
|
2018-09-18 19:47:14 +03:00
|
|
|
}
|
2018-09-19 09:23:39 +03:00
|
|
|
|
2018-09-18 20:14:05 +03:00
|
|
|
return true;
|
2018-09-18 11:45:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-18 19:47:14 +03:00
|
|
|
* Flip $tplTypes to retrieve types by mime
|
2018-09-18 11:45:45 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function flipTypes(): array{
|
|
|
|
$result = array();
|
2018-09-19 18:00:05 +03:00
|
|
|
foreach ($this::$tplTypes as $type => $mime) {
|
|
|
|
$result = array_merge($result, array_fill_keys($mime, $type));
|
2018-09-18 11:45:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2018-09-18 19:47:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user template directory
|
|
|
|
*
|
|
|
|
* @return Node
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
private function getUserTemplateDir(): Node {
|
|
|
|
// has the user manually set a directory as the default template dir ?
|
|
|
|
$templateDirID = $this->config->getUserValue($this->userId, $this->appName, 'template_dir', false);
|
|
|
|
|
|
|
|
if ($templateDirID !== false) {
|
|
|
|
$templateDir = $this->userFolder->getById($templateDirID);
|
|
|
|
} else {
|
|
|
|
// fallback to default template dir
|
|
|
|
try {
|
|
|
|
$templateDir = $this->userFolder->get('Templates');
|
|
|
|
} catch (NotFoundException $e) {
|
2018-09-19 09:23:39 +03:00
|
|
|
throw new NotFoundException($e->getMessage());
|
2018-09-18 19:47:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $templateDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format template file for json return object
|
|
|
|
*
|
2018-09-18 20:31:29 +03:00
|
|
|
* @param ISimpleFile|Node $template
|
2018-09-18 19:47:14 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function formatNodeReturn($template): array{
|
|
|
|
return [
|
|
|
|
'name' => $template->getName(),
|
2018-09-18 20:10:46 +03:00
|
|
|
'preview' => $this->urlGenerator->linkToRoute('richdocuments.templates.getPreview', ['templateName' => $template->getName()]),
|
2018-09-19 18:00:05 +03:00
|
|
|
'ext' => $this->flipTypes()[$template->getMimeType()],
|
2018-09-18 19:47:14 +03:00
|
|
|
'etag' => $template->getETag(),
|
2018-09-19 09:23:39 +03:00
|
|
|
'delete' => $this->urlGenerator->linkToRoute('richdocuments.templates.delete', ['templateName' => $template->getName()])
|
2018-09-18 19:47:14 +03:00
|
|
|
];
|
|
|
|
}
|
2018-09-18 11:45:45 +03:00
|
|
|
}
|