<?php
function getTagLinks ()
{
global $db_tagTableName;
global $defaultTags;
global $selectedTags;
global $allNotesTagName;
if (isset ($selectedTags))
{
$tags = $selectedTags;
}
else
{
$tags = $defaultTags;
}
// Send a query to the SQL server, to get a list of used tags
$result = mysql_query ("SELECT * FROM $db_tagTableName");
$tagsString = "";
while (($row = mysql_fetch_array ($result)) != null)
{
// Fix for javascript issue in Opera
$bold = "700";
$normal = "400";
// The AllNotes and Default Tag get special Ids so it's possible to document.getElementById() these in JavaScript part
if ($allNotesTagName == $row["tagname"])
{
$tagId = "_TagAllNotes";
}
else
{
if ($defaultTags == $row["tagname"])
{
$tagId = "_TagDefaultNotes";
}
else
{
$tagId = "";
}
}
// If tag is listed in defaultTags, display it bold
if (strstr ($tags, $row["tagname"]) != false)
{
$tagsString .= "<span " . (($tagId != "") ? ("id=\"" . $tagId . "\"") : ("")) . " class=\"tag\" onMouseOver=\"TagOnMouseOver (this)\" style=\"font-weight: " . $bold . ";\" onClick=\"ToggleTag(this)\">";
$tagsString .= $row["tagname"];
$tagsString .= "</span>";
$tagsString .= "<span class=\"tagmenu\" id=\"" . $row["tagname"] . "\" onClick=\"ShowTagContextMenu(this)\"><img style=\"position: relative; top: 3px;\" src=\"images/downarrow.png\" alt=\"*\"></span>";
$tagsString .= " ";
}
else
{
$tagsString .= "<span " . (($tagId != "") ? ("id=\"" . $tagId . "\"") : ("")) . " class=\"tag\" onMouseOver=\"TagOnMouseOver (this)\" style=\"font-weight: " . $normal . ";\" onClick=\"ToggleTag(this)\">";
$tagsString .= $row["tagname"];
$tagsString .= "</span>";
$tagsString .= "<span class=\"tagmenu\" id=\"" . $row["tagname"] . "\" onClick=\"ShowTagContextMenu(this)\"><img style=\"position: relative; top: 3px;\" src=\"images/downarrow.png\" alt=\"*\"></span>";
$tagsString .= " ";
}
}
// Update Javascript variable which contains the actually selected notes
$tagsString .= "<script type=\"text/javascript\">SetActiveTags(\"" . $tags . "\");</script>";
return $tagsString;
}
// Checks if the Tag String is empty, and sets it to default active tags if so
// ---------------------------------------------------------------------------
// IN/OUT $tags - The String which contains the TagNames seperated by spaces
function CheckIfIsEmptyTag (&$tags)
{
global $defaultTags;
if (trim($tags) == "")
$tags = $defaultTags;
}
// Updates tags in database
// Parameter $note is unused. Remove!
function updateNoteTags ($note, $tags, $id)
{
global $db_tagTableName;
global $db_tableName;
global $db_noteTagsTableName;
$result = mysql_query ("DELETE FROM $db_noteTagsTableName WHERE noteid='$id'");
// Foreach tag take a look if it exists in database, if not insert it
// Update the notetags table and insert noteid and tagid
foreach (explode (" ", $tags) as $tag)
{
$tag = ereg_replace ("[^a-zA-Z0-9 ]*", "", $tag);
$tag = trim($tag);
if ($tag == "")
continue;
$result = mysql_query ("SELECT * from $db_tagTableName WHERE tagname='$tag'");
if ($result)
{
if (mysql_num_rows ($result) == 0)
{
$result = mysql_query ("INSERT INTO $db_tagTableName (tagname, rating) VALUES('$tag', '0')");
echo mysql_error ();
$result = mysql_query ("SELECT * from $db_tagTableName WHERE tagname='$tag';");
}
}
$row = mysql_fetch_array($result);
$tagId = $row["id"];
$result = mysql_query ("INSERT INTO $db_noteTagsTableName (tagid, noteid) VALUES($tagId, $id);");
}
}
// Renames a Tag in Database
function RenameTag ($oldTag, $newTag)
{
global $db_tagTableName;
$query = "SELECT id FROM $db_tagTableName WHERE `tagname`='$oldTag'";
$result = mysql_query($query);
if ($result)
{
$row = mysql_fetch_array($result);
$id = $row["id"];
if (isset ($id))
{
$query = "UPDATE $db_tagTableName SET tagname='$newTag' WHERE id='$id'";
$result = mysql_query ($query);
}
}
$jsonArray = array();
$jsonArray[0]["tagLinks"] = getTagLinks();
$json = new Services_JSON();
echo $json->encode($jsonArray);
exit;
}
// Deletes a tag in database
function DeleteTag ($tagName)
{
global $db_tagTableName;
$query = "SELECT id FROM $db_tagTableName WHERE `tagname`='$tagName'";
$result = mysql_query($query);
if ($result)
{
$row = mysql_fetch_array($result);
$id = $row["id"];
if (isset ($id))
{
$query = "DELETE FROM $db_tagTableName WHERE id='$id'";
$result = mysql_query ($query);
echo mysql_error();
}
}
$jsonArray = array();
$jsonArray[0]["tagLinks"] = getTagLinks();
$json = new Services_JSON();
echo $json->encode($jsonArray);
exit;
}