HomeController.php 2.7 KB

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