Esempio n. 1
0
 private function query($instance)
 {
     /** property listing model **/
     $model = new wpl_property();
     $data = $instance['data'];
     $this->start = 0;
     $this->limit = $data['limit'];
     $this->orderby = urldecode($data['orderby']);
     $this->order = $data['order'];
     /** detect kind **/
     if (isset($data['kind']) and (trim($data['kind']) != '' or trim($data['kind']) != '-1')) {
         $kind = $data['kind'];
     } else {
         $kind = 0;
     }
     $where = array('sf_select_confirmed' => 1, 'sf_select_finalized' => 1, 'sf_select_deleted' => 0, 'sf_select_expired' => 0, 'sf_select_kind' => $kind);
     if (trim($data['listing']) and $data['listing'] != '-1') {
         $where['sf_select_listing'] = $data['listing'];
     }
     if (trim($data['property_type']) and $data['property_type'] != '-1') {
         $where['sf_select_property_type'] = $data['property_type'];
     }
     if (trim($data['property_ids'])) {
         $where['sf_multiple_id'] = trim($data['property_ids'], ', ');
     }
     if (trim($data['only_featured'])) {
         $where['sf_select_sp_featured'] = 1;
     }
     if (trim($data['only_hot'])) {
         $where['sf_select_sp_hot'] = 1;
     }
     if (trim($data['only_openhouse'])) {
         $where['sf_select_sp_openhouse'] = 1;
     }
     if (trim($data['only_forclosure'])) {
         $where['sf_select_sp_forclosure'] = 1;
     }
     /** Parent **/
     if (isset($data['parent']) and trim($data['parent'])) {
         $where['sf_parent'] = $data['parent'];
     }
     if (isset($data['auto_parent']) and trim($data['auto_parent'])) {
         /** current proeprty id - This features works only in single property page **/
         $property_data = NULL;
         $pid = wpl_request::getVar('pid', 0);
         if ($pid) {
             $property_data = $model->get_property_raw_data($pid);
         }
         if (isset($property_data['mls_id'])) {
             $where['sf_parent'] = $property_data['mls_id'];
         }
     }
     if (isset($data['random']) and trim($data['random']) and trim($data['property_ids']) == '') {
         $query_rand = "SELECT p.`id` FROM `#__wpl_properties` AS p WHERE 1 " . wpl_db::create_query($where) . " ORDER BY RAND() LIMIT " . $this->limit;
         $results = wpl_db::select($query_rand);
         $rand_ids = array();
         foreach ($results as $result) {
             $rand_ids[] = $result->id;
         }
         $where['sf_multiple_id'] = implode(',', $rand_ids);
     }
     /** Similar properties **/
     if (isset($data['sml_only_similars']) and $data['sml_only_similars']) {
         $sml_where = array('sf_select_confirmed' => 1, 'sf_select_finalized' => 1, 'sf_select_deleted' => 0, 'sf_select_expired' => 0);
         /** current proeprty id - This features works only in single property page **/
         $pid = wpl_request::getVar('pid', 0);
         $property_data = wpl_property::get_property_raw_data($pid);
         if ($property_data) {
             $sml_where['sf_notselect_id'] = $pid;
             $sml_where['sf_select_kind'] = $property_data['kind'];
             if (isset($data['sml_inc_listing']) and $data['sml_inc_listing']) {
                 $sml_where['sf_select_listing'] = $property_data['listing'];
             }
             if (isset($data['sml_inc_property_type']) and $data['sml_inc_property_type']) {
                 $sml_where['sf_select_property_type'] = $property_data['property_type'];
             }
             if (isset($data['sml_inc_price']) and $data['sml_inc_price']) {
                 $down_rate = $data['sml_price_down_rate'] ? $data['sml_price_down_rate'] : 0.8;
                 $up_rate = $data['sml_price_up_rate'] ? $data['sml_price_up_rate'] : 1.2;
                 $price_down_range = $property_data['price_si'] * $down_rate;
                 $price_up_range = $property_data['price_si'] * $up_rate;
                 $sml_where['sf_tmin_price_si'] = $price_down_range;
                 $sml_where['sf_tmax_price_si'] = $price_up_range;
             }
             if (isset($data['sml_inc_radius']) and $data['sml_inc_radius']) {
                 $latitude = $property_data['googlemap_lt'];
                 $longitude = $property_data['googlemap_ln'];
                 $radius = $data['sml_radius'];
                 $unit_id = $data['sml_radius_unit'];
                 if ($latitude and $longitude and $radius and $unit_id) {
                     $sml_where['sf_radiussearchunit'] = $unit_id;
                     $sml_where['sf_radiussearch_lat'] = $latitude;
                     $sml_where['sf_radiussearch_lng'] = $longitude;
                     $sml_where['sf_radiussearchradius'] = $radius;
                 }
             }
         }
         /** overwrite $where if similar where is correct **/
         if (count($sml_where) > 3) {
             $where = $sml_where;
         }
     }
     /** start search **/
     $model->start($this->start, $this->limit, $this->orderby, $this->order, $where);
     /** run the search **/
     return $model->query();
 }