Added bootstrap
This commit is contained in:
parent
fb79a6e9ac
commit
0829b8de2d
2 changed files with 101 additions and 74 deletions
6
api.php
6
api.php
|
|
@ -1606,7 +1606,7 @@ class PHP_CRUD_API {
|
|||
|
||||
$result = $this->db->query($this->db->getSql('reflect_belongs_to'),array($table_list[0],$table_names,$database,$database));
|
||||
while ($row = $this->db->fetchRow($result)) {
|
||||
$table_fields[$table['name']][$row[1]]->references=$row[2];
|
||||
$table_fields[$table['name']][$row[1]]->references=array($row[2],$row[3]);
|
||||
}
|
||||
$result = $this->db->query($this->db->getSql('reflect_has_many'),array($table_names,$table_list[0],$database,$database));
|
||||
while ($row = $this->db->fetchRow($result)) {
|
||||
|
|
@ -1740,7 +1740,7 @@ class PHP_CRUD_API {
|
|||
echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
|
||||
}
|
||||
if (isset($action['fields'][$field]->references)) {
|
||||
echo ',"x-references": "'.$action['fields'][$field]->references.'"';
|
||||
echo ',"x-references": '.json_encode($action['fields'][$field]->references);
|
||||
}
|
||||
if (isset($action['fields'][$field]->primaryKey)) {
|
||||
echo ',"x-primary-key": true';
|
||||
|
|
@ -1835,7 +1835,7 @@ class PHP_CRUD_API {
|
|||
echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
|
||||
}
|
||||
if (isset($action['fields'][$field]->references)) {
|
||||
echo ',"x-references": "'.$action['fields'][$field]->references.'"';
|
||||
echo ',"x-references": '.json_encode($action['fields'][$field]->references);
|
||||
}
|
||||
if (isset($action['fields'][$field]->primaryKey)) {
|
||||
echo ',"x-primary-key": true';
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
$apiUrl = 'http://localhost:8001/blog.php';
|
||||
$debug = true;
|
||||
$debug = false;
|
||||
|
||||
function apiCall($method, $url, $data = false) {
|
||||
$ch = curl_init();
|
||||
|
|
@ -19,12 +19,13 @@ function apiCall($method, $url, $data = false) {
|
|||
return json_decode($response,true);
|
||||
}
|
||||
|
||||
function menu($tags) {
|
||||
$html = '<ul>';
|
||||
function menu($tags,$subject) {
|
||||
$html= '<ul class="nav nav-pills">';
|
||||
foreach ($tags as $tag) {
|
||||
$html.= '<li><a href="?action=list&subject='.$tag['name'].'">'.$tag['name'].'</a></li>';
|
||||
$active = $tag['name']==$subject?' class="active"':'';
|
||||
$html.= '<li'.$active.'><a href="?action=list&subject='.$tag['name'].'">'.$tag['name'].'</a></li>';
|
||||
}
|
||||
$html.= '</ul></div>';
|
||||
$html.= '</ul>';
|
||||
return $html;
|
||||
}
|
||||
|
||||
|
|
@ -34,17 +35,22 @@ function home($definition) {
|
|||
}
|
||||
|
||||
function head() {
|
||||
$html = '<html><head></head><body>';
|
||||
$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 foot($debug) {
|
||||
if ($debug) $html = '<hr/><pre>'.$debug.'</pre></body></html>';
|
||||
else $html = '';
|
||||
return $html;
|
||||
}
|
||||
function listRecords($apiUrl,$subject,$field,$id,$definition) {
|
||||
$properties = properties($subject,$definition);
|
||||
$references = references($subject,$properties);
|
||||
$referenced = referenced($subject,$properties);
|
||||
$primaryKey = primaryKey($subject,$properties);
|
||||
|
||||
function listRecords($apiUrl,$subject,$field,$id,$references,$referenced,$primaryKey) {
|
||||
$filter = '';
|
||||
$html = '';
|
||||
if ($field) {
|
||||
|
|
@ -54,36 +60,48 @@ function listRecords($apiUrl,$subject,$field,$id,$references,$referenced,$primar
|
|||
$html .= ' <a href="'.$href.'">remove</a></p>';
|
||||
}
|
||||
$data = apiCall('GET',$apiUrl.'/'.$subject.$filter );
|
||||
$html.= '<table>';
|
||||
$html.= '<table class="table">';
|
||||
$html.= '<tr>';
|
||||
foreach ($data[$subject]['columns'] as $i=>$column) {
|
||||
if (!$references[$i]) {
|
||||
$html.= '<th>'.$column.'</th>';
|
||||
}
|
||||
$html.= '<th>related</th>';
|
||||
}
|
||||
$html.= '<th>belongs to</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]) {
|
||||
$href = '?action=view&subject='.$references[$i].'&id='.$field;
|
||||
$html.= '<td><a href="'.$href.'">'.$field.'</a></td>';
|
||||
} else {
|
||||
if (!$references[$i]) {
|
||||
$html.= '<td>'.$field.'</td>';
|
||||
}
|
||||
}
|
||||
$html.= '<td>';
|
||||
$first = true;
|
||||
foreach ($references as $i=>$relation) {
|
||||
$id = $record[$i];
|
||||
if ($relation) {
|
||||
if (!$first) $html.= ', ';
|
||||
$href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
|
||||
$html.= '<a href="'.$href.'">'.$relation[0].'</a>';
|
||||
$first = false;
|
||||
}
|
||||
}
|
||||
$html.= '</td>';
|
||||
$html.= '<td>';
|
||||
foreach ($referenced as $i=>$relations) {
|
||||
$id = $record[$i];
|
||||
if ($relations) foreach ($relations as $j=>$relation) {
|
||||
if ($j) $html.= ' ';
|
||||
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=view&subject='.$subject.'&id='.$record[$primaryKey].'">view</a>';
|
||||
$html.= '<a href="?action=edit&subject='.$subject.'&id='.$record[$primaryKey].'">edit</a>';
|
||||
$html.= '</td>';
|
||||
$html.= '</tr>';
|
||||
}
|
||||
|
|
@ -91,52 +109,63 @@ function listRecords($apiUrl,$subject,$field,$id,$references,$referenced,$primar
|
|||
return $html;
|
||||
}
|
||||
|
||||
function viewRecord($apiUrl,$subject,$id,$references,$referenced,$primaryKey) {
|
||||
$data = apiCall('GET',$apiUrl.'/'.$subject.'/'.$id);
|
||||
$html = '<table>';
|
||||
$i=0;
|
||||
foreach ($data as $column=>$field) {
|
||||
$html.= '<tr><th>'.$column.'</th>';
|
||||
if ($references[$i]) {
|
||||
$href = '?action=view&subject='.$references[$i].'&id='.$field;
|
||||
$html.= '<td><a href="'.$href.'">'.$field.'</a></td>';
|
||||
} else {
|
||||
$html.= '<td>'.$field.'</td>';
|
||||
}
|
||||
$html.= '</tr>';
|
||||
$i++;
|
||||
}
|
||||
$html.= '<tr><th>related</th><td>';
|
||||
$keys = array_keys($data);
|
||||
foreach ($referenced as $i=>$relations) {
|
||||
$id = $data[$keys[$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>';
|
||||
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]) {
|
||||
if (!$first) $text.= ' - ';
|
||||
$text.= $field;
|
||||
$first = false;
|
||||
}
|
||||
}
|
||||
$html.= '</td></tr>';
|
||||
$html.= '<tr><th>actions</th><td>';
|
||||
$html.= '<a href="?action=view&subject='.$subject.'&id='.$data[$keys[$primaryKey]].'">view</a>';
|
||||
$html.= '</td></tr>';
|
||||
$html.= '</table>';
|
||||
$html.= '<option value="'.$record[$primaryKey].'">'.$text.'</option>';
|
||||
}
|
||||
$html.= '</select>';
|
||||
return $html;
|
||||
}
|
||||
|
||||
function properties($subject,$action,$definition) {
|
||||
if (!$subject || !$definition) return false;
|
||||
if ($action=='view') {
|
||||
$path = '/'.$subject.'/{id}';
|
||||
if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
|
||||
return false;
|
||||
}
|
||||
$properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
|
||||
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 {
|
||||
$path = '/'.$subject;
|
||||
if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
|
||||
return false;
|
||||
$html.= '<input class="form-control" id="'.$column.'" value="'.$field.'"/>';
|
||||
}
|
||||
$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;
|
||||
|
|
@ -175,21 +204,19 @@ $subject = isset($_GET['subject'])?$_GET['subject']:'';
|
|||
$field = isset($_GET['field'])?$_GET['field']:'';
|
||||
$id = isset($_GET['id'])?$_GET['id']:'';
|
||||
$definition = apiCall('GET',$apiUrl);
|
||||
$properties = properties($subject,$action,$definition);
|
||||
$references = references($subject,$properties);
|
||||
$referenced = referenced($subject,$properties);
|
||||
$primaryKey = primaryKey($subject,$properties);
|
||||
$debug = $debug?json_encode($definition,JSON_PRETTY_PRINT):false;
|
||||
|
||||
echo head();
|
||||
echo '<div class="menu">';
|
||||
echo menu($definition['tags']);
|
||||
echo '<div class="container-fluid">';
|
||||
echo '<div class="row">';
|
||||
echo menu($definition['tags'],$subject);
|
||||
echo '</div>';
|
||||
echo '<div class="content">';
|
||||
echo '<div class="row">';
|
||||
switch ($action){
|
||||
case '': echo home(); break;
|
||||
case 'list': echo listRecords($apiUrl,$subject,$field,$id,$references,$referenced,$primaryKey); break;
|
||||
case 'view': echo viewRecord($apiUrl,$subject,$id,$references,$referenced,$primaryKey); break;
|
||||
case 'list': echo listRecords($apiUrl,$subject,$field,$id,$definition); break;
|
||||
case 'edit': echo editRecord($apiUrl,$subject,$id,$definition); break;
|
||||
}
|
||||
echo '</div>';
|
||||
echo foot($debug);
|
||||
if ($debug) echo '<hr/><pre>'.$debug.'</pre></body></html>';
|
||||
echo '</div>';
|
||||
Loading…
Add table
Add a link
Reference in a new issue