GithubHelp home page GithubHelp logo

atellier2 / location-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rainlab/location-plugin

0.0 0.0 0.0 186 KB

Adds location based features, such as Country and State.

License: MIT License

JavaScript 4.82% PHP 87.79% HTML 7.38%

location-plugin's Introduction

Location plugin

This plugin adds location based features to OctoberCMS.

  • Easily add Country and State to any model
  • Form widget for address lookups (Google API)

Extended features

To integrate locations with front-end users consider installing the User Plus+ plugin (RainLab.UserPlus).

Google API key requirement

As of June 22, 2016 the Google Maps service requires an API key. You may generate a key from the following link:

Copy the key and enter it in the Settings > Location settings area. If you find the address finder is not working, you may need to enable the Places API and the Maps JavaScript API.

Add Country and State to any model

This plugin provides an easy way to add location fields, country and state, to any model. Simply add these columns to the database table:

$table->integer('country_id')->unsigned()->nullable()->index();
$table->integer('state_id')->unsigned()->nullable()->index();

Then implement the RainLab.Location.Behaviors.LocationModel behavior in the model class:

public $implement = ['RainLab.Location.Behaviors.LocationModel'];

This will automatically create two "belongs to" relationships:

  1. state - relation for RainLab\Location\Models\State
  2. country - relation for RainLab\Location\Models\Country

Back-end usage

Forms

You are free to add the following form field definitions:

country:
    label: rainlab.location::lang.country.label
    type: dropdown
    span: left
    placeholder: rainlab.location::lang.country.select

state:
    label: rainlab.location::lang.state.label
    type: dropdown
    span: right
    dependsOn: country
    placeholder: rainlab.location::lang.state.select

Lists

For the list column definitions, you can use the following snippet:

 country:
     label: rainlab.location::lang.country.label
     searchable: true
     relation: country
     select: name
     sortable: false

 state:
     label: rainlab.location::lang.state.label
     searchable: true
     relation: state
     select: name
     sortable: false

Front-end usage

The front-end can also use the relationships by creating a partial called country-state with the content:

{% set countryId = countryId|default(form_value('country_id')) %}
{% set stateId = stateId|default(form_value('state_id')) %}

<div class="form-group">
    <label for="accountCountry">Country</label>
    {{ form_select_country('country_id', countryId, {
        id: 'accountCountry',
        class: 'form-control',
        emptyOption: '',
        'data-request': 'onInit',
        'data-request-update': {
            'country-state': '#partialCountryState'
        }
    }) }}
</div>

<div class="form-group">
    <label for="accountState">State</label>
    {{ form_select_state('state_id', countryId, stateId, {
        id: 'accountState',
        class: 'form-control',
        emptyOption: ''
    }) }}
</div>

This partial can be rendered in a form with the following:

<div id="partialCountryState">
    {% partial 'country-state' countryId=user.country_id stateId=user.state_id %}
</div>

Short code accessors

The behavior will also add a special short code accessor and setter to the model that converts country_code and state_code to their respective identifiers.

// Softly looks up and sets the country_id and state_id
// for these Country and State relations.

$model->country_code = "US";
$model->state_code = "FL";
$model->save();

Address Finder Form Widget

This plugin introduces an address lookup form field called addressfinder. The form widget renders a Google Maps autocomplete address field that automatically populates mapped fields based on the value entered and selected in the address.

Available mappings:

  • street
  • city
  • zip
  • state
  • country
  • country-long
  • latitude
  • longitude
  • vicinity

Available options:

You can restrict the address lookup to certain countries by defining the countryRestriction option. The option accepts a comma separated list of ISO 3166-1 ALPHA-2 compatible country codes (see: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).

By default the street mapper places the house number before the street name. However in some countries the number is commonly placed after the street name. You can reverse the order by using the reverseStreetNumber: true option.

Usage:

# ===================================
#  Form Field Definitions
# ===================================

fields:
    address:
        label: Address
        type: addressfinder
        countryRestriction: 'us,ch'
        reverseStreetNumber: false
        fieldMap:
            latitude: latitude
            longitude: longitude
            city: city
            zip: zip
            street: street
            country: country_code
            state: state_code
            vicinity: vicinity

    city:
        label: City
    zip:
        label: Zip
    street:
        label: Street
    country_code:
        label: Country
    state_code:
        label: State
    latitude:
        label: Latitude
    longitude:
        label: Longitude
    vicinity:
        label: Vicinity

location-plugin's People

Contributors

daftspunk avatar luketowers avatar inalto avatar acasar avatar octoberapp avatar samgeorges avatar gergo85 avatar vojtasvoboda avatar eoler avatar strangerpings avatar samuell1 avatar ribsousa avatar oscarnevarezleal avatar lucaszdv avatar gabsource avatar alxy avatar voveson avatar webvpf avatar ah94med avatar ametad avatar atellier2 avatar boyfromhell avatar priitperna avatar grafxflow avatar leocantthinkofaname avatar targetblank avatar vosco88 avatar xyz1123581321 avatar zhiweiwu0425 avatar theservat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.