api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

editor.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. $apiUrl = 'http://localhost:8001/blog.php';
  3. $debug = false;
  4. function apiCall($method, $url, $data = false) {
  5. $ch = curl_init();
  6. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  7. curl_setopt($ch, CURLOPT_URL, $url);
  8. if ($data) {
  9. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  10. $headers = array();
  11. $headers[] = 'Content-Type: application/json';
  12. $headers[] = 'Content-Length: ' . strlen($data);
  13. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  14. }
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  16. $response = curl_exec($ch);
  17. curl_close($ch);
  18. return json_decode($response,true);
  19. }
  20. function menu($tags,$subject) {
  21. $html= '<ul class="nav nav-pills">';
  22. foreach ($tags as $tag) {
  23. $active = $tag['name']==$subject?' class="active"':'';
  24. $html.= '<li'.$active.'><a href="?action=list&subject='.$tag['name'].'">'.$tag['name'].'</a></li>';
  25. }
  26. $html.= '</ul>';
  27. return $html;
  28. }
  29. function home($definition) {
  30. $html = 'Nothing';
  31. return $html;
  32. }
  33. function head() {
  34. $html = '<!DOCTYPE html><html lang="en">';
  35. $html.= '<head><title>PHP-CRUD-API editor</title>';
  36. $html.= '<meta name="viewport" content="width=device-width, initial-scale=1">';
  37. $html.= '<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">';
  38. $html.= '<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet">';
  39. $html.= '<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>';
  40. $html.= '</head><body>';
  41. return $html;
  42. }
  43. function displayColumn($columns) {
  44. $names = array('name','title','description');
  45. foreach ($names as $name) {
  46. if (isset($columns[$name])) return $columns[$name];
  47. }
  48. return false;
  49. }
  50. function referenceText($subject,$data,$field,$id,$definition) {
  51. $properties = properties($subject,$definition);
  52. $references = references($subject,$properties);
  53. $referenced = referenced($subject,$properties);
  54. $primaryKey = primaryKey($subject,$properties);
  55. $indices = array_flip($data[$subject]['columns']);
  56. $displayColumn = displayColumn($indices);
  57. $records = $data[$subject]['records'];
  58. foreach ($records as $record) {
  59. if ($record[$indices[$field]]==$id) {
  60. if ($displayColumn===false) {
  61. $text = '';
  62. $first = true;
  63. foreach ($record as $i=>$value) {
  64. if (!$references[$i] && $i!=$primaryKey) {
  65. if (!$first) $text.= ' - ';
  66. $text.= $value;
  67. $first = false;
  68. }
  69. }
  70. return $text;
  71. } else {
  72. return $record[$indices[$displayColumn]];
  73. }
  74. }
  75. }
  76. return '?';
  77. }
  78. function listRecords($apiUrl,$subject,$field,$id,$definition) {
  79. $properties = properties($subject,$definition);
  80. $references = references($subject,$properties);
  81. $referenced = referenced($subject,$properties);
  82. $primaryKey = primaryKey($subject,$properties);
  83. $args = array();
  84. if ($field) {
  85. $args['filter']=$field.',eq,'.$id;
  86. }
  87. $include = implode(',',array_filter(array_map(function($v){ return $v[0]; },$references)));
  88. if ($include) {
  89. $args['include']=$include;
  90. }
  91. $data = apiCall('GET',$apiUrl.'/'.$subject.'?'.http_build_query($args));
  92. $html = '';
  93. if ($field) {
  94. $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
  95. $href = '?action=list&subject='.$subject;
  96. $html .= ' <a href="'.$href.'">remove</a></p>';
  97. }
  98. $html.= '<table class="table">';
  99. $html.= '<tr>';
  100. foreach ($data[$subject]['columns'] as $i=>$column) {
  101. $html.= '<th>'.$column.'</th>';
  102. }
  103. $html.= '<th>has many</th>';
  104. $html.= '<th>actions</th>';
  105. $html.= '</tr>';
  106. foreach ($data[$subject]['records'] as $record) {
  107. $html.= '<tr>';
  108. foreach ($record as $i=>$value) {
  109. if ($references[$i]) {
  110. $html.= '<td>';
  111. $href = '?action=list&subject='.$references[$i][0].'&field='.$references[$i][1].'&id='.$value;
  112. $html.= '<a href="'.$href.'">';
  113. $html.= referenceText($references[$i][0],$data,$references[$i][1],$value,$definition);
  114. $html.= '</a>';
  115. $html.= '</td>';
  116. } else {
  117. $html.= '<td>'.$value.'</td>';
  118. }
  119. }
  120. $html.= '<td>';
  121. foreach ($referenced as $i=>$relations) {
  122. $id = $record[$i];
  123. if ($relations) foreach ($relations as $j=>$relation) {
  124. if ($j) $html.= ', ';
  125. $href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
  126. $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
  127. }
  128. }
  129. $html.= '</td>';
  130. $html.= '<td>';
  131. $html.= '<a href="?action=edit&subject='.$subject.'&id='.$record[$primaryKey].'">edit</a>';
  132. $html.= '</td>';
  133. $html.= '</tr>';
  134. }
  135. $html.= '</table>';
  136. return $html;
  137. }
  138. function selectSubject($apiUrl,$subject,$name,$value,$definition) {
  139. $properties = properties($subject,$definition);
  140. $references = references($subject,$properties);
  141. $primaryKey = primaryKey($subject,$properties);
  142. $data = apiCall('GET',$apiUrl.'/'.$subject);
  143. $indices = array_flip($data[$subject]['columns']);
  144. $displayColumn = displayColumn($indices);
  145. $html = '<select class="form-control">';
  146. foreach ($data[$subject]['records'] as $record) {
  147. if ($displayColumn===false) {
  148. $text = '';
  149. $first = true;
  150. foreach ($record as $i=>$field) {
  151. if (!$references[$i] && $i!=$primaryKey) {
  152. if (!$first) $text.= ' - ';
  153. $text.= $field;
  154. $first = false;
  155. }
  156. }
  157. $html.= '<option value="'.$record[$primaryKey].'">'.$text.'</option>';
  158. } else {
  159. $html.= '<option value="'.$record[$primaryKey].'">'.$record[$displayColumn].'</option>';
  160. }
  161. }
  162. $html.= '</select>';
  163. return $html;
  164. }
  165. function editRecord($apiUrl,$subject,$id,$definition) {
  166. $properties = properties($subject,$definition);
  167. $references = references($subject,$properties);
  168. $referenced = referenced($subject,$properties);
  169. $primaryKey = primaryKey($subject,$properties);
  170. $data = apiCall('GET',$apiUrl.'/'.$subject.'/'.$id);
  171. $html = '<form>';
  172. $i=0;
  173. foreach ($data as $column=>$field) {
  174. $html.= '<div class="form-group">';
  175. $html.= '<label for="'.$column.'">'.$column.'</label>';
  176. if ($references[$i]) {
  177. $html.= selectSubject($apiUrl,$references[$i][0],$column,$field,$definition);
  178. } else {
  179. $readonly = $i==$primaryKey?' readonly':'';
  180. $html.= '<input class="form-control" id="'.$column.'" value="'.$field.'"'.$readonly.'/>';
  181. }
  182. $html.= '</div>';
  183. $i++;
  184. }
  185. $html.= '</form>';
  186. return $html;
  187. }
  188. function properties($subject,$definition) {
  189. if (!$subject || !$definition) return false;
  190. $path = '/'.$subject;
  191. if (!isset($definition['paths'][$path])) {
  192. $path = '/'.$subject.'/{id}';
  193. }
  194. $properties = false;
  195. if (isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
  196. $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
  197. } elseif (isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
  198. $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'];
  199. }
  200. return $properties;
  201. }
  202. function references($subject,$properties) {
  203. if (!$subject || !$properties) return false;
  204. $references = array();
  205. foreach ($properties as $field=>$property) {
  206. $references[] = isset($property['x-references'])?$property['x-references']:false;
  207. }
  208. return $references;
  209. }
  210. function referenced($subject,$properties) {
  211. if (!$subject || !$properties) return false;
  212. $referenced = array();
  213. foreach ($properties as $field=>$property) {
  214. $referenced[] = isset($property['x-referenced'])?$property['x-referenced']:false;
  215. }
  216. return $referenced;
  217. }
  218. function primaryKey($subject,$properties) {
  219. if (!$subject || !$properties) return false;
  220. $i = 0;
  221. foreach ($properties as $field=>$property) {
  222. if (isset($property['x-primary-key'])) return $i;
  223. $i++;
  224. }
  225. return false;
  226. }
  227. $action = isset($_GET['action'])?$_GET['action']:'';
  228. $subject = isset($_GET['subject'])?$_GET['subject']:'';
  229. $field = isset($_GET['field'])?$_GET['field']:'';
  230. $id = isset($_GET['id'])?$_GET['id']:'';
  231. $definition = apiCall('GET',$apiUrl);
  232. $debug = $debug?json_encode($definition,JSON_PRETTY_PRINT):false;
  233. echo head();
  234. echo '<div class="container-fluid">';
  235. echo '<div class="row">';
  236. echo menu($definition['tags'],$subject);
  237. echo '</div>';
  238. echo '<div class="row">';
  239. switch ($action){
  240. case '': echo home(); break;
  241. case 'list': echo listRecords($apiUrl,$subject,$field,$id,$definition); break;
  242. case 'edit': echo editRecord($apiUrl,$subject,$id,$definition); break;
  243. }
  244. echo '</div>';
  245. if ($debug) echo '<hr/><pre>'.$debug.'</pre></body></html>';
  246. echo '</div>';