save_run_team.php 3.4 KB

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