HomeController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. const TIME_LEFT_OUT_OF_CREDITS_TEXT = "You ran out of Credits";
  15. public function __construct()
  16. {
  17. $this->middleware('auth');
  18. }
  19. /**
  20. * @description Get the Background Color for the Days-Left-Box in HomeView
  21. *
  22. * @param float $days
  23. *
  24. * @return string
  25. */
  26. public function getTimeLeftBoxBackground(float $days)
  27. {
  28. switch ($days)
  29. {
  30. case ($days >= 15):
  31. return $this::TIME_LEFT_BG_SUCCESS;
  32. break;
  33. case ($days >= 8 && $days <= 14):
  34. return $this::TIME_LEFT_BG_WARNING;
  35. break;
  36. case ($days <= 7):
  37. return $this::TIME_LEFT_BG_DANGER;
  38. break;
  39. default:
  40. return $this::TIME_LEFT_BG_WARNING;
  41. }
  42. }
  43. /**
  44. * @description Set "hours", "days" or nothing behind the remaining time
  45. *
  46. * @param float $days
  47. * @param float $hours
  48. *
  49. * @return string
  50. */
  51. public function getTimeLeftBoxUnit(float $days, float $hours)
  52. {
  53. if ($days < 1)
  54. {
  55. if ($hours < 1)
  56. {
  57. return;
  58. }
  59. else
  60. {
  61. return "hours";
  62. }
  63. }
  64. return "days";
  65. }
  66. /**
  67. * @description Get the Text for the Days-Left-Box in HomeView
  68. *
  69. * @param float $days
  70. * @param float $hours
  71. *
  72. * @return string
  73. */
  74. public function getTimeLeftBoxText(float $days, float $hours)
  75. {
  76. if ($days < 1)
  77. {
  78. if ($hours < 1)
  79. {
  80. return $this::TIME_LEFT_OUT_OF_CREDITS_TEXT;
  81. }
  82. else
  83. {
  84. return strval($hours);
  85. }
  86. }
  87. return strval(number_format($days, 0));
  88. }
  89. /** Show the application dashboard. */
  90. public function index(Request $request)
  91. {
  92. $usage = Auth::user()->creditUsage();
  93. $credits = Auth::user()->Credits();
  94. $bg = "";
  95. $boxText = "";
  96. $unit = "";
  97. /** Build our Time-Left-Box */
  98. if ($credits > 0.01 and $usage > 0)
  99. {
  100. $days = number_format(($credits * 30) / $usage, 2, '.', '');
  101. $hours = number_format($credits / ($usage / 30 / 24) , 2, '.', '');
  102. $bg = $this->getTimeLeftBoxBackground($days);
  103. $boxText = $this->getTimeLeftBoxText($days, $hours);
  104. $unit = $this->getTimeLeftBoxUnit($days, $hours);
  105. }
  106. // RETURN ALL VALUES
  107. return view('home')->with([
  108. 'useage' => $usage,
  109. 'credits' => $credits,
  110. 'useful_links' => UsefulLink::all()->sortBy('id'),
  111. 'bg' => $bg,
  112. 'boxText' => $boxText,
  113. 'unit' => $unit
  114. ]);
  115. }
  116. }