57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Setting;
|
|
|
|
use App\Models\CmsFilePeraturan;
|
|
use App\Models\ReferenceSector;
|
|
use App\Services\SigdCrudService;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class PeraturanService extends SigdCrudService
|
|
{
|
|
public function __construct(CmsFilePeraturan $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
public function save($data)
|
|
{
|
|
$id = $data['id'] ?? null;
|
|
$file = CmsFilePeraturan::rowActive()->where('id', $id)->first();
|
|
$storagePath = 'peraturan';
|
|
|
|
if ($file) {
|
|
$data['file_upload'] = $file->file_upload;
|
|
if (isset($data['file_document'])) {
|
|
$oldFilePath = $storagePath . '/' . $file->file_upload;
|
|
|
|
if ($file->file_upload != $data['file_document']->hashName()) {
|
|
Storage::disk('public')->delete($oldFilePath);
|
|
}
|
|
|
|
$data['file_upload'] = basename($data['file_document']->store($storagePath, 'public'));
|
|
}
|
|
|
|
$this->update($file, $data);
|
|
} else {
|
|
if (isset($data['file_document'])) {
|
|
$data['file_upload'] = basename($data['file_document']->store($storagePath, 'public'));
|
|
}
|
|
|
|
$file = $this->create($data);
|
|
}
|
|
|
|
return $file;
|
|
}
|
|
|
|
public function destroy(CmsFilePeraturan $fileData, $storagePath)
|
|
{
|
|
if ($fileData) {
|
|
$oldFilePath = $storagePath . '/' . $fileData->file_upload;
|
|
Storage::disk('public')->delete($oldFilePath);
|
|
|
|
$this->delete($fileData);
|
|
}
|
|
}
|
|
}
|