Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com>
  • Loading branch information
sampoyigi committed Feb 5, 2024
1 parent 08015d5 commit ac7efbe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
53 changes: 22 additions & 31 deletions app/admin/formwidgets/MapArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function initialize()
$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,7 +162,7 @@ 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')
]);
}
Expand All @@ -176,31 +176,33 @@ public function getTopAreaModel()
return null;
}

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

private function isAreaWithinTopAreaBoundaries($area): bool {
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) {
$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) {
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);
$areaCircleBoundaries->bounds->north, $areaCircleBoundaries->bounds->east);
if (!$topAreaPolygon->pointInPolygon($areaCircleBounds->getSouthWest())) {
throw new ApplicationException("Circle out of bounds");
}
Expand All @@ -216,18 +218,18 @@ private function isAreaWithinTopAreaBoundaries($area): bool {

if ($area->type == 'polygon') {
$areaVertices = json_decode($area->boundaries['vertices']);
foreach($areaVertices as $areaVertex) {
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);
$areaCircleBoundaries->bounds->north, $areaCircleBoundaries->bounds->east);

if (!$topAreaCircle->pointInRadius($areaCircleBounds->getSouthWest())) {
throw new ApplicationException("Circle out of bounds", );
throw new ApplicationException("Circle out of bounds",);
}
if (!$topAreaCircle->pointInRadius($areaCircleBounds->getNorthEast())) {
throw new ApplicationException("Circle out of bounds");
Expand Down Expand Up @@ -258,7 +260,7 @@ public function onSaveRecord()
});

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 @@ -283,7 +285,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 @@ -292,22 +294,11 @@ public function onDeleteArea()
];
}

public function getMapShapeAttributes($area)
{
return $this->getAttributes($area);
}

public function getTopMapShapeAttributes()
{
return $this->getAttributes($this->getTopAreaModel(), true);
}

private function getAttributes($area, $isTop = false)
public function getMapAreaShapes($area)
{

$areaColor = $area->color;

$attributes = [
$attributes = collect([
'data-id' => $area->area_id ?? 1,
'data-name' => $area->name ?? '',
'data-default' => $area->type ?? 'address',
Expand All @@ -320,12 +311,12 @@ private function getAttributes($area, $isTop = false)
'fillColor' => $areaColor,
'strokeColor' => $areaColor,
'distanceUnit' => setting('distance_unit'),
'draggable' => !$isTop,
'clickable' => !$isTop
]),
];
]);

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

return Html::attributes($attributes);
return $attributes;
}

protected function getMapAreas()
Expand Down Expand Up @@ -365,8 +356,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
17 changes: 8 additions & 9 deletions app/admin/formwidgets/maparea/area_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-hidden="true"></button>
</div>
<input type="hidden" name="areaId" value="{{ $formAreaId }}">
<input type="hidden" data-map-shape {!! $this->getMapShapeAttributes($formWidget->model) !!}>
@if($this->getTopAreaModel() && ($this->getTopAreaModel()->area_id != $formWidget->model->area_id))
<input type="hidden" data-map-shape {!! $this->getTopMapShapeAttributes() !!}>
@endif
@foreach($this->getMapAreaShapes($formWidget->model) as $mapShape)
<input type="hidden" data-map-shape {!! Html::attributes($mapShape) !!}>
@endforeach
<div class="modal-body">
<div class="form-fields p-0">
@foreach ($formWidget->getFields() as $field)
Expand All @@ -28,13 +27,13 @@
</div>
<div class="modal-footer text-right">
<button
type="button"
class="btn btn-link"
data-bs-dismiss="modal"
type="button"
class="btn btn-link"
data-bs-dismiss="modal"
>@lang('admin::lang.button_close')</button>
<button
type="submit"
class="btn btn-primary"
type="submit"
class="btn btn-primary"
>@lang('admin::lang.button_save')</button>
</div>
</div>
Expand Down

0 comments on commit ac7efbe

Please sign in to comment.