HomeController.php 3.1 KB

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