<commit_before>//get calling params
var args = arguments[0];
var siteID = args.siteID;

// Initialize Variables
var photo;
var utmEasting;
var utmNorthing;
var utmZone;

//set stake orientation text
var stakeText;
if ($.pickStake.index == 0) {
	stakeText = "Top Left / Bottom Right";
} else {
	stakeText = "Top Right / Bottom Left";
}
	
//validate form before inserting to database
function doneBtn(e){
	//disable button for 1 second to prevent double entry
	e.source.enabled = false;
	setTimeout(function(){ e.source.enabled = true; },1000);
	
	// check that the following (required) fields are not empty
	Ti.App.fireEvent('transectChange');
	Ti.App.fireEvent('surveyorChange');
	Ti.App.fireEvent('plotDistanceChange');
	
	//check photo exists and a stake orientation has been selected
	if(photo == null && $.pickStake.index == null){
		$.photoError.visible = true;
		$.photoError.text = "* Please take a photo";
		$.stakeError.visible = true;
		$.stakeError.text = "* Must select a stake orientation";
		return;
	}else if (photo == null){
		$.photoError.visible = true;
		$.photoError.text = "* Please take a photo";
		return;
	}else if($.pickStake.index == null){
		$.stakeError.visible = true;
		$.stakeError.text = "* Must select a stake orientation";
		return;
	}else{
		$.photoError.visible = false;
		$.stakeError.visible = false;
	}
		// Name and Save Photo
		var photoName = savePhoto(photo);
		
		try{
			//Connect to database
			var db = Ti.Database.open('ltemaDB');
			
			//add photo name to media table
			db.execute( 'INSERT INTO media (media_name) VALUES (?)', photoName);
			
			//get the id of the last row inserted into the database - *not sure if this is acceptable sql code to use?
			var results = db.execute('SELECT last_insert_rowid() as mediaID');
			var mediaID = results.fieldByName('mediaID');
			
			//Insert Query - add row to transect table
			db.execute(	'INSERT INTO transect (transect_name,surveyor,other_surveyors,plot_distance,stake_orientation,utm_zone,utm_easting,utm_northing,comments,site_id,media_id) VALUES (?,?,?,?,?,?,?,?,?,?,?)', 
						$.tsctName.value, $.srvyName.value, $.otherSrvyName.value, $.plotDist.value, stakeText, utmZone, utmEasting, utmNorthing, $.comments.value, siteID, mediaID);
						
		}catch(e){
			
		}finally{	
			//close the result set
			results.close();	
			//Close the database
			db.close();
			//refresh and close
			Ti.App.fireEvent("app:refreshTransects");
			$.addTransect.close();
		}
	}

function takePhoto() {		
	//remove photo error msg
	$.photoError.visible = false;
	
	//call camera module and set thumbnail
	var pic = require('camera');
	pic.getPhoto(function(myPhoto, UTMEasting, UTMNorthing, n_UTMZone) {
		//Set thumbnail
		$.transectThumbnail.image = myPhoto;
		
		//set variables with values
		photo = myPhoto;
		utmEasting = UTMEasting;
		utmNorthing = UTMNorthing;
		utmZone = n_UTMZone;
		
		//alert("UTMEasting: " + UTMEasting + "\nUTMNorthing: " + UTMNorthing + "\nUTMZone: " + n_UTMZone);
	});
}

//Name and save photo to filesystem - do this when done btn is pressed
function savePhoto(photo){
	//get the name of the current site survery, year, park
	try{
		//Connect to database
		var db = Ti.Database.open('ltemaDB');
		
		//Query - Retrieve site survery, year, park
		var rows = db.execute('SELECT s.year, p.protocol_name, prk.park_name \
						FROM site_survey s, protocol p, park prk \
						WHERE s.protocol_id = p.protocol_id \
						AND s.park_id = prk.park_id \
						AND site_id = ?', siteID);
		
		//Get requested data from each row in table
		
		var year = rows.fieldByName('year');
		var protocolName = rows.fieldByName('protocol_name');
		var parkName = rows.fieldByName('park_name');
	
		//Name the directory
		var dir = year + ' - ' + protocolName + ' - ' + parkName; 
		//get the photo
		var img = photo;
		
		//name the photo  (timestamp - utc in ms)
		var timestamp = new Date().getTime();
		var filename = "T" + timestamp;
		
		// Create image Directory for site
		var imageDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, dir);
		if (! imageDir.exists()) {
			imageDir.createDirectory();
		}
		
		// .resolve() provides the resolved native path for the directory.
		var imageFile = Ti.Filesystem.getFile(imageDir.resolve(), filename + '.png');
		imageFile.write(img);
		
		var path = filename + '.png'; 

	} catch(e) {
		//Ti.App.fireEvent("app:dataBaseError", e);
	} finally {
		rows.close();
		db.close();<commit_msg>Code cleanup<commit_after>//get calling params
var args = arguments[0];
var siteID = args.siteID;

// Initialize Variables
var photo;
var utmEasting;
var utmNorthing;
var utmZone;

//set stake orientation text
var stakeText;
if ($.pickStake.index == 0) {
	stakeText = "Top Left / Bottom Right";
} else {
	stakeText = "Top Right / Bottom Left";
}
	
//validate form before inserting to database
function doneBtn(e){
	//disable button for 1 second to prevent double entry
	e.source.enabled = false;
	setTimeout(function(){ e.source.enabled = true; },1000);
	
	// check that the following (required) fields are not empty
	Ti.App.fireEvent('transectChange');
	Ti.App.fireEvent('surveyorChange');
	Ti.App.fireEvent('plotDistanceChange');
	
	//check photo exists and a stake orientation has been selected
	if(photo == null && $.pickStake.index == null){
		$.photoError.visible = true;
		$.photoError.text = "* Please take a photo";
		$.stakeError.visible = true;
		$.stakeError.text = "* Must select a stake orientation";
		return;
	}else if (photo == null){
		$.photoError.visible = true;
		$.photoError.text = "* Please take a photo";
		return;
	}else if($.pickStake.index == null){
		$.stakeError.visible = true;
		$.stakeError.text = "* Must select a stake orientation";
		return;
	}else{
		$.photoError.visible = false;
		$.stakeError.visible = false;
	}
		// Name and Save Photo
		var photoName = savePhoto(photo);
		
		try{
			//Connect to database
			var db = Ti.Database.open('ltemaDB');
			
			//add photo name to media table
			db.execute( 'INSERT INTO media (media_name) VALUES (?)', photoName);
			
			//get the id of the last row inserted into the database - *not sure if this is acceptable sql code to use?
			var results = db.execute('SELECT last_insert_rowid() as mediaID');
			var mediaID = results.fieldByName('mediaID');
			
			//Insert Query - add row to transect table
			db.execute(	'INSERT INTO transect (transect_name,surveyor,other_surveyors,plot_distance,stake_orientation,utm_zone,utm_easting,utm_northing,comments,site_id,media_id) VALUES (?,?,?,?,?,?,?,?,?,?,?)', 
						$.tsctName.value, $.srvyName.value, $.otherSrvyName.value, $.plotDist.value, stakeText, utmZone, utmEasting, utmNorthing, $.comments.value, siteID, mediaID);
						
		}catch(e){
			Ti.App.fireEvent("app:dataBaseError", e);
		}finally{	
			//close the result set
			results.close();	
			//Close the database
			db.close();
			//refresh and close
			Ti.App.fireEvent("app:refreshTransects");
			$.addTransect.close();
		}
	}

function takePhoto() {		
	//remove photo error msg
	$.photoError.visible = false;
	
	//call camera module and set thumbnail
	var pic = require('camera');
	pic.getPhoto(function(myPhoto, UTMEasting, UTMNorthing, n_UTMZone) {
		//Set thumbnail
		$.transectThumbnail.image = myPhoto;
		
		//set variables with values
		photo = myPhoto;
		utmEasting = UTMEasting;
		utmNorthing = UTMNorthing;
		utmZone = n_UTMZone;
		
		//alert("UTMEasting: " + UTMEasting + "\nUTMNorthing: " + UTMNorthing + "\nUTMZone: " + n_UTMZone);
	});
}

//Name and save photo to filesystem - do this when done btn is pressed
function savePhoto(photo){
	//get the name of the current site survery, year, park
	try{
		//Connect to database
		var db = Ti.Database.open('ltemaDB');
		
		//Query - Retrieve site survery, year, park
		var rows = db.execute('SELECT s.year, p.protocol_name, prk.park_name \
						FROM site_survey s, protocol p, park prk \
						WHERE s.protocol_id = p.protocol_id \
						AND s.park_id = prk.park_id \
						AND site_id = ?', siteID);
		
		//Get requested data from each row in table
		
		var year = rows.fieldByName('year');
		var protocolName = rows.fieldByName('protocol_name');
		var parkName = rows.fieldByName('park_name');
	} catch(e) {
		Ti.App.fireEvent("app:dataBaseError", e);
	} finally {
		rows.close();
		db.close();
	}
	
	//Name the directory
	var dir = year + ' - ' + protocolName + ' - ' + parkName; 
	//get the photo
	var img = photo;
	
	//name the photo  (timestamp - utc in ms)
	var timestamp = new Date().getTime();
	var filename = "T" + timestamp;
	
	try {
		// Create image Directory for site
		var imageDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, dir);
		if (! imageDir.exists()) {
			imageDir.createDirectory();
		}
		
		// .resolve() provides the resolved native path for the directory.
		var imageFile = Ti.Filesystem.getFile(imageDir.resolve(), filename + '.png');
		imageFile.write(img);
		
		var path = filename + '.png'; 

	} catch(e) {
		Ti.App.fireEvent("app:fileSystemError", e);
	} finally {
		imageDir = null;
		imageFile = null;
{'fraction_non_alphanumeric': 0.13599735070096036, 'fraction_numerical': 0.0015454244397836406, 'mean_word_length': 3.5368052078117174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3719893', 'n_tokens_mistral': 3353, 'n_tokens_neox': 2948, 'n_words': 843}