Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RES-1908 country codes #665

Merged
merged 9 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Console/Commands/CheckGroupLocations.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CheckGroupLocations extends Command
*
* @var string
*/
protected $description = 'Check that all gtroup locations are geocodeable';
protected $description = 'Check that all group locations are geocodeable';

/**
* Create a new command instance.
Expand Down
45 changes: 0 additions & 45 deletions app/Console/Commands/FixLatitudeLongitude.php

This file was deleted.

39 changes: 39 additions & 0 deletions app/Console/Commands/GroupCountryField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Console\Commands;

use App\Group;
use App\Helpers\Fixometer;
use Illuminate\Console\Command;

class GroupCountryField extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'groups:country';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Set the group country field from the country_code field';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$groups = Group::all();

foreach ($groups as $group) {
$group->country = Fixometer::getCountryFromCountryCode($group->country_code);
$group->save();
}
}
}
4 changes: 2 additions & 2 deletions app/Console/Commands/ImportGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function handle()
return iconv( "iso-8859-15", "UTF-8", $str );
}, $fields);

// Format is 'Name', 'Location', 'Postcode', 'Area', 'Country', 'Latitude', 'Longitude', 'Website', 'Phone', 'Networks', 'Description'.
// Format is 'Name', 'Location', 'Postcode', 'Area', 'CountryCode', 'Latitude', 'Longitude', 'Website', 'Phone', 'Networks', 'Description'.

$groupname = $fields[0];
$location = $fields[1];
Expand Down Expand Up @@ -122,7 +122,7 @@ public function handle()
$group->area = $area;
$group->latitude = $lat;
$group->longitude = $lng;
$group->country = $country;
$group->country_code = $country;
$group->website = $website;
$group->phone = $phone;
$group->free_text = $description;
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Commands/SyncEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Group;
use App\Helpers\Fixometer;
use App\Party;
use DateTime;
use Illuminate\Console\Command;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function handle()

$custom_fields = [
['key' => 'party_grouphash', 'value' => $event->group],
['key' => 'party_groupcountry', 'value' => $group->country],
['key' => 'party_groupcountry', 'value' => Fixometer::getCountryFromCountryCode($group->country_code)],
['key' => 'party_groupcity', 'value' => $group->area],
['key' => 'party_venue', 'value' => $event->venue],
['key' => 'party_location', 'value' => $event->location],
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Commands/SyncGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Group;
use App\Helpers\Fixometer;
use Illuminate\Console\Command;

class SyncGroups extends Command
Expand Down Expand Up @@ -54,7 +55,7 @@ public function handle()
try {
$custom_fields = [
['key' => 'group_city', 'value' => $group->area],
['key' => 'group_country', 'value' => $group->country],
['key' => 'group_country', 'value' => Fixometer::getCountryFromCountryCode($group->country_code)],
['key' => 'group_website', 'value' => $group->website],
['key' => 'group_hash', 'value' => $group->idgroups],
['key' => 'group_latitude', 'value' => $group->latitude],
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protected function schedule(Schedule $schedule)
->daily()
->sendOutputTo(storage_path().'/logs/discourse_usernames.log')
->emailOutputTo(env('SEND_COMMAND_LOGS_TO'), 'tech@therestartproject.org');

$schedule->command('groups:country')->hourly();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Group extends Model implements Auditable
'postcode',
'latitude',
'longitude',
'country',
'country_code',
'free_text',
'facebook',
'wordpress_post_id',
Expand Down
4 changes: 2 additions & 2 deletions app/Helpers/Geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public function geocode($location)

foreach ($decoded->{'address_components'} as $component) {
if ($component->types && count($component->types) && $component->types[0] === 'country') {
$country = $component->long_name;
$country_code = $component->short_name;
}
}

return [
'latitude' => $latitude,
'longitude' => $longitude,
'country' => $country,
'country_code' => $country_code,
];
}
}
Expand Down
15 changes: 7 additions & 8 deletions app/Http/Controllers/API/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public static function getGroupsByUsersNetworks(Request $request)
// New Collection Instance
$collection = collect([]);

$countries = array_flip(\App\Helpers\Fixometer::getAllCountries());

foreach ($groups as $group) {
// If we have a bounding box, check that the group is within it.
if (! $bbox || (
Expand All @@ -102,7 +100,8 @@ public static function getGroupsByUsersNetworks(Request $request)
'timezone' => $group->timezone,
'location' => [
'value' => $group->location,
'country' => Fixometer::translateCountry($group->country, $countries),
'country' => Fixometer::getCountryFromCountryCode($group->country_code),
'country_code' => $group->country_code,
'latitude' => $group->latitude,
'longitude' => $group->longitude,
'area' => $group->area,
Expand Down Expand Up @@ -606,7 +605,7 @@ public function createGroupv2(Request $request) {
'postcode' => $postcode,
'latitude' => $latitude,
'longitude' => $longitude,
'country' => $country,
'country_code' => $country,
'free_text' => $description,
'shareable_code' => Fixometer::generateUniqueShareableCode(\App\Group::class, 'shareable_code'),
'timezone' => $timezone,
Expand Down Expand Up @@ -750,7 +749,7 @@ public function updateGroupv2(Request $request, $idGroup) {
'location' => $location,
'latitude' => $latitude,
'longitude' => $longitude,
'country' => $country,
'country_code' => $country,
'free_text' => $description,
'timezone' => $timezone,
'phone' => $phone,
Expand Down Expand Up @@ -875,7 +874,7 @@ private function validateGroupParams(Request $request, $create): array {

$latitude = null;
$longitude = null;
$country = null;
$country_code = null;

if ($timezone && !in_array($timezone, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC))) {
throw ValidationException::withMessages(['location ' => __('partials.validate_timezone')]);
Expand All @@ -895,7 +894,7 @@ private function validateGroupParams(Request $request, $create): array {

// Note that the country returned by the geocoder is already in English, which is what we need for the
// value in the database.
$country = $geocoded['country'];
$country_code = $geocoded['country_code'];
}

return array(
Expand All @@ -909,7 +908,7 @@ private function validateGroupParams(Request $request, $create): array {
$timezone,
$latitude,
$longitude,
$country,
$country_code,
$network_data,
);
}
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/API/UserGroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\API;

use App\Group;
use App\Helpers\Fixometer;
use App\Http\Controllers\Controller;
use App\Role;
use App\User;
Expand Down Expand Up @@ -83,7 +84,7 @@ protected static function mapDetailsAndAuditToChange($userGroupAssociation, $aud
$group = Group::find($userGroupAssociation->group);
$userGroupChange['group_name'] = $group->name;
$userGroupChange['group_area'] = $group->area;
$userGroupChange['group_country'] = $group->country;
$userGroupChange['group_country'] = Fixometer::getCountryFromCountryCode($group->country_code);

return $userGroupChange;
}
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,6 @@ public static function expandGroups($groups, $your_groupids, $nearby_groupids)
$user = Auth::user();

if ($groups) {
$countries = array_flip(Fixometer::getAllCountries('en'));

foreach ($groups as $group) {
$group_image = $group->groupImage;

Expand Down Expand Up @@ -513,7 +511,8 @@ public static function expandGroups($groups, $your_groupids, $nearby_groupids)
asset('uploads/mid_'.$group_image->image->path) : null,
'location' => [
'location' => rtrim($group->location),
'country' => Fixometer::translateCountry($group->country, $countries),
'country' => Fixometer::getCountryFromCountryCode($group->country_code),
'country_code' => $group->country_code,
'distance' => $distance,
],
'next_event' => $event ? $event->event_date_local : null,
Expand Down
12 changes: 6 additions & 6 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function postProfileInfoEdit(Request $request, App\Helpers\Geocoder $geoc
User::find($id)->update([
'email' => $request->input('email'),
'name' => $request->input('name'),
'country' => $request->input('country'),
'country_code' => $request->input('country'),
'location' => $request->input('townCity'),
'age' => $request->input('age'),
'gender' => $request->input('gender'),
Expand All @@ -171,7 +171,7 @@ public function postProfileInfoEdit(Request $request, App\Helpers\Geocoder $geoc
}

if (! empty($user->location)) {
$geocoded = $geocoder->geocode("{$user->location}, {$user->country}");
$geocoded = $geocoder->geocode("{$user->location}, " . Fixometer::getCountryFromCountryCode($user->country_code));
if (! empty($geocoded)) {
$user->latitude = $geocoded['latitude'];
$user->longitude = $geocoded['longitude'];
Expand Down Expand Up @@ -536,7 +536,7 @@ public function all()
$user['permissions'] = $User->getRolePermissions($user->role);
$user['groups'] = $user->groups;
$user['lastLogin'] = $user->lastLogin();
$user['country'] = Fixometer::getCountryFromCountryCode($user->country);
$user['country'] = Fixometer::getCountryFromCountryCode($user->country_code);

return $user;
});
Expand Down Expand Up @@ -607,7 +607,7 @@ public function search(Request $request)
$user['permissions'] = $User->getRolePermissions($user->role);
$user['groups'] = $user->groups;
$user['lastLogin'] = $user->lastLogin();
$user['country'] = Fixometer::getCountryFromCountryCode($user->country);
$user['country'] = Fixometer::getCountryFromCountryCode($user->country_code);

return $user;
});
Expand Down Expand Up @@ -920,7 +920,7 @@ public function postRegister(Request $request, $hash = null)

if (Auth::check()) { //Existing users are to update
$user = User::find(Auth::user()->id);
$user->country = $request->input('country');
$user->country_code = $request->input('country');
$user->location = $request->input('city');
$user->gender = $request->input('gender');
$user->age = $request->input('age');
Expand All @@ -935,7 +935,7 @@ public function postRegister(Request $request, $hash = null)
'role' => $role,
'recovery' => substr(bin2hex(openssl_random_pseudo_bytes(32)), 0, 24),
'recovery_expires' => strftime('%Y-%m-%d %X', time() + (24 * 60 * 60)),
'country' => $request->input('country'),
'country_code' => $request->input('country'),
'location' => $request->input('city'),
'gender' => $request->input('gender'),
'age' => $request->input('age'),
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Resources/GroupLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
* example="United Kingdom"
* ),
* @OA\Property(
* property="country_code",
* description="The two-letter ISO country code.",
* format="string",
* example="GB"
* ),
* @OA\Property(
* property="lat",
* title="lat",
* description="Latitude of the group.",
Expand Down Expand Up @@ -66,7 +72,8 @@ public function toArray($request)
'location' => $this->location,
'area' => $this->area,
'postcode' => $this->postcode,
'country' => \App\Helpers\Fixometer::translateCountry($this->country),
'country' => \App\Helpers\Fixometer::getCountryFromCountryCode($this->country_code),
'country_code' => $this->country_code,
'lat' => $this->latitude,
'lng' => $this->longitude,
];
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/CreateWordpressPostForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function createEventOnWordpress($theParty): void
['key' => 'party_venue', 'value' => $theParty->venue],
['key' => 'party_location', 'value' => $theParty->location],
['key' => 'party_time', 'value' => $theParty->getEventStartEndLocal()],
['key' => 'party_groupcountry', 'value' => $group->country],
['key' => 'party_groupcountry', 'value' => Fixometer::getCountryFromCountryCode($group->country_code)],
['key' => 'party_groupcity', 'value' => $group->area],
['key' => 'party_date', 'value' => $theParty->event_date_local],
['key' => 'party_timestamp', 'value' => $startTimestamp],
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/CreateWordpressPostForGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function createGroupOnWordpress($group): void
if (!$group->wordpress_post_id) {
$custom_fields = [
['key' => 'group_city', 'value' => $group->area],
['key' => 'group_country', 'value' => $group->country],
['key' => 'group_country', 'value' => Fixometer::getCountryFromCountryCode($group->country_code)],
['key' => 'group_website', 'value' => $group->website],
['key' => 'group_hash', 'value' => $group->idgroups],
['key' => 'group_avatar_url', 'value' => $group->groupImagePath()],
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/EditWordpressPostForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function handle(EditEvent $event)

$custom_fields = [
['key' => 'party_grouphash', 'value' => $data['group']],
['key' => 'party_groupcountry', 'value' => $group->country],
['key' => 'party_groupcountry', 'value' => Fixometer::getCountryFromCountryCode($group->country_code)],
['key' => 'party_groupcity', 'value' => $group->area],
['key' => 'party_venue', 'value' => $data['venue']],
['key' => 'party_location', 'value' => $data['location']],
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/EditWordpressPostForGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function handle(EditGroup $event)
if (is_numeric($group->wordpress_post_id)) {
$custom_fields = [
['key' => 'group_city', 'value' => $group->area],
['key' => 'group_country', 'value' => $group->country],
['key' => 'group_country', 'value' => Fixometer::getCountryFromCountryCode($group->country_code)],
['key' => 'group_website', 'value' => $data['website']],
['key' => 'group_hash', 'value' => $id],
['key' => 'group_avatar_url', 'value' => $data['group_avatar']],
Expand Down
Loading