<?php
/**
 * Manage Users settings
 *
 * PHP version 5.0
 *
 * @category ###Anova###
 * @package ###manage settings###
 * @author karthiselvam_52ag05
 * @copyright Copyright (c) 2010 {@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'); //configurations
if ($CFG['admin']['module']['facebook'])
	{
		if (version_compare(PHP_VERSION,'5','>=')) {
			require_once ('../facebook/client/facebook.php');
		} else {
		 	require_once ('../facebook/php4client/facebook.php');
		  	require_once ('../facebook/php4client/facebookapi_php4_restlib.php');
		}
	}
$CFG['lang']['include_files'][] = 'languages/%s/'.$CFG['admin']['index']['anova_module'].'/manageSettings.php';
$CFG['html']['header'] = 'general/html_header.php';
$CFG['html']['footer'] = 'general/html_footer.php';
$CFG['is']['ajax_page'] = false;
if (isset($_REQUEST['ajax_page']) and $_REQUEST['ajax_page']) {
    $CFG['is']['ajax_page'] = true;
    $CFG['html']['is_use_header'] = false;
    $CFG['html']['is_use_footer'] = false;
}
$CFG['mods']['include_files'][] = 'common/classes/class_Image.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_ftp.lib.php';
$CFG['lang']['include_files'][] = 'languages/%s/common/email_notify.inc.php';
$CFG['mods']['include_files'][] = 'common/classes/swiftmailer/lib/EasySwift.php';
$CFG['site']['is_module_page'] = $CFG['admin']['index']['anova_module'];
// compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
$CFG['auth']['is_authenticate'] = 'members';
// we shouldn't send any texts as we have to redirect/send headers.
/**
 * 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 ManageSettingsFormHandler begins --------------->>>>>//
/**
 * ManageSettingsFormHandler
 *
 * @package
 * @author karthiselvam_75ag08
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
class ManageSettingsFormHandler extends FormHandler
	{
		/**
		 * To update the form values into users table
		 *
		 * @param string $table_name Table name
		 * @param integer $user_id User id
		 * @param array $fields_to_update_arr Array of fields to update
		 * @return void
		 * @access public
		 */
		public function updateFormFieldsInTable($table_name, $user_id, $fields_to_insert_arr = array())
			{
				// field name, parameters and fields value are intialized
				$field_names_separated_by_comma = '';
				$parameters_separated_by_comma = '';
				$field_values_arr = array();
				// field name, parameters and fields value are set
				foreach($fields_to_insert_arr as $field_name)
					if (isset($this->fields_arr[$field_name]))
						{
							$field_names_separated_by_comma .= $this->getUserTableField($field_name).' = '.$this->dbObj->Param($field_name).', ';
							$field_values_arr[] = $this->fields_arr[$field_name];
						}
				// eleminates last comma
				$field_names_separated_by_comma = substr($field_names_separated_by_comma, 0, strrpos($field_names_separated_by_comma, ','));

				$sql = 'UPDATE '.$table_name.' SET '.$field_names_separated_by_comma;
				$sql .= ' WHERE '.$this->getUserTableField('user_id').' = '.$this->dbObj->Param($user_id);
				$field_values_arr[] = $user_id;

				$stmt = $this->dbObj->Prepare($sql);
				$result = $this->dbObj->Execute($stmt, $field_values_arr);
				if (!$result)
						trigger_db_error($this->dbObj);
			}

		/**
		 * To update the form values into users table
		 *
		 * @param string $table_name Table name
		 * @param integer $user_id User id
		 * @param array $fields_to_update_arr Array of fields to update
		 * @return void
		 * @access public
		 */
		public function updateFormFieldsInUsersLogable($table_name, $user_id, $fields_to_insert_arr = array())
			{
				$this->fields_arr['subscribe_keywords'] = $this->removeDuplicateKeywords($this->fields_arr['subscribe_keywords']);
				// field name, parameters and fields value are intialized
				$field_names_separated_by_comma = '';
				$parameters_separated_by_comma = '';
				$field_values_arr = array();
				// field name, parameters and fields value are set
				foreach($fields_to_insert_arr as $field_name)
				if (isset($this->fields_arr[$field_name]))
					{
						$field_names_separated_by_comma .= $field_name.' = '.$this->dbObj->Param($field_name).', ';
						$field_values_arr[] = $this->fields_arr[$field_name];
					}
				// eleminates last comma
				$field_names_separated_by_comma = substr($field_names_separated_by_comma, 0, strrpos($field_names_separated_by_comma, ','));

				$sql = 'UPDATE '.$table_name.' SET '.$field_names_separated_by_comma;
				$sql .= ' WHERE user_id = '.$this->dbObj->Param($user_id);
				$field_values_arr[] = $user_id;

				$stmt = $this->dbObj->Prepare($sql);
				$result = $this->dbObj->Execute($stmt, $field_values_arr);
				if (!$result)
					trigger_db_error($this->dbObj);
			}

		/**
		 *
		 * @return void
		 * @access public
		 */
		public function updateUserImagePath()
			{
				$image_path = $this->fields_arr['image_path'];
				$user_id = $this->CFG['user']['user_id'];

				$sql = 'UPDATE '.$this->CFG['db']['tbl']['users'].
						' SET '.$this->getUserTableField('image_path').' = '.$this->dbObj->Param($image_path).
						' WHERE '.$this->getUserTableField('user_id').' = '.$this->dbObj->Param($user_id);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($image_path, $user_id));
				if (!$rs)
						trigger_db_error($this->dbObj);
			}

		/**
		 * 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);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($user_id));
				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;
			}

		/**
		 * PhotoUpload::setIHObject()
		 *
		 * @param  $imObj
		 * @return
		 */
		public function setIHObject($imObj)
			{
				$this->imageObj = $imObj;
			}

		/**
		 * chkValidFileType()
		 *
		 * @param  $field_name
		 * @param string $err_tip
		 * @return
		 */
		public function chkValidFileType($field_name, $err_tip = '')
			{
				$this->EXTERN = strtolower($_FILES[$field_name]['type']);
				if (!in_array($this->EXTERN, $this->CFG['admin']['ans_photos']['format_arr']))
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * chkValideFileSize()
		 *
		 * @param  $field_name
		 * @param string $err_tip
		 * @return
		 */
		public function chkValideFileSize($field_name, $err_tip = '')
			{
				$max_size = $this->CFG['admin']['ans_photos']['max_size'] * 1024;
				if ($_FILES[$field_name]['size'] == 0)
					{
						$this->fields_err_tip_arr[$field_name] = $this->LANG['err_tip_invalid_file'];
						return false;
					}
				if ($_FILES[$field_name]['size'] > $max_size)
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * chkErrorInFile()
		 *
		 * @param  $field_name
		 * @param string $err_tip
		 * @return
		 */
		public function chkErrorInFile($field_name, $err_tip = '')
			{
				if ($_FILES[$field_name]['error'])
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * PhotoUpload::getServerDetails()
		 *
		 * @return
		 */
		public function getServerDetails()
			{
				$sql = 'SELECT server_url, ftp_server, ftp_usrename, ftp_password'.
						' FROM '.$this->CFG['db']['tbl']['server_settings'].
						' WHERE server_for=\'Ans_photo\' AND server_status=\'Yes\' LIMIT 0,1';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$row = array();
				if ($rs->PO_RecordCount())
					{
						$row = $rs->FetchRow();
						$this->fields_arr['ftp_server'] = $row['ftp_server'];
						$this->fields_arr['ftp_usrename'] = $row['ftp_usrename'];
						$this->fields_arr['ftp_password'] = $row['ftp_password'];
						$this->fields_arr['server_url'] = $row['server_url'];
						return true;
					}
				return false;
			}

		/**
		 * PhotoUpload::storeImagesTempServer()
		 *
		 * @param  $uploadUrl
		 * @param  $extern
		 * @return
		 */
		public function storeImagesTempServer($uploadUrl, $extern)
			{
				// GET LARGE IMAGE
				if ($this->CFG['admin']['ans_photos']['large_name'] == 'L')
					{
						if ($this->CFG['admin']['ans_photos']['large_height'] or $this->CFG['admin']['ans_photos']['large_width'])
							{
								$this->imageObj->resize($this->CFG['admin']['ans_photos']['large_width'], $this->CFG['admin']['ans_photos']['large_height'], '-');
								$this->imageObj->output_resized($uploadUrl.'L.'.$extern, strtoupper($extern));
								$image_info = getImageSize($uploadUrl.'L.'.$extern);
								$this->L_WIDTH = $image_info[0];
								$this->L_HEIGHT = $image_info[1];
							}
						else
							{
								$this->imageObj->output_original($uploadUrl.'L.'.$extern, strtoupper($extern));
								$image_info = getImageSize($uploadUrl.'L.'.$extern);
								$this->L_WIDTH = $image_info[0];
								$this->L_HEIGHT = $image_info[1];
							}
					}
				// GET THUMB IMAGE
				if ($this->CFG['admin']['ans_photos']['thumb_name'] == 'T')
					{
						$this->imageObj->resize($this->CFG['admin']['ans_photos']['thumb_width'], $this->CFG['admin']['ans_photos']['thumb_height'], '-');
						$this->imageObj->output_resized($uploadUrl.'T.'.$extern, strtoupper($extern));
						$image_info = getImageSize($uploadUrl.'T.'.$extern);
						$this->T_WIDTH = $image_info[0];
						$this->T_HEIGHT = $image_info[1];
					}
				// GET SMALL IMAGE
				if ($this->CFG['admin']['ans_photos']['small_name'] == 'S')
					{
						$this->imageObj->resize($this->CFG['admin']['ans_photos']['small_width'], $this->CFG['admin']['ans_photos']['small_height'], '-');
						$this->imageObj->output_resized($uploadUrl.'S.'.$extern, strtoupper($extern));
						$image_info = getImageSize($uploadUrl.'S.'.$extern);
						$this->S_WIDTH = $image_info[0];
						$this->S_HEIGHT = $image_info[1];
					}
				$wname = $this->CFG['admin']['ans_photos']['large_name'].'_WIDTH';
				$hname = $this->CFG['admin']['ans_photos']['large_name'].'_HEIGHT';
				$this->L_WIDTH = $this->$wname;
				$this->L_HEIGHT = $this->$hname;

				$wname = $this->CFG['admin']['ans_photos']['thumb_name'].'_WIDTH';
				$hname = $this->CFG['admin']['ans_photos']['thumb_name'].'_HEIGHT';
				$this->T_WIDTH = $this->$wname;
				$this->T_HEIGHT = $this->$hname;

				$wname = $this->CFG['admin']['ans_photos']['small_name'].'_WIDTH';
				$hname = $this->CFG['admin']['ans_photos']['small_name'].'_HEIGHT';
				$this->S_WIDTH = $this->$wname;
				$this->S_HEIGHT = $this->$hname;
			}

		/**
		 * EditPersonalProfileFormHandler::photoUpload()
		 *
		 * @return
		 */
		public function photoUpload()
			{
				$extern = strtolower(substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'], '.') + 1));
				$this->setFormField('user_id', $this->CFG['user']['user_id']);
				$this->setFormField('photo_ext', $extern);
				$image_name = getImageName($this->CFG['user']['user_id']);
				$imageObj = new ImageHandler($_FILES['photo']['tmp_name']);
				$this->setIHObject($imageObj);
				$temp_dir = '../'.$this->CFG['admin']['ans_photos']['temp_folder'];
				$this->chkAndCreateFolder($temp_dir);
				$temp_file = $temp_dir.$image_name;
				$this->storeImagesTempServer($temp_file, $extern);
				$dir = $this->CFG['admin']['ans_photos']['folder'];
				$local_upload = true;
				if ($this->getServerDetails())
					{
						if ($FtpObj = new FtpHandler($this->getFormField('ftp_server'), $this->getFormField('ftp_usrename'), $this->getFormField('ftp_password')))
							{
								$FtpObj->makeDirectory($dir);
								$FtpObj->changeDirectory($dir);
								$FtpObj->moveTo($temp_file.'.'.$extern, $dir.$image_name.'.'.$extern);
								unlink($temp_file.'.'.$extern);
								if ($this->CFG['admin']['ans_photos']['large_name'] == 'L')
									{
										$FtpObj->moveTo($temp_file.'L.'.$extern, $dir.$image_name.'L.'.$extern);
										unlink($temp_file.'L.'.$extern);
									}
								if ($this->CFG['admin']['ans_photos']['thumb_name'] == 'T')
									{
										$FtpObj->moveTo($temp_file.'T.'.$extern, $dir.$image_name.'T.'.$extern);
										unlink($temp_file.'T.'.$extern);
									}
								if ($this->CFG['admin']['ans_photos']['small_name'] == 'S')
									{
										$FtpObj->moveTo($temp_file.'S.'.$extern, $dir.$image_name.'S.'.$extern);
										unlink($temp_file.'S.'.$extern);
									}
								$this->setFormField('server_url', $this->getFormField('server_url'));
								$local_upload = false;
								return;
							}
					}
				if ($local_upload)
					{
						$dir = '../'.$this->CFG['admin']['ans_photos']['folder'];
						$this->chkAndCreateFolder($dir);
						$uploadUrl = $dir.$image_name;
						if ($this->CFG['admin']['ans_photos']['large_name'] == 'L')
							{
								copy($temp_file.'L.'.$extern, $uploadUrl.'L.'.$extern);
								unlink($temp_file.'L.'.$extern);
							}
						if ($this->CFG['admin']['ans_photos']['thumb_name'] == 'T')
							{
								copy($temp_file.'T.'.$extern, $uploadUrl.'T.'.$extern);
								unlink($temp_file.'T.'.$extern);
							}
						if ($this->CFG['admin']['ans_photos']['small_name'] == 'S')
							{
								copy($temp_file.'S.'.$extern, $uploadUrl.'S.'.$extern);
								unlink($temp_file.'S.'.$extern);
							}
						$this->setFormField('server_url', $this->CFG['site']['url']);
					}
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['users'].
						' SET '.$this->CFG['users']['t_width'].' = '.$this->dbObj->Param($this->T_WIDTH).
						', '.$this->CFG['users']['t_height'].' = '.$this->dbObj->Param($this->T_HEIGHT).
						', '.$this->CFG['users']['l_width'].' = '.$this->dbObj->Param($this->L_WIDTH).
						', '.$this->CFG['users']['l_height'].' = '.$this->dbObj->Param($this->L_HEIGHT).
						', '.$this->CFG['users']['s_width'].' = '.$this->dbObj->Param($this->S_WIDTH).
						', '.$this->CFG['users']['s_height'].' = '.$this->dbObj->Param($this->S_HEIGHT).
						', '.$this->CFG['users']['photo_ext'].' = '.$this->dbObj->Param($extern).
						', '.$this->CFG['users']['image_path'].' = \'\''.
						', '.$this->CFG['users']['photo_server_url'].' = '.$this->dbObj->Param($this->fields_arr['server_url']).
						' WHERE '.$this->CFG['users']['user_id'].' = '.$this->dbObj->Param($this->CFG['user']['user_id']);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->T_WIDTH, $this->T_HEIGHT, $this->L_WIDTH, $this->L_HEIGHT, $this->S_WIDTH,
														 $this->S_HEIGHT, $extern, $this->fields_arr['server_url'], $this->CFG['user']['user_id']));
				if (!$rs)
					trigger_db_error($this->dbObj);
			}

		/**
		 * ManageSettingsFormHandler::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]);
					}
			}

		/**
		 * ManageSettingsFormHandler::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];
					}
			}

		/**
		 * ManageSettingsFormHandler::populateIndexPageFields()
		 *
		 * @param mixed $index_block_arr
		 * @return
		 */
		public function populateIndexPageFields($index_block_arr)
			{
				$headings_count = 0;
				$populateIndexPageFields_arr = array();
				$inc = 0;
				foreach($index_block_arr as $value)
					{
						$populateIndexPageFields_arr[$inc]['label'] = $this->LANG[$value];
						$populateIndexPageFields_arr[$inc]['headings_count_cond'] = 0;
						$populateIndexPageFields_arr[$inc]['check_index_block'] = 0;

						if ($this->CFG['admin']['index'][$value] != 'yes')
							continue;
						if ($value == 'recent_forums' or $value == 'forums_with_most_replies')
							{
								if (!chkIsAllowedModule(array('forums')))
									continue;
							}
						if ($value == 'questions_with_videos')
							{
								if (!chkIsVideoAllowed('question'))
									continue;
							}
						if ($value == 'questions_with_audios')
							{
								if (!chkIsAudioAllowed('question'))
									continue;
							}
						if ($value == 'recent_blogs' or $value == 'blogs_with_most_comment')
							{
								if (!chkIsAllowedModule(array('blog')))
									continue;
							}

						if ($value == 'popular_videos')
							{
								if (!chkIsVideoAllowed('question') AND !chkIsVideoAllowed('answer') AND !chkIsAudioAllowed('question') AND !chkIsAudioAllowed('answer'))
									continue;
							}

						if ($headings_count == 0 and $this->CFG['admin']['index'][$value] == 'yes')
							{
								$headings_count++;
								$populateIndexPageFields_arr[$inc]['headings_count_cond'] = 1;
							}

						if ($this->CFG['admin']['index'][$value] == 'yes')
							{
								$populateIndexPageFields_arr[$inc]['check_index_block'] = 1;
							}
						$populateIndexPageFields_arr[$inc]['value'] = $value;
						$inc++;
					}
				return $populateIndexPageFields_arr;
			}

		/**
		 * To check the confirmation of password and password are same
		 */
		public function serializeIndexBlockValues($index_block_arr)
			{
				$array = array();
				foreach($index_block_arr as $value)
					{
						$array[$value] = $this->fields_arr[$value];
					}
				$this->fields_arr['index_block'] = serialize($array);
			}

		/**
		 * To check the confirmation of password and username are same
		 */
		public function chkIsPasswordAndUserNameAreSame($err_tip = '')
			{
				if ($this->fields_arr['name'] and $this->fields_arr['password'])
					{
						if ($this->fields_arr['name'] == $this->fields_arr['password'])
							{
								$this->fields_err_tip_arr['password'] = $err_tip;
								return false;
							}
					}
				elseif ($this->CFG['user']['name'] and $this->fields_arr['password'])
					{
						if ($this->CFG['user']['name'] == $this->fields_arr['password'])
							{
								$this->fields_err_tip_arr['password'] = $err_tip;
								return false;
							}
					}
				return true;
			}

		/**
		 * To check the confirmation of password and password are same
		 */
		public function chkIsSamePasswords($field_name1, $field_name2, $err_tip = '')
			{
				$is_ok = ($this->fields_arr[$field_name1] == $this->fields_arr[$field_name2]);
				if (!$is_ok)
					$this->fields_err_tip_arr[$field_name1] = $this->fields_err_tip_arr[$field_name2] = $err_tip;
				return $is_ok;
			}

		/**
		 * To check the password in expected size
		 */
		public function chkIsValidSize($field_name, $min, $max, $err_tip = '')
			{
				if ((strlen($this->fields_arr[$field_name]) < $min) or (strlen($this->fields_arr[$field_name]) > $max))
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * ManageSettingsFormHandler::chkIsAllowedUserName()
		 *
		 * @param string $err_tip
		 * @return
		 */
		public function chkIsAllowedUserName($err_tip = '')
			{
				$user_name = strtolower($this->fields_arr['name']);
				if (in_array($user_name, $this->CFG['admin']['not_allowed_usernames']))
					{
						$this->fields_err_tip_arr['name'] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * ManageSettingsFormHandler::chkIsNotDuplicateName()
		 *
		 * @param mixed $table_name
		 * @param mixed $field_name
		 * @param string $err_tip
		 * @return
		 */
		public function chkIsNotDuplicateName($table_name, $field_name, $err_tip = '')
			{
				$sql = 'SELECT '.$this->getUserTableField('user_id').
						' FROM '.$table_name.
						' WHERE '.$this->getUserTableField('name').' = '.$this->dbObj->Param($field_name);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr[$field_name]));
				if (!$rs)
					trigger_db_error($this->dbObj);

				// counts number of rows
				$numrows = $rs->PO_RecordCount();
				// finds row exists
				$is_ok = ($numrows == 0) ;
				// set error tips
				if (!$is_ok)
					$this->fields_err_tip_arr[$field_name] = $err_tip;
				return $is_ok;
			}

		/**
		 * To check for the duplicate email
		 *
		 * @param string $table_name table name
		 * @param string $field_name field name
		 * @param string $err_tip error tip
		 * @return boolean true/false
		 * @access public
		 */
		public function chkIsNotDuplicateEmail($table_name, $field_name, $err_tip = '')
			{
				$sql = 'SELECT '.$this->getUserTableField('user_id').
						' FROM '.$table_name.
						' WHERE '.$this->getUserTableField('email').' = '.$this->dbObj->Param($field_name);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['new_mail']));
				if (!$rs)
					trigger_db_error($this->dbObj);

				// counts number of rows
				$numrows = $rs->PO_RecordCount();
				// finds row exists
				$is_ok = ($numrows == 0) ;
				// set error tips
				if (!$is_ok)
					$this->fields_err_tip_arr[$field_name] = $err_tip;
				return $is_ok;
			}

         public function IsOpenidUser()
			{
				$uid = $this->CFG['user']['user_id'];
				$sql = ' SELECT * FROM '.$this->CFG['db']['tbl']['user_identity'].''.
					   ' WHERE user_id = '.$this->dbObj->Param($uid);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($uid));
				$numrows = $rs->PO_RecordCount();
				if($numrows >0)
					return false;
				else
					return true;

			}

		/**
		 * ManageSettingsFormHandler::updateCloseWindow()
		 *
		 * @return
		 */
		public function updateCloseWindow()
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['users_ans_log'].
						' SET index_block = '.$this->dbObj->Param($this->getFormField('block')).
						' WHERE user_id = '.$this->dbObj->Param($this->CFG['user']['user_id']);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->getFormField('block'), $this->CFG['user']['user_id']));
				if (!$rs)
					trigger_db_error($this->dbObj);
			}

		/**
		 * ManageSettingsFormHandler::sendActivationForMail()
		 *
		 * @param mixed $mailto
		 * @param mixed $name
		 * @param mixed $code
		 * @return
		 */
		public function sendActivationForMail($mailto, $name, $code)
			{
				$mcode = $name.$code;
				$activation_link = getAnovaUrl('activatemailaccount', '?code='.urlencode($mcode), '?code='.urlencode($mcode), 'root');
				$activation_link = '<a href="'.$activation_link.'">'.$activation_link.'</a>';
				$activation_subject = str_ireplace('VAR_SITENAME', $this->CFG['site']['name'], $this->LANG['mailactivation_subject']);
				$link = '<a href="'.$this->CFG['site']['url'].'">'.$this->CFG['site']['url'].'</a>';
				$activation_message = str_ireplace('VAR_SITENAME', $this->CFG['site']['name'], $this->LANG['mailactivation_message']);
				$activation_message = str_ireplace('VAR_USERNAME', $this->CFG['user']['display_name'], $activation_message);
				$activation_message = str_ireplace('VAR_LINK', $link, $activation_message);
				$activation_message = str_ireplace('VAR_ACTIVATION_LINK', $activation_link, $activation_message);

				$is_ok = $this->_sendMail($mailto, $activation_subject, $activation_message, $this->CFG['site']['noreply_name'], $this->CFG['site']['noreply_email']);
				if ($is_ok)
					$this->setDisplayVar('mail_status', $this->LANG['managesettings_activation_code_sent']);
				else
					$this->setDisplayVar('mail_status', $this->LANG['common_email_failure']);
			}

		/**
		 * To send email
		 *
		 * @param string $to_email to email id
		 * @param string $subject subject
		 * @param string $body mail body
		 * @param string $sender_name sender name
		 * @param string $sender_email sender email
		 * @return void
		 * @access private
		 */
		private function _sendMail($to_email, $subject, $body, $sender_name, $sender_email)
			{
				$this->buildEmailTemplate($subject, $body, false, true);
				$EasySwift = new EasySwift($this->getSwiftConnection());
				$EasySwift->flush();
				$EasySwift->addPart($this->getEmailContent(), "text/html");
				$from_address = $sender_name.'<'.$sender_email.'>';
				return $EasySwift->send($to_email, $from_address, $this->getEmailSubject());
			}

		public function updateNingImageStatus()
			{
				$uid = $this->CFG['user']['user_id'];
				$sql = ' UPDATE '.$this->CFG['db']['tbl']['ning_identity'].''.
				    	' SET ning_image =  \''.$this->getFormField('ng_image').'\''.
					    ' WHERE  user_id = '.$uid ;
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
				        trigger_db_error($this->dbObj);
		   }

		/**
		 * ManageSettingsFormHandler::isFacebookImageAllowed()
		 *
		 * @return
		 */
		public function isFacebookImageAllowed()
			{
				$uid = $this->CFG['user']['user_id'];
				$sql = 'SELECT facebook_image FROM '.$this->CFG['db']['tbl']['facebook_identity'].
					   ' WHERE  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);

				$row['facebook_image'] = '';
				if ($rs->PO_RecordCount())
					$row = $rs->FetchRow();
				return $row['facebook_image'];
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getFacebookProfileId()
			{
				$uid = $this->CFG['user']['user_id'];
				$sql = 'SELECT facebook_id FROM '.$this->CFG['db']['tbl']['facebook_identity'].
					   ' WHERE  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);

				$row['facebook_id'] = '';
				if ($rs->PO_RecordCount())
					$row = $rs->FetchRow();
				return $row['facebook_id'];
			}

		/**
		 * ManageSettingsFormHandler::getFacebookCurrentStatus()
		 *
		 * @return
		 */
		public function getFacebookCurrentStatus()
			{
				$uid = $this->CFG['user']['user_id'];
				$sql = 'SELECT facebook_image_current FROM '.$this->CFG['db']['tbl']['facebook_identity'].
					   ' WHERE  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);

				$row['facebook_image_current'] = '';
				if ($rs->PO_RecordCount())
					$row = $rs->FetchRow();
				return $row['facebook_image_current'];
			}

		/**
		 * ManageSettingsFormHandler::updateFacebookImageStatus()
		 *
		 * @return
		 */
		public function updateFacebookImageStatus()
			{
				$uid = $this->CFG['user']['user_id'];
				if($this->getFormField('fb_image') =='No')
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['facebook_identity'].
					    		' SET facebook_image_current = '.$this->dbObj->Param($this->getFormField('fb_image')).
						    	' WHERE  user_id = '.$this->dbObj->Param($uid);
						$field_values = array($this->getFormField('fb_image'), $uid);
					}
				else
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['facebook_identity'].
					    		' SET facebook_image_current = '.$this->dbObj->Param($this->getFormField('fb_image')).
								', facebook_image = '.$this->dbObj->Param($this->getFormField('fb_image')).
								' WHERE  user_id = '.$this->dbObj->Param($uid);
							$field_values = array($this->getFormField('fb_image'), $this->getFormField('fb_image'), $uid);
					}
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($field_values));
				if (!$rs)
				        trigger_db_error($this->dbObj);
		   }

	   /**
	    * ManageSettingsFormHandler::updateFacebookImageCurrentStatus()
	    *
	    * @param mixed $current_settings
	    * @return
	    */
	   public function updateFacebookImageCurrentStatus($current_settings)
			{
				$uid = $this->CFG['user']['user_id'];
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['facebook_identity'].
				    	' SET facebook_image = '.$this->dbObj->Param($current_settings).
						' WHERE  user_id = '.$this->dbObj->Param($uid) ;
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($current_settings, $uid));
				if (!$rs)
				        trigger_db_error($this->dbObj);
		   }

		public 	 function isTwitterImageAllowed()
			{
				$uid = $this->CFG['user']['user_id'];
				$sql = ' SELECT twitter_image FROM '.$this->CFG['db']['tbl']['twitter_identity'].''.
					   ' WHERE  user_id = '.$uid ;
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
				        trigger_db_error($this->dbObj);
				if ($rs->PO_RecordCount())
					$row = $rs->FetchRow();
				return $row['twitter_image'];

			}

		public function updateTwitterImageStatus()
			{
				$uid = $this->CFG['user']['user_id'];
				$sql = ' UPDATE '.$this->CFG['db']['tbl']['twitter_identity'].''.
				    	' SET twitter_image =  \''.$this->getFormField('tr_image').'\''.
					    ' WHERE  user_id = '.$uid ;
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
				        trigger_db_error($this->dbObj);
		   }

	}
// <<<<<-------------- Class ManageSettingsFormHandler begins ---------------//
// -------------------- Code begins -------------->>>>>//
$managefrm = new ManageSettingsFormHandler();
$index_block_arr = array('recent_questions', 'popular_questions', 'recently_answered', 'best_answered_questions', 'recent_forums', 'recent_blogs', 'featured_analyst', 'popular_videos');
$managefrm->setPageBlockNames(array('form_search', 'form_settings'));
$managefrm->setFormField('subscribe_keywords', '');
$managefrm->setFormField('keyword_mail', '');
$managefrm->setFormField('reply_mail', '');
$managefrm->setFormField('favorite_mail', '');
$managefrm->setFormField('best_ans_mail', '');
$managefrm->setFormField('abuse_mail', '');
$managefrm->setFormField('internal_mail', '');
$managefrm->setFormField('all_email_notification', '');
$managefrm->setFormField('new_mail', '');
$managefrm->setFormField('search_question', '');
$managefrm->setFormField('image_path', '');
$managefrm->setFormField('msg', '');
$managefrm->setFormField('task', '');
$managefrm->setFormField('photo', '');
$managefrm->setFormField('photo_ext', '');
$managefrm->setFormField('photo_server_url', '');
$managefrm->setFormField('index_block', '');
$managefrm->setFormField('bio', '');
$managefrm->setFormField('password', '');
$managefrm->setFormField('confirm_password', '');
$managefrm->setFormField('name', '');
$managefrm->setFormField('display_name', '');
$managefrm->setFormField('sex', '');
$managefrm->setFormField('gender', '');
$managefrm->setFormField('close_id', '');
$managefrm->setFormField('block', '');
$managefrm->setFormField('avatar', '');
$managefrm->setFormField('fb_image', '');
$managefrm->setFormField('fb_image_status', '');
$managefrm->setFormField('ng_image', '');
$managefrm->setFormField('ng_image_status', '');
$managefrm->setFormField('previous_status', '');
$managefrm->setFormField('auth_token', '');
$managefrm->sanitizeFormInputs($_REQUEST);
$isFacebookUser = false;
if ($CFG['admin']['module']['facebook'])
	{
		$isFacebookUser = $managefrm->isFacebookUser();
		if ($isFacebookUser)
			{
				$appapikey = $CFG['facebook']['api_key'];
				$appsecret = $CFG['facebook']['appsecret'];
				$facebook = new Facebook($appapikey, $appsecret);
				$smartyObj->assign('show_publish_stream', false);
				$smartyObj->assign('show_offline_access',false);

				if( !$facebook->api_client->users_hasAppPermission("offline_access") )
					{
						$smartyObj->assign('show_offline_access',true);
						$managefrm->updateFaceBookInfiniteSession($CFG['user']['user_id'],'',$_REQUEST[$CFG['facebook']['api_key'] . '_user']);
					}
				else
					{
						if( isset($_REQUEST[$CFG['facebook']['api_key'] . '_expires']) && ($_REQUEST[$CFG['facebook']['api_key'] . '_expires'] == 0))
							{
								$managefrm->updateFaceBookInfiniteSession($CFG['user']['user_id'],$_REQUEST[$CFG['facebook']['api_key'] . '_session_key'],
																			$_REQUEST[$CFG['facebook']['api_key'] . '_user']);
							}
					}

				if(!$facebook->api_client->users_hasAppPermission("publish_stream"))
					{
						$smartyObj->assign('show_publish_stream',true);
					}
			}
	}
if ($isFacebookUser && $managefrm->isFormGETed($_GET, 'fb_image_status'))
	{
		ob_start;
		$managefrm->sanitizeFormInputs($_REQUEST);
		$managefrm->getFormField('fb_image_status');
		$managefrm->setFormField('fb_image', $managefrm->getFormField('fb_image_status'));
		$managefrm->updateFacebookImageStatus();
		if($managefrm->getFormField('tr_image') == 'No')
			{
				$managefrm->updateImagePath();
			}
		ob_end_flush();
		die();
	}
if($isFacebookUser)
	{
		$managefrm->setFormField('fb_image', $managefrm->getFacebookCurrentStatus());
	}

if($managefrm->IsTwitterUser())
	{
		$managefrm->setFormField('tr_image', $managefrm->isTwitterImageAllowed());

	}
$managefrm->setIndexBlockFormField($index_block_arr);
$pagename = 'Manage Settings';
$managefrm->sanitizeFormInputs($_REQUEST);
if ($managefrm->isFormGETed($_GET, 'ng_image_status'))
	{
		ob_start;
		$managefrm->sanitizeFormInputs($_REQUEST);
		$managefrm->getFormField('ng_image_status');
		$managefrm->setFormField('ng_image', $managefrm->getFormField('ng_image_status'));
		$managefrm->updateNingImageStatus();
		if($managefrm->getFormField('ng_image') == 'No')
			{
				$managefrm->updateImagePath();
			}
		ob_end_flush();
		die();
	}

if ($managefrm->isFormGETed($_GET, 'close_id'))
	{
		ob_start;
		// 1 - recent_questions
		// 2 - popular_questions
		// 3 - recent_blogs
		// 4 - recent_forums
		// 5 -featured_analyst
		// 6 -best_answered_questions
		// 7 -popular_videos
		// 8 - recently_answered
		$close_id = trim($managefrm->getFormField('close_id'));
		$user_settings_arr = $managefrm->getUserDetailsArrFromDB($CFG['db']['tbl']['users_ans_log'], array('index_block'), $CFG['user']['user_id']);
		$req_array = unserialize($user_settings_arr['index_block']);
		switch ($close_id)
			{
				case 1:
					$req_array['recent_questions'] = 'no';
					break;
				case 2:
					$req_array['popular_questions'] = 'no';
					break;
				case 3:
					$req_array['recent_blogs'] = 'no';
					break;
				case 4:
					$req_array['recent_forums'] = 'no';
					break;
				case 5:
					$req_array['featured_analyst'] = 'no';
					break;
				case 6:
					$req_array['best_answered_questions'] = 'no';
					break;
				case 7:
					$req_array['popular_videos'] = 'no';
					break;
				case 8:
					$req_array['recently_answered'] = 'no';
					break;
			}
		$new_req_array = serialize($req_array);
		$managefrm->setFormField('block' , $new_req_array) ;
		$managefrm->updateCloseWindow();
		ob_end_flush();
		die();
	}
if($managefrm->chkIsNingUser())
	{
		$managefrm->setFormField('ng_image', $managefrm->isNingImageAllowed());
	}
if ($managefrm->isFormGETed($_GET, 'task'))
	{
		$managefrm->setFormField('task', $managefrm->getFormField('task'));
	}
$managefrm->LANG['managesettings_password'] = str_ireplace('VAR_MIN_COUNT', $CFG['admin']['password_min_size'], $managefrm->LANG['managesettings_password']);
$managefrm->LANG['managesettings_password'] = str_ireplace('VAR_MAX_COUNT', $CFG['admin']['password_max_size'], $managefrm->LANG['managesettings_password']);
$managefrm->LANG['managesettings_display_name'] = str_ireplace('VAR_MIN_COUNT', $CFG['admin']['display_name_min_size'], $managefrm->LANG['managesettings_display_name']);
$managefrm->LANG['managesettings_display_name'] = str_ireplace('VAR_MAX_COUNT', $CFG['admin']['display_name_max_size'], $managefrm->LANG['managesettings_display_name']);
// Default page block
$managefrm->setAllPageBlocksHide();
$managefrm->setPageBlockShow('form_search');
$managefrm->setPageBlockShow('form_settings');
$title = $LANG['managesettings_title'];
if ($managefrm->isFormPOSTed($_POST, 'managesettings_submit'))
	{
		$user_details_arr = $managefrm->getUserDetailsFromUsersTable($CFG['db']['tbl']['users'], $CFG['user']['user_id']);
		$managefrm->sanitizeFormInputs($_POST);
		if($isFacebookUser)
			{
				$current_settings = $managefrm->getFacebookCurrentStatus();
				$prev_settings = $managefrm->isFacebookImageAllowed();
				$managefrm->setFormField('previous_status', $prev_settings);
				if($current_settings !=  $prev_settings )
					{
						if ($managefrm->getFormField('fb_image') !='Yes')
							{
								$managefrm->updateImagePath();
								$managefrm->updateFacebookImageCurrentStatus($current_settings);
							}
					}
			 }
		if (!$CFG['admin']['use_profile_external_image'])
			{
				if (isset($_FILES['photo']) and $_FILES['photo']['tmp_name'])
					{
						$managefrm->chkValidFileType('photo', $LANG['err_tip_invalid_file_type']) and
						$managefrm->chkValideFileSize('photo', $LANG['err_tip_invalid_file_size']) and
						$managefrm->chkErrorInFile('photo', $LANG['err_tip_invalid_file']);
					}
			}
		$update_users_fields = array();
		$update_users_fields[] = 'bio';
		if ($managefrm->getFormField('bio'))
			{
				$bio = html_entity_decode($managefrm->getFormField('bio'));
				$bio = strip_tags ($bio, $CFG['html']['allowed_tags']);
				$managefrm->setFormField('bio', $bio);
			}
		if ($managefrm->getFormField('name'))
			{
				$managefrm->chkIsNotEmpty('name', $LANG['err_tip_compulsory']) and
				$managefrm->chkIsAlphaNumeric('name', $LANG['err_tip_invalid_username']) and
				$managefrm->chkIsValidSize('name', $CFG['admin']['username_min_size'], $CFG['admin']['username_max_size'], $LANG['err_tip_invalid_size']) and
				$managefrm->chkIsAllowedUserName($LANG['not_allowed_username']) and
				$managefrm->chkIsNotDuplicateName($CFG['db']['tbl']['users'], 'name', $LANG['err_tip_name_already_exists']);
				$update_users_fields[] = 'name';
				$managefrm->setFormField('name', $managefrm->getFormField('name'));
			}
		$managefrm->chkIsNotEmpty('display_name', $LANG['err_tip_compulsory']) and
		$managefrm->chkIsValidSize('display_name', $CFG['admin']['display_name_min_size'], $CFG['admin']['display_name_max_size'], $LANG['err_tip_invalid_size']);
		$update_users_fields[] = 'display_name';
		if (($managefrm->getFormField('new_mail') or $managefrm->getFormField('password')) AND isDemoSiteAndDemoUserId($CFG['user']['user_id']))
			{
				$managefrm->setPageBlockShow('block_msg_form_error');
				$LANG['common_msg_error_sorry'] = $LANG['no_permission_to_edit_password_mail'];
				$managefrm->setCommonErrorMsg($managefrm->LANG['common_msg_error_sorry']);
				$managefrm->setPageBlockShow('form_editprofile');
			}
		else
			{
				if ($managefrm->getFormField('new_mail'))
					{
						$managefrm->chkIsNotEmpty('new_mail', $LANG['err_tip_compulsory']) and
						$managefrm->chkIsValidEmail('new_mail', $LANG['err_tip_invalid_email']) and
						($managefrm->chkIsNotDuplicateEmail($CFG['db']['tbl']['users'], 'new_mail', $LANG['err_tip_email_already_exists']) or
							$managefrm->setCommonErrorMsg($LANG['err_tip_email_already_exists']));
					}
				if ($managefrm->getFormField('password'))
					{
						$managefrm->chkIsValidSize('password', $CFG['admin']['password_min_size'], $CFG['admin']['password_max_size'], $LANG['err_tip_invalid_size']);
						$managefrm->chkIsNotEmpty('confirm_password', $LANG['err_tip_compulsory']) and
						$managefrm->chkIsSamePasswords('password', 'confirm_password', $LANG['common_err_tip_same_password']);
						$managefrm->chkIsPasswordAndUserNameAreSame($LANG['password_user_name']);
						if ($managefrm->isValidFormInputs())
							{
								$update_users_fields[] = 'password';
								$managefrm->setFormField('password', md5($managefrm->getFormField('password')));
							}
					}
			}
		if ($managefrm->getFormField('gender'))
			{
				$update_users_fields[] = 'gender';
				$managefrm->setFormField('gender', $managefrm->getFormField('gender'));
			}
		if ($managefrm->isValidFormInputs())
			{
				if ($managefrm->getFormField('new_mail'))
					{
						$nameCode = substr(md5($CFG['user']['name']), 0, 8);
						$mailCode = substr(md5($managefrm->getFormField('new_mail')), 0, 8);
						$mailto = $managefrm->getFormField('new_mail');
						$managefrm->sendActivationForMail($mailto, $nameCode, $mailCode);
					}
				$managefrm->serializeIndexBlockValues($index_block_arr);
				$insert_arr = array();
				$get_insert_arr = array('subscribe_keywords', 'keyword_mail', 'reply_mail', 'favorite_mail', 'best_ans_mail', 'abuse_mail', 'internal_mail', 'index_block');
				foreach($get_insert_arr as $insert_value)
					{
						if ($managefrm->getFormField($insert_value) != '')
							array_push($insert_arr, $insert_value);
					}
				$managefrm->updateFormFieldsInUsersLogable($CFG['db']['tbl']['users_ans_log'], $CFG['user']['user_id'], $insert_arr);
				$managefrm->updateFormFieldsInTable($CFG['db']['tbl']['users'], $CFG['user']['user_id'], $update_users_fields);
				if ( (($isFacebookUser AND $managefrm->isFacebookImageAllowed() != 'Yes') OR !$isFacebookUser) && (($managefrm->IsTwitterUser() && $managefrm->isTwitterImageAllowed() != 'Yes') || !$managefrm->IsTwitterUser()))
					{
						if ($CFG['admin']['use_profile_external_image'])
							{
								$managefrm->updateUserImagePath();
							}
						else if (isset($_FILES['photo']) and $_FILES['photo']['tmp_name'])
							{
								$managefrm->photoUpload();
							}
						else if ($managefrm->getFormField('avatar'))
							{
								$avatarImagePath = $CFG['site']['url'].$CFG['admin']['avatar_path'].$managefrm->getFormField('avatar');
								$managefrm->setFormField('image_path' , $avatarImagePath);
								$managefrm->updateUserImagePath();
							}
					}
				else if($managefrm->chkIsNingUser())
					{
						$ning = $managefrm->isNingImageAllowed();
						if(trim($ning) != 'Yes' )
							{

								if ($CFG['admin']['use_profile_external_image'])
									{
										$managefrm->updateUserImagePath();
									}
								else if (isset($_FILES['photo']) and $_FILES['photo']['tmp_name'])
									{
										$managefrm->photoUpload();
									}
								else if ($managefrm->getFormField('avatar'))
									{
										$avatarImagePath = $CFG['site']['url'] . $CFG['admin']['avatar_path'] . $managefrm->getFormField('avatar');
										$managefrm->setFormField('image_path' , $avatarImagePath);
										$managefrm->updateUserImagePath();
									}
							}
					}
				else
					{
						if ($CFG['admin']['use_profile_external_image'])
									{
										$managefrm->updateUserImagePath();
									}
								else if (isset($_FILES['photo']) and $_FILES['photo']['tmp_name'])
									{
										$managefrm->photoUpload();
									}
								else if ($managefrm->getFormField('avatar'))
									{
										$avatarImagePath = $CFG['site']['url'] . $CFG['admin']['avatar_path'] . $managefrm->getFormField('avatar');
										$managefrm->setFormField('image_path' , $avatarImagePath);
										$managefrm->updateUserImagePath();
									}
					}


				$managefrm->setFormField('password', '');
				$managefrm->setPageBlockShow('block_msg_form_success');
				$managefrm->setCommonSuccessMsg($LANG['managesettings_updated_successfully']);
				$_SESSION['user']['display_name'] = $managefrm->getFormField('display_name');

				if($isFacebookUser)
					{
						$current_settings = $managefrm->getFacebookCurrentStatus();
						$prev_settings = $managefrm->isFacebookImageAllowed();
						$managefrm->setFormField('previous_status', $prev_settings);
						if($current_settings ==  'Yes')
							{
								if ($managefrm->getFormField('fb_image') =='Yes')
									{
										$user_id = $managefrm->getFacebookProfileId();
										$user_details = $facebook->api_client->users_getInfo($user_id, array('last_name','first_name','name','sex', 'pic', 'pic_with_logo', 'pic_small'));

										$pic_with_logo = $user_details[0]['pic_with_logo'];
										$pic = $user_details[0]['pic'];
										$pic_small = $user_details[0]['pic_small'];
										$managefrm->chkIsFaceBookImageUploaded($pic_with_logo, $CFG['user']['user_id']);
									}
							}
					}
				Redirect2URL(getAnovaUrl('managesettings', '?msg=1', '?msg=1'));
			}
		else
			{
				// $managefrm->setAllPageBlocksHide();
				$managefrm->setPageBlockShow('block_msg_form_error');
				$managefrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$managefrm->setPageBlockShow('form_editprofile');
			}
	}
elseif ($managefrm->isFormPOSTed($_POST, 'managesettings_cancel'))
	{
		if ($managefrm->isFormPOSTed($_POST, 'task'))
			Redirect2URL(getAnovaUrl('index'));
		else
			Redirect2URL(getMemberUrl($CFG['user']['user_id'], $CFG['user']['name']));
	}
else
	{
		$user_settings_arr = $managefrm->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'),
			$CFG['user']['user_id']);
		$managefrm->populateIndexBlockValues($user_settings_arr['index_block']);
		$managefrm->setFormField('subscribe_keywords', $user_settings_arr['subscribe_keywords']);
		$managefrm->setFormField('keyword_mail', $user_settings_arr['keyword_mail']);
		$managefrm->setFormField('reply_mail', $user_settings_arr['reply_mail']);
		$managefrm->setFormField('favorite_mail', $user_settings_arr['favorite_mail']);
		$managefrm->setFormField('best_ans_mail', $user_settings_arr['best_ans_mail']);
		$managefrm->setFormField('abuse_mail', $user_settings_arr['abuse_mail']);
		$managefrm->setFormField('internal_mail', $user_settings_arr['internal_mail']);
		$user_details_arr = $managefrm->getUserDetailsFromUsersTable($CFG['db']['tbl']['users'], $CFG['user']['user_id']);
		$managefrm->setFormField('image_path', $user_details_arr['image_path']);
		$managefrm->setFormField('bio', $user_details_arr['bio']);
		$managefrm->setFormField('display_name', $user_details_arr['display_name']);
		$managefrm->setFormField('gender', $user_details_arr['gender']);
	}
if ($managefrm->isFormPOSTed($_GET, 'msg'))
	{
		$managefrm->setPageBlockShow('block_msg_form_success');
		$managefrm->setCommonSuccessMsg($LANG['managesettings_updated_successfully']);
	}
// --------------------Page block templates begins-------------------->>>>>//
if ($managefrm->isShowPageBlock('form_settings'))
	{
		$managefrm->LANG['common_tag_size'] = str_ireplace('VAR_MIN_SIZE', $managefrm->CFG['admin']['tag_min_size'], $managefrm->LANG['common_tag_size']);
		$managefrm->LANG['common_tag_size'] = str_ireplace('VAR_MAX_SIZE', $managefrm->CFG['admin']['tag_max_size'], $managefrm->LANG['common_tag_size']);
		$managefrm->form_settings['common_tag_size'] = $managefrm->LANG['common_tag_size'];
		$managefrm->form_settings['htmlspecial'] = htmlspecialchars($CFG['html']['allowed_tags']);
		if (isUserImageAllowed())
			{
				$managefrm->form_settings['user_details_arr'] = $user_details_arr;
				if (!$CFG['admin']['use_profile_external_image'])
					{
						$managefrm->form_settings['str_ireplace'] = str_ireplace('VAR_SIZE', $CFG['admin']['ans_photos']['max_size'], $LANG['photo_allowed_size']);
						$allowed_image_formats = implode(', ', $CFG['admin']['ans_photos']['format_arr']);
						$allowed_image_formats = str_replace('image/', '', $allowed_image_formats);
						$managefrm->form_settings['photo_allowed_image_type'] = $LANG['photo_allowed_image_type'].': '.$allowed_image_formats;
					}
			}
		$managefrm->form_settings['populateIndexPageFields'] = $managefrm->populateIndexPageFields($index_block_arr);
		$managefrm->form_settings['trim_task'] = trim($managefrm->getFormField('task'));
	}
// include the header file
$managefrm->includeHeader();
// include the content of the page
setTemplateFolder('members/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('manageSettings.tpl');
// includ the footer of the page
?>
<script language="javascript" type="text/javascript">
	function checkAllMail(frm)
		{
			var frmLength = frm.elements.length;
			var rdId = '';
			var rdVal = '_no';
			if (($('all_email_notification').checked))
				rdVal = '_yes';
			for ( var i=0; i<frmLength; i++) {
				if ((frm.elements[i].name.indexOf('_mail')!=-1) && (frm.elements[i].type == 'radio')){
					rdId = frm.elements[i].name + rdVal;
					$(rdId).checked = true;
				}
			}
		}
	function checkAllSettings(frm)
		{
			var frmLength = frm.elements.length;
			var rdId = '';
			var rdVal = '_no';
			if (($('all_home_settings').checked))
				rdVal = '_yes';
			for ( var i=0; i<frmLength; i++) {
				if ((frm.elements[i].name.indexOf('_mail')==-1) && (frm.elements[i].type == 'radio') && (frm.elements[i].name.indexOf('gender')==-1)){
					rdId = frm.elements[i].name + rdVal;
					$(rdId).checked = true;
				}
			}
		}
	needLiveValidation('display_name',Array('Presence','Length|3|30'));
	<?php if(!$isFacebookUser && !$managefrm->chkIsNingUser() && !$managefrm->IsTwitterUser() && isMember()) { ?>
	needLiveValidation('new_mail',Array('Email'));
	needLiveValidation('password',Array('Length|4|30'));
	needLiveValidation('confirm_password',Array('Confirmation|password'));
	<?php } ?>
	<?php if($isFacebookUser) { ?>
	function openFacebook()
		{
			var left = (screen.width/2)-(550/2);
			var top = (screen.height/2)-(350/2);
			window.open ("http://www.facebook.com/authorize.php?api_key=<?php echo $CFG['facebook']['api_key'];?>&ext_perm=email&v=1.0","mywindow","menubar=1,resizable=1, width=550, height=350, top="+top+", left="+left);
			mywindow.moveTo(0,0);
		}
	<?php } ?>
</script>
<?php if (isUserImageAllowed()) { ?>
	<script language="javascript" type="text/javascript">
		ajaxUpdateDiv('<?php echo $CFG['site']['url'].$CFG['admin']['index']['anova_module']?>/avatars.php', '', 'selShowAvatarImages');
	</script>
<?php } ?>
<?php
// <<<<<<--------------------Page block templates Ends--------------------//
$managefrm->includeFooter();
?>