PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/templates/tests/chorcreator.php

https://github.com/mkweb/NabServ
PHP | 298 lines | 231 code | 67 blank | 0 comment | 35 complexity | d70384e38a2cbe985be956914624e26d MD5 | raw file
  1. <?php
  2. $slots = 20;
  3. $serial = "001d92164ae2";
  4. $token = "45EA15";
  5. $runtest = false;
  6. $save = false;
  7. $saved = array();
  8. $path = PATH_FILES . DS . 'chor' . DS . 'choreographies' . DS . 'user';
  9. $files = glob($path . DS . '*');
  10. foreach($files as $file) {
  11. $saved[] = basename($file);
  12. }
  13. if(isset($_POST['save'])) {
  14. unset($_POST['save']);
  15. $data = serialize($_POST);
  16. $filename = $path . DS . $_POST['name'] . '.dat';
  17. if(file_exists($filename)) unlink($filename);
  18. $fh = fopen($filename, 'w');
  19. fputs($fh, $data);
  20. fclose($fh);
  21. header('Location: /tests/?page=chorcreator&load=' . $_POST['name'] . '.dat');
  22. exit;
  23. }
  24. if(isset($_GET['load']) && !isset($_POST['test'])) {
  25. $file = $path . DS . $_GET['load'];
  26. if(file_exists($file)) {
  27. $_POST = unserialize(file_get_contents($file));
  28. $_SERVER['REQUEST_METHOD'] = 'POST';
  29. }
  30. }
  31. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  32. if(isset($_POST['save'])) {
  33. $save = true;
  34. }
  35. if(isset($_POST['test'])) {
  36. $runtest = true;
  37. unset($_POST['test']);
  38. }
  39. if(isset($_POST['name'])) {
  40. $name = $_POST['name'];
  41. unset($_POST['name']);
  42. }
  43. $directions = array();
  44. $search = 'direction_ear_';
  45. foreach($_POST as $key => $value) {
  46. if(substr($key, 0, strlen($search)) == $search) {
  47. $directions[substr($key, strlen($search))] = $value;
  48. unset($_POST[$key]);
  49. }
  50. }
  51. $tempo = $_POST['tempo'];
  52. unset($_POST['tempo']);
  53. $data = array();
  54. foreach($_POST as $key => $value) {
  55. list($type, $detail, $num) = explode('_', $key);
  56. $data[$num][$type . '_' . $detail] = $value;
  57. }
  58. foreach($data as $key => $block) {
  59. $datafound = false;
  60. foreach($block as $value) {
  61. if($value != '') {
  62. $datafound = true;
  63. }
  64. }
  65. if(!$datafound) {
  66. unset($data[$key]);
  67. }
  68. }
  69. }
  70. if($runtest) {
  71. $apicall = array($tempo);
  72. foreach($data as $time => $d) {
  73. foreach($d as $key => $value) {
  74. list($type, $detail) = explode('_', $key);
  75. switch($type) {
  76. case 'ear':
  77. $dir = $directions[$detail . '_' . $time];
  78. $apicall[] = sprintf('%d,%s,%d,%d,0,%d', $time, 'motor', $detail, $value, $dir);
  79. break;
  80. case 'led':
  81. list($r, $g, $b) = explode(',', $value);
  82. $apicall[] = sprintf('%d,%s,%d,%d,%d,%d', $time, 'led', $detail, $r, $g, $b);
  83. break;
  84. }
  85. }
  86. }
  87. $apicall = join(',', $apicall);
  88. $url = BASE_URL . "/vl/api.php?sn=" . $serial . "&token=" . $token . "&chor=" . $apicall;
  89. file_get_contents($url);
  90. }
  91. ?>
  92. <html>
  93. <head>
  94. <link rel="stylesheet" media="screen" type="text/css" href="/res/tests/css/colorpicker.css" />
  95. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
  96. <script type="text/javascript" src="/res/tests/js/colorpicker.js"></script>
  97. <script type="text/javascript">
  98. $(document).ready(function() {
  99. $('.colorbutton').each(function() {
  100. var col_id = '#' + this.id;
  101. var input_id = col_id.substr(0, col_id.length - 3);
  102. if($(input_id).val() != '') {
  103. $(col_id).css('background-color', 'rgb(' + $(input_id).val() + ')');
  104. }
  105. }).ColorPicker({
  106. onShow: function (colpkr) {
  107. $(colpkr).fadeIn(500);
  108. return false;
  109. },
  110. onHide: function (colpkr) {
  111. $(colpkr).fadeOut(500);
  112. return false;
  113. },
  114. onChange: function(hsb, hex, rgb) {
  115. var currentColorId = '#' + $('#currentColorId').val();
  116. var input = currentColorId.substr(0, currentColorId.length - 3);
  117. $(currentColorId).css('backgroundColor', '#' + hex);
  118. $(currentColorId).attr('rel', '#' + hex);
  119. $(input).val(rgb.r + ',' + rgb.g + ',' + rgb.b);
  120. },
  121. onBeforeShow: function () {
  122. $('#currentColorId').val(this.id);
  123. }
  124. });
  125. });
  126. </script>
  127. <style type="text/css">
  128. select {
  129. border: 1px solid #000;
  130. width: 40px;
  131. background-color: #FFF;
  132. }
  133. table {
  134. border-collapse: collapse;
  135. }
  136. .colorbutton {
  137. background-color: #FFF;
  138. }
  139. table {
  140. border: 2px solid #000;
  141. padding: 4px;
  142. }
  143. table td.firstline, table td.firstrow {
  144. font-family: Verdana;
  145. font-size: 10pt;
  146. font-weight: bold;
  147. background-color: #666;
  148. color: #FFF;
  149. }
  150. table td.firstrow {
  151. text-align: center;
  152. }
  153. </style>
  154. </head>
  155. <body>
  156. <? if(count($saved) > 0): ?>
  157. <? foreach($saved as $file): ?>
  158. <a href="/tests/?page=chorcreator&load=<?= $file ?>"><?= $file ?></a><br />
  159. <? endforeach ?>
  160. <hr />
  161. <? endif ?>
  162. <input type="hidden" id="currentColorId" />
  163. <?php
  164. $fields = array(
  165. 'ear_1' => 'dropdown',
  166. 'ear_0' => 'dropdown',
  167. 'led_4' => 'color',
  168. 'led_3' => 'color',
  169. 'led_2' => 'color',
  170. 'led_1' => 'color',
  171. 'led_0' => 'color'
  172. );
  173. $names = array(
  174. 'ear_1' => 'Ear L',
  175. 'ear_0' => 'Ear R',
  176. 'led_4' => 'Nose',
  177. 'led_3' => 'Color L',
  178. 'led_2' => 'Color C',
  179. 'led_1' => 'Color R',
  180. 'led_0' => 'Bottom'
  181. );
  182. $earvalues = array();
  183. for($i = 0; $i <= 180; $i += 10) {
  184. $earvalues[] = $i;
  185. }
  186. ?>
  187. <form action="" method="post">
  188. Name: <input type="text" name="name" value="<?= (isset($name) ? $name : 'chor_' . substr(md5(microtime()), 0, 7)) ?>" /><br />
  189. Tempo: <input type="text" name="tempo" value="<?= (isset($tempo) ? $tempo : '10') ?>" />
  190. <table border="0">
  191. <tr>
  192. <td class="firstrow">&nbsp;</td>
  193. <? for($i = 1; $i <= $slots; $i++): ?>
  194. <td class="firstrow"><?= $i ?></td>
  195. <? endfor ?>
  196. </tr>
  197. <? foreach($fields as $name => $type): ?>
  198. <tr>
  199. <? for($i = -1; $i < $slots; $i++): ?>
  200. <? if($i == -1): ?>
  201. <td width="100" class="firstline"><?= $names[$name] ?></td>
  202. <? else: ?>
  203. <td align="center" style="background-color: #<?= ($i % 2 == 0 ? '09A87B': 'EFEFEF') ?>; width: 50px;">
  204. <? if($type == 'dropdown'): ?>
  205. <select name="<?= $name ?>_<?= $i ?>" id="<?= $name ?>_<?= $i ?>_id">
  206. <option value="">--</option>
  207. <? for($j = 0; $j <= 180; $j += 10): ?>
  208. <option<?= (isset($data[$i][$name]) && $data[$i][$name] != '' && $data[$i][$name] == $j ? ' selected="selected"' : '') ?>><?= $j ?></option>
  209. <? endfor ?>
  210. </select>
  211. <br />
  212. <? $id = substr($name, -1) . '_' . $i; ?>
  213. <select name="direction_<?= $name ?>_<?= $i ?>">
  214. <option value="0"<?= (isset($directions[$id]) && $directions[$id] == '0' ? ' selected="selected"' : '') ?>>F</option>
  215. <option value="1"<?= (isset($directions[$id]) && $directions[$id] == '1' ? ' selected="selected"' : '') ?>>B</option>
  216. </select>
  217. <? else: ?>
  218. <div class="colorbutton" id="<?= $name ?>_<?= $i ?>_id" style="border: 1px solid #000; width: 40px; height: 20px;"></div>
  219. <input type="hidden" name="<?= $name ?>_<?= $i ?>" id="<?= $name ?>_<?= $i ?>" value="<?= (isset($data[$i][$name]) ? $data[$i][$name] : '') ?>" />
  220. <? endif ?>
  221. </td>
  222. <? endif ?>
  223. <? endfor ?>
  224. </tr>
  225. <? endforeach?>
  226. </table>
  227. <input type="submit" name="test" value="Test" />
  228. <input type="submit" name="save" value="Save" />
  229. </form>
  230. </body>
  231. </html>