![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 import('Dataface/LinkTool.php'); 00003 class dataface_actions_existing_related_record { 00004 function handle(&$params){ 00005 import( 'Dataface/ExistingRelatedRecordForm.php'); 00006 00007 $app =& Dataface_Application::getInstance(); 00008 $query =& $app->getQuery(); 00009 $resultSet =& $app->getResultSet(); 00010 00011 //$record =& $app->getRecord(); // loads the current record 00012 00013 if ( !isset( $query['-relationship'] ) ){ 00014 return PEAR::raiseError( 00015 Dataface_LanguageTool::translate( 00016 'Error: No relationship specified', 00017 'Error. No relationship was specified when trying to add existing related record.' 00018 ), 00019 DATAFACE_E_NOTICE 00020 ); 00021 00022 } 00023 $record = null; 00024 $form = new Dataface_ExistingRelatedRecordForm($record, $query['-relationship']); 00025 $res = $form->_build(); 00026 if ( PEAR::isError($res) ) return Dataface_Error::permissionDenied($res->getMessage()); 00027 00028 /* 00029 * 00030 * We need to add the current GET parameter flags (the GET vars starting with '-') so 00031 * that the controller knows to pass control to this method again upon form submission. 00032 * 00033 */ 00034 foreach ( $query as $key=>$value){ 00035 if ( strpos($key,'-')===0 ){ 00036 $form->addElement('hidden', $key); 00037 $form->setDefaults( array( $key=>$value) ); 00038 00039 } 00040 } 00041 00042 /* 00043 * Store the current query string (the portion after the '?') in the form, so we 00044 * can retrieve it after and redirect back to our original location. 00045 */ 00046 $form->addElement('hidden', '-query'); 00047 $form->setDefaults( array( '-action'=>$query['-action'],'-query'=>$_SERVER['QUERY_STRING']) ); 00048 00049 00050 if ( !$form->_record || !is_a($form->_record, 'Dataface_Record') ){ 00051 throw new Exception( 00052 Dataface_LanguageTool::translate( 00053 'Fatal Error', 00054 'Fatal Error: Form should have loaded record but the record was null. '.Dataface_Error::printStackTrace(), 00055 array('stack_trace'=>Dataface_Error::printStackTrace(), 'msg'=>'Form should have loaded record but the record was null.') 00056 ), 00057 E_USER_ERROR 00058 ); 00059 } 00060 00061 if ( !Dataface_PermissionsTool::checkPermission('add existing related record',$form->_record) ) { 00062 return Dataface_Error::permissionDenied( 00063 Dataface_LanguageTool::translate( 00064 'Error: Permission denied adding existing related record', 00065 'Permission Denied. You do not have sufficient permissions to add an existing related record. Required permission: "add existing related record", but you have only been granted permissions: "'.implode(',',$form->_record->getPermissions()).'".', 00066 array('required_permission'=>'add existing related record', 'granted_permissions'=>implode(',', $form->_record->getPermissions()) ) 00067 ) 00068 ); 00069 00070 } 00071 00072 if ( $form->validate() ){ 00073 $res = $form->process(array(&$form, 'save'), true); 00074 $response =& Dataface_Application::getResponse(); 00075 00076 if ( PEAR::isError($res) && !Dataface_Error::isNotice($res) ){ 00077 return $res; 00078 } else if ( Dataface_Error::isNotice($res) ){ 00079 //$response['--msg'] = @$response['--msg'] . "\n".$res->getMessage(); 00080 $app->addError(PEAR::raiseError( 00081 df_translate( 00082 'Failed to add record because of errors', 00083 'Failed to add record to relationship because of the following errors:' 00084 ), 00085 DATAFACE_E_NOTICE) 00086 ); 00087 $app->addError($res); 00088 $success = false; 00089 } else { 00090 $success = true; 00091 } 00092 if ( $success ){ 00093 import('Dataface/Utilities.php'); 00094 Dataface_Utilities::fireEvent('after_action_existing_related_record'); 00095 $fquery = array('-action'=>'browse'); 00096 $msg = Dataface_LanguageTool::translate( 00097 'Record successfully added to relationship', 00098 "The record has been successfully added to the ".$query['-relationship']." relationship.\n" , 00099 array('relationship'=>$query['-relationship']) 00100 ); 00101 $msg = urlencode(trim(($success ? $msg :'').@$response['--msg'])); 00102 00103 00104 $vals = $form->exportValues(); 00105 if ( isset($vals['--redirect']) ){ 00106 $qmark = (strpos($vals['--redirect'],'?') !== false) ? '&':'?'; 00107 $app->redirect($vals['--redirect'].$qmark.'--msg='.$msg); 00108 } 00109 foreach ($vals['__keys__'] as $key=>$value){ 00110 $fquery[$key] = "=".$value; 00111 } 00112 $link = Dataface_LinkTool::buildLink($fquery); 00113 $app->redirect("$link"."&--msg=".$msg); 00114 00115 } 00116 } 00117 00118 00119 ob_start(); 00120 $form->display(); 00121 $out = ob_get_contents(); 00122 ob_end_clean(); 00123 00124 00125 $context = array('form'=>$out); 00126 if ( isset($query['-template']) ) $template = $query['-template']; 00127 else if ( isset( $params['action']['template']) ) $template = $params['action']['template']; 00128 else $template = 'Dataface_Add_Existing_Related_Record.html'; 00129 df_display($context, $template, true); 00130 } 00131 00132 00133 00134 } 00135 ?>