HomeController.php 4.0 KB

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