Xataface 2.0
Xataface Application Framework
actions/rest_form.php
Go to the documentation of this file.
00001 <?php
00002 class dataface_actions_rest_form {
00003         function handle($params){
00004                 session_write_close();
00005                 $app = Dataface_Application::getInstance();
00006                 $query = $app->getQuery();
00007                 
00008                 try {
00009                         
00010                         if ( @$query['--id'] ){
00011                                 // This is a form for a particular record
00012                                 
00013                                 if ( @$query['-relationship'] ){
00014                                         // This is a related record form
00015                                         
00016                                 
00017                                 } else {
00018                                         // This is an edit form for a particular record
00019                                         
00020                                         $rec = df_get_record_by_id($query['--id']);
00021                                         if ( !$rec ){
00022                                                 throw new Exception("Record could not be found");
00023                                         }
00024                                         if ( PEAR::isError($rec) ) throw new Exception($rec->getMessage());
00025                                         
00026                                         
00027                                         if ( !$rec->checkPermission('edit') ){
00028                                                 throw new Exception("Failed to get edit form for record.  Permission denied");
00029                                         }
00030                                         
00031                                         $tableObj = $rec->_table;
00032                                         
00033                                         $fields = null;
00034                                         if ( @$query['--fields'] ){
00035                                                 $fields = explode(',', $query['--fields']);
00036                                                 
00037                                         } else {
00038                                                 
00039                                                 $temp = $tableObj->fields(false, false, true);
00040                                                 $fields = array_keys($temp);
00041                                         
00042                                         }
00043                                         
00044                                         $form = array();
00045                                         
00046                                         
00047                                 
00048                                 
00049                                 }
00050                                 
00051                                 
00052                         } else if ( @$query['-table'] ){
00053                                 // This is a new record form for a particular table
00054                                 $table = $query['-table'];
00055                                 $tableObj = Dataface_Table::loadTable($table);
00056                                 
00057                                 $tablePerms = $tableObj->getPermissions();
00058                                 
00059                                 if ( !@$tablePerms['new'] ){
00060                                         throw new Exception("Failed to build form data because you do not have permission to create new records on this table.");
00061                                         
00062                                 }
00063                                 
00064                                 
00065                                 
00066                                 
00067                                 $fields = null;
00068                                 if ( @$query['--fields'] ){
00069                                         $fields = explode(',', $query['--fields']);
00070                                         
00071                                 } else {
00072                                         $temp = $tableObj->fields(false, false, true);
00073                                         $fields = array_keys($temp);
00074                                 
00075                                 }
00076                                 
00077                                 $form = array();
00078                                 $defaults = array();
00079                                 $valuelists = array();
00080                                 
00081                                 if ( !$fields ){
00082                                         throw new Exception("No fields were specified for the form.");
00083                                 }
00084                                 
00085                                 foreach ($fields as $f){
00086                                 
00087                                         $perms = $tableObj->getPermissions(array('field'=>$f));
00088                                         if ( !@$perms['new']){
00089                                                 // No permission to create 'new' data on this field.
00090                                                 continue;
00091                                         }
00092                                         
00093                                         $data = $tableObj->getField($f);
00094                                         
00095                                         $form[$f] = array(
00096                                                 'widget'=>$data['widget']
00097                                         );
00098                                         
00099                                         $defaults[$f] = $tableObj->getDefaultValue($f);
00100                                         
00101                                         if ( @$data['vocabulary'] ){
00102                                                 $form[$f]['vocabulary'] = $data['vocabulary'];
00103                                                 if ( !isset($valuelists[$data['vocabulary']]) ){
00104                                                         $valuelists[$data['vocabulary']] = $tableObj->getValuelist($data['vocabulary']);
00105                                                         
00106                                                 }
00107                                         }
00108                                         
00109                                         if ( @$data['validators'] ){
00110                                                 $form[$f]['validators'] = $data['validators'];
00111                                         }
00112                                         
00113                                 }
00114                                 
00115                                 $this->out(array(
00116                                         'code'=>200,
00117                                         'message'=>'Form successfully created',
00118                                         'form'=>$form,
00119                                         'defaults'=>$defaults,
00120                                         'valuelists'=>$valuelists
00121                                         
00122                                 ));
00123                                 exit;
00124                                 
00125                                 
00126                         
00127                         } else {
00128                         
00129                                 throw new Exception("Invalid parameters for rest_form");
00130                         }
00131                 
00132                 } catch (Exception $ex){
00133                 
00134                         $this->out(array(
00135                                 'code' => $ex->getCode(),
00136                                 'message' => $ex->getMessage()
00137                         ));
00138                         exit;
00139                 }
00140                 
00141         }
00142         
00143         function out($params){
00144                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00145                 echo json_encode($params);
00146         }
00147 }
 All Data Structures Namespaces Files Functions Variables Enumerations