Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
history_restore_record.php
Go to the documentation of this file.
1 <?php
3 
4  function handle(&$params){
5 
6  if ( !@$_POST['history__id'] ) return PEAR::raiseError("No history id specified", DATAFACE_E_ERROR);
7  $historyid = $_POST['history__id'];
8  if ( !preg_match('/\d+/', $historyid) ) return PEAR::raiseError("Invalid history id provided.", DATAFACE_E_ERROR);
9 
11  $record =& $app->getRecord();
12  if ( !$record ) return PEAR::raiseError("No record was specified", DATAFACE_E_ERROR);
13 
14  import("Dataface/HistoryTool.php");
15  $ht = new Dataface_HistoryTool();
16  $hrecord = $ht->getRecordById($record->_table->tablename, $historyid);
17 
18  // make sure that this history record matches the current record.
19  $keys = array_keys($record->_table->keys());
20  if ( $record->strvals($keys) != $hrecord->strvals($keys) )
21  return PEAR::raiseError("Attempt to restore record history from unmatching history record.", DATAFACE_E_ERROR);
22 
23 
24  // Now that we are convinced that we have the correct record, we can restore it.
25  if ( @$_POST['-fieldname'] ) $fieldname = $_POST['-fieldname'];
26  else $fieldname = null;
27  $res = $ht->restore($record, $historyid, $fieldname, true);
28 
29  if ( PEAR::isError($res) ) return $res;
30 
31  $url = false; //$app->getPreviousUrl(true);
32  if ( @$_POST['-locationid'] ) $url = DATAFACE_SITE_HREF.'?'.$app->decodeLocation($_POST['-locationid']);
33 
34  if ( !$url ){
35  // If the url is not specified we will just create a url to return
36  // to the specified record's history listing.
37  $url = $record->getURL('-action=history');
38 
39  }
40 
41  if ( $fieldname ){
42  $msg = "Field '$fieldname' successfully restored to its value from '".$hrecord->strval('history__modified')."'.";
43  } else {
44  $msg = "Record successfully restored to its value from '".$hrecord->strval('history__modified')."'.";
45  }
46  $url .= "&--msg=".urlencode($msg);
47 
48  $app->redirect($url);
49 
50  }
51 
52 }
53 
54 ?>