Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.1 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\UserToVersionGantt;
use App\Models\ShowHideColumn;
class ShowHideColumnController extends Controller
{
public function getByUserGantt($gantt_id)
{
$user_id = $this->currentId;
if($user_id > 0){
$cekData = UserToVersionGantt::where("version_gantt_id", $gantt_id)->where("user_id", $user_id)->count();
if($cekData <=0){
$data = array(
"status"=>"not have access"
);
}else{
$cekAvailable = ShowHideColumn::where("version_gantt_id", $gantt_id)->where("user_id", $user_id)->count();
if($cekAvailable > 0){
$data = ShowHideColumn::where("version_gantt_id", $gantt_id)->where("user_id", $user_id)->get();
}else{
$data = array(
"status"=>"not yet have"
);
}
}
return response()->json(['status'=>'success','code'=>200,'data'=>$data], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get list user to version gantt, please try again later!','code'=>400], 400);
}
}
public function add(Request $request)
{
$this->validate($request, [
'version_gantt_id' => 'required',
'columns'=> 'required'
]);
$columns = $request->columns;
$success = 0;
foreach ($columns as $column) {
$dataAdd = array(
'version_gantt_id'=>$request->version_gantt_id,
'user_id'=>$this->currentId,
'column_name'=>$column,
'show'=>true,
'created_by'=>$this->currentName
);
$result = ShowHideColumn::create($dataAdd);
if($result){
$success++;
}else{
$success--;
}
}
if($success > 0){
return response()->json(['status'=>'success','message'=>'add show hide column successfully!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'add show hide column failed!','code'=>400], 400);
}
}
public function update(Request $request, $gantt_id)
{
$this->validate($request, [
'columns'=> 'required'
]);
if(!$gantt_id || (int) $gantt_id < 0 || $gantt_id==""){
return response()->json(['status'=>'failed','message'=>'gantt id is required!','code'=>400], 400);
}
$user_id = $this->currentId;
$columns = $request->columns;
$success = 0;
foreach ($columns as $column => $show) {
$check = ShowHideColumn::where("column_name", $column)->where("version_gantt_id", $gantt_id)->where("user_id", $user_id)->count();
if($check > 0){
$dataUpdate = array(
"show"=>$show
);
$result = ShowHideColumn::where("column_name", $column)->where("version_gantt_id", $gantt_id)->where("user_id", $user_id)
->update($dataUpdate);
}else{
$dataNew = array(
'version_gantt_id'=>$gantt_id,
'user_id'=>$user_id,
'column_name'=>$column,
'show'=>$show,
'created_by'=>$this->currentName
);
$result = ShowHideColumn::create($dataNew);
}
if($result){
$success++;
}else{
$success--;
}
}
if($success > 0){
return response()->json(['status'=>'success','message'=>'data show hide column successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data show hide column failed updated!','code'=>400], 400);
}
}
}