<?php
/**
 * embed content
 *
 * @package
 * @author senthil_52ag05
 * @copyright Copyright (c) 2009
 * @version $Id: $
 * @access public
 **/
//-------------- Class EmbedHandler begins --------------->>>>>//
class EmbedHandler extends AnovaHandler
	{
		/**
		 * to display embed content
		 *
		 * @return 		string
		 * @access 		public
		 */
		public function displayEmbedContent()
			{
				switch($this->fields_arr['embed'])
					{
						case 'user':
							$this->embedUserDetails();
							break;
						case 'question':
							$this->embedQuestionDetails();
							break;
					}
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function embedUserDetails()
			{
				global $smartyObj;
				if ($userDetails = $this->chkIsValidUser($this->fields_arr['user']))
					{
						$embedUserDetails_arr = array();
						$uid 		= $userDetails['user_id'];
						$userLog 	= $this->getUserLog($uid);
						$uname 		= $userDetails['name'];

						$embedUserDetails_arr['userDetails'] = $userDetails;
						$embedUserDetails_arr['userLog'] = $userLog;

                    	$embedUserDetails_arr['myanswers']['url'] = getMemberUrl($userDetails['user_id'], $userDetails['name']);
						$embedUserDetails_arr['display_name_manual'] = stripString($userDetails['display_name'], $this->CFG['username']['medium_length']);
						$embedUserDetails_arr['questions']['url'] = getAnovaUrl('questions', '?view=search&uname='.$uname.'&opt=ques', 'search/?uname='.$uname.'&opt=ques', 'root');
                    	$embedUserDetails_arr['resolved_questions']['url'] = getAnovaUrl('questions', '?view=search&uname='.$uname.'&opt=resolve', 'search/?uname='.$uname.'&opt=resolve', 'root');
						$embedUserDetails_arr['getResolvedQuestions'] = $this->getResolvedQuestions($uid);
                    	$embedUserDetails_arr['answered_questions']['url'] = getAnovaUrl('questions', '?view=search&uname='.$uname.'&opt=ans', 'search/?uname='.$uname.'&opt=ans', 'root');

						$smartyObj->assign('embedUserDetails_arr', $embedUserDetails_arr);
						setTemplateFolder('root/', $this->CFG['admin']['index']['anova_module']);
						$smartyObj->display('embedUserDetails.tpl');
					}
			}

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function chkIsValidUser($username)
		    {
				if (!$username)
					return false;

				$sql = 'SELECT '.$this->getUserTableFields(array('user_id', 'name', 'display_name', 'image_path', 'gender', 't_height', 't_width', 'photo_server_url', 'photo_ext')).
						', u.'.$this->getUserTableField('user_id').' AS img_user_id'.
						' FROM '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE u.'.$this->getUserTableField('name').'='.$this->dbObj->Param($username).
						' AND u.'.$this->getUserTableField('usr_status').'=\'Ok\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($username));
				if (!$rs)
				        trigger_db_error($this->dbObj);
				if (!$rs->PO_RecordCount())
					{
						return false;
					}
				$row = $rs->FetchRow();
				$row['name'] = $row['name'];
				return $row;
		    }

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function embedQuestionDetails()
			{
				global $smartyObj;
				$qid = $this->fields_arr['qid'];
				if ($questionDetails = $this->chkIsValidQuestion($qid))
					{
						$embedQuestionDetails_arr = array();
						$embedQuestionDetails_arr['questionDetails'] = $questionDetails;


    					$embedQuestionDetails_arr['myanswers']['url'] = getMemberUrl($questionDetails['user_id'], $questionDetails['name']);

						$embedQuestionDetails_arr['asked_by_manual'] = ucwords(stripString($questionDetails['asked_by'], $this->CFG['username']['medium_length']));
    					$embedQuestionDetails_arr['answer']['url'] = getAnovaUrl('answer', '?title='.$questionDetails['seo_title'], $questionDetails['seo_title'].'/', 'root');

    					$embedQuestionDetails_arr['description_manual'] = nl2br(wordWrapManual($questionDetails['description'], 100));
    					$embedQuestionDetails_arr['asked_by_manual2'] = stripString($questionDetails['asked_by'], $this->CFG['username']['short_length']);

    					$embedQuestionDetails_arr['be_first_answer']['url'] = getAnovaUrl('answer', '?title='.$questionDetails['seo_title'], $questionDetails['seo_title'].'/', 'members');

				      	$cat_id = $questionDetails['cat_id'];
				  		if (!$cat_id)
				  			$cat_id = $questionDetails['pcat_id'];
					  	$embedQuestionDetails_arr['showCategoryName'] = $this->showCategoryName($cat_id);


			      		if ($questionDetails['total_answer'])
						  	{
					  			$embedQuestionDetails_arr['video_plural'] = $this->LANG['video'];
								$embedQuestionDetails_arr['audio_plural'] = $this->LANG['common_total_audio'];
								$embedQuestionDetails_arr['answer_plural'] = $this->LANG['common_total_answer'];
								if($questionDetails['total_videos']!= 1) $embedQuestionDetails_arr['video_plural'] = $this->LANG['common_total_videos'];
								if($questionDetails['total_audios']!= 1) $embedQuestionDetails_arr['audio_plural'] = $this->LANG['common_total_audios'];
								if($questionDetails['total_answer']!= 1) $embedQuestionDetails_arr['answer_plural'] = $this->LANG['answers'];
							}
						$smartyObj->assign('embedQuestionDetails_arr', $embedQuestionDetails_arr);
						setTemplateFolder('root/', $this->CFG['admin']['index']['anova_module']);
						$smartyObj->display('embedQuestionDetails.tpl');
					}
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function chkIsValidQuestion($qid)
			{
				if (!$qid)
					return false;
				$sql = 'SELECT q.ques_id, q.total_answer, q.pcat_id, q.cat_id, q.best_ans_id, q.description, q.total_stars, q.question, UNIX_TIMESTAMP(date_asked) as date_asked'.
						', q.video_id, q.audio_id, '.$this->getUserTableField('display_name').' as asked_by, u.'.$this->getUserTableField('user_id').' as img_user_id,'.
						$this->getUserTableFields(array('t_height', 'name', 't_width', 's_height', 's_width','photo_server_url', 'photo_ext')).' , '.
						$this->getUserTableFields(array('image_path', 'gender'), false).'q.status, q.user_id'.
						', IF(status=\'Open\' AND date_closed>=NOW(), 1, 0) is_open, TIMEDIFF(date_closed, NOW()) as date_closed'.
						', q.total_videos, q.total_audios, q.seo_title FROM '.$this->CFG['db']['tbl']['questions'].' AS q, '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE q.user_id=u.'.$this->getUserTableField('user_id').' AND q.status IN (\'Open\', \'Resolved\')'.
						' AND u.'.$this->getUserTableField('usr_status').'=\'Ok\' AND q.ques_id='.$this->dbObj->Param($qid);

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($qid));
				if (!$rs)
				        trigger_db_error($this->dbObj);
				if (!$rs->PO_RecordCount())
					return false;

				$row = $rs->FetchRow();
				return $row;
			}

		/**
		 * EmbedHandler::includeAjaxHeader()
		 *
		 * @param mixed $assign_var
		 * @return
		 */
		public function includeAjaxHeader($assign_var = true)
			{
				global $CFG, $smartyObj;
				if($assign_var)
					{
						$this->assignSmartyVariables();
					}
			}

	}
//----------------------------- Code begins ------------------>>>>>//
$embedcontent = new EmbedHandler();

$embedcontent->setFormField('embed', '');
$embedcontent->setFormField('user', '');
$embedcontent->setFormField('qid', '');

$embedcontent->includeAjaxHeader();
$embedcontent->sanitizeFormInputs($_REQUEST);
if ($embedcontent->isFormGETed($_GET, 'embed'))
	{
		ob_start();
		$documentWrite = true;
		$onlyContent = 0;
		if (isset($_REQUEST['content']) AND $onlyContent = $_REQUEST['content'])
			{
				$documentWrite = false;
			}
		echo ($onlyContent == 2)?'<div id="userbadge">':'';

		$embedcontent->displayEmbedContent();

		echo ($onlyContent == 2)?'</div>':'';

		$str = ob_get_clean();
		$str = trim($str);
		$str = str_replace(array("\n", "\t", "\r", "'"), array("", "", "", "&rsquo;"), $str);
		if ($documentWrite)
			{
				print 'document.writeln(\''.$str.'\');';
			}
		else
			{
				print $str;
			}
	}
//<<<<<------------------------- Code ends ----------------------------------//
?>
