HomeController.php 2.6 KB

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