| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?php
- namespace App\Http\Controllers;
- use App\Calendar;
- use App\CalendarGame;
- use App\Season;
- use App\Team;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\File;
- class CalendarController extends Controller
- {
- protected $rules = [
- // 'name' => ['required']
- ];
- public function index()
- {
- if (\Illuminate\Support\Facades\Auth::user()->level == 1)
- return redirect('/admin/dashboard');
-
- $calendars = Calendar::orderBy('position')->orderBy('name')->get(); //->paginate(50);
- return view('calendars.index',compact('calendars'))
- ->with('i', (request()->input('page', 1) - 1) * 5);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- $seasons = Season::orderBy('name')->pluck('name', 'id')->toArray();;
- return view('calendars.create', compact('seasons'));
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- //CalendarGame::truncate();
- //Calendar::truncate();
- $request->validate($this->rules);
- $input = $request->all();
- $input["name"] = '';
- $calendar = Calendar::create($input);
- // Creo le giornate
- if(request()->file)
- {
- $file = request()->file;
- $filename = time() . '_' . $file->getClientOriginalName();
- if (! File::exists(public_path()."/files/calendars"))
- File::makeDirectory(public_path()."/files/calendars");
- $path = public_path('files/calendars');
- request()->file->move($path, $filename);
- $handle = fopen($path . "/" . $filename, "r");
- if ($handle)
- {
- $aTeams = array();
- $aT = array("ANDATA", "RITORNO");
- foreach($aT as $type)
- {
- $date = '';
- $day = 0;
- $insert = false;
- $handle = fopen($path . "/" . $filename, "r");
- while (($line = fgets($handle)) !== false)
- {
- if (substr($line, 0, 3) == '.--' || substr($line, 0, 3) == '|--')
- {
- $insert = false;
- }
- if (strpos($line, $type . ':') !== false)
- {
- $day += 1;
- $tmp = $line;
- if ($type == 'ANDATA')
- {
- $start = strpos($tmp, '|');
- $end = strpos($tmp, '|', 1);
- }
- else
- {
- $start = strpos($tmp, 'RITORNO: ');
- $end = strrpos($tmp, '|');
- }
- if ($type == 'ANDATA')
- {
- $tmp = substr($tmp, $start + 1, $end - 1);
- $tmp = str_replace($type . ":", "", $tmp);
- }
- else
- {
- $tmp = substr($tmp, $start + 8, 10);
- }
- $tmp = trim($tmp);
- list($dt, $month, $year) = explode("/", $tmp);
- $date = "20" . $year . "-" . $month . "-" . $dt;
- }
- if($insert)
- {
- $tmp = $line;
- $tmp = trim(str_replace("|", "", $tmp));
- list($home, $away) = explode("-", $tmp);
- $home_team_id = $this->getTeam($home, $calendar->season_id);
- $away_team_id = $this->getTeam($away, $calendar->season_id);
- $calendar_game = new CalendarGame();
- $calendar_game->calendar_id = $calendar->id;
- $calendar_game->home_team_id = $type == 'ANDATA' ? $home_team_id : $away_team_id;
- $calendar_game->away_team_id = $type == 'ANDATA' ? $away_team_id : $home_team_id;
- $calendar_game->date = $date;
- $calendar_game->day = $day;
- $calendar_game->type = $type;
- $calendar_game->home_goals = null;
- $calendar_game->away_goals = null;
- $calendar_game->home_points = null;
- $calendar_game->away_points = null;
- $calendar_game->save();
- }
- if (substr($line, 0, 3) == '|--')
- {
- $insert = true;
- }
- }
- }
- fclose($handle);
- }
- else
- {
- }
- }
- return redirect()->route('calendars.index')
- ->with('success','Calendar created successfully.');
- }
- public function getTeam($team, $season_id)
- {
- $ret = 0;
- $team = trim($team);
- if (strpos(strtolower($team), 'riposa') !== false)
- {
- $ret = null;
- }
- else
- {
- $t = Team::where('name', '=', $team)->where('season_id', '=', $season_id)->first();
- if($t == null)
- {
- $t = new Team();
- $t->name = $team;
- $t->type = '';
- $t->season_id = $season_id;
- $t->save();
- }
- $ret = $t->id;
- }
- return $ret;
- }
- /**
- * Display the specified resource.
- *
- * @param \App\Calendar $calendar
- * @return \Illuminate\Http\Response
- */
- public function show(Calendar $calendar)
- {
- return view('calendars.show',compact('calendar'));
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\Calendar $calendar
- * @return \Illuminate\Http\Response
- */
- public function edit(Calendar $calendar)
- {
- $seasons = Season::orderBy('name')->pluck('name', 'id')->toArray();;
- return view('calendars.edit',compact('calendar', 'seasons'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param \App\Calendar $calendar
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, Calendar $calendar)
- {
- $request->validate($this->rules);
- $input = $request->all();
- $input["name"] = '';
- // Creo le giornate
- if(request()->file)
- {
- // Elimino il vecchio
- $aTeams = array();
- $games = CalendarGame::where('calendar_id', '=', $calendar->id)->get();
- foreach($games as $game)
- {
- $home_team_id = $game->home_team_id;
- $away_team_id = $game->away_team_id;
- if ($home_team_id != null)
- {
- if(!in_array($home_team_id, $aTeams, true))
- {
- array_push($aTeams, $home_team_id);
- }
- }
- if ($away_team_id != null)
- {
- if(!in_array($away_team_id, $aTeams, true))
- {
- array_push($aTeams, $away_team_id);
- }
- }
- $game->delete();
- }
- foreach($aTeams as $team_id)
- {
- $th = Team::where('id', '=', $team_id)->first();
- if ($th != null)
- $th->delete();
- }
- $file = request()->file;
- $filename = time() . '_' . $file->getClientOriginalName();
- if (! File::exists(public_path()."/files/calendars"))
- File::makeDirectory(public_path()."/files/calendars");
- $path = public_path('files/calendars');
- request()->file->move($path, $filename);
- $handle = fopen($path . "/" . $filename, "r");
- if ($handle)
- {
- $aTeams = array();
- $aT = array("ANDATA", "RITORNO");
- foreach($aT as $type)
- {
- $date = '';
- $day = 0;
- $insert = false;
- $handle = fopen($path . "/" . $filename, "r");
- while (($line = fgets($handle)) !== false)
- {
- if (substr($line, 0, 3) == '.--' || substr($line, 0, 3) == '|--')
- {
- $insert = false;
- }
- if (strpos($line, $type . ':') !== false)
- {
- $day += 1;
- $tmp = $line;
- if ($type == 'ANDATA')
- {
- $start = strpos($tmp, '|');
- $end = strpos($tmp, '|', 1);
- }
- else
- {
- $start = strpos($tmp, 'RITORNO: ');
- $end = strrpos($tmp, '|');
- }
- if ($type == 'ANDATA')
- {
- $tmp = substr($tmp, $start + 1, $end - 1);
- $tmp = str_replace($type . ":", "", $tmp);
- }
- else
- {
- $tmp = substr($tmp, $start + 8, 10);
- }
- $tmp = trim($tmp);
- list($dt, $month, $year) = explode("/", $tmp);
- $date = "20" . $year . "-" . $month . "-" . $dt;
- }
- if($insert)
- {
- $tmp = $line;
- $tmp = trim(str_replace("|", "", $tmp));
- list($home, $away) = explode("-", $tmp);
- $home_team_id = $this->getTeam($home, $calendar->season_id);
- $away_team_id = $this->getTeam($away, $calendar->season_id);
- $calendar_game = new CalendarGame();
- $calendar_game->calendar_id = $calendar->id;
- $calendar_game->home_team_id = $type == 'ANDATA' ? $home_team_id : $away_team_id;
- $calendar_game->away_team_id = $type == 'ANDATA' ? $away_team_id : $home_team_id;
- $calendar_game->date = $date;
- $calendar_game->day = $day;
- $calendar_game->type = $type;
- $calendar_game->home_goals = null;
- $calendar_game->away_goals = null;
- $calendar_game->home_points = null;
- $calendar_game->away_points = null;
- $calendar_game->save();
- }
- if (substr($line, 0, 3) == '|--')
- {
- $insert = true;
- }
- }
- }
- fclose($handle);
- }
- else
- {
- }
- }
- $calendar->update($input);
- return redirect()->route('calendars.index')
- ->with('success','Calendar updated successfully');
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\Calendar $calendar
- * @return \Illuminate\Http\Response
- */
- public function destroy(Calendar $calendar)
- {
- $aTeams = array();
- $games = CalendarGame::where('calendar_id', '=', $calendar->id)->get();
- foreach($games as $game)
- {
- $home_team_id = $game->home_team_id;
- $away_team_id = $game->away_team_id;
- if ($home_team_id != null)
- {
- if(!in_array($home_team_id, $aTeams, true))
- {
- array_push($aTeams, $home_team_id);
- }
- }
- if ($away_team_id != null)
- {
- if(!in_array($away_team_id, $aTeams, true))
- {
- array_push($aTeams, $away_team_id);
- }
- }
- $game->delete();
- }
- foreach($aTeams as $team_id)
- {
- $th = Team::where('id', '=', $team_id)->first();
- if ($th != null)
- $th->delete();
- }
- // CalendarGame::where('calendar_id', '=', $calendar->id)->delete();
- $calendar->delete();
- return redirect()->route('calendars.index')
- ->with('success','Calendar deleted successfully');
- }
- }
|