Skip to content

Instantly share code, notes, and snippets.

@sebduggan
Last active March 21, 2016 15:29
Show Gist options
  • Save sebduggan/9a17754b942c423ac26c to your computer and use it in GitHub Desktop.
Save sebduggan/9a17754b942c423ac26c to your computer and use it in GitHub Desktop.
SES URLs in Mura FW/1 plugins
<displayobjects location="global">
<displayobject
name="Statistics"
component="includes.displayObjects"
displaymethod="dspStatistics"
persist="false">
<route
pattern="batting"
action="statistics:main.batting" />
<route
pattern="batting/{'[0-9]{4}'}"
action="statistics:main.batting" />
<route
pattern="player"
action="statistics:main.playerredirect" />
<route
pattern="player/{member.filename}"
action="statistics:main.player" />
<route
pattern="player/{member.filename}/detail"
action="statistics:main.playerdetail" />
</displayobject>
</displayobjects>
public any function dspStatistics($) {
var action = $.getEvent().getValue("sesAction", "statistics:main.default");
return getApplication().doAction(action);
}
public any function onSite404(required $, required event) {
var qMatches = getSesUrlMatches(arguments.event);
if (qMatches.recordcount) {
var sesUrlParts = listtoarray(replace(event.getValue("currentfilenameadjusted"), qMatches.filename, ""), "/");
if (validateSesUrlParams($, event, qMatches.displaymethod, sesUrlParts)) {
local.contentBean = application.contentManager.getActiveContentByFilename(qMatches.filename, event.getValue('siteid'));
event.setValue('contentBean', local.contentBean);
}
}
}
<cfscript>
private function getSesRoutes() {
var objectsLen = 0;
var displayObject = "";
var result = {};
var route = {};
var routes = [];
var pluginXML = variables.pluginConfig.getPluginManager().getPluginXML(variables.pluginConfig.getModuleId());
if ( structKeyExists(pluginXML.plugin.displayobjects,"displayobject") ) {
objectsLen = arrayLen(pluginXML.plugin.displayobjects.displayobject);
if (objectsLen) {
for (var i=1; i<=objectsLen; i++) {
displayObject = pluginXML.plugin.displayobjects.displayobject[i];
if (structKeyExists(displayObject,"route")) {
routes = [];
for (var r=1; r<=arraylen(displayObject.route); r++) {
route = {};
route["pattern"] = displayObject.route[r].xmlAttributes.pattern;
route["action"] = displayObject.route[r].xmlAttributes.action;
arrayAppend(routes, route);
}
result[displayObject.xmlAttributes.displaymethod] = routes;
}
}
}
}
return result;
}
private function getSesUrlMatches(required event) {
var qs = getQueryService();
var sql = "";
var dbtype = application.configBean.getDBType();
var lengthFunction = "";
if (dbtype == "mssql") {
lengthFunction = "datalength";
} else if (listfindnocase("mysql,oracle", dbtype)) {
lengthFunction = "length";
} else if (dbtype == "postgresql") {
lengthFunction = "char_length";
} else if (dbtype == "nuodb") {
lengthFunction = "character_length";
}
sql = "SELECT
tcontent.filename,
tplugindisplayobjects.displaymethod
FROM
tplugindisplayobjects
INNER JOIN
tcontentobjects
ON
tplugindisplayobjects.objectid = tcontentobjects.objectid
INNER JOIN
tcontent
ON
tcontentobjects.contenthistid = tcontent.contenthistid
AND
tcontent.active = 1
WHERE
tplugindisplayobjects.moduleid = :moduleid
AND
tplugindisplayobjects.displaymethod IN (:displaymethods)
AND
tcontent.siteid = :siteid
AND
left(:fullurl, #lengthfunction#(tcontent.filename)) = tcontent.filename
ORDER BY
#lengthfunction#(tcontent.filename) DESC";
qs.addParam(name="moduleid", value=variables.pluginConfig.getModuleId(), cfsqltype="cf_sql_varchar");
qs.addParam(name="siteid", value=arguments.event.getValue("siteid"), cfsqltype="cf_sql_varchar");
qs.addParam(name="displaymethods", value=structkeylist(variables.pluginConfig.getApplication().getValue("sesRoutes")), list=true, cfsqltype="cf_sql_varchar");
qs.addParam(name="fullurl", value=arguments.event.getValue("currentfilenameadjusted"), cfsqltype="cf_sql_varchar");
return qs.execute(sql=sql).getResult();
}
private function validateSesUrlParams(required $, required event, required displayMethod, required sesUrlParts) {
var routes = variables.pluginConfig.getApplication().getValue("sesRoutes");
var sesUrlParams = [];
if (not structkeyexists(routes, arguments.displayMethod)) {
return false;
}
for (var route in routes[arguments.displayMethod]) {
var routeParts = listToArray(route.pattern, "/");
if (arrayLen(routeParts) != arrayLen(arguments.sesUrlParts)) {
continue;
}
var match = true;
for (var i=1; i<=arrayLen(routeParts); i++) {
if (reFind("^{[""''].+[""'']}$", routeParts[i])) {
match = validateSesUrlPartRegex($, routeParts[i], arguments.sesUrlParts[i]);
arrayAppend(sesUrlParams, arguments.sesUrlParts[i]);
} else if (reFind("^{.+}$", routeParts[i])) {
match = validateSesUrlPartBean($, routeParts[i], arguments.sesUrlParts[i]);
arrayAppend(sesUrlParams, arguments.sesUrlParts[i]);
} else if (routeParts[i] != arguments.sesUrlParts[i]) {
match = false;
break;
}
}
if (!match) {
continue;
}
arguments.event.setValue("sesUrlParams", sesUrlParams);
arguments.event.setValue("sesAction", route.action);
return true;
}
return false;
}
private function validateSesUrlPartRegex(required $, required pattern, required urlPart) {
arguments.pattern = reReplace(arguments.pattern, "^{[""''](.+)[""'']}$", "\1");
return refindNoCase("^" & arguments.pattern & "$", arguments.urlPart);
}
private function validateSesUrlPartBean(required $, required pattern, required urlPart) {
arguments.pattern = reReplace(arguments.pattern, "^{(.+)}$", "\1");
var entity = listFirst(arguments.pattern, ".");
var column = listLast(arguments.pattern, ".");
var checkBean = arguments.$.getBean(entity).loadBy("#column#" = arguments.urlPart);
return !checkBean.getIsNew();
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment