chess/app/Http/Controllers/RefController.php

396 lines
14 KiB
PHP
Raw Permalink Normal View History

2024-08-29 03:56:32 +00:00
<?php
namespace App\Http\Controllers;
use App\Models\ModelingTaskModel;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class RefController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function getInit($request,$route)
{
$modelName = '\\App\Models\\'.$route.'Model';
$model = new $modelName;
$data = $model->initData($request,$route);
return $data;
}
public function index(Request $request)
{
//INIT
$init = $this->getInit($request,$request->get('type'));
$route = $init['route'];
$param['title'] = $init['title'];
$param['tableHead'] = $init['tableHead'];
$param['head'] = $init['head'];
$param['detail'] = empty($init['detail'])?false:true;
if (isset($init['headAlias'])) {
$param['headAlias'] = $init['headAlias'];
}else{
$param['headAlias'] = null;
}
$param['bisaAdd'] = $init['bisaAdd'];
$param['adaDetail'] = $init['adaDetail'];
$view = 'content.ref.index';
//END INIT
$param['table'] = route('ref.data')."?type=".$route;
$param['delete'] = route('ref.delete')."?type=".$route;
$param['edit'] = route('ref.edit')."?type=".$route;
$param['store'] = route('ref.store')."?type=".$route;
$param['storeCustom'] = route('ref.storeCustom')."?type=".$route;
// $param['setActive'] = route('ref.setActive')."?type=".$route;
$param['vForm'] = 'content.ref.'.$route.'.form';
$param['vAction'] = 'content.ref.'.$route.'.action';
$param['vFormDet'] = 'content.ref.'.$route.'.formDet';
$param['init'] = $init;
// return view('home')->nest('child',$view, $param);
return $this->bsGetView($view, $param);
}
public function data(Request $request)
{
$init = $this->getInit($request,$request->get('type'));
$data = $init['data_table'];
$modelName = '\\App\Models\\'.$init['route'].'Model';
$model = new $modelName;
$dt = $model->getDT($data,$init);
$dt->addColumn('action', function ($data) use($init) {
if(is_array($init['db_key'])) {
if(count($init['db_key'])>1) {
$keyCustom = '';
foreach($init['db_key'] as $i => $dbKey) {
if(($i+1)==count($init['db_key'])) {
$keyCustom.= $data->{$dbKey};
} else {
$keyCustom.= $data->{$dbKey}.'|';
}
}
$param['id_key'] = $keyCustom;
}
} else {
$param['id_key'] = $data->{$init['db_key']};
}
$param['data'] = $data;
//SET BUTTON
return $this->getButtonAction($init['actButton'],$param);
});
$dt->rawColumns(['is_active','created_at','updated_at','action']);
return $dt->make(true);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
//GET INIT
DB::beginTransaction();
try {
//code...
$init = $this->getInit($request,$request->get('type'));
$get_id = $request->input('get_id');
$setField = $init['set_field'];
$timestamps = $init['timestamps'];
//IF EDIT DATA
if ($get_id) {
if ($timestamps) {
$setField['updated_at'] = date('Y-m-d H:i:s');
}
if(is_array($init['db_key'])) {
if(count($init['db_key'])>1) {
$data = DB::table($init['db'])->where(function($query) use ($request, $init, $get_id) {
$primaryKey = explode('|',$get_id);
foreach($init['db_key'] as $k => $i) {
$query->where($i, $primaryKey[$k]);
}
})->update($setField);
}
} else {
DB::table($init['db'])->where($init['db_key'], $get_id)->update($setField);
}
//IF ADD DATA
}else {
if ($timestamps) {
$setField['created_at'] = date('Y-m-d H:i:s');
$setField['updated_at'] = date('Y-m-d H:i:s');
}
if ($init['serial']===false) {
$getId = DB::table($init['db'])->max('id');
$setField['id'] = $getId+1;
}
if ($init['seq']) {
$seq = DB::Table($init['db'])->max('seq');
$setField['seq'] = $seq+1;
}
DB::table($init['db'])->insert($setField);
}
DB::commit();
return response()->json([
'rc' => 0,
'rm' => "Berhasil"
]);
} catch (\Throwable $e) {
//throw $th;
DB::rollBack();
return response()->json([
'rc' => 99,
'data' => $e->getMessage()
]);
} catch (QueryException $e) {
DB::rollBack();
return response()->json([
'rc' => 500,
'data' => $e->getMessage()
]);
}
}
public function storeCustom(Request $request)
{
$init = $this->getInit($request,$request->get('type'));
$modelName = '\\App\Models\\'.$init['route'].'Model';
$model = new $modelName;
return $model->storeCustom($request);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Request $request)
{
//
$init = $this->getInit($request,$request->get('type'));
return response()->json([
'rc' => 0,
'data' => $init['get_data_edit']
]);
}
public function delete(Request $request)
{
$init = $this->getInit($request,$request->get('type'));
$get_id = $request->input('id');
DB::beginTransaction();
try {
//code...
if(is_array($init['db_key'])) {
if(count($init['db_key'])>1) {
$data = DB::table($init['db'])->where(function($query) use ($request, $init, $get_id) {
$primaryKey = explode('|',$get_id);
foreach($init['db_key'] as $k => $i) {
$query->where($i, $primaryKey[$k]);
}
})->delete();
}
} else {
if($request->get('type')=='Model') {
ModelingTaskModel::where('model_id', $get_id)->delete();
}
DB::table($init['db'])->where($init['db_key'], $get_id)->delete();
}
DB::commit();
return response()->json([
'rc' => 0,
'rm' => 'Data Berhasil Dihapus'
]);
} catch (\Throwable $e) {
DB::rollBack();
if($e->getCode()=='23503') {
return response()->json([
'rc' => 99,
'rm' => 'Data Tidak Dapat Dihapus Karena Memiliki Relasi Dengan Data Lain!'
]);
} else {
return response()->json([
'rc' => 500,
'rm' => $e->getMessage()
]);
}
} catch (QueryException $e) {
DB::rollBack();
if($e->getCode()=='23503') {
return response()->json([
'rc' => 99,
'rm' => 'Data Tidak Dapat Dihapus Karena Memiliki Relasi Dengan Data Lain!'
]);
} else {
return response()->json([
'rc' => 500,
'rm' => $e->getMessage()
]);
}
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
public function getButtonAction($typeButton,$param)
{
$editBtn ="";
$detailBtn ="";
$ActiveBtn = "";
$mapsBtn = "";
$hapusBtn = "";
//MAP BUTTON
if ( in_array("maps", $typeButton)) {
$mapsBtn = ' <li class="navi-item">
<a class="navi-link popup-gmaps" href="https://maps.google.com/maps?q='.$param['data']->latitude.','.$param['data']->longitude.'&hl=es&z=10&amp;output=embed">
<span class="navi-icon"><i class="fa fa-map-marker"></i></span>
<span class="navi-text"> Lihat Lokasi</span>
</a>
</li>
';
}
// DETAIL BUTTON
if ( in_array("detail", $typeButton)) {
$detailBtn = '<li class="navi-item">
<a onclick="detail(`'.$param['id_key'].'`)" href="#" class="navi-link">
<span class="navi-icon"><i class="la la-arrows-alt"></i></span>
<span class="navi-text"> Detail</span>
</a>
</li>';
}
// EDIT BUTTON
if (in_array("edit", $typeButton)) {
$editBtn = ' <li class="navi-item">
<a onclick="edit(`'.$param['id_key'].'`)" href="#" class="navi-link">
<span class="navi-icon"><i class="la la-edit"></i></span>
<span class="navi-text"> Edit</span>
</a>
</li>';
}
// HAPUS BUTTON
if (in_array("hapus", $typeButton)) {
$hapusBtn = '<li class="navi-item">
<a onclick="hapus(`'.$param['id_key'].'`)" href="#" class="navi-link">
<span class="navi-icon"><i class="la la-trash"></i></span>
<span class="navi-text"> Hapus</span>
</a>
</li>';
}
// ACTIVE BUTTON
if (in_array("active", $typeButton)) {
if ($param['data']->is_active == 't') {
$ActiveBtn = '
<li class="navi-item">
<a onclick="setActive(`'.$param['id_key'].'`,`'.$param['data']->is_active.'`)" href="#" class="navi-link">
<span class="navi-icon"><i class="la la-times-circle-o"></i> </span>
<span class="navi-text">Deactived</span>
</a>
</li>';
}else{
$ActiveBtn = '
<li class="navi-item">
<a onclick="setActive(`'.$param['id_key'].'`,`'.$param['data']->is_active.'`)" href="#" class="navi-link">
<span class="navi-icon"><i class="la la-check-circle-o"></i> </span>
<span class="navi-text">Active</span>
</a>
</li>';
}
}
$resultBtn = $editBtn.$ActiveBtn.$detailBtn.$mapsBtn.$hapusBtn;
return '
<div class="dropdown dropdown-inline">
<a href="javascript:;" class="btn btn-sm btn-clean btn-icon mr-2" data-toggle="dropdown">
<span class="svg-icon svg-icon-md">
<svg width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<rect x="0" y="0" width="24" height="24"/>
<path d="M5,8.6862915 L5,5 L8.6862915,5 L11.5857864,2.10050506 L14.4852814,5 L19,5 L19,9.51471863 L21.4852814,12 L19,14.4852814 L19,19 L14.4852814,19 L11.5857864,21.8994949 L8.6862915,19 L5,19 L5,15.3137085 L1.6862915,12 L5,8.6862915 Z M12,15 C13.6568542,15 15,13.6568542 15,12 C15,10.3431458 13.6568542,9 12,9 C10.3431458,9 9,10.3431458 9,12 C9,13.6568542 10.3431458,15 12,15 Z" fill="#000000"/>
</g>
</svg>
</span>
</a>
<div class="dropdown-menu dropdown-menu-sm dropdown-menu-right">
<ul class="navi flex-column navi-hover py-2">
<li class="navi-header font-weight-bolder text-uppercase font-size-xs text-primary pb-2">
Choose an action:
</li>
'.$resultBtn.'
</ul>
</div>
</div>
';
}
}