<?
/**
 * VoiceOne Callcenter
 *
 * Frontend to callcenter log service
 * @package callcenter
 * @subpackage callcenter
 */
  include ("../admin/config/cfg.php");
  global $cwlang;
  $alert=getAlert();
  
  if(!_isModuleLoaded('recording')) goToMainPage('',"../");
  //=============== POST AND GET ===============
  if((isset($_GET['del']))&&(trim($_GET['del'])!='')){
  	//put Contact
	$ret=$asterisk->delRecordedCall($_GET['del']);
	if($ret>0) $alert="<div class=\"AlertGood\">".T_("Recorded call has been deleted")."</div>";
	else $alert="<div class=\"AlertBad\">".T_("Recorded call has NOT been deleted")."</div>";
	//go to main page
	goToMainPage($alert,"./?rnd=".rand(0,99999));
  }
  if((isset($_GET['showpage']))&&(trim($_GET['showpage'])!=0)){
	$page=$showpage;
  }else $page=1;
  //================ PAGE LOGIC ================
  $html="";
  $pagination="";
  $result_per_page=15;
  $ret=$asterisk->getRecordedCalls();
  if((is_array($ret))&&(count($ret)>0)){
	$res_num=count($ret);
  	foreach($ret as $f){
	  //pagination
	  $i++;
	  if($i<=(($page-1)*$result_per_page)) continue; //this entry is in a previous page
	  if($i>(($page)*$result_per_page)) break; //this entry is in the next page
  	  //call entry
      $call_date=date('d/m/Y H:i', $f["call_date"]);
  	  $caller=$f["caller"];
      $callee=$f["callee"];
      switch($f["call_type"]){
      	case 1:
      		$call_type="<a style=\"background:none;\" title=\"".T_("Incoming")."\"><img src=\"$relBasePath/public/img/incoming.gif\" alt\"".T_("Incoming")."\" /></a>";
      		break;
      	case 2:
      		$call_type="<a style=\"background:none;\" title=\"".T_("Outgoing")."\"><img src=\"$relBasePath/public/img/outgoing.gif\" alt\"".T_("Outgoing")."\" />";
      		break;
      	case 3:
      		$call_type="<a style=\"background:none;\" title=\"".T_("Internal")."\"><img src=\"$relBasePath/public/img/internal.gif\" alt\"".T_("Internal")."\" />";
      		break;
      	default:
      		$call_type=" - ";
      		break;
      }
  	  $file_name=$f["file_name"];
  	  //html
  	  $html.="<tr>\n";
      $html.="  <td width=\"25%\" class=\"first\">".$call_date."</td>\n";
      $html.="  <td width=\"15%\" style=\"text-align:right; border-right:none;\">".$caller."</td>\n";
      $html.="  <td width=\"1%\" style=\"border-right:none;\">".$call_type."</td>\n";
      $html.="  <td width=\"15%\"style=\"text-align:left;\">".$callee."</td>\n";
      $html.="  <td width=\"1%\">";
      $html.="     <a title=\"".T_("Play Audio")."\" href=\"stream.php?filename=".$file_name."\" ><img src=\"$relBasePath/public/img/play.gif\"/></a>";
      $html.="  </td>\n";
      $html.="  <td width=\"1%\"><a title=\"".T_("Delete")."\" href=\"$PHP_SELF?del=".$file_name."\" onclick=\"if (confirm('".T_("Are you sure to delete recording audio file?")."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></td>\n";
      $html.="</tr>\n"; 	  
  	}
    //$pagination
    $page_num=intval($res_num/$result_per_page);
    if(($res_num%$result_per_page)>0) $page_num++;
    if($page_num>1){
	  $next_page=$page+1;
	  $prev_page=$pgae-1;
	  for($i=1;$i<=$page_num;$i++){
	    if($i==$page){
		  $pagination.="<span style=\"font-weight:bold; text-decoration:underline;\">".$i."</span>&nbsp;";
	    }else{
	      $pagination.="<span><a style=\"text-decoration:none;\" href=\"?showpage=".$i."\">".$i."</a></span>&nbsp;";
	    }
	  }
	  if($page>1){
	    $pagination="<span><a style=\"text-decoration:none;\" href=\"?showpage=".($page-1)."\">&lt;</a></span>&nbsp;".$pagination;
	    $pagination="<span><a style=\"text-decoration:none;\" href=\"?showpage=1\" ".$i."\">&lt;&lt;</a></span>&nbsp;".$pagination;
	  }else{
	    $pagination="<span>&lt;</span>&nbsp;".$pagination;
	    $pagination="<span>&lt;&lt;</span>&nbsp;".$pagination;
	  }
	  if($page<$page_num){
	    $pagination.="<span><a style=\"text-decoration:none;\" href=\"?showpage=".($page+1)."\">&gt;</a></span>&nbsp;";
	    $pagination.="<span><a style=\"text-decoration:none;\" href=\"?showpage=".$page_num."\" ".$i."\">&gt;&gt;</a></span>&nbsp;";
	  }else{
	    $pagination.="<span>&gt;</span>&nbsp;";
	    $pagination.="<span>&gt;&gt;</span>&nbsp;";
	  }
    }	  
  }else{
	$html="<td colspan=\"6\" class=\"first\">".T_("There are no recorded calls on the system")."</td>";
  }
  //================ MAIN PAGE ================
  ob_start();
  ?>
    <div class="SectionHeader">
      <h2 style="float:right;margin:10px 0 5px 0;"><?=$subtitle?></h2>
      <h1><?=T_('Recordings')?></h1>
    </div>
	<div id="alert"></div>
    <table id="ext_table" cellpadding="0" cellspacing="0" width="100%">
      <tr>
      	<th class="first bordered"><?=T_("Date")?></th>
      	<th colspan="3" class="bordered"><?=T_("Call")?></th>
      	<!--th class="bordered"><?=T_("Caller")?></th-->
      	<!--th class="bordered"><?=T_("Callee")?></th-->
      	<th class="bordered"><nobr><?=T_("Play Audio")?></nobr></th>
      	<th class="bordered"><?=T_("Delete")?></th>
    	</tr>
    	<?=$html?>
  	</table>
    <!-- Pagination buttons-->
    <div style="text-align:center;padding-top:5px;font-size:smaller;"><?=$pagination?></div>
  <?
  $contents=ob_get_contents();
  ob_end_clean();
  //================ SIDEBAR ================
  ob_start();
  ?>
    <div class="Sidebar">
      <h1><?=T_("Information")?></h1>
      <p style="min-height: 300px;">
        <?=T_("In this section are shown the recorded calls witch have been made by users or automatically by the system. It's possible to listen audio file, download it or, if user is allowed, delete it.")?>
        <br/><br/> 
	    <img src="<?=$relBasePath?>/public/img/outgoing.gif"/>&nbsp;&nbsp;<?=T_("Outgoing Call")?><br/>
	    <img src="<?=$relBasePath?>/public/img/incoming.gif"/>&nbsp;&nbsp;<?=T_("Incoming Call")?><br/>
	    <img src="<?=$relBasePath?>/public/img/internal.gif"/>&nbsp;&nbsp;<?=T_("Internal Call")?><br/>
	  </p>
    </div>
  <?
  $sidebar=ob_get_contents();
  ob_end_clean();
  //================ SETTINGS ================
  $smarty->assign("section", "recording");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('main.tpl');
?>
