2011-04-25 15:24:01 +00:00
< ? php
class Logbook_model extends CI_Model {
function __construct ()
{
// Call the Model constructor
parent :: __construct ();
}
2011-07-22 00:08:47 +00:00
/* Add QSO to Logbook */
function add () {
// Join date+time
$datetime = date ( 'Y-m-d' ) . " " . $this -> input -> post ( 'start_time' );
// Create array with QSO Data
$data = array (
'COL_TIME_ON' => $datetime ,
'COL_TIME_OFF' => $datetime ,
'COL_CALL' => strtoupper ( $this -> input -> post ( 'callsign' )),
'COL_BAND' => $this -> input -> post ( 'band' ),
'COL_FREQ' => $this -> input -> post ( 'freq' ),
'COL_MODE' => $this -> input -> post ( 'mode' ),
'COL_RST_RCVD' => $this -> input -> post ( 'rst_recv' ),
'COL_RST_SENT' => $this -> input -> post ( 'rst_sent' ),
2011-07-24 22:33:08 +00:00
'COL_NAME' => $this -> input -> post ( 'name' ),
2011-07-22 00:08:47 +00:00
'COL_COMMENT' => $this -> input -> post ( 'comment' ),
'COL_SAT_NAME' => $this -> input -> post ( 'sat_name' ),
'COL_SAT_MODE' => $this -> input -> post ( 'sat_mode' ),
'COL_GRIDSQUARE' => $this -> input -> post ( 'locator' ),
'COL_COUNTRY' => $this -> input -> post ( 'country' ),
'COL_MY_RIG' => $this -> input -> post ( 'equipment' ),
2011-08-01 10:37:16 +00:00
'COL_QSLSDATE' => date ( 'Y-m-d' ),
'COL_QSLRDATE' => date ( 'Y-m-d' ),
'COL_QSL_SENT' => $this -> input -> post ( 'qsl_sent' ),
'COL_QSL_RCVD' => $this -> input -> post ( 'qsl_recv' ),
'COL_QSL_SENT_VIA' => $this -> input -> post ( 'qsl_sent_method' ),
'COL_QSL_RCVD_VIA' => $this -> input -> post ( 'qsl_recv_method' ),
2011-08-20 21:24:10 +00:00
'COL_OPERATOR' => $this -> session -> userdata ( 'user_callsign' ),
2011-07-22 00:08:47 +00:00
);
// Add QSO to database
$this -> db -> insert ( $this -> config -> item ( 'table_name' ), $data );
}
/* Edit QSO */
function edit () {
$data = array (
'COL_TIME_ON' => $this -> input -> post ( 'time_on' ),
'COL_TIME_OFF' => $this -> input -> post ( 'time_off' ),
'COL_CALL' => strtoupper ( $this -> input -> post ( 'callsign' )),
'COL_BAND' => $this -> input -> post ( 'band' ),
'COL_FREQ' => $this -> input -> post ( 'freq' ),
'COL_MODE' => $this -> input -> post ( 'mode' ),
'COL_RST_RCVD' => $this -> input -> post ( 'rst_recv' ),
'COL_RST_SENT' => $this -> input -> post ( 'rst_sent' ),
2011-08-21 14:15:10 +00:00
'COL_GRIDSQUARE' => $this -> input -> post ( 'locator' ),
2011-07-22 00:08:47 +00:00
'COL_COMMENT' => $this -> input -> post ( 'comment' ),
'COL_NAME' => $this -> input -> post ( 'name' ),
'COL_SAT_NAME' => $this -> input -> post ( 'sat_name' ),
'COL_SAT_MODE' => $this -> input -> post ( 'sat_mode' ),
2011-08-01 10:37:16 +00:00
'COL_QSLSDATE' => date ( 'Y-m-d' ),
'COL_QSLRDATE' => date ( 'Y-m-d' ),
'COL_QSL_SENT' => $this -> input -> post ( 'qsl_sent' ),
'COL_QSL_RCVD' => $this -> input -> post ( 'qsl_recv' ),
'COL_QSL_SENT_VIA' => $this -> input -> post ( 'qsl_sent_method' ),
'COL_QSL_RCVD_VIA' => $this -> input -> post ( 'qsl_recv_method' ),
2011-07-22 00:08:47 +00:00
);
$this -> db -> where ( 'COL_PRIMARY_KEY' , $this -> input -> post ( 'id' ));
$this -> db -> update ( $this -> config -> item ( 'table_name' ), $data );
}
/* Return last 10 QSOs */
function last_ten () {
2011-08-23 15:34:05 +00:00
$this -> db -> select ( 'COL_CALL, COL_BAND, COL_TIME_ON, COL_RST_RCVD, COL_RST_SENT, COL_MODE, COL_NAME, COL_COUNTRY, COL_PRIMARY_KEY, COL_SAT_NAME' );
2011-08-17 15:12:41 +00:00
$this -> db -> order_by ( " COL_TIME_ON " , " desc " );
2011-07-22 00:08:47 +00:00
$this -> db -> limit ( 10 );
return $this -> db -> get ( $this -> config -> item ( 'table_name' ));
}
2011-07-24 22:33:08 +00:00
/* Callsign QRA */
function call_qra ( $callsign ) {
$this -> db -> select ( 'COL_CALL, COL_GRIDSQUARE, COL_TIME_ON' );
$this -> db -> where ( 'COL_CALL' , $callsign );
$where = " COL_GRIDSQUARE != \" \" " ;
$this -> db -> where ( $where );
$this -> db -> order_by ( " COL_TIME_ON " , " desc " );
$this -> db -> limit ( 1 );
$query = $this -> db -> get ( $this -> config -> item ( 'table_name' ));
$callsign = " " ;
if ( $query -> num_rows () > 0 )
{
$data = $query -> row ();
$callsign = strtoupper ( $data -> COL_GRIDSQUARE );
}
return $callsign ;
}
function call_name ( $callsign ) {
$this -> db -> select ( 'COL_CALL, COL_NAME, COL_TIME_ON' );
$this -> db -> where ( 'COL_CALL' , $callsign );
$where = " COL_NAME != \" \" " ;
$this -> db -> where ( $where );
$this -> db -> order_by ( " COL_TIME_ON " , " desc " );
$this -> db -> limit ( 1 );
$query = $this -> db -> get ( $this -> config -> item ( 'table_name' ));
$name = " " ;
if ( $query -> num_rows () > 0 )
{
$data = $query -> row ();
$name = $data -> COL_NAME ;
} else {
//$json = file_get_contents("http://callbytxt.org/db/".$callsign.".json");
//$obj = json_decode($json);
//$uppercase_name = strtolower($obj->{'calls'}->{'first_name'});
// $name = ucwords($uppercase_name);
}
return $name ;
}
2011-07-22 00:08:47 +00:00
/* Return QSO Info */
function qso_info ( $id ) {
$this -> db -> where ( 'COL_PRIMARY_KEY' , $id );
return $this -> db -> get ( $this -> config -> item ( 'table_name' ));
}
2011-04-25 15:24:01 +00:00
function get_qsos ( $num , $offset ) {
2011-08-23 15:34:05 +00:00
$this -> db -> select ( 'COL_CALL, COL_BAND, COL_TIME_ON, COL_RST_RCVD, COL_RST_SENT, COL_MODE, COL_NAME, COL_COUNTRY, COL_PRIMARY_KEY, COL_SAT_NAME' );
2011-04-25 15:24:01 +00:00
$this -> db -> order_by ( " COL_TIME_ON " , " desc " );
2011-06-17 12:52:00 +00:00
$query = $this -> db -> get ( $this -> config -> item ( 'table_name' ), $num , $offset );
2011-04-25 15:24:01 +00:00
return $query ;
}
2011-06-17 12:52:00 +00:00
function get_last_qsos ( $num ) {
2011-08-23 15:34:05 +00:00
$this -> db -> select ( 'COL_CALL, COL_BAND, COL_TIME_ON, COL_RST_RCVD, COL_RST_SENT, COL_MODE, COL_NAME, COL_COUNTRY, COL_PRIMARY_KEY, COL_SAT_NAME' );
2011-06-17 12:52:00 +00:00
$this -> db -> order_by ( " COL_TIME_ON " , " desc " );
$this -> db -> limit ( $num );
$query = $this -> db -> get ( $this -> config -> item ( 'table_name' ));
return $query ;
}
function get_todays_qsos () {
$morning = date ( 'Y-m-d 00:00:00' );
$night = date ( 'Y-m-d 23:59:59' );
2011-06-18 22:38:09 +00:00
$query = $this -> db -> query ( 'SELECT * FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_TIME_ON between \'' . $morning . '\' AND \'' . $night . '\'' );
2011-06-17 12:52:00 +00:00
return $query ;
}
function total_qsos () {
2011-06-18 22:38:09 +00:00
$query = $this -> db -> query ( 'SELECT COUNT( * ) as count FROM ' . $this -> config -> item ( 'table_name' ) . '' );
2011-06-17 12:52:00 +00:00
if ( $query -> num_rows () > 0 )
{
foreach ( $query -> result () as $row )
{
return $row -> count ;
}
}
}
function todays_qsos () {
$morning = date ( 'Y-m-d 00:00:00' );
$night = date ( 'Y-m-d 23:59:59' );
2011-06-18 22:38:09 +00:00
$query = $this -> db -> query ( 'SELECT COUNT( * ) as count FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_TIME_ON between \'' . $morning . '\' AND \'' . $night . '\'' );
2011-06-17 12:52:00 +00:00
if ( $query -> num_rows () > 0 )
{
foreach ( $query -> result () as $row )
{
return $row -> count ;
}
}
}
2011-08-23 16:12:47 +00:00
function get_this_weeks_qsos () {
$morning = date ( 'Y/m/d' , time () + ( 1 - date ( 'w' )) * 24 * 3600 );
$night = date ( 'Y/m/d' , time () + ( 7 - date ( 'w' )) * 24 * 3600 );
$query = $this -> db -> query ( 'SELECT * FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_TIME_ON between \'' . $morning . '\' AND \'' . $night . '\'' );
return $query ;
}
2011-06-17 12:52:00 +00:00
function month_qsos () {
$morning = date ( 'Y-m-01 00:00:00' );
$night = date ( 'Y-m-30 23:59:59' );
2011-06-18 22:38:09 +00:00
$query = $this -> db -> query ( 'SELECT COUNT( * ) as count FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_TIME_ON between \'' . $morning . '\' AND \'' . $night . '\'' );
2011-06-17 12:52:00 +00:00
if ( $query -> num_rows () > 0 )
{
foreach ( $query -> result () as $row )
{
return $row -> count ;
}
}
}
function year_qsos () {
$morning = date ( 'Y-01-01 00:00:00' );
$night = date ( 'Y-12-31 23:59:59' );
2011-06-18 22:38:09 +00:00
$query = $this -> db -> query ( 'SELECT COUNT( * ) as count FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_TIME_ON between \'' . $morning . '\' AND \'' . $night . '\'' );
2011-06-17 12:52:00 +00:00
if ( $query -> num_rows () > 0 )
{
foreach ( $query -> result () as $row )
{
return $row -> count ;
}
}
}
function total_ssb () {
2011-07-21 23:10:09 +00:00
$query = $this -> db -> query ( 'SELECT COUNT( * ) as count FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_MODE = \'SSB\' or COL_MODE = \'LSB\' or COL_MODE = \'USB\'' );
2011-06-17 12:52:00 +00:00
if ( $query -> num_rows () > 0 )
{
foreach ( $query -> result () as $row )
{
return $row -> count ;
}
}
}
2011-07-24 22:33:08 +00:00
function total_sat () {
$query = $this -> db -> query ( 'SELECT COL_SAT_NAME, COUNT( * ) as count FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_SAT_NAME != \'null\' GROUP BY COL_SAT_NAME' );
return $query ;
}
2011-06-17 12:52:00 +00:00
function total_cw () {
2011-07-24 22:33:08 +00:00
$query = $this -> db -> query ( 'SELECT COUNT( * ) as count FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_MODE = \'CW\' ' );
2011-06-17 12:52:00 +00:00
if ( $query -> num_rows () > 0 )
{
foreach ( $query -> result () as $row )
{
return $row -> count ;
}
}
}
function total_fm () {
2011-07-21 23:10:09 +00:00
$query = $this -> db -> query ( 'SELECT COUNT( * ) as count FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_MODE = \'FM\'' );
2011-06-17 12:52:00 +00:00
if ( $query -> num_rows () > 0 )
{
foreach ( $query -> result () as $row )
{
return $row -> count ;
}
}
}
function total_digi () {
2011-07-21 23:10:09 +00:00
$query = $this -> db -> query ( 'SELECT COUNT( * ) as count FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE COL_MODE != \'SSB\' or COL_MODE != \'LSB\' or COL_MODE = \'USB\' or COL_MODE = \'CW\' or COL_MODE = \'FM\'' );
2011-06-17 12:52:00 +00:00
if ( $query -> num_rows () > 0 )
{
foreach ( $query -> result () as $row )
{
return $row -> count ;
}
}
}
function total_bands () {
2011-06-18 22:38:09 +00:00
$query = $this -> db -> query ( 'SELECT DISTINCT (COL_BAND) AS band, count( * ) AS count FROM ' . $this -> config -> item ( 'table_name' ) . ' GROUP BY band ORDER BY count DESC' );
2011-06-17 12:52:00 +00:00
return $query ;
}
2011-08-17 17:15:09 +00:00
function api_search_query ( $query ) {
$time_start = microtime ( true );
$results = @ $this -> db -> query ( $query );
$time_end = microtime ( true );
$time = round ( $time_end - $time_start , 4 );
return array ( 'query' => $query , 'results' => $results , 'time' => $time );
}
2011-04-25 15:24:01 +00:00
}
2011-08-17 17:15:09 +00:00
?>