Files
My-KOPKB/be/app/Services/SocialMediaService.php
T
ISMAIL MASSERAN 94ecbe5887 first init
2026-06-08 11:37:14 +08:00

194 lines
6.1 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class SocialMediaService
{
/**
* Get all active social media platforms
*/
public function getActiveSocialMedia(): array
{
try {
$socialMedia = DB::table('social_media_settings')
->where('is_active', true)
->orderBy('sort_order', 'asc')
->orderBy('name', 'asc')
->get()
->toArray();
return array_map(function ($item) {
return [
'id' => $item->id,
'platform' => $item->platform,
'name' => $item->name,
'url' => $item->url,
'icon' => $item->icon,
'sort_order' => $item->sort_order,
];
}, $socialMedia);
} catch (\Exception $e) {
Log::error('Error fetching active social media: ' . $e->getMessage());
throw $e;
}
}
/**
* Get all social media settings (including inactive) for admin
*/
public function getAllSocialMediaSettings(): array
{
try {
$settings = DB::table('social_media_settings')
->orderBy('sort_order', 'asc')
->orderBy('name', 'asc')
->get()
->toArray();
return array_map(function ($item) {
return [
'id' => $item->id,
'platform' => $item->platform,
'name' => $item->name,
'url' => $item->url,
'icon' => $item->icon,
'is_active' => (bool) $item->is_active,
'sort_order' => $item->sort_order,
'created_at' => $item->created_at,
'updated_at' => $item->updated_at,
];
}, $settings);
} catch (\Exception $e) {
Log::error('Error fetching all social media settings: ' . $e->getMessage());
throw $e;
}
}
/**
* Update social media settings (bulk update)
*/
public function updateSocialMediaSettings(array $socialMediaData): array
{
try {
// Clear existing settings
DB::table('social_media_settings')->truncate();
// Insert new settings
$insertData = [];
foreach ($socialMediaData as $index => $item) {
$insertData[] = [
'platform' => $item['platform'],
'name' => $item['name'],
'url' => $item['url'],
'icon' => $item['icon'] ?? null,
'is_active' => $item['is_active'] ?? true,
'sort_order' => $item['sort_order'] ?? $index,
'created_at' => now(),
'updated_at' => now(),
];
}
DB::table('social_media_settings')->insert($insertData);
// Return updated settings
return $this->getAllSocialMediaSettings();
} catch (\Exception $e) {
Log::error('Error updating social media settings: ' . $e->getMessage());
throw $e;
}
}
/**
* Add a single social media platform
*/
public function addSocialMediaPlatform(array $data): array
{
try {
$id = DB::table('social_media_settings')->insertGetId([
'platform' => $data['platform'],
'name' => $data['name'],
'url' => $data['url'],
'icon' => $data['icon'] ?? null,
'is_active' => $data['is_active'] ?? true,
'sort_order' => $data['sort_order'] ?? 0,
'created_at' => now(),
'updated_at' => now(),
]);
return $this->getAllSocialMediaSettings();
} catch (\Exception $e) {
Log::error('Error adding social media platform: ' . $e->getMessage());
throw $e;
}
}
/**
* Update a single social media platform
*/
public function updateSocialMediaPlatform(int $id, array $data): array
{
try {
DB::table('social_media_settings')
->where('id', $id)
->update([
'platform' => $data['platform'],
'name' => $data['name'],
'url' => $data['url'],
'icon' => $data['icon'] ?? null,
'is_active' => $data['is_active'] ?? true,
'sort_order' => $data['sort_order'] ?? 0,
'updated_at' => now(),
]);
return $this->getAllSocialMediaSettings();
} catch (\Exception $e) {
Log::error('Error updating social media platform: ' . $e->getMessage());
throw $e;
}
}
/**
* Delete a social media platform
*/
public function deleteSocialMediaPlatform(int $id): array
{
try {
DB::table('social_media_settings')->where('id', $id)->delete();
return $this->getAllSocialMediaSettings();
} catch (\Exception $e) {
Log::error('Error deleting social media platform: ' . $e->getMessage());
throw $e;
}
}
/**
* Toggle active status of a social media platform
*/
public function toggleSocialMediaPlatform(int $id): array
{
try {
$platform = DB::table('social_media_settings')->where('id', $id)->first();
if (!$platform) {
throw new \Exception('Social media platform not found');
}
DB::table('social_media_settings')
->where('id', $id)
->update([
'is_active' => !$platform->is_active,
'updated_at' => now(),
]);
return $this->getAllSocialMediaSettings();
} catch (\Exception $e) {
Log::error('Error toggling social media platform: ' . $e->getMessage());
throw $e;
}
}
}