Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
new_related_record.php
Go to the documentation of this file.
1 <?php
3 
4  function handle(&$params){
5  //global $myctr;
7  $query =& $app->getQuery();
8  $resultSet =& $app->getResultSet();
9 
10  //$record =& $app->getRecord(); // loads the current record
11 
12  import( 'Dataface/ShortRelatedRecordForm.php');
13  if ( !isset( $query['-relationship'])){
15  'No relationship specified in new related record',
16  'No relationship was specified while trying to create new related record. Please specify a relationship.'
18  );
19  }
20 
21 
22  $record = null; // we let the Form automatically handle loading of record.
23  $form = new Dataface_ShortRelatedRecordForm($record, $query['-relationship']);
24 
25  $form->_build();
26 
27  /*
28  *
29  * We need to add the current GET parameter flags (the GET vars starting with '-') so
30  * that the controller knows to pass control to this method again upon form submission.
31  *
32  */
33  //$myctr = 0;
34  foreach ( $query as $key=>$value){
35  //echo "doing $key";
36 
37  if ( strpos($key,'-')===0 ){
38  $form->addElement('hidden', $key);
39 
40 
41  $form->setDefaults( array( $key=>$value) );
42  //if ( $myctr == 2 ) exit;
43 
44 
45 
46  }
47  //$myctr++;
48  }
49 
50  /*
51  * Store the current query string (the portion after the '?') in the form, so we
52  * can retrieve it after and redirect back to our original location.
53  */
54  $form->addElement('hidden', '-query');
55  $form->setDefaults( array( '-action'=>$query['-action'],'-query'=>$_SERVER['QUERY_STRING']) );
56 
57 
58  if ( !Dataface_PermissionsTool::checkPermission('add new related record',$form->_record, array('relationship'=>$query['-relationship']))){
61  'Permission denied while trying to add new related record',
62  'Permission Denied: You do not have permission to add related records to the current record.'
63  )
64  );
65  //$this->_vars['error'] = "<div class=\"error\">Error. Permission Denied.<!-- At line ".__LINE__." of file ".__FILE__." --></div>";
66  //return;
67  }
68 
69  if ( $form->validate() ){
70  $vals = $form->exportValues();
71 
72  $res = $form->process(array(&$form, 'save'), true);
73 
74  $response =& Dataface_Application::getResponse();
75 
76  if ( PEAR::isError($res) && !Dataface_Error::isNotice($res) ){
77  return $res;
78  //$this->_vars['error'] = "<div class=\"error\">Error. ".$res->toString()."<!-- At line ".__LINE__." of file ".__FILE__." --></div>";
79  //return;
80  } else if ( Dataface_Error::isNotice($res) ){
81  $success = false;
82  $app->addError($res);
83  //$response['--msg'] = @$response['--msg'] . "\n".$res->getMessage();
84  } else {
85  $success = true;
86  }
87 
88  if ( $success ){
89  import('Dataface/Utilities.php');
90  Dataface_Utilities::fireEvent('after_action_new_related_record');
91  $fquery = array('-action'=>'browse');
92  $msg = urlencode(
93  trim(
95  "Record successfully added to relationship",
96  "Record successfully added to ".$query['-relationship']." relationship.\n",
97  array('relationship'=>$query['-relationship'])
98  ).
99  (isset($response['--msg']) ? $response['--msg'] : '')
100  )
101  );
102 
103 
104  foreach ($vals['__keys__'] as $key=>$value){
105  $fquery[$key] = "=".$value;
106  }
107  $fquery['-relationship'] = $query['-relationship'];
108  $fquery['-action'] = 'related_records_list';
109  $link = Dataface_LinkTool::buildLink($fquery);
110  $app->redirect("$link"."&--msg=".$msg);
111 
112  }
113  }
114 
115  ob_start();
116  $gdefs = array();
117  foreach ( $_GET as $gkey=>$gval ){
118  if ( substr($gkey,0, 4) == '--q:' ){
119  $gdefs[substr($gkey, 4)] = $gval;
120  }
121  }
122  if ( count($gdefs) > 0 ){
123  $form->setDefaults($gdefs);
124  }
125 
126 
127 
128 
129  $form->display();
130  $out = ob_get_contents();
131  ob_end_clean();
132 
133 
134  $context = array('form'=>$out);
135  if ( isset($query['-template']) ) $template = $query['-template'];
136  else if ( isset( $params['action']['template']) ) $template = $params['action']['template'];
137  else $template = 'Dataface_Add_New_Related_Record.html';
138  df_display($context, $template, true);
139 
140  }
141 }
142 
143 
144 
145 ?>