You've already forked Atomcms-edit
Initial commit
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Hotel\BadgeTextEditors;
|
||||
|
||||
use App\Filament\Resources\Hotel\BadgeTextEditors\Pages\CreateBadgeTextEditor;
|
||||
use App\Filament\Resources\Hotel\BadgeTextEditors\Pages\EditBadgeTextEditor;
|
||||
use App\Filament\Resources\Hotel\BadgeTextEditors\Pages\ListBadgeTextEditors;
|
||||
use App\Models\WebsiteBadge;
|
||||
use App\Services\SettingsService;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Columns\ImageColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class BadgeTextEditorResource extends Resource
|
||||
{
|
||||
#[\Override]
|
||||
protected static ?string $model = WebsiteBadge::class;
|
||||
|
||||
#[\Override]
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Hotel';
|
||||
|
||||
#[\Override]
|
||||
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-pencil-square';
|
||||
|
||||
#[\Override]
|
||||
protected static ?string $navigationLabel = 'Badge Editor';
|
||||
|
||||
#[\Override]
|
||||
protected static ?string $modelLabel = 'Badge Text';
|
||||
|
||||
#[\Override]
|
||||
protected static ?string $slug = 'hotel/badge-text-editor';
|
||||
|
||||
#[\Override]
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextInput::make('badge_key')
|
||||
->required()
|
||||
->label('Badge Key - Expl. ATOM101')
|
||||
->placeholder('This is the badge code'),
|
||||
TextInput::make('badge_name')
|
||||
->required()
|
||||
->label('Badge Name')
|
||||
->placeholder('This is the name of the badge: Expl. The ATOM Badge'),
|
||||
Textarea::make('badge_description')
|
||||
->required()
|
||||
->label('Badge Description')
|
||||
->placeholder('Please add a description for the badge.'),
|
||||
]);
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
$settingsService = app(SettingsService::class);
|
||||
$badgesPath = $settingsService->getOrDefault('badges_path', '/gamedata/c_images/album1584/');
|
||||
|
||||
return $table
|
||||
->columns([
|
||||
ImageColumn::make('badge_key')
|
||||
->label('Badge Image')
|
||||
->getStateUsing(function ($record) use ($badgesPath) {
|
||||
$badgeName = str_replace('badge_desc_', '', $record->badge_key);
|
||||
|
||||
return asset($badgesPath . $badgeName . '.gif');
|
||||
})
|
||||
->width(50)
|
||||
->height(50),
|
||||
TextColumn::make('badge_name')
|
||||
->label('Badge Code & Name')
|
||||
->formatStateUsing(fn ($record) => $record->badge_key . ' : ' . $record->badge_name)
|
||||
->searchable(query: function ($query, $search) {
|
||||
$query->where('badge_key', 'like', "%{$search}%")
|
||||
->orWhere('badge_name', 'like', "%{$search}%");
|
||||
})
|
||||
->sortable(),
|
||||
TextColumn::make('badge_description')
|
||||
->label('Badge Description')
|
||||
->getStateUsing(fn ($record) => Str::limit($record->badge_description, 65))
|
||||
->searchable(),
|
||||
])
|
||||
->filters([])
|
||||
->defaultSort('badge_key', 'asc')
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
DeleteAction::make(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListBadgeTextEditors::route('/'),
|
||||
'create' => CreateBadgeTextEditor::route('/create'),
|
||||
'edit' => EditBadgeTextEditor::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Filament\Resources\Hotel\BadgeTextEditors\Pages;
|
||||
|
||||
use App\Filament\Resources\Hotel\BadgeTextEditors\BadgeTextEditorResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateBadgeTextEditor extends CreateRecord
|
||||
{
|
||||
#[\Override]
|
||||
protected static string $resource = BadgeTextEditorResource::class;
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Filament\Resources\Hotel\BadgeTextEditors\Pages;
|
||||
|
||||
use App\Filament\Resources\Hotel\BadgeTextEditors\BadgeTextEditorResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use PDOException;
|
||||
|
||||
class EditBadgeTextEditor extends EditRecord
|
||||
{
|
||||
#[\Override]
|
||||
protected static string $resource = BadgeTextEditorResource::class;
|
||||
|
||||
#[\Override]
|
||||
protected function getActions(): array
|
||||
{
|
||||
return [DeleteAction::make()];
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function afterSave(): void {}
|
||||
|
||||
#[\Override]
|
||||
protected function handleRecordUpdate(Model $record, array $data): Model
|
||||
{
|
||||
try {
|
||||
return parent::handleRecordUpdate($record, $data);
|
||||
} catch (PDOException $e) {
|
||||
if ($e->getCode() === '23000') {
|
||||
Log::error('Duplicate badge key error: ' . $e->getMessage());
|
||||
|
||||
Notification::make()
|
||||
->title('Duplicate Badge Key')
|
||||
->body('The badge key already exists. Please use a unique badge key.')
|
||||
->danger()
|
||||
->persistent()
|
||||
->send();
|
||||
|
||||
return $record;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Hotel\BadgeTextEditors\Pages;
|
||||
|
||||
use App\Filament\Resources\Hotel\BadgeTextEditors\BadgeTextEditorResource;
|
||||
use App\Models\WebsiteBadge;
|
||||
use App\Services\SettingsService;
|
||||
use Exception;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ListBadgeTextEditors extends ListRecords
|
||||
{
|
||||
#[\Override]
|
||||
protected static string $resource = BadgeTextEditorResource::class;
|
||||
|
||||
#[\Override]
|
||||
protected function getActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make()
|
||||
->label('Add Badge')
|
||||
->color('info')
|
||||
->modalHeading('Add a New Badge')
|
||||
->modalButton('Create Badge')
|
||||
->after(function () {
|
||||
Notification::make()
|
||||
->title('Badge Created')
|
||||
->body('The badge was successfully created.')
|
||||
->success()
|
||||
->send();
|
||||
}),
|
||||
Action::make('export')
|
||||
->label('Export to ExternalTexts')
|
||||
->action('exportToJson'),
|
||||
Action::make('backup')
|
||||
->label('Create Backup of ExternalTexts')
|
||||
->color('success')
|
||||
->action('createBackup'),
|
||||
];
|
||||
}
|
||||
|
||||
public function exportToJson(SettingsService $settingsService): void
|
||||
{
|
||||
$jsonPath = $settingsService->getOrDefault('nitro_external_texts_file');
|
||||
|
||||
if (empty($jsonPath)) {
|
||||
Notification::make()
|
||||
->title('Export Failed')
|
||||
->body('The JSON file path is not configured in the website settings.')
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! file_exists($jsonPath)) {
|
||||
Notification::make()
|
||||
->title('Export Failed')
|
||||
->body('The JSON file does not exist at the specified path.')
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$jsonData = json_decode(file_get_contents($jsonPath), true);
|
||||
|
||||
$badges = WebsiteBadge::all();
|
||||
$badgeKeys = $badges->pluck('badge_key')->toArray();
|
||||
|
||||
foreach ($jsonData as $key => $value) {
|
||||
if (
|
||||
(str_starts_with((string) $key, 'badge_desc_') || str_starts_with((string) $key, 'badge_name_')) &&
|
||||
! in_array(str_replace(['badge_desc_', 'badge_name_'], '', $key), $badgeKeys)
|
||||
) {
|
||||
unset($jsonData[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($badges as $badge) {
|
||||
$jsonData['badge_desc_' . $badge->badge_key] = $badge->badge_description;
|
||||
$jsonData['badge_name_' . $badge->badge_key] = $badge->badge_name;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = file_put_contents(
|
||||
$jsonPath,
|
||||
json_encode($jsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
);
|
||||
|
||||
if ($result === false) {
|
||||
throw new Exception('Failed to write to the JSON file.');
|
||||
}
|
||||
|
||||
Notification::make()
|
||||
->title('Export Successful')
|
||||
->body('Badge data exported successfully.')
|
||||
->success()
|
||||
->send();
|
||||
} catch (Exception $e) {
|
||||
Log::error('Failed to export badge data: ' . $e->getMessage());
|
||||
|
||||
Notification::make()
|
||||
->title('Export Failed')
|
||||
->body('Failed to export badge data. Please check file permissions or contact your administrator.')
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
}
|
||||
|
||||
public function createBackup(SettingsService $settingsService): void
|
||||
{
|
||||
$jsonPath = $settingsService->getOrDefault('nitro_external_texts_file');
|
||||
|
||||
if (empty($jsonPath)) {
|
||||
Notification::make()
|
||||
->title('Backup Failed')
|
||||
->body('The JSON file path is not configured in the website settings.')
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! file_exists($jsonPath)) {
|
||||
Notification::make()
|
||||
->title('Backup Failed')
|
||||
->body('The JSON file does not exist at the specified path.')
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$backupPath = dirname((string) $jsonPath) . '/ExternalTexts_' . time() . '.json';
|
||||
|
||||
if (copy($jsonPath, $backupPath)) {
|
||||
Notification::make()
|
||||
->title('Backup Successful')
|
||||
->body('A backup of the JSON file has been created: ' . basename($backupPath))
|
||||
->success()
|
||||
->send();
|
||||
} else {
|
||||
Notification::make()
|
||||
->title('Backup Failed')
|
||||
->body('Failed to create a backup of the JSON file.')
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user