コード例 #1
0
 public static function store()
 {
     $params = $_POST;
     $rain = isset($_POST['rain']) && $_POST['rain'] ? "1" : "0";
     // checked=1, unchecked=0
     $wet_no_rain = isset($_POST['wet_no_rain']) && $_POST['wet_no_rain'] ? "1" : "0";
     // checked=1, unchecked=0
     $windy = isset($_POST['windy']) && $_POST['windy'] ? "1" : "0";
     // checked=1, unchecked=0
     $variant = isset($_POST['variant']) && $_POST['variant'] ? "1" : "0";
     // checked=1, unchecked=0
     $dark = isset($_POST['dark']) && $_POST['dark'] ? "1" : "0";
     // checked=1, unchecked=0
     $led = isset($_POST['led']) && $_POST['led'] ? "1" : "0";
     // checked=1, unchecked=0
     $snow = isset($_POST['snow']) && $_POST['snow'] ? "1" : "0";
     // checked=1, unchecked=0
     $date = $_POST['date'];
     $time = $_POST['time'];
     $comment = $_POST['comment'];
     $courseid = $_POST['courseid'];
     $gamedate = $date . " " . $time . ":00";
     $game = new Game(array('courseid' => $courseid, 'gamedate' => $gamedate, 'comment' => $comment, 'rain' => $rain, 'wet_no_rain' => $wet_no_rain, 'windy' => $windy, 'variant' => $variant, 'dark' => $dark, 'led' => $led, 'snow' => $snow));
     $errors = $game->errors();
     $course = Course::find($courseid);
     $scores = array();
     // When implementing multiple players per game, cycle through playerid's here
     $playerid = $_POST['playerid'];
     foreach ($course->holes as $hole) {
         $stroke = $_POST['hole' . $hole->hole_num];
         // 'holeN' will be something like 'playername-holeN'
         $ob = $_POST['obhole' . $hole->hole_num];
         $score = new Score(array('holeid' => $hole->holeid, 'playerid' => $playerid, 'stroke' => $stroke, 'ob' => $ob));
         $errors = array_merge($errors, $score->errors());
         $scores[] = $score;
     }
     if (count($errors) == 0) {
         // Game and scores were all valid
         $gameid = $game->save();
         foreach ($scores as $score) {
             $score->gameid = $gameid;
             $score->save();
         }
         Redirect::to('/game/' . $game->gameid, array('message' => 'Peli ja sen tulokset lisätty.'));
     } else {
         View::make('game/new.html', array('errors' => $errors, 'attributes' => $params, 'course' => $course));
     }
 }
コード例 #2
0
 public static function store()
 {
     $params = $_POST;
     $rain = isset($_POST['rain']) && $_POST['rain'] ? "1" : "0";
     // checked=1, unchecked=0
     $wet_no_rain = isset($_POST['wet_no_rain']) && $_POST['wet_no_rain'] ? "1" : "0";
     // checked=1, unchecked=0
     $windy = isset($_POST['windy']) && $_POST['windy'] ? "1" : "0";
     // checked=1, unchecked=0
     $variant = isset($_POST['variant']) && $_POST['variant'] ? "1" : "0";
     // checked=1, unchecked=0
     $dark = isset($_POST['dark']) && $_POST['dark'] ? "1" : "0";
     // checked=1, unchecked=0
     $led = isset($_POST['led']) && $_POST['led'] ? "1" : "0";
     // checked=1, unchecked=0
     $snow = isset($_POST['snow']) && $_POST['snow'] ? "1" : "0";
     // checked=1, unchecked=0
     $doubles = isset($_POST['doubles']) && $_POST['doubles'] ? "1" : "0";
     // checked=1, unchecked=0
     $temp = $_POST['temp'] != "" ? $_POST['temp'] : null;
     // temperature can be null (or 0!)
     $date = $_POST['date'];
     $time = $_POST['time'];
     $comment = $_POST['comment'];
     $courseid = $_POST['courseid'];
     $gamedate = $date . " " . $time . ":00";
     $game = new Game(array('courseid' => $courseid, 'gamedate' => $gamedate, 'comment' => $comment, 'rain' => $rain, 'wet_no_rain' => $wet_no_rain, 'windy' => $windy, 'variant' => $variant, 'dark' => $dark, 'led' => $led, 'snow' => $snow, 'doubles' => $doubles, 'temp' => $temp));
     $errors = $game->errors();
     $course = Course::find($courseid);
     if (isset($_FILES['csv']['tmp_name'])) {
         // Scores will be read from a CSV file.
         $tmpName = $_FILES['csv']['tmp_name'];
         if (!empty($tmpName)) {
             $csvAsArray = array_map('str_getcsv', file($tmpName));
         } else {
             $errors[] = "Valitse CSV-tiedosto.";
         }
         if (count($errors) == 0) {
             // Game was valid
             $gameid = $game->save();
             // Read and save scores
             $score_errors = CSVScoreProcessor::process($csvAsArray, $gameid, $course);
             if (count($score_errors) == 0) {
                 // Scores were valid as well
                 // Clear cached pages
                 Cache::clear();
                 Redirect::to('/game/' . $game->gameid, array('message' => 'Peli ja sen tulokset lisätty.'));
             } else {
                 // Scores had errors, nothing was saved
                 $errors = array_merge($errors, $score_errors);
                 View::make('game/new.html', array('errors' => $errors, 'attributes' => $params, 'course' => $course));
             }
         } else {
             View::make('game/new.html', array('errors' => $errors, 'attributes' => $params, 'course' => $course));
         }
     } else {
         // Scores will be read from input forms.
         $scores = array();
         // Game's players
         $players = array();
         foreach (Player::all() as $player) {
             if (isset($_POST['player' . $player->playerid])) {
                 $players[] = $player;
             }
         }
         // Cycle through players
         foreach ($players as $player) {
             $legal_index = 'legal-player' . $player->playerid;
             $legal = isset($_POST[$legal_index]) && $_POST[$legal_index] ? "1" : "0";
             // checked=1, unchecked=0
             foreach ($course->holes as $hole) {
                 // inputs are in format 'player1-hole1'
                 $stroke = $_POST['player' . $player->playerid . '-hole' . $hole->hole_num];
                 $ob = $_POST['player' . $player->playerid . '-obhole' . $hole->hole_num];
                 $score = new Score(array('holeid' => $hole->holeid, 'playerid' => $player->playerid, 'stroke' => $stroke, 'ob' => $ob, 'legal' => $legal));
                 $errors = array_merge($errors, $score->errors());
                 $scores[] = $score;
             }
         }
         if (count($errors) == 0) {
             // Game and scores were all valid
             $gameid = $game->save();
             foreach ($scores as $score) {
                 $score->gameid = $gameid;
                 $score->save();
             }
             // Clear cached pages
             Cache::clear();
             Redirect::to('/game/' . $game->gameid, array('message' => 'Peli ja sen tulokset lisätty.'));
         } else {
             View::make('game/new.html', array('errors' => $errors, 'attributes' => $params, 'course' => $course, 'players' => $players));
         }
     }
 }