CalendarController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Calendar;
  4. use App\CalendarGame;
  5. use App\Season;
  6. use App\Team;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\File;
  9. class CalendarController extends Controller
  10. {
  11. protected $rules = [
  12. // 'name' => ['required']
  13. ];
  14. public function index()
  15. {
  16. if (\Illuminate\Support\Facades\Auth::user()->level == 1)
  17. return redirect('/admin/dashboard');
  18. $calendars = Calendar::orderBy('position')->orderBy('name')->get(); //->paginate(50);
  19. return view('calendars.index',compact('calendars'))
  20. ->with('i', (request()->input('page', 1) - 1) * 5);
  21. }
  22. /**
  23. * Show the form for creating a new resource.
  24. *
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function create()
  28. {
  29. $seasons = Season::orderBy('name')->pluck('name', 'id')->toArray();;
  30. return view('calendars.create', compact('seasons'));
  31. }
  32. /**
  33. * Store a newly created resource in storage.
  34. *
  35. * @param \Illuminate\Http\Request $request
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function store(Request $request)
  39. {
  40. //CalendarGame::truncate();
  41. //Calendar::truncate();
  42. $request->validate($this->rules);
  43. $input = $request->all();
  44. $input["name"] = '';
  45. $calendar = Calendar::create($input);
  46. // Creo le giornate
  47. if(request()->file)
  48. {
  49. $file = request()->file;
  50. $filename = time() . '_' . $file->getClientOriginalName();
  51. if (! File::exists(public_path()."/files/calendars"))
  52. File::makeDirectory(public_path()."/files/calendars");
  53. $path = public_path('files/calendars');
  54. request()->file->move($path, $filename);
  55. $handle = fopen($path . "/" . $filename, "r");
  56. if ($handle)
  57. {
  58. $aTeams = array();
  59. $aT = array("ANDATA", "RITORNO");
  60. foreach($aT as $type)
  61. {
  62. $date = '';
  63. $day = 0;
  64. $insert = false;
  65. $handle = fopen($path . "/" . $filename, "r");
  66. while (($line = fgets($handle)) !== false)
  67. {
  68. if (substr($line, 0, 3) == '.--' || substr($line, 0, 3) == '|--')
  69. {
  70. $insert = false;
  71. }
  72. if (strpos($line, $type . ':') !== false)
  73. {
  74. $day += 1;
  75. $tmp = $line;
  76. if ($type == 'ANDATA')
  77. {
  78. $start = strpos($tmp, '|');
  79. $end = strpos($tmp, '|', 1);
  80. }
  81. else
  82. {
  83. $start = strpos($tmp, 'RITORNO: ');
  84. $end = strrpos($tmp, '|');
  85. }
  86. if ($type == 'ANDATA')
  87. {
  88. $tmp = substr($tmp, $start + 1, $end - 1);
  89. $tmp = str_replace($type . ":", "", $tmp);
  90. }
  91. else
  92. {
  93. $tmp = substr($tmp, $start + 8, 10);
  94. }
  95. $tmp = trim($tmp);
  96. list($dt, $month, $year) = explode("/", $tmp);
  97. $date = "20" . $year . "-" . $month . "-" . $dt;
  98. }
  99. if($insert)
  100. {
  101. $tmp = $line;
  102. $tmp = trim(str_replace("|", "", $tmp));
  103. list($home, $away) = explode("-", $tmp);
  104. $home_team_id = $this->getTeam($home, $calendar->season_id);
  105. $away_team_id = $this->getTeam($away, $calendar->season_id);
  106. $calendar_game = new CalendarGame();
  107. $calendar_game->calendar_id = $calendar->id;
  108. $calendar_game->home_team_id = $type == 'ANDATA' ? $home_team_id : $away_team_id;
  109. $calendar_game->away_team_id = $type == 'ANDATA' ? $away_team_id : $home_team_id;
  110. $calendar_game->date = $date;
  111. $calendar_game->day = $day;
  112. $calendar_game->type = $type;
  113. $calendar_game->home_goals = null;
  114. $calendar_game->away_goals = null;
  115. $calendar_game->home_points = null;
  116. $calendar_game->away_points = null;
  117. $calendar_game->save();
  118. }
  119. if (substr($line, 0, 3) == '|--')
  120. {
  121. $insert = true;
  122. }
  123. }
  124. }
  125. fclose($handle);
  126. }
  127. else
  128. {
  129. }
  130. }
  131. return redirect()->route('calendars.index')
  132. ->with('success','Calendar created successfully.');
  133. }
  134. public function getTeam($team, $season_id)
  135. {
  136. $ret = 0;
  137. $team = trim($team);
  138. if (strpos(strtolower($team), 'riposa') !== false)
  139. {
  140. $ret = null;
  141. }
  142. else
  143. {
  144. $t = Team::where('name', '=', $team)->where('season_id', '=', $season_id)->first();
  145. if($t == null)
  146. {
  147. $t = new Team();
  148. $t->name = $team;
  149. $t->type = '';
  150. $t->season_id = $season_id;
  151. $t->save();
  152. }
  153. $ret = $t->id;
  154. }
  155. return $ret;
  156. }
  157. /**
  158. * Display the specified resource.
  159. *
  160. * @param \App\Calendar $calendar
  161. * @return \Illuminate\Http\Response
  162. */
  163. public function show(Calendar $calendar)
  164. {
  165. return view('calendars.show',compact('calendar'));
  166. }
  167. /**
  168. * Show the form for editing the specified resource.
  169. *
  170. * @param \App\Calendar $calendar
  171. * @return \Illuminate\Http\Response
  172. */
  173. public function edit(Calendar $calendar)
  174. {
  175. $seasons = Season::orderBy('name')->pluck('name', 'id')->toArray();;
  176. return view('calendars.edit',compact('calendar', 'seasons'));
  177. }
  178. /**
  179. * Update the specified resource in storage.
  180. *
  181. * @param \Illuminate\Http\Request $request
  182. * @param \App\Calendar $calendar
  183. * @return \Illuminate\Http\Response
  184. */
  185. public function update(Request $request, Calendar $calendar)
  186. {
  187. $request->validate($this->rules);
  188. $input = $request->all();
  189. $input["name"] = '';
  190. // Creo le giornate
  191. if(request()->file)
  192. {
  193. // Elimino il vecchio
  194. $aTeams = array();
  195. $games = CalendarGame::where('calendar_id', '=', $calendar->id)->get();
  196. foreach($games as $game)
  197. {
  198. $home_team_id = $game->home_team_id;
  199. $away_team_id = $game->away_team_id;
  200. if ($home_team_id != null)
  201. {
  202. if(!in_array($home_team_id, $aTeams, true))
  203. {
  204. array_push($aTeams, $home_team_id);
  205. }
  206. }
  207. if ($away_team_id != null)
  208. {
  209. if(!in_array($away_team_id, $aTeams, true))
  210. {
  211. array_push($aTeams, $away_team_id);
  212. }
  213. }
  214. $game->delete();
  215. }
  216. foreach($aTeams as $team_id)
  217. {
  218. $th = Team::where('id', '=', $team_id)->first();
  219. if ($th != null)
  220. $th->delete();
  221. }
  222. $file = request()->file;
  223. $filename = time() . '_' . $file->getClientOriginalName();
  224. if (! File::exists(public_path()."/files/calendars"))
  225. File::makeDirectory(public_path()."/files/calendars");
  226. $path = public_path('files/calendars');
  227. request()->file->move($path, $filename);
  228. $handle = fopen($path . "/" . $filename, "r");
  229. if ($handle)
  230. {
  231. $aTeams = array();
  232. $aT = array("ANDATA", "RITORNO");
  233. foreach($aT as $type)
  234. {
  235. $date = '';
  236. $day = 0;
  237. $insert = false;
  238. $handle = fopen($path . "/" . $filename, "r");
  239. while (($line = fgets($handle)) !== false)
  240. {
  241. if (substr($line, 0, 3) == '.--' || substr($line, 0, 3) == '|--')
  242. {
  243. $insert = false;
  244. }
  245. if (strpos($line, $type . ':') !== false)
  246. {
  247. $day += 1;
  248. $tmp = $line;
  249. if ($type == 'ANDATA')
  250. {
  251. $start = strpos($tmp, '|');
  252. $end = strpos($tmp, '|', 1);
  253. }
  254. else
  255. {
  256. $start = strpos($tmp, 'RITORNO: ');
  257. $end = strrpos($tmp, '|');
  258. }
  259. if ($type == 'ANDATA')
  260. {
  261. $tmp = substr($tmp, $start + 1, $end - 1);
  262. $tmp = str_replace($type . ":", "", $tmp);
  263. }
  264. else
  265. {
  266. $tmp = substr($tmp, $start + 8, 10);
  267. }
  268. $tmp = trim($tmp);
  269. list($dt, $month, $year) = explode("/", $tmp);
  270. $date = "20" . $year . "-" . $month . "-" . $dt;
  271. }
  272. if($insert)
  273. {
  274. $tmp = $line;
  275. $tmp = trim(str_replace("|", "", $tmp));
  276. list($home, $away) = explode("-", $tmp);
  277. $home_team_id = $this->getTeam($home, $calendar->season_id);
  278. $away_team_id = $this->getTeam($away, $calendar->season_id);
  279. $calendar_game = new CalendarGame();
  280. $calendar_game->calendar_id = $calendar->id;
  281. $calendar_game->home_team_id = $type == 'ANDATA' ? $home_team_id : $away_team_id;
  282. $calendar_game->away_team_id = $type == 'ANDATA' ? $away_team_id : $home_team_id;
  283. $calendar_game->date = $date;
  284. $calendar_game->day = $day;
  285. $calendar_game->type = $type;
  286. $calendar_game->home_goals = null;
  287. $calendar_game->away_goals = null;
  288. $calendar_game->home_points = null;
  289. $calendar_game->away_points = null;
  290. $calendar_game->save();
  291. }
  292. if (substr($line, 0, 3) == '|--')
  293. {
  294. $insert = true;
  295. }
  296. }
  297. }
  298. fclose($handle);
  299. }
  300. else
  301. {
  302. }
  303. }
  304. $calendar->update($input);
  305. return redirect()->route('calendars.index')
  306. ->with('success','Calendar updated successfully');
  307. }
  308. /**
  309. * Remove the specified resource from storage.
  310. *
  311. * @param \App\Calendar $calendar
  312. * @return \Illuminate\Http\Response
  313. */
  314. public function destroy(Calendar $calendar)
  315. {
  316. $aTeams = array();
  317. $games = CalendarGame::where('calendar_id', '=', $calendar->id)->get();
  318. foreach($games as $game)
  319. {
  320. $home_team_id = $game->home_team_id;
  321. $away_team_id = $game->away_team_id;
  322. if ($home_team_id != null)
  323. {
  324. if(!in_array($home_team_id, $aTeams, true))
  325. {
  326. array_push($aTeams, $home_team_id);
  327. }
  328. }
  329. if ($away_team_id != null)
  330. {
  331. if(!in_array($away_team_id, $aTeams, true))
  332. {
  333. array_push($aTeams, $away_team_id);
  334. }
  335. }
  336. $game->delete();
  337. }
  338. foreach($aTeams as $team_id)
  339. {
  340. $th = Team::where('id', '=', $team_id)->first();
  341. if ($th != null)
  342. $th->delete();
  343. }
  344. // CalendarGame::where('calendar_id', '=', $calendar->id)->delete();
  345. $calendar->delete();
  346. return redirect()->route('calendars.index')
  347. ->with('success','Calendar deleted successfully');
  348. }
  349. }