index.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. require "settings.php";
  3. $pdo = null;
  4. try {
  5. $pdo = new PDO("sqlite:" . "${db_path}/slork.sqlite");
  6. } catch (PDOException $e) {
  7. // handle the exception here
  8. }
  9. require "functions.php";
  10. /**
  11. * BUSINESS LOGIC
  12. *
  13. * $screen = 0; // start
  14. * 1) First we validate or create a maintainer.
  15. * Adding a maintainer is a detour but results
  16. * in the start page with the new maintainer added
  17. * to be selected.
  18. * $_REQUEST['action'] == 'i'; // New maintainer
  19. * $_REQUEST['action'] == 'm'; // Maintainer selected
  20. * 1a) Cannot validate new maintainer; back to
  21. * $screen = 0;
  22. * 2) The maintainer will be presented a page with
  23. * players. He can edit or remove a player.
  24. * $screen = 3; $_REQUEST['action'] == 'm'; // edit player
  25. * $screen = 4; $_REQUEST['action'] == 'x'; // remove player
  26. * 3) The edit page allows the maintainer to add
  27. * or remove features from the player.
  28. */
  29. // global
  30. $screen = 0;
  31. $maintainer = array(0, '?');
  32. $action = '';
  33. if ( isset( $_REQUEST['action'] ) ) {
  34. $action = $_REQUEST['action'];
  35. }
  36. if ( $action == 'i' ) {
  37. // Try adding new maintainer ...
  38. $maintainer = add_maintainer( $_REQUEST['api'] );
  39. // ... then return to start screen
  40. $screen = 0;
  41. }
  42. if ( $action == 'm' ) {
  43. // There is a maintainer
  44. $maintainer = get_maintainer( $_REQUEST['maintainer'] );
  45. $screen = 2;
  46. }
  47. if ( $action == 'p' ) {
  48. // New player
  49. $maintainer = add_player( $_REQUEST['maintainer'], $_REQUEST['player'] );
  50. if ( $maintainer[0] < 100 ) {
  51. print( "<pre>" );
  52. print("ERROR ${maintainer[0]}: ${maintainer[1]}" );
  53. print("</pre>" );
  54. $maintainer = get_maintainer( $_REQUEST['maintainer'] );
  55. }
  56. $screen = 2;
  57. }
  58. if ( $action == 'e' ) {
  59. // Request for edit screen
  60. $maintainer = get_maintainer( $_REQUEST['maintainer'] );
  61. $screen = 3;
  62. }
  63. if ( $action == 's' ) {
  64. // Getting the Edit array back from the maintainer
  65. // Store preferences and return to Edit screen
  66. $maintainer = get_maintainer( $_REQUEST['maintainer'] );
  67. $player = $_REQUEST['player'];
  68. $properties = $_REQUEST['properties'];
  69. if ( in_array( 'ALL', $properties ) ) {
  70. $query = <<<QUERY
  71. SELECT DISTINCT property FROM properties;
  72. QUERY;
  73. $stmt = $pdo->prepare( $query );
  74. $stmt->execute();
  75. $rows = $stmt->fetchAll();
  76. $properties = [];
  77. foreach( $rows as $row ) {
  78. $properties[] = $row['property'];
  79. }
  80. }
  81. if ( in_array( 'NONE', $properties ) ) {
  82. $properties = [];
  83. $properties[] = "name";
  84. }
  85. store_player_properties( $player, $maintainer, $properties );
  86. $screen = 3;
  87. }
  88. if ( $action == 'x' ) {
  89. // Request to remove player
  90. $maintainer = delete_player( $_REQUEST['player'] );
  91. if ( $maintainer[0] == 0 ) {
  92. $screen = 0;
  93. } else {
  94. $screen = 2;
  95. }
  96. }
  97. assert( '$screen >= 0', "By now \$screen should be set to a value >= 0." );
  98. ?>
  99. <!doctype html>
  100. <html lang="en">
  101. <head>
  102. <!-- Required meta tags -->
  103. <meta charset="utf-8">
  104. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  105. <!-- Bootstrap CSS -->
  106. <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  107. <!-- Latest compiled and minified CSS -->
  108. <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">
  109. <title>Slork</title>
  110. </head>
  111. <body>
  112. <div class="container">
  113. <div class="row">
  114. <div class="col-md">
  115. One of three columns
  116. </div>
  117. <div class="col-md">
  118. <?php
  119. require "snippets.php";
  120. if ( $maintainer[0] > 0 && $maintainer[0] < 100 ) {
  121. // Error in creating maintainer
  122. print( "<pre>" );
  123. print( "ERROR $maintainer[0]: $maintainer[1]" );
  124. print( "</pre>" );
  125. // Try again
  126. $screen = 0;
  127. }
  128. // Choose or add a maintainer; default screen
  129. print( $snippets[$screen] );
  130. ?>
  131. </div>
  132. <div class="col-md">
  133. <pre>
  134. <?php
  135. print_r( $_REQUEST );
  136. ?>
  137. </pre>
  138. </div>
  139. </div>
  140. </div>
  141. <!-- Optional JavaScript -->
  142. <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  143. <script src="//code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  144. <script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  145. <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  146. <!-- Latest compiled and minified JavaScript -->
  147. <script src="//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>
  148. </body>
  149. </html>