<?php
/*===================================================+
|| # HoloCMS - Website and Content Management System
|+===================================================+
|| # Copyright © 2008 Meth0d. All rights reserved.
|| # http://www.meth0d.org
|+===================================================+
|| # HoloCMS is provided "as is" and comes without
|| # warrenty of any kind. HoloCMS is free software!
|+===================================================*/

include('../core.php');
include('../includes/session.php');

$key = FilterText($_GET['key']);
$tag = FilterText($_POST['tagName']);

$tmp = mysql_query("SELECT * FROM user_tags WHERE user_id = '".$my_id."' LIMIT 20") or die(mysql_error());
$tag_num = mysql_num_rows($tmp);

$randomq[] = "Quale La Tua Tag Preferita?";

srand ((double) microtime() * 1000000);
$chosen = rand(0,count($randomq)-1);

if($key == "remove"){

mysql_query("DELETE FROM user_tags WHERE tag = '".$tag."' AND user_id = '".$my_id."' LIMIT 1") or die(mysql_error());
echo "SUCCESS";

} elseif($key == "p_remove"){

echo "<div id=\"profile-tags-container\">\n";

mysql_query("DELETE FROM user_tags WHERE tag = '".$tag."' AND user_id = '".$my_id."' LIMIT 1") or die(mysql_error());
$get_tags = mysql_query("SELECT * FROM user_tags WHERE user_id = '" . $my_id . "' ORDER BY id LIMIT 25") or die(mysql_error());
$rows = mysql_num_rows($get_tags);

	if($rows > 0){
		while ($row = mysql_fetch_assoc($get_tags)){
			printf("    <span class=\"tag-search-rowholder\">
        <a href=\"tags.php?tag=%s\" class=\"tag-search-link tag-search-link-%s\"
        >%s</a><img border=\"0\" class=\"tag-delete-link tag-delete-link-%s\" onMouseOver=\"this.src='web-gallery/images/buttons/tags/tag_button_delete_hi.gif'\" onMouseOut=\"this.src='web-gallery/images/buttons/tags/tag_button_delete.gif'\" src=\"web-gallery/images/buttons/tags/tag_button_delete.gif\"
        /></span>", $row['tag'], $row['tag'], $row['tag'], $row['tag']);
		}
	} else {
		echo "No tags";
	}

echo "\n    <img id=\"tag-img-added\" border=\"0\" src=\"imgs/tag_button_added.gif\" style=\"display:none\"/>    
</div>";

} elseif($key == "p_list"){

echo "<div id=\"profile-tags-container\">\n";

mysql_query("DELETE FROM user_tags WHERE tag = '".$tag."' AND user_id = '".$my_id."' LIMIT 1") or die(mysql_error());
$get_tags = mysql_query("SELECT * FROM user_tags WHERE user_id = '" . $my_id . "' ORDER BY id LIMIT 25") or die(mysql_error());
$rows = mysql_num_rows($get_tags);

	if($rows > 0){
		while ($row = mysql_fetch_assoc($get_tags)){
			printf("    <span class=\"tag-search-rowholder\">
        <a href=\"tags.php?tag=%s\" class=\"tag-search-link tag-search-link-%s\"
        >%s</a><img border=\"0\" class=\"tag-delete-link tag-delete-link-%s\" onMouseOver=\"this.src='imgs/tag_button_delete_hi.gif'\" onMouseOut=\"this.src='web-gallery/images/buttons/tags/tag_button_delete.gif'\" src=\"web-gallery/images/buttons/tags/tag_button_delete.gif\"
        /></span>", $row['tag'], $row['tag'], $row['tag'], $row['tag']);
		}
	} else {
		echo "Nessuna Tag";
	}

echo "\n    <img id=\"tag-img-added\" border=\"0\" src=\"web-gallery/images/buttons/tags/tag_button_added.gif\" style=\"display:none\"/>    
</div>";

} elseif($key == "add"){

$tag = FilterText($_POST['tagName']);
$filter = preg_replace("/[^a-zA-Z0-9 |\d]/i", "", $tag);

	if(strlen($tag) < 2 || strlen($tag) > 20){
	echo "invalidtag"; exit;
	} else {
	$check = mysql_query("SELECT * FROM user_tags WHERE user_id = '".$my_id."' AND tag = '".$filter."' LIMIT 1") or die(mysql_error());
	$num = mysql_num_rows($check);
		if($num > 0){
		echo "invalidtag"; exit;
		} else {
			if($tag_num > 20){
			echo "invalidtag"; exit;
			} else {
			mysql_query("INSERT INTO user_tags (user_id,tag) VALUES ('".$my_id."','".$filter."')");
			echo "valid"; exit;
			}
		}
	}

} elseif($key == "mytagslist"){

echo "   <div class=\"habblet\" id=\"my-tags-list\">\n\n";
            echo "<ul class=\"tag-list make-clickable\"> ";
	while($row = mysql_fetch_assoc($tmp)){
                    printf("<li><a href=\"tags.php?tag=%s\" class=\"tag\" style=\"font-size:10px\">%s</a>\n
                        <a class=\"tag-remove-link\"\n
                        title=\"Rimuovi Tag\"\n
                        href=\"#\"></a></li>\n", $row['tag'], $row['tag']);
	}
            echo "</ul>";
if($tag_num < 20){
echo "     <form method=\"post\" action=\"tag_ajax.php?key=add\" onsubmit=\"TagHelper.addFormTagToMe();return false;\" >
    <div class=\"add-tag-form clearfix\">
		<a  class=\"new-button\" href=\"#\" id=\"add-tag-button\" onclick=\"TagHelper.addFormTagToMe();return false;\"><b>Add tag</b><i></i></a>
        <input type=\"text\" id=\"add-tag-input\" maxlength=\"20\" style=\"float: right\"/>
        <em class=\"tag-question\">" . $randomq[$chosen] . "</em>
    </div>
    <div style=\"clear: both\"></div> 
    </form>";
}
echo "    </div>

<script type=\"text/javascript\">

    TagHelper.setTexts({
        tagLimitText: \"Tu\'Hai raggiunto i limiti Per le tag\",
        invalidTagText: \"Tag Invalita\",
        buttonText: \"OK\"
    });
        TagHelper.bindEventsToTagLists();

</script>\n";

}
?>

