Issues (2050)

controller/mapa_edificaciones.php (4 issues)

1
<?php
2
/*
3
 * Copyright (C) 2017 Joe Nilson <joenilson at gmail.com>
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
/**
19
 * Description of mapa_edificaciones
20
 *
21
 * @author Joe Nilson <joenilson at gmail.com>
22
 */
23
class mapa_edificaciones extends fs_controller
24
{
25
    public $edificaciones_tipo;
26
    public $edificaciones_mapa;
27
    public $padre;
28
    public $hijo;
29
    public $mapa;
30
    public $edificacion;
31
    public $edificacion_padre;
32
    public $edificacion_info;
33
    public $edificacion_interior;
34
35
    public function __construct()
36
    {
37
        parent::__construct(__CLASS__, 'Mapa de Edificaciones', 'residentes', false, false, false);
38
    }
39
40
    /**
41
     * @throws JsonException
42
     */
43
    protected function private_core()
44
    {
45
        $this->edificaciones_tipo = new residentes_edificaciones_tipo();
46
        $this->edificaciones_mapa = new residentes_edificaciones_mapa();
47
        $tipos = $this->edificaciones_tipo->all();
48
        $this->padre = $tipos[0];
49
50
        $accion_p = \filter_input(INPUT_POST, 'accion');
51
        $accion_g = \filter_input(INPUT_GET, 'accion');
52
        $accion = ($accion_p)?:$accion_g;
53
        if ($accion === 'agregar_base') {
54
            $this->agregar($this->padre);
55
        } elseif ($accion === 'agregar_hijo') {
56
            $id = \filter_input(INPUT_POST, 'id_hijo');
57
            $objeto = $this->edificaciones_tipo->get($id);
58
            $this->agregar($objeto);
59
        } elseif ($accion === 'eliminar') {
60
            $id = \filter_input(INPUT_GET, 'id');
61
            $estructura = $this->edificaciones_mapa->get($id);
62
            if ($estructura->tiene_hijos()) {
63
                $this->new_error_msg('Esta estructura tiene edificaciones internas, '.
64
                                'primero debe eliminarlas para eliminar esta.');
65
            } else {
66
                try {
67
                    $estructura->delete();
68
                    $this->new_message('Edificación eliminada correctamente.');
69
                } catch (\Exception $ex) {
70
                    $this->new_error_msg('Ocurrió un error intentando eliminar la edificación');
71
                    $this->new_error_msg($ex->getTraceAsString());
72
                }
73
            }
74
        }
75
76
        $tipo = $accion = \filter_input(INPUT_GET, 'type');
0 ignored issues
show
The assignment to $accion is dead and can be removed.
Loading history...
77
        if ($tipo === 'select-hijos') {
78
            $this->obtener_hijos();
79
        }
80
        $inmuebles = \filter_input(INPUT_GET, 'inmuebles');
81
        if (!empty($inmuebles)) {
82
            $this->inmuebles = $inmuebles;
0 ignored issues
show
Bug Best Practice introduced by
The property inmuebles does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83
            $this->inmuebles_info = $this->edificaciones_mapa->get($inmuebles);
0 ignored issues
show
Bug Best Practice introduced by
The property inmuebles_info does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84
            $this->lista_inmuebles = $this->edificaciones_mapa->get_by_field('padre_id', $inmuebles);
0 ignored issues
show
Bug Best Practice introduced by
The property lista_inmuebles does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
85
            $this->template = "mapa/inmuebles";
86
        }
87
        $this->mapa = $this->edificaciones_mapa->get_by_field('id_tipo', $this->padre->id);
88
        $this->hijo = $this->edificaciones_tipo->get_by_field('padre', $this->padre->id);
89
    }
90
91
    /**
92
     * funcion para guardar los codigos de las edificaciones base Manzana, Zona, Grupo, Edificio, etc
93
     */
94
    public function agregar($objeto)
95
    {
96
        $inicio = \filter_input(INPUT_POST, 'inicio');
97
        $final_p = \filter_input(INPUT_POST, 'final');
98
        $final=(!empty($final_p))?$final_p:$inicio;
99
        $inmuebles = 0;
100
        $error = 0;
101
        $linea = 0;
102
        if ($inicio === $final) {
103
            $this->edificacion($inicio, $inmuebles, $error, $objeto);
104
            $linea++;
105
        } else {
106
            foreach (range($inicio, $final) as $item) {
107
                $this->edificacion($item, $inmuebles, $error, $objeto);
108
                $linea++;
109
            }
110
        }
111
        if ($error) {
112
            $this->new_error_msg('No puedieron guardarse la informacion de '.$error.' inmuebles, revise su listado.');
113
        }
114
        $this->new_message('Se guardaron correctamente '.$inmuebles.' inmuebles.');
115
    }
116
117
    public function edificacion($i, &$inmuebles, &$error, $objeto)
118
    {
119
        $id = \filter_input(INPUT_POST, 'id');
120
        $codigo_padre = \filter_input(INPUT_POST, 'codigo_padre');
121
        $padre_id = \filter_input(INPUT_POST, 'padre_id');
122
        $item = (is_int($i))?str_pad($i, 3, "0", STR_PAD_LEFT):$i;
123
        $punto = new residentes_edificaciones_mapa();
124
        $punto->id = $id;
125
        $punto->id_tipo = $objeto->id;
126
        $punto->codigo_edificacion = $item;
127
        $punto->codigo_padre = $codigo_padre;
128
        $punto->padre_tipo = $objeto->padre;
129
        $punto->padre_id = $padre_id;
130
        $punto->numero = '';
131
        if ($punto->save()) {
132
            $inmuebles++;
133
        } else {
134
            $error++;
135
        }
136
    }
137
138
    public function obtener_hijos()
139
    {
140
        $this->template = false;
141
        $id_tipo = \filter_input(INPUT_GET, 'id_tipo');
142
        $hijos = array();
143
        if ($id_tipo) {
144
            $hijos = $this->edificaciones_tipo->get_by_field('padre', $id_tipo);
145
        }
146
        header('Content-Type: application/json');
147
        echo json_encode($hijos, JSON_THROW_ON_ERROR);
148
    }
149
}
150