Fixed minor things and added missing file
[vanilla-miry.git] / extensions / TagThis / library / Function.TagThis.php
blobc287b67b3f8200bea4e0e008859da29c8fe812f5
1 <?php
3 //Used for adding tags prior to using TagThis
4 function AutomaticTags()
6 global $Context;
7 $query = "SELECT DiscussionID,Name FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Discussion`;";
8 $resource = $Context->Database->Execute($query,'','','An error occured selecting all discussions.');
10 $result = $Context->Database->GetRow($resource);
12 while ($result)
14 $DiscussionID = $result['DiscussionID'];
15 $originaltags = GetDiscussionTags($result['DiscussionID']);
16 $tags = GetTitleTags($result['Name']);
18 if (is_array($originaltags)) {$tags = array_merge($originaltags, $tags);}
20 $tags = array_unique($tags);
22 echo "<p>DiscussionID #".$DiscussionID." : ".implode(" ",$tags)."</p>";
24 //first delete the old tags
25 $query = "DELETE FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Tags` WHERE DiscussionID = '".$DiscussionID."';";
26 $result = $Context->Database->Execute($query,'','','An error occured removing tags for this discussions.');
28 //now add the new ones
29 foreach ($tags as &$tag)
31 $tag = trim(strtolower($tag));
32 if ($tag > "")
34 $query = "INSERT INTO `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Tags` (`TagName`, `DiscussionID`) VALUES ('".FormatStringForDatabaseInput($tag)."', '".$DiscussionID."');";
35 $result = $Context->Database->Execute($query,'','','An error occured adding tags for this discussions.');
39 $result = $Context->Database->GetRow($resource);
44 //removes common words from discussion titles
45 function GetTitleTags($title)
48 //removes anything that's not alphanumeric
49 $title = ereg_replace("[^A-Za-z0-9 ]", "", $title);
51 //Found both these lists - one has a lot less than the other, but might be less harsh
52 include('CommonList.inc.php');
54 $tags = explode(" ", $title);
56 //only return tags that aren't common
57 foreach ($tags as $tag)
59 if (!in_array(strtolower($tag), $commonWords))
61 $newtags[] = strtolower($tag);
65 return $newtags;
70 //for searching by tag
72 function SearchByTag(&$DiscussionManager)
74 if ($Tag = ForceIncomingString('Tag', ""))
76 $DiscussionManager->DelegateParameters['SqlBuilder']->AddJoin('Tags', 'tag', 'DiscussionID', 't', 'DiscussionID', 'left join', "");
77 $DiscussionManager->DelegateParameters['SqlBuilder']->AddWhere('tag', 'TagName', '', FormatStringForDatabaseInput(urldecode($Tag)), '=');
81 function SearchByTagInfo(&$SearchForm)
83 $SearchForm->PageDetails = substr($SearchForm->PageDetails,0, strlen($SearchForm->PageDetails)-16).' tag "'.ForceIncomingString('Tag', "").'"</strong></p>';
86 //tagging
88 function SaveTags(&$DiscussionForm)
90 global $Context;
92 //This means it's a fresh discussion, so we want the title tags
93 if (($DiscussionForm->DelegateParameters['SaveDiscussion']->FirstCommentID == 0) && ($Context->Configuration['TT_TITLE_TAG']))
95 $titletags = GetTitleTags($DiscussionForm->DelegateParameters['ResultDiscussion']->Name);
98 //only save the tags if saving the discussions was successful
99 if ($DiscussionForm->DelegateParameters['ResultDiscussion'])
101 //first delete the old tags
102 $query = "DELETE FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Tags` WHERE DiscussionID = '".$DiscussionForm->DelegateParameters['ResultDiscussion']->DiscussionID."';";
103 $result = $Context->Database->Execute($query,'','','An error occured removing tags for this discussions.');
105 //now add the new ones
107 $tags = explode(",", (ForceIncomingString('Tags', '')));
108 $tags2 = explode(" ", (str_replace(",","",ForceIncomingString('Tags', ''))));
110 //have they done it by command or space?
111 if (count($tags2) > count($tags)) {$tags = $tags2;}
113 //add title tags if there are any.
114 if (isset($titletags))
116 $tags = array_merge ($tags, $titletags);
119 $tags = array_unique($tags);
121 //no tags entered - hopefully solve a persons issue.
122 if (!isset($tags))
124 return;
127 foreach ($tags as &$tag)
129 $tag = trim(strtolower($tag));
130 if ($tag > "")
132 $query = "INSERT INTO `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Tags` (`TagName`, `DiscussionID`) VALUES ('".FormatStringForDatabaseInput($tag)."', '".$DiscussionForm->DelegateParameters['ResultDiscussion']->DiscussionID."');";
133 $result = $Context->Database->Execute($query,'','','An error occured adding tags for this discussions.');
140 function GetDiscussionTags($DiscussionID)
142 global $Context;
144 $query = "SELECT TagName FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Tags` as t LEFT JOIN `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Discussion` as d ON t.DiscussionID = d.DiscussionID WHERE t.DiscussionID = '".$DiscussionID."' AND Active = 1;";
145 $result = $Context->Database->Execute($query,'','','An error occured getting tags for this discussions.');
147 $taglist = "";
148 while (@$row = $Context->Database->GetRow($result))
150 $taglist.= $row['TagName'] . ", ";
152 $taglist = substr($taglist, 0, count($taglist)-3);
153 return $taglist;
156 function AddUserTagCloud($UserID)
158 global $Context;
159 global $Configuration;
161 //get discussions this user has started
162 $query = "SELECT DiscussionID FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Discussion` WHERE AuthUserID = '".$UserID."';";
163 $result = $Context->Database->Execute($query,'','','An error occured getting discussions starts by this user.');
165 while (@$row = $Context->Database->GetRow($result))
167 $Discussions[$row['DiscussionID']] = 1;
170 //if BlogThis is in use, select discussions where he has a comment blogged in it to get the tags from that discussion
171 if (array_key_exists('BLOGTHIS', $Configuration))
173 $query = "SELECT DiscussionID FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Comment` WHERE AuthUserID = '".$UserID."' AND BlogThis = 1;";
174 $result = $Context->Database->Execute($query,'','','An error occured getting discussions blogged by this user.');
176 while (@$row = $Context->Database->GetRow($result))
178 $Discussions[$row['DiscussionID']] = 1;
182 //if there are no tags, display a site wide tag cloud
183 if (!isset($Discussions))
185 AddDiscussionsTagCloud ();
186 return;
189 //now go through each discussion and get the tags
190 foreach ($Discussions as $DiscussionID => $Value)
192 $query = "SELECT TagName FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Tags` as t LEFT JOIN `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Discussion` as d ON t.DiscussionID = d.DiscussionID WHERE t.DiscussionID = '".$DiscussionID."' AND Active = 1;";
193 $tagresult = $Context->Database->Execute($query,'','','An error occured getting tags for this discussions.');
195 while (@$tagrow = $Context->Database->GetRow($tagresult))
197 if (isset($tags[$tagrow['TagName']]))
199 $tags[$tagrow['TagName']]++;
201 else
203 $tags[$tagrow['TagName']] = 1;
208 //if there are no tags, display a site wide tag cloud
209 if (!isset($tags))
211 AddDiscussionsTagCloud ();
212 return;
215 AddTagPanel($tags, 'User');
219 function AddDiscussionsTagCloud()
221 global $Context;
222 global $Configuration;
224 $query = "SELECT TagName FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Tags` as t LEFT JOIN `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Discussion` as d ON t.DiscussionID = d.DiscussionID WHERE Active = 1;";
225 $tagresult = $Context->Database->Execute($query,'','','An error occured getting all tags for this forum.');
227 while (@$tagrow = $Context->Database->GetRow($tagresult))
229 if (isset($tags[$tagrow['TagName']]))
231 $tags[$tagrow['TagName']]++;
233 else
235 $tags[$tagrow['TagName']] = 1;
239 if (!isset($tags)) {return;}
241 AddTagPanel($tags, 'Site');
244 //for a single discussion
245 function AddDiscussionTagCloud($DiscussionID)
247 global $Context;
248 global $Configuration;
250 $query = "SELECT t.DiscussionID, TagName FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Tags` as t LEFT JOIN `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Discussion` as d ON t.DiscussionID = d.DiscussionID WHERE Active = 1;";
251 $tagresult = $Context->Database->Execute($query,'','','An error occured getting all tags for this forum.');
253 while (@$tagrow = $Context->Database->GetRow($tagresult))
255 if (isset($tags[$tagrow['TagName']]))
257 $tags[$tagrow['TagName']]++;
259 else
261 $tags[$tagrow['TagName']] = 1;
263 //Is this a tag for this discussion?
264 if ($tagrow['DiscussionID'] == $DiscussionID)
266 $tags2[$tagrow['TagName']] = 1;
270 //if there are no tags, display a site wide tag cloud
271 if (!isset($tags2))
273 AddDiscussionsTagCloud ();
274 return;
277 //go through the tags from this discussion and add the weight from the overall
279 foreach ($tags2 as $key => &$value)
281 $value = $tags[$key];
284 AddTagPanel($tags2, 'Discussion');
288 function AddTagPanel($tags, $tagtype)
290 global $Context;
291 global $Panel;
293 if (isset($tags))
295 $tagcloud = GetTagCloud($tags);
297 if ($tagtype)
298 $ListName = $Context->GetDefinition($tagtype . 'TagCloud');
299 else
300 $ListName = $Context->GetDefinition('TagCloud');
302 $Panel->AddString("<h2>".$ListName."</h2><div id=\"TagCloud\">".$tagcloud."</div>", $Context->Configuration['TT_PANEL_POSITION']);
306 function GetTagCloud($tags)
308 global $Configuration;
310 //sorts by value to get the highest value
311 arsort($tags);
312 $maxnumber = each ($tags);
313 $maxnumber = $maxnumber['value'];
315 if ($Configuration['TT_CLOUD_LIMIT'] > 0)
317 $tags = array_slice($tags, 0, $Configuration['TT_CLOUD_LIMIT'], true);
320 //sort alphabetically
321 ksort ($tags);
323 $output = "";
325 foreach ($tags as $tag => $amount)
327 $fontsize = (($Configuration['TT_MAX_FONT'] - $Configuration['TT_MIN_FONT']) * ($amount / $maxnumber)) + $Configuration['TT_MIN_FONT'];
329 //it looked strange having everything really big if they were the only tags for that topic
330 if ($maxnumber == "1")
332 $fontsize = "100";
335 $output .= '<span style="font-size:'.ceil($fontsize).'%"><a href="'.GetUrl($Configuration, 'search.php', '', '', '', '', 'PostBackAction=Search&Type=Topics&Tag='.urlencode($tag)).'">'.$tag . "</a></span> ";
338 $output = substr($output,0,strlen($output)-1);
339 return $output;
342 function AddTagEntry(&$DiscussionForm)
344 global $Context;
346 $tags = GetDiscussionTags($DiscussionForm->DiscussionID);
348 echo '<li>
349 <label for="txtTags">'.$Context->GetDefinition('DiscussionTags').'</label>
350 <input id="txtTags" type="text" name="Tags" class="DiscussionBox" value="'.$tags.'" />
351 </li>';