Skip to content

Commit

Permalink
Fixed broken date submitted / date occurrence fields in upload form a…
Browse files Browse the repository at this point in the history
…nd api. No time zone support yet but at least dates are now consistent and not downright broken
  • Loading branch information
infinitesunrise committed Oct 23, 2016
1 parent 8b8f96b commit 1753960
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
11 changes: 8 additions & 3 deletions api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ function upload(){
$image_size = (isset($_FILES['image']) ? $image['size'] : '');
$plate = (isset($_POST['plate']) ? $_POST['plate'] : '');
$state = (isset($_POST['state']) ? $_POST['state'] : '');
$date = (isset($_POST['date']) ? $_POST['date'] : '');
$date_occurrence = (isset($_POST['date']) ? $_POST['date'] : '');
$date_added = date('U');
$gps_latitude = (isset($_POST['gps_latitude']) ? $_POST['gps_latitude'] : '');
$gps_longitude = (isset($_POST['gps_longitude']) ? $_POST['gps_longitude'] : '');
$street1 = (isset($_POST['street1']) ? $_POST['street1'] : '');
Expand All @@ -278,17 +279,21 @@ function upload(){
$upload->image = array('name' => $image_name, 'type' => $image_type, 'error' => $image_error, 'size' => $image_size);
$upload->plate = $plate;
$upload->state = $state;
$upload->date = $date;
$upload->date_occurrence = $date_occurrence;
$upload->date_added = $date_added;
$upload->gps_latitude = $gps_latitude;
$upload->gps_longitude = $gps_longitude;
$upload->street1 = $street1;
$upload->street2 = $street2;
$upload->description = $description;

//error_log($upload->date);

$result = new_upload($image,
$plate,
$state,
$date,
$date_occurrence,
$date_added,
$gps_latitude,
$gps_longitude,
$street1,
Expand Down
70 changes: 42 additions & 28 deletions index_actual.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,26 +811,39 @@ function limit_text() {

function initialize_datetimepicker() {
var date = new Date();
$('#datetimepicker').datetimepicker({ format:'unixtime' });
$('#datetimepicker').datetimepicker({ format:'n/j/Y g:iA' });
$('#datetimepicker').attr('unixtime', Date.parse(date)/1000 );
$('#datetimepicker').val(unixtime_to_pretty($('#datetimepicker').attr('unixtime')));
$('#datetimepicker').on('change', function() {
$('#datetimepicker').attr('unixtime', $('#datetimepicker').val() );
$('#datetimepicker').val(unixtime_to_pretty($('#datetimepicker').val()));
//$('#datetimepicker').val(unixtime_to_pretty($('#datetimepicker').attr('unixtime')));
//$('#datetimepicker').on('change', function() {
// console.log('changed. current value: ' + $('#datetimepicker').val());
// $('#datetimepicker').attr('unixtime', $('#datetimepicker').val() );
// console.log('unixtime attr: ' + $('#datetimepicker').attr('unixtime'));
// $('#datetimepicker').val(unixtime_to_pretty($('#datetimepicker').val()));
//});
$('#datetimepicker').on('change', function(){
console.log('changed. current value: ' + $('#datetimepicker').val());
$('#datetimepicker').attr('unixtime', pretty_to_unixtime($('#datetimepicker').val()) );
console.log('unixtime attr: ' + $('#datetimepicker').attr('unixtime'));
});
}

function unixtime_to_pretty(unixtime){
var date = new Date(unixtime * 1000);
var date_string = (date.getMonth()+1) + '/' + date.getDate() + '/' + date.getFullYear() + ' ';
if (date.getHours() == 0){ date_string += '12:'; }
else if (date.getHours() <= 12){ date_string += date.getHours() + ':'; }
else { date_string += (date.getHours()-11) + ':'; }
if (date.getMinutes() < 10){ date_string += '0' + date.getMinutes(); }
else { date_string += date.getMinutes(); }
if (date.getHours() < 12){ date_string += 'AM'; }
else { date_string += 'PM' }
return date_string;
var pretty = (date.getMonth()+1) + '/' + date.getDate() + '/' + date.getFullYear() + ' ';
if (date.getHours() == 0){ pretty += '12:'; }
else if (date.getHours() <= 12){ pretty += date.getHours() + ':'; }
else { pretty += (date.getHours()-11) + ':'; }
if (date.getMinutes() < 10){ pretty += '0' + date.getMinutes(); }
else { pretty += date.getMinutes(); }
if (date.getHours() < 12){ pretty += 'AM'; }
else { pretty += 'PM' }
return pretty;
}

function pretty_to_unixtime(pretty){
var isotime = pretty.split(" ")[0].replace(/\//g,'-') + ' ' + capturetime.split(" ")[1].replace('AM','').replace('PM','') + ':00';
var unixtime = Date.parse(isotime)/1000;
return unixtime;
}

function fill_plate_and_state(){
Expand Down Expand Up @@ -938,6 +951,7 @@ function fill_date_and_gps(e) {
var isotime = capturetime.split(" ")[0].replace(/:/g,'-') + 'T' + capturetime.split(" ")[1];
var unixtime = Date.parse(isotime)/1000;
$('#datetimepicker').val(unixtime_to_pretty(unixtime));
$('#datetimepicker').attr('unixtime', unixtime );
}
});
}
Expand Down Expand Up @@ -974,7 +988,7 @@ function submit_form() {
formData.append( 'image', $('#image_submission')[0].files[0] );
formData.append( 'plate', $('#plate').val() );
formData.append( 'state', $('#state').val() );
formData.append( 'date', $('#datetimepicker').attr('unixtime') /*$('#datetimepicker').val()*/ );
formData.append( 'date', $('#datetimepicker').attr('unixtime') );
formData.append( 'gps_latitude', $('#latitude').val() );
formData.append( 'gps_longitude', $('#longitude').val() );
formData.append( 'street1', $('#street1').val() );
Expand Down Expand Up @@ -1005,7 +1019,8 @@ function submit_form() {
+ '&nbsp;&nbsp;&nbsp;&nbsp;size: ' + Math.round(response['upload']['image']['size'] / 1000) + ' kb<br/>'
+ 'plate: ' + response['upload']['plate'] + '<br/>'
+ 'state: ' + response['upload']['state'] + '<br/>'
+ 'date: ' + response['upload']['date'] + '<br/>'
+ 'date of occurrence: ' + unixtime_to_pretty(response['upload']['date_occurrence']) + '<br/>'
+ 'date of upload: ' + unixtime_to_pretty(response['upload']['date_added']) + '<br/>'
+ 'gps: ' + response['upload']['gps_latitude'] + ' / ' + response['upload']['gps_longitude'] + '<br/>'
+ 'street 1: ' + response['upload']['street1'] + '<br/>'
+ 'street 2: ' + response['upload']['street2'] + '<br/>'
Expand Down Expand Up @@ -1036,7 +1051,8 @@ function submit_form() {
+ '&nbsp;&nbsp;&nbsp;&nbsp;size: ' + Math.round(response['upload']['image']['size'] / 1000) + ' kb<br/>'
+ 'plate: ' + response['upload']['plate'] + '<br/>'
+ 'state: ' + response['upload']['state'] + '<br/>'
+ 'date: ' + response['upload']['date'] + '<br/>'
+ 'date of occurrence: ' + unixtime_to_pretty(response['upload']['date_occurrence']) + '<br/>'
+ 'date of upload: ' + unixtime_to_pretty(response['upload']['date_added']) + '<br/>'
+ 'gps: ' + response['upload']['gps_latitude'] + ' / ' + response['upload']['gps_longitude'] + '<br/>'
+ 'street 1: ' + response['upload']['street1'] + '<br/>'
+ 'street 2: ' + response['upload']['street2'] + '<br/>'
Expand Down Expand Up @@ -1156,9 +1172,9 @@ function initialize_upload_view(){
include $config_folder . '/google_style.php';
echo ";\n"; }
?>
var extra = '\\' + config.google_extra_layer + '\\';
try { var tiles = new L.Google('ROADMAP', { mapOptions: { styles: options } }, extra); }
catch (err) { console.log(err); }
var extra = '\\' + config.google_extra_layer + '\\';
try { var tiles = new L.Google('ROADMAP', { mapOptions: { styles: options } }, extra); }
catch (err) { console.log(err); }
}
else if (config.use_bing) {
var imagerySet = config.bing_imagery;
Expand All @@ -1169,14 +1185,12 @@ function initialize_upload_view(){
else if (config.use_mapboxgljs){
mapboxgl.accessToken = config.mapbox_key;
var style_url = (config.mobile && config.mapbox_mobile_style_url) ? config.mapbox_mobile_style_url : config.mapbox_style_url;
if(config.mobile && config.mapbox_mobile_style_url){
submit_map = new mapboxgl.Map({
container: 'submit_map',
style: style_url,
center: [config.center_long, config.center_lat],
zoom: config.zoom
});
}
submit_map = new mapboxgl.Map({
container: 'submit_map',
style: style_url,
center: [config.center_long, config.center_lat],
zoom: config.zoom
});
}
else {
try { var tiles2 = L.tileLayer(config.map_url, {maxZoom: 20}); }
Expand Down
25 changes: 14 additions & 11 deletions submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

function new_upload($image,
$plate,
$state,
$time,
$state,
$date_occurrence,
$date_added,
$gps_latitude,
$gps_longitude,
$street1,
$street2,
$description)
{
require 'admin/config_pointer.php';

//error_log($time);

if (empty($image))
{ return array('error' => 'Submissions without an image attached are currently not accepted.'); }
Expand All @@ -36,10 +35,14 @@ function new_upload($image,
{ return array('error' => 'State field must be a string'); }

//Ensure unix timestamp
if (!is_timestamp($time))
{ error_log('Trying to convert time string: ' . $time . ' to unix...'); $time = strtotime($time); error_log('And now its: ' . $time);}
if (!$time)
{ return array('error' => 'Unacceptable time value.'); }
if (!is_timestamp($date_occurrence))
{ $date_occurrence = strtotime($date_occurrence); }
if (!$date_occurrence)
{ return array('error' => 'Unacceptable date value.'); }
$date_occurrence = date('Y-m-d H:i:s', $date_occurrence);
$date_added = date('Y-m-d H:i:s', $date_added);

//error_log($date_occurrence);

if (empty($gps_latitude) || empty($gps_longitude))
{ return array('error' => 'Both latitude and longitude are required.'); }
Expand Down Expand Up @@ -81,9 +84,9 @@ function new_upload($image,
}

$stmt = $connection->prepare("INSERT INTO cibl_queue (
increment, url, plate, state, date_occurrence, gps_lat, gps_long, street1, street2, description)
VALUES (?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param('isssiddsss', $increment, $url, $plate, $state, $time, $gps_latitude, $gps_longitude, $street1, $street2, $description);
increment, url, plate, state, date_occurrence, date_added, gps_lat, gps_long, street1, street2, description)
VALUES (?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param('isssssddsss', $increment, $url, $plate, $state, $date_occurrence, $date_added, $gps_latitude, $gps_longitude, $street1, $street2, $description);
$result = $stmt->execute();
$stmt->close();

Expand Down

0 comments on commit 1753960

Please sign in to comment.