Support Incident Tracker GIT4.x
tags.inc.php
Go to the documentation of this file.
00001 <?php
00002 // tags.inc.php - functions relating to Tags
00003 //
00004 // SiT (Support Incident Tracker) - Support call tracking system
00005 // Copyright (C) 2010-2011 The Support Incident Tracker Project
00006 // Copyright (C) 2000-2009 Salford Software Ltd. and Contributors
00007 //
00008 // This software may be used and distributed according to the terms
00009 // of the GNU General Public License, incorporated herein by reference.
00010 
00011 // Prevent script from being run directly (ie. it must always be included
00012 if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME']))
00013 {
00014     exit;
00015 }
00016 
00017 
00024 function get_tag_id($tag)
00025 {
00026     global $dbTags;
00027     $sql = "SELECT tagid FROM `{$dbTags}` WHERE name = LOWER('$tag')";
00028     $result = mysql_query($sql);
00029     if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
00030     if (mysql_num_rows($result) == 1)
00031     {
00032         $id = mysql_fetch_row($result);
00033         return $id[0];
00034     }
00035     else
00036     {
00037         //need to add
00038         $sql = "INSERT INTO `{$dbTags}` (name) VALUES (LOWER('$tag'))";
00039         $result = mysql_query($sql);
00040         if (mysql_error()) trigger_error(mysql_error(),E_USER_ERROR);
00041         return mysql_insert_id();
00042     }
00043 }
00044 
00045 
00055 function new_tag($id, $type, $tag)
00056 {
00057     global $dbSetTags;
00058     /*
00059     TAG TYPES
00060     1 - contact
00061     2 - incident
00062     3 - Site
00063     4 - task
00064     5 - product
00065     6 - skill
00066     7 - kb article
00067     8 - report
00068  */
00069     if ($tag!='')
00070     {
00071         $tagid = get_tag_id($tag);
00072         // Ignore errors, die silently
00073         $sql = "INSERT INTO `{$dbSetTags}` VALUES ('$id', '$type', '$tagid')";
00074         $result = @mysql_query($sql);
00075     }
00076     return true;
00077 }
00078 
00079 
00089 function remove_tag($id, $type, $tag)
00090 {
00091     global $dbSetTags, $dbTags;
00092     if ($tag != '')
00093     {
00094         $tagid = get_tag_id($tag);
00095         // Ignore errors, die silently
00096         $sql = "DELETE FROM `{$dbSetTags}` WHERE id = '$id' AND type = '$type' AND tagid = '$tagid'";
00097         $result = @mysql_query($sql);
00098 
00099         // Check tag usage count and remove (purge) disused tags completely
00100         purge_tag($tagid);
00101     }
00102     return true;
00103 }
00104 
00105 
00113 function replace_tags($type, $id, $tagstring)
00114 {
00115     global $dbSetTags;
00116     // first remove old tags
00117     $sql = "DELETE FROM `{$dbSetTags}` WHERE id = '$id' AND type = '$type'";
00118     $result = mysql_query($sql);
00119     if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
00120 
00121     // Change separators to spaces
00122     $separators = array(', ',';',',');
00123     $tags = str_replace($separators, ' ', trim($tagstring));
00124     $tag_array = explode(" ", $tags);
00125     foreach ($tag_array AS $tag)
00126     {
00127         new_tag($id, $type, trim($tag));
00128     }
00129 }
00130 
00131 
00138 function purge_tag($tagid)
00139 {
00140     // Check tag usage count and remove disused tag completely
00141     global $dbSetTags, $dbTags;
00142     $sql = "SELECT COUNT(id) FROM `{$dbSetTags}` WHERE tagid = '$tagid'";
00143     $result = mysql_query($sql);
00144     list($count) = mysql_fetch_row($result);
00145     if ($count == 0)
00146     {
00147         $sql = "DELETE FROM `{$dbTags}` WHERE tagid = '$tagid' LIMIT 1";
00148         @mysql_query($sql);
00149     }
00150 }
00151 
00152 
00158 function purge_tags()
00159 {
00160     global $dbTags;
00161     $sql = "SELECT tagid FROM `{$dbTags}`";
00162     $result = mysql_query($sql);
00163     if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
00164     if (mysql_num_rows($result) > 0)
00165     {
00166         while ($tag = mysql_fetch_object($result))
00167         {
00168             purge_tag($tag->tagid);
00169         }
00170     }
00171 }
00172 
00173 
00181 function list_tags($recordid, $type, $html = TRUE)
00182 {
00183     global $CONFIG, $dbSetTags, $dbTags, $iconset;
00184 
00185     $sql = "SELECT t.name, t.tagid FROM `{$dbSetTags}` AS s, `{$dbTags}` AS t WHERE s.tagid = t.tagid AND ";
00186     $sql .= "s.type = '$type' AND s.id = '$recordid'";
00187     $result = mysql_query($sql);
00188     if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
00189     $numtags = mysql_num_rows($result);
00190 
00191     if ($html AND $numtags > 0)
00192     {
00193         $str .= "<div class='taglist'>";
00194     }
00195 
00196     $count = 1;
00197     while ($tags = mysql_fetch_object($result))
00198     {
00199         if ($html)
00200         {
00201             $str .= "<a href='view_tags.php?tagid={$tags->tagid}'>".$tags->name;
00202             if (array_key_exists($tags->name, $CONFIG['tag_icons']))
00203             {
00204                 $str .= "&nbsp;<img src='images/icons/{$iconset}/16x16/{$CONFIG['tag_icons'][$tags->name]}.png' alt='' />";
00205             }
00206             $str .= "</a>";
00207         }
00208         else
00209         {
00210             $str .= $tags->name;
00211         }
00212 
00213         if ($count < $numtags) $str .= ", ";
00214         if ($html AND !($count%5)) $str .= "<br />\n";
00215         $count++;
00216     }
00217     if ($html AND $numtags > 0) $str .= "</div>";
00218     return trim($str);
00219 }
00220 
00221 
00229 function list_tag_icons($recordid, $type)
00230 {
00231     global $CONFIG, $dbSetTags, $dbTags, $iconset;
00232     $sql = "SELECT t.name, t.tagid ";
00233     $sql .= "FROM `{$dbSetTags}` AS st, `{$dbTags}` AS t WHERE st.tagid = t.tagid AND ";
00234     $sql .= "st.type = '$type' AND st.id = '$recordid' AND (";
00235     $counticons = count($CONFIG['tag_icons']);
00236     $count = 1;
00237     foreach ($CONFIG['tag_icons'] AS $icon)
00238     {
00239         $sql .= "t.name = '{$icon}'";
00240         if ($count < $counticons) $sql .= " OR ";
00241         $count++;
00242     }
00243     $sql .= ")";
00244     $result = mysql_query($sql);
00245     if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
00246     $numtags = mysql_num_rows($result);
00247     if ($numtags > 0)
00248     {
00249         while ($tags = mysql_fetch_object($result))
00250         {
00251             $str .= "<a href='view_tags.php?tagid={$tags->tagid}' title='{$tags->name}'>";
00252             $str .= "<img src='images/icons/{$iconset}/16x16/{$CONFIG['tag_icons'][$tags->name]}.png' alt='{$tags->name}' />";
00253             $str .= "</a> ";
00254         }
00255     }
00256     return $str;
00257 }
00258 
00259 
00267 function show_tag_cloud($orderby="name", $showcount = FALSE)
00268 {
00269     global $CONFIG, $dbTags, $dbSetTags, $iconset;
00270 
00271     // First purge any disused tags
00272     purge_tags();
00273     $sql = "SELECT COUNT(name) AS occurrences, name, t.tagid FROM `{$dbTags}` AS t, `{$dbSetTags}` AS st WHERE t.tagid = st.tagid GROUP BY name ORDER BY $orderby";
00274     if ($orderby == "occurrences") $sql .= " DESC";
00275     $result = mysql_query($sql);
00276     if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
00277 
00278     $countsql = "SELECT COUNT(id) AS counted FROM `{$dbSetTags}` GROUP BY tagid ORDER BY counted DESC LIMIT 1";
00279     $countresult = mysql_query($countsql);
00280     if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
00281     list($max) = mysql_fetch_row($countresult);
00282 
00283     $countsql = "SELECT COUNT(id) AS counted FROM `{$dbSetTags}` GROUP BY tagid ORDER BY counted ASC LIMIT 1";
00284     $countresult = mysql_query($countsql);
00285     if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
00286     list($min) = mysql_fetch_row($countresult);
00287     unset($countsql, $countresult);
00288 
00289     if (mb_substr($_SERVER['SCRIPT_NAME'],-8) != "main.php")
00290     {
00291         //not in the dashboard
00292         $operations = array();
00293         $operations[$GLOBALS['strAlphabetically']] = 'view_tags.php?orderby=name';
00294         $operations[$GLOBALS['strPopularity']] = 'view_tags.php?orderby=occurrences';
00295         $html .= "<p align='center'>{$GLOBALS['strSort']}: " . html_action_links($operations) . "</p>";
00296     }
00297 
00298     if (mysql_num_rows($result) > 0)
00299     {
00300         $html .= "<table class='maintable'><tr><td class='tagcloud'>";
00301         while ($obj = mysql_fetch_object($result))
00302         {
00303             $size = round(log($obj->occurrences * 100) * 32);
00304             if ($size == 0) $size = 100;
00305             if ($size > 0 AND $size <= 100) $taglevel = 'taglevel1';
00306             if ($size > 100 AND $size <= 150) $taglevel = 'taglevel2';
00307             if ($size > 150 AND $size <= 200) $taglevel = 'taglevel3';
00308             if ($size > 200) $taglevel = 'taglevel4';
00309             $html .= "<a href='view_tags.php?tagid=$obj->tagid' class='$taglevel' style='font-size: {$size}%; font-weight: normal;' title='{$obj->occurrences}'>";
00310             if (array_key_exists($obj->name, $CONFIG['tag_icons']))
00311             {
00312                 $html .= "{$obj->name}&nbsp;<img src='images/icons/{$iconset}/";
00313                 if ($size <= 200)
00314                 {
00315                     $html .= "16x16";
00316                 }
00317                 else
00318                 {
00319                     $html .= "32x32";
00320                 }
00321                 $html .= "/{$CONFIG['tag_icons'][$obj->name]}.png' alt='' />";
00322             }
00323             else $html .= $obj->name;
00324             $html .= "</a>";
00325             if ($showcount) $html .= "({$obj->occurrences})";
00326             $html .= " \n";//&nbsp;\n";
00327         }
00328         $html .= "</td></tr></table>";
00329     }
00330     else $html .= user_alert($GLOBALS['strNothingToDisplay'], E_USER_NOTICE);
00331     return $html;
00332 }
00333 
00334 ?>