<?php

include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );

    
    /*
     * This plugin generate TagCloud for a specific Blog
     */
     class PluginTagCloud extends PluginBase
     {
	
	/*
	* Constructor
	*/
	function PluginTagCloud(){
	
		$this->author = "Ben Yacoub Hatem";
		$this->desc = "This plugin generate TagCloud for a specific Blog, usage
			    :<br/>
			    <b>{ \$tagcloud->getTagCloud(MaxArticles,MaxKeywords)}</b><br/><br/>
			   <em> blogId</em> is the id of the Current Blog<br/>
			   <em> MaxArticles</em> Latest articles to parse and generate the Tag Clouds<br/> 
			   <em> MaxKeywords</em> maximum number of keywords to display<br/> <br/>
			   getTagCloud return html of the Tag Cloud ready to use.<br/>
			   See Readme for example usage.";
		    
		$this->PluginBase();
		$this->db =& Db::getDb();
		$this->id = "TagCloud";
	}

	/*
	* Return cloud of the latest articles
	*/
	function getTagCloud($MaxArticles = 20, $MaxKeywords = 50){

		$this->_prefix = Db::getPrefix();
		$blogId = $this->blogInfo->getId();
		
	
		$MaxArticles = mysql_escape_string($MaxArticles);
	
		$query = "SELECT t.normalized_text,t.normalized_topic FROM " . $this->_prefix . 
		    "articles a, " . $this->_prefix."articles_text t" .
		    " WHERE a.id=t.article_id " .
		    " AND a.status=1 AND a.blog_id = " . $blogId . 
		    " ORDER BY a.date DESC LIMIT 0,$MaxArticles;";
	
	
		$result = $this->db->Execute($query);
		    if(!$result || ($result->RecordCount() == 0))
		    return false;
	
		// get the articles content
		while ($row = $result->FetchRow()) {
			$data[] = $row['normalized_topic'].' '.$row['normalized_text'];
		}	
		$data = implode(' ',$data);
		
		// Split keywords
		$words = preg_split('/\s*[\s+\.|\?|,|(|)|\-+|\'|\"|!|=|;|&#0215;|\$|\/|:|{|}]\s*/i', $data);
		$acv = array_count_values( $words );
		
		// Remove unwanted keywords
		$bannedwords = array( '', 'a', 'an', 'the', 'and', 'of', 'i', 'its' , 'to', 'is', 'in', 'with', 'for', 'as', 'that', 'on', 'at', 'this', 'my', 'was', 'our', 'it', 'you', 'we', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '10', 'about', 'after', 'all', 'almost', 'along', 'also', 'amp', 'another', 'any', 'are', 'area', 'around', 'available', 'back', 'be', 'because', 'been', 'being', 'best', 'better', 'big', 'bit', 'both', 'but', 'by', 'c', 'came', 'can', 'capable', 'control', 'could', 'course', 'd', 'dan', 'day', 'decided', 'did', 'didn', 'different', 'div', 'do', 'doesn', 'don', 'down', 'drive', 'e', 'each', 'easily', 'easy', 'edition', 'end', 'enough', 'even', 'every', 'example', 'few', 'find', 'first', 'found', 'from', 'get', 'go', 'going', 'good', 'got', 'gt', 'had', 'hard', 'has', 'have', 'he', 'her', 'here', 'how', 'if', 'into', 'isn', 'just', 'know', 'last', 'left', 'li', 'like', 'little', 'll', 'long', 'look', 'lot', 'lt', 'm', 'made', 'make', 'many', 'mb', 'me', 'menu', 'might', 'mm', 'more', 'most', 'much', 'name', 'nbsp', 'need', 'new', 'no', 'not', 'now', 'number', 'off', 'old', 'one', 'only', 'or', 'original', 'other', 'out', 'over', 'part', 'place', 'point', 'pretty', 'probably', 'problem', 'put', 'quite', 'quot', 'r', 're', 'really', 'results', 'right', 's', 'same', 'saw', 'see', 'set', 'several', 'she', 'sherree', 'should', 'since', 'size', 'small', 'so', 'some', 'something', 'special', 'still', 'stuff', 'such', 'sure', 'system', 't', 'take', 'than', 'their', 'them', 'then', 'there', 'these', 'they', 'thing', 'things', 'think', 'those', 'though', 'through', 'time', 'today', 'together', 'too', 'took', 'two', 'up', 'us', 'use', 'used', 'using', 've', 'very', 'want', 'way', 'well', 'went', 'were', 'what', 'when', 'where', 'which', 'while', 'white', 'who', 'will', 'would', 'your');
		$i = 0;
		foreach($acv as $k=>$v) {
			if (!array_search(strtolower($k),$bannedwords) and eregi("[a-zA-Z]",$k) and $i<=$MaxKeywords and strlen($k)>2) {
				if (isset($new_acv[strtolower($k)] )) 
					$new_acv[strtolower($k)] += $v;
				else 
					$new_acv[strtolower($k)] = $v;
				$i++;
			}
		}
		
		// Sort the keys alphabetically.
		ksort( $new_acv );
		
		// Create the Cloud
		$Cloud = '';
		foreach( $new_acv as $k=>$v) {
			$size = $v*0.4;
			$weight = $v*4;
			
			if ($size > 4) $size = 4;
			if ($weight > 50) $weight = 50;

            $rg = $this->blogInfo->getBlogRequestGenerator();
            $baseUrl = $rg->getBaseUrl();		
			$Cloud .= "<a href=\"{$baseUrl}?searchTerms=$k&op=Search&blogId={$blogId}\" style=\"font-size: {$size}em; font-weight: {$weight}\" title=\"$k\">$k</a> \n";
		}
	
		return $Cloud;
		
    }
	
	
}
        
        
?>