317 lines
12 KiB
PHP
317 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Announcement;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class EmployerAnnouncementController extends Controller
|
|
{
|
|
/**
|
|
* Get all approved announcements for employers.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getAnnouncements(Request $request)
|
|
{
|
|
try {
|
|
$page = (int)$request->input('page', 1);
|
|
$perPage = (int)$request->input('per_page', 15);
|
|
|
|
$query = Announcement::with(['employer.employerProfile', 'sponsor'])->where('status', 'approved')->latest();
|
|
$total = $query->count();
|
|
$offset = ($page - 1) * $perPage;
|
|
|
|
$announcements = $query->skip($offset)->take($perPage)->get()
|
|
->map(function ($announcement) {
|
|
$postedBy = 'System';
|
|
$organization = 'Migrant Support';
|
|
|
|
if ($announcement->sponsor_id) {
|
|
$postedBy = $announcement->sponsor->full_name;
|
|
$organization = $announcement->sponsor->organization_name;
|
|
} elseif ($announcement->employer_id) {
|
|
$postedBy = $announcement->employer->name;
|
|
$organization = $announcement->employer->employerProfile->company_name ?? 'Employer';
|
|
}
|
|
|
|
// Decode charity details if they exist in json
|
|
$charityDetails = null;
|
|
$content = $announcement->body;
|
|
if (strpos($announcement->body, '{"type":"Charity"') === 0) {
|
|
$decoded = json_decode($announcement->body, true);
|
|
if ($decoded) {
|
|
$charityDetails = $decoded;
|
|
$content = $decoded['content'] ?? $announcement->body;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'id' => $announcement->id,
|
|
'title' => $announcement->title,
|
|
'body' => $content,
|
|
'type' => $announcement->type,
|
|
'employer_name' => $postedBy,
|
|
'company_name' => $organization,
|
|
'created_at' => $announcement->created_at->toISOString(),
|
|
'time_ago' => $announcement->created_at->diffForHumans(),
|
|
'charity_details' => $charityDetails,
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'announcements' => $announcements,
|
|
'pagination' => [
|
|
'total' => $total,
|
|
'per_page' => $perPage,
|
|
'current_page' => $page,
|
|
'last_page' => max(1, (int)ceil($total / $perPage)),
|
|
]
|
|
]
|
|
], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
logger()->error('Mobile Employer Get All Announcements Failure: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'An error occurred while fetching announcements.',
|
|
'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error'
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get all announcements posted by this employer.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getMyAnnouncements(Request $request)
|
|
{
|
|
/** @var User $employer */
|
|
$employer = $request->attributes->get('employer');
|
|
|
|
try {
|
|
$page = (int)$request->input('page', 1);
|
|
$perPage = (int)$request->input('per_page', 15);
|
|
|
|
$query = Announcement::where('employer_id', $employer->id)->latest();
|
|
$total = $query->count();
|
|
$offset = ($page - 1) * $perPage;
|
|
|
|
$announcements = $query->skip($offset)->take($perPage)->get()
|
|
->map(function ($announcement) {
|
|
$charityDetails = null;
|
|
$content = $announcement->body;
|
|
if (strpos($announcement->body, '{"type":"Charity"') === 0) {
|
|
$decoded = json_decode($announcement->body, true);
|
|
if ($decoded) {
|
|
$charityDetails = $decoded;
|
|
$content = $decoded['content'] ?? $announcement->body;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'id' => $announcement->id,
|
|
'title' => $announcement->title,
|
|
'body' => $content,
|
|
'type' => $announcement->type,
|
|
'status' => $announcement->status ?? 'pending',
|
|
'remarks' => $announcement->remarks,
|
|
'created_at' => $announcement->created_at->toISOString(),
|
|
'time_ago' => $announcement->created_at->diffForHumans(),
|
|
'charity_details' => $charityDetails,
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'announcements' => $announcements,
|
|
'pagination' => [
|
|
'total' => $total,
|
|
'per_page' => $perPage,
|
|
'current_page' => $page,
|
|
'last_page' => max(1, (int)ceil($total / $perPage)),
|
|
]
|
|
]
|
|
], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
logger()->error('Mobile Employer Get My Announcements Failure: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'An error occurred while fetching announcements.',
|
|
'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error'
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a new announcement for workers.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function createAnnouncement(Request $request)
|
|
{
|
|
/** @var User $employer */
|
|
$employer = $request->attributes->get('employer');
|
|
|
|
$isCharity = $request->type === 'charity' ||
|
|
$request->has('event_date') ||
|
|
$request->has('location_details') ||
|
|
$request->has('provided_items') ||
|
|
$request->has('contact_person_name');
|
|
|
|
$rules = [
|
|
'title' => 'required|string|max:255',
|
|
'body' => 'required_without:content|string|max:5000',
|
|
'content' => 'required_without:body|string|max:5000',
|
|
'type' => 'nullable|string|in:info,warning,success,charity',
|
|
];
|
|
|
|
if ($isCharity) {
|
|
$rules['event_date'] = 'required|string';
|
|
$rules['event_time'] = 'required_without_all:start_time,end_time|nullable|string';
|
|
$rules['start_time'] = 'required_without:event_time|nullable|string';
|
|
$rules['end_time'] = 'required_without:event_time|nullable|string';
|
|
$rules['provided_items'] = 'required|string';
|
|
$rules['location_details'] = 'required|string';
|
|
$rules['location_pin'] = 'required|string|url';
|
|
$rules['contact_person_name'] = 'required|string|max:255';
|
|
$rules['contact_number'] = 'required|string|regex:/^\d{7,15}$/';
|
|
$rules['country_code'] = 'required|string|regex:/^\+\d{1,4}$/';
|
|
}
|
|
|
|
$validator = Validator::make($request->all(), $rules);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Validation error.',
|
|
'errors' => $validator->errors()
|
|
], 422);
|
|
}
|
|
|
|
try {
|
|
$bodyText = $request->body ?? $request->content;
|
|
|
|
if ($isCharity) {
|
|
$eventTime = $request->event_time;
|
|
if (empty($eventTime)) {
|
|
$eventTime = $request->start_time . ' - ' . $request->end_time;
|
|
}
|
|
|
|
$bodyText = json_encode([
|
|
'type' => 'Charity',
|
|
'provided_items' => $request->provided_items,
|
|
'event_date' => $request->event_date,
|
|
'event_time' => $eventTime,
|
|
'location_details' => $request->location_details,
|
|
'location_pin' => $request->location_pin,
|
|
'content' => $request->body ?? $request->content,
|
|
'contact_person_name' => $request->contact_person_name,
|
|
'contact_number' => $request->contact_number,
|
|
'country_code' => $request->country_code,
|
|
]);
|
|
}
|
|
|
|
$announcement = Announcement::create([
|
|
'title' => $request->title,
|
|
'body' => $bodyText,
|
|
'type' => $request->type ?? ($isCharity ? 'charity' : 'info'),
|
|
'employer_id' => $employer->id,
|
|
'status' => 'pending',
|
|
]);
|
|
|
|
$charityDetails = null;
|
|
$content = $announcement->body;
|
|
if (strpos($announcement->body, '{"type":"Charity"') === 0) {
|
|
$decoded = json_decode($announcement->body, true);
|
|
if ($decoded) {
|
|
$charityDetails = $decoded;
|
|
$content = $decoded['content'] ?? $announcement->body;
|
|
}
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Announcement posted successfully.',
|
|
'data' => [
|
|
'announcement' => [
|
|
'id' => $announcement->id,
|
|
'title' => $announcement->title,
|
|
'body' => $content,
|
|
'type' => $announcement->type,
|
|
'status' => $announcement->status ?? 'pending',
|
|
'remarks' => $announcement->remarks,
|
|
'created_at' => $announcement->created_at->toISOString(),
|
|
'time_ago' => $announcement->created_at->diffForHumans(),
|
|
'charity_details' => $charityDetails,
|
|
]
|
|
]
|
|
], 201);
|
|
|
|
} catch (\Exception $e) {
|
|
logger()->error('Mobile Employer Create Announcement Failure: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'An error occurred while creating the announcement.',
|
|
'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error'
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete an announcement.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function deleteAnnouncement(Request $request, $id)
|
|
{
|
|
/** @var User $employer */
|
|
$employer = $request->attributes->get('employer');
|
|
|
|
try {
|
|
$announcement = Announcement::where('employer_id', $employer->id)
|
|
->where('id', $id)
|
|
->first();
|
|
|
|
if (!$announcement) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Announcement not found or unauthorized.'
|
|
], 404);
|
|
}
|
|
|
|
$announcement->delete();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Announcement deleted successfully.'
|
|
], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
logger()->error('Mobile Employer Delete Announcement Failure: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'An error occurred while deleting the announcement.',
|
|
'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error'
|
|
], 500);
|
|
}
|
|
}
|
|
}
|