<?php 
//+-----------------------------------------------------+//
//+ Copyright Floopo.com
//+ common.php
//+ Common functions
//+-----------------------------------------------------+//

//____________________________________________________________MEMBERS GLOBAL DATA_____________________________________________________________

class MembersGlobalData {

    var $user_name = null;//set a null user to avaiod breaking sql.
    var $valid_user = false;//assume not valid user

    function MembersGlobalData($user_name) {

        //Check if user exists
        if(!$user_name) {
            $this->valid_user = false;
            $this->user_name = null;
        }
        else {

            $sql = "SELECT user_id FROM member_profile WHERE user_name = '$user_name'";
            if(mysql_num_rows(mysql_query($sql)) > 0) {
                $this->valid_user = true;
                $this->user_name = $user_name;
            }
        }
    }

    //__Get Number of People a Member is Following___________________________________
    function GetFollowing() {//requires dbase connection
        $sql = "SELECT following.user_name, member_profile.avatar
        FROM following
        JOIN member_profile
        ON following.user_name = member_profile.user_name
        WHERE following.followers_user_name = '$this->user_name'";
        $query = mysql_query($sql);
        $following = array();
        while($result = mysql_fetch_array($query)) {
            $following[] = $result;
        }
        return $following;
    }

    //__Get Number of People Following a Member___________
    function GetFollowers() {//requires dbase connection
        $sql = "SELECT following.followers_user_name, member_profile.avatar
        FROM following
        JOIN member_profile
        ON following.followers_user_name = member_profile.user_name
        WHERE following.user_name = '$this->user_name'";
        $query = mysql_query($sql);
        $followers = array();
        while($result = mysql_fetch_array($query)) {
            $followers[] = $result;
        }
        return $followers;
    }

    //__Count Members Updates_____________________________
    function CountMembersUpdates() {//requires dbase connection
        $sql = "SELECT content_id FROM content_text WHERE user_name = '$this->user_name'";
        $updates = mysql_num_rows(mysql_query($sql));
        return $updates;
    }

    //__Get Members Web Profiles_________________________

    function WebProfiles() {//requires dbase connection
        $sql = "SELECT * FROM web_profiles WHERE user_name = '$this->user_name' ORDER BY indexer DESC";
        $query = mysql_query($sql);
        $web_profiles_array = array();
        while($result = mysql_fetch_array($query)) {
            $web_profiles_array[] = $result;
        }
        return $web_profiles_array;
    }

    //__Get General Profile Info______________________________

    function MembersProfile() {
        global $config;
        $sql = "SELECT * FROM member_profile WHERE user_name = '$this->user_name'";
        $result = mysql_fetch_array(mysql_query($sql));
        return $result;
    }

}

//___________________________________________________________RELATIONSHIPS_____________________________________________________________________

class RelationShipCheck {

    var $my_username = null;
    var $members_user_name = null;
    var $error_count = 0;

    function RelationShipCheck($my_username,$members_user_name) {

        //does the user exist
        $sql = "SELECT user_id FROM member_profile WHERE user_name = '$my_username' AND account_status = 'active'";
        $sql2 = "SELECT user_id FROM member_profile WHERE user_name = '$members_user_name' AND account_status = 'active'";
        if(mysql_num_rows(mysql_query($sql)) > 0 && mysql_num_rows(mysql_query($sql2)) > 0) {
            $this->my_username = $my_username;
            $this->members_user_name = $members_user_name;
        }
        else {
            $this->error_count += 1;
        }

    }

    //___Am I following them_______________________
    function AmIFollowingThem() {

        if($this->error_count > 0) {
            return false;
        }

        $me = $this->my_username;
        $them = $this->members_user_name;

        $sql = "SELECT indexer FROM following WHERE followers_user_name = '$me' AND user_name = '$them'";
        if(mysql_num_rows(mysql_query($sql)) > 0) {
            return true;
        }
        else {
            return false;
        }

    }

    //___Am the following me_______________________
    function AreTheyFollowingMe() {

        if($this->error_count > 0) {
            return false;
        }

        $me = $this->my_username;
        $them = $this->members_user_name;

        $sql = "SELECT indexer FROM following WHERE followers_user_name = '$them' AND user_name = '$me'";
        if(mysql_num_rows(mysql_query($sql)) > 0) {
            return true;
        }
        else {
            return false;
        }

    }

}

//USAGE----------------------------------------------------------
/*

$relationship = new RelationShipCheck($env_user_name, $some_member);
($relationship->AmIFollowingThem())? $foo : $bar; //returns true/false
($relationship->AreTheyFollowingMe())? $foo : $bar; //returns true/false

*/
//---------------------------------------------------------------

//_________________________________________________________________________________________________________________________________________________
?>