Replacement Model

by Roydon DSouza

JavaScript

var msg = "";
var pnTemplate = {
type: "Booking",
title: "Booking Notification",
template_body: "type:<<tet>>,ttt:rtre"

};
var replacement = [];
replacement[0]= "{ 'test' : 'test' }";
var showNotification = true;

var c = createPnBody('messaage in body',pnTemplate, replacement, showNotification );
console.log(c);



function createPnBody (msg, pnTemplate,replacement, isShow)  {
		console.log(pnTemplate);
		//convert & save
		var json = {};
//        json['type'] = pnTemplate.type;
//        var alert = {};
//        alert['title'] = pnTemplate.title;
//        alert['body'] = msg;
//        json['alert'] = alert;

        // $body = $pnTemplate->getTemplateBody();
        // $body = $this->replaceVariables($replacement, $body); 
        var body = pnTemplate.template_body;
        body = replaceVariables(replacement, body);
     
        //convert text to array
        var arr = body.split(',');
        var param = {};
				var tmp = null;
        for (let v of arr) {
            tmp = v.split(':');
            param[tmp[0]] = tmp[1]+''; //converting everything to string
        }
        //json['parameters'] = param;
        json = param;

//        if (isShow === true) {
//            json['isShowNotification'] = true;
//        } else {
//            json['isShowNotification'] = false;
//        }

        return JSON.stringify(json);
		
	}
  
  function replaceVariables  (replacement, text)  {
  console.log('text', text);
		var variables = [];
	    //text no null
	    variables = text.match(/<<\w+:\d{1,3}>>|<<\w+>>/g);
	    console.log('placeholders ', variables);
	    var variablesFound = variables ? variables.length : 0;
	    console.log('variables', variables);
	    console.log('here');
	    if (variablesFound > 0) {
	    	console.log('length',variablesFound);
	        //$toReplace = array();
	        for (var key in variables) {
	            console.log('---');
	            var len = 0;
	            var variable = variables[key];
	           ...