HomeController.php 2.6 KB

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