Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add check if collabora supports template filling #3875

Merged
merged 1 commit into from
Aug 5, 2024
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
18 changes: 8 additions & 10 deletions lib/Listener/FileCreatedFromTemplateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\Richdocuments\Listener;

use OCA\Richdocuments\Service\CapabilitiesService;
use OCA\Richdocuments\Service\TemplateFieldService;
use OCA\Richdocuments\TemplateManager;
use OCP\EventDispatcher\Event;
Expand All @@ -17,17 +18,12 @@

/** @template-implements IEventListener<Event|FileCreatedFromTemplateEvent> */
class FileCreatedFromTemplateListener implements IEventListener {
/** @var TemplateManager */
private $templateManager;
/** @var TemplateFieldService */
private $templateFieldService;

public function __construct(
TemplateManager $templateManager,
TemplateFieldService $templateFieldService
private TemplateManager $templateManager,
private TemplateFieldService $templateFieldService,
private CapabilitiesService $capabilitiesService,
) {
$this->templateManager = $templateManager;
$this->templateFieldService = $templateFieldService;
}

public function handle(Event $event): void {
Expand Down Expand Up @@ -55,8 +51,10 @@ public function handle(Event $event): void {
$this->templateManager->setTemplateSource($event->getTarget()->getId(), $templateFile->getId());
}

$filledTemplate = $this->templateFieldService->fillFields($templateFile, $event->getTemplateFields());
$event->getTarget()->putContent($filledTemplate);
if ($this->capabilitiesService->hasFormFilling()) {
$filledTemplate = $this->templateFieldService->fillFields($templateFile, $event->getTemplateFields());
$event->getTarget()->putContent($filledTemplate);
}

// Avoid having the mimetype of the source file set
$event->getTarget()->getStorage()->getCache()->update($event->getTarget()->getId(), [
Expand Down
15 changes: 11 additions & 4 deletions lib/Service/CapabilitiesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ public function getServerProductName(): ?string {
}

public function hasNextcloudBranding(): bool {
$productVersion = $this->getCapabilities()['productVersion'] ?? '0.0.0.0';
return version_compare($productVersion, '21.11', '>=');
return $this->isVersionAtLeast('21.11');
}

public function hasDrawSupport(): bool {
$productVersion = $this->getCapabilities()['productVersion'] ?? '0.0.0.0';
return version_compare($productVersion, '6.4.7', '>=');
return $this->isVersionAtLeast('6.4.7');
}

public function hasTemplateSource(): bool {
Expand All @@ -87,6 +85,15 @@ public function hasWASMSupport(): bool {
return $this->getCapabilities()['hasWASMSupport'] ?? false;
}

public function hasFormFilling(): bool {
return $this->isVersionAtLeast('24.04.5.2');
}

private function isVersionAtLeast(string $version): bool {
$productVersion = $this->getCapabilities()['productVersion'] ?? '0.0.0.0';
return version_compare($productVersion, $version, '>=');
}

public function getProductName(): string {
$theme = $this->config->getAppValue(Application::APPNAME, 'theme', 'nextcloud');

Expand Down
9 changes: 9 additions & 0 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class TemplateFieldService {
public function __construct(
private IClientService $clientService,
private CapabilitiesService $capabilitiesService,
private AppConfig $appConfig,
private IRootFolder $rootFolder,
private LoggerInterface $logger
Expand All @@ -29,6 +30,10 @@ public function __construct(
* @return array|string
*/
public function extractFields(Node|int $file) {
if (!$this->capabilitiesService->hasFormFilling()) {
return [];
}

if (is_int($file)) {
$file = $this->rootFolder->getFirstNodeById($file);
}
Expand Down Expand Up @@ -84,6 +89,10 @@ public function extractFields(Node|int $file) {
* @return string|resource
*/
public function fillFields(Node|int $file, array $fields = []) {
if (!$this->capabilitiesService->hasFormFilling()) {
throw new \RuntimeException('Form filling not supported by the Collabora server');
}

if (is_int($file)) {
$file = $this->rootFolder->getFirstNodeById($file);
}
Expand Down
Loading