<?php

///////////////////////////////////////////////////////////////////////////////////////
// PHPmotion                                                http://www.phpmotion.com //
///////////////////////////////////////////////////////////////////////////////////////
// License: You are not to sell or distribute this software without permission       //
// Help and support please visit http://www.phpmotion.com                            //
// Copyright reserved                                                                //
///////////////////////////////////////////////////////////////////////////////////////

include_once ('classes/config.php');
include_once ('classes/sessions.php');
include_once ('online.php');

$page_title		= $lang_browse_members . ' ' . $lang_on .  ' ' . $site_name;

$submitted 		= $_POST['submitted'];
$which_one 		= mysql_real_escape_string($_GET['load']);
$limit 		= 20;

// this is per each template from config.inc.php advanced config settings
$page_display_small_width	= $config['general_medium_thumb_width']; // 80

//total members
$sql 			= "SELECT user_id FROM member_profile WHERE account_status = 'active'";
$members_total 	= number_format( @mysql_num_rows( mysql_query( $sql ) ) );

//CHECK IF FORM SUBMITTED
if ($_POST['submitted'] == 'yes') {

	//check for search fields and buld sql query
    	if ($_POST['firstname'] != '') {
    		$query_1 = " AND first_name = '" . mysql_real_escape_string($_POST['firstname']) ."' ";
    	}

    	if ($_POST['surname'] != '') {
      	$query_2 = "AND last_name = '" . mysql_real_escape_string($_POST['surname']) ."' ";
    	}

    	if ($_POST['username'] != '') {
      	$query_3 = "AND user_name = '" . mysql_real_escape_string($_POST['username']) ."' ";
    	}

    	if ($_POST['home_country'] != '') {
      	$query_4 = "AND current_country = '" . mysql_real_escape_string($_POST['home_country']) ."' ";
    	}

	$sqlquery = "SELECT * FROM member_profile WHERE account_status ='active'" . $query_1 . $query_2 . $query_3 . $query_4;

	//store information submitted in form inside sessions for move during pagination
      @session_start();
      @session_register('firstname');
      @session_register('surname');
      @session_register('username');
      @session_register('home_country');
      $_SESSION['firstname'] = $_POST['firstname'];
      $_SESSION['surname'] = $_POST['username'];
      $_SESSION['username'] = $_POST['username'];
      $_SESSION['home_country'] = $_POST['home_country'];

	//creat 'load' variable
	$load = 'search';
}

//CHECK IF FORM SUBMITTED BUT PAGE NEXT ON QUERY

if ($_GET['load'] == 'search') {

	//check for search fields and buld sql query
    	if ($_SESSION['firstname'] != '') {
      	$query_1 = " AND first_name = '" . mysql_real_escape_string($_SESSION["firstname"]) ."' ";
    	}

    	if ($_SESSION['surname'] != '') {
      	$query_2 = "AND last_name = '" . mysql_real_escape_string($_SESSION["surname"]) ."' ";
    	}

    	if ($_SESSION['username'] != '') {
      	$query_3 = "AND user_name = '" . mysql_real_escape_string($_SESSION["username"]) ."' ";
    	}

    	if ($_SESSION['home_country'] != '') {
      	$query_4 = "AND current_country = '" . mysql_real_escape_string($_SESSION["home_country"]) ."' ";
    	}

    	$sqlquery = "SELECT * FROM member_profile WHERE account_status ='active'" . $query_1 . $query_2 . $query_3 . $query_4;

	//creat 'load' variable
	$load = 'search';
}

//Show all members
if ( $which_one == 'all' || $which_one == '' && $_POST['submitted']=='' ) {

	//query for all members
	$sqlquery = "SELECT * FROM member_profile WHERE account_status = 'active' ORDER BY user_id ASC";

	//creat 'load' variable
	$load = 'all';
}

//Run Query as generated by above options and display result
//Pagination
$pagination		= pagination("$sqlquery", $limit);
$set_limit 		= $pagination[0]['set_limit'];
$total_pages 	= $pagination[0]['total_pages'];
$current_page 	= $pagination[0]['current_page'];
$total_records	= $pagination[0]['total_records'];
$next_page 		= $pagination[0]['next_page'];					//use in html navigation (src)
$prev_page 		= $pagination[0]['prev_page'];					//use in html navigation (src)
$nl 			= $pagination[0]['nl'];							//use in html navigation: next>>
$pl 			= $pagination[0]['pl'];							//use in html navigation: <<previous

$sql 			= $sqlquery.' LIMIT '.$set_limit.', '.$limit;
$query 		= @mysql_query($sql);
$members_full 	= array();

while ($result = mysql_fetch_array($query)) {

	$member_id 		= $result['user_id'];

    	//get picture information
      $sql1 = "SELECT * FROM pictures WHERE user_id = $member_id AND approved ='yes'";

      $result1 = @mysql_query($sql1);

      if (@mysql_num_rows($result1) == 0) {

      	// show place holder for no image uploaded by user at all
            $picture = $config['site_base_url'] . "/themes/$user_theme/images/placeholder.gif";
            $picture_array = array('picture' => $picture);

            $thumb_new_width_array	= array('thumb_new_width' => 80);
		$thumb_new_height_array	= array('thumb_new_height' => 80);


	} else {

      	$results = mysql_fetch_array($result1);
            $result1_existing_file = $results['file_name'];
            $result1_approved = $results['approved'];

            // show current picture
            $picture = $config['site_base_url'] . '/pictures/' . $result1_existing_file;
            $picture_array = array('picture' => $picture);

            //list($width, $height)	= getimagesize($picture);
            list($width, $height)	= getimagesize( "pictures/$result1_existing_file" );


           	if ( $width > $page_display_small_width ) {
			$large_img_ratio	= $width/$height;

			if ( $large_img_ratio > 1 ) {
           			$new_smallwidth	= $page_display_small_width;
            		$new_smallheight	= $page_display_small_width / $large_img_ratio;
            	} else {
            		$new_smallheight	= $page_display_small_width;
            		$new_smallwidth	= $page_display_small_width * $large_img_ratio;
			}
		}

		if ( $height > $page_display_small_height ) {
			$large_img_ratio	= $width/$height;

           		if ( $large_img_ratio > 1 ) {
				$new_smallwidth	= $page_display_small_width;
				$new_smallheight	= $page_display_small_width / $large_img_ratio;
			} else {
				$new_smallheight	= $page_display_small_width;
				$new_smallwidth	= $page_display_small_width * $large_img_ratio;
			}
		}

		$new_smallwidth		= floor($new_smallwidth);
		$new_smallheight		= floor($new_smallheight);
		$thumb_new_width_array	= array('thumb_new_width' => $new_smallwidth);
		$thumb_new_height_array	= array('thumb_new_height' => $new_smallheight);

	}

	// get videos by member
      $sql = "SELECT * FROM videos WHERE user_id = $member_id AND approved='yes'";
      $membersvideos = mysql_num_rows(mysql_query($sql));
      $membersvideos_array = array('membersvideos' => $membersvideos);

      // getmembers favs
      $sql = "SELECT * FROM favorites WHERE user_id = $member_id";
      $membersfavs = mysql_num_rows(mysql_query($sql));
      $membersfavs_array = array('membersfavs' => $membersfavs);

      // getmembers friends
      $sql = "SELECT * FROM friends WHERE user_id = $member_id AND invitation_status = 'accepted' OR friends_id = $member_id AND invitation_status = 'accepted'";
      $membersfriends = mysql_num_rows(mysql_query($sql));
      $membersfriends_array = array('membersfriends' => $membersfriends);

      // get last seen / active
      $sql				= "SELECT * FROM member_profile WHERE user_id = $member_id AND account_status = 'active'";
      $db_date			= $result['last_seen'];
      $change_date 		= dateTimeDiff($db_date);
      $result['last_seen'] 	= $change_date;

      if ( $db_date == '0000-00-00 00:00:00' ) $result['last_seen'] = $lang_never;

      $last_seen_array		= array('member_last_seen' => $result['last_seen']);

	// merge arrays
      $merged_array = array_merge($membersvideos_array, $result, $membersfavs_array, $membersfriends_array, $picture_array, $thumb_new_width_array, $thumb_new_height_array, $last_seen_array);
      $members_full[] = $merged_array;

}// end while


//set condition for hidding certain blocks (e.g "no emails to list")
if (empty($members_full)) {
	$show_v = 1;

} else {
	$show_v = 2;
}

//create name fro display on see more page
$see_even_more_title 	= $members_username . ' - ' . $config['word_videos'];
$see_even_more_out_link	= members/$members_username;


//PAGINATION PLUS >> start  -- reusable code

$url = 'people';												//the url to be put in links - EDIT ME

$additional_url_variable = '/'.$load.'/';								//add information in query string here , e.g. '&load=groups&friends=all' - EDIT ME
//$hide_numbering = true;
include_once('includes/pagination.inc.php');

//PAGINATION PLUS >> end

$template		= "themes/$user_theme/templates/main_1.htm";
$inner_template1 	= "themes/$user_theme/templates/inner_see_members.htm";			//middle of page
$TBS 			= new clsTinyButStrong;
$TBS->NoErr		= true;											// no more error message displayed.

$TBS->LoadTemplate("$template");
$TBS->MergeBlock('mp', $members_full);

$TBS->Render	= TBS_OUTPUT;
$TBS->Show();
@mysql_close();

die();

?>