'/help/about', // an annoying hack. 'tags' => '/:tag_name', ); } //-------------------------------------------- // Show the tag cloud. //-------------------------------------------- function tags(&$reg, &$req, &$resp) { $tag_name = trim(strval($req->get('tag_name'))); //-------------------------------------------- // Bring in the tag cloud iterator. //-------------------------------------------- require_once FA_COMPONENTS_DIR .'/tag_cloud_iterator.php'; //-------------------------------------------- // Figure out which view we should use. //-------------------------------------------- if($tag_name == '') { //-------------------------------------------- // We're looking at all of the tags at once.. //-------------------------------------------- $total_tags = 0; //-------------------------------------------- // Get the tags. //-------------------------------------------- $finder = &$this->getFinder('thread_tags'); $tags = &$finder->selectWhere('COUNT(ts.tag_id) as tag_count', '', array(), 1); //-------------------------------------------- // Count them. //-------------------------------------------- if ($tags !== NULL) { $total_tags = $tags->get('tag_count'); } //-------------------------------------------- // Get the the tags but with all of their info. //-------------------------------------------- $finder = &$this->getFinder('thread_tags'); $tags = &$finder->selectAllWhere('ts.*, COUNT(tt.thread_id) as tag_count', '', array(), 'tag_name', 'tag_name ASC'); $resp->assignRef('tag_cloud', new TagCloudIterator($tags, $total_tags)); //-------------------------------------------- // Set the tags to the template. //-------------------------------------------- $resp->assign('content', 'tag_list.html'); $this->setPage('Tags'); } else { $info = &new InformationDisplay($reg, $req, $resp); //-------------------------------------------- // We're looking at one single tag / multiple tags. //-------------------------------------------- if(strpos($tag_name, " ") !== FALSE) { //-------------------------------------------- // Deal with tag intersection. //-------------------------------------------- $tag_names = explode(" ", $tag_name); } else { $tag_names = array($tag_name); } $tag_names = clean_tags($tag_names); $tag_sql_where = "IN(". substr(str_repeat('?,', count($tag_names)), 0, -1) .")"; //-------------------------------------------- // Get the tags. //-------------------------------------------- $finder = &$this->getFinder('tags'); $tags = &$finder->findAllWhere("ts.tag_name ". $tag_sql_where, $tag_names); //-------------------------------------------- // Whoops! None of the tags we're looking for exist! //-------------------------------------------- if($tags === NULL) { $info->text("We're sorry but none of the tags you are looking for seem to exist."); } //-------------------------------------------- // Loop through the tags that we found and collect // their names and ids into an array so we can find // threads that use these tags. //-------------------------------------------- $tag_names = array(); $tag_ids = array(); while($tags->next()) { $tag = &$tags->current(); if(!in_array($tag->getId(), $tag_ids)) { $tag_names[] = $tag->get('tag_name'); $tag_ids[] = $tag->getId(); } } $resp->assign('using_tags', $tags->numRows() >= 2 ? 1 : 0); //-------------------------------------------- // So, we've got to this point but there are no // tags. Oh well, error :P //-------------------------------------------- if(empty($tag_names)) { $info->text("We're sorry but none of the tags you are looking for seem to exist."); } //-------------------------------------------- // Get the threads. //-------------------------------------------- $finder = &$this->getFinder('threads'); $finder->addTempFrom("k4_thread_tags tt2"); $threads = &$finder->findAllWhere("t.deleted=0 AND tt.thread_id = tt2.thread_id AND tt2.tag_id $tag_sql_where", $tag_ids, 't.created DESC', 'tt2.tag_id'); //-------------------------------------------- // Get tags that are often used with this tag //-------------------------------------------- $finder = &$this->getFinder('tags'); $finder->addTempFrom("k4_thread_tags tt1 JOIN k4_thread_tags tt2 USING ( thread_id ) "); $similar_tags = &$finder->selectAllWhere("COUNT(tt2.tag_id) AS tag_count", "tt1.tag_id $tag_sql_where AND tt1.tag_id <> tt2.tag_id AND ts.tag_id = tt2.tag_id", $tag_ids, FALSE, 'tt2.tag_id', 250); $total_tags = 0; //-------------------------------------------- // Make a total count of the number of times // these tags have been used. Sadly, this means // looping over the tags we just got. //-------------------------------------------- while($similar_tags->next()) { $tag = &$similar_tags->current(); $total_tags += intval($tag->get('tag_count')); } $similar_tags->reset(); //-------------------------------------------- // Find people that often use these tags. //-------------------------------------------- $sql = ""; $sql .= "SELECT DISTINCT u.user_id, u.name, COUNT(tt2.tag_id) AS tag_count FROM "; $sql .= "k4_thread_users tu, k4_users u, "; $sql .= "k4_thread_tags tt1 JOIN k4_thread_tags tt2 USING (thread_id) "; $sql .= "WHERE u.user_id = tu.user_id "; $sql .= "AND "; $sql .= "tt1.tag_id $tag_sql_where AND tt1.tag_id <> tt2.tag_id AND tu.thread_id = tt2.thread_id "; $sql .= "GROUP BY tt2.tag_id ORDER BY u.name ASC"; $dba = &$reg->get('dba'); $users = &$dba->executeQuery($sql, $tag_ids); $users_array = array(); $total_user_tag_count = 0; //-------------------------------------------- // Loop over the users found and cache them. //-------------------------------------------- while($users->next()) { $user = &$users->current(); if(!isset($users_array[$user['name']])) { $users_array[$user['name']] = array( 'name' => $user['name'], 'user_id' => $user['user_id'], 'id' => $user['user_id'], 'tag_count' => 0, ); } $users_array[$user['name']]['tag_count'] += $user['tag_count']; $total_user_tag_count += $user['tag_count']; } $users_array = array_values($users_array); $users_array = &new FAArrayIterator($users_array); //-------------------------------------------- // Assign everything to the template. //-------------------------------------------- $resp->assignRef('tags', $tags); $resp->assignRef('thread_list', $threads); $resp->assignRef('tags_combined', implode('+', $tag_names)); $resp->assignRef('similar_tags', new TagCloudIterator($similar_tags, $total_tags)); $resp->assignRef('users', new TagCloudIterator($users_array, $total_user_tag_count)); $resp->assign('content', 'tag_single.html'); $this->setPage('Tags: '. implode(', ', $tag_names)); } $this->setLayout('forum_base.html'); } //-------------------------------------------- // An about page explaining what tags are. //-------------------------------------------- function help(&$reg, &$req, &$resp) { $resp->assign('content', 'tag_about.html'); $this->setPage('What are Tags?'); $this->setLayout('forum_base.html'); } } ?>