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,52 +45,76 @@ function head() {
45 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 73
 function listRecords($apiUrl,$subject,$field,$id,$definition) {
49 74
     $properties = properties($subject,$definition);
50 75
     $references = references($subject,$properties);
51 76
     $referenced = referenced($subject,$properties);
52 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 89
     $html = '';
56 90
     if ($field) {
57
-        $filter = '?filter[]='.$field.',eq,'.$id; 
58 91
         $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
59 92
         $href = '?action=list&subject='.$subject;
60 93
         $html .= ' <a href="'.$href.'">remove</a></p>';
61
-    }
62
-    $data = apiCall('GET',$apiUrl.'/'.$subject.$filter  );
94
+    }    
63 95
     $html.= '<table class="table">';
64 96
     $html.= '<tr>';
65 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 100
     $html.= '<th>has many</th>';
72 101
     $html.= '<th>actions</th>';
73 102
     $html.= '</tr>';
74 103
     foreach ($data[$subject]['records'] as $record) {
75 104
         $html.= '<tr>';
76 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 114
                 $html.= '<td>'.$field.'</td>';
79 115
             }
80 116
         }
81 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 118
         foreach ($referenced as $i=>$relations) {
95 119
             $id = $record[$i];
96 120
             if ($relations) foreach ($relations as $j=>$relation) {
@@ -120,7 +144,7 @@ function selectSubject($apiUrl,$subject,$name,$value,$definition) {
120 144
         $text = '';
121 145
         $first = true;
122 146
         foreach ($record as $i=>$field) {
123
-            if (!$references[$i]) {
147
+            if (!$references[$i] && $i!=$primaryKey) {
124 148
                 if (!$first) $text.= ' - ';
125 149
                 $text.= $field;
126 150
                 $first = false;
@@ -147,7 +171,8 @@ function editRecord($apiUrl,$subject,$id,$definition) {
147 171
         if ($references[$i]) {
148 172
             $html.= selectSubject($apiUrl,$references[$i][0],$column,$field,$definition);
149 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 177
         $html.= '</div>';
153 178
         $i++;

Loading…
Cancel
Save