2023-06-04 14:09:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Master\Http\Controllers;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Routing\Controller;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Modules\Master\DataTables\CityDataTable;
|
|
|
|
use Modules\Master\Entities\City;
|
2023-06-07 01:14:39 +00:00
|
|
|
use Modules\Master\Entities\Country;
|
2023-06-04 14:09:39 +00:00
|
|
|
use Modules\Master\Http\Requests\City\StoreCityRequest;
|
|
|
|
use Modules\Master\Http\Requests\City\UpdateCityRequest;
|
|
|
|
|
|
|
|
class CityController extends Controller
|
|
|
|
{
|
|
|
|
protected $user;
|
|
|
|
protected $model = City::class;
|
|
|
|
protected $module;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware(function ($request, $next) {
|
|
|
|
$this->user = Auth::guard('web')->user();
|
|
|
|
return $next($request);
|
|
|
|
});
|
|
|
|
|
|
|
|
$module = file_get_contents(dirname(__FILE__, 3) . '/module.json');
|
|
|
|
$this->module = json_decode($module);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return Renderable
|
|
|
|
*/
|
2023-06-07 03:36:43 +00:00
|
|
|
public function index(CityDataTable $dataTable, Request $request)
|
2023-06-04 14:09:39 +00:00
|
|
|
{
|
|
|
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
|
|
|
|
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
|
|
|
|
}
|
|
|
|
|
2023-06-07 03:36:43 +00:00
|
|
|
if (isset($request->province_id) && !empty($request->province_id)) {
|
|
|
|
$this->show($request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-07 01:14:39 +00:00
|
|
|
addVendor('chained-select');
|
|
|
|
|
|
|
|
$countries = Country::all();
|
|
|
|
|
2023-06-07 16:21:33 +00:00
|
|
|
$_table = $dataTable->render($this->module->alias . '::city.index', compact('countries'));
|
|
|
|
if(!empty($_table->data)){
|
|
|
|
return $_table;
|
|
|
|
}
|
2023-06-04 14:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return Renderable
|
|
|
|
*/
|
|
|
|
public function store(StoreCityRequest $request)
|
|
|
|
{
|
|
|
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.create')) {
|
|
|
|
abort(403, 'Sorry !! You are Unauthorized to create any ' . $this->module->alias . ' !');
|
|
|
|
}
|
|
|
|
|
|
|
|
//Validate the request
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
|
|
|
// Store the City...
|
|
|
|
if ($validated) {
|
|
|
|
try {
|
|
|
|
$this->model::create($validated);
|
|
|
|
echo json_encode(['status' => 'success', 'message' => $this->module->name . ' city created successfully.']);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' city created failed.']);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' city created failed.']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return Renderable
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.create')) {
|
|
|
|
abort(403, 'Sorry !! You are Unauthorized to create any ' . $this->module->alias . ' !');
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return Renderable
|
|
|
|
*/
|
2023-06-07 03:36:43 +00:00
|
|
|
public function show(Request $request)
|
2023-06-04 14:09:39 +00:00
|
|
|
{
|
|
|
|
if(is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
|
|
|
|
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
|
|
|
|
}
|
|
|
|
|
2023-06-07 03:36:43 +00:00
|
|
|
$cities = City::where('province_id', $request->province_id)->get();
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
foreach ($cities as $row) {
|
|
|
|
$result = [
|
|
|
|
$row->id => $row->name,
|
|
|
|
];
|
|
|
|
|
|
|
|
$data[] = $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo json_encode($data);
|
2023-06-04 14:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return Renderable
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.update')) {
|
|
|
|
abort(403, 'Sorry !! You are Unauthorized to update any ' . $this->module->alias . ' !');
|
|
|
|
}
|
|
|
|
|
2023-06-07 01:14:39 +00:00
|
|
|
$data = $this->model::with('province')->find($id);
|
2023-06-04 14:09:39 +00:00
|
|
|
echo json_encode($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return Renderable
|
|
|
|
*/
|
2023-06-07 01:14:39 +00:00
|
|
|
public function update(UpdateCityRequest $request, City $city)
|
2023-06-04 14:09:39 +00:00
|
|
|
{
|
|
|
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.update')) {
|
|
|
|
abort(403, 'Sorry !! You are Unauthorized to update any ' . $this->module->alias . ' !');
|
|
|
|
}
|
|
|
|
|
|
|
|
//Validate the request
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
|
|
|
// Update the City...
|
|
|
|
if ($validated) {
|
|
|
|
try {
|
2023-06-07 01:14:39 +00:00
|
|
|
$city->update($validated);
|
2023-06-04 14:09:39 +00:00
|
|
|
echo json_encode(['status' => 'success', 'message' => $this->module->name . ' city updated successfully.']);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' city updated failed.']);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' city updated failed.']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return Renderable
|
|
|
|
*/
|
2023-06-07 01:14:39 +00:00
|
|
|
public function destroy(City $city)
|
2023-06-04 14:09:39 +00:00
|
|
|
{
|
|
|
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.delete')) {
|
|
|
|
abort(403, 'Sorry !! You are Unauthorized to delete any ' . $this->module->alias . ' !');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-06-07 01:14:39 +00:00
|
|
|
$city->delete();
|
2023-06-04 14:09:39 +00:00
|
|
|
echo json_encode(['status' => 'success', 'message' => $this->module->name . ' city deleted successfully.']);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' city deleted failed.']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|