'; foreach ($tags as $tag) { $active = $tag['name']==$subject?' class="active"':''; $html.= ''.$tag['name'].''; } $html.= ''; return $html; } function home($definition) { $html = 'Nothing'; return $html; } function head() { $html = ''; $html.= 'PHP-CRUD-API editor'; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; return $html; } function displayColumn($columns) { $names = array('name','title','description'); foreach ($names as $name) { if (isset($columns[$name])) return $columns[$name]; } return false; } 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']); $displayColumn = displayColumn($indices); $records = $data[$subject]['records']; foreach ($records as $record) { if ($record[$indices[$field]]==$id) { if ($displayColumn===false) { $text = ''; $first = true; foreach ($record as $i=>$value) { if (!$references[$i] && $i!=$primaryKey) { if (!$first) $text.= ' - '; $text.= $value; $first = false; } } return $text; } else { return $record[$indices[$displayColumn]]; } } } 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 .= '

filtered where "'.$field.'" = "'.$id.'".'; $href = '?action=list&subject='.$subject; $html .= ' remove

'; } $html.= ''; $html.= ''; foreach ($data[$subject]['columns'] as $i=>$column) { $html.= ''; } $html.= ''; $html.= ''; $html.= ''; foreach ($data[$subject]['records'] as $record) { $html.= ''; foreach ($record as $i=>$value) { if ($references[$i]) { $html.= ''; } else { $html.= ''; } } $html.= ''; $html.= ''; $html.= ''; } $html.= '
'.$column.'has manyactions
'; $href = '?action=list&subject='.$references[$i][0].'&field='.$references[$i][1].'&id='.$value; $html.= ''; $html.= referenceText($references[$i][0],$data,$references[$i][1],$value,$definition); $html.= ''; $html.= ''.$value.''; 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.= ''.$relation[0].''; } } $html.= ''; $html.= 'edit'; $html.= '
'; 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); $indices = array_flip($data[$subject]['columns']); $displayColumn = displayColumn($indices); $html = ''; 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 = '
'; $i=0; foreach ($data as $column=>$field) { $html.= '
'; $html.= ''; if ($references[$i]) { $html.= selectSubject($apiUrl,$references[$i][0],$column,$field,$definition); } else { $readonly = $i==$primaryKey?' readonly':''; $html.= ''; } $html.= '
'; $i++; } $html.= '
'; 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 '
'; echo '
'; echo menu($definition['tags'],$subject); echo '
'; echo '
'; 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 '
'; if ($debug) echo '
'.$debug.'
'; echo '
';