jeudi 18 février 2016

Create checkbox filter in php/Joomla

First of all, I must tell you I have very little experience with this. The problem I have is that I'm trying to recode a Joomla generated website into showing checkboxes as filters. Right now I'm using a multiselect list that works but I need to have checkboxes (so that users don't need to hold Shift or Ctrl when selecting multiple values). There are two filters (one for state and the other for the city) and the filter options for cities are generated based on the selections in the filter for states. I can't figure out how to make everything into checkboxes in a way that will continue to generate "city checkboxes" based on the checked boxes in the "State" area...

public static function makeStateList($req_country_id,$req_state_id,$name,$onChange,$firstOption,$style,$class="input-medium"){
        global $configClass,$languages;
        $db = JFactory::getDbo();
        $stateArr = array();
        $show_available_states_cities = $configClass['show_available_states_cities'];

        $lgs = OSPHelper::getLanguages();
        $translatable = JLanguageMultilang::isEnabled() && count($lgs);
        $suffix = "";
        if($translatable){
            $suffix = OSPHelper::getFieldSuffix();
        }

        if((!HelperOspropertyCommon::checkCountry()) or ($req_country_id > 0)){

            $query  = "Select id as value,state_name".$suffix." as text from #__osrs_states where published = 1 ";
            if($req_country_id > 0){
                $query .= " and country_id = '$req_country_id'";
            }else{
                $query .= " and country_id = '".$configClass['show_country_id']."'";
            }
            if($show_available_states_cities == 1){
                $query .= " and id in (Select state from #__osrs_properties where approved = '1' and published = '1')";
            }
            $query .= " order by state_name";
            $db->setQuery($query);
            $states = $db->loadObjectList();
            if($firstOption != ""){
                $stateArr[] = JHTML::_('select.option','',$firstOption);
                $stateArr   = array_merge($stateArr,$states);
            }else{
                $stateArr   = $states;
            }



        return JHTML::_('select.genericlist',$stateArr,$name,'class="'.$class.'" multiple="multiple" '.$onChange.' '.$style,'value','text',$req_state_id);


        }else{
            $stateArr[] = JHTML::_('select.option','',$firstOption);
            return JHTML::_('select.genericlist',$stateArr,$name,'class="'.$class.'" disabled','value','text');
        }
    }


    /**
     * Load City
     *
     * @param unknown_type $option
     * @param unknown_type $state_id
     * @param unknown_type $city_id
     * @return unknown
     */
    public static function loadCity($option,$state_id,$city_id,$class="input-medium"){
        global $mainframe,$configClass,$languages;
        $db = JFactory::getDBO();

        $lgs = OSPHelper::getLanguages();
        $translatable = JLanguageMultilang::isEnabled() && count($lgs);
        $suffix = "";
        if($translatable){
            $suffix = OSPHelper::getFieldSuffix();
        }

        $availSql = "";
        $show_available_states_cities = $configClass['show_available_states_cities'];
        $cityArr = array();
        $cityArr[]= JHTML::_('select.option','',JText::_('OS_ALL_CITIES'));
        if($state_id > 0){
            if($show_available_states_cities == 1){
                $availSql = " and id in (Select city from #__osrs_properties where approved = '1' and published = '1')";
            }
            $db->setQuery("Select id as value, city".$suffix." as text from #__osrs_cities where  published = '1' $availSql and state_id = '$state_id' order by city");

            $cities = $db->loadObjectList();
            $cityArr   = array_merge($cityArr,$cities);
            $disabled  = "";
        }else{
            $disabled  = "disabled";
        }
        return JHTML::_('select.genericlist',$cityArr,'city','class="'.$class.'" '.$disabled,'value','text',$city_id);
    }

Sorry for the long post. I hope this makes sense... Thank you for all your help!




Aucun commentaire:

Enregistrer un commentaire