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.

ui.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. class PHP_CRUD_UI {
  3. protected $settings;
  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 url($base,$subject,$action,$id='',$field='') {
  21. return $base.trim("$subject/$action/$id/$field",'/');
  22. }
  23. function menu($parameters) {
  24. extract($parameters);
  25. $html= '<ul class="nav nav-pills">';
  26. foreach ($definition['tags'] as $tag) {
  27. $active = $tag['name']==$subject?' class="active"':'';
  28. $html.= '<li'.$active.'><a href="'.$this->url($base,$tag['name'],'list').'">'.$tag['name'].'</a></li>';
  29. }
  30. $html.= '</ul>';
  31. return $html;
  32. }
  33. function home($parameters) {
  34. extract($parameters);
  35. $html = 'Nothing';
  36. return $html;
  37. }
  38. function head() {
  39. $html = '<!DOCTYPE html><html lang="en">';
  40. $html.= '<head><title>PHP-CRUD-UI</title>';
  41. $html.= '<meta name="viewport" content="width=device-width, initial-scale=1">';
  42. $html.= '<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">';
  43. $html.= '<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet">';
  44. $html.= '<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>';
  45. $html.= '</head><body>';
  46. return $html;
  47. }
  48. function displayColumn($columns) {
  49. // TODO: make configurable
  50. $names = array('name','title','description','username');
  51. foreach ($names as $name) {
  52. if (isset($columns[$name])) return $columns[$name];
  53. }
  54. return false;
  55. }
  56. function referenceText($subject,$data,$field,$id,$definition) {
  57. $properties = $this->properties($subject,$definition);
  58. $references = $this->references($subject,$properties);
  59. $referenced = $this->referenced($subject,$properties);
  60. $primaryKey = $this->primaryKey($subject,$properties);
  61. $indices = array_flip($data[$subject]['columns']);
  62. $displayColumn = $this->displayColumn($indices);
  63. $records = $data[$subject]['records'];
  64. foreach ($records as $record) {
  65. if ($record[$indices[$field]]==$id) {
  66. if ($displayColumn===false) {
  67. $text = '';
  68. $first = true;
  69. foreach ($record as $i=>$value) {
  70. if (!$references[$i] && $i!=$primaryKey) {
  71. if (!$first) $text.= ' - ';
  72. $text.= $value;
  73. $first = false;
  74. }
  75. }
  76. return $text;
  77. } else {
  78. return $record[$displayColumn];
  79. }
  80. }
  81. }
  82. return false;
  83. }
  84. function listRecords($parameters) {
  85. extract($parameters);
  86. $properties = $this->properties($subject,$definition);
  87. $references = $this->references($subject,$properties);
  88. $referenced = $this->referenced($subject,$properties);
  89. $primaryKey = $this->primaryKey($subject,$properties);
  90. $args = array();
  91. if ($field) {
  92. $args['filter']=$field.',eq,'.$id;
  93. }
  94. $include = implode(',',array_filter(array_map(function($v){ return $v[0]; },$references)));
  95. if ($include) {
  96. $args['include']=$include;
  97. }
  98. $data = $this->apiCall('GET',$url.'/'.$subject.'?'.http_build_query($args));
  99. $html = '';
  100. if ($field) {
  101. $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
  102. $href = $this->url($base,$subject,'list');
  103. $html .= ' <a href="'.$href.'">remove</a></p>';
  104. }
  105. $html.= '<table class="table">';
  106. $html.= '<tr>';
  107. foreach ($data[$subject]['columns'] as $i=>$column) {
  108. $html.= '<th>'.$column.'</th>';
  109. }
  110. $html.= '<th>has many</th>';
  111. $html.= '<th>actions</th>';
  112. $html.= '</tr>';
  113. foreach ($data[$subject]['records'] as $record) {
  114. $html.= '<tr>';
  115. foreach ($record as $i=>$value) {
  116. if ($references[$i]) {
  117. $html.= '<td>';
  118. $href = $this->url($base,$references[$i][0],'list',$value,$references[$i][1]);
  119. $html.= '<a href="'.$href.'">';
  120. $html.= $this->referenceText($references[$i][0],$data,$references[$i][1],$value,$definition);
  121. $html.= '</a>';
  122. $html.= '</td>';
  123. } else {
  124. $html.= '<td>'.$value.'</td>';
  125. }
  126. }
  127. $html.= '<td>';
  128. foreach ($referenced as $i=>$relations) {
  129. $id = $record[$i];
  130. if ($relations) foreach ($relations as $j=>$relation) {
  131. if ($j) $html.= ', ';
  132. $href = $this->url($base,$relation[0],'list',$id,$relation[1]);
  133. $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
  134. }
  135. }
  136. $html.= '</td>';
  137. $html.= '<td>';
  138. $href = $this->url($base,$subject,'edit',$record[$primaryKey]);
  139. $html.= '<a href="'.$href.'">edit</a>';
  140. $html.= '</td>';
  141. $html.= '</tr>';
  142. }
  143. $html.= '</table>';
  144. return $html;
  145. }
  146. function selectSubject($url,$subject,$name,$value,$definition) {
  147. $properties = $this->properties($subject,$definition);
  148. $references = $this->references($subject,$properties);
  149. $primaryKey = $this->primaryKey($subject,$properties);
  150. $data = $this->apiCall('GET',$url.'/'.$subject);
  151. $indices = array_flip($data[$subject]['columns']);
  152. $displayColumn = $this->displayColumn($indices);
  153. $html = '<select class="form-control">';
  154. foreach ($data[$subject]['records'] as $record) {
  155. if ($displayColumn===false) {
  156. $text = '';
  157. $first = true;
  158. foreach ($record as $i=>$field) {
  159. if (!$references[$i] && $i!=$primaryKey) {
  160. if (!$first) $text.= ' - ';
  161. $text.= $field;
  162. $first = false;
  163. }
  164. }
  165. $html.= '<option value="'.$record[$primaryKey].'">'.$text.'</option>';
  166. } else {
  167. $html.= '<option value="'.$record[$primaryKey].'">'.$record[$displayColumn].'</option>';
  168. }
  169. }
  170. $html.= '</select>';
  171. return $html;
  172. }
  173. function editRecord($parameters) {
  174. extract($parameters);
  175. $properties = $this->properties($subject,$definition);
  176. $references = $this->references($subject,$properties);
  177. $referenced = $this->referenced($subject,$properties);
  178. $primaryKey = $this->primaryKey($subject,$properties);
  179. $data = $this->apiCall('GET',$url.'/'.$subject.'/'.$id);
  180. $html = '<form>';
  181. $i=0;
  182. foreach ($data as $column=>$field) {
  183. $html.= '<div class="form-group">';
  184. $html.= '<label for="'.$column.'">'.$column.'</label>';
  185. if ($references[$i]) {
  186. $html.= $this->selectSubject($url,$references[$i][0],$column,$field,$definition);
  187. } else {
  188. $readonly = $i==$primaryKey?' readonly':'';
  189. $html.= '<input class="form-control" id="'.$column.'" value="'.$field.'"'.$readonly.'/>';
  190. }
  191. $html.= '</div>';
  192. $i++;
  193. }
  194. $html.= '</form>';
  195. return $html;
  196. }
  197. function properties($subject,$definition) {
  198. if (!$subject || !$definition) return false;
  199. $path = '/'.$subject;
  200. if (!isset($definition['paths'][$path])) {
  201. $path = '/'.$subject.'/{id}';
  202. }
  203. $properties = false;
  204. if (isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
  205. $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
  206. } elseif (isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
  207. $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'];
  208. }
  209. return $properties;
  210. }
  211. function references($subject,$properties) {
  212. if (!$subject || !$properties) return false;
  213. $references = array();
  214. foreach ($properties as $field=>$property) {
  215. $references[] = isset($property['x-references'])?$property['x-references']:false;
  216. }
  217. return $references;
  218. }
  219. function referenced($subject,$properties) {
  220. if (!$subject || !$properties) return false;
  221. $referenced = array();
  222. foreach ($properties as $field=>$property) {
  223. $referenced[] = isset($property['x-referenced'])?$property['x-referenced']:false;
  224. }
  225. return $referenced;
  226. }
  227. function primaryKey($subject,$properties) {
  228. if (!$subject || !$properties) return false;
  229. $i = 0;
  230. foreach ($properties as $field=>$property) {
  231. if (isset($property['x-primary-key'])) return $i;
  232. $i++;
  233. }
  234. return false;
  235. }
  236. public function __construct($config) {
  237. extract($config);
  238. // initialize
  239. $url = isset($url)?$url:null;
  240. $base = isset($base)?$base:null;
  241. $definition = isset($definition)?$definition:null;
  242. $method = isset($method)?$method:null;
  243. $request = isset($request)?$request:null;
  244. $get = isset($get)?$get:null;
  245. $post = isset($post)?$post:null;
  246. // defaults
  247. if (!$definition) {
  248. $definition = isset($_SESSION['definition'])?$_SESSION['definition']:null;
  249. if (!$definition) {
  250. $definition = $this->apiCall('GET',$url);
  251. $_SESSION['definition'] = $definition;
  252. }
  253. }
  254. if (!$method) {
  255. $method = $_SERVER['REQUEST_METHOD'];
  256. }
  257. if (!$request) {
  258. $request = isset($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:'';
  259. if (!$request) {
  260. $request = isset($_SERVER['ORIG_PATH_INFO'])?$_SERVER['ORIG_PATH_INFO']:'';
  261. }
  262. }
  263. if (!$get) {
  264. $get = $_GET;
  265. }
  266. if (!$post) {
  267. $post = 'php://input';
  268. }
  269. $request = trim($request,'/');
  270. if (!$base) {
  271. $count = $request?(-1*strlen($request)):strlen($_SERVER['REQUEST_URI']);
  272. $base = rtrim(substr($_SERVER['REQUEST_URI'],0,$count),'/').'/';
  273. }
  274. $this->settings = compact('url', 'base', 'definition', 'method', 'request', 'get', 'post');
  275. }
  276. protected function parseRequestParameter(&$request,$characters) {
  277. if (!$request) return false;
  278. $pos = strpos($request,'/');
  279. $value = $pos?substr($request,0,$pos):$request;
  280. $request = $pos?substr($request,$pos+1):'';
  281. if (!$characters) return $value;
  282. return preg_replace("/[^$characters]/",'',$value);
  283. }
  284. protected function getParameters($settings) {
  285. extract($settings);
  286. $subject = $this->parseRequestParameter($request, 'a-zA-Z0-9\-_');
  287. $action = $this->parseRequestParameter($request, 'a-zA-Z0-9\-_');
  288. $id = $this->parseRequestParameter($request, 'a-zA-Z0-9\-_');
  289. $field = $this->parseRequestParameter($request, 'a-zA-Z0-9\-_');
  290. return compact('url','base','subject','action','id','field','definition');
  291. }
  292. function executeCommand() {
  293. $parameters = $this->getParameters($this->settings);
  294. $html = $this->head();
  295. $html.= '<div class="container-fluid">';
  296. $html.= '<div class="row">';
  297. $html.= $this->menu($parameters);
  298. $html.= '</div>';
  299. $html.= '<div class="row">';
  300. switch($parameters['action']){
  301. case '': $html.= $this->home($parameters); break;
  302. case 'list': $html.= $this->listRecords($parameters); break;
  303. case 'edit': $html.= $this->editRecord($parameters); break;
  304. }
  305. $html.= '</div>';
  306. $html.= '</div>';
  307. return $html;
  308. }
  309. }
  310. session_start();
  311. $ui = new PHP_CRUD_UI(array(
  312. 'url' => 'http://localhost:8001/blog.php',
  313. ));
  314. echo $ui->executeCommand();