<?php
/**
 * rss generator
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		RssFormHandler
 * @author 		senthil_52ag05
 * @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
 */
require_once('../common/configs/config.inc.php');
$CFG['site']['is_module_page'] = $CFG['admin']['index']['anova_module'];
$CFG['html']['header'] = $CFG['admin']['index']['anova_module'].'/general/rss_header.php';
$CFG['html']['footer'] = $CFG['admin']['index']['anova_module'].'/general/rss_footer.php';

$CFG['benchmark']['is_expose_parse_time'] = false;
$CFG['debug']['is_debug_mode'] = false;
$CFG['debug']['is_db_debug_mode'] = false;

//compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
/**
 * To include application top file
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');

class RssFormHandler extends FormHandler
	{
		 /**
		 * to convert Date
		 *
		 * @param date $date_time
		 * @return string
		 * @access public
		 **/
		public function convertDate($date_time)
			{
				return strftime( "%a, %d %b %Y %T %Z" , strtotime($date_time));
			}

		/**
		 * to strip sting
		 *
		 * @param string $str
		 * @param string $content_len
		 * @return string
		 * @access public
		 **/
		public function stripString($str, $content_len)
			{
				$str = strip_tags($str,'ENT_QUOTES');
				if (strlen($str) > $content_len )
					$str = substr($str, 0, $content_len).'...';
				return strip_tags($str,'ENT_QUOTES');
			}

		/**
		 * To generate item list
		 *
		 * @param string $title
		 * @param string $link
		 * @param string $content
		 * @param date $date
		 * @param integer $content_len
		 * @return string
		 * @access public
		 **/
		public function generateItemList($title, $link, $content, $date, $content_len)
			{
				$title1 = seoFriendlyText($title, 'UTF-8');
				//$content = seoFriendlyText($content, 'UTF-8');
?>
					     <item>
					        <title> <?php echo $title; ?></title>
					        <link><?php echo $link;?></link>
					        <description> <?php echo $this->stripString($content, $content_len);?></description>
					        <pubDate><?php echo $this->convertDate($date); ?></pubDate>
					     </item>
<?php
			}

		/**
		 * RssFormHandler::showCategoryName()
		 *
		 * @param mixed $cat_id
		 * @return
		 */
		public function showCategoryName($cat_id)
		    {
				$sql = 'SELECT cat_name, seo_title, cat_id FROM '.$this->CFG['db']['tbl']['questions_category'].' WHERE cat_id='.$this->dbObj->Param('cat_id');
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($cat_id));
				if (!$rs)
					    trigger_db_error($this->dbObj);
				$cat_name = '';
				if ($rs->PO_RecordCount())
					{
						$row = $rs->FetchRow();
						$cat_name = $row['cat_name'];
					}
				return $cat_name;
		    }


		/**
		 * RssFormHandler::getRecentQuestions()
		 * To get recent questions
		 *
		 * @param integer $limit
		 * @param integer $content_len
		 * @return void
		 * @access public
		 **/
		public function getRecentQuestions($limit = 100, $content_len = 5000)
			{
				$sql = 'SELECT q.ques_id, q.total_answer, q.cat_id, q.pcat_id, q.status, q.total_stars, q.question, date_asked'.
						', '.$this->getUserTableField('display_name').' as asked_by, '.$this->getUserTableFields(array('image_path', 'name', 'gender'), false).' q.user_id'.
						', video_id, audio_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\''.
						' ORDER BY ques_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);
				while($row = $rs->FetchRow())
					{
						$link = getAnovaUrl('answer', '?title='.$row['seo_title'], $row['seo_title'].'/');
						$description = '';
						$catagory_name = '';
						$catagory_name = $this->showCategoryName($row['cat_id']);
						if (!$catagory_name)
							$catagory_name = $this->showCategoryName($row['pcat_id']);
						if ($catagory_name)
							$description .= $this->LANG['common_in'].' '.$catagory_name.' - ';
						$description .= '<span class="clsForumAuthor">'.$this->LANG['common_question_asked_by'].' '.$row['asked_by'].'</span>';
						$description .= ' - <span>'.$row['total_answer'].' '.$this->LANG['answers'].' ('.$row['total_videos'].' '.$this->LANG['common_total_videos'].' | '.$row['total_audios'].' '.$this->LANG['common_total_audios'].')</span>';

						$this->generateItemList($row['question'], $link, $description, $row['date_asked'], $content_len);
					}
			}

		/**
		 * RssFormHandler::getPopularQuestions()
		 * To get recent questions
		 *
		 * @param integer $limit
		 * @param integer $content_len
		 * @return void
		 * @access public
		 **/
		public function getPopularQuestions($limit = 100, $content_len = 5000)
			{
				$sql = 'SELECT q.ques_id, q.total_answer, q.cat_id, q.pcat_id, q.status, q.total_stars, q.question, date_asked'.
						', '.$this->getUserTableField('display_name').' as asked_by, '.$this->getUserTableFields(array('image_path', 'name', 'gender'), false).' q.user_id'.
						', video_id, audio_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 total_views > 0 ORDER BY total_views DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);
				while($row = $rs->FetchRow())
					{
						$link = getAnovaUrl('answer', '?title='.$row['seo_title'], $row['seo_title'].'/');
						$description = '';
						$catagory_name = '';
						$catagory_name = $this->showCategoryName($row['cat_id']);
						if (!$catagory_name)
							$catagory_name = $this->showCategoryName($row['pcat_id']);
						if ($catagory_name)
							$description .= $this->LANG['common_in'].' '.$catagory_name.' - ';
						$description .= '<span class="clsForumAuthor">'.$this->LANG['common_question_asked_by'].' '.$row['asked_by'].'</span>';
						$description .= ' - <span>'.$row['total_answer'].' '.$this->LANG['answers'].' ('.$row['total_videos'].' '.$this->LANG['common_total_videos'].' | '.$row['total_audios'].' '.$this->LANG['common_total_audios'].')</span>';

						$this->generateItemList($row['question'], $link, $description, $row['date_asked'], $content_len);
					}
			}

		/**
		 * RssFormHandler::getRecentForums()
		 *
		 * To get recent forums
		 *
		 * @param integer $limit
		 * @param integer $content_len
		 * @return
		 */
		public function getRecentForums($limit = 100, $content_len = 5000)
			{
				$sql = 'SELECT ft.forum_id, ft.topic_id, ft.seo_title, ft.forum_topic, ft.user_id, ft.total_response'.
						', ft.total_views, ft.last_post_user_id, ft.last_post_date, date_added'.
						', u.'.$this->getUserTableField('user_id').' AS topic_user_id, u.'.$this->getUserTableField('display_name').' AS topic_user_name'.
						', u1.'.$this->getUserTableField('user_id').' AS last_user_id, u1.'.$this->getUserTableField('display_name').' AS last_user_name'.
						' FROM '.$this->CFG['db']['tbl']['forum_topics'].' AS ft'.
						' LEFT JOIN '.$this->CFG['db']['tbl']['users'].' AS u1 ON ft.last_post_user_id = u1.'.$this->getUserTableField('user_id').
						', '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE ft.user_id = u.'.$this->getUserTableField('user_id').
						' AND u.'.$this->getUserTableField('usr_status').' = \'Ok\''.
						' AND topic_status = \'Yes\''.
						' ORDER BY ft.topic_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);
				while($row = $rs->FetchRow())
					{
						if (!($row['title'] = $this->getForumTitle($row['forum_id'])))
							continue;
						$link = getForumsUrl('forumsresponses', '?title='.$row['title'].'&amp;topic_title='.$row['seo_title'], $row['title'].'/'.$row['seo_title'].'/');
						$description = ' '.$this->LANG['common_forum_started_by'].': '.$row['topic_user_name'].' ('.$row['total_response'].' '.$this->LANG['common_total_responses'].' | '.$row['total_views'].' '.$this->LANG['common_total_views'].')';

						$this->generateItemList($row['forum_topic'], $link, $description, $row['date_added'], $content_len);
					}
			}

		/**
		 * RssFormHandler::getRecentBlogs()
		 *
		 * @param integer $limit
		 * @param integer $content_len
		 * @return
		 */
		public function getRecentBlogs($limit = 100, $content_len = 5000)
			{
				$sql = 'SELECT blog_id, subject, seo_title, message, total_comments, total_views, b.user_id, date_added'.
						' FROM '.$this->CFG['db']['tbl']['blogs'].' AS b'.
						' WHERE status=\'Active\''.
						' ORDER BY blog_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);
				while($row = $rs->FetchRow())
					{
						$user_details = $this->getUserDetailsFromUsersTable($this->CFG['db']['tbl']['users'], $row['user_id']);

						$link = getBlogUrl('blogcomment', '?title='.$row['seo_title'], $row['seo_title'].'/');
						$description = $this->LANG['common_comment_posted_by'].': '.ucwords(stripString($user_details['display_name'], $this->CFG['username']['medium_length'])).' ('.$row['total_comments'].' '.$this->LANG['common_total_comments'].' | '.$row['total_views'].' '.$this->LANG['common_total_views'].')';
						$description .= ' '.$row['message'];

						$this->generateItemList($row['subject'], $link, $description, $row['date_added'], $content_len);
					}
			}
	}

//<<<<<-------------- Class RssFormHandler begins ---------------//

//-------------------- Code begins -------------->>>>>//
$rssfrm = new RssFormHandler();

//include the header file
$rssfrm->includeHeader();

$rssfrm->setFormField('act', '');
$rssfrm->setFormField('rcnt', 25);
$rssfrm->setFormField('desc_cnt', 750);

$rssfrm->sanitizeFormInputs($_REQUEST);

if ($rssfrm->isFormGETed($_GET, 'act'))
	{
		switch($rssfrm->getFormField('act')){
			case 'popularquestions':
?>
						<title><?php echo $CFG['site']['name'];?>: <?php echo $LANG['common_popular_question_title'];?></title>
<?php
						$rssfrm->getPopularQuestions($rssfrm->getFormField('rcnt'), $rssfrm->getFormField('desc_cnt'));
				break;
			case 'recentforums':
?>
						<title><?php echo $CFG['site']['name'];?>: <?php echo $LANG['common_recent_forum_title'];?></title>
<?php
						$rssfrm->getRecentForums($rssfrm->getFormField('rcnt'), $rssfrm->getFormField('desc_cnt'));
				break;
			case 'recentblogs':
?>
						<title><?php echo $CFG['site']['name'];?>: <?php echo $LANG['common_recent_blog_title'];?></title>
<?php
						$rssfrm->getRecentBlogs($rssfrm->getFormField('rcnt'), $rssfrm->getFormField('desc_cnt'));
				break;
			default:
?>
						<title><?php echo $CFG['site']['name'];?>: <?php echo $LANG['common_recent_question_title'];?></title>
<?php
						$rssfrm->getRecentQuestions($rssfrm->getFormField('rcnt'), $rssfrm->getFormField('desc_cnt'));
		} // switch
	}
	else
		{
?>
			<title><?php echo $CFG['site']['name'];?>: <?php echo $LANG['common_recent_question_title'];?></title>
<?php
			$rssfrm->getRecentQuestions($rssfrm->getFormField('rcnt'), $rssfrm->getFormField('desc_cnt'));
		}

$rssfrm->includeFooter();
?>