<?php
/**
 * VideoUpload
 *
 * @category	Anova
 * @package     VideoUpload
 * @author 		shankar_76ag08
 * @copyright 	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @since 		2008-11-13
 **/
class VideoUpload extends FormHandler
	{
		public $video_details;

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

				$this->fields_arr['video_for'] = $param_arr[0];
				$this->fields_arr['vid'] = $param_arr[1];

				$sql = 'SELECT video_id, content_id, user_id, video_ext, video_size,'.
						' playing_time, date_added, t_width, t_height, video_server_url'.
						' FROM '.$this->CFG['db']['tbl']['ans_video'].' WHERE'.
						' video_id='.$this->dbObj->Param('video_id').' AND'.
						' video_status=\'Ok\' AND'.
						' video_encoded_status=\'Yes\' AND'.
						' video_for='.$this->dbObj->Param('video_for');

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

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

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

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		/**
		 * VideoUpload::updateVideoTotalViews()
		 *
		 * @return
		 */
		public function updateVideoTotalViews()
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video'].
						' SET total_views=total_views+1, latest_viewed=NOW()'.
						' WHERE video_id='.$this->dbObj->Param($this->fields_arr['vid']);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['vid']));
				if (!$rs)
					    trigger_db_error($this->dbObj);
				$this->updateVideoViews();
			}
		public function updateVideoViews()
			{

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

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

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

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

if(!isAjax())
	Redirect2URL($CFG['site']['url']);
$VideoUpload->setPageBlockNames(array('video_display'));
//default form fields and values...
$VideoUpload->resetFieldsArray();
$VideoUpload->setAllPageBlocksHide();
$VideoUpload->sanitizeFormInputs($_REQUEST);

if($VideoUpload->isFormGETed($_REQUEST, 'pg'))
	{
		if(!$VideoUpload->populateVideoDetails())
			{
				$VideoUpload->setAllPageBlocksHide();
				$VideoUpload->setCommonErrorMsg($LANG['error_video']);
				$VideoUpload->setPageBlockShow('block_msg_form_error');
			}
		else
			{
				$VideoUpload->setPageBlockShow('video_display');
				$VideoUpload->updateVideoTotalViews();

			}
	}
//<<<<<-------------------- Code ends----------------------//

if ($VideoUpload->isShowPageBlock('video_display'))
	{
		$VideoUpload->videos_folder = $CFG['admin']['ans_videos']['folder'];
		$VideoUpload->flv_player_url = $CFG['site']['url'].$CFG['admin']['index']['anova_module'].'/files/flash/flv_player/flvplayer_elite.swf';
		$VideoUpload->arguments_play = 'pg=video_'.$VideoUpload->getFormField('vid');
		$VideoUpload->configXmlcode_url = $CFG['site']['url'].$CFG['admin']['index']['anova_module'].'/elite_videoConfigXmlCode.php?';

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