MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/public_html/imenso.co/timesheet/js/team.js
// THIS FILE REQURES THAT THE LIB FILE IS LOADED FIRST


if (!_.isObject(app)) {
  alert("System Library failed to load [SYSLD01]. Pleasr report this error to support@nthriveeducation.com");
  //the app object is defined in lib.js
}

if(app.dev){
	app.log("DEV MODE ON");
}

app.files = [{
	name:"teamtable",
	file:'./../../inc/team/table.inc.html'
	},
	{
	name:'teamrow',
	file:'./../../inc/team/row.inc.html'
	},
	{
	name:'teamsearch',
	file:'./../../inc/team/search.inc.html'
	},
	{
	name:'editor',
	file:'./../../inc/team/editor.inc.html'
	},
  {
  name:'pagination',
  file:'./../../inc/common/pagination.inc.html'
  },
  {
  name:'navbar',
  file:'./../../inc/layouts/navbar.inc.html'
  }
];

app.filter={
        search_title:"",
        page:1,
        sorting:"firstname",
        sortingby:"asc"
      };

$(document).ready(function(){
	app.setListeners();
	app.init(function(){
		app.startApp();
	});
});


app.setListeners = function() {
	app.setLibListeners();
	
	
  	$(document).on('click', '#updateuserbutton', function(e) {
		app.handleUser();
  	});
    $(document).on('click', '#searchuserbutton', function(e) {
      app.filter.search_title=$('#search_title').val();
      app.getUser();
    });

    $(document).on('click touch', '#addteambutton', function(e) {
        e.preventDefault();
        var id = 0;
        app.editUser(id);
   });

  	$(document).on('click', '#teameditlink', function(e) {
  		e.preventDefault();
  		var id = $(this).attr("data-id");
      app.editUser(id);
  	});

  	$(document).on('click', '#teamdeletelink', function(e) {
		  e.preventDefault();
		  app.user.id=$(this).attr("data-id");
     	app.deleteUser();
  	});

   $(document).on('click touch', '.paginationlink a', function(e) {
      app.filter.page=$(this).attr("data-page");
      app.getUser();
      $('.paginationlink').removeClass('active');
      $(this).closest('.paginationlink').addClass('active');

  });
  	
}


app.launchUser = function() {

	app.log("Launching with user ",app.user);

	$.ajax({
        method: "POST",
        url: app.location+"api/imensoapi.php",
        dataType: "json",
        data: {
          action: "launchload",
          user: app.user
        }
      })
      .fail(function() {
        alert("There was an error  connecting to the server SR[01]")
      })
      .done(function(resp) {
        if (resp.resp == 'OK') {
          if (resp.error) {
            alert("Transaction error. " + resp.error);
          } else {
			       app.log("User launched");
             app.user = Cookies.getJSON('usertoken');
          
            app.launcheScreen();
			     
          }
        } else {
          alert("There was an error  connecting to the server SR[02]")
        }
      }); //- ajax

} //- launchUser

app.launcheScreen=function(){
  
$("body").prepend(app.templates.navbar({
    active:"team",
    type:app.user.acces_level
  }));
  app.getUser();
}

app.startApp = function(){		
	app.log("Starting");
	app.user = Cookies.getJSON('usertoken');
	if (app.user && app.user.token && app.user.token.length > 20) {
	
	app.launchUser();
	} else {

	window.location.href=app.location;
	}
 
}

app.getUser = function(){
  app.userForm={
  search_title:app.filter.search_title,
  page:app.filter.page,
  sorting:app.filter.sorting,
  sortingby:app.filter.sortingby
};
	$.ajax({
		method: "POST",
		url: app.location+"api/admin.php",
		dataType: "json",
		data:{
			action: "getuser",
			user: app.userForm,
			token:app.user.token
		}
	}).fail(function(){
		alert("There was an error  connecting to the server SR[01]");
	}).done(function(resp){
		if(resp.resp == "OK")
		{
			app.resp = resp;
			app.showUserList();
		}
		else{
			alert("There was an error  connecting to the server SR[02]")
		}
	});
}


app.showUserList = function(){
	var rows = [];
	_.each(app.resp.users, function(user){
		rows.push(app.templates.teamrow(user));
	});
  if($('#teamsearch').length==0)
  {
   $("#maincontainer").html(app.templates.teamsearch());
  }
	$("#result").html(app.templates.teamtable({
		rows: rows.join("")
	}));	
   $('#pagination').html(app.templates.pagination({
      pagination: app.resp.paginationresult
    }));
}


app.editUser = function(editId) {
  app.log("Got click: ", editId);
    var edit = {};
    var heading="Add user";
    if (_.isNumber(parseInt(editId)) && parseInt(editId) > 0) {
        edit = _.findWhere(app.resp.users, {
            id: editId
        });
        heading="Edit user";
    } else {
        edit = {
            id: "",
            firstname: "",
            lastname: "",
            email: "",
            usertype:"",
            token:""
        };
    }
    edit.usertoken=app.user.token;
    edit.heading=heading;

    app.log("Got click: ", edit);
     $('#modalContainer').html(app.templates.modal({
      'title': edit.heading,
      'content': app.templates.editor(edit)
    }));

    $('#modal').modal('show');
   

} //- ef




app.handleUser = function(){
	app.addUser();
	var errors = app.validateUser();
	if(errors.length>0)
	{

		app.alertMessage(errors.join("<br \>"),"warning");
		return;
	}

	app.saveUser();
	app.showUserList(app.resp.users);

}

app.addUser = function() {
  var checked = $('#usertype_user').is(":checked");
  var editId = $('#id').val().trim();
   app.userForm={
    id:editId,
    fname:$('#fname').val().trim(),
    lname:$('#lname').val().trim(),
    email:$('#email').val().trim(),
    usertype:( checked==true ? $('#usertype_user').val().trim():$('#usertype_admin').val().trim()),
    password:""
  }
   if (editId=="")
  {
      app.userForm.password=$('#password').val().trim();
  }

}

app.validateUser=function(){
	
  var error=[];
   if(app.userForm.fname=="")
  {
      error.push("Please enter first name");
  }
  if(app.userForm.lname=="")
  {
      error.push("Please enter last name");
  }
  if(app.userForm.email=="")
  {
      error.push("Please enter e-mail address");
  }
  else if(!app.validateEmail(app.userForm.email))
  {
      error.push("Invalid e-mail address");
  }
  if (app.userForm.id=="")
  {
    if(app.userForm.password=="")
    {
      error.push("Please enter password");
    }
    if(app.userForm.password!="" && app.userForm.password.length<6)
    {
      error.push("Password must be at least 6 digits!");
    }  
  }
  if(app.userForm.usertype=="")
  {
      error.push("Please select a user type");

  }

  return error;
}


app.saveUser=function(){
$('#updateuserbutton').prop("disabled",true);
$('#updateuserbutton').html("Saving....");

  $.ajax({
      method: "POST",
      url: app.location+"api/admin.php",
      dataType: "json",
      data: {
        action: "saveuser",
        user:app.userForm,
        token:app.user.token
      }
    })
    .fail(function() {
      alert("There was an error  connecting to the server SR[01]")
    })
    .done(function(resp) {
      if(resp.msg.length>0 && resp.msg=="timeout")
      {
         app.userLogout(); 
      }
      else if (resp.resp == 'OK') {

          if(resp.msg.length) {
            app.alertMessage("Failed User Save. " + resp.msg,"warning");

            $('#updateuserbutton').prop("disabled",false);
            $('#updateuserbutton').html("save");
        } else {
            $("#modal").modal('hide');
            app.getUser();
           }
       }
        else {
        alert("There was an error  connecting to the server SR[02]");
      }
    }); //- ajax

}//-ef getUser



app.deleteUser=function(){
/*
	app.log("User delete: ", app.user.token);
  if (app.user.acces_level=="admin") {
      app.alertMessage("<strong>Danger!</strong> permission denied.","danger");
   return;
  }

*/
   if (!confirm("Please confirm if you want to delete this record?")) {
      return;
    }


  $.ajax({
      method: "POST",
      url: app.location+"api/admin.php",
      dataType: "json",
      data: {
        action: "deleteuser",
        user:app.user,
        token:app.user.token
      }
    })
    .fail(function() {
      alert("There was an error  connecting to the server SR[01]")
    })
    .done(function(resp) {
      if(resp.msg.length>0 && resp.msg=="timeout")
      {
         app.userLogout(); 
      }
      else if (resp.resp == 'OK') {

        if(resp.msg.length){
          app.alertMessage("<strong>Failed:</strong> " + resp.msg,"danger");
        
        } else {
            
            app.getUser();
           }
       }
        else {
        alert("There was an error  connecting to the server SR[02]")
      }
    }); //- ajax

}//-ef deleteUser



app.loadTemplates = function() {
	
	app.templates.modal = _.template("<div class='modal fade' tabindex='-1' role='dialog' id='modal'><div class='modal-dialog' role='document'><div class='modal-content'><div class='modal-header'><h5 class='modal-title'><%=title%></h5><button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button></div><div class='modal-body'><%=content%></div><div style='display:none' class='modal-footer'><button type='button'  class='btn btn-default' data-dismiss='modal'>Close</button></div></div></div></div>");

}
;