Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Feb 21, 2024
1 parent 193cbc6 commit 5621997
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 180 deletions.
31 changes: 18 additions & 13 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ parameters:
count: 1
path: src/Classes/Manager.php

-
message: "#^Call to an undefined static method Igniter\\\\User\\\\Facades\\\\AdminAuth\\:\\:isSuperUser\\(\\)\\.$#"
count: 1
path: src/Classes/Manager.php

-
message: "#^Call to an undefined static method Igniter\\\\User\\\\Facades\\\\AdminAuth\\:\\:user\\(\\)\\.$#"
count: 1
Expand Down Expand Up @@ -235,18 +240,13 @@ parameters:
count: 1
path: src/Extension.php

-
message: "#^Call to an undefined static method Igniter\\\\Local\\\\Models\\\\ReviewSettings\\:\\:get\\(\\)\\.$#"
count: 2
path: src/Extension.php

-
message: "#^Call to an undefined static method Igniter\\\\User\\\\Facades\\\\Auth\\:\\:customer\\(\\)\\.$#"
count: 2
path: src/Extension.php

-
message: "#^Call to an undefined method Igniter\\\\Flame\\\\Database\\\\Model\\:\\:find\\(\\)\\.$#"
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:find\\(\\)\\.$#"
count: 1
path: src/FormWidgets/MapArea.php

Expand Down Expand Up @@ -295,11 +295,6 @@ parameters:
count: 1
path: src/Http/Controllers/Locations.php

-
message: "#^Call to an undefined static method Igniter\\\\Flame\\\\Geolite\\\\Facades\\\\Geocoder\\:\\:getLogs\\(\\)\\.$#"
count: 1
path: src/Http/Controllers/Locations.php

-
message: "#^Call to an undefined static method Igniter\\\\Local\\\\Facades\\\\Location\\:\\:check\\(\\)\\.$#"
count: 1
Expand Down Expand Up @@ -375,6 +370,11 @@ parameters:
count: 1
path: src/MainMenuWidgets/LocationPicker.php

-
message: "#^Call to an undefined static method Igniter\\\\Flame\\\\Geolite\\\\Facades\\\\Geocoder\\:\\:getLogs\\(\\)\\.$#"
count: 1
path: src/MainMenuWidgets/LocationPicker.php

-
message: "#^Call to an undefined static method Igniter\\\\Local\\\\Facades\\\\Location\\:\\:current\\(\\)\\.$#"
count: 2
Expand Down Expand Up @@ -457,12 +457,12 @@ parameters:

-
message: "#^Access to an undefined property Igniter\\\\Local\\\\Models\\\\Location\\:\\:\\$location_lat\\.$#"
count: 3
count: 4
path: src/Models/Location.php

-
message: "#^Access to an undefined property Igniter\\\\Local\\\\Models\\\\Location\\:\\:\\$location_lng\\.$#"
count: 3
count: 4
path: src/Models/Location.php

-
Expand Down Expand Up @@ -615,6 +615,11 @@ parameters:
count: 1
path: src/Models/Review.php

-
message: "#^Call to an undefined static method Igniter\\\\Local\\\\Models\\\\ReviewSettings\\:\\:get\\(\\)\\.$#"
count: 1
path: src/Models/ReviewSettings.php

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\:\\:selectDistance\\(\\)\\.$#"
count: 1
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'text_location_summary' => 'We can deliver to you %s',
'text_enter_location' => 'Check if we can deliver to you',
'text_hours' => 'Working Hours',
'text_more_info' => 'More info',
'text_opening' => 'Opening',
'text_24h' => 'All day',
'text_delivery_coverage' => '<span class="text-danger">Sorry, but this restaurant does not deliver to your location</span>',
Expand Down Expand Up @@ -140,6 +141,7 @@
'alert_location_required' => 'No location found or selected',
'alert_order_type_required' => 'There\'s no active online ordering option for this location',
'alert_order_is_unavailable' => 'This restaurant is unavailable to take %s orders at the selected time.',
'alert_delivery_area_unavailable' => 'Sorry, we do not deliver to the address you entered.',
'alert_slot_time_required' => 'Please select a slot time.',
'alert_slot_date_required' => 'Please select a slot date.',
'alert_slot_type_required' => 'Please select a slot type.',
Expand Down
15 changes: 12 additions & 3 deletions resources/models/reviewsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Model configuration options for settings model.
*/

use Igniter\Local\Models\ReviewSettings;

return [
'form' => [
'toolbar' => [
Expand Down Expand Up @@ -40,11 +42,18 @@
'condition' => 'checked',
],
],
'ratings[ratings]' => [
'hints' => [
'label' => 'lang:igniter.local::default.reviews.label_hints',
'tab' => 'lang:igniter.local::default.reviews.text_tab_title_reviews',
'type' => 'partial',
'path' => 'igniter.local::_partials/reviews/ratings',
'type' => 'repeater',
'default' => ReviewSettings::$defaultHints,
'form' => [
'fields' => [
'value' => [
'type' => 'text',
],
],
],
'commentAbove' => 'lang:igniter.local::default.reviews.help_hints',
],
],
Expand Down
6 changes: 4 additions & 2 deletions src/Classes/CoveredArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ protected function checkConditions($cartTotal, $value = 'total')

protected function calculateDistanceCharges()
{
if (!Location::userPosition()->isValid()) {
$distanceCharges = collect($this->model->boundaries['distance'] ?? []);

if (!Location::userPosition()->isValid() || $distanceCharges->isEmpty()) {
return 0;
}

$distanceFromLocation = round(Location::checkDistance(), 2);

$condition = collect($this->model->boundaries['distance'] ?? [])
$condition = $distanceCharges
->sortBy('priority')
->map(function ($condition) {
return new CoveredAreaCondition([
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public function getDeliveryChargeConditions()
*/
public function checkMinimumOrder($cartTotal)
{
return $cartTotal >= $this->minimumOrderTotal();
return $this->checkMinimumOrderTotal($cartTotal);
}

public function checkDistance()
Expand Down
6 changes: 5 additions & 1 deletion src/Classes/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function current(): ?LocationInterface

$slug = $this->resolveLocationSlug();
if ($slug && $model = $this->getBySlug($slug)) {
$this->setModel($model);
$this->setCurrent($model);
} else {
$id = $this->getSession('id');
if ($id && $model = $this->getById($id)) {
Expand Down Expand Up @@ -99,6 +99,10 @@ public function currentOrAssigned(): array
return [$this->getId()];
}

if (AdminAuth::isSuperUser()) {
return [];
}

return AdminAuth::user()?->locations?->pluck('location_id')->all() ?? [];
}

Expand Down
22 changes: 11 additions & 11 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
use Igniter\Local\Models\Review;
use Igniter\Local\Models\ReviewSettings;
use Igniter\Local\Models\Scopes\LocationScope;
use Igniter\Main\Classes\MainController;
use Igniter\Local\Requests\LocationRequest;
use Igniter\Reservation\Models\Reservation;
use Igniter\User\Facades\Auth;
use Igniter\User\Models\User;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;

class Extension extends \Igniter\System\Classes\BaseExtension
{
Expand Down Expand Up @@ -65,10 +64,6 @@ public function register()

public function boot()
{
Event::listen('main.page.init', function (MainController $controller) {
View::share('allowReviews', ReviewSettings::allowReviews());
});

$this->bindRememberLocationAreaEvents();

$this->addReviewsRelationship();
Expand All @@ -77,19 +72,19 @@ public function boot()

User::extend(function ($model) {
$model->addDynamicMethod('getAvailableLocations', function () use ($model) {
if ($model->locations->isEmpty() && $model->isSuperUser()) {
if ($model->isSuperUser()) {
return $model->locations()->getModel()->query()->get();
}

return $model->locations;
});

$model->addDynamicMethod('isAssignedLocation', function ($location) use ($model) {
if ($model->locations->isEmpty()) {
return $model->isSuperUser();
if ($model->isSuperUser()) {
return true;
}

return $model->locations->contains($location);
return $model->locations?->contains($location);
});
});

Expand Down Expand Up @@ -308,6 +303,10 @@ protected function addReviewsRelationship(): void
protected function bindRememberLocationAreaEvents(): void
{
Event::listen('location.position.updated', function ($location, $position, $oldPosition) {
if ($position->format() === $oldPosition->format()) {
return;
}

$this->updateCustomerLastArea([
'query' => $position->format(),
]);
Expand Down Expand Up @@ -350,7 +349,7 @@ protected function updateCustomerLastArea($value)
$lastArea = @json_decode($customer->last_location_area, true) ?: [];
$lastArea = array_merge($lastArea, $value);

$customer->update([
$customer->updateQuietly([
'last_location_area' => json_encode($lastArea),
]);
}
Expand All @@ -364,6 +363,7 @@ protected function registerLocationsMainMenuItems()
->permission('Admin.Locations')
->mergeConfig([
'form' => 'igniter.local::/models/location',
'request' => LocationRequest::class,
]),
]);
});
Expand Down
4 changes: 2 additions & 2 deletions src/FormWidgets/MapArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public function loadAssets()
);
}

$this->addJs('formwidgets/mapview.js', 'mapview-js');
$this->addJs('formwidgets/mapview.shape.js', 'mapview-shape-js');
$this->addJs('mapview.js', 'mapview-js');
$this->addJs('mapview.shape.js', 'mapview-shape-js');
}

public function render()
Expand Down
10 changes: 0 additions & 10 deletions src/Http/Controllers/Locations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Igniter\Local\Http\Controllers;

use Igniter\Admin\Facades\AdminMenu;
use Igniter\Flame\Geolite\Facades\Geocoder;
use Igniter\Local\Facades\Location as LocationFacade;

class Locations extends \Igniter\Admin\Classes\AdminController
Expand Down Expand Up @@ -53,15 +52,6 @@ public function settings_onSave($context = null)
return $this->asExtension('FormController')->edit_onSave($context, LocationFacade::getId());
}

public function formAfterSave($model)
{
if ($model->is_auto_lat_lng) {
if ($logs = Geocoder::getLogs()) {
flash()->error(implode(PHP_EOL, $logs))->important();
}
}
}

public function mapViewCenterCoords()
{
$model = $this->getFormModel();
Expand Down
7 changes: 5 additions & 2 deletions src/MainMenuWidgets/LocationPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Igniter\Admin\Widgets\Form;
use Igniter\Flame\Exception\ApplicationException;
use Igniter\Flame\Exception\FlashException;
use Igniter\Flame\Geolite\Facades\Geocoder;
use Igniter\Local\Facades\Location as LocationFacade;
use Igniter\Local\Models\Location;
use Igniter\Local\Requests\LocationRequest;
use Igniter\User\Facades\AdminAuth;
use Illuminate\Support\Facades\DB;

Expand Down Expand Up @@ -100,7 +100,6 @@ public function onSaveRecord()

$form = $this->makeLocationFormWidget($model);

$this->config['request'] = LocationRequest::class;
$saveData = $this->validateFormWidget($form, $form->getSaveData());

$modelsToSave = $this->prepareModelsToSave($model, $saveData);
Expand All @@ -111,6 +110,10 @@ public function onSaveRecord()
}
});

if ($logs = Geocoder::getLogs()) {
flash()->error(implode(PHP_EOL, $logs))->important()->now();
}

flash()->success(sprintf(lang('igniter::admin.alert_success'),
lang('igniter.local::default.picker.text_form_name').' '.($form->context == 'create' ? 'created' : 'updated')))->now();

Expand Down
22 changes: 13 additions & 9 deletions src/Models/Concerns/HasDeliveryAreas.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ protected function geocodeAddressOnSave()
return;
}

if (!$this->isDirty([
'location_address_1',
'location_address_2',
'location_city',
'location_state',
'location_postcode',
'location_country_id',
])) {
return;
if ($this->location_lat && $this->location_lng) {
if (!$this->isDirty([
'location_address_1',
'location_address_2',
'location_city',
'location_state',
'location_postcode',
'location_country_id',
'location_lat',
'location_lng',
])) {
return;
}
}

$address = format_address($this->getAddress(), false);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static function findBy($saleType, $saleId)

public function getRatingOptions()
{
return array_get(ReviewSettings::get('ratings'), 'ratings', []);
return ReviewSettings::get('hints', ReviewSettings::$defaultHints);
}

//
Expand Down
8 changes: 8 additions & 0 deletions src/Models/ReviewSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ class ReviewSettings extends Model
// Reference to field configuration
public string $settingsFieldsConfig = 'reviewsettings';

public static array $defaultHints = [
['value' => 'Poor'],
['value' => 'Average'],
['value' => 'Good'],
['value' => 'Very Good'],
['value' => 'Excellent'],
];

public static function allowReviews()
{
return (bool)self::get('allow_reviews', true);
Expand Down
3 changes: 1 addition & 2 deletions src/Models/Scopes/LocationScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Igniter\Flame\Database\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class LocationScope extends Scope
{
Expand All @@ -27,7 +26,7 @@ public function addSelectDistance()
$sql .= ' cos( radians( location_lng ) - radians(?) ) + sin( radians(?) ) *';
$sql .= ' sin( radians( location_lat ) ) ) ) AS distance';

return $builder->select(DB::raw($sql), [$latitude, $longitude, $latitude]);
return $builder->selectRaw($sql, [$latitude, $longitude, $latitude]);
};
}
}
Loading

0 comments on commit 5621997

Please sign in to comment.