Desacople e inyeccion de dependencias

This commit is contained in:
imnavajas
2025-07-22 16:01:34 +02:00
parent a1aaa095d4
commit 9ed397e9ad
9 changed files with 391 additions and 154 deletions

View File

@ -32,7 +32,7 @@ class PresupuestoFicheroModel extends \App\Models\BaseModel
$this->db->table($this->table . " t1")
->set('presupuesto_id', $presupuesto_id)
->set('nombre', $filename)
->set('file_path', WRITEPATH . 'uploads/presupuestos/' . $new_filename)
->set('file_path', 'uploads/presupuestos/' . $new_filename)
->set('upload_by', $user_id)
->set('upload_at', date('Y-m-d H:i:s'))
->insert();
@ -54,8 +54,9 @@ class PresupuestoFicheroModel extends \App\Models\BaseModel
// se comprueba que el $file->nombre no sea igual a ninguno de los elementos del array $old_files
if (!in_array($file->nombre, $old_files)) {
if (file_exists($file->file_path)) {
unlink($file->file_path);
$fullPath = WRITEPATH . $file->file_path;
if (file_exists($fullPath)) {
unlink($fullPath);
}
$this->db
@ -76,20 +77,23 @@ class PresupuestoFicheroModel extends \App\Models\BaseModel
->table($this->table . " t1")
->where('presupuesto_id', $presupuesto_id_origen)->get()->getResult();
if ($files) {
foreach ($files as $file) {
$hash = $this->generateFileHash($file->nombre);
// se copia el fichero a la nueva ubicación
if (!file_exists(WRITEPATH . $file->file_path)) {
copy($file->file_path, WRITEPATH . 'uploads/presupuestos/' . $hash);
$originalPath = WRITEPATH . $file->file_path;
$newPath = 'uploads/presupuestos/' . $hash;
if (file_exists($originalPath)) {
copy($originalPath, WRITEPATH . $newPath);
}
$this->db->table($this->table . " t1")
->set('presupuesto_id', $presupuesto_id_destino)
->set('nombre', $file->nombre)
->set('file_path', WRITEPATH . 'uploads/presupuestos/' . $hash)
->set('file_path', $newPath)
->set('upload_by', auth()->user()->id)
->set('upload_at', date('Y-m-d H:i:s'))
->insert();
@ -105,6 +109,25 @@ class PresupuestoFicheroModel extends \App\Models\BaseModel
->where('presupuesto_id', $presupuesto_id)->get()->getResult();
}
public function deleteMissingFiles(int $presupuesto_id, array $keepNames = [])
{
$files = $this->getFiles($presupuesto_id);
foreach ($files as $file) {
if (!in_array($file->nombre, $keepNames)) {
$fullPath = WRITEPATH . $file->file_path;
if (file_exists($fullPath)) {
unlink($fullPath);
}
$this->db->table($this->table)
->where('presupuesto_id', $presupuesto_id)
->where('nombre', $file->nombre)
->delete();
}
}
}
/**
* Función para convertir el nombre y extensión de un fichero en un hash único
@ -117,6 +140,4 @@ class PresupuestoFicheroModel extends \App\Models\BaseModel
{
return hash('sha256', $filename);
}
}