function handle(&$params) { if (!@$_POST['history__id']) { return PEAR::raiseError("No history id specified", DATAFACE_E_ERROR); } $historyid = $_POST['history__id']; if (!preg_match('/\\d+/', $historyid)) { return PEAR::raiseError("Invalid history id provided.", DATAFACE_E_ERROR); } $app =& Dataface_Application::getInstance(); $record =& $app->getRecord(); if (!$record) { return PEAR::raiseError("No record was specified", DATAFACE_E_ERROR); } import("Dataface/HistoryTool.php"); $ht = new Dataface_HistoryTool(); $hrecord = $ht->getRecordById($record->_table->tablename, $historyid); // make sure that this history record matches the current record. $keys = array_keys($record->_table->keys()); if ($record->strvals($keys) != $hrecord->strvals($keys)) { return PEAR::raiseError("Attempt to restore record history from unmatching history record.", DATAFACE_E_ERROR); } // Now that we are convinced that we have the correct record, we can restore it. if (@$_POST['-fieldname']) { $fieldname = $_POST['-fieldname']; } else { $fieldname = null; } $res = $ht->restore($record, $historyid, $fieldname, true); if (PEAR::isError($res)) { return $res; } $url = false; //$app->getPreviousUrl(true); if (@$_POST['-locationid']) { $url = DATAFACE_SITE_HREF . '?' . $app->decodeLocation($_POST['-locationid']); } if (!$url) { // If the url is not specified we will just create a url to return // to the specified record's history listing. $url = $record->getURL('-action=history'); } if ($fieldname) { $msg = "Field '{$fieldname}' successfully restored to its value from '" . $hrecord->strval('history__modified') . "'."; } else { $msg = "Record successfully restored to its value from '" . $hrecord->strval('history__modified') . "'."; } $url .= "&--msg=" . urlencode($msg); header('Location: ' . $url); exit; }
function handle(&$params) { $app =& Dataface_Application::getInstance(); if (!@$_GET['history__id']) { return PEAR::raiseError('No history id supplied', DATAFACE_E_ERROR); } $historyid = $_GET['history__id']; $query =& $app->getQuery(); $table = $query['-table']; import('Dataface/HistoryTool.php'); $ht = new Dataface_HistoryTool(); if (@$_GET['-show_changes']) { $record = $ht->getDiffs($table, $historyid); } else { $record = $ht->getRecordById($table, $historyid); } if (!$record) { return PEAR::raiseError("No history record for table {$table} with history id {$historyid} could be found", DATAFACE_E_ERROR); } if (PEAR::isError($record)) { return $record; } $context = array('history_record' => &$record); $context['table_record'] = new Dataface_Record($table, $record->vals()); $t =& Dataface_Table::loadTable($table); $numfields = count($t->fields()); $pts = 0; $ppf = array(); foreach ($t->fields() as $field) { if ($t->isText($field['name'])) { $pts += 5; $ppf[$field['name']] = $pts; } else { $pts++; $ppf[$field['name']] = $pts; } } $firstField = null; $threshold = floatval(floatval($pts) / floatval(2)); foreach ($t->fields() as $field) { if ($ppf[$field['name']] >= $threshold) { $firstField = $field['name']; break; } } $context['first_field_second_col'] = $firstField; $context['changes'] = @$_GET['-show_changes']; $context['table'] =& $t; df_display($context, 'Dataface_HistoryRecordDetails.html'); }
function test_restore_to_date() { $app =& Dataface_Application::getInstance(); $record = df_get_record('HistoryToolTest', array('name' => 'Johnny')); $this->assertEquals('john.gif', $record->val('container_field')); $record->setValue('container_field', 'john2.gif'); $record->save(); $ht = new Dataface_HistoryTool(); $hid = $ht->logRecord($record); $history1 = $ht->getRecordById('HistoryToolTest', $hid); $this->assertEquals(array('name' => 'Johnny', 'container_field' => 'john2.gif'), $history1->strvals(array('name', 'container_field'))); $record->setValue('container_field', 'john3.gif'); $record->save(); $hid2 = $ht->logRecord($record); $history2 = $ht->getRecordById('HistoryToolTest', $hid2); $this->assertEquals(array('name' => 'Johnny', 'container_field' => 'john3.gif'), $history2->strvals(array('name', 'container_field'))); $record2 = df_get_record('HistoryToolTest', array('name' => 'Johnny')); $this->assertEquals($record2->strvals(array('name', 'container_field')), $history2->strvals(array('name', 'container_field'))); $sql = array(); $sql[] = "update `HistoryToolTest__history` set `history__modified` = '2004-01-02' where `history__id` = '{$hid}'"; foreach ($sql as $q) { $res = xf_db_query($q, $app->db()); if (!$res) { trigger_error(xf_db_error($app->db()), E_USER_ERROR); } } $ht->restoreToDate($record, '2004-02-02'); $record3 = df_get_record('HistoryToolTest', array('name' => 'Johnny')); $this->assertEquals(array('name' => 'Johnny', 'container_field' => 'john2.gif'), $record3->strvals(array('name', 'container_field'))); }
function handle(&$params) { $app =& Dataface_Application::getInstance(); if (!@$_GET['history__id']) { return PEAR::raiseError('No history id supplied', DATAFACE_E_ERROR); } $historyid = $_GET['history__id']; $query =& $app->getQuery(); $table = $query['-table']; $r = $app->getRecord(); import('Dataface/HistoryTool.php'); $ht = new Dataface_HistoryTool(); if (@$_GET['-fromcurrent']) { $record = $ht->getDiffs($table, $historyid); $record->escapeOutput = false; } else { if (@$_GET['-show_changes']) { $thisVersion = $ht->getRecordById($table, $historyid); if (PEAR::isError($thisVersion)) { return $thisVersion; } else { if (!$thisVersion) { return PEAR::raiseError('No history record found', DATAFACE_E_ERROR); } } $mdate = $thisVersion->strval("history__modified"); //echo "mdate: ".$mdate; $prevDate = date('Y-m-d H:i:s', strtotime('-1 second', strtotime($mdate))); //echo " prevdate: ".$prevDate.' '; $prevVersionId = $ht->getPreviousVersion($r, $prevDate, $thisVersion->val('history__language'), null, true); //echo "Prev: $prevVersionId"; if (!$prevVersionId) { $record = new Dataface_Record($table . '__history', array()); } else { $record = $ht->getDiffs($table, $prevVersionId, $historyid); } $record->escapeOutput = false; } else { $record = $ht->getRecordById($table, $historyid); } } if (!$record) { return PEAR::raiseError("No history record for table {$table} with history id {$historyid} could be found", DATAFACE_E_ERROR); } if (PEAR::isError($record)) { return $record; } $record->secureDisplay = false; $context = array('history_record' => &$record); $context['source_record'] = $app->getRecord(); $t =& Dataface_Table::loadTable($table); $numfields = count($t->fields()); $pts = 0; $ppf = array(); $fields = $t->fields(); $tmp = array(); foreach ($fields as $k => $f) { if ($r->checkPermission('view', array('field' => $k))) { $tmp[$k] = $fields[$k]; } } $fields = $tmp; $context['fields'] = $fields; foreach ($fields as $field) { if ($t->isText($field['name'])) { $pts += 5; $ppf[$field['name']] = $pts; } else { $pts++; $ppf[$field['name']] = $pts; } } $firstField = null; $threshold = floatval(floatval($pts) / floatval(2)); foreach ($fields as $field) { if ($ppf[$field['name']] >= $threshold) { $firstField = $field['name']; break; } } $context['first_field_second_col'] = $firstField; $context['changes'] = @$_GET['-show_changes']; $context['table'] =& $t; df_display($context, 'Dataface_HistoryRecordDetails.html'); }