66 lines
2.1 KiB
PHP
66 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Activity;
|
|
|
|
use App\Enums\ActivityType;
|
|
use App\Models\ActivityForm;
|
|
use App\Models\ActivityFormMetadata;
|
|
use App\Services\SigdCrudService;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class FormMetadataService extends SigdCrudService
|
|
{
|
|
public function __construct(ActivityFormMetadata $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
public function store($data)
|
|
{
|
|
$formId = $data['form_id'] ?? null;
|
|
$form = ActivityForm::rowActive()->where('id', $formId)->first();
|
|
if ($form) {
|
|
$storagePath = 'forms/' . $form->sector . '/' . $form->form_code . '/' . $form->inventory_year . '/metadata';
|
|
|
|
$metadata = null;
|
|
if (isset($data['id'])) {
|
|
$metadata = ActivityFormMetadata::rowActive()->find($data['id']);
|
|
}
|
|
|
|
if ($metadata) {
|
|
if (isset($data['file_document'])) {
|
|
$oldFilePath = $storagePath . '/' . $metadata->file_name;
|
|
|
|
if ($metadata->file_name != $data['file_document']->hashName()) {
|
|
Storage::disk('public')->delete($oldFilePath);
|
|
}
|
|
|
|
$data['file_name'] = basename($data['file_document']->store($storagePath, 'public'));
|
|
}
|
|
|
|
$this->update($metadata, $data);
|
|
} else {
|
|
if (isset($data['file_document'])) {
|
|
$data['file_name'] = basename($data['file_document']->store($storagePath, 'public'));
|
|
}
|
|
|
|
$metadata = $this->create($data);
|
|
}
|
|
|
|
logUserActivity(ActivityType::INSERT_METADATA, $formId);
|
|
return $metadata;
|
|
}
|
|
}
|
|
|
|
public function destroy(ActivityFormMetadata $metadata, $storagePath)
|
|
{
|
|
if ($metadata) {
|
|
$oldFilePath = $storagePath . '/' . $metadata->file_name;
|
|
Storage::disk('public')->delete($oldFilePath);
|
|
|
|
$this->delete($metadata);
|
|
logUserActivity(ActivityType::DELETE_METADATA, $metadata->form_id);
|
|
}
|
|
}
|
|
}
|