Ajax Redirect do not encode segment string after hash


#1

Hi.

I’m developing a plugin that use segmentation as a filter.

This plugin make ajax calls to updatae some data. When redirectOnSucess is called, the portion of the segment is decoded, and the segment is not recognized after redirection (Custom Segment is shown in Segment list).

Example, if URL is


http://127.0.0.1/piwik/index.php?module=CoreHome&action=index&idSite=1&period=day&date=yesterday&updated=46&segment=pageUrl%3D%3Dhttp%253A%252F%252F127.0.0.1%252Fsample_site%252Fsample-two%252F#module=pluginName&action=index&idSite=1&period=day&date=yesterday&a=1&segment=pageUrl%3D%3Dhttp%253A%252F%252F127.0.0.1%252Fsample_site%252Fsample-two%252F

Redirection is set to this URL


http://127.0.0.1/piwik/index.php?module=CoreHome&action=index&idSite=1&period=day&date=yesterday&updated=47&segment=pageUrl%3D%3Dhttp%253A%252F%252F127.0.0.1%252Fsample_site%252Fsample-two%252F#module=pluginName&action=index&idSite=1&period=day&date=yesterday&a=1&segment=pageUrl==http%3A%2F%2F127.0.0.1%2Fsample_site%2Fsample-two%2F

I changed code in ajaxHelper.js to handle this.

Please let me know if this is a bug and if the changes are right.

This is the protion of code I changed:


this.redirectOnSuccess = function (params) {
        this.setCallback(function(response) {
            // add updated=X to the URL so that a "Your changes have been saved" message is displayed
            if (typeof params == 'object') {
                params = piwikHelper.getQueryStringFromParameters(params);
            }
            var urlToRedirect = piwikHelper.getCurrentQueryStringWithParametersModified(params);
            var updatedUrl = new RegExp('&updated=([0-9]+)');
            var updatedCounter = updatedUrl.exec(urlToRedirect);
            if (!updatedCounter) {
                urlToRedirect += '&updated=1';
            } else {
                updatedCounter = 1 + parseInt(updatedCounter[1]);
                urlToRedirect = urlToRedirect.replace(new RegExp('(&updated=[0-9]+)'), '&updated=' + updatedCounter);
            }
            var currentHashStr = window.location.hash;
            if(currentHashStr.length > 0) {
            	// ADDED BY JULIO MARCANO
            	// find segment string
            	segmentStart = currentHashStr.search("segment=");
            	if (segmentStart > 0) {
            		segmentEnd = currentHashStr.indexOf("&",segmentStart);
            		if (segmentEnd < 0) segmentEnd = currentHashStr.length;  
                	segmentText = encodeURIComponent(currentHashStr.substring(segmentStart + 8, segmentEnd));
                	var currentHashStrTemp = currentHashStr.substring(0,segmentStart) +
                	                 "segment=" + segmentText;
                	if (segmentEnd < currentHashStr.length) {
                		currentHashStrTemp += currentHashStr.substring(segmentEnd);
                	}
                	currentHashStr = currentHashStrTemp; 
            	}
            	// ADDED /
            	
                urlToRedirect += currentHashStr;
            }
            
            piwikHelper.redirectToUrl(urlToRedirect);
        });
    };

Thanks
Julio