Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
existing_related_record.php
Go to the documentation of this file.
1 <?php
2 import('Dataface/LinkTool.php');
4  function handle(&$params){
5  import( 'Dataface/ExistingRelatedRecordForm.php');
6 
8  $query =& $app->getQuery();
9  $resultSet =& $app->getResultSet();
10 
11  //$record =& $app->getRecord(); // loads the current record
12 
13  if ( !isset( $query['-relationship'] ) ){
14  return PEAR::raiseError(
16  'Error: No relationship specified',
17  'Error. No relationship was specified when trying to add existing related record.'
18  ),
20  );
21 
22  }
23  $record = null;
24  $form = new Dataface_ExistingRelatedRecordForm($record, $query['-relationship']);
25  $res = $form->_build();
26  if ( PEAR::isError($res) ) return Dataface_Error::permissionDenied($res->getMessage());
27 
28  /*
29  *
30  * We need to add the current GET parameter flags (the GET vars starting with '-') so
31  * that the controller knows to pass control to this method again upon form submission.
32  *
33  */
34  foreach ( $query as $key=>$value){
35  if ( strpos($key,'-')===0 ){
36  $form->addElement('hidden', $key);
37  $form->setDefaults( array( $key=>$value) );
38 
39  }
40  }
41 
42  /*
43  * Store the current query string (the portion after the '?') in the form, so we
44  * can retrieve it after and redirect back to our original location.
45  */
46  $form->addElement('hidden', '-query');
47  $form->setDefaults( array( '-action'=>$query['-action'],'-query'=>$_SERVER['QUERY_STRING']) );
48 
49 
50  if ( !$form->_record || !is_a($form->_record, 'Dataface_Record') ){
51  throw new Exception(
53  'Fatal Error',
54  'Fatal Error: Form should have loaded record but the record was null. '.Dataface_Error::printStackTrace(),
55  array('stack_trace'=>Dataface_Error::printStackTrace(), 'msg'=>'Form should have loaded record but the record was null.')
56  ),
57  E_USER_ERROR
58  );
59  }
60 
61  if ( !$form->_record->checkPermission('add existing related record', array('relationship'=>$query['-relationship']))){
62  //if ( !Dataface_PermissionsTool::checkPermission('add existing related record',$form->_record) ) {
65  'Error: Permission denied adding existing related record',
66  '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()).'".',
67  array('required_permission'=>'add existing related record', 'granted_permissions'=>implode(',', $form->_record->getPermissions()) )
68  )
69  );
70 
71  }
72 
73  if ( $form->validate() ){
74  $res = $form->process(array(&$form, 'save'), true);
75  $response =& Dataface_Application::getResponse();
76 
77  if ( PEAR::isError($res) && !Dataface_Error::isNotice($res) ){
78  return $res;
79  } else if ( Dataface_Error::isNotice($res) ){
80  //$response['--msg'] = @$response['--msg'] . "\n".$res->getMessage();
81  $app->addError(PEAR::raiseError(
82  df_translate(
83  'Failed to add record because of errors',
84  'Failed to add record to relationship because of the following errors:'
85  ),
87  );
88  $app->addError($res);
89  $success = false;
90  } else {
91  $success = true;
92  }
93  if ( $success ){
94  import('Dataface/Utilities.php');
95  Dataface_Utilities::fireEvent('after_action_existing_related_record');
96  $fquery = array('-action'=>'browse');
98  'Record successfully added to relationship',
99  "The record has been successfully added to the ".$query['-relationship']." relationship.\n" ,
100  array('relationship'=>$query['-relationship'])
101  );
102  $msg = urlencode(trim(($success ? $msg :'').@$response['--msg']));
103 
104 
105  $vals = $form->exportValues();
106  if ( isset($vals['--redirect']) ){
107  $qmark = (strpos($vals['--redirect'],'?') !== false) ? '&':'?';
108  $app->redirect($vals['--redirect'].$qmark.'--msg='.$msg);
109  }
110  foreach ($vals['__keys__'] as $key=>$value){
111  $fquery[$key] = "=".$value;
112  }
113  $link = Dataface_LinkTool::buildLink($fquery);
114  $app->redirect("$link"."&--msg=".$msg);
115 
116  }
117  }
118 
119 
120  ob_start();
121  $form->display();
122  $out = ob_get_contents();
123  ob_end_clean();
124 
125 
126  $context = array('form'=>$out);
127  if ( isset($query['-template']) ) $template = $query['-template'];
128  else if ( isset( $params['action']['template']) ) $template = $params['action']['template'];
129  else $template = 'Dataface_Add_Existing_Related_Record.html';
130  df_display($context, $template, true);
131  }
132 
133 
134 
135 }
136 ?>