<?php
/**
 * Manage Users settings
 *
 * PHP version 5.0
 *
 * @category 	###Anova###
 * @package 	###Popular video audios###
 * @author 		karthiselvam_75ag08
 * @copyright 	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license 	http://www.agriya.com Agriya Infoway Licence
 * @version 	SVN: $Id:  $
 * @since 		2008-12-22
 */
/**
 * File having common configuration variables required for the entire project
 */
class PopularVideosAudiosHanlder extends AnovaHandler
	{
		public function showPopularNavigation($start, $limit, $recordCnt)
			{
				$showPopularNavigation_val = '';
				$popularUrl = getAnovaUrl('popularvideosaudios');

				$showPopularNavigation_val = '<div class="clsAnswersPreviousNext">
										<div class="clsShowAnotherQuestion">';

				if ($this->fields_arr['show'] == 0)
					{
						$showPopularNavigation_val .= '<p class="clsQusPre"><span>'.$this->LANG['common_paging_previous'].'</span></p>';
					}
				else
					{
						$show = ($this->fields_arr['show'] > 0)?($this->fields_arr['show']-1):0;
						$showPopularNavigation_val .= '<p class="clsQusPre"><a href="'.$popularUrl.'?show='.$show.'">'.$this->LANG['common_paging_previous'].'</a></p>';
					}
				if ($recordCnt < ($start + $limit + 1))
					{
						$showPopularNavigation_val .= '<p class="clsNoBorder clsQusNxt"><span>'.$this->LANG['common_paging_next'].'</span></p>';
					}
				else
					{
						$show = ($this->fields_arr['show']+1);
						$showPopularNavigation_val .= '<p class="clsNoBorder clsQusNxt"><a href="'.$popularUrl.'?show='.$show.'">'.$this->LANG['common_paging_next'].'</a></p>';
					}

				$showPopularNavigation_val .= '</div></div>';
				return $showPopularNavigation_val;
			}

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function displayPopularRecords()
			{
				$displayPopularRecords_arr = array();
				$start = 0;
				$limit = 10;
				if ($this->fields_arr['show'] > 0)
					{
						$start = $limit * $this->fields_arr['show'];
					}
				$video_types_for = array();
				$audio_types_for = array();

				if(chkIsVideoAllowed('question'))
					$video_types_for[] = 'Question';
				if(chkIsVideoAllowed('answer'))
					$video_types_for[] = 'Answer';
				if(chkVideoModuleAllowed('blog'))
					$video_types_for[] = 'Blog';
				if(chkVideoModuleAllowed('blogcomment'))
					$video_types_for[] = 'Blogcomment';
				if(chkVideoModuleAllowed('forumresponse'))
					$video_types_for[] = 'Forumresponse';
				if(chkIsVideoAllowed('debate'))
					$video_types_for[] = 'Debate';

				if(chkIsAudioAllowed('question'))
					$audio_types_for[] = 'Question';
				if(chkIsAudioAllowed('answer'))
					$audio_types_for[] = 'Answer';
				if(chkAudioModuleAllowed('blog'))
					$audio_types_for[] = 'Blog';
				if(chkAudioModuleAllowed('blogcomment'))
					$audio_types_for[] = 'Blogcomment';
				if(chkAudioModuleAllowed('forumresponse'))
					$audio_types_for[] = 'Forumresponse';
				if(chkIsAudioAllowed('debate'))
					$audio_types_for[] = 'Debate';

				$video_type_fors = implode('\',\'', $video_types_for);
				$audio_type_fors = implode('\',\'', $audio_types_for);

				$sql = 'SELECT \'video\' AS type, av.video_id AS type_id, av.content_id, av.video_for AS type_for, av.total_views AS view_count'.
						', UNIX_TIMESTAMP(av.date_added) as date_added'.
						' FROM '.$this->CFG['db']['tbl']['ans_video'].' AS av'.
						' WHERE av.total_views > 0 AND av.video_status = \'Ok\' AND av.video_encoded_status = \'Yes\''.
						' AND av.video_for IN (\''.$video_type_fors.'\') '.
						' AND av.latest_viewed >= DATE_SUB(NOW(), INTERVAL '.$this->CFG['admin']['popular_days']['limit'].' DAY)'.
						' UNION '.
						' SELECT \'audio\' AS type, ad.audio_id AS type_id, ad.content_id, ad.audio_for AS type_for, ad.total_views AS view_count'.
						', UNIX_TIMESTAMP(ad.date_added) as date_added'.
						' FROM '.$this->CFG['db']['tbl']['ans_audio'].' AS ad'.
						' WHERE ad.total_views > 0 AND ad.audio_status = \'Ok\' AND ad.audio_encoded_status = \'Yes\''.
						' AND ad.audio_for IN (\''.$audio_type_fors.'\') '.
						' AND ad.latest_viewed >= DATE_SUB(NOW(), INTERVAL '.$this->CFG['admin']['popular_days']['limit'].' DAY)'.
						' ORDER BY view_count DESC, type DESC';

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

				$this->_popularVideoAudioCnt = 0;
				$this->contentDisplayedArray = array();

				$recordCnt = $rs->PO_RecordCount();
				if(chkIsPaymentAllowed() and $this->CFG['plan']['plan_featured_video_count'] != $this->CFG['plan']['unlimited'])
					$recordCnt = $this->CFG['plan']['plan_featured_video_count'];

				if ($recordCnt)//if ($recordCnt == $rs->PO_RecordCount())
					{
						if($this->CFG['admin']['navigation']['top'])
							{
								$this->pagination_top = $this->showPopularNavigation($start, $limit, $recordCnt);
							}
						$dispCnt = 0;
						$inc = 1;
						while($row = $rs->FetchRow())
							{
								if($inc > $recordCnt)
									continue;

								$dispCnt++;

								if ($dispCnt > $start AND ($dispCnt < ($start + $limit + 1)) )
									{
										$displayPopularRecords_arr[$inc]['displayPopularVideoAudio_arr'] = $this->displayPopularVideoAudio($row);
										$displayPopularRecords_arr[$inc]['record'] = $row;
									}
								$inc++;
							} // while

						if($this->CFG['admin']['navigation']['bottom'])
							{
								$this->pagination_bottom = $this->showPopularNavigation($start, $limit, $recordCnt);
							}
					}

				return $displayPopularRecords_arr;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayPopularVideoAudio($row)
			{
				$contentDetails = $this->getContentDetails($row);

				if (!$contentDetails)
					return false;
				$displayPopularVideoAudio_arr = array();
				//increment popular video audio count
				$this->_popularVideoAudioCnt++;
				$type_id = $row['type_id'];
				$type_for = $row['type_for'];
				$displayPopularVideoAudio_arr['popularId'] = 'popular_'.$type_id.'_'.$this->_popularVideoAudioCnt;
				//popular video audio url
				$displayPopularVideoAudio_arr['popularVideoAudioUrl'] = getAnovaUrl('ansviewvideo', '?pg='.$type_for.'_'.$type_id, '?pg='.$type_for.'_'.$type_id);
				$displayPopularVideoAudio_arr['popularType'] = $row['type'];
				$displayPopularVideoAudio_arr['popularHeight'] = '';
				if ($row['type'] == 'audio')
					{
						$displayPopularVideoAudio_arr['popularVideoAudioUrl'] = getAnovaUrl('ansviewaudio', '?pg='.$type_for.'_'.$type_id, '?pg='.$type_for.'_'.$type_id);
						$displayPopularVideoAudio_arr['popularHeight'] = 'height';
					}
    			     $displayPopularVideoAudio_arr['contentDetails'] = $contentDetails;
				$userDetails = $this->getUserDetailsFromUsersTable($this->CFG['db']['tbl']['users'], $contentDetails['user_id']);
				$userDetails['uLink'] = getMemberUrl($userDetails['user_id'], $userDetails['name']);
				$displayPopularVideoAudio_arr['userDetails'] = $userDetails;
				return $displayPopularVideoAudio_arr;
			}
	}
//<<<<<-------------- Class PopularVideosAudiosHanlder begins ---------------//
//-------------------- Code begins -------------->>>>>//
#print_r($_POST);
$popularfrm = new PopularVideosAudiosHanlder();
$popularfrm->setPageBlockNames(array('form_popular_video_audio'));
//default form fields and values...
$popularfrm->setFormField('show', 0);
$popularfrm->setFormField('start', '');
$cfg_title = $LANG['popular_page_title'];
$popularfrm->sanitizeFormInputs($_REQUEST);
// Default page block
$popularfrm->setAllPageBlocksHide();
$popularfrm->setPageBlockShow('form_popular_video_audio');
if ($popularfrm->isShowPageBlock('form_popular_video_audio'))
	{
		$popularfrm->form_popular_video_audio['displayPopularRecords'] = $popularfrm->displayPopularRecords();
		$smartyObj->assign('smarty_paging_list', $popularfrm->populatePageLinksGET($popularfrm->getFormField('start')));
	}
//--------------------Page block templates begins-------------------->>>>>//
$CFG['html']['page_id'] .= 'popularVideosAudios';
$CFG['site']['title'] = $cfg_title.' - '.$CFG['site']['title'];
//include the header file
$popularfrm->includeHeader();
//include the content of the page
setTemplateFolder('general/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('popularVideosAudios.tpl');
//include the footer of the page
//<<<<<<--------------------Page block templates Ends--------------------//
$popularfrm->includeFooter();
?>