.
*/
namespace Fisharebest\Webtrees;
/**
* Defined in session.php
*
* @global Tree $WT_TREE
*/
global $WT_TREE;
use Fisharebest\Webtrees\Controller\SimpleController;
use Fisharebest\Webtrees\Functions\FunctionsDate;
use Fisharebest\Webtrees\Functions\FunctionsImport;
define('WT_SCRIPT_NAME', 'edit_changes.php');
require './includes/session.php';
$controller = new SimpleController;
$controller
->restrictAccess(Auth::isModerator($WT_TREE))
->setPageTitle(I18N::translate('Pending changes'))
->pageHeader()
->addInlineJavascript("
function show_diff(diffurl) {
window.opener.location = diffurl;
return false;
}
");
$action = Filter::get('action');
$change_id = Filter::getInteger('change_id');
$index = Filter::get('index');
$ged = Filter::getInteger('ged');
echo '
', I18N::translate('Pending changes'), '
';
switch ($action) {
case 'undo':
$gedcom_id = Database::prepare("SELECT gedcom_id FROM `##change` WHERE change_id=?")->execute(array($change_id))->fetchOne();
$xref = Database::prepare("SELECT xref FROM `##change` WHERE change_id=?")->execute(array($change_id))->fetchOne();
// Undo a change, and subsequent changes to the same record
Database::prepare(
"UPDATE `##change`" .
" SET status = 'rejected'" .
" WHERE status = 'pending'" .
" AND gedcom_id = ?" .
" AND xref = ?" .
" AND change_id >= ?"
)->execute(array($gedcom_id, $xref, $change_id));
break;
case 'accept':
$gedcom_id = Database::prepare("SELECT gedcom_id FROM `##change` WHERE change_id=?")->execute(array($change_id))->fetchOne();
$xref = Database::prepare("SELECT xref FROM `##change` WHERE change_id=?")->execute(array($change_id))->fetchOne();
// Accept a change, and all previous changes to the same record
$changes = Database::prepare(
"SELECT change_id, gedcom_id, gedcom_name, xref, old_gedcom, new_gedcom" .
" FROM `##change` c" .
" JOIN `##gedcom` g USING (gedcom_id)" .
" WHERE c.status = 'pending'" .
" AND gedcom_id = ?" .
" AND xref = ?" .
" AND change_id <= ?" .
" ORDER BY change_id"
)->execute(array($gedcom_id, $xref, $change_id))->fetchAll();
foreach ($changes as $change) {
if (empty($change->new_gedcom)) {
// delete
FunctionsImport::updateRecord($change->old_gedcom, $gedcom_id, true);
} else {
// add/update
FunctionsImport::updateRecord($change->new_gedcom, $gedcom_id, false);
}
Database::prepare("UPDATE `##change` SET status='accepted' WHERE change_id=?")->execute(array($change->change_id));
Log::addEditLog("Accepted change {$change->change_id} for {$change->xref} / {$change->gedcom_name} into database");
}
break;
case 'undoall':
Database::prepare(
"UPDATE `##change`" .
" SET status='rejected'" .
" WHERE status='pending' AND gedcom_id=?"
)->execute(array($WT_TREE->getTreeId()));
break;
case 'acceptall':
$changes = Database::prepare(
"SELECT change_id, gedcom_id, gedcom_name, xref, old_gedcom, new_gedcom" .
" FROM `##change` c" .
" JOIN `##gedcom` g USING (gedcom_id)" .
" WHERE c.status='pending' AND gedcom_id=?" .
" ORDER BY change_id"
)->execute(array($WT_TREE->getTreeId()))->fetchAll();
foreach ($changes as $change) {
if (empty($change->new_gedcom)) {
// delete
FunctionsImport::updateRecord($change->old_gedcom, $change->gedcom_id, true);
} else {
// add/update
FunctionsImport::updateRecord($change->new_gedcom, $change->gedcom_id, false);
}
Database::prepare("UPDATE `##change` SET status='accepted' WHERE change_id=?")->execute(array($change->change_id));
Log::addEditLog("Accepted change {$change->change_id} for {$change->xref} / {$change->gedcom_name} into database");
}
break;
}
$changed_gedcoms = Database::prepare(
"SELECT g.gedcom_name" .
" FROM `##change` c" .
" JOIN `##gedcom` g USING (gedcom_id)" .
" WHERE c.status='pending'" .
" GROUP BY g.gedcom_name"
)->fetchOneColumn();
if ($changed_gedcoms) {
$changes = Database::prepare(
"SELECT c.*, UNIX_TIMESTAMP(c.change_time) + :offset AS change_timestamp, u.user_name, u.real_name, g.gedcom_name, new_gedcom, old_gedcom" .
" FROM `##change` c" .
" JOIN `##user` u USING (user_id)" .
" JOIN `##gedcom` g USING (gedcom_id)" .
" WHERE c.status='pending'" .
" ORDER BY gedcom_id, c.xref, c.change_id"
)
->execute(array('offset' => WT_TIMESTAMP_OFFSET))
->fetchAll();
$output = '
';
$prev_xref = null;
$prev_gedcom_id = null;
foreach ($changes as $change) {
$tree = Tree::findById($change->gedcom_id);
preg_match('/^0 (?:@' . WT_REGEX_XREF . '@ )?(' . WT_REGEX_TAG . ')/', $change->old_gedcom . $change->new_gedcom, $match);
switch ($match[1]) {
case 'INDI':
$record = new Individual($change->xref, $change->old_gedcom, $change->new_gedcom, $tree);
break;
case 'FAM':
$record = new Family($change->xref, $change->old_gedcom, $change->new_gedcom, $tree);
break;
case 'SOUR':
$record = new Source($change->xref, $change->old_gedcom, $change->new_gedcom, $tree);
break;
case 'REPO':
$record = new Repository($change->xref, $change->old_gedcom, $change->new_gedcom, $tree);
break;
case 'OBJE':
$record = new Media($change->xref, $change->old_gedcom, $change->new_gedcom, $tree);
break;
case 'NOTE':
$record = new Note($change->xref, $change->old_gedcom, $change->new_gedcom, $tree);
break;
default:
$record = new GedcomRecord($change->xref, $change->old_gedcom, $change->new_gedcom, $tree);
break;
}
if ($change->xref != $prev_xref || $change->gedcom_id != $prev_gedcom_id) {
if ($prev_xref) {
$output .= '
';
} else {
// No pending changes - refresh the parent window and close this one
$controller->addInlineJavascript('closePopupAndReloadParent();');
}
echo '