<?php
//+-----------------------------------------------+//
//+ Copyright Floopo.com
//+ helper.messages.php
//+ Emailing
//+-----------------------------------------------+//

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

//________LOAD INBOX OR SENT OR COMPOSE__________
$box = (($_GET['box'])?Sanatize($_GET['box']):Sanatize($_POST['box']));//what are we doing



//________CHECK IF WE ARE DELETING ITEMS__________

if(isset($_POST['delete'])) {

    foreach($_POST['list'] as $key => $value) {

       $value = Sanatize(mysql_real_escape_string($value));

        //are we deleting inbox or sent items
        $field = (($_POST['which_box'] == 'inbox')? 'receiver_delete' : 'sender_delete');

        //Delete (i.e. update dbase record - no actual 'physical' delete)
        $display_notification = (($dbase->UpdateRecord("UPDATE personal_messages SET $field = 1 WHERE indexer = $value")) ? $lang['request_completed'] : $lang['generic_error']);
    }
}



//________CHECK IF WE ARE CREATING NEW__________

if(isset($_POST['send'])) {

    //show the submitted text
    foreach($_POST as $key => $value) {
        $$key = $value;
    }
    //process the form
    $process = new FormProcessor();
    ($process->RequiredFields('send_to,text_message'))?$proceed1 = true:$display_notification =
        $lang['fill_in_all'];

    //check if member is active
    (IsMemberActive(Sanatize(mysql_real_escape_string($send_to))))?$proceed2 = true:
        $display_notification = $lang['generic_error'];

    //check the relationship with member
    if($proceed1 == true && $proceed2 == true) {
        $relationship = new RelationShipCheck($env_user_name,$send_to);
        ($relationship->AmIFollowingThem()) ?$proceed3 = true : $display_notification = $lang['only_email_following_followers'];//returns true/false
        ($relationship->AreTheyFollowingMe())?$proceed4 = true : $display_notification = $lang['only_email_following_followers'];//returns true/false

        //Attempt to insert into dbase
        if($proceed3 == true || $proceed4 == true) {
            $send_to = Sanatize(mysql_real_escape_string($send_to));
            $text_message = Sanatize($text_message);
            $dbase->InsertRecord("INSERT INTO personal_messages (sender, receiver, message, date_sent) 
                                          values ('$env_user_name', '$send_to', '$text_message', NOW())");

            //if all ok..load inbox and show success
            if($dbase->obj_status) {
            
    //_____Send email notifications________________________________________________________________________________
    
	//get email address
    $email_address = NotificationStatus($send_to); //also checks notifications settings
    if($email_address != ''){
    
	//Get Welcome Message
    $result = $dbase->SelectRecord("SELECT message, subject FROM system_messages WHERE title = 'direct_message'");
    
	//Send Email
    $notify = new Postage($result['message']);
    $notify->EmailFormat('html');
    $notify->ToFromSubject($email_address, $config['system_email'], $result['subject']);
    $notify->EmailVars(array('user_name'=>$send_to,
                             'senders_username'=>$env_user_name));
    $notify->SendNow();
    }
    //_____________________________________________________________________________________________________________
    
            
                $display_notification = $lang['request_completed'];
                $box = 1;
            }
            else {
                $display_notification = $lang['generic_error'];
            }

        }

    }

}



//________LOAD CORRECT DISPLAY_________
if(!$skip_select) {
$result = '';
    switch($box) {

            //sent
        case 2:
            $result = $dbase->SelectRecordLoop("SELECT receiver AS user_name, message, date_sent, indexer  FROM personal_messages WHERE sender = '$env_user_name' AND sender_delete = 0 ORDER BY indexer DESC");
            $pm_title = 'Sent Items';
            $which_box = 'sent';
            //check if anything to display
            $show_pm_list = ((count($result) <= 0)?2:1);
            break;

            //Create new email
        case 3:
            $show_pm_new = 1;
            break;

            //inbox
        default:
            $result = $dbase->SelectRecordLoop("SELECT sender AS user_name, message, date_sent, indexer  FROM personal_messages WHERE receiver = '$env_user_name' AND receiver_delete = 0 ORDER BY indexer DESC");
            $pm_title = 'Inbox';
            $which_box = 'inbox';
            //check if anything to display
            $show_pm_list = ((count($result) <= 0)?2:1);
            break;

    }

}

//________FCK EDITOR______________________________________
if($box == 3) {
    include (FCK_EDITOR);
    $sBasePath = BASEURL.'/thirdparty/fckeditor/';
    $oFCKeditor = new FCKeditor('text_message');
    $oFCKeditor->BasePath = $sBasePath;
    $oFCKeditor->CustomConfigurationsPath = "fckconfig.js";
    $oFCKeditor->ToolbarSet = $config['fckeditor_toolbar'];//OR manually set to 'Basic', 'BasicPlus' or 'Advanced'
    $oFCKeditor->SkinPath = $sBasePath + 'editor/skins/silver/';
    $oFCKeditor->Value = "$text_message";
    $oFCKeditor->Width = '647';
    $oFCKeditor->Height = '175';
    $FCKeditor1 = $oFCKeditor->CreateHtml();
    $fck_editor = $FCKeditor1;
}
//_________________________________________________________
?>