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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

editor.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 referenceText($subject,$data,$field,$id,$definition) {
  44. $properties = properties($subject,$definition);
  45. $references = references($subject,$properties);
  46. $referenced = referenced($subject,$properties);
  47. $primaryKey = primaryKey($subject,$properties);
  48. $indices = array_flip($data[$subject]['columns']);
  49. $records = $data[$subject]['records'];
  50. foreach ($records as $record) {
  51. if ($record[$indices[$field]]==$id) {
  52. $text = '';
  53. $first = true;
  54. foreach ($record as $i=>$value) {
  55. if (!$references[$i] && $i!=$primaryKey) {
  56. if (!$first) $text.= ' - ';
  57. $text.= $value;
  58. $first = false;
  59. }
  60. }
  61. return $text;
  62. }
  63. }
  64. return '?';
  65. }
  66. function listRecords($apiUrl,$subject,$field,$id,$definition) {
  67. $properties = properties($subject,$definition);
  68. $references = references($subject,$properties);
  69. $referenced = referenced($subject,$properties);
  70. $primaryKey = primaryKey($subject,$properties);
  71. $args = array();
  72. if ($field) {
  73. $args['filter']=$field.',eq,'.$id;
  74. }
  75. $include = implode(',',array_filter(array_map(function($v){ return $v[0]; },$references)));
  76. if ($include) {
  77. $args['include']=$include;
  78. }
  79. $data = apiCall('GET',$apiUrl.'/'.$subject.'?'.http_build_query($args));
  80. $html = '';
  81. if ($field) {
  82. $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
  83. $href = '?action=list&subject='.$subject;
  84. $html .= ' <a href="'.$href.'">remove</a></p>';
  85. }
  86. $html.= '<table class="table">';
  87. $html.= '<tr>';
  88. foreach ($data[$subject]['columns'] as $i=>$column) {
  89. $html.= '<th>'.$column.'</th>';
  90. }
  91. $html.= '<th>has many</th>';
  92. $html.= '<th>actions</th>';
  93. $html.= '</tr>';
  94. foreach ($data[$subject]['records'] as $record) {
  95. $html.= '<tr>';
  96. foreach ($record as $i=>$field) {
  97. if ($references[$i]) {
  98. $html.= '<td>';
  99. $href = '?action=list&subject='.$references[$i][0].'&field='.$references[$i][1].'&id='.$id;
  100. $html.= '<a href="'.$href.'">';
  101. $html.= referenceText($references[$i][0],$data,$references[$i][1],$field,$definition);
  102. $html.= '</a>';
  103. $html.= '</td>';
  104. } else {
  105. $html.= '<td>'.$field.'</td>';
  106. }
  107. }
  108. $html.= '<td>';
  109. foreach ($referenced as $i=>$relations) {
  110. $id = $record[$i];
  111. if ($relations) foreach ($relations as $j=>$relation) {
  112. if ($j) $html.= ', ';
  113. $href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
  114. $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
  115. }
  116. }
  117. $html.= '</td>';
  118. $html.= '<td>';
  119. $html.= '<a href="?action=edit&subject='.$subject.'&id='.$record[$primaryKey].'">edit</a>';
  120. $html.= '</td>';
  121. $html.= '</tr>';
  122. }
  123. $html.= '</table>';
  124. return $html;
  125. }
  126. function selectSubject($apiUrl,$subject,$name,$value,$definition) {
  127. $properties = properties($subject,$definition);
  128. $references = references($subject,$properties);
  129. $primaryKey = primaryKey($subject,$properties);
  130. $data = apiCall('GET',$apiUrl.'/'.$subject);
  131. $html = '<select class="form-control">';
  132. foreach ($data[$subject]['records'] as $record) {
  133. $text = '';
  134. $first = true;
  135. foreach ($record as $i=>$field) {
  136. if (!$references[$i] && $i!=$primaryKey) {
  137. if (!$first) $text.= ' - ';
  138. $text.= $field;
  139. $first = false;
  140. }
  141. }
  142. $html.= '<option value="'.$record[$primaryKey].'">'.$text.'</option>';
  143. }
  144. $html.= '</select>';
  145. return $html;
  146. }
  147. function editRecord($apiUrl,$subject,$id,$definition) {
  148. $properties = properties($subject,$definition);
  149. $references = references($subject,$properties);
  150. $referenced = referenced($subject,$properties);
  151. $primaryKey = primaryKey($subject,$properties);
  152. $data = apiCall('GET',$apiUrl.'/'.$subject.'/'.$id);
  153. $html = '<form>';
  154. $i=0;
  155. foreach ($data as $column=>$field) {
  156. $html.= '<div class="form-group">';
  157. $html.= '<label for="'.$column.'">'.$column.'</label>';
  158. if ($references[$i]) {
  159. $html.= selectSubject($apiUrl,$references[$i][0],$column,$field,$definition);
  160. } else {
  161. $readonly = $i==$primaryKey?' readonly':'';
  162. $html.= '<input class="form-control" id="'.$column.'" value="'.$field.'"'.$readonly.'/>';
  163. }
  164. $html.= '</div>';
  165. $i++;
  166. }
  167. $html.= '</form>';
  168. return $html;
  169. }
  170. function properties($subject,$definition) {
  171. if (!$subject || !$definition) return false;
  172. $path = '/'.$subject;
  173. if (!isset($definition['paths'][$path])) {
  174. $path = '/'.$subject.'/{id}';
  175. }
  176. $properties = false;
  177. if (isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
  178. $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
  179. } elseif (isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
  180. $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'];
  181. }
  182. return $properties;
  183. }
  184. function references($subject,$properties) {
  185. if (!$subject || !$properties) return false;
  186. $references = array();
  187. foreach ($properties as $field=>$property) {
  188. $references[] = isset($property['x-references'])?$property['x-references']:false;
  189. }
  190. return $references;
  191. }
  192. function referenced($subject,$properties) {
  193. if (!$subject || !$properties) return false;
  194. $referenced = array();
  195. foreach ($properties as $field=>$property) {
  196. $referenced[] = isset($property['x-referenced'])?$property['x-referenced']:false;
  197. }
  198. return $referenced;
  199. }
  200. function primaryKey($subject,$properties) {
  201. if (!$subject || !$properties) return false;
  202. $i = 0;
  203. foreach ($properties as $field=>$property) {
  204. if (isset($property['x-primary-key'])) return $i;
  205. $i++;
  206. }
  207. return false;
  208. }
  209. $action = isset($_GET['action'])?$_GET['action']:'';
  210. $subject = isset($_GET['subject'])?$_GET['subject']:'';
  211. $field = isset($_GET['field'])?$_GET['field']:'';
  212. $id = isset($_GET['id'])?$_GET['id']:'';
  213. $definition = apiCall('GET',$apiUrl);
  214. $debug = $debug?json_encode($definition,JSON_PRETTY_PRINT):false;
  215. echo head();
  216. echo '<div class="container-fluid">';
  217. echo '<div class="row">';
  218. echo menu($definition['tags'],$subject);
  219. echo '</div>';
  220. echo '<div class="row">';
  221. switch ($action){
  222. case '': echo home(); break;
  223. case 'list': echo listRecords($apiUrl,$subject,$field,$id,$definition); break;
  224. case 'edit': echo editRecord($apiUrl,$subject,$id,$definition); break;
  225. }
  226. echo '</div>';
  227. if ($debug) echo '<hr/><pre>'.$debug.'</pre></body></html>';
  228. echo '</div>';