<?php
/**
 * Tag
 *
 * @category	Anova
 * @package		TagCloudHandler
 * @author 		karthiselvam_52ag05
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id: $
 * @since 		2008-12-19
 **/
class TagCloudHandler extends FormHandler
	{
		/**
		 * Tag::populateTags()
		 *
		 * @return
		 **/
		public function populateTags()
			{
				$this->populateTags_arr	=	array();
				if ($this->fields_arr['t'] == 'search')
					{
						$sql = 'SELECT tag_name, search_count AS tag_count FROM '.$this->CFG['db']['tbl']['tags'];
						$sql .= ' WHERE search_count>0 AND total_count>0';
						$orderSql = ' ORDER BY search_count DESC';
					}
				else
					{
						$sql = 'SELECT tag_name, total_count AS tag_count FROM '.$this->CFG['db']['tbl']['tags'];
						$sql .= ' WHERE total_count>0';
						$orderSql = ' ORDER BY total_count DESC';
					}
				$sql .= $orderSql.' LIMIT 0, '.$this->CFG['admin']['tags_count'];
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			    if (!$rs)
				    trigger_db_error($this->dbObj);

				if ($rs->PO_RecordCount()>0)
				    {
						$this->populateTags_arr['RecordCount']	=	1;
				    	$resultArray = array();
						while($row = $rs->FetchRow())
							{
								 $resultArray[] = $row;
							}
						if ($this->fields_arr['s']=='a')
							{
								foreach ($resultArray as $key => $row)
									{
									    $tag_name[$key]  = strtolower($row['tag_name']);
									}
								array_multisort($tag_name, SORT_ASC, $resultArray);
							}
				    	$this->populateTags_arr['alphabetLI']	=	$this->populateTags_arr['questionsLI'] = '';
						$this->populateTags_arr['alphaClass']	=	$this->populateTags_arr['freqClass'] = '';
				    	if ($this->fields_arr['s']=='a')
							{
								$this->populateTags_arr['alphabetLI']	=	'clsLeftActiveQuestionsLink';
							}
				    	if ($this->fields_arr['s']=='f')
				    		{
								$this->populateTags_arr['questionsLI']	=	'clsLeftActiveQuestionsLink';
							}
						$this->populateTags_arr['tags_alp_url']	=	getAnovaUrl('tags','?t='.$this->fields_arr['t'].'&amp;s=a', '?t='.$this->fields_arr['t'].'&amp;s=a');
						$this->populateTags_arr['tags_fre_url']	=	getAnovaUrl('tags','?t='.$this->fields_arr['t'].'&amp;s=f', '?t='.$this->fields_arr['t'].'&amp;s=f');

						$classes = array('clsTagStyleBlue', 'clsTagStyleGrey', 'clsTagStyleGreen', 'clsTagStyleRed');
						$tagClassArray = array();
						foreach($resultArray as $row)
							{
								$tagArray[$row['tag_name']] = $row['tag_count'];
								$class = $classes[rand(0, count($classes))%count($classes)];
								$tagClassArray[$row['tag_name']] = $class;
							}
						$tagArray = $this->setFontSizeInsteadOfSearchCount($tagArray);
						$this->populateTags_arr['row']	=	array();
						$inc=0;
						foreach($tagArray as $tag=>$fontSize)
							{
								$this->populateTags_arr['row'][$inc]['url']	=	getAnovaUrl('questions','?view=search&amp;tags='.$tag, 'search/?tags='.$tag);
								$this->populateTags_arr['row'][$inc]['class']	=	'clsTagStyleFont'.$fontSize;
								$this->populateTags_arr['row'][$inc]['tag']	=	$tag;
								$inc++;
							}
				    }
				else
					{
						$this->populateTags_arr['RecordCount']	=	0;
					}
				return 	$this->populateTags_arr;
			}
	}
//<<<<<-------------- Class Tag begins ---------------//
//-------------------- Code begins -------------->>>>>//
$Tag = new TagCloudHandler();
$Tag->setFormField('t', 'freq');
$Tag->setFormField('s', 'a');

$Tag->setAllPageBlocksHide();
$Tag->setPageBlockShow('allTagBlock');

$Tag->sanitizeFormInputs($_REQUEST);

$Tag->tagCommon['type_title']	=	$type_title = $LANG['common_top_tags_title'];
if ($Tag->getFormField('t') == 'search')
	$Tag->tagCommon['type_title']	=	$type_title = $LANG['common_top_search_tags_title'];

//<<<<<-------------------- Code ends----------------------//
//-------------------- Page block templates begins -------------------->>>>>//
if ($Tag->isShowPageBlock('allTagBlock'))
    {
		$Tag->allTagBlock['populateTags']	=	$Tag->populateTags();
	}
//include the header file
$Tag->includeHeader();
//include the content of the page
setTemplateFolder('general/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('tags.tpl');
//includ the footer of the page
//<<<<<-------------------- Page block templates ends ------------------>>>>//
$Tag->includeFooter();
?>