feat: implement server tag management with individual fields and update routes

This commit is contained in:
2025-12-18 21:09:45 -05:00
parent db04b8a6c3
commit b0f7c7f20d
6 changed files with 167 additions and 217 deletions

View File

@@ -8,58 +8,6 @@ use Paymenter\Extensions\Servers\Proxmox\Models\Server;
class ServerTagController
{
/**
* Proxmox-style tag colors
*/
private static $proxmoxTagColors = [
'#e74c3c', // Red
'#e67e22', // Orange
'#f1c40f', // Yellow
'#2ecc71', // Green
'#1abc9c', // Teal
'#3498db', // Blue
'#9b59b6', // Purple
'#e91e63', // Pink
'#00bcd4', // Cyan
'#8bc34a', // Light Green
'#ff5722', // Deep Orange
'#607d8b', // Blue Grey
'#795548', // Brown
'#9e9e9e', // Grey
'#673ab7', // Deep Purple
'#4caf50', // Green Material
];
/**
* Generate a consistent color for a tag based on its text
*/
public static function generateTagColor(string $tagText): string
{
$hash = 0;
for ($i = 0; $i < strlen($tagText); $i++) {
$hash = ord($tagText[$i]) + (($hash << 5) - $hash);
$hash = $hash & $hash;
}
$index = abs($hash) % count(self::$proxmoxTagColors);
return self::$proxmoxTagColors[$index];
}
/**
* Get tags for a server
*/
public function show($serverId)
{
$server = Server::where('id', $serverId)
->where('user_id', Auth::id())
->firstOrFail();
return response()->json([
'success' => true,
'tags' => $server->tags ?? [],
]);
}
/**
* Update tags for a server
*/
@@ -70,44 +18,15 @@ class ServerTagController
->where('user_id', Auth::id())
->firstOrFail();
$tags = $request->input('tags', []);
if (!is_array($tags)) {
$tags = [];
}
$tags = array_slice($tags, 0, 3);
$processedTags = [];
foreach ($tags as $tag) {
if (!is_array($tag) || !isset($tag['text'])) continue;
$text = trim($tag['text'] ?? '');
if (empty($text)) continue;
$text = strtolower(preg_replace('/[^a-zA-Z0-9_-]/', '', $text));
if (empty($text)) continue;
$text = substr($text, 0, 32);
$processedTags[] = [
'text' => $text,
'color' => $tag['color'] ?? self::generateTagColor($text),
];
}
$server->tag1 = $processedTags[0]['text'] ?? null;
$server->tag1color = $processedTags[0]['color'] ?? null;
$server->tag2 = $processedTags[1]['text'] ?? null;
$server->tag2color = $processedTags[1]['color'] ?? null;
$server->tag3 = $processedTags[2]['text'] ?? null;
$server->tag3color = $processedTags[2]['color'] ?? null;
$server->tag1 = $request->input('tag1');
$server->tag1color = $request->input('tag1color');
$server->tag2 = $request->input('tag2');
$server->tag2color = $request->input('tag2color');
$server->tag3 = $request->input('tag3');
$server->tag3color = $request->input('tag3color');
$server->save();
return response()->json([
'success' => true,
'tags' => $processedTags,
]);
return response()->json(['success' => true]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
@@ -115,15 +34,4 @@ class ServerTagController
], 500);
}
}
/**
* Get available tag colors
*/
public function colors()
{
return response()->json([
'success' => true,
'colors' => self::$proxmoxTagColors,
]);
}
}