/
home
/
maktab
/
server
/
app
/
Http
/
Controllers
/
api
/
File Upload :
llllll
Current File: /home/maktab/server/app/Http/Controllers/api/UserController.php
<?php namespace App\Http\Controllers\api; use App\Http\Controllers\Controller; use App\Models\Attendance; use App\Models\Cnt; use App\Models\Maktab; use App\Models\Student; use App\Models\StudentAttendance; use App\Models\User; use App\Models\user_info; use App\Models\Children; use App\Models\Users_to_maktab; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use App\Http\Controllers\api\PolUserController as PUC; use function PHPUnit\Framework\isEmpty; class UserController extends Controller { // public function me() // { // $user = User::with(['file', 'maktab', 'cloony', 'type_cnt',"children",'userInfo','users_to_maktab.type_cnt','users_to_maktab.maktab','users_to_maktab.cloony'])->select(['*', User::$SELECT_TYPE_STR])->findOrFail(auth()->id()); // $record = Users_to_maktab::where('user_id', $user->id) // ->where('maktab_id', $user->maktab_id) // ->where('type_employee', $user->type_employee) // ->where('type', $user->type) // ->first(); // if (!$record) { // $record = Users_to_maktab::create([ // 'user_id' => $user->id, // 'maktab_id' => $user->maktab_id, // 'type_employee' => $user->type_employee, // 'type' => $user->type, // 'active' => 1, // ]); // } // foreach ($user->users_to_maktab as $users_to_maktab ) // $maktabTitle = $user->maktab ? $user->maktab->title : ''; // use an empty string or a default value // $user->title = $user->name . " " . $user->type_str . " " . $maktabTitle; // $cnt = Cnt::where('value', 'مربی')->first(); // if (($user->type == 4 && $user->type_employee != $cnt->id)) { // $user['not_teacher'] = true; // } else { // $user['not_teacher'] = false; // } // // return response()->json($user); // // } public function me() { $user = User::with([ 'file', 'maktab', 'cloony', 'type_cnt', 'children', 'userInfo', 'users_to_maktab'=>function($query){ $query->select(['*',User::$SELECT_TYPE_STR]); }, 'users_to_maktab.type_cnt', 'users_to_maktab.maktab', 'users_to_maktab.cloony' ]) ->select(['*', User::$SELECT_TYPE_STR]) ->findOrFail(auth()->id()); // بررسی میکنیم که رکورد مربوط به users_to_maktab وجود داشته باشد $record = Users_to_maktab::where('user_id', $user->id) ->where('maktab_id', $user->maktab_id) ->where('type_employee', $user->type_employee) ->where('type', $user->type) ->first(); if (!$record) { $record = Users_to_maktab::create([ 'user_id' => $user->id, 'maktab_id' => $user->maktab_id, 'type_employee' => $user->type_employee, 'type' => $user->type, 'active' => 1, ]); } foreach ($user->users_to_maktab as $utm) { $maktabTitle = $utm->maktab ? $utm->maktab->title : ''; $userType_cnt = $utm->type_cnt ? $utm->type_cnt->value : ''; $utm->title =$userType_cnt . " " . $maktabTitle; if ($utm->type != 4) { $utm->title =$utm->type_str; } } // dd($user->maktab->title); // $mainMaktabTitle = $user->maktab->title ?? ''; $user->title = $user->name . ' ' . ($user->type_cnt->value ?? $user->type_str ?? '') . ' ' . ($user->maktab->title ?? ''); $cnt = Cnt::where('value', 'مربی')->first(); $user['not_teacher'] = ($user->type == 4 && $user->type_employee != $cnt->id); return response()->json($user); } public function userInfoById(Request $request,$id) { $u = auth()->user(); if ($u->type == 4 || $u->type == 2 || $u->type == 5 || $u->type == 1 || $u->type == 3) { return User::with(['userInfo','userTypeCnt','file','maktab','type_cnt','attendance','cloony','message','children']) ->where('id',$id)->first(); } } public function user_to_maktabs($id) { // Retrieve the authenticated user. $user = auth()->user(); if (!$user) { abort(403, 'Unauthorized'); } // Check if a record exists matching the user's current maktab details. $record = Users_to_maktab::where('user_id', $user->id) ->where('maktab_id', $user->maktab_id) ->where('type_employee', $user->type_employee) ->where('type', $user->type) ->first(); // If no such record exists, create one with an inactive state. if (!$record) { $record = Users_to_maktab::create([ 'user_id' => $user->id, 'maktab_id' => $user->maktab_id, 'type_employee' => $user->type_employee, 'type' => $user->type, 'active' => 0, ]); } // Retrieve the desired new account record by its ID. $newAccount = Users_to_maktab::find($id); if (!$newAccount) { abort(404, "Account not found."); } // Update the user's properties based on the new account. $user->maktab_id = $newAccount->maktab_id; $user->type_employee = $newAccount->type_employee; $user->type = $newAccount->type; $user->save(); // Deactivate all accounts for this user. Users_to_maktab::where('user_id', $user->id)->update(['active' => 0]); // Activate the chosen account. $newAccount->active = 1; $newAccount->save(); // Optionally, you might return the newly activated account. return $user; } public function attendanceMe(Request $request) { $data = $request->all(); $attendance = Attendance::where('employee_id', auth()->id()); if ($request->has('filters')) { foreach ($data['filters'] as $filters) { $where = (new PUC())->selectwhere($filters['op']); if ($where == 'where') { $attendance->$where($filters['field'], PUC::$arrayOp[$filters['op']], $filters['value']); } elseif ($where == 'whereIn') { $attendance->$where($filters['field'], $filters['value']); } else { $attendance->$where($filters['field']); } } } return response()->json($this->addPaginate($attendance, $data)); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\jsonResponse */ public function update(Request $request) { $id = auth()->id(); $data = $request->all(); if (isset($data['mobile']) && !is_null($data['mobile'])) { $check_mobile = User::where('mobile', $data['mobile']) ->where('id', '<>', $id) ->first(); if (!is_null($check_mobile)) { throw new UnprocessableEntityHttpException('این شماره همراه توسط شخص دیگری در حال استفاده است'); } } if (isset($data['fname']) && isset($data['lname'])) { $data['name'] = $data['fname'] . ' ' . $data['lname']; } User::findOrFail($id) ->update($data); $userData = auth()->user(); // return $userData['id']; // return $user_info = user_info::all(); if (isset($data['user_info']) && count($data['user_info']) >= 0) { $infoData = $data['user_info']; $infoData['user_id']=$userData['id'] ; if (!$user_info = user_info::where('user_id', $userData['id'])->first()) { user_info::create($infoData); }else{ $user_info->update($infoData); } } if (isset($data['children']) && count($data['children']) >= 0) { Children::where('user_id', $userData['id'])->delete(); foreach ($data['children'] as $children) { $children['user_id'] = $userData['id']; Children::create($children); } } return response()->json(true); } public function dashboard() { if (auth()->user()->type == 3) { $model = Attendance::class; $maktab_id = auth()->user()->maktab_id; $const = Cnt::where('key', 'type_employee')->where('value', 'مربی')->first(); $count_teacher = User::where('type_employee', $const->id)->where('maktab_id', $maktab_id)->count(); $last = $model::where('maktab_id', $maktab_id)->orderBy('date_at', 'desc')->first(); } else { $model = StudentAttendance::class; $maktab_id = auth()->user()->maktab_id; $count_teacher = Student::where('employee_id', auth()->id())->where('maktab_id', $maktab_id)->count(); $last = $model::where('maktab_id', $maktab_id)->where('employee_id', auth()->id())->orderBy('date_at', 'desc')->first(); } $present = 0; $absent = 0; $employee_id = null; if (!is_null($last)) { $date = $last->date_at; if (auth()->user()->type == 4) { $employee_id = auth()->id(); } $present = $this->countPresentAbsent($date, $maktab_id, true, $model, $employee_id); $absent = $this->countPresentAbsent($date, $maktab_id, false, $model, $employee_id); } return response()->json([ 'count_teacher' => $count_teacher, 'present' => $present, 'absent' => $absent ]); } public function countPresentAbsent($date, $maktab_id, $present, $model, $employee_id = null) { $base = $model::whereDate('date_at', $date)->where('maktab_id', $maktab_id); if (!is_null($employee_id)) { $base->where('employee_id', $employee_id); } return $base->where('present', $present)->count(); } public function teacherList($maktab_id) { $employee = User::where('maktab_id', $maktab_id) ->where('type', 4) ->where('type_employee', 1) ->select(['id', 'name']) ->get(); return response()->json($employee); } public function updateUserInfo(Request $request) { $user = response()->json(User::with(['file', 'maktab', 'cloony', 'type_cnt'])->select(['*', User::$SELECT_TYPE_STR])->findOrFail(auth()->id())); $data = $request->all(); // dd($data); // return $data; $userData = json_decode($user->getContent(), true); if (!$user_info = user_info::where('user_id', $userData['id'])->first()) { $user_info = new user_info(); $create = 1; } $user_info->user_id = $userData['id']; if (isset($create)) { $return_array['user_info'] = user_info::create($data); $return_array['cade'] = 200; $return_array['alert'] = 'saved'; } else { $user_info->update(); $return_array['cade'] = 200; $return_array['alert'] = 'updated'; } return response()->json($user_info); } public function readUserInfo() { // Retrieve the user_info record along with the associated user $userInfo = user_info::with(['user' => function ($q) { return $q->select(['name', 'id']); }])->where('user_id', auth('user')->id())->first(); if ($userInfo) { // Convert userInfo to an array $userInfoData = $userInfo->toArray(); // Define the skill statuses $skillStatuses = [ 'teachingSkill' => $userInfo->teachingSkill ? true : false, 'advertisingSkill' => $userInfo->advertisingSkill ? true : false, 'speechSkill' => $userInfo->speechSkill ? true : false, 'writingSkill' => $userInfo->writingSkill ? true : false, 'arabicSkill' => $userInfo->arabicSkill ? true : false, 'englishSkill' => $userInfo->englishSkill ? true : false, 'computerSkill' => $userInfo->computerSkill ? true : false, 'WordSkill' => $userInfo->WordSkill ? true : false, 'exelSkill' => $userInfo->exelSkill ? true : false, 'powerPointSkill' => $userInfo->powerPointSkill ? true : false, 'audiovisualSkill' => $userInfo->audiovisualSkill ? true : false, 'itSkill' => $userInfo->itSkill ? true : false, 'programmingSkill' => $userInfo->programmingSkill ? true : false, ]; // Merge the skill statuses into the userInfo data $userInfoData = array_merge($userInfoData, $skillStatuses); return response()->json($userInfoData); } return response()->json(['error' => 'User info not found'], 404); } public function deleteUserInfo($user_id) { $return_array = []; if (!$user_info = user_info::find($user_id)) { $return_array['error'] = ("User with ID $user_id not found."); return $return_array; } // Error Handling try { $return_array = $user_info->delete(); } catch (Exception $e) { // Log the error and handle it appropriately $return_array['error'] = $e->getMessage(); return $return_array; } return $return_array; } }
Copyright ©2k19 -
Hexid
|
Tex7ure