A customer asked me to come up with a “document link database”.
They have several thousand documents distributed across various nationally based sites and the idea is that each document will have a SKU and no matter what happens, the href links will always be correct. Or anyway, as correct as humanly possible.
So I came up with a table that contains the SKU, the link and where it should take you. This is still SS3, btw.
class RelocatorLink extends DataObject{
private static $db = array(
“DocumentSKU” => “Varchar(200)”,
“Link” => “Varchar(200)”,
“RelocatesTo” => “Varchar(200)”
);
private static $summary_fields = array(
"DocumentSKU", "Link", "RelocatesTo"
);
}
the pages will have a script that goes like
jQuery.noConflict();
(function($) {
$("a").click(function(e){
e.preventDefault();
const $href = $(this).attr("href");
var params = "link=" + $(this).attr("href");
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if(this.responseText != "") {
window.location.href = this.responseText;
}
else{
window.location.href= $href;
}
}
};
xhttp.open("POST", home/findAndReturnLink" + "?" + params, true);
xhttp.send();
});
}(jQuery));
and a function that goes like
function findAndReturnLink(){
$link = $_REQUEST["link"];
$res = RelocatorLink::get()->filter(array("Link" => $link))->first();
if($res) {
return $res->RelocatesTo;
}
else{
return false;
}
}
So basically, you click on a link, the script calls a function that searches the table, if it finds the record, it returns the redirect, otherwise the link works as normal.
File security is handled by the remote site so the visitor may be taken to some national site where they may have to log in to access the documents.
I know there are things to be added, such as making the sure the SKU is unique and some sort of message in case Javascript is blocked on the visitor’s browser (the site runs Bootstrap so it should have Javascript enabled and JQuery loaded.
But I can’t help but feel uneasy. As a solution, it seems so simple that I am almost certain it must be risky or dangerous in some way.
I’m not asking Silverstripe to certify this or do my work for me but is there something obviously wrong with this solution?
Thanks for reading this far.