save_run_team.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. * @param resource $db Database connection.
  20. * @return int 0 on success, negative values on error.
  21. * @category Action
  22. */
  23. function action($db = null){
  24. $player = filter_input(INPUT_POST, 'uid');
  25. $run = filter_input(INPUT_POST, 'run');
  26. $name = filter_input(INPUT_POST, 'name');
  27. $description = filter_input(INPUT_POST, 'description');
  28. if ($name == null || strlen($name) == 0){
  29. return -1;
  30. }
  31. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  32. $q = $db->query($s);
  33. $r = $q->fetchArray(SQLITE3_ASSOC);
  34. $team_id = $r["id"];
  35. $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;";
  36. $q = $db->query($s);
  37. $r = $q->fetchArray(SQLITE3_ASSOC);
  38. if (!$r){
  39. return -2;
  40. }
  41. $statement = $db->prepare('INSERT INTO team (uid, id, name, description, area_type, area, stage, difficulty) VALUES (:uid, :id, :name, :description, :area_type, :area, :stage, :difficulty);');
  42. $statement->bindValue(':uid', $player);
  43. $statement->bindValue(':id', $team_id);
  44. $statement->bindValue(':name', $name);
  45. $statement->bindValue(':description', $description);
  46. $statement->bindValue(':area_type', $r["area_type"]);
  47. $statement->bindValue(':area', $r["area"]);
  48. $statement->bindValue(':stage', $r["stage"]);
  49. $statement->bindValue(':difficulty', $r["difficulty"]);
  50. $res = $statement->execute();
  51. if (!$res){
  52. return -3;
  53. }
  54. $s = "SELECT unit FROM run_party WHERE run = $run AND unit IS NOT NULL;";
  55. $q = $db->query($s);
  56. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  57. $s = "INSERT INTO team_unit VALUES ('$team_id', '" . $r["unit"] . "');";
  58. $db->query($s);
  59. }
  60. return 0;
  61. }
  62. ?>