From cd3ce0c1911336118fa734ac5b68de628d40461c Mon Sep 17 00:00:00 2001 From: Ray1111-aa Date: Tue, 20 Oct 2026 19:22:28 +0700 Subject: [PATCH] fix: sync employee photo to user profile avatar, topbar, and dashboard --- app/Http/Controllers/Admin/UserController.php | 25 +++- .../Controllers/Hse/EmployeeController.php | 13 +++ app/Models/Employee.php | 13 ++- app/Models/User.php | 46 ++++++++ ...0_000001_add_photo_path_to_users_table.php | 52 +++++++++ resources/views/admin/users/index.blade.php | 110 +++++++++++++++++- resources/views/employee/dashboard.blade.php | 68 ++++++----- resources/views/layouts/app.blade.php | 30 +++-- resources/views/profile/index.blade.php | 39 ++++++- .../Feature/PhotoUploadAndCompressionTest.php | 77 ++++++++++++ 10 files changed, 424 insertions(+), 49 deletions(-) create mode 100644 database/migrations/2026_10_20_000001_add_photo_path_to_users_table.php diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 960cea9..daaa86e 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -6,6 +6,7 @@ use App\Http\Controllers\Controller; use App\Models\AdminActivityLog; use App\Models\Gate; use App\Models\User; +use App\Services\ImageCompressionService; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -73,6 +74,7 @@ class UserController extends Controller 'password' => ['required', 'string', 'min:6'], 'role' => ['required', 'string', 'exists:roles,name'], 'gate_id' => ['nullable', 'exists:gates,id'], + 'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'], ], [ 'name.required' => 'Nama lengkap wajib diisi.', 'username.required' => 'Username wajib diisi.', @@ -85,6 +87,11 @@ class UserController extends Controller 'role.exists' => 'Role yang dipilih tidak valid.', ]); + $photoPath = null; + if ($request->hasFile('photo')) { + $photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80); + } + $user = User::create([ 'name' => $validated['name'], 'username' => $validated['username'], @@ -92,6 +99,7 @@ class UserController extends Controller 'password' => Hash::make($validated['password']), 'role' => $validated['role'], // Simpan ke kolom legacy untuk backwards-compat 'gate_id' => $validated['gate_id'] ?? null, + 'photo_path' => $photoPath, ]); // Assign Spatie Role @@ -110,7 +118,10 @@ class UserController extends Controller // Jika user berstatus employee (role employee atau memiliki wewenang operasional karyawan), pastikan profil master Employee ada if ($user->isEmployee()) { - $user->getOrCreateEmployee(); + $emp = $user->getOrCreateEmployee(); + if ($emp && $photoPath && empty($emp->photo_path)) { + $emp->update(['photo_path' => $photoPath]); + } } // Rekam ke Log Riwayat Aktivitas Super Admin @@ -149,6 +160,7 @@ class UserController extends Controller 'password' => ['nullable', 'string', 'min:6'], 'role' => ['required', 'string', 'exists:roles,name'], 'gate_id' => ['nullable', 'exists:gates,id'], + 'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'], ], [ 'name.required' => 'Nama lengkap wajib diisi.', 'username.required' => 'Username wajib diisi.', @@ -180,6 +192,11 @@ class UserController extends Controller $passwordUpdated = true; } + if ($request->hasFile('photo')) { + ImageCompressionService::deleteIfExists($user->photo_path); + $user->photo_path = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80); + } + $user->save(); // Sync Spatie Role @@ -199,9 +216,13 @@ class UserController extends Controller $user->employee->update([ 'name' => $user->name, 'nik' => $user->username, + 'photo_path' => $user->photo_path ?: $user->employee->photo_path, ]); } elseif ($user->isEmployee()) { - $user->getOrCreateEmployee(); + $emp = $user->getOrCreateEmployee(); + if ($emp && $user->photo_path) { + $emp->update(['photo_path' => $user->photo_path]); + } } // Susun deskripsi perubahan yang mudah dipahami non-IT diff --git a/app/Http/Controllers/Hse/EmployeeController.php b/app/Http/Controllers/Hse/EmployeeController.php index 5579442..5007fce 100644 --- a/app/Http/Controllers/Hse/EmployeeController.php +++ b/app/Http/Controllers/Hse/EmployeeController.php @@ -568,6 +568,7 @@ class EmployeeController extends Controller $photoPath = null; if ($request->hasFile('photo')) { $photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80); + $user->update(['photo_path' => $photoPath]); } Employee::create([ @@ -627,6 +628,18 @@ class EmployeeController extends Controller 'photo_path' => $photoPath, ]); + // Sinkronkan akun login User terkait + $linkedUser = $employee->user ?: User::where('username', $employee->nik)->first(); + if ($linkedUser) { + if (!$employee->user_id) { + $employee->update(['user_id' => $linkedUser->id]); + } + $linkedUser->update([ + 'name' => $employee->name, + 'photo_path' => $photoPath, + ]); + } + return back()->with('success', "Data SIMPER untuk {$employee->name} berhasil diperbarui."); } } diff --git a/app/Models/Employee.php b/app/Models/Employee.php index 5f02434..438f6d0 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -48,15 +48,20 @@ class Employee extends Model public function getPhotoUrlAttribute(): ?string { - if (!$this->photo_path) { + $path = $this->photo_path; + if (empty($path) && $this->user_id) { + $path = $this->user?->photo_path; + } + + if (!$path) { return null; } - if (str_starts_with($this->photo_path, 'http://') || str_starts_with($this->photo_path, 'https://')) { - return $this->photo_path; + if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) { + return $path; } - return asset('storage/' . ltrim($this->photo_path, '/')); + return asset('storage/' . ltrim($path, '/')); } /** diff --git a/app/Models/User.php b/app/Models/User.php index cba290f..1000393 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -58,6 +58,16 @@ class User extends Authenticatable 'password', 'role', 'gate_id', + 'photo_path', + ]; + + /** + * The accessors to append to the model's array form. + * + * @var list + */ + protected $appends = [ + 'photo_url', ]; /** @@ -98,6 +108,42 @@ class User extends Authenticatable return $this->hasOne(Employee::class); } + /** + * Dapatkan URL foto profil pengguna (mendukung delegasi otomatis ke foto master Employee). + */ + public function getPhotoUrlAttribute(): ?string + { + if (!empty($this->photo_path)) { + if (str_starts_with($this->photo_path, 'http://') || str_starts_with($this->photo_path, 'https://')) { + return $this->photo_path; + } + return asset('storage/' . ltrim($this->photo_path, '/')); + } + + // Delegasi ke profil employee tertaut + if ($this->relationLoaded('employee') && $this->employee) { + return $this->employee->photo_url; + } + + $employee = $this->employee()->first(); + if ($employee && $employee->photo_url) { + return $employee->photo_url; + } + + // Fallback cari employee dengan NIK sama dengan username + if (!empty($this->username)) { + $employeeByNik = Employee::where('nik', $this->username)->first(); + if ($employeeByNik) { + if (empty($employeeByNik->user_id)) { + $employeeByNik->update(['user_id' => $this->id]); + } + return $employeeByNik->photo_url; + } + } + + return null; + } + /** * Dapatkan profil Employee yang tertaut, atau cari berdasarkan NIK (username), * atau buat profil Employee default jika akun berstatus employee / super admin penguji. diff --git a/database/migrations/2026_10_20_000001_add_photo_path_to_users_table.php b/database/migrations/2026_10_20_000001_add_photo_path_to_users_table.php new file mode 100644 index 0000000..7d014bb --- /dev/null +++ b/database/migrations/2026_10_20_000001_add_photo_path_to_users_table.php @@ -0,0 +1,52 @@ +string('photo_path')->nullable()->after('role'); + }); + } + + // Sinkronkan foto yang sudah ada dari master Employee ke akun User terkait + try { + $employees = Employee::whereNotNull('photo_path')->get(); + foreach ($employees as $emp) { + if ($emp->user_id) { + User::where('id', $emp->user_id)->update(['photo_path' => $emp->photo_path]); + } elseif (!empty($emp->nik)) { + $user = User::where('username', $emp->nik)->first(); + if ($user) { + $user->update(['photo_path' => $emp->photo_path]); + $emp->update(['user_id' => $user->id]); + } + } + } + } catch (\Throwable $e) { + // Ignore if tables are being set up in test environment + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + if (Schema::hasColumn('users', 'photo_path')) { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('photo_path'); + }); + } + } +}; diff --git a/resources/views/admin/users/index.blade.php b/resources/views/admin/users/index.blade.php index bdfd3f3..5f27dbe 100644 --- a/resources/views/admin/users/index.blade.php +++ b/resources/views/admin/users/index.blade.php @@ -210,9 +210,13 @@
-
- {{ strtoupper(substr($user->name, 0, 1)) }} -
+ @if($user->photo_url) + {{ $user->name }} + @else +
+ {{ strtoupper(substr($user->name, 0, 1)) }} +
+ @endif

{{ $user->name }}

@{{ $user->username }}

@@ -303,7 +307,7 @@
-
+ @csrf
@@ -326,6 +330,26 @@
+ +
+ +
+
+ + +
+
+ +

Otomatis dikompresi hemat storage (Maks. 10MB).

+
+
+
+
@@ -374,7 +398,7 @@
- + @csrf
@@ -399,6 +423,26 @@
+ +
+ +
+
+ + +
+
+ +

Pilih file baru jika ingin mengganti foto profil.

+
+
+
+
@@ -519,6 +563,24 @@ const rolePermissionsMap = @json($rolePermissionsMap ?? []); let currentUserDirectPerms = []; + function previewUserImage(input, previewId, placeholderId) { + const preview = document.getElementById(previewId); + const placeholder = document.getElementById(placeholderId); + if (input.files && input.files[0]) { + const reader = new FileReader(); + reader.onload = function(e) { + if (preview) { + preview.src = e.target.result; + preview.classList.remove('hidden'); + } + if (placeholder) { + placeholder.classList.add('hidden'); + } + }; + reader.readAsDataURL(input.files[0]); + } + } + function openCreateUserModal() { document.getElementById('createUserModal').classList.remove('hidden'); document.documentElement.classList.add('modal-open'); @@ -527,6 +589,17 @@ function closeCreateUserModal() { document.getElementById('createUserModal').classList.add('hidden'); document.documentElement.classList.remove('modal-open'); + const input = document.getElementById('addUserPhotoInput'); + const preview = document.getElementById('addUserPhotoPreview'); + const placeholder = document.getElementById('addUserPhotoPlaceholder'); + if (input) input.value = ''; + if (preview) { + preview.src = ''; + preview.classList.add('hidden'); + } + if (placeholder) { + placeholder.classList.remove('hidden'); + } } function toggleDirectPermsAccordion() { @@ -633,6 +706,24 @@ document.getElementById('edit_role').value = primaryRole; document.getElementById('edit_gate_id').value = user.gate_id || ''; + const editPhotoPreview = document.getElementById('editUserPhotoPreview'); + const editPhotoPlaceholder = document.getElementById('editUserPhotoPlaceholder'); + const editPhotoInput = document.getElementById('editUserPhotoInput'); + if (editPhotoInput) editPhotoInput.value = ''; + if (user.photo_url) { + if (editPhotoPreview) { + editPhotoPreview.src = user.photo_url; + editPhotoPreview.classList.remove('hidden'); + } + if (editPhotoPlaceholder) editPhotoPlaceholder.classList.add('hidden'); + } else { + if (editPhotoPreview) { + editPhotoPreview.src = ''; + editPhotoPreview.classList.add('hidden'); + } + if (editPhotoPlaceholder) editPhotoPlaceholder.classList.remove('hidden'); + } + currentUserDirectPerms = Array.isArray(userDirectPerms) ? [...userDirectPerms] : []; updatePermissionCheckboxes(); @@ -643,6 +734,15 @@ function closeEditUserModal() { document.getElementById('editUserModal').classList.add('hidden'); document.documentElement.classList.remove('modal-open'); + const editPhotoInput = document.getElementById('editUserPhotoInput'); + const editPhotoPreview = document.getElementById('editUserPhotoPreview'); + const editPhotoPlaceholder = document.getElementById('editUserPhotoPlaceholder'); + if (editPhotoInput) editPhotoInput.value = ''; + if (editPhotoPreview) { + editPhotoPreview.src = ''; + editPhotoPreview.classList.add('hidden'); + } + if (editPhotoPlaceholder) editPhotoPlaceholder.classList.remove('hidden'); } function openDeleteUserModal(userId, userName) { diff --git a/resources/views/employee/dashboard.blade.php b/resources/views/employee/dashboard.blade.php index 3d7c18e..0319925 100644 --- a/resources/views/employee/dashboard.blade.php +++ b/resources/views/employee/dashboard.blade.php @@ -6,36 +6,52 @@
-
-
-
-

- Dashboard Karyawan -

- @if($employee && $employee->hasValidSimper()) - - SIMPER Aktif - - @elseif($employee) - - SIMPER Exp - - @endif -
-
- Karyawan: {{ $employee ? $employee->name : auth()->user()->name }} - @if($employee && $employee->nik) - ({{ $employee->nik }}) - @endif - @if($employee && $employee->division) - • - {{ $employee->division }} - @endif +
+
+ @php + $empPhoto = $employee?->photo_url ?: auth()->user()->photo_url; + $empName = $employee ? $employee->name : auth()->user()->name; + @endphp + @if($empPhoto) + {{ $empName }} + @else +
+ {{ strtoupper(substr($empName, 0, 1)) }} +
+ @endif +
+
+

+ {{ $empName }} +

+ @if($employee && $employee->hasValidSimper()) + + SIMPER Aktif + + @elseif($employee) + + SIMPER Exp + + @endif +
+
+ @if($employee && $employee->nik) + NIK: {{ $employee->nik }} + @endif + @if($employee && $employee->division) + • + {{ $employee->division }} + @endif + @if($employee && $employee->position) + • + {{ $employee->position }} + @endif +
@if($employee && $employee->simper_expired_at) -
+
Masa Berlaku SIMPER: {{ $employee->simper_expired_at->format('d/m/Y') }}
@endif diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index f4bccc1..65a31cd 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -449,9 +449,13 @@
-
- {{ strtoupper(substr(auth()->user()->name, 0, 1)) }} -
+ @if(auth()->user()->photo_url) + {{ auth()->user()->name }} + @else +
+ {{ strtoupper(substr(auth()->user()->name, 0, 1)) }} +
+ @endif

{{ auth()->user()->name }}

{{ str_replace('_', ' ', auth()->user()->role) }}

@@ -740,9 +744,13 @@
-
- {{ strtoupper(substr(auth()->user()->name, 0, 1)) }} -
+ @if(auth()->user()->photo_url) + {{ auth()->user()->name }} + @else +
+ {{ strtoupper(substr(auth()->user()->name, 0, 1)) }} +
+ @endif

{{ auth()->user()->name }}

{{ str_replace('_', ' ', auth()->user()->role) }}

@@ -824,9 +832,13 @@ class="flex items-center space-x-2.5 p-1 sm:px-2.5 sm:py-1.5 rounded-xl hover:bg-slate-50 transition group focus:outline-none cursor-pointer" aria-expanded="false" aria-haspopup="true"> -
- {{ strtoupper(substr(auth()->user()->name, 0, 1)) }} -
+ @if(auth()->user()->photo_url) + {{ auth()->user()->name }} + @else +
+ {{ strtoupper(substr(auth()->user()->name, 0, 1)) }} +
+ @endif