<?php
//+-----------------------------------------------------+//
//+ Copyright Floopo.com
//+ helper.favorites.php
//+ Ajax helper - add/remove favorites
//+-----------------------------------------------------+//

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

//__________Favorite Texts and Images________________________________________________________-

if(Sanatize($_GET['type']) == 'text_fav' || Sanatize($_GET['type']) == 'image_fav') {

    //__Login check______
    loggedin_check_simple();

    $content_id = Sanatize(mysql_real_escape_string($_GET['id']));
    $content_type = Sanatize(mysql_real_escape_string($_GET['type']));

    //______check that input is ok________
    ($content_id == '' || !is_numeric($content_id)) ? die($lang['generic_error']) : null;

    $content_table = (($content_type == 'text_fav') ? 'content_text' : 'content_images');
    $favorite_table = (($content_type == 'text_fav') ? 'favorite_text' : 'favorite_images');

    if($dbase->CountRows("SELECT content_id 
                          FROM $content_table 
					      WHERE content_id = $content_id 
					      AND content_public = 1") == 0) {

        die($lang['private_or_removed']);
    }

    //___get content is if it exists___
    $current = $dbase->SelectRecord("SELECT *
                                     FROM $favorite_table
                                     WHERE user_name = '$env_user_name'
									 AND content_id = $content_id");

    $curent_id = $current['indexer'];//id of fav

    //_____We are adding?
    if($curent_id == '') {
        $dbase->InsertRecord("INSERT INTO $favorite_table (
		                      user_name, content_id
							  )values(
							  '$env_user_name', $content_id)");

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

    }
    else {
        //_____We are removing
        ($dbase->DeleteRecord("DELETE FROM $favorite_table 
	                           WHERE indexer = $curent_id")) ? die($lang['removed_from_favorites']) : die($lang['generic_error']);

    }

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

//__________Some other Ajax here___________________
?>