* Initial implementation of 'add' API call

* Fixed duplicate timezones (reimport sql/timezones.sql) (closes #48)
pull/106/merge
Andy Smith 2011-09-30 16:51:35 +01:00
rodzic aa70e8c9c6
commit be574ac706
8 zmienionych plików z 158 dodań i 52 usunięć

Wyświetl plik

@ -18,6 +18,13 @@ class API extends CI_Controller {
*/
}
function help()
{
$this->load->view('layout/header');
$this->load->view('api/help');
$this->load->view('layout/footer');
}
// FUNCTION: search()
// Handle search requests
/*
@ -78,7 +85,7 @@ class API extends CI_Controller {
$arguments = $this->_retrieve();
// Call the parser within the API model to build the query
$query = $this->api_model->parse($arguments);
$query = $this->api_model->select_parse($arguments);
// Execute the query, and retrieve the results
$s = $this->logbook_model->api_search_query($query);
@ -116,6 +123,50 @@ class API extends CI_Controller {
$this->load->view('api/index', $data);
}
function add()
{
// Load the API and Logbook models
$this->load->model('api_model');
$this->load->model('logbook_model');
$this->load->model('user_model');
if(!$this->user_model->authorize(3)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); }
// Retrieve the arguments from the query string
$arguments = $this->_retrieve();
// Call the parser within the API model to build the query
$query = $this->api_model->insert_parse($arguments);
# Check for guessable fields
if(!isset($query['COL_TIME_ON']))
{
$query['COL_TIME_ON'] = date("Y-m-d H:i:s", time());
}
if(!isset($query['COL_TIME_OFF']))
{
$query['COL_TIME_OFF'] = date("Y-m-d H:i:s", time());
}
$data['data']['queryInfo']['dbQuery'] = "";
$data['data']['queryInfo']['executionTime'] = 0;
if(!isset($query['COL_CALL'])) {
$data['data']['add_Result']['results'] = array(0 => array('Result' => 'EMISSINGCALL'));
} else {
$s = $this->logbook_model->api_insert_query($query);
$data['data']['queryInfo']['dbQuery'] = $s['query'];
$data['data']['queryInfo']['executionTime'] = $s['time'];
$data['data']['add_Result']['results'] = array(0 => array('Result' => $s['result_string']));
}
// Add some debugging information to the XML output
$data['data']['queryInfo']['call'] = "add";
$data['data']['queryInfo']['numResults'] = 0;
$this->load->view('api/index', $data);
}
// FUNCTION: _retrieve()
// Pull the search query arguments from the query string
private function _retrieve()

Wyświetl plik

@ -57,18 +57,31 @@ class API_Model extends CI_Model {
{
if($this->_columnName[key($this->_columnName)]['Name'] == $name)
{
return key($this->_columnName);
$a = key($this->_columnName);
reset($this->_columnName);
return $a;
}
next($this->_columnName);
}
reset($this->_columnName);
return 0;
}
// FUNCTION: string parse(array $arguments)
function insert_parse($arguments)
{
# $q = "INSERT INTO ".$this->config->item('table_name');
$f = explode(",", $arguments['query']);
$r = $this->_insert_field_translate($f);
return $r;
}
// FUNCTION: string select_parse(array $arguments)
// Converts an array of arguments into a MySQL query string
// See documentation for search() under the API controller for more details
function parse($arguments)
function select_parse($arguments)
{
// Initialise our string
$q = "SELECT ";
@ -101,9 +114,59 @@ class API_Model extends CI_Model {
// clause.
// $s and $r can be refactored into single array definitions, but during
// development it's easier to list them in this way for quick reference.
if($arguments['query'] != "")
{
$q .= " WHERE ";
$q = $this->_query_parse($q, $arguments['query']);
}
// Parse any order arguments
if($arguments['order'] != "")
{
$q .= " ORDER BY ";
$s = null;
$r = null;
$s[0] = '/(/';
$s[1] = '/)/';
$s[2] = '/([a-zA-Z0-9\-\_]+)([,\(]{1}|$)/';
$s[3] = '/\(asc\)/';
$s[4] = '/\(desc\)/';
$s[5] = '/,$/';
$r[0] = '(';
$r[1] = ')';
$r[2] = '++$1++ $2';
$r[3] = ' ASC ';
$r[4] = ' DESC ';
$r[5] = '';
$q .= preg_replace($s, $r, $arguments['order']);
}
$q = $this->_select_field_translate($q);
// Parse any limit arguments
if($arguments['limit'] != "")
{
// Add the limit arguments, removing any characters other than numbers and commas
$q .= " LIMIT " . preg_replace(array("/[^0-9\,]/","/,$/"), "", $arguments['limit']);
}
else
{
// If no limit argument is given, default to the first 20 results
$q .= " LIMIT 0,20";
}
return $q;
}
private function _query_parse($q, $qs)
{
if($qs != "")
{
$s = null;
$r = null;
// (and), becomes ' AND '
@ -148,55 +211,35 @@ class API_Model extends CI_Model {
$r[13] = '%';
// Bulk replace everything
$q .= preg_replace($s, $r, $arguments['query']);
$q .= preg_replace($s, $r, $qs);
}
// Parse any order arguments
if($arguments['order'] != "")
{
$q .= " ORDER BY ";
$s = null;
$r = null;
$s[0] = '/(/';
$s[1] = '/)/';
$s[2] = '/([a-zA-Z0-9\-\_]+)([,\(]{1}|$)/';
$s[3] = '/\(asc\)/';
$s[4] = '/\(desc\)/';
$s[5] = '/,$/';
$r[0] = '(';
$r[1] = ')';
$r[2] = '++$1++ $2';
$r[3] = ' ASC ';
$r[4] = ' DESC ';
$r[5] = '';
$q .= preg_replace($s, $r, $arguments['order']);
}
return $q;
}
private function _select_field_translate($q)
{
// Do search/replace on field names, to convert from friendly names
// to MySQL column names
while (list($key, $val) = each($this->_columnName)) {
$q = str_replace("++".$val['Name']."++", $key, $q);
}
// Parse any limit arguments
if($arguments['limit'] != "")
{
// Add the limit arguments, removing any characters other than numbers and commas
$q .= " LIMIT " . preg_replace(array("/[^0-9\,]/","/,$/"), "", $arguments['limit']);
}
else
{
// If no limit argument is given, default to the first 20 results
$q .= " LIMIT 0,20";
}
return $q;
}
private function _insert_field_translate($q)
{
// Do search/replace on field names, to convert from friendly names
// to MySQL column names
$r = array();
while (list($key, $val) = each($q)) {
$f = explode('=', $val);
$r[$this->column($f[0])] = $f[1];
}
return $r;
}
// ARRAY: $_columnName
// An array matching MySQL column names to friendly names, descriptions and types
private $_columnName = array(

Wyświetl plik

@ -55,10 +55,14 @@ class Logbook_model extends CI_Model {
'COL_MY_GRIDSQUARE' => $locator,
);
$this->add_qso($data);
}
function add_qso($data) {
// Add QSO to database
$this->db->insert($this->config->item('table_name'), $data);
}
/* Edit QSO */
function edit() {
@ -360,7 +364,16 @@ class Logbook_model extends CI_Model {
return array('query' => $query, 'results' => $results, 'time' => $time);
}
function api_insert_query($query) {
$time_start = microtime(true);
$results = $this->db->insert($this->config->item('table_name'), $query);
$time_end = microtime(true);
$time = round($time_end - $time_start, 4);
return array('query' => $this->db->queries[2], 'result_string' => $results, 'time' => $time);
}
function delete($id) {
$this->db->where('COL_PRIMARY_KEY', $id);
$this->db->delete($this->config->item('table_name'));

Wyświetl plik

@ -30,7 +30,7 @@ if(ENVIRONMENT == "development") {
$debugElement->setAttribute("dbQuery", $data['queryInfo']['dbQuery']);
$debugElement->setAttribute("clientVersion", $_SERVER['HTTP_USER_AGENT']);
$debugElement->setAttribute("requestURI", $_SERVER['REQUEST_URI']);
$debugElement->setAttribute("benchMark", $this->benchmark->marker['total_execution_time_start'].", ".$this->benchmark->marker['loading_time:_base_classes_start'].", ".$this->benchmark->marker['loading_time:_base_classes_end'].", ".$this->benchmark->marker['controller_execution_time_( api / search )_start']);
$debugElement->setAttribute("benchMark", $this->benchmark->marker['total_execution_time_start'].", ".$this->benchmark->marker['loading_time:_base_classes_start'].", ".$this->benchmark->marker['loading_time:_base_classes_end'].", ".$this->benchmark->marker['controller_execution_time_( api / add )_start']);
}
$queryElement->setAttribute("executionTime", $data['queryInfo']['executionTime']);
$queryElement->setAttribute("logbookURL", $this->config->item('base_url'));

Wyświetl plik

@ -27,6 +27,7 @@
/* Base CSS */
body { background-color: #e6e6e6; font-size: 15px; font-family: Arial, "Trebuchet MS", sans-serif; }
td { padding: 1px;}
pre { font-family: Courier New; font-size: 11px;}
.tr1 td { background:none repeat scroll 0 0 #F0FFFF; }
.partial td, .logbook td, .users td { padding: 5px; }
#subnav { height: 30px; color:#ffffff; clear: both; }
@ -184,6 +185,7 @@
<li class="ui-corner-all"><a href="<?php echo site_url('user');?>" title="Users">Users</a></li>
<li><a href="<?php echo site_url('setup');?>" title="Setup">Setup</a></li>
<li><a href="<?php echo site_url('adif/export');?>" title="ADIF Export">ADIF Export</a></li>
<li><a href="<?php echo site_url('api/help');?>" title="API">API</a></li>
<?php } ?>
</ul>
</div>
@ -208,4 +210,4 @@
</ul>
</div>
<?php } ?>
<div id="clear" class="clear"></div>
<div id="clear" class="clear"></div>

Wyświetl plik

@ -48,7 +48,7 @@
<td><a href="<?php echo site_url('user/edit')."/".$row->user_id; ?>"><?php echo $row->user_name; ?></a></td>
<td><?php echo $row->user_email; ?></td>
<td><?php $l = $this->config->item('auth_level'); echo $l[$row->user_type]; ?></td>
<td><a href="<?php echo site_url('user/delete')."/".$row->user_id; ?>">Delete</a></td>
<td><a href="<?php echo site_url('user/edit')."/".$row->user_id; ?>">Edit</a> <a href="<?php echo site_url('user/delete')."/".$row->user_id; ?>">Delete</a></td>
</tr>
<?php $i++; } ?>
</table>

Wyświetl plik

@ -1,7 +1,7 @@
<h2><?php echo $this->session->userdata('user_name')."'s profile"; ?></h2>
<div class="wrap_content user">
<table>
<table class="users">
<tr>
<td>Username</td>
<td><?php if(isset($user_name)) { echo $user_name; } ?></td>

File diff suppressed because one or more lines are too long