CalendarController.php 13 KB

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