HomeController.php 3.4 KB

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