TeamController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Team;
  4. use App\Category;
  5. use App\Group;
  6. use App\Season;
  7. use Illuminate\Http\Request;
  8. use Session;
  9. use Redirect;
  10. class TeamController extends Controller
  11. {
  12. protected $rules = [
  13. 'name' => ['required']
  14. ];
  15. public function index()
  16. {
  17. $type = isset($_GET["type"]) ? $_GET["type"] : '';
  18. $categories = array();
  19. if ($type != '')
  20. $categories = Category::where('type', '=', $type)->orderBy('name')->pluck('name', 'id')->toArray();
  21. $category_id = isset($_GET["category_id"]) ? $_GET["category_id"] : '';
  22. $groups = array();
  23. if ($category_id != '')
  24. $groups = Group::where('category_id', '=', $category_id)->orderBy('name')->pluck('name', 'id')->toArray();
  25. $group_id = isset($_GET["group_id"]) ? $_GET["group_id"] : '';
  26. if (($category_id != '' && sizeof($groups) == 0) || $group_id != '')
  27. {
  28. $teams = Team::orderBy('name')->get(); //->paginate(50);
  29. $aIds = array();
  30. foreach($teams as $t)
  31. {
  32. $c = $t->getCalendar();
  33. if (@$c->category_id == $category_id)
  34. {
  35. if ($group_id != '')
  36. {
  37. if (@$c->group_id == $group_id)
  38. $aIds[] = $t->id;
  39. }
  40. else
  41. $aIds[] = $t->id;
  42. }
  43. }
  44. $teams = Team::whereIn('id', $aIds); //->paginate(50);
  45. }
  46. else
  47. {
  48. $teams = Team::where('id', '=', -1); //->paginate(50);
  49. }
  50. $season_id = isset($_GET["season_id"]) ? $_GET["season_id"] : '';
  51. if ($season_id != "")
  52. $teams->where('season_id', '=', $season_id);
  53. $teams = $teams->orderBy('name')->get();
  54. $seasons = Season::all()->pluck('name', 'id')->toArray();
  55. Session::put('type', $type);
  56. Session::put('category_id', $category_id);
  57. Session::put('group_id', $group_id);
  58. Session::put('season_id', $season_id);
  59. return view('teams.index',compact('teams', 'categories', 'groups', 'seasons'))
  60. ->with('i', (request()->input('page', 1) - 1) * 5);
  61. }
  62. /**
  63. * Show the form for creating a new resource.
  64. *
  65. * @return \Illuminate\Http\Response
  66. */
  67. public function create()
  68. {
  69. return view('teams.create');
  70. }
  71. /**
  72. * Store a newly created resource in storage.
  73. *
  74. * @param \Illuminate\Http\Request $request
  75. * @return \Illuminate\Http\Response
  76. */
  77. public function store(Request $request)
  78. {
  79. $request->validate($this->rules);
  80. $input = $request->all();
  81. $input["excluded"] = isset($input["excluded"]) ? ($input["excluded"] == 'on' ? true : false) : false;
  82. Team::create($input);
  83. return redirect()->route('teams.index')
  84. ->with('success','Team created successfully.');
  85. }
  86. /**
  87. * Display the specified resource.
  88. *
  89. * @param \App\Team $team
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function show(Team $team)
  93. {
  94. return view('teams.show',compact('team'));
  95. }
  96. /**
  97. * Show the form for editing the specified resource.
  98. *
  99. * @param \App\Team $team
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function edit(Team $team)
  103. {
  104. return view('teams.edit',compact('team'));
  105. }
  106. /**
  107. * Update the specified resource in storage.
  108. *
  109. * @param \Illuminate\Http\Request $request
  110. * @param \App\Team $team
  111. * @return \Illuminate\Http\Response
  112. */
  113. public function update(Request $request, Team $team)
  114. {
  115. $request->validate($this->rules);
  116. $input = $request->all();
  117. $input["excluded"] = isset($input["excluded"]) ? ($input["excluded"] == 'on' ? true : false) : false;
  118. $team->update($input);
  119. return Redirect::to('/admin/teams/?type=' . Session::get('type') . "&category_id=" . Session::get('category_id') . "&group_id=" . Session::get('group_id'). "&season_id=" . Session::get('season_id'));
  120. // return redirect()->route('teams.index')->with('success','Team updated successfully');
  121. }
  122. /**
  123. * Remove the specified resource from storage.
  124. *
  125. * @param \App\Team $team
  126. * @return \Illuminate\Http\Response
  127. */
  128. public function destroy(Team $team)
  129. {
  130. $team->delete();
  131. return Redirect::to('/admin/teams/?type=' . Session::get('type') . "&category_id=" . Session::get('category_id') . "&group_id=" . Session::get('group_id'));
  132. //return redirect()->route('teams.index')->with('success','Team deleted successfully');
  133. }
  134. }