Delete_Team_Action.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * File for the team deletion 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. * Deletes a team.
  14. *
  15. * Reads the POST parameters looking for the following KEYS:
  16. * id: the team id.
  17. *
  18. * @category Action
  19. */
  20. class Delete_Team_Action extends Action{
  21. /**
  22. * Executes the action.
  23. */
  24. function execute(){
  25. $id = filter_input(INPUT_POST, 'id');
  26. $statement = get_context()->get_db()->prepare("
  27. SELECT count(team.id) AS c
  28. FROM
  29. team,
  30. player
  31. WHERE
  32. team.id = :team AND
  33. player.user = :user
  34. ");
  35. $statement->bindValue(':team', $id, SQLITE3_TEXT);
  36. $statement->bindValue(':user', get_context()->get_user()->get_id(), SQLITE3_TEXT);
  37. if ($statement->execute()->fetchArray(SQLITE3_ASSOC)["c"] == 0){
  38. $this->code = 401;
  39. $this->message = "Unauthorized";
  40. }
  41. else{
  42. $statement = get_context()->get_db()->prepare("DELETE FROM team WHERE id = :id;");
  43. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  44. $statement->execute();
  45. $statement = get_context()->get_db()->prepare("DELETE FROM team_unit WHERE team = :id;");
  46. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  47. $statement->execute();
  48. $this->code = 204;
  49. $this->message = "No Content";
  50. }
  51. return;
  52. }
  53. }