<?php

	include "library.php";


	$cxn = start_code();

	$cxn_common = db_common_login();


	ini_set( "post_max_size", "16M" );	
	ini_set( "upload_max_filesize", "16M" );	
		
	
			/* get variables */

	$id = val( $_SESSION, "id" );


    if (val( $_SESSION, "logged_in_as_guest" ) == "true")
    {
        echo "You are logged in as the guest user. You must be logged in to your own account to access the photos screen.<br><br>";
            
	    echo show_home_link();
	            
        exit();
    }

		/* if the user is not logged in them direct them to the login page then back to here */

	if ($id == "")
	{
		$_SESSION['nextpage'] = url_encode( "manage_photos.php" );

	    jump_to_page( val( $_SESSION, "login_page" ) );		/* must happen before print output */

		exit();
	}

		/* start the page  */

	page_header( "Photos", $cxn, $cxn_common );


		/* retrieve existing photos */

	$query = "SELECT * FROM photos WHERE member_id = ".$id." ORDER BY id";

	$result = run_query( $query, $cxn );

	if ( query_num_rows($result) == 0)
		echo black( "No photos currently stored" );

	echo "<br><table><tr>";

	for ($i=0;$i <  query_num_rows($result);$i++)
	{
		$data = get_row( $result );

		if ($data['primary_photo'] == "yes")
			echo "<td align='center'>".red( "<b>Primary Photo</b>" )."</td>";
		else
			echo "<td align='center'></td>";
	}

	echo "</tr><tr>";

	$result = run_query( $query, $cxn );

	for ($i=0;$i <  query_num_rows($result);$i++)
	{
		$data = get_row( $result );

		echo "<td align='center'>";

		show_photo( $id, 120, $data['photo_number'], true, false, $cxn );

		echo "</td>";
	}


	echo "</tr><tr>";

	$result = run_query( $query, $cxn );

	for ($i=0;$i <  query_num_rows($result);$i++)
	{
		$data = get_row( $result );

		echo "<td align='center'>";

		show_green_button( "Rotate", 100, "rotate_photo_yes_no.php", "photonumber", $data['photo_number'] );

		echo "</td>";
	}


	echo "</tr><tr>";

	$result = run_query( $query, $cxn );

	for ($i=0;$i <  query_num_rows($result);$i++)
	{
		$data = get_row( $result );

		echo "<td align='center'>";

		show_green_button( "Delete", 100, "delete_photo.php", "photonumber", $data['photo_number'] );

		echo "</td>";
	}

	echo "</tr><tr>";

	$result = run_query( $query, $cxn );

	for ($i=0;$i <  query_num_rows($result);$i++)
	{
		$data = get_row( $result );

		echo "<td align='center'>";

		show_green_button( "Set as Primary", 140, "set_as_primary.php", "photonumber", $data['photo_number'] );

		echo "</td>";
	}

	echo "</tr></table><br><br><br><br>";


		// detect which browser is in use as different browsers have different text on the 'choose file' button

	$user_agent = val( $_SERVER, "HTTP_USER_AGENT" );

//echo $user_agent."<br>";	

	if (strpos( $user_agent, "Edge" ) !== false)		// test this first because it returns multiple browser entries
		$upload_text = "Browse";
	else
	if (strpos( $user_agent, "Chrome" ) !== false)
		$upload_text = "Choose File";
	else
	if (strpos( $user_agent, "Firefox" ) !== false)
		$upload_text = "Browse";
	else
	if (strpos( $user_agent, "MSIE" ) !== false)
		$upload_text = "Browse";
	else
	if (strpos( $user_agent, "WOW" ) !== false)
		$upload_text = "Browse";
	else
		$upload_text = "Browse/Choose File";


	echo "<br>".blue( "Upload a new photo. Please click on '".$upload_text."' then click on 'Upload'" )."<br><br><br>";

	echo "<table>";

	echo "<form name='form' enctype='multipart/form-data' action='".url_encode( "upload_photo.php" )."' method='POST'>";

	echo "<tr><td valign='center'>".black( "<b>Select photo file</b>" )."</td><td valign='center'><input type='file' name='file' id='file' size='60'></td></tr>";

	echo "<tr>";

	echo "<tr><td></td><td><br><br>";

    $text = "Upload";
    $size = 90;
    $html_form_name = "form";
    
	$this_size = $size * 0.85;

    echo "<progress id='progressbar' value='0' max='100'></progress><br><br>";

    echo "<script type='text/javascript'>";
    
    echo "  function uploadFile()";
    echo "  {";
    echo "      var fd = new FormData();";
    echo "      fd.append('file', document.getElementById('file').files[0]);";
    echo "      if (document.getElementById('file').value == '')";
    echo "          alert ('Select a file to upload by clicking on the \'Browse\' / \'Choose file\' button then click \'Upload\'');";
    echo "      else";
    echo "      {";
    echo "          var xhr = new XMLHttpRequest();";
    echo "          xhr.upload.addEventListener('progress', uploadProgress, false);";
    echo "          xhr.addEventListener('load', uploadComplete, false);";
    echo "          xhr.addEventListener('error', uploadFailed, false);";
    echo "          xhr.addEventListener('abort', uploadCanceled, false);";
    echo "          xhr.open('POST', 'upload_photo.php');";
    echo "          xhr.send(fd);";
    echo "      }";
    echo "  }";
    
    echo "  function uploadProgress(evt)";
    echo "  {";
    echo "      if (evt.lengthComputable)";
    echo "      {";
    echo "          var percentComplete = Math.round((evt.loaded / evt.total) * 100);";
    echo "          document.getElementById('progressbar').value = percentComplete;";
    echo "      }";
    echo "      else ";
    echo "      {";
    
    //          document.getElementById('progressbar').innerHTML = 'unable to compute';
    
    echo "      }";
    echo "  }";
    
    echo "      function uploadComplete(evt)";
    echo "      {";
    echo "              alert( 'Photo upload complete' );";
    echo "              window.location.href = 'manage_photos.php';";
    echo "      }";
    
    echo "      function uploadFailed(evt)"; 
    echo "      {";
    echo "          alert('There was an error attempting to upload the file.');";
    echo "      }";
    
    echo "      function uploadCanceled(evt)";
    echo "      {";
    echo "          alert('The upload has been canceled by the user or the browser dropped the connection.');";
    echo "      }";
    
    echo "</script>";
        

        // NOTE: when updating the button style these lines of code will need to be updated too
        
	if (! low_res_screen())
		echo "<div onclick='uploadFile()' class='greenbuttondesktop' style='line-height: 25px; width:".$this_size.";' align='center'>".$text."</div> ";			
	else
		echo "<div onclick='uploadFile()' class='greenbuttonmobile' style='width:".$this_size.";' align='center'>".$text."</div> ";						


	echo "</td></tr>";

	echo "</tr>";

	form_end();

	echo "<br><br>".blue( "Requirements for photos" )."<br><br>";
	echo "<ul>";
	echo "<li>".black( "Files must be in JPG format" )."</li><br>";
	echo "<li>".black( "Photos must be of yourself or subjects of interest" )."</li><br>";
	echo "<li>".black( "Photos must be appropriate for the site" )."</li><br>";
	echo "<li>".black( "If you upload a photo of a group of people please identify in your profile text which person in the photo is you" )."</li><br>";
	echo "<li>".black( "You may upload as many photos as you wish" )."</li><br>";
	echo "</ul>";

//	echo "<br>".red( "The upload process may take several minutes. Please do not close this screen after clicking 'upload' until the 'Photo uploaded successfully' message appears" )."<br><br>";

?>