<?php
//+-----------------------------------------------+//
//+ Copyright Floopo.com
//+ helper.php
//+ Adding and editing new text via browser
//+-----------------------------------------------+//

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

//____DEFAULT TEMPLATE_____
$template = POPUP_TEMPLATES.'tpl_image.htm';


//___PRESETS_____
$title = $lang['new_image'];
loggedin_check();

//___START_______________


//__New Submission___
if ($_POST) {

//____________upload file___________________
$allowed_extensions = array('gif',
                                'jpg',
	                            'bmp',
                                'png',
                                'jpeg');

$allowed_file_types = array('image/gif',
                                'image/jpg',
                                'image/jpeg',
	                            'image/pjpeg',
	                            'image/x-png',
	                            'image/bmp',
                                'image/png',);
$new_filename = RandomCode(7);
$filesize_limit = $config['image_file_size'];
$destination_folder =  BASEPATH.'/content/images'; //no trailing /
$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; //error message

}else{
    //all is ok - save to dabase
    if ($fileupload->obj_status) {
    	$strip_p_tags = array('<p>', '</p>', '</br>', '<br />');
        $text_message = str_replace($strip_p_tags, '', $_POST['text_message']); //TODO some cleaning here
        $text_message = StringSnip($text_message);
        $show_notification = 1;


//________create thumb etc_____________________

$thumb_folder = BASEPATH.'/content/images/thumbs';
$original_img = $fileupload->saved_file_path; //final file as saved
$max_width = 100; //new thumb max/desired width
$max_height = 75; //new thumb max/desired heigth
$maintain_aspect = 1; //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);







//all is ok 
if($create_thumb->obj_status){

            //are we adding new content
            $saved_file_name = $fileupload->saved_file_name;
            $dbase->InsertRecord("INSERT INTO content_images (
			                      text_message, user_name, date_added, filename,date_added_short
								  ) values (
								  '$text_message', '$env_user_name', NOW(), '$saved_file_name',NOW())");

        //did everything go ok
        ($dbase->obj_status) ? $display_notification = $lang['request_completed'] : $display_notification = $lang['generic_error'];
}


}
}

}

//__Load existing content___
if ($_GET['tid']) {

    //lets check content id
    CriticalInputCheck('numeric', $_GET['tid']);

    $title = $lang['edit_text'];
    $content_id = $_GET['tid'];
    $result = $dbase->SelectRecord("SELECT text_message, user_name FROM content_text WHERE content_id = $content_id");

    $text_message = $result['text_message'];

    ($dbase->result_status) ? null : ErrorDisplay1($lang['generic_error']);

}



///FCK EDITOR________________________________________________________________________
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;
//____________________________________________________________________________________

?>