<?php
class gateway extends Controller
{

	function gateway()
	{

		parent::Controller();
	}


	function index()
	{
	    if($this->clogin->is_logged_in())
		{
			redirect("admin/cpanel", "Location");
		}
		else
		{
			redirect("login/index", "Location");
		}
	}






/**
 ***********************************************************************
 * @Start Turnk  Section ***********************************************
 ***********************************************************************
*/



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Turnk List
 * @copyright 2011
 */
 

	function trunkList($errorMessage=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD == NO_ACCESS)
			redirect("admin/cpanel/accessDenied", "Location");
			
			$data['description'] = "NULL";
			$data['ipAddress'] = "NULL";
			$data['startoffset'] = "0";
			$data['limit'] = LIMIT_PERPAGE;
			$data['total'] = $this->gatewayModel->getTotalnumTrunk($description='NULL',$ipAddress='NULL');
			$data['turnkList'] = $this->gatewayModel->trunkList($startoffset = 0,$limit = LIMIT_PERPAGE,$description='NULL',$ipAddress='NULL');
			$data['titleMessage'] = TITLE036;
			if($errorMessage ==-1)
			$data['titleErrorMessage'] = "<span style='color:#BF0000'>:: Please at first Delete all Child Client</span>";
			elseif($errorMessage ==1)
			$data['titleErrorMessage'] = "<span style='color:#BF0000'>::You have sucessfull deleted Trunk Information </span>";
			else
			$data['titleErrorMessage'] = "";
			$this->load->view('gateway/turnk_list',$data);
		}
		else
		{
			redirect("login/index", "Location");
		}
	}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Turnk List Pagination
 * @copyright 2011
 */
 
	function trunkListPagination($startoffset,$limit,$description,$ipAddress,$myRandom=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			$data['description'] = $description;
			$data['ipAddress'] = $ipAddress;
			$data['startoffset'] = $startoffset;
			$data['limit'] = $limit;
			$data['total'] = $this->gatewayModel->getTotalnumTrunk($description,$ipAddress);
			$data['turnkList'] = $this->gatewayModel->trunkList($startoffset,$limit,$description,$ipAddress);
			$data['titleMessage'] = TITLE036;
			$data['titleErrorMessage'] = "";
			$this->load->view('gateway/turnk_list_pagi',$data);
		}
		else
		{
			redirect("login/index", "Location");
		}
	}



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Add Form of New Turnk
 * @copyright 2011
 */
 

	function addTrunkForm($errorMesssage=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{

			  $data['id_trunk']		= '';
			  $data['description']		= '';
			  $data['protocalList'] = $this->generallib->getProtocalList('SIP');
			  $data['rateList']		 = $this->generallib->getTariffList($id_rate=0);
		      //$data['providertech']		= 'SIP';
			  $data['providerip']		= '';
			  $data['inuse']		= '0';
			  $data['maxuse']		= '30';
			  $data['status']		= '1';
			  $data['if_max_use']		= '0';
			  $data['disallow_loss']		= '0';
			  $data['action']		= 'Add';
			  $data['titleMessage'] = TITLE037;

				if($errorMesssage==0)
				$data['titleErrorMessage'] = "";
				elseif($errorMesssage==-1)
				$data['titleErrorMessage'] = "<span style='color:#BF0000'>:: Please fill up all mandatory filed</span>";
				else
				$data['titleErrorMessage'] = "";



				$this->load->view('gateway/turnk_form',$data);

			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Add/Edit Turnk Form
 * @copyright 2011
 */
 
	function addEditTrunk()
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
               $action = $this->input->post('action');
               $providerip = $this->input->post('providerip');
               $providertech = $this->input->post('providertech');
 
               if(empty($providerip) OR empty($providertech))
               redirect("gateway/addTrunkForm/-1", "Location");

               $addDscripttion = "Add New Trunk,  IP: ".$providerip;
			   $editDescripttion = "Edit  Trunk,  IP: ".$providerip;

			   if($action=="Add")
			   {
			      $this->gatewayModel->addTrunk();
			      $this->generallib->addUserLog($userID,'-1','Insert',$addDscripttion);
			      redirect("gateway/trunkList", "Location");
			   }
			   else
			   {
			      $this->gatewayModel->editTrunk();
			      $this->generallib->addUserLog($userID,'-1','Edit',$editDescripttion);
			      redirect("gateway/trunkList", "Location");
			   }

			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Edit Form of Trunk name
 * @copyright 2011
 */
 

	function editTrunkForm($id_trunk=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD == NO_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
                $editInfo = $this->gatewayModel->trunkEditInfo($id_trunk);
                if($editInfo==0)
                {
                   redirect("gateway/dialPlanList", "Location");
                }
                else
                {
                   foreach($editInfo as $row)
                   {
						$data['id_trunk']		= $row->id_trunk;
						$data['description']		= $row->description;
						$data['protocalList'] = $this->generallib->getProtocalList($row->providertech);
			  			$data['rateList']		 = $this->generallib->getTariffList($row->id_rate);
						//$data['providertech']		= $row->providertech;
						$data['providerip']		= $row->providerip;
						$data['inuse']		= $row->inuse;
						$data['maxuse']		= $row->maxuse;
						$data['if_max_use']		= $row->if_max_use;
						$data['disallow_loss']		= $row->disallow_loss;
						$data['status']		= $row->status;
					}
                   
                   $data['titleErrorMessage'] = "";
                   $data['titleMessage'] = TITLE037;
				   $data['action']		= 'Edit';
                   $this->load->view('gateway/turnk_form',$data);
                }
			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	

/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete of Trunk name
 * @copyright 2011
 */
 
 
	function deleteTrunk($id_trunk)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
               $this->gatewayModel->deleteTrunk($id_trunk);
               redirect("gateway/trunkList/1", "Location");
			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	
		

/**
 ***********************************************************************
 * @End Trunk  Section ********************************************
 ***********************************************************************
*/		
		



/**
 ***********************************************************************
 * @Start Dial Plan  Section *******************************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Dial Plan List
 * @copyright 2011
 */
 

	function dialPlanList($route_type = 0 , $errorMessage=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD == NO_ACCESS)
			redirect("admin/cpanel/accessDenied", "Location");
			
			$data['trunkList'] = $this->generallib->getTrunkList($idTrunk='NULL');
			$data['prefix'] = "NULL";
			$data['idTrunk'] = "NULL";
			$data['startoffset'] = "0";
			$data['routeType'] = "0";
			$data['limit'] = LIMIT_PERPAGE;
			$data['total'] = $this->gatewayModel->getTotalnumDialPlan($idTrunk='NULL',$prefix='NULL',$route_type);
			$data['dialList'] = $this->gatewayModel->dialPlanList($startoffset = 0,$limit = LIMIT_PERPAGE,$idTrunk='NULL',$prefix='NULL',$route_type);
			$data['titleMessage'] = TITLE040;
			if($errorMessage ==-1)
			$data['titleErrorMessage'] = "<span style='color:#BF0000'>:: Please at first Delete all Child Client</span>";
			elseif($errorMessage ==1)
			$data['titleErrorMessage'] = "<span style='color:#BF0000'>::You have sucessfull deleted Dial Plan </span>";
			else
			$data['titleErrorMessage'] = "";
			$this->load->view('gateway/dialplan_list',$data);
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	

/**
 * @Show Dial Plan List Pagination
*/

	function dialPlanListPagination($startoffset,$limit,$prefix,$idTrunk,$routeType,$myRandom=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			$data['prefix'] = $prefix;
			$data['idTrunk'] = $idTrunk;
			$data['startoffset'] = $startoffset;
			$data['routeType'] = $routeType;
			$data['limit'] = $limit;
			$data['total'] = $this->gatewayModel->getTotalnumDialPlan($idTrunk,$prefix,$routeType);
			$data['dialList'] = $this->gatewayModel->dialPlanList($startoffset,$limit,$idTrunk,$prefix,$routeType);
			$data['titleMessage'] = TITLE040;
			$data['titleErrorMessage'] = "";
			$this->load->view('gateway/dialplan_list_pagi',$data);
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	
		


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Add Form of New Dial Plan
 * @copyright 2011
 */
 
	function addDialPlanForm($routeType,$errorMesssage=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
				$data['id']		= '';
				$data['trunkList']		= $this->generallib->getTrunkList($id_trunk='NULL');
				$data['prefix']		= '';
				$data['add_prefix']		= '';
				$data['remove_prefix']		= '';
				$data['priority']		= '1';
				$data['jump']		= '1';
				$data['share']		= '100';
				$data['routeType'] = $routeType;
				
				$data['fromDayList']		= $this->generallib->getDayList(1);
				$data['toDayList']		= $this->generallib->getDayList(7);
				$data['from_hour']		= '00:00';
				$data['to_hour']		= '23:59';
				$data['status'] = 1;

			    $data['action']		= 'Add';
			    $data['titleMessage'] = TITLE041;

				if($errorMesssage==0)
				$data['titleErrorMessage'] = "";
				elseif($errorMesssage==-1)
				$data['titleErrorMessage'] = "<span style='color:#BF0000'>:: Please fill up all mandatory filed</span>";
				else
				$data['titleErrorMessage'] = "";



				$this->load->view('gateway/dialplan_form',$data);

			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Add/Edit Turnk Form
 * @copyright 2011
 */
 
	function addEditDialPlan()
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
               $action = $this->input->post('action');
               $prefix = $this->input->post('prefix');
               $id_trunk = $this->input->post('id_trunk');
               $priority = $this->input->post('priority');
               $routeType = $this->input->post('routeType');
 
               if(empty($prefix) OR empty($id_trunk))
               redirect("gateway/addDialPlanForm/".$routeType."/-1", "Location");

               $addDscripttion = "Add New Dial Plan,  Prefix: ".$prefix." ID Trank:".$id_trunk." and Priority:".$priority;
			   $editDescripttion = "Edit Dial Plan,  Prefix: ".$prefix." ID Trank:".$id_trunk." and Priority:".$priority;

			   if($action=="Add")
			   {
			      $this->gatewayModel->addDialPlan();
			      $this->generallib->addUserLog($userID,'-1','Insert',$addDscripttion);
			      redirect("gateway/dialPlanList/".$routeType."/0", "Location");
			   }
			   else
			   {
			      $this->gatewayModel->editDialPlan();
			      $this->generallib->addUserLog($userID,'-1','Edit',$editDescripttion);
			      redirect("gateway/dialPlanList/".$routeType."/0", "Location");
			   }

			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Edit Form of Dial Plan
 * @copyright 2011
 */

	function editDialPlanForm($id=0,$routeType = 0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD == NO_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
                $editInfo = $this->gatewayModel->dialPlanEditInfo($id);
                if($editInfo==0)
                {
                   redirect("gateway/dialPlanList", "Location");
                }
                else
                {
                   foreach($editInfo as $row)
                   {
					 $data['id']		= $row->id;
					 $data['trunkList']		= $this->generallib->getTrunkList($row->id_trunk);
					 $data['prefix']		= $row->prefix;
					 $data['add_prefix']		= $row->add_prefix;
					 $data['remove_prefix']		= $row->remove_prefix;
					 $data['priority']		= $row->priority;
					 $data['jump']		= $row->jump;
					 $data['share']		= $row->share;
					 $data['fromDayList']		= $this->generallib->getDayList($row->from_day);
					 $data['toDayList']		= $this->generallib->getDayList($row->to_day);
					 $data['from_hour']		= $row->from_hour;
					 $data['to_hour']		= $row->to_hour;
					 $data['routeType']		= $row->route_type;
					 $data['status']		= $row->status;
						
					}
                   
                   $data['titleErrorMessage'] = "";
                   $data['titleMessage'] = TITLE041;
				   $data['action']		= 'Edit';
                   $this->load->view('gateway/dialplan_form',$data);
                }
			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	
	

/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete Trunk Information
 * @copyright 2011
 */

	function deleteDialPlan($id,$routeType = 0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
               $this->gatewayModel->deleteDialPlan($id);
               redirect("gateway/dialPlanList/".$routeType."/1", "Location");
			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	
	
	

/**
 ***********************************************************************
 * @Start End Dial Plan Section ********************************************
 ***********************************************************************
*/


/**
 ***********************************************************************
 * @Start LCR Dial Plan  Section *******************************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Dial Plan List
 * @copyright 2011
 */
 

	function lcrDialPlanList($route_type = 1 , $errorMessage=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD == NO_ACCESS)
			redirect("admin/cpanel/accessDenied", "Location");
			
			$data['trunkList'] = $this->generallib->getTrunkList($idTrunk='NULL');
			$data['prefix'] = "NULL";
			$data['idTrunk'] = "NULL";
			$data['startoffset'] = "0";
			$data['routeType'] = $route_type;
			$data['limit'] = LIMIT_PERPAGE;
			$data['total'] = $this->gatewayModel->getTotalnumDialPlan($idTrunk='NULL',$prefix='NULL',$route_type);
			$data['dialList'] = $this->gatewayModel->dialPlanList($startoffset = 0,$limit = LIMIT_PERPAGE,$idTrunk='NULL',$prefix='NULL',$route_type);
			$data['titleMessage'] = TITLE040;
			if($errorMessage ==-1)
			$data['titleErrorMessage'] = "<span style='color:#BF0000'>:: Please at first Delete all Child Client</span>";
			elseif($errorMessage ==1)
			$data['titleErrorMessage'] = "<span style='color:#BF0000'>::You have sucessfull deleted Dial Plan </span>";
			else
			$data['titleErrorMessage'] = "";
			$this->load->view('gateway/lcr_dialplan_list',$data);
		}
		else
		{
			redirect("login/index", "Location");
		}
	}



/**
 * @Show Dial Plan List Pagination
*/

	function lcrDialPlanListPagi($startoffset,$limit,$prefix,$idTrunk,$routeType,$myRandom=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			$data['prefix'] = $prefix;
			$data['idTrunk'] = $idTrunk;
			$data['startoffset'] = $startoffset;
			$data['routeType'] = $routeType;
			$data['limit'] = $limit;
			$data['total'] = $this->gatewayModel->getTotalnumDialPlan($idTrunk,$prefix,$routeType=1);
			$data['dialList'] = $this->gatewayModel->dialPlanList($startoffset,$limit,$idTrunk,$prefix,$routeType=1);
			$data['titleMessage'] = TITLE040;
			$data['titleErrorMessage'] = "";
			$this->load->view('gateway/lcr_dialplan_list_pagi',$data);
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	
		



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Add Form of New Dial Plan
 * @copyright 2011
 */
 
	function addLCRDialPlanForm($routeType,$errorMesssage=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
				$data['id']		= '';
				$data['trunkList']		= $this->generallib->getTrunkList($id_trunk='NULL');
				$data['prefix']		= '';
				$data['add_prefix']		= '';
				$data['remove_prefix']		= '';
				$data['priority']		= '1';
				$data['jump']		= '1';
				$data['share']		= '100';
				$data['routeType'] = $routeType;
				
				$data['fromDayList']		= $this->generallib->getDayList(1);
				$data['toDayList']		= $this->generallib->getDayList(7);
				$data['from_hour']		= '00:00';
				$data['to_hour']		= '23:59';
				$data['status'] = 1;

			    $data['action']		= 'Add';
			    $data['titleMessage'] = TITLE041;

				if($errorMesssage==0)
				$data['titleErrorMessage'] = "";
				elseif($errorMesssage==-1)
				$data['titleErrorMessage'] = "<span style='color:#BF0000'>:: Please fill up all mandatory filed</span>";
				else
				$data['titleErrorMessage'] = "";



				$this->load->view('gateway/lcr_dialplan_form',$data);

			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
		



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Add/Edit Turnk Form
 * @copyright 2011
 */
 
	function addEditLCRDialPlan()
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$dialPlanCURD = $this->session->userdata('dialPlanCURD');
			if($dialPlanCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
               $action = $this->input->post('action');
               $prefix = $this->input->post('prefix');
               $idTrunks = $this->input->post('idTrunks');
               $priority = $this->input->post('priority');
               $routeType = $this->input->post('routeType');

               if(empty($prefix))
               redirect("gateway/addLCRDialPlanForm/".$routeType."/-1", "Location");


			   if($action=="Add")
			   {
			      $this->gatewayModel->addLCRDialPlan();
			      //$this->generallib->addUserLog($userID,'-1','Insert',$addDscripttion);
			      redirect("gateway/lcrDialPlanList/".$routeType."/0", "Location");
			   }
			   else
			   {
			      $this->gatewayModel->editDialPlan();
			      //$this->generallib->addUserLog($userID,'-1','Edit',$editDescripttion);
			      redirect("gateway/lcrDialPlanList/".$routeType."/0", "Location");
			   }
			   
			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete Rate Chart Prefix
 * @copyright 2011
 */
 

	function deleteLCRDialPlanAll()
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			//$accessGroup = $accessLevel = $this->session->userdata('accessLevel');
			$accessPrivilege = ALL_ACCESS;
			$json = array();
			if($accessPrivilege != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
				$keyvalue	= $this->input->post('check');
				$idRate	= $this->input->post('idRate');
			
				if(is_array($keyvalue))
				{
					foreach($keyvalue as $val)
					{
						$idRateKey = $val;
						$tableNameRateChart = "dialplan";
						$sqlDelete = "DELETE FROM $tableNameRateChart WHERE id = '$idRateKey'";
						$queryDelete = $this->db->query($sqlDelete);
					}
					
					echo site_url('gateway/lcrDialPlanList/1/0'.$idRate."/100/NULL");
				}
				else
					echo site_url('gateway/lcrDialPlanList/1/0'.$idRate."/1/NULL");
 			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	
		
		


}
?>