File manager - Edit - /var/www/payraty/hris-standalone/app/Http/Controllers/IndicatorController.php
Back
<?php namespace App\Http\Controllers; use App\Models\Branch; use App\Models\Employee; use App\Models\Indicator; use App\Models\Department; use App\Models\Designation; use App\Models\Competencies; use Illuminate\Http\Request; use App\Models\Performance_Type; use Illuminate\Support\Facades\Log; class IndicatorController extends Controller { public function index() { if (\Auth::user()->can('Manage Indicator')) { $user = \Auth::user(); if ($user->type == 'employee') { $employee = Employee::where('user_id', $user->id)->first(); $indicators = Indicator::where('created_by', '=', $user->creatorId())->where('branch', $employee->branch_id)->where('department', $employee->department_id)->where('designation', $employee->designation_id)->get(); } else { $indicators = Indicator::where('created_by', '=', $user->creatorId())->with(['branches', 'departments', 'designations', 'user'])->get(); } return view('indicator.index', compact('indicators')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function create() { if (\Auth::user()->can('Create Indicator')) { $performance_types = Performance_Type::with('types') ->where('created_by', \Auth::user()->creatorId()) ->get(); $competencies = Competencies::where('created_by', '=', \Auth::user()->creatorId())->get(); $brances = Branch::where('created_by', '=', \Auth::user()->creatorId())->get()->pluck('name', 'id'); $departments = Department::where('created_by', '=', \Auth::user()->creatorId())->get()->pluck('name', 'id'); $departments->prepend('Select Department', ''); $degisnation = Designation::where('created_by', '=', \Auth::user()->creatorId())->get()->pluck('name', 'id'); Log::info("herr", [$performance_types]); return view('indicator.create', compact('performance_types', 'brances', 'departments', 'degisnation', 'competencies')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function store(Request $request) { if (\Auth::user()->can('Create Indicator')) { $validator = \Validator::make( $request->all(), [ 'branch_id' => 'required', 'department_id' => 'required', 'designation_id' => 'required', 'rating' => 'required', ] ); if ($validator->fails()) { $messages = $validator->getMessageBag(); return redirect()->back()->with('error', $messages->first()); } $indicator = new Indicator(); $indicator->branch = $request->branch_id; $indicator->department = $request->department_id; $indicator->designation = $request->designation_id; $indicator->rating = json_encode($request->rating, true); if (\Auth::user()->type == 'company') { $indicator->created_user = \Auth::user()->creatorId(); } else { $indicator->created_user = \Auth::user()->id; } $indicator->created_by = \Auth::user()->creatorId(); $indicator->save(); return redirect()->route('indicator.index')->with('success', __('Indicator successfully created.')); } } public function show(Indicator $indicator) { $ratings = json_decode($indicator->rating, true); $ratingKeys = array_keys($ratings); $performance_types = Performance_Type::where('created_by', \Auth::user()->creatorId()) ->whereHas('types', function ($query) use ($ratingKeys) { $query->whereIn('id', $ratingKeys); }) ->with('types') ->get(); return view('indicator.show', compact('indicator', 'ratings', 'performance_types')); } public function edit(Indicator $indicator) { if (\Auth::user()->can('Edit Indicator')) { $ratings = json_decode($indicator->rating, true); $ratingKeys = array_keys($ratings); // $performance_types = Performance_Type::where('created_by', '=', \Auth::user()->creatorId())->get(); $performance_types = Performance_Type::where('created_by', \Auth::user()->creatorId()) ->whereHas('types', function ($query) use ($ratingKeys) { $query->whereIn('id', $ratingKeys); }) ->with('types') ->get(); $competencies = Competencies::where('created_by', '=', \Auth::user()->creatorId())->get(); $brances = Branch::where('created_by', '=', \Auth::user()->creatorId())->get(); $departments = Department::where('created_by', '=', \Auth::user()->creatorId())->get(); $degisnation = Designation::where('created_by', '=', \Auth::user()->creatorId())->get(); Log::info('performanc', [$performance_types]); Log::info('performanc', [$ratings]); return view('indicator.edit', compact('performance_types', 'brances', 'departments', 'indicator', 'ratings', 'degisnation', 'competencies')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function update(Request $request, Indicator $indicator) { if (\Auth::user()->can('Edit Indicator')) { $validator = \Validator::make( $request->all(), [ 'branch_id' => 'required|integer', 'department_id' => 'required|integer', 'designation_id' => 'required|integer', 'rating' => 'required|array', 'rating.*' => 'integer|min:1|max:5', ] ); if ($validator->fails()) { $messages = $validator->getMessageBag(); return redirect()->back()->with('error', $messages->first()); } $ratings = $request->input('rating', []); // Additional validation and sanitization if (!is_array($ratings) || empty($ratings)) { return redirect()->back()->with('error', 'Invalid or empty ratings data.'); } // Clean and validate ratings array $cleanRatings = []; foreach ($ratings as $key => $value) { // Ensure key is numeric and value is valid if (is_numeric($key) && is_numeric($value) && $value >= 1 && $value <= 5) { $cleanRatings[(string)$key] = (string)$value; } } if (empty($cleanRatings)) { return redirect()->back()->with('error', 'No valid ratings provided.'); } // Log for debugging \Log::info('Original ratings:', $ratings); \Log::info('Clean ratings:', $cleanRatings); // Check if JSON encoding is successful $encodedRatings = json_encode($cleanRatings, JSON_UNESCAPED_UNICODE); \Log::info('JSON encoded ratings:', [$cleanRatings]); if (json_last_error() !== JSON_ERROR_NONE) { \Log::error('JSON encoding error: ' . json_last_error_msg()); return redirect()->back()->with('error', 'Error processing ratings data.'); } $indicator->branch = $request->branch_id; $indicator->department = $request->department_id; $indicator->designation = $request->designation_id; $indicator->rating = $encodedRatings; // Use DB transaction to ensure data integrity \DB::beginTransaction(); try { $indicator->save(); \DB::commit(); // Verify the save was successful $indicator->refresh(); \Log::info('Saved ratings:', [$indicator->rating]); } catch (\Exception $e) { \DB::rollback(); \Log::error('Error saving indicator: ' . $e->getMessage()); return redirect()->back()->with('error', 'Error saving indicator data.'); } return redirect()->route('indicator.index')->with('success', __('Indicator successfully updated.')); } return redirect()->back()->with('error', 'Unauthorized.'); } public function destroy(Indicator $indicator) { if (\Auth::user()->can('Delete Indicator')) { if ($indicator->created_by == \Auth::user()->creatorId()) { $indicator->delete(); return redirect()->route('indicator.index')->with('success', __('Indicator successfully deleted.')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } else { return redirect()->back()->with('error', __('Permission denied.')); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.30 | Generation time: 0 |
proxy
|
phpinfo
|
Settings