<?php
	
	if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');	
	extract($_POST, EXTR_PREFIX_SAME, "wddx");	
	
	if(isset($searchModule) && $searchModule != '' && isset($searchRecord) && searchRecord != '') {
		
		require_once('modules/EcmCalls/EcmCall.php');
		EcmCall::loadCallsSettings();
		
		if(isset($_SESSION['ecmcalls_phones'][$searchModule][$searchRecord])) {

			function myImplode($before, $after, $glue, $array){
				$nbItem = count($array);
				$i = 1;
				foreach($array as $item){
					if($i < $nbItem){
						$output .= "$before$item$after$glue";
					}else $output .= "$before$item$after";
					$i++;
				}
				return $output;
			} 

			$base = $_SESSION['ecmcalls_phones'][$searchModule][$searchRecord];
		
			//$ecmcall = new EcmCall();
			$arr = array();
			foreach($base['numbers'] as $key => $number) $arr[] = $number[0];
			
			$q_numbers = myImplode('`src` LIKE \'%','\'',' OR ',$arr).' OR '.myImplode(' `dst` LIKE \'%','\'',' OR ',$arr).' OR '.myImplode(' `dcontext` LIKE \'%','%\'',' OR ',$arr);
			$query = "SELECT `disposition`, `calldate`, `src`, `dst`, `duration`, `billsec`, `id`, `note`, `dcontext`, `dstchannel` FROM `cdr` WHERE ($q_numbers) AND `deleted`='0'";
			
			
			$calls_ = array();
			$call = new EcmCall();
			$result = $GLOBALS['db']->query($query);
			if($result) {
				while($row = $GLOBALS['db']->fetchByAssoc($result)) {
					$calls_[] = $call->processCall($row);
				}
			}
			
			global $odd_bg, $even_bg, $hilite_bg;
			$json = getJSONobj();
			$calls = array(
				'positions' => $calls_,
				'colors' => array(
					'even_bg' => $even_bg,
					'hilite_bg' => $hilite_bg,
					'odd_bg' => $odd_bg	
				),
			);
			echo '['.$json->encode($calls).']';
		}
	}
	
?>