123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- $apiUrl = 'http://localhost:8001/blog.php';
- $debug = false;
-
- function apiCall($method, $url, $data = false) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
- curl_setopt($ch, CURLOPT_URL, $url);
- if ($data) {
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $headers = array();
- $headers[] = 'Content-Type: application/json';
- $headers[] = 'Content-Length: ' . strlen($data);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- curl_close($ch);
- return json_decode($response,true);
- }
-
- function menu($tags,$subject) {
- $html= '<ul class="nav nav-pills">';
- foreach ($tags as $tag) {
- $active = $tag['name']==$subject?' class="active"':'';
- $html.= '<li'.$active.'><a href="?action=list&subject='.$tag['name'].'">'.$tag['name'].'</a></li>';
- }
- $html.= '</ul>';
- return $html;
- }
-
- function home($definition) {
- $html = 'Nothing';
- return $html;
- }
-
- function head() {
- $html = '<!DOCTYPE html><html lang="en">';
- $html.= '<head><title>PHP-CRUD-API editor</title>';
- $html.= '<meta name="viewport" content="width=device-width, initial-scale=1">';
- $html.= '<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">';
- $html.= '<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet">';
- $html.= '<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>';
- $html.= '</head><body>';
- return $html;
- }
-
- function referenceText($subject,$data,$field,$id,$definition) {
- $properties = properties($subject,$definition);
- $references = references($subject,$properties);
- $referenced = referenced($subject,$properties);
- $primaryKey = primaryKey($subject,$properties);
-
- $indices = array_flip($data[$subject]['columns']);
- $records = $data[$subject]['records'];
- foreach ($records as $record) {
- if ($record[$indices[$field]]==$id) {
- $text = '';
- $first = true;
- foreach ($record as $i=>$value) {
- if (!$references[$i] && $i!=$primaryKey) {
- if (!$first) $text.= ' - ';
- $text.= $value;
- $first = false;
- }
- }
- return $text;
- }
- }
- return '?';
- }
-
- function listRecords($apiUrl,$subject,$field,$id,$definition) {
- $properties = properties($subject,$definition);
- $references = references($subject,$properties);
- $referenced = referenced($subject,$properties);
- $primaryKey = primaryKey($subject,$properties);
-
- $args = array();
- if ($field) {
- $args['filter']=$field.',eq,'.$id;
- }
- $include = implode(',',array_filter(array_map(function($v){ return $v[0]; },$references)));
- if ($include) {
- $args['include']=$include;
- }
- $data = apiCall('GET',$apiUrl.'/'.$subject.'?'.http_build_query($args));
-
- $html = '';
- if ($field) {
- $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
- $href = '?action=list&subject='.$subject;
- $html .= ' <a href="'.$href.'">remove</a></p>';
- }
- $html.= '<table class="table">';
- $html.= '<tr>';
- foreach ($data[$subject]['columns'] as $i=>$column) {
- $html.= '<th>'.$column.'</th>';
- }
- $html.= '<th>has many</th>';
- $html.= '<th>actions</th>';
- $html.= '</tr>';
- foreach ($data[$subject]['records'] as $record) {
- $html.= '<tr>';
- foreach ($record as $i=>$field) {
- if ($references[$i]) {
- $html.= '<td>';
- $href = '?action=list&subject='.$references[$i][0].'&field='.$references[$i][1].'&id='.$id;
- $html.= '<a href="'.$href.'">';
- $html.= referenceText($references[$i][0],$data,$references[$i][1],$field,$definition);
- $html.= '</a>';
- $html.= '</td>';
- } else {
- $html.= '<td>'.$field.'</td>';
- }
- }
- $html.= '<td>';
- foreach ($referenced as $i=>$relations) {
- $id = $record[$i];
- if ($relations) foreach ($relations as $j=>$relation) {
- if ($j) $html.= ', ';
- $href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
- $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
- }
- }
- $html.= '</td>';
- $html.= '<td>';
- $html.= '<a href="?action=edit&subject='.$subject.'&id='.$record[$primaryKey].'">edit</a>';
- $html.= '</td>';
- $html.= '</tr>';
- }
- $html.= '</table>';
- return $html;
- }
-
- function selectSubject($apiUrl,$subject,$name,$value,$definition) {
- $properties = properties($subject,$definition);
- $references = references($subject,$properties);
- $primaryKey = primaryKey($subject,$properties);
-
- $data = apiCall('GET',$apiUrl.'/'.$subject);
- $html = '<select class="form-control">';
- foreach ($data[$subject]['records'] as $record) {
- $text = '';
- $first = true;
- foreach ($record as $i=>$field) {
- if (!$references[$i] && $i!=$primaryKey) {
- if (!$first) $text.= ' - ';
- $text.= $field;
- $first = false;
- }
- }
- $html.= '<option value="'.$record[$primaryKey].'">'.$text.'</option>';
- }
- $html.= '</select>';
- return $html;
- }
-
- function editRecord($apiUrl,$subject,$id,$definition) {
- $properties = properties($subject,$definition);
- $references = references($subject,$properties);
- $referenced = referenced($subject,$properties);
- $primaryKey = primaryKey($subject,$properties);
-
- $data = apiCall('GET',$apiUrl.'/'.$subject.'/'.$id);
- $html = '<form>';
- $i=0;
- foreach ($data as $column=>$field) {
- $html.= '<div class="form-group">';
- $html.= '<label for="'.$column.'">'.$column.'</label>';
- if ($references[$i]) {
- $html.= selectSubject($apiUrl,$references[$i][0],$column,$field,$definition);
- } else {
- $readonly = $i==$primaryKey?' readonly':'';
- $html.= '<input class="form-control" id="'.$column.'" value="'.$field.'"'.$readonly.'/>';
- }
- $html.= '</div>';
- $i++;
- }
- $html.= '</form>';
- return $html;
- }
-
- function properties($subject,$definition) {
- if (!$subject || !$definition) return false;
- $path = '/'.$subject;
- if (!isset($definition['paths'][$path])) {
- $path = '/'.$subject.'/{id}';
- }
- $properties = false;
- if (isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
- $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
- } elseif (isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
- $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'];
- }
- return $properties;
- }
-
- function references($subject,$properties) {
- if (!$subject || !$properties) return false;
- $references = array();
- foreach ($properties as $field=>$property) {
- $references[] = isset($property['x-references'])?$property['x-references']:false;
- }
- return $references;
- }
-
- function referenced($subject,$properties) {
- if (!$subject || !$properties) return false;
- $referenced = array();
- foreach ($properties as $field=>$property) {
- $referenced[] = isset($property['x-referenced'])?$property['x-referenced']:false;
- }
- return $referenced;
- }
-
- function primaryKey($subject,$properties) {
- if (!$subject || !$properties) return false;
- $i = 0;
- foreach ($properties as $field=>$property) {
- if (isset($property['x-primary-key'])) return $i;
- $i++;
- }
- return false;
- }
-
- $action = isset($_GET['action'])?$_GET['action']:'';
- $subject = isset($_GET['subject'])?$_GET['subject']:'';
- $field = isset($_GET['field'])?$_GET['field']:'';
- $id = isset($_GET['id'])?$_GET['id']:'';
- $definition = apiCall('GET',$apiUrl);
- $debug = $debug?json_encode($definition,JSON_PRETTY_PRINT):false;
-
- echo head();
- echo '<div class="container-fluid">';
- echo '<div class="row">';
- echo menu($definition['tags'],$subject);
- echo '</div>';
- echo '<div class="row">';
- switch ($action){
- case '': echo home(); break;
- case 'list': echo listRecords($apiUrl,$subject,$field,$id,$definition); break;
- case 'edit': echo editRecord($apiUrl,$subject,$id,$definition); break;
- }
- echo '</div>';
- if ($debug) echo '<hr/><pre>'.$debug.'</pre></body></html>';
- echo '</div>';
|