Skip to content

Commit

Permalink
vanellix refactor complete
Browse files Browse the repository at this point in the history
  • Loading branch information
obinnaelviso committed Feb 12, 2024
1 parent ac7efbe commit d20109c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 92 deletions.
2 changes: 1 addition & 1 deletion app/admin/assets/css/admin.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 10 additions & 89 deletions app/admin/formwidgets/MapArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
use Admin\Traits\FormModelWidget;
use Admin\Traits\ValidatesForm;
use Igniter\Flame\Exception\ApplicationException;
use Igniter\Flame\Geolite\Geolite;
use Igniter\Flame\Geolite\Model\Bounds;
use Igniter\Flame\Geolite\Model\Coordinates;
use Igniter\Flame\Html\HtmlFacade as Html;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

Expand Down Expand Up @@ -54,7 +50,6 @@ class MapArea extends BaseFormWidget
protected $defaultAlias = 'maparea';

protected $areaColors;
protected $topArea;

protected $shapeDefaultProperties = [
'id' => null,
Expand Down Expand Up @@ -85,11 +80,10 @@ public function initialize()
'deleteLabel',
'sortable',
]);
$this->topArea = $this->getTopAreaModel();
$this->areaColors = Location_areas_model::$areaColors;

$fieldName = $this->formField->getName(false);
$this->sortableInputName = self::SORT_PREFIX.$fieldName;
$this->sortableInputName = self::SORT_PREFIX . $fieldName;
}

public function loadAssets()
Expand Down Expand Up @@ -162,83 +156,11 @@ public function onLoadRecord()

return $this->makePartial('maparea/area_form', [
'formAreaId' => $areaId,
'formTitle' => ($model->exists ? $this->editLabel : $this->addLabel).' '.lang($this->formName),
'formTitle' => ($model->exists ? $this->editLabel : $this->addLabel) . ' ' . lang($this->formName),
'formWidget' => $this->makeAreaFormWidget($model, 'edit')
]);
}

public function getTopAreaModel()
{
$mapAreas = collect($this->getMapAreas());
if ($mapAreas->count() > 1) {
return $mapAreas->sortBy('priority')->first();
}
return null;
}

public function isTopArea($area): bool
{
return ($this->topArea == null) || ($this->topArea->area_id == $area->area_id);
}

private function isAreaWithinTopAreaBoundaries($area): bool
{
if ($this->isTopArea($area)) {
return true;
}
$geolite = new Geolite();
if ($this->topArea->type == 'polygon') {
$topAreaCoordinates = collect(json_decode($this->topArea->boundaries['vertices']))->map(function ($coordinate) {
return [$coordinate->lat, $coordinate->lng];
})->toArray();
$topAreaPolygon = $geolite->polygon($topAreaCoordinates);
if ($area->type == 'polygon') {
$areaVertices = json_decode($area->boundaries['vertices']);
foreach ($areaVertices as $areaVertex) {
if (!$topAreaPolygon->pointInPolygon(new Coordinates($areaVertex->lat, $areaVertex->lng))) {
throw new ApplicationException("Polygon out of bounds");
}
}
} else if ($area->type == 'circle') {
$areaCircleBoundaries = json_decode($area->boundaries['circle']);
$areaCircleBounds = new Bounds($areaCircleBoundaries->bounds->south, $areaCircleBoundaries->bounds->west,
$areaCircleBoundaries->bounds->north, $areaCircleBoundaries->bounds->east);
if (!$topAreaPolygon->pointInPolygon($areaCircleBounds->getSouthWest())) {
throw new ApplicationException("Circle out of bounds");
}
if (!$topAreaPolygon->pointInPolygon($areaCircleBounds->getNorthEast())) {
throw new ApplicationException("Circle out of bounds");
}
}
} else if ($this->topArea->type == 'circle') {
$topAreaCircleBoundaries = json_decode($this->topArea->boundaries['circle']);
$topAreaCircle = $geolite->circle(
new Coordinates($topAreaCircleBoundaries->lat, $topAreaCircleBoundaries->lng),
$topAreaCircleBoundaries->radius);

if ($area->type == 'polygon') {
$areaVertices = json_decode($area->boundaries['vertices']);
foreach ($areaVertices as $areaVertex) {
if (!$topAreaCircle->pointInRadius(new Coordinates($areaVertex->lat, $areaVertex->lng))) {
throw new ApplicationException("Polygon out of bounds");
}
}
} else if ($area->type == 'circle') {
$areaCircleBoundaries = json_decode($area->boundaries['circle']);
$areaCircleBounds = new Bounds($areaCircleBoundaries->bounds->south, $areaCircleBoundaries->bounds->west,
$areaCircleBoundaries->bounds->north, $areaCircleBoundaries->bounds->east);

if (!$topAreaCircle->pointInRadius($areaCircleBounds->getSouthWest())) {
throw new ApplicationException("Circle out of bounds",);
}
if (!$topAreaCircle->pointInRadius($areaCircleBounds->getNorthEast())) {
throw new ApplicationException("Circle out of bounds");
}
}
}
return true;
}

public function onSaveRecord()
{
$model = strlen($areaId = post('areaId'))
Expand All @@ -253,14 +175,13 @@ public function onSaveRecord()

DB::transaction(function () use ($modelsToSave) {
foreach ($modelsToSave as $modelToSave) {
if ($this->isAreaWithinTopAreaBoundaries($modelToSave)) {
$modelToSave->saveOrFail();
}
$this->fireSystemEvent('maparea.extraValidation', [$modelToSave]);
$modelToSave->saveOrFail();
}
});

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

$this->formField->value = null;
Expand All @@ -285,7 +206,7 @@ public function onDeleteArea()

$model->delete();

flash()->success(sprintf(lang('admin::lang.alert_success'), lang($this->formName).' deleted'))->now();
flash()->success(sprintf(lang('admin::lang.alert_success'), lang($this->formName) . ' deleted'))->now();

$this->prepareVars();

Expand All @@ -298,7 +219,7 @@ public function getMapAreaShapes($area)
{
$areaColor = $area->color;

$attributes = collect([
$attributes = collect([[
'data-id' => $area->area_id ?? 1,
'data-name' => $area->name ?? '',
'data-default' => $area->type ?? 'address',
Expand All @@ -312,7 +233,7 @@ public function getMapAreaShapes($area)
'strokeColor' => $areaColor,
'distanceUnit' => setting('distance_unit'),
]),
]);
]]);

$this->fireSystemEvent('maparea.extendMapAreaShapes', [$area, $attributes]);

Expand Down Expand Up @@ -356,8 +277,8 @@ protected function makeAreaFormWidget($model, $context = null)
$config = is_string($this->form) ? $this->loadConfig($this->form, ['form'], 'form') : $this->form;
$config['context'] = $context;
$config['model'] = $model;
$config['alias'] = $this->alias.'Form';
$config['arrayName'] = $this->formField->arrayName.'[areaData]';
$config['alias'] = $this->alias . 'Form';
$config['arrayName'] = $this->formField->arrayName . '[areaData]';

$widget = $this->makeWidget('Admin\Widgets\Form', $config);
$widget->bindToController();
Expand Down
1 change: 0 additions & 1 deletion app/admin/formwidgets/mapview/assets/js/mapview.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@
var shapeObj;

shapeObj = this.getShape(shape);
console.log(shapeObj);

if (!shapeObj)
return
Expand Down
1 change: 0 additions & 1 deletion app/admin/formwidgets/mapview/assets/js/mapview.shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@
break
case 'drag':
this.$mapView.trigger('dragging.shape.ti.mapview', [visibleMapObject, this])
console.log("dem dey drag me o");
break
case 'dragend':
this.$mapView.trigger('dragged.shape.ti.mapview', [visibleMapObject, this])
Expand Down
2 changes: 2 additions & 0 deletions storage/debugbar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit d20109c

Please sign in to comment.