201 lines
6.7 KiB
PHP
201 lines
6.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Credits Plus Extension for Paymenter
|
|
*
|
|
* Created by: Exp (Discord: exp_yt)
|
|
* Company: ExpHost - https://www.exphost.net/
|
|
*
|
|
* This code is the intellectual property of Exp and ExpHost.
|
|
* Unauthorized copying, modification, or distribution is prohibited.
|
|
*/
|
|
|
|
namespace Paymenter\Extensions\Others\CreditsPlus;
|
|
|
|
use App\Classes\Extension\Extension;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class CreditsPlus extends Extension
|
|
{
|
|
public function getName(): string
|
|
{
|
|
return 'Credits Plus';
|
|
}
|
|
|
|
public function getDescription(): string
|
|
{
|
|
return 'Clickable credit display in navigation.';
|
|
}
|
|
|
|
public function getVersion(): string
|
|
{
|
|
return '2.1.0';
|
|
}
|
|
|
|
public function getAuthor(): string
|
|
{
|
|
return 'ExpHost';
|
|
}
|
|
|
|
public function boot(): void
|
|
{
|
|
$this->registerCreditsNavigation();
|
|
}
|
|
|
|
public function enabled(): void {}
|
|
|
|
public function disabled(): void {}
|
|
|
|
public function updated(): void {}
|
|
|
|
public function install(): void {}
|
|
|
|
private function registerCreditsNavigation(): void
|
|
{
|
|
try {
|
|
// Register the view namespace
|
|
View::addNamespace('creditsplus', __DIR__ . '/resources/views');
|
|
|
|
// Store reference to this instance for use in closure
|
|
$extension = $this;
|
|
$extensionPath = __DIR__;
|
|
|
|
// Use view composer to add credits data to navigation
|
|
View::composer('components.navigation.index', function ($view) use ($extension, $extensionPath) {
|
|
try {
|
|
// Don't run in admin panel - this is crucial to avoid conflicts
|
|
if (app()->runningInConsole()) {
|
|
return;
|
|
}
|
|
|
|
// Check if we're in admin context
|
|
$currentPath = request()->path();
|
|
if (str_starts_with($currentPath, 'admin') || str_contains($currentPath, '/admin/')) {
|
|
return;
|
|
}
|
|
|
|
// Don't run for API routes
|
|
if (str_starts_with($currentPath, 'api')) {
|
|
return;
|
|
}
|
|
|
|
if (!Auth::check()) {
|
|
return;
|
|
}
|
|
|
|
// Only show credits if the feature is enabled
|
|
if (!config('settings.credits_enabled', false)) {
|
|
return;
|
|
}
|
|
|
|
$user = Auth::user();
|
|
$creditsDisplay = $extension->calculateUserCreditsDisplay($user);
|
|
|
|
// Determine logo path using storage_url helper if available
|
|
$logoHtml = '';
|
|
if (config('settings.logo')) {
|
|
$logoUrl = function_exists('storage_url')
|
|
? storage_url(config('settings.logo'))
|
|
: Storage::url(config('settings.logo'));
|
|
$logoHtml = '<img src="' . e($logoUrl) . '" alt="' . e(config('app.name')) . '" class="h-10 w-auto inline-block mr-2 rtl:ml-2">';
|
|
}
|
|
|
|
// Inject credits data, logo HTML, and credits URL into the view
|
|
$creditsUrlPath = '#';
|
|
try {
|
|
if (function_exists('route')) {
|
|
$creditsUrlPath = route('account.credits');
|
|
}
|
|
} catch (\Exception $e) {
|
|
$creditsUrlPath = '/account/credits';
|
|
}
|
|
|
|
$view->with([
|
|
'userCreditsDisplay' => $creditsDisplay,
|
|
'creditsLogoHtml' => $logoHtml,
|
|
'creditsUrl' => $creditsUrlPath,
|
|
]);
|
|
|
|
// Replace the view path with our custom navigation
|
|
$customPath = $extensionPath . '/resources/views/components/navigation/index.blade.php';
|
|
if (file_exists($customPath)) {
|
|
$view->setPath($customPath);
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Silently fail
|
|
}
|
|
});
|
|
} catch (\Exception $e) {
|
|
// Silently fail
|
|
}
|
|
}
|
|
|
|
public function calculateUserCreditsDisplay($user): string
|
|
{
|
|
try {
|
|
// Check if the credits relationship exists
|
|
if (!method_exists($user, 'credits')) {
|
|
return $this->getDefaultCurrencyDisplay();
|
|
}
|
|
|
|
$credits = $user->credits()->with('currency')->get();
|
|
|
|
if ($credits->isEmpty()) {
|
|
return $this->getDefaultCurrencyDisplay();
|
|
}
|
|
|
|
$creditsByCurrency = $credits->groupBy('currency_code');
|
|
$maxAmount = null;
|
|
$maxCurrencyCode = null;
|
|
$maxCurrency = null;
|
|
|
|
foreach ($creditsByCurrency as $currencyCode => $userCredits) {
|
|
$totalAmount = $userCredits->sum('amount');
|
|
if ($totalAmount <= 0) {
|
|
continue;
|
|
}
|
|
if ($maxAmount === null || $totalAmount > $maxAmount) {
|
|
$maxAmount = $totalAmount;
|
|
$maxCurrencyCode = $currencyCode;
|
|
$maxCurrency = $userCredits->first()->currency ?? null;
|
|
}
|
|
}
|
|
|
|
if ($maxAmount === null) {
|
|
return $this->getDefaultCurrencyDisplay();
|
|
}
|
|
|
|
if ($maxCurrency && isset($maxCurrency->prefix) && isset($maxCurrency->suffix)) {
|
|
return $maxCurrency->prefix . number_format($maxAmount, 2) . $maxCurrency->suffix;
|
|
} else {
|
|
return number_format($maxAmount, 2) . ' ' . ($maxCurrencyCode ?? '');
|
|
}
|
|
} catch (\Exception $e) {
|
|
return $this->getDefaultCurrencyDisplay();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get default currency display (0.00 with currency symbol)
|
|
*/
|
|
private function getDefaultCurrencyDisplay(): string
|
|
{
|
|
try {
|
|
$currencyCode = session('currency', config('settings.default_currency', 'USD'));
|
|
|
|
if (class_exists('\App\Models\Currency')) {
|
|
$currency = \App\Models\Currency::where('code', $currencyCode)->first();
|
|
if ($currency && isset($currency->prefix) && isset($currency->suffix)) {
|
|
return $currency->prefix . '0.00' . $currency->suffix;
|
|
}
|
|
}
|
|
|
|
return '0.00 ' . $currencyCode;
|
|
} catch (\Exception $e) {
|
|
return '0.00 USD';
|
|
}
|
|
}
|
|
}
|