<?php
/**
 * Edit Users settings
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		ViewMembersHandler
 * @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
 */
require_once('../common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/admin/editMembers.php';
$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.php';
//partial optimization. Don't use DB unnecessarily ie, DB only needed when the form is submitted/posted.
//compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;

//we shouldn't send any texts as we have to redirect/send headers.
if ($_POST)
	$CFG['mods']['include_files'][] = 'common/classes/swiftmailer/lib/EasySwift.php';
/**
 * File to include the header file, database access file, session management file, help file and necessary functions
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');

class ViewMembersHandler extends AnovaHandler
	{
		/**
		 * To get the user details from the DB
		 *
		 * @param 		string $table_name table name
		 * @param 		array $fields_arr Array of fields
		 * @param 		integer $user_id user id
		 * @return 		array has array with field values
		 * @access 		public
		 */
		public function getUserDetailsArrFromDB($table_name, $fields_arr=array(), $user_id)
			{
				$sql = 'SELECT ';
				foreach($fields_arr as $field_name)
						$sql .= $field_name . ', ';
				$sql = substr($sql, 0, strrpos($sql, ','));
				$sql .= ' FROM '.$table_name.
						' WHERE user_id='.$this->dbObj->Param($user_id);

				// prepare sql
				$stmt = $this->dbObj->Prepare($sql);
				// execute sql
				$rs = $this->dbObj->Execute($stmt, array($user_id));
				//raise user error... fatal
				if (!$rs)
						trigger_db_error($this->dbObj);

				$row = array();
				if ($rs->PO_RecordCount())
						$row = $rs->FetchRow();
				$ret_fields_arr = array();
				foreach($fields_arr as $field_name)
					$ret_fields_arr[$field_name] = isset($row[$field_name]) ? $row[$field_name] : '';
				return $ret_fields_arr;
			}

		/**
		 * ViewMembersHandler::setIndexBlockFormField()
		 *
		 * @param mixed $index_block_arr
		 * @return
		 */
		public function setIndexBlockFormField($index_block_arr)
			{
				foreach($index_block_arr as $value)
					{
						$this->setFormField($value, $this->CFG['admin']['index'][$value]);
					}
			}

		/**
		 * ViewMembersHandler::populateIndexBlockValues()
		 *
		 * @param mixed $index_block_value
		 * @return
		 */
		public function populateIndexBlockValues($index_block_value)
			{
				$index_block_value_arr = unserialize($index_block_value);
				if(!is_array($index_block_value_arr))
					return;

				foreach($index_block_value_arr as $key=>$value)
					{
						if (!isset($this->fields_arr[$key])) continue;

						if(!($this->fields_arr[$key]=='compulsory' or $value=='compulsory'))
							$this->fields_arr[$key] = $value;
						else
							$this->fields_arr[$key] = $this->CFG['admin']['index'][$key];
					}
			}

		/**
		 * ViewMembersHandler::populateIndexPageFields()
		 *
		 * @param mixed $index_block_arr
		 * @return
		 */
		public function populateIndexPageFields($index_block_arr)
			{
				global $smartyObj;
				$populateIndexPageFields_arr = array();
				$populateIndexPageFields_arr['val'] = array();
				$inc = 1;
				foreach($index_block_arr as $value)
					{
						$label = $this->LANG[$value];

						if(!$this->isShowIndexBlock($this->CFG['admin']['index'][$value]))
							continue;

						$populateIndexPageFields_arr['val'][$inc]['label'] = $label;
						$populateIndexPageFields_arr['val'][$inc]['value'] =($this->getFormField($value)=='yes')? $this->LANG['common_yes_option']:$this->LANG['common_no_option'];
						$inc++;
					}
				$smartyObj->assign('populateIndexPageFields_arr', $populateIndexPageFields_arr);
			}

		/**
		 * ViewMembersHandler::chkIsValidUser()
		 *
		 * @return
		 */
		public function chkIsValidUser()
			{
				$uid = $this->fields_arr['uid'];

				if($this->isAnonymsUser($uid))
					{
						$this->setCommonErrorMsg($this->LANG['anonyms_account_msg']);
						$this->setPageBlockShow('block_msg_form_error');
						return false;
					}

				$sql = 'SELECT 1 FROM '.$this->CFG['db']['tbl']['users'].
						' WHERE '.$this->getUserTableField('user_id').'='.$this->dbObj->Param($uid);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($uid));
				if (!$rs)
					    trigger_db_error($this->dbObj);
				if($rs->PO_RecordCount())
					return true;
				return false;
			}
		public function getAnonymousTwitterUserId()
			{
				$sql = ' SELECT  user_id  FROM '.$this->CFG['db']['tbl']['users'].''.
						' WHERE name =\''.$this->CFG['user']['not_allowed'].'\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				$row = $rs->FetchRow();
				if($row)
					return $row['user_id'];
			}				
	}
//<<<<<-------------- Class ViewMembersHandler begins ---------------//
//-------------------- Code begins -------------->>>>>//
$viewmember = new ViewMembersHandler();
//$index_block_arr = array('recent_questions', 'popular_questions', 'recently_answered', 'answered_questions', 'unanswered_questions', 'questions_with_videos', 'questions_with_audios', 'best_of_answer', 'recent_forums', 'forums_with_most_replies', 'recent_blogs', 'blogs_with_most_comment', 'top_analyst', 'featured_analyst', 'analyst_weekwise', 'popular_videos_and_audios');
$index_block_arr = array('recent_questions', 'best_answered_questions', 'popular_questions', 'recently_answered', 'recent_forums', 'recent_blogs', 'featured_analyst', 'popular_videos');
$viewmember->setPageBlockNames(array('form_search', 'form_settings'));
// To set the DB object
$viewmember->setFormField('subscribe_keywords', '');
$viewmember->setFormField('keyword_mail', '');
$viewmember->setFormField('reply_mail', '');
$viewmember->setFormField('favorite_mail', '');
$viewmember->setFormField('best_ans_mail', '');
$viewmember->setFormField('abuse_mail', '');
$viewmember->setFormField('internal_mail', '');
$viewmember->setFormField('all_email_notification', '');
$viewmember->setFormField('search_question', '');
$viewmember->setFormField('image_path', '');
$viewmember->setFormField('msg', '');
$viewmember->setFormField('uid', '');
$viewmember->setFormField('name', '');
$viewmember->setFormField('password', '');
$viewmember->setFormField('confirm_password', '');
$viewmember->setFormField('email', '');
$viewmember->setFormField('gender', 'Female');
$viewmember->setFormField('bio', '');
$viewmember->setFormField('user_access', '');

$viewmember->setFormField('photo', '');
$viewmember->setFormField('photo_ext', '');
$viewmember->setFormField('photo_server_url', '');
$viewmember->setFormField('index_block', '');

$viewmember->setFormField('avatar', '');
$viewmember->setIndexBlockFormField($index_block_arr);

$viewmember->sanitizeFormInputs($_REQUEST);
$tweet_id = $viewmember->getAnonymousTwitterUserId();
// Default page block
$viewmember->setAllPageBlocksHide();
$title = $LANG['viewmembers_title'];

if ($viewmember->chkIsValidUser())
	{
		$viewmember->setPageBlockShow('form_settings');

		$user_settings_arr = $viewmember->getUserDetailsArrFromDB($CFG['db']['tbl']['users_ans_log'],
							array('subscribe_keywords', 'keyword_mail', 'reply_mail', 'favorite_mail', 'best_ans_mail', 'abuse_mail', 'internal_mail', 'index_block'),
							$viewmember->getFormField('uid'));

		$viewmember->populateIndexBlockValues($user_settings_arr['index_block']);

		$viewmember->setFormField('subscribe_keywords', $user_settings_arr['subscribe_keywords']);
		$viewmember->setFormField('keyword_mail', $user_settings_arr['keyword_mail']);
		$viewmember->setFormField('reply_mail', $user_settings_arr['reply_mail']);
		$viewmember->setFormField('favorite_mail', $user_settings_arr['favorite_mail']);
		$viewmember->setFormField('best_ans_mail', $user_settings_arr['best_ans_mail']);
		$viewmember->setFormField('abuse_mail', $user_settings_arr['abuse_mail']);
		$viewmember->setFormField('internal_mail', $user_settings_arr['internal_mail']);
		$user_details_arr = $viewmember->getUserDetailsFromUsersTable($CFG['db']['tbl']['users'], $viewmember->getFormField('uid'));
		$viewmember->setFormField('image_path', $user_details_arr['image_path']);
		$viewmember->setFormField('name', $user_details_arr['name']);
		$viewmember->setFormField('display_name', $user_details_arr['display_name']);
		$viewmember->setFormField('email', $user_details_arr['email']);
		$viewmember->setFormField('bio', wordWrapManual($user_details_arr['bio'], 45));
		$viewmember->setFormField('gender', $user_details_arr['gender']);
		$viewmember->setFormField('user_access', $user_details_arr['user_access']);
		$smartyObj->assign('user_details_arr',$user_details_arr);
	}
else
	{
		if(!$viewmember->isAnonymsUser($viewmember->getFormField('uid'))){
			$viewmember->setPageBlockShow('block_msg_form_error');
			$viewmember->setCommonErrorMsg($LANG['err_tip_invalid_user']);
		}
	}

$viewmember->populateIndexPageFields($index_block_arr);
$smartyObj->assign('index_block_arr',$index_block_arr);
//<<<<--------------------Code Ends----------------------//
//--------------------Page block templates begins-------------------->>>>>//
//include the header file
$viewmember->includeHeader();
//include the content of the page
setTemplateFolder('admin/');
$smartyObj->display('viewMembers.tpl');
//<<<<<-------------------- Page block templates ends --------------------//
$viewmember->includeFooter();
?>