<?php
/**
 * File handling question category
 *
 * @package
 * @author karthiselvam_75ag08
 * @copyright Copyright (c) 2009
 * @version $Id: $
 * @access public
 **/
class CategoryHandler extends FormHandler
	{
		/**
		 * Tag::displayCategory()
		 *
		 * @return
		 **/
		public function displayCategory()
			{
				$sql = 'SELECT cat_id, cat_name, seo_title, has_child, total_questions'.
						' FROM '.$this->CFG['db']['tbl']['questions_category'].
						' WHERE status=\'1\' AND parent_id=0 ';

				$orderSql = ' ORDER BY cat_name ASC';
				if ($this->fields_arr['s']=='f')
					$orderSql = ' ORDER BY total_questions DESC, cat_name ASC';

				$sql .= $orderSql;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			    if (!$rs)
				    trigger_db_error($this->dbObj);

				//create displayCategory array for display...
				$displayCategory = array();
				if ($rs->PO_RecordCount())
				    {
						$displayCategory['strings']['questionsLI'] = '';
						$displayCategory['strings']['alphabetLI'] = '';
						if ($this->fields_arr['s']=='a')
							{
								$displayCategory['strings']['alphabetLI'] = 'clsLeftActiveQuestionsLink';
							}
				    	if ($this->fields_arr['s']=='f')
				    		{
								$displayCategory['strings']['questionsLI'] = 'clsLeftActiveQuestionsLink';
							}
						$inc=0;
						while($row = $rs->FetchRow())
							{
								$displayCategory['array'][$inc]['record']=$row;
								$displayCategory['array'][$inc]['displaySubCategoy'] = $this->displaySubCategoy($row['cat_id']);
								$displayCategory['array'][$inc]['cat_url']=getAnovaUrl('questions', '?cat='.$row['seo_title'], 'dir/'.$row['seo_title'].'/');
								$inc++;
							}
					}
				return 	$displayCategory;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displaySubCategoy($parent_id)
			{
				$sql = 'SELECT cat_id, cat_name, seo_title, total_questions'.
						' FROM '.$this->CFG['db']['tbl']['questions_category'].
						' WHERE status=\'1\' AND parent_id='.$this->dbObj->Param($parent_id);

				$orderSql = ' ORDER BY cat_name ASC';
				if ($this->fields_arr['s']=='f')
					$orderSql = ' ORDER BY total_questions DESC, cat_name ASC';

				$sql .= $orderSql;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($parent_id));
				if (!$rs)
					    trigger_db_error($this->dbObj);

				$displaySubCategoy_arr = array();
				if ($rs->PO_RecordCount())
					{
						$i=1;
						while($row = $rs->FetchRow())
							{
								$displaySubCategoy_arr[$i]['record'] = $row;
								$displaySubCategoy_arr[$i]['url'] = getAnovaUrl('questions', '?cat='.$row['seo_title'], 'dir/'.$row['seo_title'].'/');

								$displaySubCategoy_arr[$i]['total_questions'] = '';
								if ($row['total_questions'])
									{
										$displaySubCategoy_arr[$i]['total_questions'] = '('.$row['total_questions'].')';
									}
								$i++;
							}
					}
				return $displaySubCategoy_arr;
			}
	}
//<<<<<-------------- Class Tag begins ---------------//
//-------------------- Code begins -------------->>>>>//
$category = new CategoryHandler();
$category->setPageBlockNames(array('form_question_category'));
$category->setFormField('s', 'a');
$category->setAllPageBlocksHide();
$category->setPageBlockShow('form_question_category');
$category->sanitizeFormInputs($_REQUEST);
//<<<<<-------------------- Code ends----------------------//
//-------------------- Page block templates begins -------------------->>>>>//
$category->category_top['cat_alpha']= getAnovaUrl('categories', '?s=a', '?s=a');
$category->category_top['cat_freq']= getAnovaUrl('categories', '?s=f', '?s=f');
if ($category->isShowPageBlock('form_question_category'))
    {
		$category->form_question_category['displayCategory'] = $category->displayCategory();
	}
//<<<<<-------------------- Page block templates ends ------------------>>>>//
//include the header file
$category->includeHeader();
//include the content of the page
setTemplateFolder('general/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('categories.tpl');
//includ the footer of the page
//<<<<<<--------------------Page block templates Ends--------------------//
$category->includeFooter();
?>