Skip to content

Instantly share code, notes, and snippets.

@AlexMcowkin
Last active February 5, 2020 09:13
Show Gist options
  • Save AlexMcowkin/83d00c5ce075ab099692519ceb8086ca to your computer and use it in GitHub Desktop.
Save AlexMcowkin/83d00c5ce075ab099692519ceb8086ca to your computer and use it in GitHub Desktop.
create seo url from serialize() form
newUrl = generateUserFriendlyUrl($('#filter_form').serialize(), d.start, d.length, d.order[0]['column'], d.order[0]['dir']);
window.history.pushState({id: 'my_report'}, null, "/reports/some_report/" + newUrl);
/**
* generate userfriendly url
*
* @param serialisedFormData
* @param currentPage
* @param pageOffset
* @param sortColumn
* @param sortOrder
* @returns {string}
*/
function generateUserFriendlyUrl(serialisedFormData, currentPage, pageOffset, sortColumn, sortOrder)
{
var arrFormData = serialisedFormData.split("&");
var dictCombinedKeys = {};
var arrKeyValuePairs = [];
var hiddenKeys = ['request'];
var pagerData = '';
// combine values of same key into one value
for (var i = 0; i < arrFormData.length; i++) {
var arrParam = arrFormData[i].split("=");
var strKey = decodeURIComponent(arrParam[0]);
var strValue = arrParam[1];
// remove hidden form data
if (hiddenKeys.includes(strKey)) {
continue;
}
// remove 'filter_' from key
strKey = strKey.replace('filter_', '');
// remove '[]' from key
if (strKey.endsWith('[]')) {
strKey = strKey.substring(0, strKey.length - 2);
}
if (strKey !== "" && strValue !== "") {
if (typeof (dictCombinedKeys[strKey]) === "undefined") {
dictCombinedKeys[strKey] = strValue;
} else {
dictCombinedKeys[strKey] += "," + strValue;
}
}
}
// generate new userfriendly url
for (var key in dictCombinedKeys) {
if (dictCombinedKeys.hasOwnProperty(key)) {
arrKeyValuePairs.push(key + "/" + dictCombinedKeys[key]);
}
}
// generate order column data like: order/1,desc
sortData = 'order/' + sortColumn + ',' + sortOrder + '/';
// generate pager data like: page/1
if (currentPage) {
pagerData = 'page/' + (parseInt(currentPage / pageOffset) + 1) + '/';
}
return arrKeyValuePairs.join("/") + '/' + sortData + pagerData;
}
/**
* check if we load page using request method = GET
* and generate array with key=>value from string
*
* url example with all filters:
* start_date/02%2F04%2F2010/end_date/02%2F04%2F2020/phone/111111111111/account_id/mcowkin/account_name/alex-jr/campaign/313,510/pool/1042/status/success,fail/
*
* url example with sorting and pagination:
* start_date/02%2F04%2F2010/end_date/02%2F04%2F2020/order/5,asc/page/3/
*/
if (isset($_GET['userfriendlyurl'])) {
$urlKey = $urlVal = [];
$urlPieces = explode('/', $_GET['userfriendlyurl']);
foreach ($urlPieces as $urlPieceKey => $urlPieceVal) {
if ($urlPieceKey % 2 !== 0) {
$urlPieceVal = urldecode($urlPieceVal);
if (strpos($urlPieceVal, ',') !== false) {
$urlPieceVal = explode(',', $urlPieceVal);
}
$urlVal[] = $urlPieceVal;
} else {
$urlKey[] = $urlPieceVal;
}
}
$urlGetData = array_combine($urlKey, $urlVal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment