Uname: Linux info 3.0 #1337 SMP Tue Jan 01 00:00:00 CEST 2000 all GNU/Linux
Software: Apache
PHP version: 5.6.40 [ PHP INFO ] PHP os: Linux
Server Ip: 217.160.0.194
Your Ip: 216.73.217.65
User: u72294154 (9179942) | Group: ftpusers (600)
Safe Mode: OFF
Disable Function:
NONE

name : InmuebleComponent.php
<?

App::uses('AppComponent', 'Controller/Component');

class InmuebleComponent extends AppComponent {

    public $numSimilares = 3;

    //Método que devuelve un array con los valores generados para filtros de tipo selector
    public function getSelects($conditions, $alquiler = false) {
        $options = array('precio' => array(), 'superficie' => array());

        //Precio
        if ((!empty($conditions['Inmueble.id_gestion']) && in_array($conditions['Inmueble.id_gestion'], Configure::read('Inmueble.gestionesAlquiler'))) || $alquiler) {
            for ($i = 200; $i <= 2000; $i += 50) {
                $options['precio'][$i] = number_format($i, 0, ',', '.') . ' €';
            }
        } else {
            for ($i = 50000; $i <= 800000; $i += 50000) {
                $options['precio'][$i] = number_format($i, 0, ',', '.') . ' €';
            }
            $options['precio'][1000000] = number_format(1000000, 0, ',', '.') . ' €';
            $options['precio'][1500000] = number_format(1500000, 0, ',', '.') . ' €';
            $options['precio'][3000000] = number_format(3000000, 0, ',', '.') . ' €';
            $options['precio'][4000000] = number_format(4000000, 0, ',', '.') . ' €';
        }



        // Superficie
        if ((!empty($conditions['Inmueble.id_gestion']) && in_array($conditions['Inmueble.id_gestion'], Configure::read('Inmueble.gestionesAlquiler'))) || $alquiler) {
            for ($i = 50; $i <= 3000; $i += 100) {
                $options['superficie'][$i] = number_format($i, 0, ',', '.') . ' m2';
            }
        } else {
            for ($i = 50; $i <= 3000; $i += 100) {
                $options['superficie'][$i] = number_format($i, 0, ',', '.') . ' m2';
            }
        }

        return $options;
    }

    public function getSimilares($inmueble, $conditions) {
        $this->loadModels('Inmueble');

        //Similares que cumplen con los filtros realizados en el listado de inmuebles
        $this->similaresIDs = (!empty($conditions)) ? $this->Inmueble->find('list', array('fields' => 'Inmueble.id', 'conditions' => $conditions, 'limit' => $this->numSimilares, 'order' => 'RAND()')) : array();

        //Otros inmuebles que no cumplen con todas las condiciones
        $conditions = array(
            'Inmueble.publicado' => true,
            'Inmueble.id_gestion' => $inmueble['Inmueble']['id_gestion'],
            'Inmueble.id_tipo_inmueble' => $inmueble['Inmueble']['id_tipo_inmueble'],
            'Inmueble.estado_inmueble' => 'Disponible',
            'Inmueble.precio >' => $inmueble['Inmueble']['precio'] - 20000,
            'Inmueble.precio <' => $inmueble['Inmueble']['precio'] + 20000,
            'Inmueble.id_provincia' => $inmueble['Inmueble']['id_provincia'],
            'Inmueble.id_localidad' => $inmueble['Inmueble']['id_localidad'],
            'NOT' => array('Inmueble.id' => array_merge(array_values($this->similaresIDs), array((int) $inmueble['Inmueble']['id']))),
        );
        $this->getSimilaresOtros($inmueble, $conditions);

        return $this->Inmueble->find('all', array('conditions' => array('Inmueble.id' => $this->similaresIDs), 'order' => 'RAND()'));
    }

    private function getSimilaresOtros($inmueble, $conditions, $conditionsFields = array('id_gestion', 'id_tipo_inmueble', 'id_localidad', 'zona_id')) {
        if ($this->numSimilares > count($this->similaresIDs)) {
            $this->similaresIDs = array_merge($this->similaresIDs, $this->Inmueble->find('list', array('fields' => 'Inmueble.id', 'conditions' => $conditions, 'limit' => ($this->numSimilares - count($this->similaresIDs)), 'order' => 'RAND()')));

            if ($this->numSimilares > count($this->similaresIDs) && !empty($conditionsFields)) {
                //Quitamos condiciones a medida que vamos avanzando para encontrar inmuebles similares
                end($conditionsFields);
                $key = key($conditionsFields);
                $field = 'Inmueble.' . $conditionsFields[$key];

                unset($conditions[$field]);
                unset($conditionsFields[(int) $key]);
                $conditions['NOT']['Inmueble.id'] = array_merge(array_values($this->similaresIDs), array((int) $inmueble['Inmueble']['id']));

                $this->getSimilaresOtros($inmueble, $conditions, $conditionsFields);
            }
        }
    }

}
© 2026 GrazzMean