Here’s an updated version of the file fck_cmscontent.php which generates completely dynamic URLS. So if you change from/to SEF or change your site’s location the links remain working by using the [~id~] tag.
I think this feature could be easily integrated into next versions of ModX
fck_cmscontent.php
<?php
require_once('../../../../../../modxapi.php'); // <---- Change the path for your server
// *********************************************************************************
//
// Modded By: Aaron Wardle ([email protected])
// Date: 4th August 2006
// Website: http://www.spcs.eu
// Version: 0.2
//
// Info: CMSContent allows you to add internal modx links into your documents,
// simply click the toolbar button and a dialog will popup showing a treeview of your
// site documents.
//
// Currently checks if FURLS are enabled, and adds the preifx / suffix as defined in modx settings
// If FURLS are disabled it will add links like index.php?id=1
//
// Credits: CMSContent was Originally created by woudloper for CMS Made Simple.
// Geir Landr - For the great javascript tree http://www.destroydrop.com/javascript/tree/
//
// Fixes: 1. Added rb_base_url to start of the link as some users don't have modx installed off the root of thier site
// 2. Removed the path setting at the top so just change your path in the include line.
// 3. Removed the unused include.php while code.
//
// *********************************************************************************
// **** There should be no need to edit anything else under this section. *******
// The following includes the modxapi library
$modx = new MODxAPI();
$html = $modx->executeDocument(3); // Used to start off the api.
$furls = $modx->config["friendly_urls"]; // Stores the FURL Settings for the site
$siteurl = str_replace("assets/","", $modx->config["rb_base_url"]);
// If the site uses FURL we set up our vars so we don't have to keep calling the DB
if ($furls == 1) {
$furl_prefix = $modx->config["friendly_url_prefix"];
$furl_suffix = $modx->config["friendly_url_suffix"];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>CMS Link - Insert CMS Made Simple Link</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta content="noindex, nofollow" name="robots">
<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js"></script>
<script type="text/javascript" src="fckcmscontent.js"></script>
<script type = "text/javascript">
function addInfo(myString)
{
var listen1 = document.getElementById("PageURL");
var listen = document.getElementById("txtSelection");
listen.value = myString;
listen1.value = myString;
}
</script>
<script type="text/javascript">
<!--
var oEditor = window.parent.InnerDialogLoaded();
var FCK = oEditor.FCK;
var FCKLang = oEditor.FCKLang ;
var FCKConfig = oEditor.FCKConfig ;
var FCKCMSContent = oEditor.FCKCMSContent;
// oLink: The actual selected link in the editor.
var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ;
if ( oLink )
FCK.Selection.SelectNode( oLink ) ;
window.onload = function () {
// First of all, translates the dialog box texts.
oEditor.FCKLanguageManager.TranslatePage(document);
LoadSelected(); //See function below
window.parent.SetOkButton( true ); //Show the "Ok" button.
}
//If an anchor (A) object is currently selected, load the properties into the dialog
function LoadSelected() {
var sSelected;
if ( oEditor.FCKBrowserInfo.IsGecko ) {
sSelected = FCK.EditorWindow.getSelection();
} else {
sSelected = FCK.EditorDocument.selection.createRange().text;
}
if ( sSelected == "" ) {
//alert( 'Please select a text in order to create a (internal) link' );
} else {
var listen = document.getElementById("txtTitle");
listen.value = sSelected;
}
}
//Code that runs after the OK button is clicked
function Ok() {
//Validate is option is selected
var oDoc = document.getElementById( 'PageURL' );
if(oDoc.value == "") {
alert('Please select a page in order to create a link');
return false;
}
var oTagLink = document.getElementById( 'chkTaglink' );
var oLinkTitle = document.getElementById( 'txtTitle' );
if (oLinkTitle.value == "") {
alert('Please enter a Link Title');
return false;
}
if (oTagLink.checked) {
//Create special CMS tag
var sSelected;
var sURL = document.getElementById( 'PageURL' ) ;
var sTagOutput = '<a href="' + sURL.value + '">' + document.getElementById( "txtTitle").value + '</a>';
oEditor.FCK.InsertHtml( sTagOutput );
} else {
var sURL = document.getElementById( 'PageURL' ) ;
oLink = oEditor.FCK.CreateLink( sURL.value ) ;
}
return true;
}
//-->
</script>
</head>
<body scroll="yes" style="overflow:hidden;">
<input type="hidden" id="PageURL" value="" />
<table height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td>
<table width="100%">
<tr>
<td colspan="2"><span fckLang="DlgCMSCommentPageSelection">Select a CMS Page to link to:</span> </td>
</tr>
<tr>
<td colspan="2">
<div class="dtree">
<p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p>
<script type="text/javascript">
<!--
d = new dTree('d');
d.add(0,-1,'Modx Pages');
<?php
// Here we execute a query to get all the docs in modx,
// At the moment it includes hidden docs etc.
$sql = "SELECT * FROM ".$modx->getFullTableName("site_content") ." ORDER BY isfolder DESC ";
$docsrs = $modx->dbQuery($sql); # Execute the Query
$docslimit = $modx->recordCount($docsrs); # Number of pages found
// I decided to add an IF Clause before the for loop, I fugured if I keep doing if in the loop it would slow things down.
// Below we cater for FURL's and none FURL's
if ($furls == 1) {
// This will output FURL links with prefix and suffix.
for($i = 0; $i < $docslimit; $i++) {
$docs = $modx->fetchRow($docsrs);
echo "d.add(" .$docs['id'] ."," .$docs['parent'] .",'" .$docs['pagetitle'] ." <font color=blue>(" .$docs['id'] .")</font>','javascript:addInfo(\'[~" .$docs['id'] ."~]\');');\n";
}
} else {
// This will output std links i.e http://www.spcs.eu/index.php?id=13
for($i = 0; $i < $docslimit; $i++) {
$docs = $modx->fetchRow($docsrs);
echo "d.add(" .$docs['id'] ."," .$docs['parent'] .",'" .$docs['pagetitle'] ." <font color=blue>(" .$docs['id'] .")</font>','javascript:addInfo(\'[~" .$docs['id'] ."~]\');');\n";
}
}
?>
d.config.folderLinks = false;
d.config.useCookies = false;
document.write(d);
//-->
</script>
</div>
</td>
</tr>
<tr>
<td nowrap><span fckLang="DlgCMSCommentTitle">Link Title</span> </td>
<td width="100%" style="align:right;"><input id="txtTitle" style="WIDTH: 98%" type="text" name="txtTitle"></td>
</tr>
<tr>
<td nowrap><span fckLang="DlgCMSCommentTitle">Selection</span> </td>
<td width="100%" style="align:right;"><input id="txtSelection" disabled="true" style="WIDTH: 98%" type="text" name="txtSelection"></td>
</tr>
<tr>
<td nowrap><span fckLang="DlgCMSCommentTagLink">Generate Link Title?</span> </td>
<td><input id="chkTaglink" type="checkbox" name="chkTaglink" /></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
BTW: The path mentioned in the top can be maintained if you place your modx api in the main modx folder next to the .htaccess and the index.php file.