Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
rest_form.php
Go to the documentation of this file.
1 <?php
3  function handle($params){
4  session_write_close();
6  $query = $app->getQuery();
7 
8  try {
9 
10  if ( @$query['--id'] ){
11  // This is a form for a particular record
12 
13  if ( @$query['-relationship'] ){
14  // This is a related record form
15 
16 
17  } else {
18  // This is an edit form for a particular record
19 
20  $rec = df_get_record_by_id($query['--id']);
21  if ( !$rec ){
22  throw new Exception("Record could not be found");
23  }
24  if ( PEAR::isError($rec) ) throw new Exception($rec->getMessage());
25 
26 
27  if ( !$rec->checkPermission('edit') ){
28  throw new Exception("Failed to get edit form for record. Permission denied");
29  }
30 
31  $tableObj = $rec->_table;
32 
33  $fields = null;
34  if ( @$query['--fields'] ){
35  $fields = explode(',', $query['--fields']);
36 
37  } else {
38 
39  $temp = $tableObj->fields(false, false, true);
40  $fields = array_keys($temp);
41 
42  }
43 
44  $form = array();
45 
46 
47 
48 
49  }
50 
51 
52  } else if ( @$query['-table'] ){
53  // This is a new record form for a particular table
54  $table = $query['-table'];
55  $tableObj = Dataface_Table::loadTable($table);
56 
57  $tablePerms = $tableObj->getPermissions();
58 
59  if ( !@$tablePerms['new'] ){
60  throw new Exception("Failed to build form data because you do not have permission to create new records on this table.");
61 
62  }
63 
64 
65 
66 
67  $fields = null;
68  if ( @$query['--fields'] ){
69  $fields = explode(',', $query['--fields']);
70 
71  } else {
72  $temp = $tableObj->fields(false, false, true);
73  $fields = array_keys($temp);
74 
75  }
76 
77  $form = array();
78  $defaults = array();
79  $valuelists = array();
80 
81  if ( !$fields ){
82  throw new Exception("No fields were specified for the form.");
83  }
84 
85  foreach ($fields as $f){
86 
87  $perms = $tableObj->getPermissions(array('field'=>$f));
88  if ( !@$perms['new']){
89  // No permission to create 'new' data on this field.
90  continue;
91  }
92 
93  $data = $tableObj->getField($f);
94 
95  $form[$f] = array(
96  'widget'=>$data['widget']
97  );
98 
99  $defaults[$f] = $tableObj->getDefaultValue($f);
100 
101  if ( @$data['vocabulary'] ){
102  $form[$f]['vocabulary'] = $data['vocabulary'];
103  if ( !isset($valuelists[$data['vocabulary']]) ){
104  $valuelists[$data['vocabulary']] = $tableObj->getValuelist($data['vocabulary']);
105 
106  }
107  }
108 
109  if ( @$data['validators'] ){
110  $form[$f]['validators'] = $data['validators'];
111  }
112 
113  }
114 
115  $this->out(array(
116  'code'=>200,
117  'message'=>'Form successfully created',
118  'form'=>$form,
119  'defaults'=>$defaults,
120  'valuelists'=>$valuelists
121 
122  ));
123  exit;
124 
125 
126 
127  } else {
128 
129  throw new Exception("Invalid parameters for rest_form");
130  }
131 
132  } catch (Exception $ex){
133 
134  $this->out(array(
135  'code' => $ex->getCode(),
136  'message' => $ex->getMessage()
137  ));
138  exit;
139  }
140 
141  }
142 
143  function out($params){
144  header('Content-type: application/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
145  echo json_encode($params);
146  }
147 }