<?php
/*
+--------------------------------------------------------------------------------------------------------------------------------+
| Simple Astersik Recording CDR - Written by Wayne Swart 2013 (pastoolio.poggempoel@geekabit.co.za                               |
|                                                                                                                                |
| This software is open source and comes with no guarantee. The source can be used, distributed and modified to suite any needs. |
+--------------------------------------------------------------------------------------------------------------------------------+

NB!
You should add a sim link here to the location of your recordings like the following

ln -s /var/spool/asterisk/monitor/ recording
*/

/* Some configuration */
define('dbhost','localhost');
define('dbpass','MY_DB_PASS');
define('dbuser','asteriskuser');
define('dbname','asteriskcdrdb');

define('sys_error_reporting','E_ALL');

/* Ends the configuration - There should not be any reason to change anything after this */
/* ------------------------------------------------------------------------------------- */

error_reporting(sys_error_reporting);

$conn = mysql_connect(dbhost, dbuser, dbpass) or die( mysql_error($conn) );
mysql_select_db(dbname, $conn);
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Simple Asterisk Recording CDR - Written by Wayne Swart</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="bootstrap.css" rel="stylesheet" media="screen">
    <style type="text/css" title="currentStyle">
			@import "datatables/media/css/demo_table.css";
		</style>
    <script type="text/javascript" language="javascript" src="jquery.js"></script>
    <script type="text/javascript" language="javascript" src="bootstrap.min.js"></script>
    <script type="text/javascript" language="javascript" src="datatables/media/js/jquery.dataTables.js"></script>
    <script type="text/javascript" src="datatables/media/js/dataTables.scrollingPagination.js"></script>


    <script type="text/javascript">
    jQuery().ready(function (){
	$('#Thecalls').dataTable({
		"sPaginationType": "bootstrap",
    		"aaSorting": [[ 1, "desc" ]],
    		"aoColumnDefs": [{ "bSortable": false, "aTargets": [ 3 ] }
    	]
	});


        });
    </script>

  </head>
<body>

<div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <div class="nav-collapse collapse">
            <ul class="nav">
              <li><a href="index.php?f=today">Today</a></li>
              <li><a href="index.php?f=month">This Month</a></li>
              <li><a href="index.php?f=year">This Year</a></li>
              <li><a href="index.php?f=all">All</a></li>
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>
<div class="container">
<br /><br /><br />
<?php
if( $_GET[f] == 'today' || $_GET[f] == '' || !isset($_GET[f]) ){
	$start_date = date("Y-m-d 00:00:00");
	$end_date = date("Y-m-d 23:59:59");
	$heading = "Today's calls";
}
elseif($_GET[f] == 'month'){
	$start_date = date("Y-m-01 00:00:00");
        $end_date = date("Y-m-31 23:59:59");
	$heading = "Calls for " . date("F Y");
}
elseif($_GET[f] == 'year'){
        $start_date = date("Y-01-01 00:00:00");
        $end_date = date("Y-12-31 23:59:59");
	$heading = "Calls for whole of " . date("Y");
}

if( $_GET[f] != 'all'  ){
	$SQL = "SELECT calldate,src,dst,userfield FROM cdr WHERE (duration > 0) AND (userfield != '') AND (calldate >= '$start_date') AND (calldate <= '$end_date') ORDER BY calldate DESC";
} else {
	$SQL = "SELECT calldate,src,dst,userfield FROM cdr WHERE (duration > 0) AND (userfield != '') ORDER BY calldate DESC";
	$heading = "All calls on record. (Can take long to load)";
}


$DTA = mysql_query($SQL, $conn) or die( mysql_error($conn) );

?>
<h1><?php echo $heading; ?></h1>
<table class="table table-bordered table-hover" id="Thecalls">
<thead>
	<tr>
		<th>Call Date</th>
		<th>Source</th>
		<th>Destination</th>
		<th>Recording</th>
	</tr>
</thead>
<tbody>
<?php
while($call = mysql_fetch_array($DTA)){
$call[userfield] = str_replace('audio:','',$call[userfield]);
?>
	<tr>
		<td><?php echo $call[calldate] ?>&nbsp;</td>
		<td><?php echo $call[src] ?>&nbsp;</td>
		<td><?php echo $call[dst] ?>&nbsp;</td>
		<td><a href="recording/<?php echo $call[userfield] ?>"><img src="play.png" border="0"></a>&nbsp;</td>
	</tr>
<?php
}
?>
</tbody>
</table>
</div> <!-- /container -->


</body>
</html>
