<?php
//-------------- Class WidgetFormHandler begins --------------->>>>>//
/**
 * To generate widgets
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		WidgetFormHandler
 * @author 		shankar_76ag08
 * @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-19
 */
class WidgetFormHandler extends AnovaHandler
	{

		protected $widget_type_array;

		protected $widget_question_array;

		public $paging_arr;

		/**
		 * WidgetFormHandler::setTableAndColumns()
		 *
		 * @return
		 */
		public function setTableAndColumns()
			{

				 $this->setTableNames(array($this->CFG['db']['tbl']['questions']. ' As q' , $this->CFG['db']['tbl']['users']. ' As u'));
				 $this->setReturnColumns(array( 'question','seo_title', 'status', 'u.user_id', 'u.usr_status', 'q.user_id' ));
 		    }

		/**
		 * WidgetFormHandler::buildConditionQuery()
		 *
		 * @return
		 */
		public function buildConditionQuery()
			{
				// Based on folder name filtering the records for question Views.
				//condition for recent to be done
				$this->sql_condition = ' u.user_id = q.user_id AND u.usr_status = \'Ok\'' ;
				if($this->getFormField('folder') == 'Open')
					$this->sql_condition .=' AND status = \'Open\' AND date_closed>=NOW()';
				elseif($this->getFormField('folder') == 'Popular' )
					$this->sql_condition .='';
				elseif($this->getFormField('folder') == 'Video')
					$this->sql_condition .=' AND video_id != 0';
				elseif($this->getFormField('folder') == 'Audio')
					$this->sql_condition .=' AND audio_id != 0';
				elseif($this->getFormField('folder') == 'Both')
					$this->sql_condition .=' AND video_id != 0 AND audio_id != 0';
				elseif($this->getFormField('folder') == 'my')
					{
						$this->sql_condition .=' AND q.user_id = '.$this->getFormField('uid');
					}

				if($this->getFormField('category')!='' && ctype_digit($this->getFormField('category')))
					$this->sql_condition .=' AND q.pcat_id = '.$this->getFormField('category');

				if($this->getFormField('sub_category')!='' && ctype_digit($this->getFormField('sub_category')))
					$this->sql_condition .=' AND q.cat_id = '.$this->getFormField('sub_category');

			}

		/**
		 * WidgetFormHandler::buildSortQuery()
		 *
		 * @return
		 */
		public function buildSortQuery()
			{
				if($this->getFormField('folder') == 'Popular' )
					$this->sql_sort = 'total_views DESC';
				elseif($this->getFormField('folder') == 'Recent')
					$this->sql_sort = ' date_asked DESC';
				else
					$this->sql_sort = 'ques_id DESC';
			}

		/**
		 * WidgetFormHandler::widget_type_array()
		 *
		 * @param mixed $widget_type_array
		 * @param array $first_option_arr
		 * @return
		 */
		public function widget_type_array($widget_type_array, $first_option_arr=array())
			{
				$this->widget_type_array = $first_option_arr + $widget_type_array;
			}

		/**
		 * WidgetFormHandler::widget_question_array()
		 *
		 * @param mixed $widget_question_array
		 * @param array $first_option_arr
		 * @return
		 */
		public function widget_question_array($widget_question_array, $first_option_arr=array())
			{
				$this->widget_question_array = $first_option_arr + $widget_question_array;
			}

		/**
		 * WidgetFormHandler::includeAjaxHeader()
		 *
		 * @param mixed $assign_var
		 * @return
		 */
		public function includeAjaxHeader($assign_var = true)
			{
				global $CFG, $smartyObj;
				if($assign_var)
					{
						$this->assignSmartyVariables();
					}
			}
	}
//-------------------- Code begins -------------->>>>>//
$widget = new WidgetFormHandler();
$widget->setPageBlockNames(array('form_widget', 'form_widget_preview', 'form_widget_code'));
//default form fields and values...

$widget->setFormField('type','ask_answer');
$widget->setFormField('question','recent');
$widget->setFormField('question_count',4);
$widget->setFormField('folder','Recent');
$widget->setFormField('ask_question','');
$widget->setFormField('category','');
$widget->setFormField('sub_category','');
$widget->setFormField('uid', $CFG['user']['user_id']);
//Question type array
$widget->LANG_LIST_ARR['question_type'] = array(
					'open' => $LANG['common_open'],
					'recent' => $LANG['recent'],
					'popular' => $LANG['popular']
					);
if(isMember())
	{
		$widget->LANG_LIST_ARR['question_type']['my'] = $LANG['my_question_only'];
	}
if ($CFG['admin']['module']['video'])
	$widget->LANG_LIST_ARR['question_type']['video'] = $LANG['video'];
if ($CFG['admin']['module']['audio'])
	$widget->LANG_LIST_ARR['question_type']['audio'] = $LANG['audio'];
if ($CFG['admin']['module']['video'] AND $CFG['admin']['module']['audio'])
	$widget->LANG_LIST_ARR['question_type']['both_av'] = $LANG['both_av'];
//Widget type array
$widget->LANG_LIST_ARR['widget_type'] = array(
							'ask_answer' => $LANG['ask_answer'],
							'ask_only' => $LANG['ask_only'],
							'question_only' => $LANG['question_only']);

$widget->populateCategories_arr = $widget->populateCategories();
$widget->populateSubCategories_arr = '';
/*************Start navigation******/
$widget->numpg = $CFG['data_tbl']['numpg'];
$widget->setFormField('start', 0);
$widget->setFormField('numpg', $CFG['data_tbl']['numpg']);//$this->CFG['data_tbl']['numpg']
$widget->setMinRecordSelectLimit($CFG['data_tbl']['min_record_select_limit']);
$widget->setMaxRecordSelectLimit($CFG['data_tbl']['max_record_select_limit']);
$widget->setNumPerPageListArr($CFG['data_tbl']['numperpage_list_arr']);
//Set tables and fields to return
$widget->setTableNames(array());
$widget->setReturnColumns(array());
//orderby field and orderby
$widget->setFormField('orderby_field', '');
$widget->setFormField('orderby', '');
$widget->setFormField('block', 'show');
$widget->setFormField('embed', '');

/*************End navigation******/
$widget->sanitizeFormInputs($_REQUEST);

if($widget->getFormField('category'))
	{
		$widget->populateSubCategories_arr = $widget->populateSubCategories($widget->getFormField('category'));
	}

if($widget->getFormField('block') == 'show')
	{
		$widget->setPageBlockShow('form_widget');
		$widget->setPageBlockShow('form_widget_preview');
		$widget->setPageBlockShow('form_widget_code');
	}
else
	{
		$widget->setPageBlockShow('form_widget_preview');
	}
if ($widget->isFormGETed($_GET, 'embed'))
	{
		$questions = $widget->getFormField('question_count');
		$widget->setFormField('numpg', $questions);//$this->CFG['data_tbl']['numpg']
		if($widget->getFormField('question') == 'open')
			$widget->setFormField('folder','Open');
		elseif($widget->getFormField('question') == 'recent')
			$widget->setFormField('folder','Recent');
		elseif($widget->getFormField('question') == 'popular')
			$widget->setFormField('folder','Popular');
		elseif($widget->getFormField('question') == 'video')
			$widget->setFormField('folder','Video');
		elseif($widget->getFormField('question') == 'audio')
			$widget->setFormField('folder','Audio');
		elseif($widget->getFormField('question') == 'both_av')
			$widget->setFormField('folder','Both');
		elseif($widget->getFormField('question') == 'my')
			$widget->setFormField('folder','my');
		$widget->setPageBlockShow('form_widget_preview');
		$widget->setPageBlockShow('form_widget_code');
		$widget->setTableAndColumns();
		$widget->buildSelectQuery();
		$widget->buildConditionQuery();
		$widget->buildSortQuery();
		$widget->buildQuery();
		$widget->executeQuery();
		ob_start();
?>
		<style type="text/css">
		.clsCommonPreviewWidgetSection h3{
			width:280px;
			background:#7F7F7F;
			color:#FFF;
			padding:0.2em 0 0.2em 0.8em;
			margin:0;
		}
		.clsWidgetCode{
			margin:0 0 1em 0;
			border:1px solid #7F7F7F;
			padding:0.5em;
		}
		.clsWidgetCodeTextarea{
			width:500px;
			margin:1em 0 0 0;
		}
		.clsSubmitButton{
			background:#B9F766 none repeat scroll 0 0;
			border:0 none;
			color:#444444;
			cursor:pointer;
			font-family:arial;
			font-size:20px;
			font-weight:bold;
			margin:13px 2em 0 0;
			padding:5px 8px;
		}
		* html .clsSubmitButton{
			padding:2px 5px 1px 5px;
		}
		#submitsearch{
			overflow:auto;
			zoom:1;
		}
		* html #submitsearch{
			height:1%;
			overflow:visible;
		}
		.clsOddQuestion{
			padding:3px 5px;
		}
		.clsEvenQuestion{
			padding:3px 5px;
		}
		.clsWidgetCode p{
			margin:0;
		}
			.clsWidgetCode p a{
				color:#0066CB;
				font-size:13px;
				font-weight:bold;
				padding:0 0 5px;
				text-decoration:none;
				font-family:Arial, Helvetica, sans-serif;
			}
		</style>
<?php
		$widget->includeAjaxHeader();
		$widget->showQuestionList();
		$str = ob_get_clean();
		$str = trim($str);
		$str = str_replace(array("\n", "\t", "\r", "'"), array('', '', '', "&rsquo;"), $str);
		$documentWrite = true;
		if ($documentWrite)
			{
				print 'document.writeln(\''.$str.'\');';
				exit;
			}
		else
			{
				print $str;
			}
	}
$questions = 0;
if($widget->isFormPOSTed($_POST, 'ask'))
	{
		Redirect2URL(getAnovaUrl('questions', '?view=ask&question='.$widget->getFormField('ask_question'), 'ask/?question='.$widget->getFormField('ask_question'), 'members'));
	}
if($widget->isFormGETed($_POST, 'submit1'))
	{
		$questions = $widget->getFormField('question_count');
		$widget->setFormField('numpg', $questions);//$this->CFG['data_tbl']['numpg']
		if($widget->getFormField('question') == 'open')
			$widget->setFormField('folder','Open');
		elseif($widget->getFormField('question') == 'recent')
			$widget->setFormField('folder','Recent');
		elseif($widget->getFormField('question') == 'popular')
			$widget->setFormField('folder','Popular');
		elseif($widget->getFormField('question') == 'video')
			$widget->setFormField('folder','Video');
		elseif($widget->getFormField('question') == 'audio')
			$widget->setFormField('folder','Audio');
		elseif($widget->getFormField('question') == 'both_av')
			$widget->setFormField('folder','Both');
		elseif($widget->getFormField('question') == 'my')
			$widget->setFormField('folder','my');
		$widget->setPageBlockShow('form_widget_preview');
		$widget->setPageBlockShow('form_widget_code');
   }
//-------------------- Code endok comes -------------->>>>>//
//--------------------Page block templates begins-------------------->>>>>//
if ($widget->isShowPageBlock('form_widget_preview'))
	{
		$questions = $widget->getFormField('question_count');
		$widget->setFormField('numpg', $questions);//$this->CFG['data_tbl']['numpg']
		$widget->setTableAndColumns();
		$widget->buildSelectQuery();
		$widget->buildConditionQuery();
		$widget->buildSortQuery();
		$widget->buildQuery();
		$widget->executeQuery();
   	}
//--------------------Page block templates begins-------------------->>>>>//
//include the header file
$widget->includeHeader();
//include the content of the page
setTemplateFolder('general/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('widgetGenerate.tpl');
//include the footer of the page
//<<<<<<--------------------Page block templates Ends--------------------//
$widget->includeFooter();
?>