index.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. print( "<pre>" );
  86. print_r( $properties );
  87. print( "</pre>" );
  88. store_player_properties( $player, $maintainer, $properties );
  89. $screen = 3;
  90. }
  91. if ( $action == 'r' ) {
  92. // If remove then ask verification first
  93. $screen = 4;
  94. }
  95. if ( $action == 'x' ) {
  96. // Request to remove player
  97. $maintainer = delete_player( $_REQUEST['player'] );
  98. if ( $maintainer[0] == 0 ) {
  99. $screen = 0;
  100. } else {
  101. $screen = 2;
  102. }
  103. }
  104. assert( '$screen >= 0', "By now \$screen should be set to a value >= 0." );
  105. ?>
  106. <!doctype html>
  107. <html lang="en">
  108. <head>
  109. <!-- Required meta tags -->
  110. <meta charset="utf-8">
  111. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  112. <!-- Bootstrap CSS -->
  113. <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  114. <!-- Latest compiled and minified CSS -->
  115. <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">
  116. <title>Slork</title>
  117. </head>
  118. <body>
  119. <div class="container">
  120. <div class="row">
  121. <div class="col-md">
  122. &nbsp;
  123. </div>
  124. <div class="col-md">
  125. <?php
  126. require "snippets.php";
  127. if ( $maintainer[0] > 0 && $maintainer[0] < 100 ) {
  128. // Error in creating maintainer
  129. print( "<pre>" );
  130. print( "ERROR $maintainer[0]: $maintainer[1]" );
  131. print( "</pre>" );
  132. // Try again
  133. $screen = 0;
  134. }
  135. // Choose or add a maintainer; default screen
  136. print( $snippets[$screen] );
  137. ?>
  138. </div>
  139. <div class="col-md">
  140. <pre>
  141. <?php
  142. print_r( $_REQUEST );
  143. ?>
  144. </pre>
  145. </div>
  146. </div>
  147. </div>
  148. <!-- Optional JavaScript -->
  149. <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  150. <script src="//code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  151. <script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  152. <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  153. <!-- Latest compiled and minified JavaScript -->
  154. <script src="//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>
  155. </body>
  156. </html>