Skip to content

Commit

Permalink
feat: set currency and timezone for new users (monicahq#1657)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Oct 25, 2018
1 parent 647a519 commit b5880bb
Show file tree
Hide file tree
Showing 13 changed files with 969 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
UNRELEASED CHANGES:

* Set currency and timezone for new users
* Fix settings' sidebar links and change security icon
* Fix CSV import

Expand Down
157 changes: 152 additions & 5 deletions app/Helpers/CountriesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,28 @@ public static function getAll()
return CollectionHelper::sortByCollator($countries, 'country');
}

/**
* Get country name.
*
* @param string $iso code of the country
* @return string common name (localized) of the country
*/
public static function get($iso)
{
$country = Countries::where('cca2', mb_strtoupper($iso))->first();
if ($country->count() === 0) {
$country = Countries::where('alt_spellings', mb_strtoupper($iso))->first();
}
if ($country->count() === 0) {
$country = self::getCountry($iso);
if (is_null($country)) {
return '';
}

return static::getCommonNameLocale($country);
}

/**
* Find a country by the (english) name of the country.
*
* @param string $name Common name of a country
* @return string cca2 code of the country
*/
public static function find($name)
{
$country = Countries::where('name.common', $name)->first();
Expand All @@ -50,6 +59,12 @@ public static function find($name)
return $country->cca2;
}

/**
* Get the common name of country, in locale version.
*
* @param string $country
* @return string
*/
private static function getCommonNameLocale($country)
{
$locale = App::getLocale();
Expand All @@ -59,4 +74,136 @@ private static function getCommonNameLocale($country)
array_get($country, 'name.common', '')
);
}

/**
* Get country for a specific iso code.
*
* @param string $iso
* @return object the Country element
*/
public static function getCountry($iso)
{
$country = Countries::where('cca2', mb_strtoupper($iso))->first();
if ($country->count() === 0) {
$country = Countries::where('alt_spellings', mb_strtoupper($iso))->first();
}
if ($country->count() === 0) {
return;
}

return $country;
}

/**
* Get country for a specific language.
*
* @param string $locale language code (iso)
* @return object the Country element
*/
public static function getCountryFromLocale($locale)
{
$countryCode = self::getDefaultCountryFromLocale($locale);

if (is_null($countryCode)) {
$lang = LocaleHelper::getLocaleAlpha($locale);
$country = Countries::whereLanguage($lang);
if ($country->count() === 0) {
return;
}
} else {
$country = Countries::where('cca3', $countryCode);
}

return $country->first();
}

/**
* Get default country for a language.
*
* @param string $locale language code (iso)
* @return string cca3 code
*/
private static function getDefaultCountryFromLocale($locale)
{
switch (mb_strtolower($locale)) {
case 'cs':
$country = 'CZE';
break;
case 'de':
$country = 'DEU';
break;
case 'en':
$country = 'USA';
break;
case 'es':
$country = 'ESP';
break;
case 'fr':
$country = 'FRA';
break;
case 'he':
$country = 'ISR';
break;
case 'it':
$country = 'ITA';
break;
case 'nl':
$country = 'NLD';
break;
case 'pt':
$country = 'PRT';
break;
case 'ru':
$country = 'RUS';
break;
case 'zh':
$country = 'CHN';
break;
default:
$country = null;
break;
}

return $country;
}

/**
* Get default timezone for the country.
*
* @param mixed $country Country element
* @return string timezone fo this sountry
*/
public static function getDefaultTimezone($country)
{
// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
// https://en.wikipedia.org/wiki/List_of_time_zones_by_country
switch ($country->cca3) {
case 'AUS':
$timezone = 'Australia/Melbourne';
break;
case 'CHN':
$timezone = 'Asia/Shanghai';
break;
case 'ESP':
$timezone = 'Europe/Madrid';
break;
case 'PRT':
$timezone = 'Europe/Lisbon';
break;
case 'RUS':
$timezone = 'Europe/Moscow';
break;
case 'CAN':
$timezone = 'America/Toronto';
break;
case 'USA':
$timezone = 'America/Chicago';
break;
default:
$timezone = $country->hydrate('timezones')->timezones->first()->zone_name;
break;
}

return $timezone;
}
}
45 changes: 45 additions & 0 deletions app/Helpers/RequestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Helpers;

use Vectorface\Whip\Whip;
use OK\Ipstack\Client as Ipstack;
use Illuminate\Support\Facades\Request;
use Stevebauman\Location\Facades\Location;

class RequestHelper
{
Expand All @@ -25,4 +27,47 @@ public static function ip()

return $ip;
}

/**
* Get client country.
* @return string
*/
public static function country()
{
$position = Location::get();

if (! $position) {
return;
}

return $position->countryCode;
}

/**
* Get client country and currency.
*
* @param string $ip
* @return array
*/
public static function infos($ip)
{
if (config('location.ipstack_apikey') != null) {
$ipstack = new Ipstack(config('location.ipstack_apikey'));
$position = $ipstack->get($ip ?? static::ip(), true);

if (! is_null($position) && array_get($position, 'country_code', null)) {
return [
'country' => array_get($position, 'country_code', null),
'currency' => array_get($position, 'currency.code', null),
'timezone' => array_get($position, 'time_zone.id', null),
];
}
}

return [
'country' => static::country(),
'currency' => null,
'timezone' => null,
];
}
}
7 changes: 6 additions & 1 deletion app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Contact\Tag;
use Illuminate\Http\Request;
use App\Helpers\LocaleHelper;
use App\Helpers\RequestHelper;
use App\Jobs\SendNewUserAlert;
use App\Helpers\TimezoneHelper;
use App\Jobs\ExportAccountAsSQL;
Expand Down Expand Up @@ -408,7 +409,11 @@ public function storeAcceptedInvitation(Request $request, $key)
$request->input('first_name'),
$request->input('last_name'),
$request->input('email'),
$request->input('password'));
$request->input('password'),
RequestHelper::ip()
);
$user->invited_by_user_id = $invitation->invited_by_user_id;
$user->save();

$invitation->delete();

Expand Down
49 changes: 49 additions & 0 deletions app/Http/Location/Drivers/CloudflareDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Http\Location\Drivers;

use App\Helpers\RequestHelper;
use Illuminate\Support\Fluent;
use Stevebauman\Location\Position;
use Illuminate\Support\Facades\Request;
use Stevebauman\Location\Drivers\Driver;

class CloudflareDriver extends Driver
{
public function url()
{
}

protected function hydrate(Position $position, Fluent $location)
{
$position->countryCode = $location->country_code;

return $position;
}

protected function process($ip = null)
{
if (! is_null($ip)) {
return $this->fallback->get($ip);
}

try {
return $this->getCountry();
} catch (\Exception $e) {
return false;
}
}

private function getCountry()
{
$country = Request::header('Cf-Ipcountry');

if (! is_null($country)) {
$response = ['country_code' => $country];

return new Fluent($response);
}

return $this->fallback->get(RequestHelper::ip());
}
}
35 changes: 19 additions & 16 deletions app/Models/Account/ImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,13 @@ public function existingContact()
return;
}

$contactFieldType = ContactFieldType::where([
$contactField = ContactField::where([
['account_id', $this->account_id],
['type', 'email'],
])->first();
['contact_field_type_id', $this->contactFieldEmailId()],
])->whereIn('data', iterator_to_array($this->currentEntry->EMAIL))->first();

if ($contactFieldType) {
$contactField = ContactField::where([
['account_id', $this->account_id],
['contact_field_type_id', $contactFieldType->id],
])->whereIn('data', iterator_to_array($this->currentEntry->EMAIL))->first();

if ($contactField) {
return $contactField->contact;
}
if ($contactField) {
return $contactField->contact;
}
}

Expand Down Expand Up @@ -556,8 +549,13 @@ public function importTel(Contact $contact): void
private function contactFieldEmailId()
{
if (! $this->contactFieldEmailId) {
$contactFieldType = ContactFieldType::where('type', 'email')->first();
$this->contactFieldEmailId = $contactFieldType->id;
$contactFieldType = ContactFieldType::where([
['account_id', $this->account_id],
['type', 'email'],
])->first();
if ($contactFieldType) {
$this->contactFieldEmailId = $contactFieldType->id;
}
}

return $this->contactFieldEmailId;
Expand All @@ -566,8 +564,13 @@ private function contactFieldEmailId()
private function contactFieldPhoneId()
{
if (! $this->contactFieldPhoneId) {
$contactFieldType = ContactFieldType::where('type', 'phone')->first();
$this->contactFieldPhoneId = $contactFieldType->id;
$contactFieldType = ContactFieldType::where([
['account_id', $this->account_id],
['type', 'phone'],
])->first();
if ($contactFieldType) {
$this->contactFieldPhoneId = $contactFieldType->id;
}
}

return $this->contactFieldPhoneId;
Expand Down
Loading

0 comments on commit b5880bb

Please sign in to comment.