<?php
	require_once( $GLOBALS["site_root"] . "/includes/models/Globals.inc.php" );

	class AllUsersModel {
		public function getOnlineUsers( $type = 0 ) {
			$session_timeout = 10*60;	// $GLOBALS["session_timeout"];
			switch ($GLOBALS["model_version"]) {
			case 2.0:
				switch ($type) {
				case 0:$where="";break;
				default:$where=" and user_id is not null";break;
				}
				$query = sprintf( "select count(*) as num from fo_sessions where fo_session_expires>%d%s" , time() - $session_timeout , $where );
			case 3.0:
				switch ($type) {
				case 0:$where="";break;
				default:$where=" and user_id is not null";break;
				}
				$query = sprintf( "select count(*) as num from sessions where session_expires>%d%s" , time() - $session_timeout , $where );
				break;
			}
			$row = FNTH_db_get_row( $query );
			return $row["num"];
		}

		public function getUsersOnlineState( $user_ids ) {
			if (!is_array( $user_ids )) return array();
			if (empty( $user_ids )) return array();
			$s = implode( "," , $user_ids );
			$query = sprintf( "select user_id from sessions where user_id in (%s) and session_expires>%d" , $s , time()-$GLOBALS["session_timeout"] );
			$ds = FNTH_db_get_resultset( $query );
			$onlines = array(); foreach ($ds as $row) $onlines[$row["user_id"]]=true;
			$retval = array();
			foreach ($user_ids as $user_id) $retval[$user_id]=isset( $onlines[$user_id] );
			return $retval;
		}

		public function getActiveMembers( $member_type = "" , $lang = "" , $active ) {
			if (empty( $lang )) $lang = $GLOBALS["language"];
			$filters = array();
			if ($active)
				$filters["active"]=sprintf( "expires_tstamp>now()" );
			if (!empty( $member_type ))
				$filters["type"]=sprintf( "member_type='%s'" , FNTH_db_addslashes( $member_type ) );

			if ($lang!="hu") {
				require_once( $GLOBALS["site_root"]."/includes/models/ExtendedProperties.inc.php" );
				$epm = new ExtendedPropertiesModel();
				$eprop_id = $epm->getEPropIdFromName( "use_langsite" );
				$site_language_id = $GLOBALS["langcode"];

				$filters["show_foreign"]=sprintf( "member_id in (select member_id from member_extended_properties where extended_property_id=%d and language_id=%d and value='on')",
					$eprop_id,
					$site_language_id );
			}
			$where_clause = implode( " and " , $filters );
			$query = sprintf( "select count(*) as num from members where %s" , $where_clause );
			return FNTH_db_get_field( $query , "num" );
		}

		public function getAllMembers( $member_type = "" ) {
			$filters = array();
			$filters["deleted"]=sprintf( "current_status not in ('%s,%s')" , STATUS_NEW_USER_DELETED , STATUS_DELETED );
			$filters["deleted2"]=sprintf( "not is_deleted" );
			if (!empty( $member_type ))
				$filters["type"]=sprintf( "member_type='%s'" , FNTH_db_addslashes( $member_type ) );

			$where_clause = (!empty( $filters )) ? sprintf( " where %s", implode( " and " , $filters ) ) : "";
			$query = sprintf( "select count(*) as num from members %s" , $where_clause );
			return FNTH_db_get_field( $query , "num" );

		}

		public function getActiveFreelancers( $active = true , $lang = "" ) {
			return $this->getActiveMembers( "freelancer" , $lang , $active );
		}
		
		public function getActiveAgencies( $active = true , $lang = "" ) {
			return $this->getActiveMembers( "agency" , $lang , $active );
		}

		public function getActiveParttimers( $active = true , $lang = "" ) {
			return $this->getActiveMembers( "parttime" , $lang , $active );
		}
	}
?>
