HomeController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_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($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 string $days
  45. * @param string $hours
  46. *
  47. * @return string
  48. */
  49. public function getTimeLeftBoxText(string $days, string $hours)
  50. {
  51. if ($days < 1)
  52. {
  53. if ($hours < 1)
  54. {
  55. return $this::TIME_LEFT_BG_WARNING;
  56. }
  57. else
  58. {
  59. return strval($hours);
  60. }
  61. }
  62. return strval(number_format($days, 0));
  63. }
  64. /**
  65. * @description Return either "days" or "hours" to use on the front-end
  66. *
  67. * @param float $days
  68. *
  69. * @return string
  70. */
  71. public function getTimeLeftUnit($days)
  72. {
  73. switch ($days)
  74. {
  75. case ($days < 1):
  76. return "hours";
  77. break;
  78. case ($days > 1):
  79. return "days";
  80. break;
  81. default:
  82. return "days";
  83. }
  84. }
  85. /** Show the application dashboard. */
  86. public function index(Request $request)
  87. {
  88. $usage = Auth::user()->creditUsage();
  89. $credits = Auth::user()->Credits();
  90. $bg = "";
  91. $boxText = "";
  92. $unit = "";
  93. /** Build our Time-Left-Box */
  94. if ($credits > 0.01 and $usage > 0)
  95. {
  96. $days = number_format(($credits * 30) / $usage, 2, '.', '');
  97. $hours = number_format($credits / ($usage / 30 / 24) , 2, '.', '');
  98. $bg = $this->getTimeLeftBoxBackground($days);
  99. $boxText = $this->getTimeLeftBoxText($days, $hours);
  100. $unit = $this->getTimeLeftUnit($days, $hours);
  101. }
  102. // RETURN ALL VALUES
  103. return view('home')->with([
  104. 'useage' => $usage,
  105. 'credits' => $credits,
  106. 'useful_links' => UsefulLink::all()->sortBy('id'),
  107. 'bg' => $bg,
  108. 'boxText' => $boxText,
  109. 'unit' => $unit
  110. ]);
  111. }
  112. }