<?
/**
 * VoiceOne Webservices Methods
 *  
 * @package voiceone_webservices
 * @subpackage core
 */
//******************************************************************************
//******************           PUBLIC FUNCTIONS            *********************
//******************************************************************************
/********************************* LOCATION ***********************************/
/**
* FUNCTION: getLocation
* Get locations defined on the system
* @param id:int ->  Location's id used to retireve data
* @return:LocationArray -> Array of structure locations data
*/
function getLocation($id){
  global $db;
  //set where string
  if((isset($id))&&($id!="")&&($id!=0)) $where="WHERE `id`='".$id."'";
  //get countries
  $qry="SELECT * FROM `cw_location` ".$where." ORDER BY `default` DESC,`name` ";
  $res=$db->Execute($qry);
  if($res===false) return 0;
  while($arr=$res->FetchRow()){
    //explode area codes
    if(trim($arr["special_patterns"])!="") $arr["special_patterns"]=explode("|",$arr["special_patterns"]);
    if(!is_array($arr["special_patterns"])) $arr["special_patterns"]=array();
    //explode area codes
    if(trim($arr["area_codes"])!="") $arr["area_codes"]=explode("|",$arr["area_codes"]);
    if(!is_array($arr["area_codes"])) $arr["area_codes"]=array();
    //salve location
    $ret[]=$arr;
  }
  //return
  return $ret;
}
/**
* FUNCTION: putLocation
* Save or Modify a location
* @param location:Location -> Structure contains location data
* @return:int -> If success it return location id, else return zero
*/
function putLocation($location){
  global $db;
  //set update if provisioning client already exists
  $old_area_codes=array();
  if((isset($location["id"]))&&($location["id"]!="")&&($location["id"]!=0)){
    //extract old location params
    $id=$location["id"];
    $oldLocation=getLocation($id);
    if((is_array($oldLocation))&&(is_array($oldLocation[0]))){
      $old_area_codes=$oldLocation[0]["area_codes"];
    }
    //del location
    delLocation($id,false);
  }else $id="null";
  //implode area codes
  $area_codes="";
  if((is_array($location["area_codes"]))&&(count($location["area_codes"])>0)){
    $area_codes=implode("|",$location["area_codes"]);
  }
  //check if request delete some area codes, and if these area codes are deletable
  if((is_array($old_area_code))&&(count($old_area_code)>0)){
    $deleted_area_codes=array_diff($old_area_codes,$location["area_codes"]);
    if(is_array($deleted_area_codes)){
      foreach($deleted_area_codes as $ac){
        if(!isAreaCodeErasable($id,$ac)) return 0;
      }
    }
  }  
  //implode special patterns
  $special_patterns="";
  if((is_array($location["special_patterns"]))&&(count($location["special_patterns"])>0)){
    $special_patterns=implode("|",$location["special_patterns"]);
  }
  //check default
  if($location["default"]==1){
    $qry="UPDATE `cw_location` SET `default`=0";
    $res=$db->Execute($qry);
    if($res===false) return 0;
  }
  //insert provisioning client
  $qry="INSERT INTO `cw_location`(`id`,`name`,`int_prefix`,`country_code`,`nat_prefix`,`nat_prefix_intl`,`area_codes`,`special_patterns`,`default`) ";
  $qry.="VALUES (".$id.",'".string2sql($location["name"])."','".string2sql($location["int_prefix"])."','".string2sql($location["country_code"])."',";
  $qry.="'".string2sql($location["nat_prefix"])."','".string2sql($location["nat_prefix_intl"])."','".string2sql($area_codes)."',";
  $qry.="'".string2sql($special_patterns)."','".string2sql($location["default"])."')";
  $res=$db->Execute($qry);
  if($res===false) return 0;
  if($id=="null") $id=$db->Insert_ID();
  //return
	return $id;
}
/**
* FUNCTION: delLocation
* Delete a location
* @param id:int -> Id of the location to delete
* @return:int -> If success it return deleted entity id, else return zero
*/
function delLocation($id,$reload=true){
  global $db;
  //Checks
  if((!isset($id))||($id=="")||($id==0)) return 0;
  if(($reload===true)&&(!isLocationErasable($id))) return 0;
  //delete location
  $qry="DELETE FROM `cw_location` WHERE `id`=".$id." LIMIT 1";
  $res=$db->Execute($qry);
  if($res===false) return 0; 
  //return
  return $id;
}
/**
* FUNCTION: isLocationErasable
* Check if a location is erasable
* @param id:int -> Id of the location to check
* @return:int -> If yes it return 1, else return zero
*/
function isLocationErasable($id){
  global $db;
  //Check location number
  $locations=getLocation(0);
  if(count($locations)<2) return 0;
  //Check default location
  if(is_array($locations)){
    foreach($locations as $l){
      if($l["id"]==$id){
        if($l["default"]==1) return 0;
        break;
      }
    }
  }
  //Check location use
  $channels=getChannel(0);
  if(is_array($channels)){
    foreach($channels as $c){
      if($c["location"]==$id) return 0;
    }
  }
  //return
  return 1;
}
/**
* FUNCTION: isAreaCodeErasable
* Check if a location's area code is erasable
* @param location:int -> Id of the location linked to the area code
* @param area_code:int -> Id of the area code to check
* @return:int -> If yes it return 1, else return zero
*/
function isAreaCodeErasable($location,$area_code){
  global $db;
  //Check location use
  $channels=getChannel(0);
  if(is_array($channels)){
    foreach($channels as $c){
      if(($c["location"]==$location)&&($c["area_code"]==$area_code)) return 0;
    }
  }
  //return
  return 1;
}
//******************************************************************************
//*****************           PRIVATE FUNCTIONS            *********************
//******************************************************************************

?>