Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
ajax_form.php
Go to the documentation of this file.
1 <?php
2 
4 
5  function handle(&$params){
6 
8  header('Content-type: text/html; charset='.$app->_conf['oe']);
9  $record =& $app->getRecord();
10  $query =& $app->getQuery();
11 
12  if ( isset($_REQUEST['-form-id']) ) $formid = $_REQUEST['-form-id'];
13  else $formid = 'ajax-form-'.rand();
14 
15  // First let's figure out what kind of form this is
16  $form_type = @$_REQUEST['-form-type'];
17  $form = null;
18 
19  if ( isset($_REQUEST['-fields']) ){
20  $fields = explode(',', $_REQUEST['-fields']);
21  } else {
22  $fields = null;
23  }
24 
25  switch ($form_type){
26  case 'new':
27 
28  $form = df_create_new_record_form($query['-table'], $fields);
29  $form->_build();
30  break;
31 
32  case 'edit':
33  $form = df_create_edit_record_form($query['-table'], $fields);
34  break;
35 
36  case 'new_related_record':
37  $form = df_create_new_related_record_form($record, $query['-relationship'], $fields);
38  break;
39 
40  case 'existing_related_record':
41  $form = df_create_existing_related_record_form($record, $query['-relationship']);
42  break;
43 
44  case 'composite':
45  import('Dataface/CompositeForm.php');
46  $form = new Dataface_CompositeForm($fields);
47  $form->build();
48  break;
49 
50  default:
51  @include_once('forms/'.$form_type.'.php');
52  if ( !class_exists('forms_'.$form_type) ){
53  return PEAR::raiseError('Could not find form of type "'.$form_type.'".', DATAFACE_E_ERROR);
54  }
55  $classname = 'forms_'.$form_type;
56  $form = new $classname($fields);
57  break;
58 
59  }
60 
61 
62  // We want the form to be submitted to the embedded iframe
63  $form->updateAttributes(array('target'=>$formid.'-target', 'accept-charset'=>$app->_conf['ie']));
64  $formparams = preg_grep('/^-[^\-].*/', array_keys($query));
65  foreach ( $formparams as $param){
66  $form->addElement('hidden',$param);
67  $form->setDefaults(array($param=>$query[$param]));
68  }
69  $form->addElement('hidden', '-form-id');
70  $form->setDefaults(array('-form-id'=>$formid));
71 
72  // Now that we have our form, we can do our thing with it.
73  if ( $form->validate() ){
74  /*
75  *
76  * The form was submitted and it validated ok. We now process it (ie: save its contents).
77  *
78  */
79  $app->clearMessages();
80  $result = $form->process( array( &$form, 'save') );
81  $success = true;
82  $response =& Dataface_Application::getResponse();
83 
84  if ( !$result ){
85  trigger_error("Error occurred in save: ".mysql_error( $app->db()).Dataface_Error::printStackTrace(), E_USER_ERROR);
86  exit;
88 
90  return $result;
91 
92  } else {
93 
94  trigger_error($result->toString(). Dataface_Error::printStackTrace(), E_USER_ERROR);
95  exit;
96  }
97  } else if ( Dataface_Error::isNotice($result) ){
98  $app->addError($result);
99  $success = false;
100  }
101 
102 
103  if ( $success ){
104  import('Dataface/Utilities.php');
105  Dataface_Utilities::fireEvent('after_action_ajax_form');
106 
107  $msg = implode("\n", $app->getMessages());
108  //$msg =@$response['--msg'];
109  $msg = urlencode(
111  /* i18n id */
112  'Record successfully saved',
113  /* Default success message */
114  "Record successfully saved.<br>"
115  ).$msg
116  );
117  // We need to output the success content.
118  // This could be in any of the following formats:
119  // 1. HTML --- actually not yet.. let's just do JSON
120  // 2. JSON
121  // 3. XML --- not yet.. just JSON for now.
122 
123  $targetid = @$_REQUEST['-target-id'];
124 
125  // This should:
126  // 1. Get the target element.
127  // 2. Go through the element's subtree and replace
128  // values that have been changed. How do we know what
129  // values have been changed.
130  //
131  if ( method_exists($form, 'htmlValues') ){
132  if ( method_exists($form, 'changedFields') ){
133  $changed_fields = $form->changedFields();
134  } else {
135  $changed_fields = null;
136  }
137 
138  // Convert the values to JSON
139  $changed_values = $form->htmlValues($changed_fields);
140  import('Services/JSON.php');
141  $json = new Services_JSON();
142  $changed_values_json = $json->encode($changed_values);
143 
144  } else {
145  $changed_values_json = '{}';
146  }
147 
148  echo <<<END
149 <html><body><script language="javascript"><!--
150 
151  //self.onload = function(){
152  //parent.handleEditableResponse('$targetid', $changed_values_json);
153  var targetel = parent.document.getElementById('$targetid');
154  targetel.handleResponse('$targetid', $changed_values_json);
155  targetel.onclick=parent.makeEditable;
156  targetel.onmouseover=targetel.old_onmouseover;
157  targetel.edit_form.parentNode.removeChild(targetel.edit_form);
158 
159  //}
160 
161 
162 //--></script></body></html>
163 END;
164  exit;
165 
166  }
167  }
168 
169  import('Dataface/FormTool.php');
170  $formTool = new Dataface_FormTool();
171  ob_start();
172  if (is_array($fields) and (count($fields) == 1) and (strpos($fields[0], '#') !== false) ){
173  $singleField = $fields[0];
174  } else {
175  $singleField = false;
176  }
177  $formTool->display($form, null, $singleField);
178  $out = ob_get_contents();
179  ob_end_clean();
180 
181  echo <<<END
182 
183  <div id="{$formid}-wrapper">
184  <iframe id="{$formid}-target" name="{$formid}-target" style="width:0px; height:0px; border: 0px"></iframe>
185  $out
186  </div>
187 END;
188  if ($form->isSubmitted()){
189  // The form has already been submitted so we must be displaying some
190  // errors. We need to remove this stuff from inside the iframe
191  // that we are going to be inside of, and place them on the page
192  // in the correct place
193  echo <<<END
194 <script language="javascript"><!--
195 var targetel = parent.document.getElementById('{$formid}-wrapper');
196 var sourceel = document.getElementById('{$formid}-wrapper');
197 targetel.innerHTML = sourceel.innerHTML;
198 //--></script>
199 END;
200  }
201 
202  exit;
203 
204  }
205 }
206 ?>