Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
translate.php
Go to the documentation of this file.
1 <?php
2 
4 
5  function handle(&$params){
6  import('Dataface/TranslationForm.php');
7 
9  $query =& $app->getQuery();
10  $resultSet =& $app->getResultSet();
11 
12  $source = ( isset($_REQUEST['-sourceLanguage']) ? $_REQUEST['-sourceLanguage'] : $app->_conf['default_language']);
13  $dest = ( isset($_REQUEST['-destinationLanguage']) ? $_REQUEST['-destinationLanguage'] : null);
14 
15 
16  if ( $resultSet->found()>0){
17  $form = new Dataface_TranslationForm($query['-table'], $source, $dest);
18  /*
19  * There is either a result to edit, or we are creating a new record.
20  *
21  */
22 
23  $res = $form->_build();
24  if ( PEAR::isError($res) ){
25  throw new Exception($res->toString().Dataface_Error::printStackTrace(), E_USER_ERROR);
26 
27  }
28 
29  /*
30  *
31  * We need to add the current GET parameter flags (the GET vars starting with '-') so
32  * that the controller knows to pass control to this method again upon form submission.
33  *
34  */
35  foreach ( $query as $key=>$value){
36  if ( strpos($key,'-')===0 ){
37  $form->addElement('hidden', $key);
38  $form->setDefaults( array( $key=>$value) );
39 
40  }
41  }
42 
43  /*
44  * Store the current query string (the portion after the '?') in the form, so we
45  * can retrieve it after and redirect back to our original location.
46  */
47  $form->addElement('hidden', '-query');
48  $form->setDefaults( array( '-action'=>$query['-action'],'-query'=>$_SERVER['QUERY_STRING']) );
49 
50 
51  /*
52  *
53  * We have to deal with 3 cases.
54  * 1) The form has not been submitted.
55  * 2) The form was submitted but didn't validate (ie: it had some bad input)
56  * 3) The form was submitted and was validated.
57  *
58  * We deal with Case 3 first...
59  *
60  */
61 
62  if ( $form->validate() ){
63  /*
64  *
65  * The form was submitted and it validated ok. We now process it (ie: save its contents).
66  *
67  */
68  $app->clearMessages();
69  $result = $form->process( array( &$form, 'save') );
70  $success = true;
71  $response =& Dataface_Application::getResponse();
72 
73  if ( !$result ){
74  error_log("Error occurred in save: ".mysql_error( $app->db()).Dataface_Error::printStackTrace());
75  throw new Exception("Error occurred in save. See error log for details.");
76 
77 
79  //echo "Error..";
81  return $result;
82 
83  } else {
84  //echo "not dup entry"; exit;
85  throw new Exception($result->toString(), E_USER_ERROR);
86 
87  }
88  } else if ( Dataface_Error::isNotice($result) ){
89  $app->addError($result);
90 
91  //$response['--msg'] = @$response['--msg'] ."\n".$result->getMessage();
92  $success = false;
93  }
94 
95 
96  if ( $success ){
97  /*
98  *
99  * The original query string will have the -new flag set. We need to remove this
100  * flag so that we don't redirect the user to create another new record.
101  *
102  */
103  $vals = $form->exportValues();
104  $vals['-query'] = preg_replace('/[&\?]-new=[^&]+/i', '', $vals['-query']);
105 
106  $msg = implode("\n", $app->getMessages());
107  //$msg =@$response['--msg'];
108  $msg = urlencode(
110  /* i18n id */
111  'Record successfully translated',
112  /* Default success message */
113  "Record successfully translated.<br>"
114  ).$msg
115  );
116  $link = $_SERVER['HOST_URI'].DATAFACE_SITE_HREF.'?'.$vals['-query'].'&--msg='.$msg;
117 
118 
119  /*
120  *
121  * Redirect the user to the appropriate record.
122  *
123  */
124  $app->redirect($link);
125 
126  }
127  }
128 
129  ob_start();
130  $form->display();
131  $out = ob_get_contents();
132  ob_end_clean();
133 
134 
135  $context = array('form'=>$out, 'formObj'=>$form);
136 
137 
138  } else {
139  // no records were found
140  $context = array('form'=>'', 'formObj'=>$form);
141  $app->addMessage(
143  'No records matched request',
144  'No records matched your request'
145  )
146  );
147  }
148 
149 
150  if ( isset($query['-template']) ) $template = $query['-template'];
151  else if ( isset( $params['action']['template']) ) $template = $params['action']['template'];
152  else $template = 'Dataface_Translate_Record.html';
153  df_display($context, $template, true);
154 
155  }
156 
157 }