diff --git a/addons/default/visiosoft/advs-module/resources/js/new-create.js b/addons/default/visiosoft/advs-module/resources/js/new-create.js index feb51dbd1..a58aa2a96 100644 --- a/addons/default/visiosoft/advs-module/resources/js/new-create.js +++ b/addons/default/visiosoft/advs-module/resources/js/new-create.js @@ -29,7 +29,7 @@ filter.getCats = (catId, divId) => { }; $(document).ready(function () { - if ($('input[name="slug"]').val() == "") { + if (!$('input[name="slug"]').val()) { $("select[name='currency']").val(default_currency); } diff --git a/addons/default/visiosoft/advs-module/src/Adv/AdvRepository.php b/addons/default/visiosoft/advs-module/src/Adv/AdvRepository.php index c276543b6..3c70af43b 100644 --- a/addons/default/visiosoft/advs-module/src/Adv/AdvRepository.php +++ b/addons/default/visiosoft/advs-module/src/Adv/AdvRepository.php @@ -727,4 +727,17 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface return $classifieds->paginate($limit, ['*'], 'page', $page); } + + public function getClassifiedsByCoordinates($lat, $lng, $distance = 50) + { + return $this + ->currentAds() + ->whereNotNull('map_Val') + ->select( + DB::raw("*, ( 3959 * acos( cos( radians('$lat') ) * cos( radians( SUBSTRING_INDEX(map_Val, ',', 1) ) ) * cos( radians( SUBSTRING_INDEX(map_Val, ',', -1) ) - radians('$lng') ) + sin( radians('$lat') ) * sin( radians( SUBSTRING_INDEX(map_Val, ',', 1) ) ) ) ) AS distance") + ) + ->havingRaw("distance < $distance") + ->orderBy('distance') + ->get(); + } } diff --git a/addons/default/visiosoft/advs-module/src/Adv/Contract/AdvRepositoryInterface.php b/addons/default/visiosoft/advs-module/src/Adv/Contract/AdvRepositoryInterface.php index 131918bde..81d5c20ff 100644 --- a/addons/default/visiosoft/advs-module/src/Adv/Contract/AdvRepositoryInterface.php +++ b/addons/default/visiosoft/advs-module/src/Adv/Contract/AdvRepositoryInterface.php @@ -74,4 +74,6 @@ interface AdvRepositoryInterface extends EntryRepositoryInterface public function getUnexplainedClassifiedsReport(); public function getNoImageClassifiedsReport(); + + public function getClassifiedsByCoordinates($lat, $lng, $distance = 50); } diff --git a/addons/default/visiosoft/advs-module/src/AdvsModuleServiceProvider.php b/addons/default/visiosoft/advs-module/src/AdvsModuleServiceProvider.php index 9be6ee12d..c9dbd74b7 100644 --- a/addons/default/visiosoft/advs-module/src/AdvsModuleServiceProvider.php +++ b/addons/default/visiosoft/advs-module/src/AdvsModuleServiceProvider.php @@ -177,6 +177,7 @@ class AdvsModuleServiceProvider extends AddonServiceProvider 'class/getcats/{id}' => 'Visiosoft\AdvsModule\Http\Controller\AdvsController@getCatsForNewAd', 'mapJson' => 'Visiosoft\AdvsModule\Http\Controller\AdvsController@mapJson', 'check_user' => 'Visiosoft\AdvsModule\Http\Controller\AdvsController@checkUser', + 'api/classified/get-by-coordinates' => 'Visiosoft\AdvsModule\Http\Controller\AdvsController@getClassifiedsByCoordinates', // AjaxController 'admin/advs/ajax' => [ diff --git a/addons/default/visiosoft/advs-module/src/Http/Controller/AdvsController.php b/addons/default/visiosoft/advs-module/src/Http/Controller/AdvsController.php index 7ef1b63e6..2d108042e 100644 --- a/addons/default/visiosoft/advs-module/src/Http/Controller/AdvsController.php +++ b/addons/default/visiosoft/advs-module/src/Http/Controller/AdvsController.php @@ -1204,4 +1204,24 @@ class AdvsController extends PublicController $response['maxQuantity'] = $adv->stock; return $response; } + + public function getClassifiedsByCoordinates() + { + \request()->validate([ + 'lat' => 'required', + 'lng' => 'required', + ]); + + try { + return [ + 'success' => true, + 'classifieds' => $this->adv_repository->getClassifiedsByCoordinates(\request()->lat, \request()->lng), + ]; + } catch (\Exception $e) { + return [ + 'success' => false, + 'msg' => $e->getMessage() + ]; + } + } }