<?php
/**
 * ViewAudio
 *
 * @category	###Visual Answers###
 * @package     ViewAudio
 * @author 		shankar_76ag08
 * @copyright 	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @since 		2008-12-19
 *
 * */

class ViewAudio extends FormHandler
	{
		public $audio_details;

		/**
		 * ViewAudio::populateAudioDetails()
		 *
		 * @return
		 */
		public function populateAudioDetails()
			{
				$param_arr = explode('_', $this->fields_arr['pg']);

				$this->fields_arr['audio_for'] = $param_arr[0];
				$this->fields_arr['aid'] = $param_arr[1];

				$sql = 'SELECT audio_id, content_id, user_id, audio_ext, audio_size,'.
						' playing_time, date_added, audio_server_url'.
						' FROM '.$this->CFG['db']['tbl']['ans_audio'].' WHERE'.
						' audio_id='.$this->dbObj->Param('audio_id').' AND'.
						' audio_status=\'Ok\' AND'.
						' audio_encoded_status=\'Yes\' AND'.
						' audio_for='.$this->dbObj->Param('audio_for');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['aid'], $this->fields_arr['audio_for']));
				    if (!$rs)
					    trigger_db_error($this->dbObj);

				if($this->audio_details = $rs->FetchRow())
					return true;
				return false;
			}

		/**
		 * ViewAudio::resetFieldsArray()
		 *
		 * @return
		 */
		public function resetFieldsArray()
			{
				$this->setFormField('pg', '');
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function updateAudioTotalViews()
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_audio'].
						' SET total_views=total_views+1, latest_viewed=NOW()'.
						' WHERE audio_id='.$this->dbObj->Param($this->fields_arr['aid']);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['aid']));
				if (!$rs)
					    trigger_db_error($this->dbObj);
				$this->updateAudioViews();
			}
		public function updateAudioViews()
			{

				$param_arr = explode('_', $this->fields_arr['pg']);
				$this->fields_arr['audio_for'] = $param_arr[0];
				$this->fields_arr['aid'] = $param_arr[1];

				$sql = 'INSERT INTO  '.$this->CFG['db']['tbl']['video_audio_views'].
						' SET content_id = '.$this->dbObj->Param('aid').''.
						' , user_id = '.$this->CFG['user']['user_id'].''.
						' , content_type = \'Audio\''.
						' , content_for = '.$this->dbObj->Param('audio_for').''.
						' , date_viewed = NOW()';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['aid'], $this->fields_arr['audio_for']));
				if (!$rs)
					    trigger_db_error($this->dbObj);
			}

	}
//<<<<<-------------- Class ViewAudio begins ---------------//
//-------------------- Code begins -------------->>>>>//
$ViewAudio = new ViewAudio();

if(!isAjax())
	Redirect2URL($CFG['site']['url']);

$ViewAudio->setPageBlockNames(array('audio_display'));

//default form fields and values...
$ViewAudio->resetFieldsArray();

$ViewAudio->setAllPageBlocksHide();
$ViewAudio->sanitizeFormInputs($_REQUEST);

if($ViewAudio->isFormGETed($_REQUEST, 'pg'))
	{
		if(!$ViewAudio->populateAudioDetails())
			{
				$ViewAudio->setAllPageBlocksHide();
				$ViewAudio->setCommonErrorMsg($LANG['error_audio']);
				$ViewAudio->setPageBlockShow('block_msg_form_error');
			}
		else
			{
				$ViewAudio->setPageBlockShow('audio_display');
				$ViewAudio->updateAudioTotalViews();
			}
	}
//<<<<<-------------------- Code ends----------------------//
if ($ViewAudio->isShowPageBlock('audio_display'))
	{
		$ViewAudio->player_url = $CFG['site']['url'].$CFG['admin']['index']['anova_module'].'/files/flash/mp3_player/player.swf';
		$ViewAudio->audios_folder = $CFG['admin']['ans_audios']['audio_folder'];
		$ViewAudio->config_url = getAnovaUrl('ansaudioconfigxmlcode', '?pg=music_'.$ViewAudio->getFormField('aid'), '?pg=music_'.$ViewAudio->getFormField('aid'), 'root');
		$ViewAudio->playlist_url = getAnovaUrl('ansaudioplaylistxmlcode', '?pg=music_'.$ViewAudio->getFormField('aid'), '?pg=music_'.$ViewAudio->getFormField('aid'), 'root');

	}
//--------------------Page block templates begins-------------------->>>>>//
//include the header file
$ViewAudio->includeAjaxHeader();
//include the content of the page
setTemplateFolder('root/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('ansViewAudio.tpl');
//<<<<<-------------------- Page block templates ends -------------------//
$ViewAudio->includeAjaxFooter();
?>