efService = $efService; } public static function middleware(): array { return [ //new Middleware('permission:/setting/ef'), ]; } public function index(Request $request) { if ($request->ajax()) { } $data['activities'] = $this->efService->getActivities(); $data['categories'] = $this->efService->getCategories(); $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.index', $data); } public function getCategoriesByActivity(Request $request) { $activityCode = $request->input('activity_code'); $categories = $this->efService->getCategories($activityCode); return response()->json($categories); } public function create() { $data['activities'] = $this->efService->getActivities(); $data['efSources'] = $this->efService->getEfSources(); $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.create', $data); } public function store(Request $request) { $request->validate([ 'activity_code' => 'required', 'category' => 'required', 'ef_source_code' => [ 'required', new ExistInSchema('reference', 'ef_source', 'code') ], 'description' => 'nullable', 'tag_1' => 'nullable|string', 'tag_2' => 'nullable|string', 'tag_3' => 'nullable|string', ]); try { $request['value'] = getOriginalValue($request['value']); $this->efService->create($request->all()); return redirect()->route($this->route.'.index')->with('success', 'EF berhasil ditambahkan.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'EF gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function edit($id) { $data['ef'] = $this->efService->find($id); $data['activities'] = $this->efService->getActivities(); $data['efSources'] = $this->efService->getEfSources(); $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.edit', $data); } public function update(Request $request, ReferenceEf $ef) { $request->validate([ 'activity_code' => 'required', 'category' => 'required', 'ef_source_code' => [ 'required', new ExistInSchema('reference', 'ef_source', 'code') ], 'value' => 'required', 'description' => 'nullable', 'tag_1' => 'nullable|string', 'tag_2' => 'nullable|string', 'tag_3' => 'nullable|string', ]); try { $request['value'] = getOriginalValue($request['value']); $this->efService->update($ef, $request->all()); return redirect()->route($this->route.'.index')->with('success', 'EF berhasil diperbarui.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'EF gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function destroy(ReferenceEf $ef) { try { $this->efService->delete($ef); return redirect()->route($this->route.'.index')->with('success', 'Unit berhasil dihapus.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'Unit gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function storeEfSource(Request $request) { $request->validate([ 'code' => [ 'required', 'string', 'max:255', new UniqueInSchema('reference', 'ef_source', 'code'), ], 'name' => 'required|string|max:255', 'description' => 'nullable|string', 'row_num' => 'required|numeric', ]); try { $efSource = [ 'code' => $request->input('code'), 'name' => $request->input('name'), 'description' => $request->input('description'), 'row_num' => $request->input('row_num'), ]; $this->efService->create($efSource, ReferenceEfSource::class); return response()->json($efSource, 201); } catch (\Exception $e) { return response()->json(['error' => 'Sumber Data EF gagal dibuat.'], 500); } } public function grid(Request $request) { $_data = []; $data = $this->efService->getAll(); $activityCodeFilter = $request->activityCodeFilter ?? null; $categoryFilter = $request->categoryFilter ?? null; if (!is_null($activityCodeFilter) && $activityCodeFilter != 'SEMUA AKTIVITAS') { $data = $data->where('activity_code', $activityCodeFilter); } if (!is_null($categoryFilter) && $categoryFilter != 'SEMUA KATEGORI') { $data = $data->where('category', $categoryFilter); } foreach ($data as $key => $row) { $btn = 'Edit'; $btn .= '
'; $btn .= csrf_field(); $btn .= method_field('DELETE'); $btn .= ''; $btn .= '
'; if ($row->active_status == 1) { $status = 'Aktif'; } else { $status = 'Tidak Aktif'; } $tags = array(); for ($i = 1; $i <= 3; $i++) { $col = $row->{'tag_' . $i}; // Correctly access the dynamic property if ($col && $col != '') { array_push($tags, '' . $col . ''); } } $_data[] = [ 'no' => $key+1, 'value' => $row->value ? getFormattedValue($row->value) : 0, 'activity' => $row->activity ? $row->activity->name : 'N/A', 'ef_resource' => $row->efSource ? $row->efSource->name : 'N/A', 'category' => $row->category, 'description' => $row->description, 'status' => $status, 'tags' => $tags, 'action' => @$btn, ]; } return response()->json($_data); } }