Newer
Older
ubFramework / Portal / docroot / assets / js / user / voip / extension.js
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 5 KB Cleaning Up Making It A Sub Module
/**
 * Copyright (c) 2015, 2016 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 334 2015-12-19 02:35:30Z reddawg $
 *
 */

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

var add_extension;
var add_extension_form;

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

function editCloseExtension() {
  _AJAX(
	"/user/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({
    "columns": [
      { "visible": false },
      { "width": "40" },
      null,
      { "width": "50" },
      { "width": "90" },
      { "width": "70" },
      { "width": "70" },
      { "visible": false}
    ]
  });
  
  // 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(
	  "/user/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.match(/(\d+)/g));
	    
	    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(
	      "/user/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(
	         "/user/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: 400,
    width: 900,
    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");

    _AJAX(
      "/user/voip/extension_json.php",
      "data[req]=extension_existing",
      function(data) {
        $('#eExt tbody').append(data);
      },
      function(data) {
        alert('There was a problem loading data');
      }
    );
  });
  
});

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