181 lines
5.6 KiB
PHP
181 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Person;
|
|
use App\Models\Photo;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Str;
|
|
|
|
class PhotoController extends Controller
|
|
{
|
|
/**
|
|
* Get all photos for a specific person
|
|
*/
|
|
public function getPhotos($personId)
|
|
{
|
|
$person = Person::findOrFail($personId);
|
|
$photos = $person->photos()->get();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $photos,
|
|
'profile_photo' => $person->photos()->where('is_profile_photo', true)->first()
|
|
]);
|
|
}
|
|
|
|
public function handlePhotoUpload($files, $personId, $captions = [], $mainPhotoIndex = null)
|
|
{
|
|
$uploadedPhotos = [];
|
|
|
|
foreach ($files as $index => $file) {
|
|
$extension = $file->getClientOriginalExtension();
|
|
$filename = Str::uuid() . '.' . $extension;
|
|
$path = $file->storeAs('photos/' . $personId, $filename, 'public');
|
|
|
|
// Clear existing profile photos if this is the main photo
|
|
if ($mainPhotoIndex !== null && $index == $mainPhotoIndex) {
|
|
Photo::where('person_id', $personId)->update(['is_profile_photo' => false]);
|
|
}
|
|
|
|
$photo = new Photo([
|
|
'person_id' => $personId,
|
|
'filename' => $filename,
|
|
'original_filename' => $file->getClientOriginalName(),
|
|
'file_path' => Storage::url($path),
|
|
'mime_type' => $file->getMimeType(),
|
|
'file_size' => $file->getSize() / 1024, // KB
|
|
'caption' => $captions[$index] ?? null,
|
|
'is_profile_photo' => ($mainPhotoIndex !== null && $index == $mainPhotoIndex)
|
|
]);
|
|
|
|
$photo->save();
|
|
$uploadedPhotos[] = $photo;
|
|
}
|
|
|
|
return $uploadedPhotos;
|
|
}
|
|
|
|
/**
|
|
* Upload photos for a person
|
|
*/
|
|
public function upload(Request $request, $personId)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'photos' => 'required|array',
|
|
'photos.*' => 'required|image|max:10240', // Max 10MB per image
|
|
'captions' => 'nullable|array',
|
|
'captions.*' => 'nullable|string|max:255',
|
|
'main_photo_index' => 'nullable|integer|min:0', // Changed from set_as_profile
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'errors' => $validator->errors()
|
|
], 422);
|
|
}
|
|
|
|
// Find the person
|
|
$person = Person::findOrFail($personId);
|
|
|
|
// Process each uploaded photo
|
|
if ($request->hasFile('photos')) {
|
|
$files = $request->file('photos');
|
|
$captions = $request->input('captions', []);
|
|
$mainPhotoIndex = $request->input('main_photo_index');
|
|
|
|
$uploadedPhotos = $this->handlePhotoUpload($files, $personId, $captions, $mainPhotoIndex);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Photos uploaded successfully',
|
|
'data' => $uploadedPhotos ?? []
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Set a photo as the profile photo
|
|
*/
|
|
public function setAsProfilePhoto($photoId)
|
|
{
|
|
$photo = Photo::findOrFail($photoId);
|
|
|
|
// Clear existing profile photos for this person
|
|
Photo::where('person_id', $photo->person_id)->update(['is_profile_photo' => false]);
|
|
|
|
// Set this photo as profile photo
|
|
$photo->is_profile_photo = true;
|
|
$result = $photo->save();
|
|
|
|
return response()->json([
|
|
'success' => $result,
|
|
'message' => $result ? 'Profile photo set successfully' : 'Failed to set profile photo',
|
|
'data' => $photo
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update photo caption
|
|
*/
|
|
public function updateCaption(Request $request, $photoId)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'caption' => 'required|string|max:255',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'errors' => $validator->errors()
|
|
], 422);
|
|
}
|
|
|
|
$photo = Photo::findOrFail($photoId);
|
|
$photo->caption = $request->input('caption');
|
|
$photo->save();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Caption updated successfully',
|
|
'data' => $photo
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Delete a photo
|
|
*/
|
|
public function delete($photoId)
|
|
{
|
|
$photo = Photo::findOrFail($photoId);
|
|
|
|
// Delete the physical file
|
|
$path = 'public/photos/' . $photo->person_id . '/' . $photo->filename;
|
|
if (Storage::exists($path)) {
|
|
Storage::delete($path);
|
|
}
|
|
|
|
// If this was a profile photo, try to set another one
|
|
if ($photo->is_profile_photo) {
|
|
$nextPhoto = Photo::where('person_id', $photo->person_id)
|
|
->where('id', '!=', $photo->id)
|
|
->first();
|
|
|
|
if ($nextPhoto) {
|
|
$nextPhoto->is_profile_photo = true;
|
|
$nextPhoto->save();
|
|
}
|
|
}
|
|
|
|
// Delete the database record
|
|
$photo->delete();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Photo deleted successfully'
|
|
]);
|
|
}
|
|
} |