Xataface 2.0
Xataface Application Framework
actions/ajax_form.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class dataface_actions_ajax_form {
00004 
00005         function handle(&$params){
00006 
00007                 $app = Dataface_Application::getInstance();
00008                 header('Content-type: text/html; charset='.$app->_conf['oe']);
00009                 $record =& $app->getRecord();
00010                 $query =& $app->getQuery();
00011                 
00012                 if ( isset($_REQUEST['-form-id']) ) $formid = $_REQUEST['-form-id'];
00013                 else $formid = 'ajax-form-'.rand();
00014                 
00015                 // First let's figure out what kind of form this is
00016                 $form_type = @$_REQUEST['-form-type'];
00017                 $form = null;
00018                 
00019                 if ( isset($_REQUEST['-fields']) ){
00020                         $fields = explode(',', $_REQUEST['-fields']);
00021                 } else {
00022                         $fields = null;
00023                 }
00024                 
00025                 switch ($form_type){
00026                         case 'new':
00027                                 
00028                                 $form = df_create_new_record_form($query['-table'], $fields);
00029                                 $form->_build();
00030                                 break;
00031                         
00032                         case 'edit':
00033                                 $form = df_create_edit_record_form($query['-table'], $fields);
00034                                 break;
00035                                 
00036                         case 'new_related_record':
00037                                 $form = df_create_new_related_record_form($record, $query['-relationship'], $fields);
00038                                 break;
00039                                 
00040                         case 'existing_related_record':
00041                                 $form = df_create_existing_related_record_form($record, $query['-relationship']);
00042                                 break;
00043                                 
00044                         case 'composite':
00045                                 import('Dataface/CompositeForm.php');
00046                                 $form = new Dataface_CompositeForm($fields);
00047                                 $form->build();
00048                                 break;
00049                                 
00050                         default:
00051                                 @include_once('forms/'.$form_type.'.php');
00052                                 if ( !class_exists('forms_'.$form_type) ){
00053                                         return PEAR::raiseError('Could not find form of type "'.$form_type.'".', DATAFACE_E_ERROR);
00054                                 }
00055                                 $classname = 'forms_'.$form_type;
00056                                 $form = new $classname($fields);
00057                                 break;
00058                 
00059                 }
00060                 
00061                 
00062                 // We want the form to be submitted to the embedded iframe
00063                 $form->updateAttributes(array('target'=>$formid.'-target', 'accept-charset'=>$app->_conf['ie']));
00064                 $formparams = preg_grep('/^-[^\-].*/', array_keys($query));
00065                 foreach ( $formparams as $param){
00066                         $form->addElement('hidden',$param);
00067                         $form->setDefaults(array($param=>$query[$param]));
00068                 }
00069                 $form->addElement('hidden', '-form-id');
00070                 $form->setDefaults(array('-form-id'=>$formid));
00071                 
00072                 // Now that we have our form, we can do our thing with it.
00073                 if ( $form->validate() ){
00074                         /*
00075                          *
00076                          * The form was submitted and it validated ok.  We now process it (ie: save its contents).
00077                          *
00078                          */
00079                         $app->clearMessages();
00080                         $result = $form->process( array( &$form, 'save') );
00081                         $success = true;
00082                         $response =& Dataface_Application::getResponse();
00083                         
00084                         if ( !$result ){
00085                                 trigger_error("Error occurred in save: ".mysql_error( $app->db()).Dataface_Error::printStackTrace(), E_USER_ERROR);
00086                                 exit;
00087                         } else if ( PEAR::isError($result) && !Dataface_Error::isNotice($result) ){
00088 
00089                                 if ( Dataface_Error::isDuplicateEntry($result) ){
00090                                         return $result;
00091                                         
00092                                 } else {
00093 
00094                                         trigger_error($result->toString(). Dataface_Error::printStackTrace(), E_USER_ERROR);
00095                                         exit;
00096                                 }
00097                         } else if ( Dataface_Error::isNotice($result) ){
00098                                 $app->addError($result);
00099                                 $success = false;
00100                         }
00101                         
00102                         
00103                         if ( $success ){
00104                                 import('Dataface/Utilities.php');
00105                                 Dataface_Utilities::fireEvent('after_action_ajax_form');
00106                                 
00107                                 $msg = implode("\n", $app->getMessages());
00108                                 //$msg =@$response['--msg'];
00109                                 $msg = urlencode(
00110                                         Dataface_LanguageTool::translate(
00111                                                 /* i18n id */
00112                                                 'Record successfully saved',
00113                                                 /* Default success message */
00114                                                 "Record successfully saved.<br>"
00115                                         ).$msg
00116                                 );
00117                                 // We need to output the success content.
00118                                 // This could be in any of the following formats:
00119                                 //      1. HTML --- actually not yet.. let's just do JSON
00120                                 //      2. JSON
00121                                 //      3. XML --- not yet.. just JSON for now.
00122                                 
00123                                 $targetid = @$_REQUEST['-target-id'];
00124                                 
00125                                 // This should:
00126                                 // 1. Get the target element.
00127                                 // 2. Go through the element's subtree and replace
00128                                 //              values that have been changed.  How do we know what
00129                                 //              values have been changed.
00130                                 // 
00131                                 if ( method_exists($form, 'htmlValues') ){
00132                                         if ( method_exists($form, 'changedFields') ){
00133                                                 $changed_fields = $form->changedFields();
00134                                         } else {
00135                                                 $changed_fields = null;
00136                                         }
00137                                         
00138                                         // Convert the values to JSON
00139                                         $changed_values = $form->htmlValues($changed_fields);
00140                                         import('Services/JSON.php');
00141                                         $json = new Services_JSON();
00142                                         $changed_values_json = $json->encode($changed_values);
00143                                         
00144                                 } else {
00145                                         $changed_values_json = '{}';
00146                                 }
00147 
00148                                 echo <<<END
00149 <html><body><script language="javascript"><!--
00150         
00151         //self.onload =  function(){
00152                 //parent.handleEditableResponse('$targetid', $changed_values_json);
00153                 var targetel = parent.document.getElementById('$targetid');
00154                 targetel.handleResponse('$targetid', $changed_values_json);
00155                 targetel.onclick=parent.makeEditable;
00156                 targetel.onmouseover=targetel.old_onmouseover;
00157                 targetel.edit_form.parentNode.removeChild(targetel.edit_form);
00158         
00159         //}
00160         
00161         
00162 //--></script></body></html>
00163 END;
00164                                 exit;
00165                                                 
00166                         }
00167                 }
00168                 
00169                 import('Dataface/FormTool.php');
00170                 $formTool = new Dataface_FormTool();
00171                 ob_start();
00172                 if (is_array($fields) and (count($fields) == 1) and (strpos($fields[0], '#') !== false) ){
00173                         $singleField = $fields[0];
00174                 } else {
00175                         $singleField = false;
00176                 }
00177                 $formTool->display($form, null, $singleField);
00178                 $out = ob_get_contents();
00179                 ob_end_clean();
00180                         
00181                 echo <<<END
00182                 
00183                 <div id="{$formid}-wrapper">
00184                         <iframe id="{$formid}-target" name="{$formid}-target" style="width:0px; height:0px; border: 0px"></iframe>
00185                         $out
00186                 </div>
00187 END;
00188                 if ($form->isSubmitted()){
00189                         // The form has already been submitted so we must be displaying some
00190                         // errors.  We need to remove this stuff from inside the iframe
00191                         // that we are going to be inside of, and place them on the page
00192                         // in the correct place
00193                         echo <<<END
00194 <script language="javascript"><!--
00195 var targetel = parent.document.getElementById('{$formid}-wrapper');
00196 var sourceel = document.getElementById('{$formid}-wrapper');
00197 targetel.innerHTML = sourceel.innerHTML;
00198 //--></script>
00199 END;
00200                 }
00201                 
00202                 exit;
00203                 
00204         }
00205 }
00206 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations