| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- /**
- * File for the run team saving action.
- *
- * Implements an action function to be called from the {@see Controller}.
- *
- * @category Action
- */
- /**
- * Saves a run party as a team.
- *
- * Reads the POST parameters looking for the following KEYS:
- * uid
- * run
- * name
- * description
- * Validates the data and creates the team in the database.
- *
- * @return int 0 on success, negative values on error.
- * @category Action
- * @global resource Database connection.
- */
- function action(){
- global $db;
- $player = filter_input(INPUT_POST, 'uid');
- $run = filter_input(INPUT_POST, 'run');
- $name = filter_input(INPUT_POST, 'name');
- $description = filter_input(INPUT_POST, 'description');
- if ($name == null || strlen($name) == 0){
- return -1;
- }
- $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
- $q = $db->query($s);
- $r = $q->fetchArray(SQLITE3_ASSOC);
- $team_id = $r["id"];
- $statement = $db->prepare("
- SELECT
- run.area AS area,
- run.stage AS stage,
- run.difficulty AS difficulty,
- k_area.type AS area_type
- FROM
- run,
- k_area
- WHERE
- run.uid = :uid AND
- run.id = :run AND
- k_area.id = run.area AND
- run.helper = 0;
- ");
- $statement->bindValue(':uid', $player, SQLITE3_INTEGER);
- $statement->bindValue(':run', $run, SQLITE3_INTEGER);
- $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if (!$r){
- return -2;
- }
- $statement = $db->prepare('INSERT INTO team (uid, id, name, description, area_type, area, stage, difficulty, score) VALUES (:uid, :id, :name, :description, :area_type, :area, :stage, :difficulty, :score);');
- $statement->bindValue(':uid', $player, SQLITE3_INTEGER);
- $statement->bindValue(':id', $team_id, SQLITE3_INTEGER);
- $statement->bindValue(':name', $name, SQLITE3_TEXT);
- $statement->bindValue(':description', $description, SQLITE3_TEXT);
- $statement->bindValue(':area_type', $r["area_type"], SQLITE3_INTEGER);
- $statement->bindValue(':area', $r["area"], SQLITE3_INTEGER);
- $statement->bindValue(':stage', $r["stage"], SQLITE3_INTEGER);
- $statement->bindValue(':difficulty', $r["difficulty"], SQLITE3_INTEGER);
- $statement->bindValue(':score', 0, SQLITE3_INTEGER);
- $res = $statement->execute();
- if (!$res){
- return -3;
- }
- $s = "SELECT unit FROM run_party WHERE run = $run AND unit IS NOT NULL;";
- $q = $db->query($s);
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- $statement = $db->prepare("
- INSERT INTO team_unit (team, unit)
- VALUES (:team, :unit)
- ");
- $statement->bindValue(':team', $team_id, SQLITE3_INTEGER);
- $statement->bindValue(':unit', $r["unit"], SQLITE3_INTEGER);
- $statement->execute();
- }
- return 0;
- }
- ?>
|