Browse Source

Start of editor

Maurits van der Schee 9 years ago
parent
commit
60750aacd2
2 changed files with 220 additions and 5 deletions
  1. 34
    5
      api.php
  2. 186
    0
      examples/editor.php

+ 34
- 5
api.php View File

@@ -949,11 +949,10 @@ class PHP_CRUD_API {
949 949
 		}
950 950
 	}
951 951
 
952
-	protected function processKeyParameter($key,$tables,$database) {
953
-		if (!$key) return false;
952
+	protected function findPrimaryKey($table,$database) {
954 953
 		$count = 0;
955 954
 		$field = false;
956
-		if ($result = $this->db->query($this->db->getSql('reflect_pk'),array($tables[0],$database))) {
955
+		if ($result = $this->db->query($this->db->getSql('reflect_pk'),array($table,$database))) {
957 956
 			while ($row = $this->db->fetchRow($result)) {
958 957
 				$count++;
959 958
 				$field = $row[0];
@@ -961,6 +960,12 @@ class PHP_CRUD_API {
961 960
 			$this->db->close($result);
962 961
 		}
963 962
 		if ($count!=1 || $field==false) $this->exitWith404('1pk');
963
+		return $field;
964
+	}
965
+
966
+	protected function processKeyParameter($key,$tables,$database) {
967
+		if (!$key) return false;
968
+		$field = $this->findPrimaryKey($tables[0],$database);
964 969
 		return array($key,$field);
965 970
 	}
966 971
 
@@ -1595,10 +1600,22 @@ class PHP_CRUD_API {
1595 1600
 		}
1596 1601
 
1597 1602
 		foreach ($tables as $t=>$table)	{
1603
+			$table_list = array($table['name']);
1604
+			$table_fields = $this->findFields($table_list,false,$database);
1605
+			$table_names = array_map(function($v){ return $v['name'];},$tables);
1606
+			
1607
+			$result = $this->db->query($this->db->getSql('reflect_belongs_to'),array($table_list[0],$table_names,$database,$database));
1608
+			while ($row = $this->db->fetchRow($result)) {
1609
+				$table_fields[$table['name']][$row[1]]->references=$row[2];
1610
+			}
1611
+			$result = $this->db->query($this->db->getSql('reflect_has_many'),array($table_names,$table_list[0],$database,$database));
1612
+			while ($row = $this->db->fetchRow($result)) {
1613
+				$table_fields[$table['name']][$row[3]]->related[]=array($row[0],$row[1]);
1614
+			}
1615
+			
1598 1616
 			foreach (array('root_actions','id_actions') as $path) {
1599 1617
 				foreach ($table[$path] as $i=>$action) {
1600
-					$table_list = array($table['name']);
1601
-					$fields = $this->findFields($table_list,false,$database);
1618
+					$fields = $table_fields;
1602 1619
 					if ($table_authorizer) $this->applyTableAuthorizer($table_authorizer,$action['name'],$database,$table_list);
1603 1620
 					if ($column_authorizer) $this->applyColumnAuthorizer($column_authorizer,$action['name'],$database,$fields);
1604 1621
 					if (!$table_list || !$fields[$table['name']]) $tables[$t][$path][$i] = false;
@@ -1717,6 +1734,12 @@ class PHP_CRUD_API {
1717 1734
 							if ($k>0) echo ',';
1718 1735
 							echo '"'.$field.'": {';
1719 1736
 							echo '"type": "string"';
1737
+							if (isset($action['fields'][$field]->related)) {
1738
+								echo ',"x-related": '.json_encode($action['fields'][$field]->related);
1739
+							}
1740
+							if (isset($action['fields'][$field]->references)) {
1741
+								echo ',"x-references": "'.$action['fields'][$field]->references.'"';
1742
+							}
1720 1743
 							echo '}';
1721 1744
 						}
1722 1745
 						echo '}'; //properties
@@ -1803,6 +1826,12 @@ class PHP_CRUD_API {
1803 1826
 							if ($k>0) echo ',';
1804 1827
 							echo '"'.$field.'": {';
1805 1828
 							echo '"type": "string"';
1829
+							if (isset($action['fields'][$field]->related)) {
1830
+								echo ',"x-related": '.json_encode($action['fields'][$field]->related);
1831
+							}
1832
+							if (isset($action['fields'][$field]->references)) {
1833
+								echo ',"x-references": "'.$action['fields'][$field]->references.'"';
1834
+							}
1806 1835
 							echo '}';
1807 1836
 						}
1808 1837
 						echo '}'; //properties

+ 186
- 0
examples/editor.php View File

@@ -0,0 +1,186 @@
1
+<?php
2
+$apiUrl = 'http://localhost:8001/blog.php';
3
+$debug = true;
4
+
5
+function apiCall($method, $url, $data = false) {
6
+    $ch = curl_init();
7
+    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
8
+    curl_setopt($ch, CURLOPT_URL, $url);
9
+    if ($data) {
10
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
11
+        $headers = array();
12
+        $headers[] = 'Content-Type: application/json';
13
+        $headers[] = 'Content-Length: ' . strlen($data);
14
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
15
+    }
16
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
17
+    $response = curl_exec($ch);
18
+    curl_close($ch);
19
+    return json_decode($response,true);
20
+}
21
+
22
+function menu($tags) {
23
+    $html = '<ul>';
24
+    foreach ($tags as $tag) {
25
+        $html.= '<li><a href="?action=list&subject='.$tag['name'].'">'.$tag['name'].'</a></li>';
26
+    }
27
+    $html.= '</ul></div>';
28
+    return $html;
29
+}
30
+
31
+function home($definition) {
32
+    $html = 'Nothing';
33
+    return $html;
34
+}
35
+
36
+function head() {
37
+    $html = '<html><head></head><body>';
38
+    return $html;
39
+}
40
+
41
+function foot($debug) {
42
+    if ($debug) $html = '<hr/><pre>'.$debug.'</pre></body></html>';
43
+    else $html = '';
44
+    return $html;
45
+}
46
+
47
+function listRecords($apiUrl,$subject,$field,$id,$references,$related) {
48
+    $filter = '';
49
+    $html = '';
50
+    if ($field) {
51
+        $filter = '?filter[]='.$field.',eq,'.$id; 
52
+        $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
53
+        $href = '?action=list&subject='.$subject;
54
+        $html .= ' <a href="'.$href.'">remove</a></p>';
55
+    }
56
+    $data = apiCall('GET',$apiUrl.'/'.$subject.$filter  );
57
+    $html.= '<table>';
58
+    $html.= '<tr>';
59
+    foreach ($data[$subject]['columns'] as $column) {
60
+        $html.= '<th>'.$column.'</th>';
61
+    }
62
+    $html.= '<th>related</th>';
63
+    $html.= '</tr>';
64
+    foreach ($data[$subject]['records'] as $record) {
65
+        $html.= '<tr>';
66
+        foreach ($record as $i=>$field) {
67
+            if ($references[$i]) {
68
+                $href = '?action=view&subject='.$references[$i].'&id='.$field;
69
+                $html.= '<td><a href="'.$href.'">'.$field.'</a></td>';
70
+            } else {
71
+                $html.= '<td>'.$field.'</td>';
72
+            }
73
+        }
74
+        $html.= '<td>';
75
+        foreach ($related as $i=>$relations) {
76
+            $id = $record[$i];
77
+            foreach ($relations as $j=>$relation) {
78
+                if ($j) $html.= ' ';
79
+                $href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
80
+                $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
81
+            }
82
+        }
83
+        $html.= '</td>';
84
+        $html.= '</tr>';
85
+    }
86
+    $html.= '</table>';
87
+    return $html;
88
+}
89
+
90
+function viewRecord($apiUrl,$subject,$id,$references,$related) {
91
+    $data = apiCall('GET',$apiUrl.'/'.$subject.'/'.$id);
92
+    $html = '<table>';
93
+    $i=0;
94
+    foreach ($data as $column=>$field) {
95
+        $html.= '<tr><th>'.$column.'</th>';
96
+        if ($references[$i]) {
97
+            $href = '?action=view&subject='.$references[$i].'&id='.$field;
98
+            $html.= '<td><a href="'.$href.'">'.$field.'</a></td>';
99
+        } else {
100
+            $html.= '<td>'.$field.'</td>';
101
+        }
102
+        $html.= '</tr>';
103
+        $i++;
104
+    }
105
+    $html.= '<tr><th>related</th><td>';
106
+    foreach ($related as $i=>$relations) {
107
+        $keys = array_keys($data);
108
+        $id = $data[$keys[$i]];
109
+        foreach ($relations as $j=>$relation) {
110
+            if ($j) $html.= ' ';
111
+            $href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
112
+            $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
113
+        }
114
+    }
115
+    $html.= '</td></tr>';
116
+    $html.= '</table>';
117
+    return $html;
118
+}
119
+
120
+
121
+function references($subject,$action,$definition) {
122
+    if (!$subject || !$definition) return false;
123
+    if ($action=='view') {
124
+        $path = '/'.$subject.'/{id}';
125
+        if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
126
+            return false;
127
+        }
128
+        $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
129
+    } else {
130
+        $path = '/'.$subject;
131
+        if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
132
+            return false;
133
+        }
134
+        $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'];
135
+    }
136
+    $references = array();
137
+    foreach ($properties as $field=>$property) {
138
+        $references[] = isset($property['x-references'])?$property['x-references']:false;
139
+    }
140
+    return $references;
141
+}
142
+
143
+function related($subject,$action,$definition) {
144
+    if (!$subject || !$definition) return false;
145
+    if ($action=='view') {
146
+        $path = '/'.$subject.'/{id}';
147
+        if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
148
+            return false;
149
+        }
150
+        $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
151
+    } else {
152
+        $path = '/'.$subject;
153
+        if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
154
+            return false;
155
+        }
156
+        $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'];
157
+    }
158
+    $related = array();
159
+    foreach ($properties as $field=>$property) {
160
+        $related[] = isset($property['x-related'])?$property['x-related']:false;
161
+    }
162
+    return $related;
163
+}
164
+
165
+
166
+$action = isset($_GET['action'])?$_GET['action']:'';
167
+$subject = isset($_GET['subject'])?$_GET['subject']:'';
168
+$field = isset($_GET['field'])?$_GET['field']:'';
169
+$id = isset($_GET['id'])?$_GET['id']:'';
170
+$definition = apiCall('GET',$apiUrl);
171
+$references = references($subject,$action,$definition);
172
+$related = related($subject,$action,$definition);
173
+$debug = $debug?json_encode($definition,JSON_PRETTY_PRINT):false;
174
+
175
+echo head();
176
+echo '<div class="menu">';
177
+echo menu($definition['tags']);
178
+echo '</div>';
179
+echo '<div class="content">';
180
+switch ($action){
181
+    case '': echo home(); break;
182
+    case 'list': echo listRecords($apiUrl,$subject,$field,$id,$references,$related); break;
183
+    case 'view': echo viewRecord($apiUrl,$subject,$id,$references,$related); break;
184
+}
185
+echo '</div>';
186
+echo foot($debug);

Loading…
Cancel
Save