145 lines
6.6 KiB
PHP
145 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Classification;
|
|
use App\Models\SubClassification;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use SebastianBergmann\Type\TrueType;
|
|
use Yajra\DataTables\DataTables;
|
|
|
|
class DatatableController extends Controller
|
|
{
|
|
public function classification(Request $request){
|
|
if ($request->ajax()) {
|
|
|
|
|
|
|
|
$data = Classification::with(['subClassifications'])->get();
|
|
|
|
return DataTables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('status', function ($data) {
|
|
if ($data->status == '0') {
|
|
return '<span class="badge bg-danger" style="display:inline-block;font-size:0.8rem;">Nonaktif</span>';
|
|
}else{
|
|
return '<span class="badge bg-primary" style="display:inline-block;font-size:0.8rem;">Aktif</span>';
|
|
}
|
|
})
|
|
->addColumn('sub_classifications', function ($data) {
|
|
$subClassificationCount = $data->subClassifications->count();
|
|
|
|
if($subClassificationCount > 0){
|
|
return $subClassificationCount. ' [<a href="'.route('dashboard.sub.classifications.index', ['classification' => $data->slug]).'">Lihat</a>]';
|
|
}else{
|
|
return $subClassificationCount;
|
|
}
|
|
|
|
})
|
|
->addColumn('action', function ($data) {
|
|
$showUrl = route('dashboard.classifications.show', ['id' => encryptId($data->id)]);
|
|
|
|
$addSubClassificationUrl = route('dashboard.sub.classifications.create', ['classification' => $data->slug]);
|
|
|
|
$editUrl = route('dashboard.classifications.edit', ['id' => encryptId($data->id)]);
|
|
|
|
$deleteUrl = route('dashboard.classifications.destroy', ['id' => encryptId($data->id)]);
|
|
|
|
$dropdown = '<div class="dropdown" style="display:flex; justify-content:center;">
|
|
<a class="dropdown-toggle btn btn-icon btn-light" data-bs-toggle="dropdown"><em class="icon ni ni-more-v"></em></a>
|
|
<div class="dropdown-menu dropdown-menu-end">
|
|
<ul class="link-list-opt">
|
|
<li><a href="'.$showUrl.'"><em class="icon ni ni-eye"></em><span>Detail</span></a></li>';
|
|
|
|
if (is_role([1])) {
|
|
$dropdown .= '<li><a href="'.$addSubClassificationUrl.'"><em class="icon ni ni-plus"></em><span>Tambah Sub Klasifikasi</span></a></li>';
|
|
}
|
|
|
|
if (is_role([1])) {
|
|
$dropdown .= '<li><a href="'.$editUrl.'"><em class="icon ni ni-edit"></em><span>Ubah</span></a></li>';
|
|
}
|
|
|
|
if (is_role([1])) {
|
|
$dropdown .= '<li><a href="javascript:void(0);" onclick="deleteItem(\''.$deleteUrl.'\', \'Apakah Anda yakin ingin menghapus klasifikasi '.$data->name.'? Item ini akan dihapus secara permanen!\')"><em class="icon ni ni-trash"></em><span>Hapus</span></a></li>';
|
|
|
|
}
|
|
|
|
$dropdown .= '</ul>
|
|
</div>
|
|
</div>';
|
|
|
|
return $dropdown;
|
|
})
|
|
|
|
->rawColumns(['sub_classifications', 'status', 'action'])
|
|
->make(true);
|
|
}
|
|
|
|
return response()->json(['error' => 'Unauthorized'], 401);
|
|
|
|
}
|
|
|
|
public function subClassification(Request $request){
|
|
if ($request->ajax()) {
|
|
|
|
$classificationParam = $request->input('classification', null);
|
|
|
|
if($classificationParam){
|
|
$classification = Classification::where('slug', '=', $classificationParam)->first();
|
|
$data = SubClassification::with(['classification'])->where('classification_id', '=', $classification->id)->get();
|
|
}else{
|
|
$data = SubClassification::with(['classification'])->get();
|
|
}
|
|
|
|
return DataTables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('classification', function ($data) {
|
|
return $data->classification->name;
|
|
})
|
|
->addColumn('status', function ($data) {
|
|
if ($data->status == '0') {
|
|
return '<span class="badge bg-danger" style="display:inline-block;font-size:0.8rem;">Nonaktif</span>';
|
|
}else{
|
|
return '<span class="badge bg-primary" style="display:inline-block;font-size:0.8rem;">Aktif</span>';
|
|
}
|
|
})
|
|
->addColumn('action', function ($data) {
|
|
$showUrl = route('dashboard.sub.classifications.show', ['id' => encryptId($data->id)]);
|
|
|
|
$editUrl = route('dashboard.sub.classifications.edit', ['id' => encryptId($data->id)]);
|
|
|
|
$deleteUrl = route('dashboard.sub.classifications.destroy', ['id' => encryptId($data->id)]);
|
|
|
|
$dropdown = '<div class="dropdown" style="display:flex; justify-content:center;">
|
|
<a class="dropdown-toggle btn btn-icon btn-light" data-bs-toggle="dropdown"><em class="icon ni ni-more-v"></em></a>
|
|
<div class="dropdown-menu dropdown-menu-end">
|
|
<ul class="link-list-opt">
|
|
<li><a href="'.$showUrl.'"><em class="icon ni ni-eye"></em><span>Detail</span></a></li>';
|
|
|
|
if (is_role([1])) {
|
|
$dropdown .= '<li><a href="'.$editUrl.'"><em class="icon ni ni-edit"></em><span>Ubah</span></a></li>';
|
|
}
|
|
|
|
if (is_role([1])) {
|
|
$dropdown .= '<li><a href="javascript:void(0);" onclick="deleteItem(\''.$deleteUrl.'\', \'Apakah Anda yakin ingin menghapus sub klasifikasi '.$data->name.'? Item ini akan dihapus secara permanen!\')"><em class="icon ni ni-trash"></em><span>Hapus</span></a></li>';
|
|
|
|
}
|
|
|
|
$dropdown .= '</ul>
|
|
</div>
|
|
</div>';
|
|
|
|
return $dropdown;
|
|
})
|
|
|
|
->rawColumns(['status', 'action'])
|
|
->make(true);
|
|
}
|
|
|
|
return response()->json(['error' => 'Unauthorized'], 401);
|
|
|
|
}
|
|
}
|