<?php
/**
 * Answers rating page.
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Index###
 * @author 		shankar_76ag08
 * @copyright	copyright (c) 2010 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id:
 * @since 		2008-10-31
 */
/**
 * File having common configuration variables required for the entire project
 */
require_once('../common/configs/config.inc.php');
if ($CFG['admin']['module']['facebook'])
	{
		if (version_compare(PHP_VERSION,'5','>=')) {
			require_once ('../facebook/client/facebook.php');
		} else {
		 	require_once ('../facebook/php4client/facebook.php');
		  	require_once ('../facebook/php4client/facebookapi_php4_restlib.php');
		}
	}
$CFG['lang']['include_files'][] = 'languages/%s/'.$CFG['admin']['index']['anova_module'].'/answer.php';
$CFG['html']['header'] = 'general/html_header.php';
$CFG['html']['footer'] = 'general/html_footer.php';

$CFG['mods']['include_files'][] = 'common/classes/class_ListRecordsHandler.lib.php';
if ($CFG['admin']['module']['forums'])
	$CFG['mods']['include_files'][] = 'common/classes/class_ForumHandler.lib.php';

$CFG['site']['is_module_page'] = $CFG['admin']['index']['anova_module'];
//compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
$CFG['is']['ajax_page'] = true;
$CFG['auth']['is_authenticate'] = 'members';
/**
 * To include application top file
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');
//-------------- Class LoginFormHandler begins --------------->>>>>//
class AnswerRatingHandler extends FormHandler
	{
		/**
		 * to add rating details
		 *
		 * @return 		string
		 * @access 		public
		 */
		public function updateFavoriteContent()
			{
				$updateFavoriteContent_arr = array();
				$table_name = $this->CFG['db']['tbl']['user_bookmarked'];
				switch($this->fields_arr['ctype'])
					{
						case 'Question':
							$add_text = $this->LANG['answers_add_to_favorites'];
							$remove_text = $this->LANG['answers_remove_favorites'];
							break;
						case 'Forum':
							$add_text = $this->LANG['forum_add_to_favorites'];
							$remove_text = $this->LANG['forum_remove_favorites'];
							break;
						case 'User';
							$add_text = $this->LANG['common_user_add_to_favorites'];
							$remove_text = $this->LANG['common_user_remove_favorites'];
							break;
						case 'Debate';
							$add_text = $this->LANG['answers_add_to_favorites'];
							$remove_text = $this->LANG['answers_remove_favorites'];
							break;
					}
				$sql = 'SELECT bookmark_id FROM '.$table_name.
						' WHERE content_id = '.$this->dbObj->Param('content_id').
						' AND content_type = '.$this->dbObj->Param('content_type').
						' AND user_id = '.$this->dbObj->Param('user_id');
				$stmt = $this->dbObj->Prepare($sql);
				$result = $this->dbObj->Execute($stmt, array($this->fields_arr['cid'], $this->fields_arr['ctype'], $this->CFG['user']['user_id']));
				if (!$result)
						trigger_db_error($this->dbObj);

				if ($result->PO_RecordCount())
					{
						$sql = 'DELETE FROM '.$table_name.
								' WHERE content_id='.$this->dbObj->Param('content_id').
								' AND content_type = '.$this->dbObj->Param('content_type').
								' AND user_id='.$this->dbObj->Param('uid');
						$field_values_arr = array($this->fields_arr['cid'], $this->fields_arr['ctype'], $this->CFG['user']['user_id']);
						$favorite_text = $add_text;
						$favorite_class = 'clsFavourite';
					}
				else
					{
						$sql = 'INSERT INTO '.$table_name.
								' SET date_added=NOW()'.
								', content_id='.$this->dbObj->Param('content_id').
								', content_type='.$this->dbObj->Param('content_type').
								', user_id='.$this->dbObj->Param('user_id');
						$field_values_arr = array($this->fields_arr['cid'], $this->fields_arr['ctype'], $this->CFG['user']['user_id']);
						$favorite_text = $remove_text;
						$favorite_class = 'clsRemovFrmFav';
					}
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, $field_values_arr);
				if (!$rs)
					    trigger_db_error($this->dbObj);

				$updateFavoriteContent_arr['favorite_answers']['title'] = $favorite_text;
				$updateFavoriteContent_arr['favorite_answers']['class'] = $favorite_class;

				return $updateFavoriteContent_arr;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function updateIgnoreUser()
			{
				$updateIgnoreUser_arr = array();
				$uid = $this->CFG['user']['user_id'];
				$ignore_id = $this->fields_arr['ignore_id'];
				$sql = 'SELECT 1 FROM '.$this->CFG['db']['tbl']['user_ignored'].
						' WHERE user_id = '.$this->dbObj->Param($uid).
						' AND ignored_id = '.$this->dbObj->Param($ignore_id);
				$stmt = $this->dbObj->Prepare($sql);
				$result = $this->dbObj->Execute($stmt, array($uid, $ignore_id));
				if (!$result)
						trigger_db_error($this->dbObj);

				if ($result->PO_RecordCount())
					{
						$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['user_ignored'].
								' WHERE user_id = '.$this->dbObj->Param($uid).
								' AND ignored_id = '.$this->dbObj->Param($ignore_id);
						$field_values_arr = array($uid, $ignore_id);
						$favorite_text = $this->LANG['myanswers_ignore_user'];
					}
				else
					{
						$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['user_ignored'].
								' SET user_id = '.$this->dbObj->Param($uid).
								', ignored_id = '.$this->dbObj->Param($ignore_id);
						$field_values_arr = array($uid, $ignore_id);
						$favorite_text = $this->LANG['myanswers_unignore_user'];
					}
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, $field_values_arr);
				if (!$rs)
					    trigger_db_error($this->dbObj);

				$updateIgnoreUser_arr['favorite_answers']['title'] = $favorite_text;
				return $updateIgnoreUser_arr;
			}
	}
//----------------------------- Code begins ------------------>>>>>//
$answerRating = new AnswerRatingHandler();
$answerRating->setPageBlockNames(array('favorite_content', 'ignore_user'));

$answerRating->setFormField('cid', '');
$answerRating->setFormField('ignore_id', '');
$answerRating->setFormField('ctype', '');

$answerRating->favorite_answers_url = getAnovaUrl('favoriteanswers');

$answerRating->sanitizeFormInputs($_REQUEST);
if ($answerRating->isFormGETed($_GET, 'cid'))
	{
		$answerRating->favorite_content_arr = array();
		$answerRating->favorite_content_arr = $answerRating->updateFavoriteContent();
		$answerRating->setPageBlockShow('favorite_content');
	}
elseif ($answerRating->isFormGETed($_GET, 'ignore_id'))
	{
		$answerRating->ignore_user_arr = array();
		$answerRating->ignore_user_arr = $answerRating->updateIgnoreUser();
		$answerRating->setPageBlockShow('ignore_user');
	}
//<<<<<------------------------- Code ends ----------------------------------//
//include the header file
$answerRating->includeAjaxHeader();
//include the content of the page
setTemplateFolder('members/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('favoriteAnswers.tpl');
//<<<<<<--------------------Page block templates Ends--------------------//
//include the footer of the page
$answerRating->includeAjaxFooter();
?>