Newer
Older
ubFramework / Portal / docroot / assets / js / admin / voip / extension.js
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 7 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: extension.js 375 2016-01-27 15:10:40Z reddawg $
 *
 */

/* Datatables Variables */
var EDT;
var edit_extension;
var edit_extension_form;

var add_extension;
var add_extension_form;

function editExtension() {
  _AJAX(
	"/admin/voip/extension_json.php",
	edit_extension_form.serialize(),
	function (data) {
	  alert(data.ret_string);
	}
  );
}

function editCloseExtension() {
  _AJAX(
	"/admin/voip/extension_json.php",
	edit_extension_form.serialize(),
	function (data) {
	  alert(data.ret_string);
	  if (data.ret == 1)
	    edit_extension.dialog('close');
	}
  );	
}

function addExtension() {
	
}

$(function() {
  // Initialize Account DataTable
  EDT = $('#extTable').dataTable();
  
  // Initialize Edit Extension Modal
  edit_extension = $( "#dialog-edit-extension" ).dialog({
    autoOpen: false,
    height: 400,
    width: 750,
    modal: true,
    buttons: {
      "Save": editExtension,
      "Save & Close": editCloseExtension,
      Cancel: function() {
        edit_extension.dialog( "close" );
      }
    },
    close: function() {
      edit_extension_form[ 0 ].reset();
    }
  });

  edit_extension_form = $("#edit-extension-form" ).on("submit", function( event ) {
    event.preventDefault();
  });
  
  EDT.delegate(".edit_extension", "click", function() {
    edit_extension.dialog("open");
	    
    _AJAX(
	  "/admin/voip/extension_json.php",
	  "data[req]=extension_info&data[ext]=" + this.id,
	  function(data) {
	    $('#e_ext').val(data.name);
	    $('#e_ext_label').text(data.name);
	    
	    var cid = data.callerid.split("<");

	    $('#e_cid_name').val(cid[0].trim());
	    $('#e_cid_number').val(cid[1].slice(0, -1));
	    $('#e_secret').val(data.secret);
            $('#e_active').val(data.ext_active);
	    
	    _AJAX(
	      "/admin/voip/extension_json.php",
	      "data[req]=mailbox_list&data[account_code]=" + data.accountcode,
	      function(mbData) {
	        $("#e_mailbox option").remove();
	        $.each(mbData, function(index, item) {

	          if (item.mailbox == data.mailbox)
	            $("#e_mailbox").append($("<option selected></option>").text(item.label).val(item.mailbox));
	          else
	            $("#e_mailbox").append($("<option></option>").text(item.label).val(item.mailbox));
	        });
          },
          function(data) {        
	      }
	    );
	     _AJAX(
	         "/admin/voip/extension_json.php",
	         "data[req]=recording_options&data[account_code]=" + data.accountcode,
	         function(mbData) {
	           $("#e_recording option").remove();
	           $.each(mbData, function(index, item) {

	             if (item.id == data.ext_recording)
	               $("#e_recording").append($("<option selected></option>").text(item.label).val(item.id));
	             else
	               $("#e_recording").append($("<option></option>").text(item.label).val(item.id));
	           });
	           },
	           function(data) {        
	         }
	       );
	  }
	);
  });
  
  // Initialize Edit Extension Modal
  add_extension = $( "#dialog-add-extension" ).dialog({
    autoOpen: false,
    height: 550,
    width: 1170,
    modal: true,
    buttons: {
      "Save": editExtension,
      Cancel: function() {
        add_extension.dialog( "close" );
      }
    },
    close: function() {
      add_extension_form[ 0 ].reset();
    }
  });
  
  add_extension_form = $("#add-extension-form" ).on("submit", function( event ) {
    event.preventDefault();
  });
  
  $('#addExt').on("click", function() {
	  add_extension.dialog("open");

	  /* Populate Account List */
	  _AJAX(
	    "/admin/voip/voip_json.php",
	    "data[req]=accountCodes",
	    function(data) {
	    $("#ae_actList option").remove();
	      $.each(data, function(index, item) {
	        if ($('#actList').val() == item.ID)
	          $('#ae_actList').append($("<option selected></option>").text(item.Desc).val(item.ID));
	        else
	          $('#ae_actList').append($("<option></option>").text(item.Desc).val(item.ID));
	      });
	    }
	  );
	  
    _AJAX(
      "/admin/voip/extension_json.php",
      "data[req]=extension_existing&data[account_code]=" + $('#actList').val(),
      function(data) {
        $('#eExt tbody').append(data);
      },
      function(data) {
        alert('There was a problem loading data');
      }
    );
  });
  
  $("#ae_actList").on("change", function() {
    _AJAX(
        "/admin/voip/extension_json.php",
        "data[req]=extension_existing&data[account_code]=" + $('#ae_actList').val(),
        function(data) {
          $('#eExt > tbody').empty();
          $('#eExt tbody').append(data);
        },
        function(data) {
          alert('There was a problem loading data');
        }
      );
  });
  
  $("#actList").on("change", function() {
    _AJAX(
      "/admin/voip/extension_json.php",
      "data[req]=extension_list&data[account_code]=" + $('#actList').val(),
        function(data) {
          EDT.fnClearTable();
          if (data)
            EDT.fnAddData(data);
        },
        function(data) {
          alert('There was a problem loading data');
        }
      );
  });
  
  $("#actList").combobox({
    select:function() {
	  _AJAX(
	    "/admin/voip/extension_json.php",
	    "data[req]=extension_list&data[account_code]=" + $('#actList').val(),
	    function(data) {
	      EDT.fnClearTable();
	      if (data)
	        EDT.fnAddData(data);
	    },
	    function(data) {
	      alert('There was a problem loading data');
	    }
	  );
	}
  });
  
});

/* Entry Point */
function _start() {
  /* Populate Account List */
  _AJAX(
    "/admin/voip/voip_json.php",
    "data[req]=accountCodes",
    function(data) {
    $("#actList option").remove();
      $.each(data, function(index, item) {
        $("#actList").append($("<option></option>").text(item.Desc).val(item.ID));
    });
    }
  );

  _AJAX(
    "/admin/voip/extension_json.php",
    "data[req]=extension_list",
    function(data) {
      EDT.fnAddData(data);
    },
    function(data) {
      alert('There was a problem loading data');
    }
  );
}