From b6da02a6b97c085bccd7dc40f251b34b49bb6151 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 28 Nov 2013 23:10:01 +0200 Subject: [PATCH] Add support for countries --- README.md | 3 +- app/config/app.php | 2 + app/controllers/AccountController.php | 12 +++-- app/controllers/ClientController.php | 18 ++++++-- ...013_11_28_195703_setup_countries_table.php | 45 +++++++++++++++++++ app/database/seeds/CountriesSeeder.php | 37 +++++++++++++++ app/database/seeds/DatabaseSeeder.php | 3 ++ app/models/Country.php | 6 +++ app/views/accounts/details.blade.php | 9 ++++ app/views/clients/edit.blade.php | 29 +++++++++--- composer.json | 3 +- 11 files changed, 151 insertions(+), 16 deletions(-) create mode 100755 app/database/migrations/2013_11_28_195703_setup_countries_table.php create mode 100755 app/database/seeds/CountriesSeeder.php create mode 100755 app/models/Country.php diff --git a/README.md b/README.md index 4cac85ef50ca..bc77877bfe2c 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,5 @@ Configure config/database.php and then initialize the database * [DataTables/DataTables](https://github.com/DataTables/DataTables) - Tables plug-in for jQuery * [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+ -* [Intervention/image](https://github.com/Intervention/image) - PHP Image Manipulation \ No newline at end of file +* [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 \ No newline at end of file diff --git a/app/config/app.php b/app/config/app.php index 75d92ca007f6..494293a5cf6b 100755 --- a/app/config/app.php +++ b/app/config/app.php @@ -120,6 +120,7 @@ return array( 'Barryvdh\Debugbar\ServiceProvider', 'Chumper\Datatable\DatatableServiceProvider', 'Intervention\Image\ImageServiceProvider', + 'Webpatser\Countries\CountriesServiceProvider', ), /* @@ -214,5 +215,6 @@ return array( 'Omnipay' => 'Omnipay\Omnipay', 'CreditCard' => 'Omnipay\Common\CreditCard', 'Image' => 'Intervention\Image\Facades\Image', + 'Countries' => 'Webpatser\Countries\CountriesFacade', ), ); \ No newline at end of file diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index 30f5d9df8983..49e40531e048 100755 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -43,25 +43,28 @@ class AccountController extends \BaseController { public function showSection($section = ACCOUNT_DETAILS) { if ($section == ACCOUNT_DETAILS) - { + { $account = Account::with('users')->find(Auth::user()->account_id); - - return View::make('accounts.details', array('account' => $account)); + $countries = Country::orderBy('name')->get(); + + return View::make('accounts.details', array('account'=>$account, 'countries'=>$countries)); } else if ($section == ACCOUNT_SETTINGS) { $account = Account::with('account_gateways')->find(Auth::user()->account_id); $accountGateway = null; + $config = null; if (count($account->account_gateways) > 0) { $accountGateway = $account->account_gateways[0]; + $config = $accountGateway->config; } $data = [ 'account' => $account, 'accountGateway' => $accountGateway, - 'config' => json_decode($accountGateway->config), + 'config' => json_decode($config), 'gateways' => Gateway::all() ]; @@ -405,6 +408,7 @@ class AccountController extends \BaseController { $account->city = Input::get('city'); $account->state = Input::get('state'); $account->postal_code = Input::get('postal_code'); + $account->country_id = Input::get('country_id'); $account->save(); $user = $account->users()->first(); diff --git a/app/controllers/ClientController.php b/app/controllers/ClientController.php index 8b5083d7115b..655b9223cef7 100755 --- a/app/controllers/ClientController.php +++ b/app/controllers/ClientController.php @@ -51,7 +51,13 @@ class ClientController extends \BaseController { */ 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); } @@ -86,7 +92,12 @@ class ClientController extends \BaseController { public function edit($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); } @@ -127,6 +138,7 @@ class ClientController extends \BaseController { $client->state = Input::get('state'); $client->notes = Input::get('notes'); $client->postal_code = Input::get('postal_code'); + $client->country_id = Input::get('country_id'); $client->save(); $data = json_decode(Input::get('data')); @@ -161,7 +173,7 @@ class ClientController extends \BaseController { } Session::flash('message', 'Successfully updated client'); - return Redirect::to('clients'); + return Redirect::to('clients/' . $client->id); } } diff --git a/app/database/migrations/2013_11_28_195703_setup_countries_table.php b/app/database/migrations/2013_11_28_195703_setup_countries_table.php new file mode 100755 index 000000000000..37d35f884d3f --- /dev/null +++ b/app/database/migrations/2013_11_28_195703_setup_countries_table.php @@ -0,0 +1,45 @@ +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'); + } + +} diff --git a/app/database/seeds/CountriesSeeder.php b/app/database/seeds/CountriesSeeder.php new file mode 100755 index 000000000000..7fe9c9e3fb05 --- /dev/null +++ b/app/database/seeds/CountriesSeeder.php @@ -0,0 +1,37 @@ +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'] + )); + } + } +} \ No newline at end of file diff --git a/app/database/seeds/DatabaseSeeder.php b/app/database/seeds/DatabaseSeeder.php index f92251cb2daf..4d194ed466c0 100755 --- a/app/database/seeds/DatabaseSeeder.php +++ b/app/database/seeds/DatabaseSeeder.php @@ -15,6 +15,9 @@ class DatabaseSeeder extends Seeder { $this->call('UserTableSeeder'); $this->call('ConstantsSeeder'); + + $this->call('CountriesSeeder'); + $this->command->info('Seeded the countries!'); } } \ No newline at end of file diff --git a/app/models/Country.php b/app/models/Country.php new file mode 100755 index 000000000000..bfd714a7261f --- /dev/null +++ b/app/models/Country.php @@ -0,0 +1,6 @@ +addOption('','')->label('Country') + ->fromQuery($countries, 'name', 'id')->select($account ? $account->country_id : '') }} {{ Former::actions( Button::lg_primary_submit('Save') ) }} {{ Former::close() }} + @stop \ No newline at end of file diff --git a/app/views/clients/edit.blade.php b/app/views/clients/edit.blade.php index 5a98d5f86e28..71f862a9de21 100755 --- a/app/views/clients/edit.blade.php +++ b/app/views/clients/edit.blade.php @@ -26,7 +26,9 @@ {{ Former::textarea('notes') }} {{ Former::legend('Contacts') }} -
+
{{ Former::hidden('id')->data_bind("value: id, valueUpdate: 'afterkeydown'") }} {{ Former::text('first_name')->data_bind("value: first_name, valueUpdate: 'afterkeydown'") }} {{ Former::text('last_name')->data_bind("value: last_name, valueUpdate: 'afterkeydown'") }} @@ -35,12 +37,12 @@
- - {{ link_to('#', 'Add contact', array('onclick'=>'return addContact()')) }} - - + {{ link_to('#', 'Remove contact', array('data-bind'=>'click: $parent.removeContact')) }} + + {{ link_to('#', 'Add contact', array('onclick'=>'return addContact()')) }} +
@@ -48,11 +50,14 @@
{{ Former::legend('Address') }} - {{ Former::text('address1') }} - {{ Former::text('address2') }} + {{ Former::text('address1')->label('Street') }} + {{ Former::text('address2')->label('Apt/Floor') }} {{ Former::text('city') }} {{ Former::text('state') }} {{ 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)") }} @@ -61,6 +66,10 @@ diff --git a/composer.json b/composer.json index 7e2b3c581238..3716c0e4f334 100755 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "barryvdh/laravel-debugbar": "dev-master", "chumper/datatable": "dev-master", "omnipay/omnipay": "2.x", - "intervention/image": "dev-master" + "intervention/image": "dev-master", + "webpatser/laravel-countries": "dev-master" }, "autoload": { "classmap": [