Maurits van der Schee 8 роки тому
джерело
коміт
1370996321
1 змінених файлів з 0 додано та 270 видалено
  1. 0
    270
      examples/editor.php

+ 0
- 270
examples/editor.php Переглянути файл

@@ -1,270 +0,0 @@
1
-<?php
2
-$apiUrl = 'http://localhost:8001/blog.php';
3
-$debug = false;
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,$subject) {
23
-    $html= '<ul class="nav nav-pills">';
24
-    foreach ($tags as $tag) {
25
-        $active = $tag['name']==$subject?' class="active"':'';
26
-        $html.= '<li'.$active.'><a href="?action=list&subject='.$tag['name'].'">'.$tag['name'].'</a></li>';
27
-    }
28
-    $html.= '</ul>';
29
-    return $html;
30
-}
31
-
32
-function home($definition) {
33
-    $html = 'Nothing';
34
-    return $html;
35
-}
36
-
37
-function head() {
38
-    $html = '<!DOCTYPE html><html lang="en">';
39
-    $html.= '<head><title>PHP-CRUD-API editor</title>';
40
-    $html.= '<meta name="viewport" content="width=device-width, initial-scale=1">';
41
-    $html.= '<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">';
42
-    $html.= '<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet">';
43
-    $html.= '<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>';
44
-    $html.= '</head><body>';
45
-    return $html;
46
-}
47
-
48
-function displayColumn($columns) {
49
-	$names = array('name','title','description');
50
-	foreach ($names as $name) {
51
-		if (isset($columns[$name])) return $columns[$name];
52
-	}
53
-	return false;
54
-}
55
-
56
-
57
-function referenceText($subject,$data,$field,$id,$definition) {
58
-    $properties = properties($subject,$definition);
59
-    $references = references($subject,$properties);
60
-    $referenced = referenced($subject,$properties);
61
-    $primaryKey = primaryKey($subject,$properties);
62
-    
63
-    $indices = array_flip($data[$subject]['columns']);
64
-	$displayColumn = displayColumn($indices);
65
-	
66
-    $records = $data[$subject]['records'];
67
-    foreach ($records as $record) {
68
-        if ($record[$indices[$field]]==$id) {
69
-			if ($displayColumn===false) {
70
-				$text = '';
71
-				$first = true;
72
-				foreach ($record as $i=>$value) {
73
-					if (!$references[$i] && $i!=$primaryKey) {
74
-						if (!$first) $text.= ' - ';
75
-						$text.= $value;
76
-						$first = false;
77
-					}
78
-				} 
79
-				return $text;
80
-			} else {
81
-				return $record[$indices[$displayColumn]];
82
-			}
83
-        }
84
-    }
85
-    return '?';
86
-}
87
-
88
-function listRecords($apiUrl,$subject,$field,$id,$definition) {
89
-    $properties = properties($subject,$definition);
90
-    $references = references($subject,$properties);
91
-    $referenced = referenced($subject,$properties);
92
-    $primaryKey = primaryKey($subject,$properties);
93
-    
94
-    $args = array();
95
-    if ($field) {
96
-        $args['filter']=$field.',eq,'.$id; 
97
-    }
98
-    $include = implode(',',array_filter(array_map(function($v){ return $v[0]; },$references)));
99
-    if ($include) {
100
-        $args['include']=$include; 
101
-    }
102
-    $data = apiCall('GET',$apiUrl.'/'.$subject.'?'.http_build_query($args));
103
-    
104
-    $html = '';
105
-    if ($field) {
106
-        $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
107
-        $href = '?action=list&subject='.$subject;
108
-        $html .= ' <a href="'.$href.'">remove</a></p>';
109
-    }    
110
-    $html.= '<table class="table">';
111
-    $html.= '<tr>';
112
-    foreach ($data[$subject]['columns'] as $i=>$column) {
113
-        $html.= '<th>'.$column.'</th>';
114
-    }
115
-    $html.= '<th>has many</th>';
116
-    $html.= '<th>actions</th>';
117
-    $html.= '</tr>';
118
-    foreach ($data[$subject]['records'] as $record) {
119
-        $html.= '<tr>';
120
-        foreach ($record as $i=>$value) {
121
-            if ($references[$i]) {
122
-                $html.= '<td>';
123
-                $href = '?action=list&subject='.$references[$i][0].'&field='.$references[$i][1].'&id='.$value;
124
-                $html.= '<a href="'.$href.'">';
125
-                $html.= referenceText($references[$i][0],$data,$references[$i][1],$value,$definition);
126
-                $html.= '</a>';
127
-                $html.= '</td>';
128
-            } else {
129
-                $html.= '<td>'.$value.'</td>';
130
-            }
131
-        }
132
-        $html.= '<td>';
133
-        foreach ($referenced as $i=>$relations) {
134
-            $id = $record[$i];
135
-            if ($relations) foreach ($relations as $j=>$relation) {
136
-                if ($j) $html.= ', ';
137
-                $href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
138
-                $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
139
-            }
140
-        }
141
-        $html.= '</td>';
142
-        $html.= '<td>';
143
-        $html.= '<a href="?action=edit&subject='.$subject.'&id='.$record[$primaryKey].'">edit</a>';
144
-        $html.= '</td>';
145
-        $html.= '</tr>';
146
-    }
147
-    $html.= '</table>';
148
-    return $html;
149
-}
150
-
151
-function selectSubject($apiUrl,$subject,$name,$value,$definition) {
152
-    $properties = properties($subject,$definition);
153
-    $references = references($subject,$properties);
154
-    $primaryKey = primaryKey($subject,$properties);
155
-    
156
-    $data = apiCall('GET',$apiUrl.'/'.$subject);
157
-    
158
-	$indices = array_flip($data[$subject]['columns']);
159
-	$displayColumn = displayColumn($indices);
160
-	
161
-	$html = '<select class="form-control">';
162
-    foreach ($data[$subject]['records'] as $record) {
163
-		if ($displayColumn===false) {
164
-			$text = '';
165
-			$first = true;
166
-			foreach ($record as $i=>$field) {
167
-				if (!$references[$i] && $i!=$primaryKey) {
168
-					if (!$first) $text.= ' - ';
169
-					$text.= $field;
170
-					$first = false;
171
-				}
172
-			} 
173
-			$html.= '<option value="'.$record[$primaryKey].'">'.$text.'</option>';
174
-		} else {
175
-			$html.= '<option value="'.$record[$primaryKey].'">'.$record[$displayColumn].'</option>';
176
-		}
177
-    }
178
-    $html.= '</select>';
179
-    return $html;
180
-}
181
-
182
-function editRecord($apiUrl,$subject,$id,$definition) {
183
-    $properties = properties($subject,$definition);
184
-    $references = references($subject,$properties);
185
-    $referenced = referenced($subject,$properties);
186
-    $primaryKey = primaryKey($subject,$properties);
187
-    
188
-    $data = apiCall('GET',$apiUrl.'/'.$subject.'/'.$id);
189
-    $html = '<form>';
190
-    $i=0;
191
-    foreach ($data as $column=>$field) {
192
-        $html.= '<div class="form-group">';
193
-        $html.= '<label for="'.$column.'">'.$column.'</label>';
194
-        if ($references[$i]) {
195
-            $html.= selectSubject($apiUrl,$references[$i][0],$column,$field,$definition);
196
-        } else {
197
-            $readonly = $i==$primaryKey?' readonly':'';
198
-            $html.= '<input class="form-control" id="'.$column.'" value="'.$field.'"'.$readonly.'/>';
199
-        }
200
-        $html.= '</div>';
201
-        $i++;
202
-    }
203
-    $html.= '</form>';
204
-    return $html;
205
-}
206
-
207
-function properties($subject,$definition) {
208
-    if (!$subject || !$definition) return false;
209
-    $path = '/'.$subject;
210
-    if (!isset($definition['paths'][$path])) {
211
-        $path = '/'.$subject.'/{id}';
212
-    }
213
-    $properties = false;
214
-    if (isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
215
-        $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
216
-    } elseif (isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
217
-        $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'];
218
-    }
219
-    return $properties;
220
-}
221
-
222
-function references($subject,$properties) {
223
-    if (!$subject || !$properties) return false;
224
-    $references = array();
225
-    foreach ($properties as $field=>$property) {
226
-        $references[] = isset($property['x-references'])?$property['x-references']:false;
227
-    }
228
-    return $references;
229
-}
230
-
231
-function referenced($subject,$properties) {
232
-    if (!$subject || !$properties) return false;
233
-    $referenced = array();
234
-    foreach ($properties as $field=>$property) {
235
-        $referenced[] = isset($property['x-referenced'])?$property['x-referenced']:false;
236
-    }
237
-    return $referenced;
238
-}
239
-
240
-function primaryKey($subject,$properties) {
241
-    if (!$subject || !$properties) return false;
242
-    $i = 0;
243
-    foreach ($properties as $field=>$property) {
244
-        if (isset($property['x-primary-key'])) return $i;
245
-        $i++;
246
-    }
247
-    return false;
248
-}
249
-
250
-$action = isset($_GET['action'])?$_GET['action']:'';
251
-$subject = isset($_GET['subject'])?$_GET['subject']:'';
252
-$field = isset($_GET['field'])?$_GET['field']:'';
253
-$id = isset($_GET['id'])?$_GET['id']:'';
254
-$definition = apiCall('GET',$apiUrl);
255
-$debug = $debug?json_encode($definition,JSON_PRETTY_PRINT):false;
256
-
257
-echo head();
258
-echo '<div class="container-fluid">';
259
-echo '<div class="row">';
260
-echo menu($definition['tags'],$subject);
261
-echo '</div>';
262
-echo '<div class="row">';
263
-switch ($action){
264
-    case '': echo home(); break;
265
-    case 'list': echo listRecords($apiUrl,$subject,$field,$id,$definition); break;
266
-    case 'edit': echo editRecord($apiUrl,$subject,$id,$definition); break;
267
-}
268
-echo '</div>';
269
-if ($debug) echo '<hr/><pre>'.$debug.'</pre></body></html>';
270
-echo '</div>';

Loading…
Відмінити
Зберегти