Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
export_json.php
Go to the documentation of this file.
1 <?php
139  function handle(&$params){
141  $query =& $app->getQuery();
142 
143  $records = df_get_selected_records($query);
144  if ( !$records ){
145  if ( $query['-mode'] == 'list' ){
146  $records = df_get_records_array($query['-table'], $query);
147  } else {
148  $records = array( $app->getRecord() );
149  }
150  }
151 
152  $jsonProfile = 'basic';
153  if ( @$query['--profile'] ){
154  $jsonProfile = $query['--profile'];
155  }
156 
157  $displayMethod = 'val';
158  if ( @$query['--displayMethod'] == 'display' ){
159  $displayMethod = 'display';
160  } else if ( @$query['--displayMethod'] == 'htmlValue' ){
161  $displayMethod = 'htmlValue';
162  }
163 
164  $out = array();
165  if ( isset( $query['--fields'] ) ){
166  $fields = explode(' ', $query['--fields']);
167  } else {
168  $fields = null;
169  }
170 
171 
172  foreach ($records as $record){
173  if ( !$record->checkPermission('export_json') ){
174  continue;
175  }
176 
177  $del = $record->table()->getDelegate();
178  $row = null;
179  if ( isset($del) and method_exists($del, 'export_json') ){
180  $row = $del->export_json($record, $jsonProfile, $records);
181  }
182  if ( !isset($row) ){
183 
184  if ( !is_array($fields) ){
185  $fields = array_keys($record->table()->fields(false,true));
186  }
187  if ( is_array($fields) ){
188  $allowed_fields = array();
189  foreach ($fields as $field ){
190  if ( !$record->checkPermission('export_json', array('field'=>$field) ) ){
191  continue;
192  }
193  $allowed_fields[] = $field;
194  }
195  }
196 
197  $row = array();
198 
199  foreach ( $allowed_fields as $fld ){
200  $row[$fld] = $record->$displayMethod($fld);
201  }
202  }
203 
204  if ( isset($del) and method_exists($del, 'filter_json') ){
205  $del->filter_json($record, $row, $jsonProfile, $records);
206  }
207 
208  $out[] = $row;
209  }
210 
211  if ( @$query['--single'] ){
212  if ( count($out) > 0 ){
213  $out = $out[0];
214  }
215  }
216 
217  if ( @$query['--var'] ){
218  $out = array(
219  'code' => 200,
220  $query['--var'] => $out
221  );
222  }
223 
224  //import('Services/JSON.php');
225  //$json = new Services_JSON;
226  $enc_out = json_encode($out);
227  header('Content-type: application/json; charset='.$app->_conf['oe']);
228  header('Connection: close');
229  echo $enc_out;
230  exit;
231  }
232 }