<?php
//+-----------------------------------------------+//
//+ Copyright Floopo.com
//+ helper.avatar.php
//+ Update members avatar
//+-----------------------------------------------+//

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

$show_avatar_form = 1;

//___CURRENT AVATAR_______
$result = $dbase->SelectRecord("SELECT avatar FROM member_profile WHERE user_name = '$env_user_name' AND account_status = 'active'");
$current_avatar = $result['avatar'];


//___CHECK IF SUBMIT_______
if(isset($_POST['submit'])) {

    //set allowed file types
    $allowed_extensions = array('gif','jpg','png');

    $allowed_file_types = array('image/gif','image/jpg','image/jpeg','image/pjpeg','image/x-png','image/png');

    $new_filename = RandomCode(10);
    $filesize_limit = $config['avatar_file_size'];
    $destination_folder = BASEPATH.'/content/temp'; //no trailing /

    //upload file
    $fileupload = new FileUpload($new_filename,$destination_folder,$filesize_limit,
        $allowed_extensions,$allowed_file_types);

    //check for upload errors
    if(!$fileupload->obj_status) {

        $display_notification = ($fileupload->message)? $fileupload->message : $lang['generic_error']; //error message

    } else {

        //__Create thumb etc_____________________
        $thumb_folder = BASEPATH.'/content/avatars';
        $original_img = $fileupload->saved_file_path; //file as saved
        $max_width = 75; //new thumb max/desired width
        $max_height = 75; //new thumb max/desired heigth
        $maintain_aspect = 0; //or set to '0' to force thumb size
        $new_thumb_name = $new_filename; // file name with no extesnsion


        $create_thumb = new ThumbCreator($original_img,$thumb_folder);
        $create_thumb->ThumbSize($max_width,$max_height,$maintain_aspect);
        $create_thumb->SaveThumb($new_thumb_name);

    }

    //__Save to dbase_____________________
    if($create_thumb->obj_status) {

        //Update dbase with new file name
        $saved_file_name = $fileupload->saved_file_name;
        $saved_file_name = mysql_real_escape_string($saved_file_name);
        if($dbase->UpdateRecord("UPDATE member_profile SET avatar = '$saved_file_name' WHERE user_name = '$env_user_name'")) {
            $display_notification = $lang['request_completed'];

            //unlink old file
            ($current_avatar == 'default.png') ? NULL : @unlink(BASEPATH.'/content/avatars/'.$current_avatar);

            //unlink temp files
            @unlink(BASEPATH.'/content/temp/'.$fileupload->saved_file_name);

        } else {
            $display_notification = $lang['generic_error'];
        }
    }

}



//___DELETE PROFILE_______
if(isset($_POST['delete'])) {
//set dbase to default
$dbase->UpdateRecord("UPDATE member_profile SET avatar = 'default.png' WHERE user_name = '$env_user_name'");

//Unlink file
($current_avatar == 'default.png') ? NULL : @unlink(BASEPATH.'/content/avatars/'.$current_avatar);

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



//_____Get My Avatar___________
$result = $dbase->SelectRecord("SELECT avatar FROM member_profile WHERE user_name = '$env_user_name' AND account_status = 'active'");

//show or hide the "delete avatart link"
($result['avatar'] == 'default.png') ? NULL : $show_avatar_delete = 1;

//avatar image url
$avatar = $env_base_url.'/content/avatars/'.$result['avatar'];




?>