Skip to content

Commit

Permalink
Add meta information for referenced and primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed May 22, 2016
1 parent 6f14a0e commit fb79a6e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
22 changes: 11 additions & 11 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1610,10 +1610,10 @@ protected function swagger($settings) {
}
$result = $this->db->query($this->db->getSql('reflect_has_many'),array($table_names,$table_list[0],$database,$database));
while ($row = $this->db->fetchRow($result)) {
$table_fields[$table['name']][$row[3]]->related[]=array($row[0],$row[1]);
$table_fields[$table['name']][$row[3]]->referenced[]=array($row[0],$row[1]);
}
$primary = $this->findPrimaryKey($table_list[0],$database);
$table_fields[$table['name']][$primary]->primary = true;
$primaryKey = $this->findPrimaryKey($table_list[0],$database);
$table_fields[$table['name']][$primaryKey]->primaryKey = true;

foreach (array('root_actions','id_actions') as $path) {
foreach ($table[$path] as $i=>$action) {
Expand Down Expand Up @@ -1736,14 +1736,14 @@ protected function swagger($settings) {
if ($k>0) echo ',';
echo '"'.$field.'": {';
echo '"type": "string"';
if (isset($action['fields'][$field]->related)) {
echo ',"x-related": '.json_encode($action['fields'][$field]->related);
if (isset($action['fields'][$field]->referenced)) {
echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
}
if (isset($action['fields'][$field]->references)) {
echo ',"x-references": "'.$action['fields'][$field]->references.'"';
}
if (isset($action['fields'][$field]->primary)) {
echo ',"x-primary": true';
if (isset($action['fields'][$field]->primaryKey)) {
echo ',"x-primary-key": true';
}
echo '}';
}
Expand Down Expand Up @@ -1831,14 +1831,14 @@ protected function swagger($settings) {
if ($k>0) echo ',';
echo '"'.$field.'": {';
echo '"type": "string"';
if (isset($action['fields'][$field]->related)) {
echo ',"x-related": '.json_encode($action['fields'][$field]->related);
if (isset($action['fields'][$field]->referenced)) {
echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
}
if (isset($action['fields'][$field]->references)) {
echo ',"x-references": "'.$action['fields'][$field]->references.'"';
}
if (isset($action['fields'][$field]->primary)) {
echo ',"x-primary": true';
if (isset($action['fields'][$field]->primaryKey)) {
echo ',"x-primary-key": true';
}
echo '}';
}
Expand Down
32 changes: 16 additions & 16 deletions examples/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function foot($debug) {
return $html;
}

function listRecords($apiUrl,$subject,$field,$id,$references,$related,$primary) {
function listRecords($apiUrl,$subject,$field,$id,$references,$referenced,$primaryKey) {
$filter = '';
$html = '';
if ($field) {
Expand Down Expand Up @@ -73,7 +73,7 @@ function listRecords($apiUrl,$subject,$field,$id,$references,$related,$primary)
}
}
$html.= '<td>';
foreach ($related as $i=>$relations) {
foreach ($referenced as $i=>$relations) {
$id = $record[$i];
if ($relations) foreach ($relations as $j=>$relation) {
if ($j) $html.= ' ';
Expand All @@ -83,15 +83,15 @@ function listRecords($apiUrl,$subject,$field,$id,$references,$related,$primary)
}
$html.= '</td>';
$html.= '<td>';
$html.= '<a href="?action=view&subject='.$subject.'&id='.$record[$primary].'">view</a>';
$html.= '<a href="?action=view&subject='.$subject.'&id='.$record[$primaryKey].'">view</a>';
$html.= '</td>';
$html.= '</tr>';
}
$html.= '</table>';
return $html;
}

function viewRecord($apiUrl,$subject,$id,$references,$related,$primary) {
function viewRecord($apiUrl,$subject,$id,$references,$referenced,$primaryKey) {
$data = apiCall('GET',$apiUrl.'/'.$subject.'/'.$id);
$html = '<table>';
$i=0;
Expand All @@ -108,7 +108,7 @@ function viewRecord($apiUrl,$subject,$id,$references,$related,$primary) {
}
$html.= '<tr><th>related</th><td>';
$keys = array_keys($data);
foreach ($related as $i=>$relations) {
foreach ($referenced as $i=>$relations) {
$id = $data[$keys[$i]];
if ($relations) foreach ($relations as $j=>$relation) {
if ($j) $html.= ' ';
Expand All @@ -118,7 +118,7 @@ function viewRecord($apiUrl,$subject,$id,$references,$related,$primary) {
}
$html.= '</td></tr>';
$html.= '<tr><th>actions</th><td>';
$html.= '<a href="?action=view&subject='.$subject.'&id='.$data[$keys[$primary]].'">view</a>';
$html.= '<a href="?action=view&subject='.$subject.'&id='.$data[$keys[$primaryKey]].'">view</a>';
$html.= '</td></tr>';
$html.= '</table>';
return $html;
Expand Down Expand Up @@ -151,20 +151,20 @@ function references($subject,$properties) {
return $references;
}

function related($subject,$properties) {
function referenced($subject,$properties) {
if (!$subject || !$properties) return false;
$related = array();
$referenced = array();
foreach ($properties as $field=>$property) {
$related[] = isset($property['x-related'])?$property['x-related']:false;
$referenced[] = isset($property['x-referenced'])?$property['x-referenced']:false;
}
return $related;
return $referenced;
}

function primary($subject,$properties) {
function primaryKey($subject,$properties) {
if (!$subject || !$properties) return false;
$i = 0;
foreach ($properties as $field=>$property) {
if (isset($property['x-primary'])) return $i;
if (isset($property['x-primary-key'])) return $i;
$i++;
}
return false;
Expand All @@ -177,8 +177,8 @@ function primary($subject,$properties) {
$definition = apiCall('GET',$apiUrl);
$properties = properties($subject,$action,$definition);
$references = references($subject,$properties);
$related = related($subject,$properties);
$primary = primary($subject,$properties);
$referenced = referenced($subject,$properties);
$primaryKey = primaryKey($subject,$properties);
$debug = $debug?json_encode($definition,JSON_PRETTY_PRINT):false;

echo head();
Expand All @@ -188,8 +188,8 @@ function primary($subject,$properties) {
echo '<div class="content">';
switch ($action){
case '': echo home(); break;
case 'list': echo listRecords($apiUrl,$subject,$field,$id,$references,$related,$primary); break;
case 'view': echo viewRecord($apiUrl,$subject,$id,$references,$related,$primary); break;
case 'list': echo listRecords($apiUrl,$subject,$field,$id,$references,$referenced,$primaryKey); break;
case 'view': echo viewRecord($apiUrl,$subject,$id,$references,$referenced,$primaryKey); break;
}
echo '</div>';
echo foot($debug);

0 comments on commit fb79a6e

Please sign in to comment.