<?php
//+-----------------------------------------------------+//
//+ Copyright Floopo.com
//+ helper.following.php
//+ Following helper
//+-----------------------------------------------------+//

defined('BASEPATH') or die('You can not directly access this file');//check its within this framework

//__________Favorite Texts and Images________________________________________________________-

if(Sanatize($_GET['type']) == 'following' && Sanatize($_GET['action']) != '' && Sanatize($_GET['member']) != '') {

    //__Login check______
    loggedin_check_simple();

    $member = Sanatize(mysql_real_escape_string($_GET['member']));
    $action = Sanatize(mysql_real_escape_string($_GET['action']));


    //__Am I trying to add myself______
   ($member == $env_user_name)? die($lang['cant_add_self']) : NULL;



//________________Following__________________________________________
if(Sanatize($_GET['action']) == 'add'){

     $approved = 'yes';


    //TODO: add action for "blocked" members

    //__Am already following______
    ($dbase->CountRows("SELECT indexer
                        FROM following
                        WHERE user_name = '$member'
						AND followers_user_name = '$env_user_name'
                        AND approved = 'yes'") > 0)? die($lang['already_following']) : NULL;



    //_______Is profile private___________
    if($dbase->CountRows("SELECT user_id
                         FROM member_profile
                         WHERE user_name = '$member'
						 AND privacy = 'private'") > 0){


     //__have i requested aready______________
    ($dbase->CountRows("SELECT indexer
                        FROM following
                        WHERE user_name = '$member'
						AND followers_user_name = '$env_user_name'
                        AND approved = 'pending'") > 0)? die($lang['private_already_requested']) : NULL;

    $approved = 'pending';

}


    //add to following
    $dbase->InsertRecord("INSERT INTO following (
                          user_name, followers_user_name, approved
                          ) values (
                          '$member', '$env_user_name', '$approved')");

    if($dbase->obj_status){
    
    //_____Send email notifications________________________________________________________________________________
    
	//get email address
    $email_address = NotificationStatus($member); //also checks notifications settings
    if($email_address != ''){
    
	//Get Welcome Message
    $result = $dbase->SelectRecord("SELECT message, subject FROM system_messages WHERE title = 'followed'");
    
	//Send Email
    $notify = new Postage($result['message']);
    $notify->EmailFormat('html');
    $notify->ToFromSubject($email_address, $config['system_email'], $result['subject']);
    $notify->EmailVars(array('user_name'=>$member,
                             'followers_username'=>$env_user_name));
    $notify->SendNow();
    }
    //_____________________________________________________________________________________________________________
    
	die($lang['request_completed']);
	}else{
	
	die($lang['generic_error']);
	}

}


//________________Following Delete__________________________________________
if(Sanatize($_GET['action']) == 'remove' && Sanatize($_GET['delete_type']) == 1){

    //add to following
    $dbase->DeleteRecord("DELETE FROM following
                          WHERE
                          user_name = '$member'
                          AND
                          followers_user_name = '$env_user_name'");

    ($dbase->obj_status)? die($lang['request_completed']) : die($lang['generic_error']);

}


//________________Followers Delete__________________________________________
if(Sanatize($_GET['action']) == 'remove' && Sanatize($_GET['delete_type']) == 2){

    //add to following
    $dbase->DeleteRecord("DELETE FROM following
                          WHERE
                          user_name = '$env_user_name'
                          AND
                          followers_user_name = '$member'");

    ($dbase->obj_status)? die($lang['request_completed']) : die($lang['generic_error']);

}


}else{
die($lang['generic_error']);
}

//__________Some other Ajax here___________________
?>