location module refactor

This commit is contained in:
vedatakd 2021-01-23 17:31:24 +03:00
parent 5473dd7006
commit 9ab2c30e4d
48 changed files with 404 additions and 158 deletions

View File

@ -10,7 +10,7 @@ use Visiosoft\AdvsModule\Adv\Contract\AdvInterface;
use Anomaly\Streams\Platform\View\ViewTemplate; use Anomaly\Streams\Platform\View\ViewTemplate;
use Visiosoft\CatsModule\Category\Contract\CategoryInterface; use Visiosoft\CatsModule\Category\Contract\CategoryInterface;
use Visiosoft\LocationModule\City\Contract\CityInterface; use Visiosoft\LocationModule\City\Contract\CityInterface;
use Visiosoft\LocationModule\Country\Contract\CountryInterface; use Visiosoft\LocationModule\Country\Contract\CountryRepositoryInterface;
class AdvLoader { class AdvLoader {
@ -21,8 +21,8 @@ class AdvLoader {
$this->template = $template; $this->template = $template;
} }
public function load(AdvInterface $adv, CategoryInterface $cats, CityInterface $city, CountryInterface $country) { public function load(AdvInterface $adv, CategoryInterface $cats, CityInterface $city, CountryRepositoryInterface $country) {
$this->template->set('adv', $adv); $this->template->set('adv', $adv);
$this->template->set('country', $country->getCountry($adv->country_id)); $this->template->set('country', $country->find($adv->country_id));
} }
} }

View File

@ -31,11 +31,6 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface
$this->folderRepository = $folderRepository; $this->folderRepository = $folderRepository;
} }
public function findById($id)
{
return $this->model->orderBy('created_at', 'DESC')->where('advs_advs.id', $id)->first();
}
public function searchAdvs( public function searchAdvs(
$type, $param = null, $customParameters = [], $type, $param = null, $customParameters = [],
$limit = null, $category = null, $city = null, $paginate = true $limit = null, $category = null, $city = null, $paginate = true

View File

@ -16,6 +16,6 @@ class AdvResolver {
} }
public function resolve() { public function resolve() {
return $this->adv->findById($this->route->parameter('id')); return $this->adv->find($this->route->parameter('id'));
} }
} }

View File

@ -27,7 +27,7 @@ class GetAd
public function handle(AdvRepositoryInterface $groups) public function handle(AdvRepositoryInterface $groups)
{ {
if ($this->id) { if ($this->id) {
return $groups->findById($this->id); return $groups->find($this->id);
} }
return null; return null;
} }

View File

@ -4,8 +4,6 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryRepositoryInterface;
interface AdvRepositoryInterface extends EntryRepositoryInterface interface AdvRepositoryInterface extends EntryRepositoryInterface
{ {
public function findById($id);
public function searchAdvs( public function searchAdvs(
$type, $param = null, $customParameters = null, $type, $param = null, $customParameters = null,
$limit = null, $category = null, $city = null, $paginate = true $limit = null, $category = null, $city = null, $paginate = true

View File

@ -145,7 +145,7 @@ class AdvsController extends PublicController
$param = $this->requestHttp->toArray(); $param = $this->requestHttp->toArray();
$countries = $this->country_repository->viewAll(); $countries = $this->country_repository->newQuery()->get();
$isActiveDopings = $this->adv_model->is_enabled('dopings'); $isActiveDopings = $this->adv_model->is_enabled('dopings');

View File

@ -18,7 +18,7 @@ class OptionConfigurationTableColumns
'value' => function (EntryModel $entry, 'value' => function (EntryModel $entry,
AdvRepositoryInterface $advRepository) { AdvRepositoryInterface $advRepository) {
$adv = $advRepository->findById($entry->parent_adv_id); $adv = $advRepository->find($entry->parent_adv_id);
return "<span><a href='" . route('adv_detail', [$entry->parent_adv_id]) . "'>$adv->name</a></span>"; return "<span><a href='" . route('adv_detail', [$entry->parent_adv_id]) . "'>$adv->name</a></span>";
} }
], ],

View File

@ -124,5 +124,5 @@
<script> <script>
var null_msg = "{{ trans('visiosoft.module.location::message.null_msg') }}!" var null_msg = "{{ trans('visiosoft.module.location::message.null_msg') }}!"
var defaultCountry = {{ defaultCountry }} var defaultCountry = "{{ defaultCountry }}";
</script> </script>

View File

@ -1,10 +1,12 @@
<?php namespace Visiosoft\LocationModule\City; <?php namespace Visiosoft\LocationModule\City;
use Anomaly\Streams\Platform\Entry\EntryCriteria; use Anomaly\Streams\Platform\Entry\EntryCriteria;
use Visiosoft\LocationModule\City\Contract\CityRepositoryInterface;
class CityCriteria extends EntryCriteria class CityCriteria extends EntryCriteria
{ {
public function getSubCities($city) { public function getCitiesByCountryId($country_id) {
return $this->query->where('parent_country_id', $city)->get(); $city_repository = app(CityRepositoryInterface::class);
return $city_repository->getCitiesByCountryId($country_id);
} }
} }

View File

@ -2,30 +2,8 @@
use Visiosoft\LocationModule\City\Contract\CityInterface; use Visiosoft\LocationModule\City\Contract\CityInterface;
use Anomaly\Streams\Platform\Model\Location\LocationCitiesEntryModel; use Anomaly\Streams\Platform\Model\Location\LocationCitiesEntryModel;
use Visiosoft\LocationModule\District\DistrictModel;
class CityModel extends LocationCitiesEntryModel implements CityInterface class CityModel extends LocationCitiesEntryModel implements CityInterface
{ {
public function getCities($id = null) {
if($id != null)
{
return CityModel::query()->where('location_cities.id', $id)->first();
}
return CityModel::all();
}
public function getSubCities($country) {
return $this->query()->where('parent_country_id', $country)->orderBy('order','ASC')->get();
}
public function deleteCitiesByCountry($id) {
$districts = new DistrictModel();
$city = $this->where('parent_country_id',$id);
$city_id = $city->orderBy('id','DESC')->get();
foreach ($city_id as $item)
{
$districts->deleteDistrictByCity($item->id);
}
return $city->delete();
}
} }

View File

@ -30,11 +30,6 @@ class CityRepository extends EntryRepository implements CityRepositoryInterface
$this->citiesEntryTranslationsModel = $citiesEntryTranslationsModel; $this->citiesEntryTranslationsModel = $citiesEntryTranslationsModel;
} }
public function findById($id)
{
return $this->model->orderBy('created_at', 'DESC')->where('location_cities.id', $id)->first();
}
public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc') public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc')
{ {
return $this->citiesEntryTranslationsModel->newQuery() return $this->citiesEntryTranslationsModel->newQuery()
@ -48,4 +43,11 @@ class CityRepository extends EntryRepository implements CityRepositoryInterface
->orderBy($orderBy, $direction) ->orderBy($orderBy, $direction)
->get(); ->get();
} }
public function getCitiesByCountryId($country_id) {
return $this->newQuery()
->where('parent_country_id', $country_id)
->orderBy('order','ASC')
->get();
}
} }

View File

@ -4,5 +4,5 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
interface CityInterface extends EntryInterface interface CityInterface extends EntryInterface
{ {
public function getCities();
} }

View File

@ -4,7 +4,7 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryRepositoryInterface;
interface CityRepositoryInterface extends EntryRepositoryInterface interface CityRepositoryInterface extends EntryRepositoryInterface
{ {
public function findById($id);
public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc'); public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc');
public function getCitiesByCountryId($country_id);
} }

View File

@ -0,0 +1,17 @@
<?php namespace Visiosoft\LocationModule\City\Events;
class DeletedCities
{
private $cities;
public function __construct($cities)
{
$this->cities = $cities;
}
public function getCities()
{
return $this->cities;
}
}

View File

@ -0,0 +1,28 @@
<?php namespace Visiosoft\LocationModule\City\Listeners;
use Visiosoft\LocationModule\City\Contract\CityRepositoryInterface;
use Visiosoft\LocationModule\City\Events\DeletedCities;
class DeletedCountry
{
public $cityRepository;
public function __construct(CityRepositoryInterface $cityRepository)
{
$this->cityRepository = $cityRepository;
}
public function handle(\Visiosoft\LocationModule\Country\Events\DeletedCountry $event)
{
$country = $event->getCountry();
$query = $this->cityRepository->newQuery()
->where('parent_country_id', $country->id);
if (count($cities = $query->get())) {
$query->delete();
event(new DeletedCities($cities));
}
}
}

View File

@ -1,6 +1,7 @@
<?php namespace Visiosoft\LocationModule\City\Table; <?php namespace Visiosoft\LocationModule\City\Table;
use Anomaly\Streams\Platform\Ui\Table\TableBuilder; use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
use Visiosoft\LocationModule\City\Table\Handler\Delete;
class CityTableBuilder extends TableBuilder class CityTableBuilder extends TableBuilder
{ {
@ -60,7 +61,9 @@ class CityTableBuilder extends TableBuilder
* @var array|string * @var array|string
*/ */
protected $actions = [ protected $actions = [
'delete' 'delete' => [
'handler' => Delete::class,
],
]; ];
/** /**

View File

@ -0,0 +1,24 @@
<?php namespace Visiosoft\LocationModule\City\Table\Handler;
use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler;
use Visiosoft\LocationModule\City\Contract\CityRepositoryInterface;
use Visiosoft\LocationModule\City\Events\DeletedCities;
class Delete extends ActionHandler
{
public function handle(CityRepositoryInterface $repository, array $selected)
{
$query = $repository->newQuery()->whereIn('location_cities.id', $selected);
if ($count = count($cities = $query->get())) {
$query->delete();
event(new DeletedCities($cities));
}
if ($selected && $count > 0) {
$this->messages->success(trans('streams::message.delete_success', compact('count')));
}
}
}

View File

@ -4,5 +4,5 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
interface CountryInterface extends EntryInterface interface CountryInterface extends EntryInterface
{ {
public function getCountry($id);
} }

View File

@ -4,9 +4,5 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryRepositoryInterface;
interface CountryRepositoryInterface extends EntryRepositoryInterface interface CountryRepositoryInterface extends EntryRepositoryInterface
{ {
public function findById($id);
public function viewAll();
public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc'); public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc');
} }

View File

@ -5,8 +5,5 @@ use Anomaly\Streams\Platform\Model\Location\LocationCountriesEntryModel;
class CountryModel extends LocationCountriesEntryModel implements CountryInterface class CountryModel extends LocationCountriesEntryModel implements CountryInterface
{ {
public function getCountry($id)
{
return CountryModel::query()->where('location_countries.id', $id)->first();
}
} }

View File

@ -32,14 +32,6 @@ class CountryRepository extends EntryRepository implements CountryRepositoryInte
$this->model = $model; $this->model = $model;
$this->countriesEntryTranslationsModel = $countriesEntryTranslationsModel; $this->countriesEntryTranslationsModel = $countriesEntryTranslationsModel;
} }
public function findById($id)
{
return $this->model->orderBy('created_at', 'DESC')->where('location_countries.id', $id)->first();
}
public function viewAll(){
return $this->model->get();
}
public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc') public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc')
{ {

View File

@ -0,0 +1,17 @@
<?php namespace Visiosoft\LocationModule\Country\Events;
class DeletedCountry
{
private $country;
public function __construct($country)
{
$this->country = $country;
}
public function getCountry()
{
return $this->country;
}
}

View File

@ -1,6 +1,7 @@
<?php namespace Visiosoft\LocationModule\Country\Table; <?php namespace Visiosoft\LocationModule\Country\Table;
use Anomaly\Streams\Platform\Ui\Table\TableBuilder; use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
use Visiosoft\LocationModule\Country\Table\Handler\Delete;
class CountryTableBuilder extends TableBuilder class CountryTableBuilder extends TableBuilder
{ {
@ -60,7 +61,9 @@ class CountryTableBuilder extends TableBuilder
* @var array|string * @var array|string
*/ */
protected $actions = [ protected $actions = [
'delete' 'delete' => [
'handler' => Delete::class,
],
]; ];
/** /**

View File

@ -0,0 +1,49 @@
<?php namespace Visiosoft\LocationModule\Country\Table\Handler;
use Anomaly\Streams\Platform\Model\EloquentModel;
use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler;
use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
use Visiosoft\LocationModule\Country\Events\DeletedCountry;
class Delete extends ActionHandler
{
public function handle(TableBuilder $builder, array $selected)
{
$count = 0;
$model = $builder->getTableModel();
/* @var EloquentModel $entry */
foreach ($selected as $id) {
$entry = $model->find($id);
$deletable = true;
if ($entry instanceof EloquentModel) {
$deletable = $entry->isDeletable();
}
if ($entry && $deletable && $entry->delete()) {
$builder->fire('row_deleted', compact('builder', 'model', 'entry'));
event(new DeletedCountry($entry));
$count++;
}
}
if ($count) {
$builder->fire('rows_deleted', compact('count', 'builder', 'model'));
}
if ($selected && $count > 0) {
$this->messages->success(trans('streams::message.delete_success', compact('count')));
}
if ($selected && $count === 0) {
$this->messages->warning(trans('streams::message.delete_success', compact('count')));
}
}
}

View File

@ -5,4 +5,6 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryRepositoryInterface;
interface DistrictRepositoryInterface extends EntryRepositoryInterface interface DistrictRepositoryInterface extends EntryRepositoryInterface
{ {
public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc'); public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc');
public function getDistrictByCityId($city);
} }

View File

@ -2,26 +2,8 @@
use Visiosoft\LocationModule\District\Contract\DistrictInterface; use Visiosoft\LocationModule\District\Contract\DistrictInterface;
use Anomaly\Streams\Platform\Model\Location\LocationDistrictsEntryModel; use Anomaly\Streams\Platform\Model\Location\LocationDistrictsEntryModel;
use Visiosoft\LocationModule\Neighborhood\NeighborhoodModel;
class DistrictModel extends LocationDistrictsEntryModel implements DistrictInterface class DistrictModel extends LocationDistrictsEntryModel implements DistrictInterface
{ {
public function getDistricts() {
return DistrictModel::all();
}
public function getSubDistricts($city) {
return $this->query()->where('parent_city_id', $city)->orderBy('order','ASC')->get();
}
public function deleteDistrictByCity($id) {
$neighborhood = new NeighborhoodModel();
$districts = $this->where('parent_city_id',$id);
$districts_id = $districts->orderBy('id','DESC')->get();
foreach ($districts_id as $item)
{
$neighborhood->deleteNeighborhoodByDistrict($item->id);
}
return $districts->delete();
}
} }

View File

@ -46,4 +46,11 @@ class DistrictRepository extends EntryRepository implements DistrictRepositoryIn
->orderBy($orderBy, $direction) ->orderBy($orderBy, $direction)
->get(); ->get();
} }
public function getDistrictByCityId($city) {
return $this->newQuery()
->where('parent_city_id', $city)
->orderBy('order','ASC')
->get();
}
} }

View File

@ -0,0 +1,17 @@
<?php namespace Visiosoft\LocationModule\District\Events;
class DeletedDistricts
{
private $districts;
public function __construct($districts)
{
$this->districts = $districts;
}
public function getDistricts()
{
return $this->districts;
}
}

View File

@ -0,0 +1,30 @@
<?php namespace Visiosoft\LocationModule\District\Listeners;
use Visiosoft\LocationModule\District\Contract\DistrictRepositoryInterface;
use Visiosoft\LocationModule\District\Events\DeletedDistricts;
class DeletedCities
{
public $districtRepository;
public function __construct(DistrictRepositoryInterface $districtRepository)
{
$this->districtRepository = $districtRepository;
}
public function handle(\Visiosoft\LocationModule\City\Events\DeletedCities $event)
{
$cities = $event->getCities();
$cities = $cities->pluck('id')->all();
$query = $this->districtRepository->newQuery()
->whereIn('parent_city_id', $cities);
if (count($districts = $query->get())) {
$query->delete();
event(new DeletedDistricts($districts));
}
}
}

View File

@ -1,6 +1,7 @@
<?php namespace Visiosoft\LocationModule\District\Table; <?php namespace Visiosoft\LocationModule\District\Table;
use Anomaly\Streams\Platform\Ui\Table\TableBuilder; use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
use Visiosoft\LocationModule\District\Table\Handler\Delete;
class DistrictTableBuilder extends TableBuilder class DistrictTableBuilder extends TableBuilder
{ {
@ -60,7 +61,9 @@ class DistrictTableBuilder extends TableBuilder
* @var array|string * @var array|string
*/ */
protected $actions = [ protected $actions = [
'delete' 'delete' => [
'handler' => Delete::class,
],
]; ];
/** /**

View File

@ -0,0 +1,24 @@
<?php namespace Visiosoft\LocationModule\District\Table\Handler;
use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler;
use Visiosoft\LocationModule\District\Contract\DistrictRepositoryInterface;
use Visiosoft\LocationModule\District\Events\DeletedDistricts;
class Delete extends ActionHandler
{
public function handle(DistrictRepositoryInterface $repository, array $selected)
{
$query = $repository->newQuery()->whereIn('location_districts.id', $selected);
if ($count = count($cities = $query->get())) {
$query->delete();
event(new DeletedDistricts($cities));
}
if ($selected && $count > 0) {
$this->messages->success(trans('streams::message.delete_success', compact('count')));
}
}
}

View File

@ -1,28 +1,21 @@
<?php namespace Visiosoft\LocationModule\Http\Controller\Admin; <?php namespace Visiosoft\LocationModule\Http\Controller\Admin;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Visiosoft\LocationModule\City\CityModel; use Visiosoft\LocationModule\City\Contract\CityRepositoryInterface;
use Visiosoft\LocationModule\City\Form\CityFormBuilder; use Visiosoft\LocationModule\City\Form\CityFormBuilder;
use Visiosoft\LocationModule\City\Table\CityTableBuilder; use Visiosoft\LocationModule\City\Table\CityTableBuilder;
use Anomaly\Streams\Platform\Http\Controller\AdminController; use Anomaly\Streams\Platform\Http\Controller\AdminController;
use Visiosoft\LocationModule\District\DistrictModel;
class CitiesController extends AdminController class CitiesController extends AdminController
{ {
public function index(CityTableBuilder $table, Request $request) public function index(CityTableBuilder $table, Request $request)
{ {
if($this->request->action == "delete") { $cities = app(CityRepositoryInterface::class);
$disticts = new DistrictModel();
foreach ($this->request->id as $item)
{
$disticts->deleteDistrictByCity($item);
}
}
$cities = new CityModel();
if(!isset($request->country) || $request->country==""){ if(!isset($request->country) || $request->country==""){
return $table->render(); return $table->render();
} else { } else {
$cities = $cities->getSubCities($request->country); $cities = $cities->getCitiesByCountryId($request->country);
if (count($cities) == 0) { if (count($cities) == 0) {
$this->messages->error('Selected country has no related city.'); $this->messages->error('Selected country has no related city.');
return redirect('/admin/location/countries'); return redirect('/admin/location/countries');

View File

@ -1,6 +1,5 @@
<?php namespace Visiosoft\LocationModule\Http\Controller\Admin; <?php namespace Visiosoft\LocationModule\Http\Controller\Admin;
use Visiosoft\LocationModule\City\CityModel;
use Visiosoft\LocationModule\Country\Form\CountryFormBuilder; use Visiosoft\LocationModule\Country\Form\CountryFormBuilder;
use Visiosoft\LocationModule\Country\Table\CountryTableBuilder; use Visiosoft\LocationModule\Country\Table\CountryTableBuilder;
use Anomaly\Streams\Platform\Http\Controller\AdminController; use Anomaly\Streams\Platform\Http\Controller\AdminController;
@ -16,13 +15,6 @@ class CountriesController extends AdminController
*/ */
public function index(CountryTableBuilder $table) public function index(CountryTableBuilder $table)
{ {
if($this->request->action == "delete") {
$city = new CityModel();
foreach ($this->request->id as $item)
{
$city->deleteCitiesByCountry($item);
}
}
return $table->render(); return $table->render();
} }

View File

@ -1,28 +1,20 @@
<?php namespace Visiosoft\LocationModule\Http\Controller\Admin; <?php namespace Visiosoft\LocationModule\Http\Controller\Admin;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Visiosoft\LocationModule\District\DistrictModel; use Visiosoft\LocationModule\District\Contract\DistrictRepositoryInterface;
use Visiosoft\LocationModule\District\Form\DistrictFormBuilder; use Visiosoft\LocationModule\District\Form\DistrictFormBuilder;
use Visiosoft\LocationModule\District\Table\DistrictTableBuilder; use Visiosoft\LocationModule\District\Table\DistrictTableBuilder;
use Anomaly\Streams\Platform\Http\Controller\AdminController; use Anomaly\Streams\Platform\Http\Controller\AdminController;
use Visiosoft\LocationModule\Neighborhood\NeighborhoodModel;
class DistrictsController extends AdminController class DistrictsController extends AdminController
{ {
public function index(DistrictTableBuilder $table, Request $request) public function index(DistrictTableBuilder $table, Request $request)
{ {
if($this->request->action == "delete") { $districts = app(DistrictRepositoryInterface::class);
$neighborhoods = new NeighborhoodModel();
foreach ($this->request->id as $item)
{
$neighborhoods->deleteNeighborhoodByDistrict($item);
}
}
$districts = new DistrictModel();
if(!isset($request->city) || $request->city==""){ if(!isset($request->city) || $request->city==""){
return $table->render(); return $table->render();
}else{ }else{
$districts = $districts->getSubDistricts($request->city); $districts = $districts->getDistrictByCityId($request->city);
if (count($districts) == 0) { if (count($districts) == 0) {
$this->messages->error('Selected city has no related district.'); $this->messages->error('Selected city has no related district.');
return redirect('/admin/location/cities'); return redirect('/admin/location/cities');

View File

@ -1,28 +1,20 @@
<?php namespace Visiosoft\LocationModule\Http\Controller\Admin; <?php namespace Visiosoft\LocationModule\Http\Controller\Admin;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Visiosoft\LocationModule\Neighborhood\Contract\NeighborhoodRepositoryInterface;
use Visiosoft\LocationModule\Neighborhood\Form\NeighborhoodFormBuilder; use Visiosoft\LocationModule\Neighborhood\Form\NeighborhoodFormBuilder;
use Visiosoft\LocationModule\Neighborhood\NeighborhoodModel;
use Visiosoft\LocationModule\Neighborhood\Table\NeighborhoodTableBuilder; use Visiosoft\LocationModule\Neighborhood\Table\NeighborhoodTableBuilder;
use Anomaly\Streams\Platform\Http\Controller\AdminController; use Anomaly\Streams\Platform\Http\Controller\AdminController;
use Visiosoft\LocationModule\Village\VillageModel;
class NeighborhoodsController extends AdminController class NeighborhoodsController extends AdminController
{ {
public function index(NeighborhoodTableBuilder $table, Request $request) public function index(NeighborhoodTableBuilder $table, Request $request)
{ {
if($this->request->action == "delete") { $neighborhoods = app(NeighborhoodRepositoryInterface::class);
$village = new VillageModel();
foreach ($this->request->id as $item)
{
$village->deleteVillageByNeighborhood($item);
}
}
$neighborhoods = new NeighborhoodModel();
if(!isset($request->district) || $request->district==""){ if(!isset($request->district) || $request->district==""){
return $table->render(); return $table->render();
}else{ }else{
$neighborhoods = $neighborhoods->getSubNeighborhoods($request->district); $neighborhoods = $neighborhoods->getNeighborhoodsByDistrictId($request->district);
if (count($neighborhoods) == 0) { if (count($neighborhoods) == 0) {
$this->messages->error('Selected district has no related neighborhood.'); $this->messages->error('Selected district has no related neighborhood.');
return redirect('/admin/location/districts'); return redirect('/admin/location/districts');

View File

@ -1,20 +1,21 @@
<?php namespace Visiosoft\LocationModule\Http\Controller\Admin; <?php namespace Visiosoft\LocationModule\Http\Controller\Admin;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Visiosoft\LocationModule\Village\Contract\VillageRepositoryInterface;
use Visiosoft\LocationModule\Village\Form\VillageFormBuilder; use Visiosoft\LocationModule\Village\Form\VillageFormBuilder;
use Visiosoft\LocationModule\Village\Table\VillageTableBuilder; use Visiosoft\LocationModule\Village\Table\VillageTableBuilder;
use Anomaly\Streams\Platform\Http\Controller\AdminController; use Anomaly\Streams\Platform\Http\Controller\AdminController;
use Visiosoft\LocationModule\Village\VillageModel;
class VillageController extends AdminController class VillageController extends AdminController
{ {
public function index(VillageTableBuilder $table, Request $request) public function index(VillageTableBuilder $table, Request $request)
{ {
$villages = new VillageModel(); $villages = app(VillageRepositoryInterface::class);
if(!isset($request->neighborhood) || $request->neighborhood==""){ if(!isset($request->neighborhood) || $request->neighborhood==""){
return $table->render(); return $table->render();
}else{ }else{
$villages = $villages->getSubVillages($request->neighborhood); $villages = $villages->getVillagesByNeighborhoodId($request->neighborhood);
if (count($villages) == 0) { if (count($villages) == 0) {
$this->messages->error('Selected neighborhood has no related village.'); $this->messages->error('Selected neighborhood has no related village.');
return back(); return back();

View File

@ -8,10 +8,14 @@ use Anomaly\Streams\Platform\Model\Location\LocationVillageEntryModel;
use Visiosoft\LocationModule\City\CityModel; use Visiosoft\LocationModule\City\CityModel;
use Visiosoft\LocationModule\City\CityRepository; use Visiosoft\LocationModule\City\CityRepository;
use Visiosoft\LocationModule\City\Contract\CityRepositoryInterface; use Visiosoft\LocationModule\City\Contract\CityRepositoryInterface;
use Visiosoft\LocationModule\City\Events\DeletedCities;
use Visiosoft\LocationModule\Country\Events\DeletedCountry;
use Visiosoft\LocationModule\District\Contract\DistrictRepositoryInterface; use Visiosoft\LocationModule\District\Contract\DistrictRepositoryInterface;
use Visiosoft\LocationModule\District\DistrictModel; use Visiosoft\LocationModule\District\DistrictModel;
use Visiosoft\LocationModule\District\DistrictRepository; use Visiosoft\LocationModule\District\DistrictRepository;
use Visiosoft\LocationModule\District\Events\DeletedDistricts;
use Visiosoft\LocationModule\Neighborhood\Contract\NeighborhoodRepositoryInterface; use Visiosoft\LocationModule\Neighborhood\Contract\NeighborhoodRepositoryInterface;
use Visiosoft\LocationModule\Neighborhood\Events\DeletedNeighborhoods;
use Visiosoft\LocationModule\Neighborhood\NeighborhoodModel; use Visiosoft\LocationModule\Neighborhood\NeighborhoodModel;
use Visiosoft\LocationModule\Neighborhood\NeighborhoodRepository; use Visiosoft\LocationModule\Neighborhood\NeighborhoodRepository;
use Visiosoft\LocationModule\Village\Contract\VillageRepositoryInterface; use Visiosoft\LocationModule\Village\Contract\VillageRepositoryInterface;
@ -85,4 +89,19 @@ class LocationModuleServiceProvider extends AddonServiceProvider
VillageRepositoryInterface::class => VillageRepository::class, VillageRepositoryInterface::class => VillageRepository::class,
CountryRepositoryInterface::class => CountryRepository::class, CountryRepositoryInterface::class => CountryRepository::class,
]; ];
protected $listeners = [
DeletedCountry::class => [
\Visiosoft\LocationModule\City\Listeners\DeletedCountry::class,
],
DeletedCities::class => [
\Visiosoft\LocationModule\District\Listeners\DeletedCities::class,
],
DeletedDistricts::class => [
\Visiosoft\LocationModule\Neighborhood\Listeners\DeletedDistricts::class,
],
DeletedNeighborhoods::class => [
\Visiosoft\LocationModule\Village\Listeners\DeletedNeighborhoods::class
],
];
} }

View File

@ -5,4 +5,6 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryRepositoryInterface;
interface NeighborhoodRepositoryInterface extends EntryRepositoryInterface interface NeighborhoodRepositoryInterface extends EntryRepositoryInterface
{ {
public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc'); public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc');
public function getNeighborhoodsByDistrictId($district);
} }

View File

@ -0,0 +1,17 @@
<?php namespace Visiosoft\LocationModule\Neighborhood\Events;
class DeletedNeighborhoods
{
private $neighborhood;
public function __construct($neighborhood)
{
$this->neighborhood = $neighborhood;
}
public function getNeighborhoods()
{
return $this->neighborhood;
}
}

View File

@ -0,0 +1,31 @@
<?php namespace Visiosoft\LocationModule\Neighborhood\Listeners;
use Visiosoft\LocationModule\Neighborhood\Contract\NeighborhoodRepositoryInterface;
use Visiosoft\LocationModule\Neighborhood\Events\DeletedNeighborhoods;
class DeletedDistricts
{
public $neighborhoodRepository;
public function __construct(NeighborhoodRepositoryInterface $neighborhoodRepository)
{
$this->neighborhoodRepository = $neighborhoodRepository;
}
public function handle(\Visiosoft\LocationModule\District\Events\DeletedDistricts $event)
{
$districts = $event->getDistricts();
$districts = $districts->pluck('id')->all();
$query = $this->neighborhoodRepository->newQuery()
->whereIn('parent_district_id', $districts);
if (count($neighborhoods = $query->get())) {
$query->delete();
event(new DeletedNeighborhoods($neighborhoods));
}
}
}

View File

@ -2,26 +2,8 @@
use Visiosoft\LocationModule\Neighborhood\Contract\NeighborhoodInterface; use Visiosoft\LocationModule\Neighborhood\Contract\NeighborhoodInterface;
use Anomaly\Streams\Platform\Model\Location\LocationNeighborhoodsEntryModel; use Anomaly\Streams\Platform\Model\Location\LocationNeighborhoodsEntryModel;
use Visiosoft\LocationModule\Village\VillageModel;
class NeighborhoodModel extends LocationNeighborhoodsEntryModel implements NeighborhoodInterface class NeighborhoodModel extends LocationNeighborhoodsEntryModel implements NeighborhoodInterface
{ {
public function getNeighborhoods() {
return NeighborhoodModel::all();
}
public function getSubNeighborhoods($district) {
return $this->query()->where('parent_district_id', $district)->orderBy('order','ASC')->get();
}
public function deleteNeighborhoodByDistrict($id) {
$village = new VillageModel();
$neighborhood = $this->where('parent_district_id',$id);
$neighborhoods_id = $neighborhood->orderBy('id','DESC')->get();
foreach ($neighborhoods_id as $item)
{
$village->deleteVillageByNeighborhood($item->id);
}
return $neighborhood->delete();
}
} }

View File

@ -46,4 +46,12 @@ class NeighborhoodRepository extends EntryRepository implements NeighborhoodRepo
->orderBy($orderBy, $direction) ->orderBy($orderBy, $direction)
->get(); ->get();
} }
public function getNeighborhoodsByDistrictId($district)
{
return $this->newQuery()
->where('parent_district_id', $district)
->orderBy('order', 'ASC')
->get();
}
} }

View File

@ -0,0 +1,24 @@
<?php namespace Visiosoft\LocationModule\Neighborhood\Table\Handler;
use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler;
use Visiosoft\LocationModule\Neighborhood\Contract\NeighborhoodRepositoryInterface;
use Visiosoft\LocationModule\Neighborhood\Events\DeletedNeighborhoods;
class Delete extends ActionHandler
{
public function handle(NeighborhoodRepositoryInterface $repository, array $selected)
{
$query = $repository->newQuery()->whereIn('location_neighborhoods.id', $selected);
if ($count = count($cities = $query->get())) {
$query->delete();
event(new DeletedNeighborhoods($cities));
}
if ($selected && $count > 0) {
$this->messages->success(trans('streams::message.delete_success', compact('count')));
}
}
}

View File

@ -1,6 +1,7 @@
<?php namespace Visiosoft\LocationModule\Neighborhood\Table; <?php namespace Visiosoft\LocationModule\Neighborhood\Table;
use Anomaly\Streams\Platform\Ui\Table\TableBuilder; use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
use Visiosoft\LocationModule\Neighborhood\Table\Handler\Delete;
class NeighborhoodTableBuilder extends TableBuilder class NeighborhoodTableBuilder extends TableBuilder
{ {
@ -60,7 +61,9 @@ class NeighborhoodTableBuilder extends TableBuilder
* @var array|string * @var array|string
*/ */
protected $actions = [ protected $actions = [
'delete' 'delete' => [
'handler' => Delete::class,
],
]; ];
/** /**

View File

@ -5,4 +5,6 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryRepositoryInterface;
interface VillageRepositoryInterface extends EntryRepositoryInterface interface VillageRepositoryInterface extends EntryRepositoryInterface
{ {
public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc'); public function getByEntryIDsAndOrderByTransCol($entryIDs, $orderBy, $direction = 'asc');
public function getVillagesByNeighborhoodId($neighborhood);
} }

View File

@ -0,0 +1,27 @@
<?php namespace Visiosoft\LocationModule\Village\Listeners;
use Visiosoft\LocationModule\Village\Contract\VillageRepositoryInterface;
class DeletedNeighborhoods
{
public $villageRepository;
public function __construct(VillageRepositoryInterface $villageRepository)
{
$this->villageRepository = $villageRepository;
}
public function handle(\Visiosoft\LocationModule\Neighborhood\Events\DeletedNeighborhoods $event)
{
$neighborhoods = $event->getNeighborhoods();
$neighborhoods = $neighborhoods->pluck('id')->all();
$query = $this->villageRepository->newQuery()
->whereIn('parent_neighborhood_id', $neighborhoods);
if (count($villages = $query->get())) {
$query->delete();
}
}
}

View File

@ -5,15 +5,5 @@ use Anomaly\Streams\Platform\Model\Location\LocationVillageEntryModel;
class VillageModel extends LocationVillageEntryModel implements VillageInterface class VillageModel extends LocationVillageEntryModel implements VillageInterface
{ {
public function getVillages() {
return VillageModel::all();
}
public function getSubVillages($neighborhood) {
return $this->query()->where('parent_neighborhood_id', $neighborhood)->orderBy('order','ASC')->get();
}
public function deleteVillageByNeighborhood($id) {
$this->where('parent_neighborhood_id',$id)->orderBy('id','DESC')->delete();
}
} }

View File

@ -46,4 +46,9 @@ class VillageRepository extends EntryRepository implements VillageRepositoryInte
->orderBy($orderBy, $direction) ->orderBy($orderBy, $direction)
->get(); ->get();
} }
public function getVillagesByNeighborhoodId($neighborhood)
{
return $this->newQuery()->where('parent_neighborhood_id', $neighborhood)->orderBy('order','ASC')->get();
}
} }