save_run_team.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * File for the run team saving action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @category Action
  8. */
  9. /**
  10. * Saves a run party as a team.
  11. *
  12. * Reads the POST parameters looking for the following KEYS:
  13. * uid
  14. * run
  15. * name
  16. * description
  17. * Validates the data and creates the team in the database.
  18. *
  19. * @return int 0 on success, negative values on error.
  20. * @category Action
  21. * @global resource Database connection.
  22. */
  23. function action(){
  24. global $db;
  25. $player = filter_input(INPUT_POST, 'uid');
  26. $run = filter_input(INPUT_POST, 'run');
  27. $name = filter_input(INPUT_POST, 'name');
  28. $description = filter_input(INPUT_POST, 'description');
  29. if ($name == null || strlen($name) == 0){
  30. return -1;
  31. }
  32. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  33. $q = $db->query($s);
  34. $r = $q->fetchArray(SQLITE3_ASSOC);
  35. $team_id = $r["id"];
  36. $s = "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 = '$player' AND run.id = '$run' AND k_area.id = run.area AND run.helper = 0;";
  37. $q = $db->query($s);
  38. $r = $q->fetchArray(SQLITE3_ASSOC);
  39. if (!$r){
  40. return -2;
  41. }
  42. $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);');
  43. $statement->bindValue(':uid', $player);
  44. $statement->bindValue(':id', $team_id);
  45. $statement->bindValue(':name', $name);
  46. $statement->bindValue(':description', $description);
  47. $statement->bindValue(':area_type', $r["area_type"]);
  48. $statement->bindValue(':area', $r["area"]);
  49. $statement->bindValue(':stage', $r["stage"]);
  50. $statement->bindValue(':difficulty', $r["difficulty"]);
  51. $statement->bindValue(':score', $score);
  52. $res = $statement->execute();
  53. if (!$res){
  54. return -3;
  55. }
  56. $s = "SELECT unit FROM run_party WHERE run = $run AND unit IS NOT NULL;";
  57. $q = $db->query($s);
  58. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  59. $s = "INSERT INTO team_unit VALUES ('$team_id', '" . $r["unit"] . "');";
  60. $db->query($s);
  61. }
  62. return 0;
  63. }
  64. ?>