HomeController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\PartnerDiscount;
  4. use App\Models\UsefulLink;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Hash;
  9. use Illuminate\Support\Facades\Http;
  10. use Illuminate\Support\Facades\Storage;
  11. use Illuminate\Support\Facades\URL;
  12. use App\Settings\GeneralSettings;
  13. use App\Settings\WebsiteSettings;
  14. use App\Settings\ReferralSettings;
  15. class HomeController extends Controller
  16. {
  17. const TIME_LEFT_BG_SUCCESS = 'bg-success';
  18. const TIME_LEFT_BG_WARNING = 'bg-warning';
  19. const TIME_LEFT_BG_DANGER = 'bg-danger';
  20. public function __construct()
  21. {
  22. $this->middleware('auth');
  23. }
  24. public function callHome()
  25. {
  26. if (Storage::exists('callHome')) {
  27. return;
  28. }
  29. Http::asForm()->post('https://market.controlpanel.gg/callhome.php', [
  30. 'id' => Hash::make(URL::current()),
  31. ]);
  32. Storage::put('callHome', 'This is only used to count the installations of cpgg.');
  33. }
  34. /**
  35. * @description Get the Background Color for the Days-Left-Box in HomeView
  36. *
  37. * @param float $daysLeft
  38. * @return string
  39. */
  40. public function getTimeLeftBoxBackground(float $daysLeft): string
  41. {
  42. if ($daysLeft >= 15) {
  43. return $this::TIME_LEFT_BG_SUCCESS;
  44. }
  45. if ($daysLeft <= 7) {
  46. return $this::TIME_LEFT_BG_DANGER;
  47. }
  48. return $this::TIME_LEFT_BG_WARNING;
  49. }
  50. /**
  51. * @description Set "hours", "days" or nothing behind the remaining time
  52. *
  53. * @param float $daysLeft
  54. * @param float $hoursLeft
  55. * @return string|void
  56. */
  57. public function getTimeLeftBoxUnit(float $daysLeft, float $hoursLeft)
  58. {
  59. if ($daysLeft > 1) {
  60. return __('days');
  61. }
  62. return $hoursLeft < 1 ? null : __('hours');
  63. }
  64. /**
  65. * @description Get the Text for the Days-Left-Box in HomeView
  66. *
  67. * @param float $daysLeft
  68. * @param float $hoursLeft
  69. * @return string
  70. */
  71. public function getTimeLeftBoxText(float $daysLeft, float $hoursLeft)
  72. {
  73. if ($daysLeft > 1) {
  74. return strval(number_format($daysLeft, 0));
  75. }
  76. return $hoursLeft < 1 ? __('You ran out of Credits') : strval($hoursLeft);
  77. }
  78. /** Show the application dashboard. */
  79. public function index(GeneralSettings $general_settings, WebsiteSettings $website_settings, ReferralSettings $referral_settings)
  80. {
  81. $usage = Auth::user()->creditUsage();
  82. $credits = Auth::user()->Credits();
  83. $bg = '';
  84. $boxText = '';
  85. $unit = '';
  86. /** Build our Time-Left-Box */
  87. if ($credits > 0.01 and $usage > 0) {
  88. $daysLeft = number_format(($credits * 30) / $usage, 2, '.', '');
  89. $hoursLeft = number_format($credits / ($usage / 30 / 24), 2, '.', '');
  90. $bg = $this->getTimeLeftBoxBackground($daysLeft);
  91. $boxText = $this->getTimeLeftBoxText($daysLeft, $hoursLeft);
  92. $unit = $daysLeft < 1 ? ($hoursLeft < 1 ? null : __('hours')) : __('days');
  93. }
  94. $this->callhome();
  95. // RETURN ALL VALUES
  96. return view('home')->with([
  97. 'usage' => $usage,
  98. 'credits' => $credits,
  99. 'useful_links_dashboard' => UsefulLink::where("position","like","%dashboard%")->get()->sortby("id"),
  100. 'bg' => $bg,
  101. 'boxText' => $boxText,
  102. 'unit' => $unit,
  103. 'numberOfReferrals' => DB::table('user_referrals')->where('referral_id', '=', Auth::user()->id)->count(),
  104. 'partnerDiscount' => PartnerDiscount::where('user_id', Auth::user()->id)->first(),
  105. 'myDiscount' => PartnerDiscount::getDiscount(),
  106. 'general_settings' => $general_settings,
  107. 'website_settings' => $website_settings,
  108. 'referral_settings' => $referral_settings
  109. ]);
  110. }
  111. }