
$opcode = isset($_POST['opcode']) ? $_POST['opcode']:'';
$src = $_POST['srcpg'];
$trg = $_POST['trgpg'];
// action directive
switch($opcode)
{
case 'verify':
// verify code here
echo 'verify action '.$src.' '.$trg.'<br />';
$s = $modx->getDocument($src);
$t = $modx->getDocument($trg);
$sql = "SELECT MAX(id) FROM ".$modx->getFullTableName("site_content");
$result = $modx->db->query($sql);
$row = $modx->fetchRow($result);
$maxID = $row['MAX(id)']+1;
echo 'next id: '.$maxID;
$sql = "UPDATE ".$modx->getFullTableName("site_content")." SET id = ".$maxID." WHERE id = ".$src;
$result = $modx->db->query($sql);
print_r ($result);
$sql = "UPDATE ".$modx->getFullTableName("site_content")." SET id = ".$src." WHERE id = ".$trg;
$result = $modx->db->query($sql);
$sql = "UPDATE ".$modx->getFullTableName("site_content")." SET id = ".$trg." WHERE id = ".$maxID;
$result = $modx->db->query($sql);
break;
default: // display module page
echo '<html>';
echo '<head></head>';
echo '<body>';
echo '<script language="JavaScript" type="text/javascript">';
echo ' function postForm(opcode){';
echo ' document.module.opcode.value=opcode;';
echo ' document.module.submit();';
echo ' }';
echo '</script>';
echo '<form name="module" method="post">';
echo '<input name="opcode" type="hidden" value="" />';
echo '<h1>Replace Page</h1>';
echo '<h3>replace Pages by switching their IDs</h3>';
echo 'Source: <input title="Source" name="srcpg" type="text" size="10" value="" />';
echo 'Target: <input title="Target" name="trgpg" type="text" size="10" value="" />';
echo '<hr />';
echo '<a href="javascript:;" onclick="postForm(\'verify\');return
false;">Replace</a>';
echo '<a href="javascript:;" onclick="postForm(\'\');return
false;"> </a>';
echo '</form>';
echo '</body>';
echo '</html>';
break;
}
To answer some of your questions:$src = isset($_POST['srcpg']) ? $_POST['srcpg'] : false; $trg = isset($_POST['trgpg']) ? $_POST['trgpg'] : false;
if ($srcpg && $trgpg) {
$s = $modx->getDocument($src);
$t = $modx->getDocument($trg);
... rest of code ...
}echo '<form name="module" method="post">'; echo '<input name="opcode" type="hidden" value="" />'; echo '<a href="javascript:;" onclick="postForm(\'firstpage\');return false;">Back</a>'; echo '</form>';
<a href="javascript:;" onclick="parent.menu.updateTree();">Refresh Tree</a>
<body onload="parent.menu.updateTree();">


// sychronise the cache
$basePath = $modx->getConfig('base_path');
include_once $basePath."manager/processors/cache_sync.class.processor.php";
$sync = new synccache();
$sync->setCachepath($basePath."assets/cache/");
$sync->setReport(false);
$sync->emptyCache();
$s = $modx->getDocument($src);

)
but that’s most likely why this function is recommended for "internal use only"...// ###########################################
// ReplacePage #
// ###########################################
// Replaces a page (target) with another one (source) by switching their IDs and keeps the published state of the target.
// Useful if you have made copies of published pages for further editing
// and want to publish the edited versions without changing the id,
// since a different id would make it necessary to edit all TV values using this id
// like @document bindings.
// This module does not respect user access-rights (not my fault, really!)
// so be careful providing executing rights for this module!
// For questions or comments look for "ppaul" in the modx-forums
// feel free to use and improve this module
// according to your needs!
$opcode = isset($_POST['opcode']) ? $_POST['opcode']:'';
$src = isset($_POST['srcpg']) ? $_POST['srcpg'] : false;
$trg = isset($_POST['trgpg']) ? $_POST['trgpg'] : false;
$tb_prefix = $modx->db->config['table_prefix'];
$theme = $modx->db->select('setting_value', $tb_prefix . 'system_settings', 'setting_name=\'manager_theme\'', '');
$theme = $modx->db->getRow($theme);
$theme = ($theme['setting_value'] <> '') ? $theme['setting_value'] : 'MODx';
$error = '';
// action directive
switch($opcode)
{
case 'replace':
// verify page existence
$errmsg = false;
$succmsg = false;
$sql = 'SELECT id, pagetitle, published FROM '.$modx->getFullTableName("site_content").' WHERE id = '.$src;
$result = $modx->db->query($sql);
$srow = $modx->fetchRow($result);
if ($srow['id'] == '')
{$errmsg = 'page '.$src.' does not exist!';}
else
{$succmsg = $srow['pagetitle'].' was replaced by ';}
$sql = 'SELECT id, pagetitle, published FROM '.$modx->getFullTableName("site_content").' WHERE id = '.$trg;
$result = $modx->db->query($sql);
$trow = $modx->fetchRow($result);
if ($trow['id'] == '')
{$errmsg .= '<br />page '.$trg.' does not exist!';}
else
{$succmsg .= '<br />'.$trow['pagetitle'];}
echo '<html>';
echo '<head>';
echo '<link rel="stylesheet" type="text/css" href="media/style/' . $theme . '/style.css" />';
echo '<link rel="stylesheet" type="text/css" href="media/style/' . $theme . '/coolButtons2.css" />';
echo '</head>';
echo '<body style="margin:30px" onload="parent.menu.updateTree()";>';
if (!$errmsg)
{
$sql = "SELECT MAX(id) FROM ".$modx->getFullTableName("site_content");
$result = $modx->db->query($sql);
$row = $modx->fetchRow($result);
$maxID = $row['MAX(id)']+1;
// exchange the ids
$sql = "UPDATE ".$modx->getFullTableName("site_content")." SET id = ".$maxID." WHERE id = ".$src;
$result = $modx->db->query($sql);
$sql = "UPDATE ".$modx->getFullTableName("site_content")." SET id = ".$src." WHERE id = ".$trg;
$result = $modx->db->query($sql);
$sql = "UPDATE modx_site_content SET id = ".$trg." WHERE id = ".$maxID;
$result = $modx->db->query($sql);
// exchange the published state too
$sql = "UPDATE ".$modx->getFullTableName("site_content")." SET published = ".$srow['published']." WHERE id = ".$src;
$result = $modx->db->query($sql);
$sql = "UPDATE ".$modx->getFullTableName("site_content")." SET published = ".$trow['published']." WHERE id = ".$trg;
$result = $modx->db->query($sql);
echo $succmsg;
}
else
{echo $errmsg;}
echo '<br /><form name="back" method="post"><input type="submit" name="back" value="Back" /></form>';
echo '</body>';
echo '</html>';
break;
case 'verify':
$sql = 'SELECT id, pagetitle FROM '.$modx->getFullTableName("site_content").' WHERE id = '.$src;
$result = $modx->db->query($sql);
$row = $modx->fetchRow($result);
if ($row['id'] == '')
{echo 'page '.$src.' does not exist!';}
else
{echo $row['pagetitle'];}
$sql = 'SELECT id, pagetitle FROM '.$modx->getFullTableName("site_content").' WHERE id = '.$trg;
$result = $modx->db->query($sql);
$row = $modx->fetchRow($result);
if ($row['id'] == '')
{echo 'page '.$trg.' does not exist!';}
else
{echo $row['pagetitle'];}
break;
default: // display module page
echo '<html>';
echo '<head>';
echo '<link rel="stylesheet" type="text/css" href="media/style/' . $theme . '/style.css" />';
echo '<link rel="stylesheet" type="text/css" href="media/style/' . $theme . '/coolButtons2.css" />';
echo '<script language="JavaScript" type="text/javascript">';
echo ' function postForm(opcode){';
echo ' document.module.opcode.value=opcode;';
echo ' document.module.submit();';
echo ' }';
echo '</script>';
echo '</head>';
echo '<body style="margin:80px">';
echo '<form name="module" method="post">';
echo '<input name="opcode" type="hidden" value="" />';
echo '<h1>Replace Page</h1>';
echo '<h3>replace Pages by switching their IDs</h3>';
echo 'Source ID: <input title="Source" name="srcpg" type="text" size="10" value="" />';
echo 'Target ID: <input title="Target" name="trgpg" type="text" size="10" value="" />';
echo '</form><br /> (hint: source is usually unpublished, target is usually published)';
echo '<hr />';
echo '<a href="javascript:;" onclick="postForm(\'replace\');return
false;"><input type="submit" name="replace" value="Replace" onclick="postform(\'replace\');return false;" /></a>';
echo '</form>';
echo '</body>';
echo '</html>';
break;
}