CalendarController.php 13 KB

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