123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
-
- namespace App\Http\Controllers\Main;
-
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\Auth;
-
- use Validator;
- use Hash;
-
- use App\SiteSetting;
- use App\Model\Staff;
- use App\Model\StaffDetail;
- use App\Model\Module\Department;
- use App\Model\Module\Roles;
-
- class StaffController extends Controller
- {
-
- /**
- * Create a staff controller.
- *
- * @return value
- */
- public function staffIndex(){
- $id = Auth::guard('sadmin')->id();
- $user = Staff::with('StaffDetail')->find($id);
-
- $department = Department::all();
- $roles = Roles::all();
- $site = SiteSetting::first();
- $my_jabatan = Department::where('_id', $user->StaffDetail->jbkod)->first();
- if(!empty($my_jabatan)){
- $my_jabatan = '['.$my_jabatan->jbkod.'] '.$my_jabatan->jnama;
- }else{
- $my_jabatan = 'null';
- }
- return view('main-dashboard.team.staff_index', compact('user','department','roles','my_jabatan','site'));
- }
-
- public function staffAdd(){
- $id = Auth::guard('sadmin')->id();
- $user = Staff::with('StaffDetail')->find($id);
- $department = Department::all();
- $roles = Roles::where('kod','!=','03')->get();
- $site = SiteSetting::first();
- return view('main-dashboard.team.staff_add', compact('user','department','roles','site'));
- }
-
-
- public function staffEdit($cid){
- $id = Auth::guard('sadmin')->id();
- $user = Staff::with('StaffDetail')->find($id);
- $department = Department::all();
-
- $staff = StaffDetail::where('_id',$cid)->first();
- $access = array();
- if($staff->roles_access == "Pegawai"){
- foreach($staff->roles_ids as $r){
- $role = Roles::where('_id',$r)->first();
- array_push($access, array(
- 'kod' => $role->kod
- ));
- }
-
- // $access = json_encode($access);
- }
- if(empty($access)){
- $access = 'null';
- }
- $roles = Roles::where('kod','!=','03')->get();
- $site = SiteSetting::first();
- return view('main-dashboard.team.staff_edit', compact('user','department','staff','access','roles','site'));
- }
-
-
- public function requestAddStaff(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'password' => 'min:8|confirmed',
- 'profil' => 'mimes:jpeg,jpg|max:2048',
- ]);
-
- if ($validator->fails()) {
- return redirect()->back()->withInput()->withErrors($validator);
- }
-
- if($request->get('jawatan') == "Pegawai"){
- if(empty($request->get('modul'))){
- return redirect()->back()->withInput()->with('error_msg','<strong>Tidak Berjaya!</strong> Sila pilih <strong>modul</strong> fail kulit/kompaun untuk pegawai akses');
- }
- }
-
- $staff = Staff::where('no_badan', $request->badan)->where('roles_access',$request->jawatan)->first();
- if(empty($staff)) {
-
- $encryptP = Hash::make($request->get('password'));
- $profile_img = '/uploads/profile.jpg';
- $saveData = false;
-
- if($request->hasfile('profil')){
- $destinationPath = 'uploads/profile';
- $uploaded = public_path().'/'.$destinationPath;
-
- $allowedfileExtension = ['jpeg','jpg'];
- $a = $request->file('profil');
- $extension = $a->getClientOriginalExtension();
- $check=in_array($extension,$allowedfileExtension);
-
- if($check) {
- $filename = $request->get('badan');
- $a->move($destinationPath,$filename.'.'.$extension);
- $profile_img = '/'.$destinationPath.'/'.$filename.'.'.$extension;
-
- $saveData = true;
- }
- }else{
- $saveData = true;
- }
-
- if($saveData == true){
-
- $sl = Staff::create([
- 'no_badan' => $request->get('badan'),
- 'password' => $encryptP,
- 'api_token' => '',
- 'roles_access' => $request->get('jawatan'),
- 'device' => '',
- 'remember_token' => '',
- 'last_login_at' => '',
- 'last_login_ip'=> '',
- 'authorized' => false,
- 'token_firebase' => '',
- ]);
-
- $detail = new StaffDetail();
- $detail->full_name = $request->get('nama');
- $detail->identity = $request->get('pengenalan');
- $detail->mobile = $request->get('telefon');
- $detail->gred = $request->get('gred');
- $detail->no_badan = $request->get('badan');
- // $detail->jbkod = $request->get('jabatan');
- $detail->roles_access = $request->get('jawatan');
- $detail->profile_img = $profile_img;
- $detail->password = $encryptP;
-
- $sl->staffdetail()->save($detail);
- if($request->get('jawatan') == 'Pegawai'){
- foreach($request->modul as $m){
- $roles = Roles::where('kod',$m)->first();
- $detail->roles()->attach($roles);
- }
- }
-
- foreach($request->jabatan as $j){
- $department = Department::where('_id', $j)->first();
- $detail->department()->attach($department);
- }
-
- return redirect()->back()->with('success_msg', '<strong>Berjaya!</strong> daftar pegawai <strong> No.Badan '.$request->get('badan').', Nama '.$request->get('nama').'</strong>');
- }
- }else {
- return redirect()->back()->withInput()->with('error_msg','<strong>Berjaya!</strong> No.Badan <strong>'.$request->get('badan').'</strong> ini telah wujud');
- }
- }
-
- public function requestEditStaff(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'profil' => 'mimes:jpeg,jpg|max:2048',
- ]);
-
- if ($validator->fails()) {
- return redirect()->back()->withInput()->withErrors($validator);
- }
-
- if($request->get('jawatan') == "Pegawai"){
- if(empty($request->get('modul'))){
- return redirect()->back()->withInput()->with('error_msg','<strong>Tidak Berjaya!</strong> Sila pilih <strong>modul</strong> fail kulit/kompaun untuk pegawai akses');
- }
- }
-
- $staff = Staff::with('StaffDetail')->where('_id', $request->_id)->first();
- if(!empty($staff)) {
-
- if($request->hasfile('profil')){
- $destinationPath = 'uploads/profile';
- $uploaded = public_path().'/'.$destinationPath;
-
- $allowedfileExtension = ['jpeg','jpg'];
- $a = $request->file('profil');
- $extension = $a->getClientOriginalExtension();
- $check=in_array($extension,$allowedfileExtension);
-
- if($check) {
- $filename = $request->get('badan');
- $a->move($destinationPath,$filename.'.'.$extension);
- $profile_img = '/'.$destinationPath.'/'.$filename.'.'.$extension;
-
- $staff->StaffDetail->profile_img = $profile_img;
- $staff->StaffDetail->save();
- }
- }
-
- $staff->roles_access = $request->get('jawatan');
- $staff->no_badan = $request->get('badan');
- $staff->save();
-
- $jabatan = "-";
- if($request->get('jawatan') == 'sysadmin'){
- $jabatan = "-";
- }else{
- foreach($request->jabatan as $j){
- $department = Department::where('_id', $j)->first();
- $department_array[] = $department->_id;
- }
- $staff->StaffDetail->department()->sync($department_array);
- }
-
- $staff->StaffDetail->full_name = $request->get('nama');
- $staff->StaffDetail->identity = $request->get('pengenalan');
- $staff->StaffDetail->mobile = $request->get('telefon');
- $staff->StaffDetail->gred = $request->get('gred');
- $staff->StaffDetail->no_badan = $request->get('badan');
- $staff->StaffDetail->roles_access = $request->get('jawatan');
- $staff->StaffDetail->save();
-
- $roles_array = array();
- if($staff->StaffDetail->roles_access == 'Pegawai' && $request->get('jawatan') == 'Pegawai'){
- foreach($request->modul as $m){
- $roles = Roles::where('kod',$m)->first();
- $roles_array[] = $roles->_id;
- }
-
- $staff->StaffDetail->roles()->sync($roles_array);
- }else{
- $staff->StaffDetail->roles()->detach();
- $staff->StaffDetail->roles_ids = [];
- $staff->StaffDetail->save();
- }
-
- return redirect()->back()->with('success_msg', '<strong>Berjaya!</strong> kemaskini pegawai <strong> No.Badan '.$request->get('badan').', Nama '.$request->get('nama').'</strong>');
- }else {
- return redirect()->back()->withInput()->with('error_msg','<strong>Berjaya!</strong> Rekod No.Badan '.$request->get('badan').' tidak ditemui');
- }
- }
- }
|