mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Merge branch 'feat/importador_libros_catalogo_antiguo' into 'main'
Feat/importador libros catalogo antiguo See merge request jjimenez/safekat!838
This commit is contained in:
@ -93,12 +93,12 @@ class CatalogoLibroModel extends Model
|
||||
protected $validationMessages = [];
|
||||
protected $skipValidation = false;
|
||||
|
||||
protected $beforeInsert = ['asignarIsk', 'asignarEan', 'asignarCubiertaUrl'];
|
||||
protected $beforeInsert = ['asignarIskn', 'asignarEan', 'asignarCubiertaUrl'];
|
||||
protected $beforeUpdate = ['asignarEan', 'asignarCubiertaUrl'];
|
||||
|
||||
protected function asignarIsk(array $data): array
|
||||
protected function asignarIskn(array $data): array
|
||||
{
|
||||
$data['data']['isk'] = model('App\Models\Catalogo\IdentificadorIskModel')->newIsk();
|
||||
$data['data']['iskn'] = model('App\Models\Catalogo\IdentificadorIsknModel')->newIskn();
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
@ -1,79 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Catalogo;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
use RuntimeException;
|
||||
|
||||
class IdentificadorIskModel extends Model
|
||||
{
|
||||
protected $table = 'identificadores_isk';
|
||||
protected $primaryKey = 'id';
|
||||
protected $returnType = \App\Entities\Catalogo\IdentificadorIsk::class;
|
||||
protected $useSoftDeletes = false; // No soft delete
|
||||
protected $useTimestamps = true;
|
||||
protected $allowedFields = ['isk'];
|
||||
|
||||
protected $beforeInsert = ['agregarIsk'];
|
||||
|
||||
/**
|
||||
* Crea un nuevo registro con un ISK único y lo devuelve.
|
||||
*/
|
||||
public function newIsk(string $contexto = 'libro'): string
|
||||
{
|
||||
$isk = $this->generarIskUnico($contexto);
|
||||
$this->insert(['isk' => $isk]);
|
||||
|
||||
return $isk;
|
||||
}
|
||||
|
||||
/**
|
||||
* Genera un ISK único validado contra la base de datos.
|
||||
*/
|
||||
private function generarIskUnico(string $contexto): string
|
||||
{
|
||||
do {
|
||||
$isk = $this->generarIsk($contexto);
|
||||
} while ($this->where('isk', $isk)->countAllResults() > 0);
|
||||
|
||||
return $isk;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formato legible de ISK, ejemplo: isk_libro_20250419_ab12c
|
||||
*/
|
||||
private function generarIsk(string $contexto): string
|
||||
{
|
||||
$fecha = date('Ymd');
|
||||
$random = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, 5);
|
||||
return "isk_{$contexto}_{$fecha}_{$random}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook para generar el ISK automáticamente al insertar.
|
||||
*/
|
||||
protected function agregarIsk(array $data): array
|
||||
{
|
||||
if (!isset($data['data']['isk']) || empty($data['data']['isk'])) {
|
||||
$data['data']['isk'] = $this->generarIskUnico('registro');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
// Bloqueo total de eliminaciones
|
||||
public function delete($id = null, bool $purge = false)
|
||||
{
|
||||
throw new RuntimeException('La eliminación de registros está deshabilitada.');
|
||||
}
|
||||
|
||||
public function deleteWhere($where)
|
||||
{
|
||||
throw new RuntimeException('La eliminación de registros está deshabilitada.');
|
||||
}
|
||||
|
||||
public function deleteBatch($where)
|
||||
{
|
||||
throw new RuntimeException('La eliminación de registros está deshabilitada.');
|
||||
}
|
||||
}
|
||||
79
ci4/app/Models/Catalogo/IdentificadorIsknModel.php
Normal file
79
ci4/app/Models/Catalogo/IdentificadorIsknModel.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Catalogo;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
use RuntimeException;
|
||||
|
||||
class IdentificadorIsknModel extends Model
|
||||
{
|
||||
protected $table = 'identificadores_iskn';
|
||||
protected $primaryKey = 'id';
|
||||
protected $returnType = \App\Entities\Catalogo\IdentificadorIskn::class;
|
||||
protected $useSoftDeletes = false; // No soft delete
|
||||
protected $useTimestamps = true;
|
||||
protected $allowedFields = ['iskn'];
|
||||
|
||||
protected $beforeInsert = ['agregarIskn'];
|
||||
|
||||
/**
|
||||
* Crea un nuevo registro con un ISKN único y lo devuelve.
|
||||
*/
|
||||
public function newIskn(string $contexto = 'libro'): string
|
||||
{
|
||||
$iskn = $this->generarIsknUnico($contexto);
|
||||
$this->insert(['iskn' => $iskn]);
|
||||
|
||||
return $iskn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Genera un ISKN único validado contra la base de datos.
|
||||
*/
|
||||
private function generarIsknUnico(string $contexto): string
|
||||
{
|
||||
do {
|
||||
$iskn = $this->generarIskn($contexto);
|
||||
} while ($this->where('iskn', $iskn)->countAllResults() > 0);
|
||||
|
||||
return $iskn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formato legible de ISKN, ejemplo: iskn_libro_20250419_ab12c
|
||||
*/
|
||||
private function generarIskn(string $contexto): string
|
||||
{
|
||||
$fecha = date('Ymd');
|
||||
$random = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, 5);
|
||||
return "iskn_{$contexto}_{$fecha}_{$random}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook para generar el ISKN automáticamente al insertar.
|
||||
*/
|
||||
protected function agregarIskn(array $data): array
|
||||
{
|
||||
if (!isset($data['data']['iskn']) || empty($data['data']['iskn'])) {
|
||||
$data['data']['iskn'] = $this->generarIsknUnico('registro');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
// Bloqueo total de eliminaciones
|
||||
public function delete($id = null, bool $purge = false)
|
||||
{
|
||||
throw new RuntimeException('La eliminación de registros está deshabilitada.');
|
||||
}
|
||||
|
||||
public function deleteWhere($where)
|
||||
{
|
||||
throw new RuntimeException('La eliminación de registros está deshabilitada.');
|
||||
}
|
||||
|
||||
public function deleteBatch($where)
|
||||
{
|
||||
throw new RuntimeException('La eliminación de registros está deshabilitada.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user