1
0
cms11/app/Classes/Traits/ManagesAttachments.php

80 lines
1.9 KiB
PHP
Raw Normal View History

2024-04-17 15:29:12 +02:00
<?php
namespace App\Classes\Traits;
use App\Classes\AttachmentsManager;
trait ManagesAttachments
{
private array $attachmentsManagers = [];
/**
* Iterates over attached manager to replace attachments in specified
* markdown with appropriate Blade components
*/
public function replaceAttachmentsInMarkdown(string $markdown)
{
foreach ($this->attachmentsManagers as $manager) {
$markdown = $manager->replaceAttachmentsInMarkdown($markdown);
}
return $markdown;
}
/**
* Return an instance of attachments manager for specified kind of files
*/
public function attachments(string $kind): AttachmentsManager
{
return $this->registerAttachmentsManager($kind);
}
2024-04-17 15:29:12 +02:00
/**
* Register an attachments manager for specified filename
*/
2024-04-17 16:18:39 +02:00
private function registerAttachmentsManager(string $kind): AttachmentsManager
2024-04-17 15:29:12 +02:00
{
2024-04-17 16:18:39 +02:00
if (!array_key_exists($kind, $this->attachmentsManagers)) {
$this->attachmentsManagers[$kind] = new AttachmentsManager($kind, $this);
2024-04-17 15:29:12 +02:00
}
2024-04-17 16:18:39 +02:00
return $this->attachmentsManagers[$kind];
2024-04-17 15:29:12 +02:00
}
/**
* Load all attachments files
*/
private function loadAttachments()
{
foreach ($this->attachmentsManagers as $manager) {
$manager->load();
}
}
2024-04-17 15:29:12 +02:00
/**
* Save all attachments files that needs to be
*/
2024-04-26 23:05:49 +02:00
private function saveAttachments(): bool
2024-04-17 15:29:12 +02:00
{
2024-04-26 23:05:49 +02:00
$allSaved = true;
2024-04-17 15:29:12 +02:00
foreach ($this->attachmentsManagers as $manager) {
2024-04-26 23:05:49 +02:00
if (!$manager->save()) {
$allSaved = false;
}
2024-04-17 15:29:12 +02:00
}
2024-04-26 23:05:49 +02:00
return $allSaved;
2024-04-17 15:29:12 +02:00
}
/**
* Repair all attachments files that needs to be
2024-04-17 15:29:12 +02:00
*/
private function repairAttachments()
2024-04-17 15:29:12 +02:00
{
foreach ($this->attachmentsManagers as $manager) {
$manager->repair();
}
2024-04-17 15:29:12 +02:00
}
}