0
votes

Hey guys im trying to create an advance search form in cakephp,

Form consist in Dropdowns, Checkboxes (HABTM relations), Textfield, and price ranger (slider), You can see the form below:

enter image description here

As you can see i have multiple dropdown , multiple checkbox etc.

The search works great but what i want is how can i save these changes or filters on url for example after the user has selected a few filters as follow:

enter image description here

I want the url to be as follow : http://localhost/website/cakephpapplication/search?location=something&room_type=sth&accommodation_type=something&.... and so one

The Action that i use is this one:

////////////////////////////////////////////////////////////////////////////////////    
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function search() {
        $this->layout = 'frontend_search';
        $default_lang_code = Configure::read('Config.language');
        if($this->Session->check('Config.language')) { // Check for existing language session
            $language_code = $this->Session->read('Config.language'); // Read existing language
            $languageData  = $this->Language->find('first', array('conditions' => array('Language.language_code'=>$language_code),'recursive' => -1,'fields' => array('Language.id'),'limit' => 1));
            if (!empty($languageData)) {
                $language_id = $languageData['Language']['id'];
            } else {
                $language_id =  $this->Language->findLanguageIdByCode($default_lang_code);
            }
        } else {
            $language_code = $default_lang_code; // Read existing language
            $languageData = $this->Language->find('first', array('conditions' => array('Language.language_code'=>$default_lang_code),'recursive' => -1,'fields' => array('Language.id'),'limit' => 1));
            $language_id = $languageData['Language']['id'];
        }

        $localeCurrency = 'EUR';        
        if ($this->Session->check('LocaleCurrency')) {
            $localeCurrency = $this->Session->read('LocaleCurrency');
        } 

        $currencies  = $this->Currency->find('all', array( 'recursive'=>-1));

        if ($this->request->is('post')) {
            if(!empty($this->request->data)){
                /*THIS IS THE MOS REQUESTED PROPERTIES */
                $searchQuery  =" SELECT  Property.* , PropertyTranslation.*, Wishlist.*, Currency.*";
                $searchQuery .=" FROM properties as Property";
                $searchQuery .=" JOIN property_translations as PropertyTranslation";
                $searchQuery .=" ON Property.id = PropertyTranslation.property_id";
                $searchQuery .=" JOIN users as User";
                $searchQuery .=" ON User.id = Property.user_id";

                $searchQuery .=" LEFT JOIN wishlists as Wishlist";
                $searchQuery .=" ON Wishlist.property_id = Property.id ";
                $searchQuery .=" LEFT JOIN currencies as Currency";
                $searchQuery .=" On Currency.id = Property.currency_id";

                if (isset($this->request->data['Safety']) && !empty($this->request->data['Safety']) && $this->request->data['Safety']!='') {
                    $searchQuery .=" JOIN  properties_safeties as PropertiesSafety";
                    $searchQuery .=" ON Property.id = PropertiesSafety.property_id";
                    foreach ($this->request->data['Safety'] as $key => $value) {
                        $searchQuery .= " AND PropertiesSafety.safety_id = '".$value."'";
                    }
                }

                if (isset($this->request->data['Extra']) && !empty($this->request->data['Extra']) && $this->request->data['Extra']!='') {
                    $searchQuery .=" JOIN  extras_properties as ExtrasProperty";
                    $searchQuery .=" ON Property.id = ExtrasProperty.property_id";
                    foreach ($this->request->data['Extra'] as $key => $value) {
                        $searchQuery .= " AND ExtrasProperty.extra_id = '".$value."'";
                    }
                }

                if (isset($this->request->data['Service']) && !empty($this->request->data['Service']) && $this->request->data['Service']!='') {
                    $searchQuery .=" JOIN  properties_services as PropertiesService";
                    $searchQuery .=" ON Property.id = PropertiesService.property_id";
                    foreach ($this->request->data['Service'] as $key => $value) {
                        $searchQuery .= " AND PropertiesService.service_id = '".$value."'";
                    }
                }

                if (isset($this->request->data['Characteristic']) && !empty($this->request->data['Characteristic']) && $this->request->data['Characteristic']!='') {
                    $searchQuery .=" JOIN  characteristics_properties as CharacteristicsProperty";
                    $searchQuery .=" ON Property.id = CharacteristicsProperty.property_id";
                    foreach ($this->request->data['Characteristic'] as $key => $value) {
                        $searchQuery .= " AND CharacteristicsProperty.characteristic_id = '".$value."'";
                    }
                }

                $searchQuery .=" WHERE Property.property_status=1 AND Property.publish_status=1 AND User.is_first_post=0  AND PropertyTranslation.language_id='".$language_id."' ";
                if (isset($this->request->data['Property']['capacity']) && !empty($this->request->data['Property']['capacity']) && $this->request->data['Property']['capacity']!='') {
                      $searchQuery .= " AND Property.capacity ='".$this->request->data['Property']['capacity']."'";
                }

                if (isset($this->request->data['Property']['address']) && !empty($this->request->data['Property']['address']) && trim($this->request->data['Property']['address'])!='') {
                    $searchQuery .= " AND Property.address  LIKE '%".$this->request->data['Property']['address']."%' ";
                }

                if (isset($this->request->data['Property']['capacity']) && !empty($this->request->data['Property']['capacity']) && $this->request->data['Property']['capacity']!='') {
                    $searchQuery .= " AND Property.capacity ='".$this->request->data['Property']['capacity']."'";
                }

                if (isset($this->request->data['Property']['room_number']) && !empty($this->request->data['Property']['room_number']) && $this->request->data['Property']['room_number']!='') {
                    $searchQuery .= " AND Property.room_number ='".$this->request->data['Property']['room_number']."'";
                }

                if (isset($this->request->data['Property']['bed_number']) && !empty($this->request->data['Property']['bed_number']) && $this->request->data['Property']['bed_number']!='') {
                    $searchQuery .= " AND Property.bed_number ='".$this->request->data['Property']['bed_number']."'";
                }

                if (isset($this->request->data['Property']['bathroom_number']) && !empty($this->request->data['Property']['bathroom_number']) && $this->request->data['Property']['bathroom_number']!='') {
                    $searchQuery .= " AND Property.bathroom_number ='".$this->request->data['Property']['bathroom_number']."'";
                }

                if (isset($this->request->data['Property']['property_type'])!='' && !empty($this->request->data['Property']['property_type']) && trim($this->request->data['Property']['property_type'])!='') {
                    $searchQuery .= " AND Property.property_type  LIKE '%".$this->request->data['Property']['property_type']."%'";
                }

                if (isset($this->request->data['Property']['property_type'])!='' && !empty($this->request->data['Property']['property_type']) && $this->request->data['Property']['property_type']=='sale') {

                    if (isset($this->request->data['Property']['surface_area'])!='' && !empty($this->request->data['Property']['surface_area'])) {
                        $searchQuery .= " AND Property.surface_area  = '".$this->request->data['Property']['surface_area']."'";
                    }

                    if (isset($this->request->data['Property']['sale_price'])!='' && !empty($this->request->data['Property']['sale_price'])) {
                        $searchQuery .= " AND Property.sale_price  = '".$this->request->data['Property']['sale_price']."'";
                    }


                }

                if (isset($this->request->data['Property']['room_type_id']) && !empty($this->request->data['Property']['room_type_id']) && $this->request->data['Property']['room_type_id']!='') {
                    $searchQuery .= " AND Property.room_type_id = '".$this->request->data['Property']['room_type_id']."'";
                }

                if (isset($this->request->data['Property']['accommodation_type_id']) && !empty($this->request->data['Property']['accommodation_type_id']) && $this->request->data['Property']['accommodation_type_id']!='') {
                    $searchQuery .= " AND Property.accommodation_type_id = '".$this->request->data['Property']['accommodation_type_id']."'";
                }

                if (isset($this->request->data['Property']['price']) && !empty($this->request->data['Property']['price']) && $this->request->data['Property']['price']!='') {
                    $price = $this->request->data['Property']['price'];
                    list($minPrice, $maxPrice) = explode(";", $price);
                    $searchQuery .= " AND  Property.rent_daily_price BETWEEN '".$minPrice."' AND '".$maxPrice."'";
                }

                $searchQuery .=" ORDER BY Property.id";
                $searchResults = $this->Property->query($searchQuery);
            }
        } else {
            $searchQuery  =" SELECT  Property.*, PropertyTranslation.*, Wishlist.*, Currency.* ";
            $searchQuery .=" FROM properties as Property";
            $searchQuery .=" JOIN property_translations as PropertyTranslation";
            $searchQuery .=" ON Property.id = PropertyTranslation.property_id";
            $searchQuery .=" JOIN users as User";
            $searchQuery .=" ON User.id = Property.user_id";

            $searchQuery .=" LEFT JOIN wishlists as Wishlist";
            $searchQuery .=" ON Wishlist.property_id = Property.id ";

            $searchQuery .=" LEFT JOIN currencies as Currency";
            $searchQuery .=" On Currency.id = Property.currency_id";

            $searchQuery .=" WHERE Property.property_status=1 AND Property.publish_status=1 AND User.is_first_post=0  AND PropertyTranslation.language_id='".$language_id."'";
            $searchQuery .=" ORDER BY Property.id";
            $searchResults = $this->Property->query($searchQuery);
        }

        $this->set(compact('searchResults','roomTypes','accommodationTypes','safeties','extras','services','characteristics','currencies'));
    }
////////////////////////////////////////////////////////////////////////////////////    

I would really appreciate your help. Thanks in advance.

1

1 Answers

1
votes

The search works great but what i want is how can i save these changes or filters on url for example after the user has selected a few filters as follow:

You want to implement the PRG pattern that turns POST into GET.

Do it yourself, the wiki article explains the pattern, or use an existing plugin: