HomeController.php 3.7 KB

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