Newer
Older
ubFramework / Portal / docroot / assets / js / admin / voip / account_settings.js
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 6 KB Cleaning Up Making It A Sub Module
/**
 * Copyright (c) 2015, Christopher W. Olsen <cwolsen@uBixTechnologies.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by uBix Technologies.
 * 4. Neither the name of uBix Technologies nor the
 *    names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY uBix Technologies ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL uBix Technologies BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: account_settings.js 402 2016-03-01 01:09:43Z reddawg $
 *
 */

/* DataTables Variables */
var ADT;
var edit_account;
var edit_account_form;

/* VoIP Servers List */
var voipServers;

function dV(l) {
  return(confirm('Are you sure that you want to delete ' + l + '?'));
}

function editAccount() {
  _AJAX(
    "/admin/voip/account_settings_json.php",
    edit_account_form.serialize(),
    function (data) {
      alert(data.ret_string);
    }
  );
}

function editCloseAccount() {
  _AJAX(
    "/admin/voip/account_settings_json.php",
    edit_account_form.serialize(),
    function (data) {
      alert(data.ret_string);
      if (data.ret == 1)
        edit_account.dialog('close');
    }
  );
}

$(function() {
  // Initialize Account DataTable
  ADT = $('#actTable').dataTable( {
    "order": [[ 0, 'asc' ]],
    "columns": [
      { "width": "80" },
      null,
      { "width": "100" },
      { "width": "100" },
      { "width": "100" },
      { "width": "80" },
      { "width": "50" }
    ]
  });
  
  // Initialize Edit Account Modal
  edit_account = $( "#dialog-edit-account" ).dialog({
    autoOpen: false,
    height: 750,
    width: 750,
    modal: true,
    buttons: {
      "Save": editAccount,
      "Save & Close": editCloseAccount,
      Cancel: function() {
        edit_account.dialog( "close" );
      }
    },
    close: function() {
      edit_account_form[ 0 ].reset();
    }
  });

  edit_account_form = $("#edit_account_form" ).on("submit", function( event ) {
    event.preventDefault();
    // Do Something
  });
  
  ADT.delegate(".edit_account", "click", function() {
    edit_account.dialog("open");
    
    _AJAX(
      "/admin/voip/account_settings_json.php",
      "data[req]=account_info&data[account_number]=" + this.id,
      function(data) {
        $('#aN').val(data.account_number);
        $('#vC').val(data.voip_code);
        $('#accountNumber').text(data.account_number);
        $('#accountCode').text(data.account_code);
        $('#voipCode').text(data.voip_code);
        $('#sP').val(data.server_primary);
        $('#sB').val(data.server_backup);
        
        $.each(voipServers, function( index, item ) {
          if (data.server_primary == item.server_address)
            $("#primaryServer").append($("<option selected></option>").text(item.server_name).val(index));
          else
        	  $("#primaryServer").append($("<option></option>").text(item.server_name).val(index));
          
          if (data.server_backup == item.server_address)
            $("#backupServer").append($("<option selected></option>").text(item.server_name).val(index));
          else
            $("#backupServer").append($("<option></option>").text(item.server_name).val(index));
        });
        
        updateTransport($('#primaryServer').val(), 'primaryTransport', data.transport_primary);
        updateTransport($('#backupServer').val(), 'backupTransport', data.transport_backup);
        updatePort($('#primaryServer').val(), 'primaryPort', data.server_port_primary);
        updatePort($('#backupServer').val(), 'backupPort', data.server_port_backup);

        $('#extensionRange').val(data.ext_range);
        $('#dialplan').val(data.dialplan);
        $('#dialplanTimeout').val(data.dialplan_timeout);
      },
      function(data) {
        
      }
    );
  });
  
  $("#primaryServer").on("change", function() {
    var sI = $('#primaryServer').val();
    
    $('#sP').val(voipServer[sI].server_address);
    
    updateTransport(sI, 'primaryTransport', 'udp');
    updatePort($('#primaryServer').val(), 'primaryPort', '5060');
  });
  
  $("#backupServer").on("change", function() {
    var sI = $('#backupServer').val();
    
    $('#sB').val(voipServer[sI].server_address);
    
    updateTransport(sI, 'backupTransport', 'udp');
    updatePort($('#backupServer').val(), 'backupTransport', '5060');
  });
  
});

function updateTransport(sI, sF, sT) {
	$("#" + sF + " option").remove();
    
    $.each(voipServers[sI].server_transports.split(','), function(index, item) {
      if (sT == item)
        $('#' + sF).append($("<option selected></option>").text(item).val(item));
      else
        $('#' + sF).append($("<option></option>").text(item).val(item)); 	
    });
}

function updatePort(sI, sF, sP) {
  $("#" + sF + " option").remove();
    
    $.each(voipServers[sI].server_ports.split(','), function(index, item) {
      if (sP == item)
        $('#' + sF).append($("<option selected></option>").text(item).val(item));
      else
        $('#' + sF).append($("<option></option>").text(item).val(item));  
    });
}
    
function _start() {
  _AJAX(
    "/admin/voip/account_settings_json.php",
    "data[req]=account_list",
    function(data) {
      if (data == null)
        alert('There was a problem loading data');
      else
        ADT.fnAddData(data);
    },
    function(data) {
      alert('There was a problem loading data');
    }
  );

  /* Get Server List */
  _AJAX(
    "/admin/voip/voip_json.php",
    "data[req]=voipServers&data[eI]=1",
    function( data ) {
      voipServers = data;
    }
  );

  
  /*

  _AJAX(
    "/admin/voip/index_json.php",
    "data[req]=ytd_history",
    function(data) {
      populateYTDHistory(data);
    },
    function(data) {
      alert('There was a problem loading call data');
    }
  );
  */
}