Save_Team_Action.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::ACTION . "Action.php");
  12. /**
  13. * Saves a run party as a team.
  14. *
  15. * Reads the POST parameters looking for the following KEYS:
  16. * player: Player ID.
  17. * run: Run ID.
  18. * name: New team name.
  19. * description: New team description.
  20. * score: New team score, defaults to 0.
  21. * Validates the data and creates the team in the database.
  22. *
  23. * @category Action
  24. */
  25. class Save_Team_Action extends Action{
  26. /**
  27. * Executes the action.
  28. */
  29. function execute(){
  30. $player = filter_input(INPUT_POST, 'player');
  31. $run = filter_input(INPUT_POST, 'run');
  32. $name = filter_input(INPUT_POST, 'name');
  33. $description = filter_input(INPUT_POST, 'description');
  34. $score = intval(filter_input(INPUT_POST, 'score'));
  35. if ($name == null || strlen($name) == 0){
  36. return 400;
  37. }
  38. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  39. $q = get_context()->get_db()->query($s);
  40. $r = $q->fetchArray(SQLITE3_ASSOC);
  41. $team_id = $r["id"];
  42. $statement = get_context()->get_db()->prepare("
  43. SELECT
  44. run.area AS area,
  45. run.stage AS stage,
  46. run.difficulty AS difficulty,
  47. area.type AS area_type
  48. FROM
  49. run,
  50. area
  51. WHERE
  52. run.player = :player AND
  53. run.id = :run AND
  54. area.id = run.area AND
  55. run.helper = 0;
  56. ");
  57. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  58. $statement->bindValue(':run', $run, SQLITE3_TEXT);
  59. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  60. if (! $r){
  61. $this->code = 204;
  62. $this->message = "Run not found";
  63. return;
  64. }
  65. $statement = get_context()->get_db()->prepare("
  66. INSERT INTO team (
  67. player,
  68. id,
  69. name,
  70. description,
  71. area_type,
  72. area,
  73. stage,
  74. difficulty,
  75. score
  76. ) VALUES (
  77. :player,
  78. :id,
  79. :name,
  80. :description,
  81. :area_type,
  82. :area,
  83. :stage,
  84. :difficulty,
  85. :score);
  86. ");
  87. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  88. $statement->bindValue(':id', $team_id, SQLITE3_TEXT);
  89. $statement->bindValue(':name', $name, SQLITE3_TEXT);
  90. $statement->bindValue(':description', $description, SQLITE3_TEXT);
  91. $statement->bindValue(':area_type', $r["area_type"], SQLITE3_TEXT);
  92. $statement->bindValue(':area', $r["area"], SQLITE3_TEXT);
  93. $statement->bindValue(':stage', $r["stage"], SQLITE3_TEXT);
  94. $statement->bindValue(':difficulty', $r["difficulty"], SQLITE3_TEXT);
  95. $statement->bindValue(':score', $score, SQLITE3_INTEGER);
  96. $res = $statement->execute();
  97. if (! $res){
  98. $this->code = 204;
  99. $this->message = "Internal error";
  100. return;
  101. }
  102. $s = "SELECT unit FROM data.run_party WHERE run = $run AND unit IS NOT NULL;";
  103. $q = get_context()->get_db()->query($s);
  104. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  105. $statement = get_context()->get_db()->prepare("
  106. INSERT INTO data.team_unit (team, unit)
  107. VALUES (:team, :unit)
  108. ");
  109. $statement->bindValue(':team', $team_id, SQLITE3_TEXT);
  110. $statement->bindValue(':unit', $r["unit"], SQLITE3_TEXT);
  111. $statement->execute();
  112. }
  113. $this->code = 204;
  114. $this->message = "No content";
  115. return;
  116. }
  117. }