Connections-Business-Directory / Connections

Directory Plugin for WordPress
https://connections-pro.com
GNU General Public License v2.0
27 stars 18 forks source link

add country/state dropdown with text input fallback #333

Open quantumJLBass opened 10 years ago

quantumJLBass commented 10 years ago

I don't know if you looked at the version of the form plugin I worked on , but I had to add a country and state dropdown, but realized it was not going to cover the admin form. Give al the changes and where you're heading I thought that if the country doesn't have a corresponding state/region array then it fallback to the text input could be added at the same time. The model I started out with is like

<?php
if (!defined('ABSPATH'))
    exit;
class cnDefaultValues {

    public static function getRegions($country="US"){
        $region_list=array();
        switch($country){
            case "US":
                $region_list = array('AL'=>"Alabama",  
                    'AK'=>"Alaska",  
                    // all of the others
                    );
                    break;
        }
        return $region_list;    
    }

    public static function getCountries(){
        $countries = array(
            "US" => array( "name" => "United States", "d_code" => "+1" ),
            // all of the others
        );
        return $countries;  
    }
    /*
    * @returns array|false
    */
    public static function getCountryByCode($code="fail"){
        $countries = cnDefaultValues::getCountries();
        $country = isset($countries[strtoupper($code)])?$countries[strtoupper($code)]:false;
        return $country;
    }

    public static function getCountriesCodeToName(){
        $countries = cnDefaultValues::getCountries();
        $list=array();
        foreach($countries as $code=>$country){
            $list[$code] = $country['name'];
        }
        return $list;
    }
    public static function getCountryCodes(){
        $keys = array_keys(cnDefaultValues::getCountries());
        return $keys;
    }
}
?>

and then just out put. I'm know there are 50 ways to skin a cat on this, but I know I need to not allow variance in the states values set per the client terms. any thoughts?

shazahm1 commented 10 years ago

Yes, I did see that in your version of Form ... I'm absolutely in favor of this.

Would you want to tackle this?

You could save some time and grab some of the code from here: https://github.com/easydigitaldownloads/Easy-Digital-Downloads/blob/master/includes/country-functions.php (don't use states in function names or variables; they're regions. just like you have)

I'd like the static functions dropped in the class.options.php file.

The countries and regions functions should have filters, like the edd functions so they can be extended.

I want to use typeahead for the input fields. See the countries example here: http://twitter.github.io/typeahead.js/examples/ related #151

For static drop downs, the functions in cnHTML should be sufficient.

quantumJLBass commented 10 years ago

That sounds fine, help to do it. The only thought is that with that type head is that you can end up with "united states", "United States", "USA", "US", "usa", "us". It looks like there can be some work to test and reformate but it'll be some work. Any thoughts on that part?

shazahm1 commented 10 years ago

There's an option typeahead to autoselect first match, effectively forcing user selection rather than allowing free text input.

Is that what you're asking?

shazahm1 commented 10 years ago

Oh, another option is to use the select function from cnHTML and set it to be an enhanced drop down. It'll use the Chosen library ... the user has to choose from the list, there is no free text input.

quantumJLBass commented 10 years ago

hmm.. the key I think is that if there is a defined list that it is a select from but if there is not then they can enter it? so new regions can be added as there is time to?

Chosen lib is good but it's be odd since you can only choose one, but the typeahead to autoselect I didn't find that so I'll look at that. I'm use to using the jQuery UI autocomplete for dropdowns just because it's in the core and I can make it do most anything.

I'll return on that here right after I finish up on the lay out of the cbn site I've been working on with this. trying to get back to the card part of this.

shazahm1 commented 10 years ago

Oh, I forgot about jQuery UI autocomplete. Its probably best to use that since it is in core.

quantumJLBass commented 10 years ago

can do.. I'll come back to this next. thanks

shazahm1 commented 10 years ago

Maybe this function can be utilized rather than hard-coding countries and regions.

https://github.com/alixaxel/phunction/blob/master/phunction/Net.php#L58

quantumJLBass commented 10 years ago

Well I just finished the implementation, but I'm very leery of using a server for something like this. I work with a lot of ecom stuff, Magento being one of the major ones, and they all for the most part hard code because of the same reason. I'll do my pull and check it out and we can go from there? I personally rather not see a dependency of this nature since this whole plugin could be used from with in a network that can't curl out side it's self at this point and I think that is a good thing.

shazahm1 commented 10 years ago

Now that this is merged ... you going to add the JS to use the jQuery UI for autcomplete?

quantumJLBass commented 10 years ago

Yes, but just a thought here, I used that chosen plugin on the expanded search .. maybe that would work instead since it's al ready in use?

shazahm1 commented 10 years ago

I did mention Chosen, but you "seemed" against it??? https://github.com/Connections-Business-Directory/Connections/issues/333#issuecomment-37338026

quantumJLBass commented 10 years ago

lol yes that is right I missed it, but.. if it was replaced, then all should be replaced I would think. I'm more then happy to write out the jQuery UI replacements, but if you want to stick with the Chosen that's cool to.

shazahm1 commented 10 years ago

Question, since I'm not familiar ... will UI allow "new" input or is it only limited by what is populated like Chosen is?

I know you want consistent exact input ... but I can see others not wanting that so much so both of those scenarios would have to be supported.

quantumJLBass commented 10 years ago

Well the thought was that with the UI the autocomplete would be extened as needed. The jQuery ui can be made to do new input, or selected from drop down. Maybe for now till that part is set, the Chosen is used? I'll take a little time and make sure that the jQuery UI replacement will do all of what the Chosen does and cover what we talked about here too? just some other thoughts.