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