Browse Source

Inline belongsto

Maurits van der Schee 8 years ago
parent
commit
a8e7964837
1 changed files with 48 additions and 23 deletions
  1. 48
    23
      examples/editor.php

+ 48
- 23
examples/editor.php View File

45
     return $html;
45
     return $html;
46
 }
46
 }
47
 
47
 
48
+function referenceText($subject,$data,$field,$id,$definition) {
49
+    $properties = properties($subject,$definition);
50
+    $references = references($subject,$properties);
51
+    $referenced = referenced($subject,$properties);
52
+    $primaryKey = primaryKey($subject,$properties);
53
+    
54
+    $indices = array_flip($data[$subject]['columns']);
55
+    $records = $data[$subject]['records'];
56
+    foreach ($records as $record) {
57
+        if ($record[$indices[$field]]==$id) {
58
+            $text = '';
59
+            $first = true;
60
+            foreach ($record as $i=>$value) {
61
+                if (!$references[$i] && $i!=$primaryKey) {
62
+                    if (!$first) $text.= ' - ';
63
+                    $text.= $value;
64
+                    $first = false;
65
+                }
66
+            } 
67
+            return $text;
68
+        }
69
+    }
70
+    return '?';
71
+}
72
+
48
 function listRecords($apiUrl,$subject,$field,$id,$definition) {
73
 function listRecords($apiUrl,$subject,$field,$id,$definition) {
49
     $properties = properties($subject,$definition);
74
     $properties = properties($subject,$definition);
50
     $references = references($subject,$properties);
75
     $references = references($subject,$properties);
51
     $referenced = referenced($subject,$properties);
76
     $referenced = referenced($subject,$properties);
52
     $primaryKey = primaryKey($subject,$properties);
77
     $primaryKey = primaryKey($subject,$properties);
53
     
78
     
54
-    $filter = '';
79
+    $args = array();
80
+    if ($field) {
81
+        $args['filter']=$field.',eq,'.$id; 
82
+    }
83
+    $include = implode(',',array_filter(array_map(function($v){ return $v[0]; },$references)));
84
+    if ($include) {
85
+        $args['include']=$include; 
86
+    }
87
+    $data = apiCall('GET',$apiUrl.'/'.$subject.'?'.http_build_query($args));
88
+    
55
     $html = '';
89
     $html = '';
56
     if ($field) {
90
     if ($field) {
57
-        $filter = '?filter[]='.$field.',eq,'.$id; 
58
         $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
91
         $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
59
         $href = '?action=list&subject='.$subject;
92
         $href = '?action=list&subject='.$subject;
60
         $html .= ' <a href="'.$href.'">remove</a></p>';
93
         $html .= ' <a href="'.$href.'">remove</a></p>';
61
-    }
62
-    $data = apiCall('GET',$apiUrl.'/'.$subject.$filter  );
94
+    }    
63
     $html.= '<table class="table">';
95
     $html.= '<table class="table">';
64
     $html.= '<tr>';
96
     $html.= '<tr>';
65
     foreach ($data[$subject]['columns'] as $i=>$column) {
97
     foreach ($data[$subject]['columns'] as $i=>$column) {
66
-        if (!$references[$i]) {
67
-            $html.= '<th>'.$column.'</th>';
68
-        }
98
+        $html.= '<th>'.$column.'</th>';
69
     }
99
     }
70
-    $html.= '<th>belongs to</th>';
71
     $html.= '<th>has many</th>';
100
     $html.= '<th>has many</th>';
72
     $html.= '<th>actions</th>';
101
     $html.= '<th>actions</th>';
73
     $html.= '</tr>';
102
     $html.= '</tr>';
74
     foreach ($data[$subject]['records'] as $record) {
103
     foreach ($data[$subject]['records'] as $record) {
75
         $html.= '<tr>';
104
         $html.= '<tr>';
76
         foreach ($record as $i=>$field) {
105
         foreach ($record as $i=>$field) {
77
-            if (!$references[$i]) {
106
+            if ($references[$i]) {
107
+                $html.= '<td>';
108
+                $href = '?action=list&subject='.$references[$i][0].'&field='.$references[$i][1].'&id='.$id;
109
+                $html.= '<a href="'.$href.'">';
110
+                $html.= referenceText($references[$i][0],$data,$references[$i][1],$field,$definition);
111
+                $html.= '</a>';
112
+                $html.= '</td>';
113
+            } else {
78
                 $html.= '<td>'.$field.'</td>';
114
                 $html.= '<td>'.$field.'</td>';
79
             }
115
             }
80
         }
116
         }
81
         $html.= '<td>';
117
         $html.= '<td>';
82
-        $first = true;
83
-        foreach ($references as $i=>$relation) {
84
-            $id = $record[$i];
85
-            if ($relation) {
86
-                if (!$first) $html.= ', ';
87
-                $href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
88
-                $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
89
-                $first = false;
90
-            }
91
-        }
92
-        $html.= '</td>';
93
-        $html.= '<td>';
94
         foreach ($referenced as $i=>$relations) {
118
         foreach ($referenced as $i=>$relations) {
95
             $id = $record[$i];
119
             $id = $record[$i];
96
             if ($relations) foreach ($relations as $j=>$relation) {
120
             if ($relations) foreach ($relations as $j=>$relation) {
120
         $text = '';
144
         $text = '';
121
         $first = true;
145
         $first = true;
122
         foreach ($record as $i=>$field) {
146
         foreach ($record as $i=>$field) {
123
-            if (!$references[$i]) {
147
+            if (!$references[$i] && $i!=$primaryKey) {
124
                 if (!$first) $text.= ' - ';
148
                 if (!$first) $text.= ' - ';
125
                 $text.= $field;
149
                 $text.= $field;
126
                 $first = false;
150
                 $first = false;
147
         if ($references[$i]) {
171
         if ($references[$i]) {
148
             $html.= selectSubject($apiUrl,$references[$i][0],$column,$field,$definition);
172
             $html.= selectSubject($apiUrl,$references[$i][0],$column,$field,$definition);
149
         } else {
173
         } else {
150
-            $html.= '<input class="form-control" id="'.$column.'" value="'.$field.'"/>';
174
+            $readonly = $i==$primaryKey?' readonly':'';
175
+            $html.= '<input class="form-control" id="'.$column.'" value="'.$field.'"'.$readonly.'/>';
151
         }
176
         }
152
         $html.= '</div>';
177
         $html.= '</div>';
153
         $i++;
178
         $i++;

Loading…
Cancel
Save