Add support for countries

This commit is contained in:
Hillel Coren 2013-11-28 23:10:01 +02:00
parent f99dde1790
commit b6da02a6b9
11 changed files with 151 additions and 16 deletions

View File

@ -54,3 +54,4 @@ Configure config/database.php and then initialize the database
* [Chumper/Datatable](https://github.com/Chumper/Datatable) - This is a laravel 4 package for the server and client side of datatables * [Chumper/Datatable](https://github.com/Chumper/Datatable) - This is a laravel 4 package for the server and client side of datatables
* [omnipay/omnipay](https://github.com/omnipay/omnipay) - A framework agnostic, multi-gateway payment processing library for PHP 5.3+ * [omnipay/omnipay](https://github.com/omnipay/omnipay) - A framework agnostic, multi-gateway payment processing library for PHP 5.3+
* [Intervention/image](https://github.com/Intervention/image) - PHP Image Manipulation * [Intervention/image](https://github.com/Intervention/image) - PHP Image Manipulation
* [webpatser/laravel-countries](https://github.com/webpatser/laravel-countries) - Almost ISO 3166_2, 3166_3, currency, Capital and more for all countries

View File

@ -120,6 +120,7 @@ return array(
'Barryvdh\Debugbar\ServiceProvider', 'Barryvdh\Debugbar\ServiceProvider',
'Chumper\Datatable\DatatableServiceProvider', 'Chumper\Datatable\DatatableServiceProvider',
'Intervention\Image\ImageServiceProvider', 'Intervention\Image\ImageServiceProvider',
'Webpatser\Countries\CountriesServiceProvider',
), ),
/* /*
@ -214,5 +215,6 @@ return array(
'Omnipay' => 'Omnipay\Omnipay', 'Omnipay' => 'Omnipay\Omnipay',
'CreditCard' => 'Omnipay\Common\CreditCard', 'CreditCard' => 'Omnipay\Common\CreditCard',
'Image' => 'Intervention\Image\Facades\Image', 'Image' => 'Intervention\Image\Facades\Image',
'Countries' => 'Webpatser\Countries\CountriesFacade',
), ),
); );

View File

@ -45,23 +45,26 @@ class AccountController extends \BaseController {
if ($section == ACCOUNT_DETAILS) if ($section == ACCOUNT_DETAILS)
{ {
$account = Account::with('users')->find(Auth::user()->account_id); $account = Account::with('users')->find(Auth::user()->account_id);
$countries = Country::orderBy('name')->get();
return View::make('accounts.details', array('account' => $account)); return View::make('accounts.details', array('account'=>$account, 'countries'=>$countries));
} }
else if ($section == ACCOUNT_SETTINGS) else if ($section == ACCOUNT_SETTINGS)
{ {
$account = Account::with('account_gateways')->find(Auth::user()->account_id); $account = Account::with('account_gateways')->find(Auth::user()->account_id);
$accountGateway = null; $accountGateway = null;
$config = null;
if (count($account->account_gateways) > 0) if (count($account->account_gateways) > 0)
{ {
$accountGateway = $account->account_gateways[0]; $accountGateway = $account->account_gateways[0];
$config = $accountGateway->config;
} }
$data = [ $data = [
'account' => $account, 'account' => $account,
'accountGateway' => $accountGateway, 'accountGateway' => $accountGateway,
'config' => json_decode($accountGateway->config), 'config' => json_decode($config),
'gateways' => Gateway::all() 'gateways' => Gateway::all()
]; ];
@ -405,6 +408,7 @@ class AccountController extends \BaseController {
$account->city = Input::get('city'); $account->city = Input::get('city');
$account->state = Input::get('state'); $account->state = Input::get('state');
$account->postal_code = Input::get('postal_code'); $account->postal_code = Input::get('postal_code');
$account->country_id = Input::get('country_id');
$account->save(); $account->save();
$user = $account->users()->first(); $user = $account->users()->first();

View File

@ -51,7 +51,13 @@ class ClientController extends \BaseController {
*/ */
public function create() public function create()
{ {
$data = array('client' => null, 'method' => 'POST', 'url' => 'clients', 'title' => 'New'); $data = array(
'client' => null,
'method' => 'POST',
'url' => 'clients',
'title' => 'New',
'countries' => Country::orderBy('name')->get());
return View::make('clients.edit', $data); return View::make('clients.edit', $data);
} }
@ -86,7 +92,12 @@ class ClientController extends \BaseController {
public function edit($id) public function edit($id)
{ {
$client = Client::with('contacts')->find($id); $client = Client::with('contacts')->find($id);
$data = array('client' => $client, 'method' => 'PUT', 'url' => 'clients/' . $id, 'title' => 'Edit'); $data = array(
'client' => $client,
'method' => 'PUT',
'url' => 'clients/' . $id,
'title' => 'Edit',
'countries' => Country::orderBy('name')->get());
return View::make('clients.edit', $data); return View::make('clients.edit', $data);
} }
@ -127,6 +138,7 @@ class ClientController extends \BaseController {
$client->state = Input::get('state'); $client->state = Input::get('state');
$client->notes = Input::get('notes'); $client->notes = Input::get('notes');
$client->postal_code = Input::get('postal_code'); $client->postal_code = Input::get('postal_code');
$client->country_id = Input::get('country_id');
$client->save(); $client->save();
$data = json_decode(Input::get('data')); $data = json_decode(Input::get('data'));
@ -161,7 +173,7 @@ class ClientController extends \BaseController {
} }
Session::flash('message', 'Successfully updated client'); Session::flash('message', 'Successfully updated client');
return Redirect::to('clients'); return Redirect::to('clients/' . $client->id);
} }
} }

View File

@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
class SetupCountriesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Creates the users table
Schema::create('countries', function($table)
{
$table->integer('id')->index();
$table->string('capital', 255)->nullable();
$table->string('citizenship', 255)->nullable();
$table->string('country_code', 3)->default('');
$table->string('currency', 255)->nullable();
$table->string('currency_code', 255)->nullable();
$table->string('currency_sub_unit', 255)->nullable();
$table->string('full_name', 255)->nullable();
$table->string('iso_3166_2', 2)->default('');
$table->string('iso_3166_3', 3)->default('');
$table->string('name', 255)->default('');
$table->string('region_code', 3)->default('');
$table->string('sub_region_code', 3)->default('');
$table->boolean('eea')->default(0);
$table->primary('id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('countries');
}
}

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Eloquent\Model as Eloquent;
class CountriesSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//Empty the countries table
DB::table('countries')->delete();
//Get all of the countries
$countries = Countries::getList();
foreach ($countries as $countryId => $country){
DB::table('countries')->insert(array(
'id' => $countryId,
'capital' => ((isset($country['capital'])) ? $country['capital'] : null),
'citizenship' => ((isset($country['citizenship'])) ? $country['citizenship'] : null),
'country_code' => $country['country-code'],
'currency' => ((isset($country['currency'])) ? $country['currency'] : null),
'currency_code' => ((isset($country['currency_code'])) ? $country['currency_code'] : null),
'currency_sub_unit' => ((isset($country['currency_sub_unit'])) ? $country['currency_sub_unit'] : null),
'full_name' => ((isset($country['full_name'])) ? $country['full_name'] : null),
'iso_3166_2' => $country['iso_3166_2'],
'iso_3166_3' => $country['iso_3166_3'],
'name' => $country['name'],
'region_code' => $country['region-code'],
'sub_region_code' => $country['sub-region-code'],
'eea' => (bool)$country['eea']
));
}
}
}

View File

@ -15,6 +15,9 @@ class DatabaseSeeder extends Seeder {
$this->call('UserTableSeeder'); $this->call('UserTableSeeder');
$this->call('ConstantsSeeder'); $this->call('ConstantsSeeder');
$this->call('CountriesSeeder');
$this->command->info('Seeded the countries!');
} }
} }

6
app/models/Country.php Executable file
View File

@ -0,0 +1,6 @@
<?php
class Country extends Eloquent
{
}

View File

@ -48,9 +48,18 @@
{{ Former::text('city') }} {{ Former::text('city') }}
{{ Former::text('state') }} {{ Former::text('state') }}
{{ Former::text('postal_code') }} {{ Former::text('postal_code') }}
{{ Former::select('country_id')->addOption('','')->label('Country')
->fromQuery($countries, 'name', 'id')->select($account ? $account->country_id : '') }}
{{ Former::actions( Button::lg_primary_submit('Save') ) }} {{ Former::actions( Button::lg_primary_submit('Save') ) }}
{{ Former::close() }} {{ Former::close() }}
<script type="text/javascript">
$(function() {
$('#country_id').combobox();
});
</script>
@stop @stop

View File

@ -26,7 +26,9 @@
{{ Former::textarea('notes') }} {{ Former::textarea('notes') }}
{{ Former::legend('Contacts') }} {{ Former::legend('Contacts') }}
<div data-bind="foreach: contacts"> <div data-bind='template: { foreach: contacts,
beforeRemove: hideContact,
afterAdd: showContact }'>
{{ Former::hidden('id')->data_bind("value: id, valueUpdate: 'afterkeydown'") }} {{ Former::hidden('id')->data_bind("value: id, valueUpdate: 'afterkeydown'") }}
{{ Former::text('first_name')->data_bind("value: first_name, valueUpdate: 'afterkeydown'") }} {{ Former::text('first_name')->data_bind("value: first_name, valueUpdate: 'afterkeydown'") }}
{{ Former::text('last_name')->data_bind("value: last_name, valueUpdate: 'afterkeydown'") }} {{ Former::text('last_name')->data_bind("value: last_name, valueUpdate: 'afterkeydown'") }}
@ -35,12 +37,12 @@
<div class="form-group"> <div class="form-group">
<div class="col-lg-8 col-lg-offset-4"> <div class="col-lg-8 col-lg-offset-4">
<span data-bind="visible: $index() === ($parent.contacts().length - 1)"> <span data-bind="visible: $parent.contacts().length > 1">
{{ link_to('#', 'Add contact', array('onclick'=>'return addContact()')) }}
</span>
<span data-bind="visible: $parent.contacts().length > 1" class="pull-right">
{{ link_to('#', 'Remove contact', array('data-bind'=>'click: $parent.removeContact')) }} {{ link_to('#', 'Remove contact', array('data-bind'=>'click: $parent.removeContact')) }}
</span> </span>
<span data-bind="visible: $index() === ($parent.contacts().length - 1)" class="pull-right">
{{ link_to('#', 'Add contact', array('onclick'=>'return addContact()')) }}
</span>
</div> </div>
</div> </div>
<div class="clearfix"></div> <div class="clearfix"></div>
@ -48,11 +50,14 @@
</div> </div>
{{ Former::legend('Address') }} {{ Former::legend('Address') }}
{{ Former::text('address1') }} {{ Former::text('address1')->label('Street') }}
{{ Former::text('address2') }} {{ Former::text('address2')->label('Apt/Floor') }}
{{ Former::text('city') }} {{ Former::text('city') }}
{{ Former::text('state') }} {{ Former::text('state') }}
{{ Former::text('postal_code') }} {{ Former::text('postal_code') }}
{{ Former::select('country_id')->addOption('','')->label('Country')
->fromQuery($countries, 'name', 'id')->select($client ? $client->country_id : '') }}
{{ Former::hidden('data')->data_bind("value: ko.toJSON(model)") }} {{ Former::hidden('data')->data_bind("value: ko.toJSON(model)") }}
@ -61,6 +66,10 @@
<script type="text/javascript"> <script type="text/javascript">
$(function() {
$('#country_id').combobox();
});
function ContactModel() { function ContactModel() {
var self = this; var self = this;
self.id = ko.observable(''); self.id = ko.observable('');
@ -81,6 +90,11 @@
window.model = new ContactsModel(); window.model = new ContactsModel();
addContact(); addContact();
@endif @endif
model.showContact = function(elem) { if (elem.nodeType === 1) $(elem).hide().slideDown() }
model.hideContact = function(elem) { if (elem.nodeType === 1) $(elem).slideUp(function() { $(elem).remove(); }) }
ko.applyBindings(model); ko.applyBindings(model);
function addContact() { function addContact() {
@ -92,6 +106,7 @@
model.contacts.remove(this); model.contacts.remove(this);
} }
</script> </script>
@stop @stop

View File

@ -12,7 +12,8 @@
"barryvdh/laravel-debugbar": "dev-master", "barryvdh/laravel-debugbar": "dev-master",
"chumper/datatable": "dev-master", "chumper/datatable": "dev-master",
"omnipay/omnipay": "2.x", "omnipay/omnipay": "2.x",
"intervention/image": "dev-master" "intervention/image": "dev-master",
"webpatser/laravel-countries": "dev-master"
}, },
"autoload": { "autoload": {
"classmap": [ "classmap": [