api.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. session_name('default');
  4. session_set_cookie_params(0, '/', '.tankernn.eu');
  5. session_start();
  6. // Database settings
  7. require ("config.php");
  8. $data = new StdClass();
  9. if (!isset($_SESSION['userid'])) {
  10. $data->result = 1;
  11. } else {
  12. $userid = $_SESSION['userid'];
  13. if (isset($_POST['a'])) {
  14. // Additional actions to perform before returning the list
  15. switch ($_POST['a']) {
  16. case 'edit':
  17. $id = $_POST['id'];
  18. case 'add':
  19. $title = $conn->escapeString($_POST['title']);
  20. $text = $conn->escapeString($_POST['text']);
  21. $deadline = $_POST['deadline'];
  22. $priority = $_POST['priority'];
  23. if (strtotime($deadline) <= time()) {
  24. $deadline = "";
  25. }
  26. if (isset($id)) {
  27. $sql = "UPDATE Todo SET priority=$priority, deadline='$deadline', title='$title', description='$text' WHERE userid=$userid AND id=$id";
  28. } else {
  29. $sql = "INSERT INTO Todo (userid, priority, deadline, title, description) VALUES ($userid, $priority, '$deadline', '$title', '$text')";
  30. }
  31. break;
  32. case 'rm':
  33. $id = $_POST['id'];
  34. $sql = "DELETE FROM Todo WHERE id=$id AND userid=$userid";
  35. break;
  36. }
  37. $data->result = $conn->query($sql) ? 0 : $conn->error;
  38. }
  39. $sql = "SELECT * FROM Todo WHERE userid=$userid ORDER BY (CASE deadline WHEN '0000-00-00' THEN 1 ELSE 0 END), deadline ASC, priority ASC";
  40. $query = $conn->query($sql);
  41. $data->list = array();
  42. while ($row = $query->fetchArray()) {
  43. array_push($data->list, $row);
  44. }
  45. }
  46. echo json_encode($data);
  47. ?>