|
|
@ -7,10 +7,20 @@ use App\Models\Divisi; |
|
|
|
|
|
|
|
|
|
|
|
class DivisiController extends Controller |
|
|
|
class DivisiController extends Controller |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function getAllChildren($divisi, $depth = 0, $array = []) { |
|
|
|
|
|
|
|
$array[$divisi->id] = $divisi->name; |
|
|
|
|
|
|
|
foreach($divisi->children as $child){ |
|
|
|
|
|
|
|
$array = $this->getAllChildren($child, $depth + 1, $array); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return $array; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function add(Request $request){ |
|
|
|
public function add(Request $request){ |
|
|
|
$this->validate($request, [ |
|
|
|
$this->validate($request, [ |
|
|
|
'name' => 'required', |
|
|
|
'name' => 'required', |
|
|
|
'description' => 'required' |
|
|
|
'description' => 'string', |
|
|
|
|
|
|
|
'parent' => 'integer' |
|
|
|
]); |
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
$data = $request->all(); |
|
|
|
$data = $request->all(); |
|
|
@ -53,7 +63,6 @@ class DivisiController extends Controller |
|
|
|
|
|
|
|
|
|
|
|
$delete = $data->delete(); |
|
|
|
$delete = $data->delete(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!$delete) |
|
|
|
if(!$delete) |
|
|
|
return response()->json(['status'=>'failed','message'=> 'Delete failed!','code'=> 500], 500); |
|
|
|
return response()->json(['status'=>'failed','message'=> 'Delete failed!','code'=> 500], 500); |
|
|
|
|
|
|
|
|
|
|
@ -64,6 +73,9 @@ class DivisiController extends Controller |
|
|
|
{ |
|
|
|
{ |
|
|
|
$payload = $request->all(); |
|
|
|
$payload = $request->all(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if($payload['columns'][0]['value'] == "") |
|
|
|
|
|
|
|
$this->list(); |
|
|
|
|
|
|
|
|
|
|
|
$dataBuilder = $this->setUpPayload($payload, 'm_divisi'); |
|
|
|
$dataBuilder = $this->setUpPayload($payload, 'm_divisi'); |
|
|
|
$builder = $dataBuilder['builder']; |
|
|
|
$builder = $dataBuilder['builder']; |
|
|
|
$countBuilder = $dataBuilder['count']; |
|
|
|
$countBuilder = $dataBuilder['count']; |
|
|
@ -75,12 +87,16 @@ class DivisiController extends Controller |
|
|
|
|
|
|
|
|
|
|
|
public function list() |
|
|
|
public function list() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$data = Divisi::all(); |
|
|
|
$parentMenus = Divisi::whereNull('parent')->with('children')->get(); |
|
|
|
$countData = $data->count(); |
|
|
|
$divisions = []; |
|
|
|
|
|
|
|
foreach($parentMenus as $menu){ |
|
|
|
if(!$data) |
|
|
|
$childs = $this->getAllChildren($menu); |
|
|
|
|
|
|
|
$divisions = $divisions + $childs; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$countData = count($divisions); |
|
|
|
|
|
|
|
if($countData == 0) |
|
|
|
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); |
|
|
|
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); |
|
|
|
|
|
|
|
|
|
|
|
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); |
|
|
|
return response()->json(['status'=>'success','code'=>200,'data'=> $divisions, 'totalRecord'=> $countData], 200); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|