Optimizaciones y funciones
This commit is contained in:
@ -20,22 +20,72 @@ use Incoviba\old\Common\Direccion;
|
||||
* @property string abreviacion
|
||||
*
|
||||
*/
|
||||
class Agente extends Model
|
||||
{
|
||||
public function direccion()
|
||||
{
|
||||
return $this->belongs_to(Direccion::class, 'direccion')->findOne();
|
||||
class Agente extends Model {
|
||||
protected $direccion_obj;
|
||||
public function direccion() {
|
||||
if ($this->direccion_obj === null) {
|
||||
$this->direccion_obj = $this->setRelationship(Direccion::class, 'id', 'direccion')->one();
|
||||
}
|
||||
return $this->direccion_obj;
|
||||
//return $this->belongs_to(Direccion::class, 'direccion')->findOne();
|
||||
}
|
||||
public function tipo()
|
||||
{
|
||||
return $this->belongs_to(TipoAgente::class, 'tipo')->findOne();
|
||||
protected $tipo_obj;
|
||||
public function tipo() {
|
||||
if ($this->tipo_obj === null) {
|
||||
$this->tipo_obj = $this->setRelationship(TipoAgente::class, 'id', 'tipo')->one();
|
||||
}
|
||||
return $this->tipo_obj;
|
||||
//return $this->belongs_to(TipoAgente::class, 'tipo')->findOne();
|
||||
}
|
||||
protected $tipos;
|
||||
public function tipos(int $tipo = 0)
|
||||
{
|
||||
if ($tipo == 0) {
|
||||
if ($this->tipos === null) {
|
||||
$this->tipos = [];
|
||||
}
|
||||
if (!isset($this->tipos[$tipo])) {
|
||||
$this->tipos[$tipo] = $this->setRelationship(AgenteTipo::class, 'id', 'agente');
|
||||
if ($tipo != 0) {
|
||||
$this->tipos[$tipo] = $this->tipos[$tipo]->where(['tipo', $tipo]);
|
||||
}
|
||||
$this->tipos[$tipo];
|
||||
}
|
||||
return $this->tipos[$tipo];
|
||||
|
||||
/*if ($tipo == 0) {
|
||||
return $this->hasMany(AgenteTipo::class, 'agente')->findMany();
|
||||
}
|
||||
return $this->hasMany(AgenteTipo::class, 'agente')->where('tipo', $tipo)->findOne();
|
||||
return $this->hasMany(AgenteTipo::class, 'agente')->where('tipo', $tipo)->findOne();*/
|
||||
}
|
||||
protected $proyectos;
|
||||
public function proyectos() {
|
||||
if ($this->proyectos === null) {
|
||||
$proyectos = [];
|
||||
foreach ($this->tipos() as $tipo) {
|
||||
foreach ($tipo->proyectos() as $proyecto) {
|
||||
$proyectos []= $proyecto->proyecto();
|
||||
}
|
||||
}
|
||||
usort($proyectos, function($a, $b) {
|
||||
return strcmp($a->descripcion, $b->descripcion);
|
||||
});
|
||||
$this->proyectos = $proyectos;
|
||||
}
|
||||
return $this->proyectos;
|
||||
}
|
||||
public function hasProyecto(int $id): bool {
|
||||
$proyectos = $this->proyectos();
|
||||
foreach ($proyectos as $proyecto) {
|
||||
if ($proyecto->id == $id) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function isOperador(): bool {
|
||||
if ($this->tipos(19)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Reference in New Issue
Block a user