save_run_team.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * player
  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. */
  22. function action(){
  23. $player = filter_input(INPUT_POST, 'player');
  24. $run = filter_input(INPUT_POST, 'run');
  25. $name = filter_input(INPUT_POST, 'name');
  26. $description = filter_input(INPUT_POST, 'description');
  27. if ($name == null || strlen($name) == 0){
  28. return -1;
  29. }
  30. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  31. $q = get_context()->get_db()->query($s);
  32. $r = $q->fetchArray(SQLITE3_ASSOC);
  33. $team_id = $r["id"];
  34. $statement = get_context()->get_db()->prepare("
  35. SELECT
  36. run.area AS area,
  37. run.stage AS stage,
  38. run.difficulty AS difficulty,
  39. area.type AS area_type
  40. FROM
  41. run,
  42. area
  43. WHERE
  44. run.player = :player AND
  45. run.id = :run AND
  46. area.id = run.area AND
  47. run.helper = 0;
  48. ");
  49. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  50. $statement->bindValue(':run', $run, SQLITE3_TEXT);
  51. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  52. if (!$r){
  53. return -2;
  54. }
  55. $statement = get_context()->get_db()->prepare("
  56. INSERT INTO team (
  57. player,
  58. id,
  59. name,
  60. description,
  61. area_type,
  62. area,
  63. stage,
  64. difficulty,
  65. score
  66. ) VALUES (
  67. :player,
  68. :id,
  69. :name,
  70. :description,
  71. :area_type,
  72. :area,
  73. :stage,
  74. :difficulty,
  75. :score);
  76. ");
  77. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  78. $statement->bindValue(':id', $team_id, SQLITE3_TEXT);
  79. $statement->bindValue(':name', $name, SQLITE3_TEXT);
  80. $statement->bindValue(':description', $description, SQLITE3_TEXT);
  81. $statement->bindValue(':area_type', $r["area_type"], SQLITE3_TEXT);
  82. $statement->bindValue(':area', $r["area"], SQLITE3_TEXT);
  83. $statement->bindValue(':stage', $r["stage"], SQLITE3_TEXT);
  84. $statement->bindValue(':difficulty', $r["difficulty"], SQLITE3_TEXT);
  85. $statement->bindValue(':score', 0, SQLITE3_TEXT);
  86. $res = $statement->execute();
  87. if (!$res){
  88. return -3;
  89. }
  90. $s = "SELECT unit FROM data.run_party WHERE run = $run AND unit IS NOT NULL;";
  91. $q = get_context()->get_db()->query($s);
  92. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  93. $statement = get_context()->get_db()->prepare("
  94. INSERT INTO data.team_unit (team, unit)
  95. VALUES (:team, :unit)
  96. ");
  97. $statement->bindValue(':team', $team_id, SQLITE3_TEXT);
  98. $statement->bindValue(':unit', $r["unit"], SQLITE3_TEXT);
  99. $statement->execute();
  100. }
  101. return 0;
  102. }
  103. ?>