Refactor: Create shared AttachmentService and AttachmentModel
- Created common AttachmentService for all modules (app/Services/) - Created common AttachmentModel (app/Models/) - Created common migration (app/Database/Migrations/) - Updated Tasks module to use shared AttachmentService - Added Twig functions: format_filesize(), get_file_icon() - Removed duplicate TaskAttachmentModel and task_attachments migration - AttachmentService can be used by any module: task, deal, contact, etc.
This commit is contained in:
@@ -52,6 +52,10 @@ class TwigGlobalsExtension extends AbstractExtension
|
||||
new TwigFunction('is_module_active', [$this, 'isModuleActive'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('is_module_available', [$this, 'isModuleAvailable'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('csrf_meta', [$this, 'csrf_meta'], ['is_safe' => ['html']]),
|
||||
|
||||
// Attachment functions
|
||||
new TwigFunction('format_filesize', [$this, 'formatFilesize'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('get_file_icon', [$this, 'getFileIcon'], ['is_safe' => ['html']]),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -59,6 +63,42 @@ class TwigGlobalsExtension extends AbstractExtension
|
||||
{
|
||||
return csrf_meta();
|
||||
}
|
||||
|
||||
public function formatFilesize(int $bytes): string
|
||||
{
|
||||
if ($bytes >= 1073741824) {
|
||||
return number_format($bytes / 1073741824, 2) . ' GB';
|
||||
} elseif ($bytes >= 1048576) {
|
||||
return number_format($bytes / 1048576, 2) . ' MB';
|
||||
} elseif ($bytes >= 1024) {
|
||||
return number_format($bytes / 1024, 2) . ' KB';
|
||||
}
|
||||
|
||||
return $bytes . ' B';
|
||||
}
|
||||
|
||||
public function getFileIcon(string $fileType): string
|
||||
{
|
||||
$icons = [
|
||||
'image' => 'fa-file-image',
|
||||
'pdf' => 'fa-file-pdf',
|
||||
'word' => 'fa-file-word',
|
||||
'excel' => 'fa-file-excel',
|
||||
'powerpoint' => 'fa-file-powerpoint',
|
||||
'zip' => 'fa-file-zipper',
|
||||
'text' => 'fa-file-lines',
|
||||
'video' => 'fa-file-video',
|
||||
'audio' => 'fa-file-audio',
|
||||
];
|
||||
|
||||
foreach ($icons as $key => $icon) {
|
||||
if (stripos($fileType, $key) !== false) {
|
||||
return $icon;
|
||||
}
|
||||
}
|
||||
|
||||
return 'fa-file';
|
||||
}
|
||||
// ========================================
|
||||
// Access Functions для Twig
|
||||
// ========================================
|
||||
|
||||
Reference in New Issue
Block a user