HomeController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Get the Text for the Days-Left-Box in HomeView
  45. *
  46. * @param float $days
  47. * @param float $hours
  48. *
  49. * @return string
  50. */
  51. public function getTimeLeftBoxText(float $days, float $hours)
  52. {
  53. if ($days < 1)
  54. {
  55. if ($hours < 1)
  56. {
  57. return $this::TIME_LEFT_OUT_OF_CREDITS_TEXT;
  58. }
  59. else
  60. {
  61. return strval($hours);
  62. }
  63. }
  64. return strval(number_format($days, 0));
  65. }
  66. /** Show the application dashboard. */
  67. public function index(Request $request)
  68. {
  69. $usage = Auth::user()->creditUsage();
  70. $credits = Auth::user()->Credits();
  71. $bg = "";
  72. $boxText = "";
  73. $unit = "";
  74. /** Build our Time-Left-Box */
  75. if ($credits > 0.01 and $usage > 0)
  76. {
  77. $days = number_format(($credits * 30) / $usage, 2, '.', '');
  78. $hours = number_format($credits / ($usage / 30 / 24) , 2, '.', '');
  79. $bg = $this->getTimeLeftBoxBackground($days);
  80. $boxText = $this->getTimeLeftBoxText($days, $hours);
  81. $unit = $days < 1 ? 'hours' : 'days';
  82. }
  83. // RETURN ALL VALUES
  84. return view('home')->with([
  85. 'useage' => $usage,
  86. 'credits' => $credits,
  87. 'useful_links' => UsefulLink::all()->sortBy('id'),
  88. 'bg' => $bg,
  89. 'boxText' => $boxText,
  90. 'unit' => $unit
  91. ]);
  92. }
  93. }