<?php
//+-----------------------------------------------+//
//+ Copyright Floopo.com
//+ helper.profile.php
//+ Edit users profile
//+-----------------------------------------------+//

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

$show_profile_form = 1;

//___CHECK IF SUBMIT_______

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

    //process tthe form
    $process = new FormProcessor();
    //check email format
    ($process->RequiredFields('welcome_message')) ? $welcome_message = Sanatize($_POST['welcome_message']):$welcome_message = $config['members_welcome_message'];
    ($process->CheckFormatEmail('email_address')) ? $email_address = Sanatize($_POST['email_address']):$display_notification = $lang['invalid_email'];
    ($process->RequiredFields('privacy')) ? $privacy = Sanatize($_POST['privacy']):$display_notification = $lang['fill_in_all'];
    ($process->RequiredFields('notifications')) ? $notifications = Sanatize($_POST['notifications']):$display_notification = $lang['fill_in_all'];

//check privacy is correct format
if($privacy == 'public' || $privacy == 'private'){
$privacy_set = true;
}
    //update record
    if($email_address != '' && $privacy_set == true) {

        $welcome_message = mysql_real_escape_string($welcome_message);
        $email_address = mysql_real_escape_string($email_address);
        $privacy = mysql_real_escape_string($privacy);

        //update the profile
        if($dbase->UpdateRecord("UPDATE member_profile
                                 SET welcome_message = '$welcome_message',
                                     email_address = '$email_address',
                                     privacy = '$privacy',
                                     notifications = '$notifications'
                                 WHERE user_name = '$env_user_name'")) {
                                 
         //update the users content
         $content_privacy = ($privacy == 'public')? 1 : 0;
         
		 $dbase->UpdateRecord("UPDATE content_text
                                 SET content_public = '$content_privacy'
                                 WHERE user_name = '$env_user_name'");
								 

		 $dbase->UpdateRecord("UPDATE content_images
                                 SET content_public = '$content_privacy'
                                 WHERE user_name = '$env_user_name'");
		
		 //results				 								                         
         $display_notification = $lang['request_completed'];


            
        } else {
            //display error and reload post in form
            $display_notification = $lang['generic_error'];
        }
    } else {
        //error occurred - set vars to display in reloaded form
        $skip_select = true;
        foreach($_POST as $key => $value) {
            $$key = $value;
        }
    }
}


//___LOAD PROFILE_______
if(!$skip_select) {
    $result = $dbase->SelectRecord("SELECT * FROM member_profile WHERE user_name = '$env_user_name' AND account_status = 'active'");
    //set the vars for the html display
    foreach($result as $key => $value) {
    
    if ($key == 'privacy' || $key == 'notifications'){ //create selected pulldown 
    $key = $key.'_'.$value;
    $$key = 'selected';
    }else{
        $$key = $value;
     }  
        
    }
    
    
}


?>