<?php
/**
 * This file handles the Email this Question page
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###invite friends###
 * @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-19
 */
class EmailAnswersFormHandler extends FormHandler
	{
    	public $sent_email_arr=array();

		/**
		 * EmailAnswersFormHandler::chkIsValidEmail()
		 *
		 * @param mixed $field_name
		 * @param string $err_tip
		 * @param mixed $is_chk_mxrr
		 * @return
		 */
		public function chkIsValidEmail($field_name, $err_tip='', $is_chk_mxrr=false)
			{
				return $is_ok = (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $this->fields_arr[$field_name]));
			}

		/**
		 * EmailAnswersFormHandler::validateEmailInAnswers()
		 *
		 * @param mixed $email
		 * @return
		 */
		public function validateEmailInAnswers($email)
			{
				if(!($email = $email))
					return;

				$this->fields_arr['email_temp'] = $email;
				if(!$this->chkIsValidEmail('email_temp'))
					{
						$this->invalid_email_arr['email'][] = $email;
						return;
					}
				$this->sent_email_arr['email'][] = $email;
			}

		/**
		 * EmailAnswersFormHandler::chkIsValidInputsProvided()
		 *
		 * @param mixed $field_name
		 * @return
		 */
		public function chkIsValidInputsProvided($field_name)
			{
				$email_arr = str_replace(' ','',$this->fields_arr[$field_name]);
				$email_arr = explode(',', $email_arr);
				$email_arr =array_unique($email_arr);

				foreach($email_arr as $key=>$value)
					{
						$this->validateEmailInAnswers($email_arr[$key]);
					}
			}

		/**
		 * To check whether the email ids are valid or not
		 *
		 * @param 		string $field_name email
		 * @param 		string $err_tip
		 * @return 		array
		 * @access 		public
		 */
		public function chkIsValidEmailIDs($field_name, $err_tip='')
			{
				$this->chkIsValidInputsProvided($field_name);
				//return isset($this->sent_email_arr['email'])?$this->sent_email_arr['email']:array();
				$email_arr = explode(',', $this->fields_arr[$field_name]);
				$emails_arr = array();
				$count_err = count($email_arr);
				foreach($email_arr as $val)
					{
						if (empty($val) || $val == '') continue;
						$is_ok = (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $val));
						//$is_ok = (preg_match("/^\S+@\S+\.\S+$/i", $val));
						if (!$is_ok)
							{
								if($count_err > 1)
									$this->fields_err_tip_arr[$field_name] = $err_tip;
								else
									$this->fields_err_tip_arr[$field_name] = $this->LANG['err_tip_invalid_email'];
								return false;
							}
						else
							{
								$emails_arr[] = $val;
							}
					}

				if (!$emails_arr)
					{
						$this->fields_err_tip_arr[$field_name] = $this->LANG['err_tip_enter_valid_email_ids'];
						return false;
					}
				return $emails_arr;
			}

		/**
		 * To send the activation code
		 *
		 * @param 		string $email email
		 * @param 		string $subj mail subject
		 * @param 		string $msg mail message content
		 * @param 		string $sender_name sender name
		 * @param 		string $sender_email sender email
		 * @return 		boolean
		 * @access 		public
		 */
		public function sendAlertMail($to_mail, $content, $sender_name, $sender_email)
			{
				$subject = $this->LANG['invite_friends_subject'];
				$subject  = str_ireplace('VAR_SITENAME', $this->CFG['site']['name'], $this->LANG['invite_friends_subject']);

				$content = str_ireplace('VAR_USERNAME', $this->fields_arr['userename'], $content);
				$content= str_ireplace('VAR_MESSAGE', $this->fields_arr['message'], $content);

				$this->buildEmailTemplate($subject, $content, false, true);
				$EasySwift = new EasySwift($this->getSwiftConnection());
				$EasySwift->flush();
				$EasySwift->addPart($this->getEmailContent(), "text/html");
				return $EasySwift->send($to_mail, $sender_email, $this->getEmailSubject());

			}
		public function clearFormFieldErrors()
			{
				$this->fields_err_tip_arr = array();
			}
    }
//<<<<<---------------- Class EmailAnswersFormHandler ends -------------//
//--------------------- Code begins -------------------->>>>>//
$emailanswersfrm = new EmailAnswersFormHandler();
$emailanswersfrm->setPageBlockNames(array('form_contactus_show'));
//set the form field
if(isMember())
	{
		$emailanswersfrm->setFormField('userename', $emailanswersfrm->CFG['user']['display_name']);
		$emailanswersfrm->setFormField('useremail', $emailanswersfrm->CFG['user']['email']);
	}
else
	{
		$emailanswersfrm->setFormField('userename', $emailanswersfrm->CFG['site']['noreply_name']);
		$emailanswersfrm->setFormField('useremail', $emailanswersfrm->CFG['site']['noreply_email']);
	}
$emailanswersfrm->setFormField('to_emails', '');
$emailanswersfrm->setFormField('to_emails', '');
$emailanswersfrm->setFormField('email_set', array());
$emailanswersfrm->setFormField('email_set_names', array());
$emailanswersfrm->setFormField('qid', '');
$emailanswersfrm->setFormField('title', '');
$emailanswersfrm->setFormField('message', '');
$emailanswersfrm->sanitizeFormInputs($_REQUEST);
$emailanswersfrm->setAllPageBlocksHide();
$emailanswersfrm->setPageBlockShow('form_contactus_show');
if ($emailanswersfrm->isFormPOSTed($_POST, 'submit_contactus'))
	{
		$emailanswersfrm->chkIsNotEmpty('userename', $LANG['err_tip_compulsory']);
		$emailanswersfrm->chkIsNotEmpty('to_emails', $LANG['err_tip_compulsory']);
		$emailanswersfrm->chkIsNotEmpty('message', $LANG['err_tip_compulsory']);
		$email_arr = $emailanswersfrm->chkIsValidEmailIDs('to_emails', $LANG['err_tip_invalid_email_ids']);
		if ($emailanswersfrm->getFormField('email_set') && !$emailanswersfrm->getFormField('to_emails'))
			{
				$emailanswersfrm->clearFormFieldErrors();
			}
		if ($emailanswersfrm->isValidFormInputs())
			{
				$site_name = '<a href="'.$CFG['site']['url'].'">'.$CFG['site']['url'].'</a>';
				$LANG['invite_friends_message'] = str_ireplace('VAR_NAME', $emailanswersfrm->getFormField('userename'), $LANG['invite_friends_message']);
				$LANG['invite_friends_message'] = str_ireplace('VAR_SITE', $CFG['site']['name'], $LANG['invite_friends_message']);
				$LANG['invite_friends_message'] = str_ireplace('VAR_LINK', $site_name, $LANG['invite_friends_message']);
				$LANG['invite_friends_message'] = str_ireplace('VAR_SITENAME', $CFG['site']['name'], $LANG['invite_friends_message']);
				foreach($email_arr as $email_ids)
				{

					$emailanswersfrm->sendAlertMail($email_ids,
											$LANG['invite_friends_message'],
											$emailanswersfrm->CFG['site']['noreply_name'],
			 								$emailanswersfrm->CFG['site']['noreply_email']);
				}
				$emailanswersfrm->setDisplayVar('mail_status', $emailanswersfrm->getFormField('to_emails'));
				Redirect2URL(getAnovaUrl('invitefriends', '?invited', '?invited'));
			}
		else
			{
				$emailanswersfrm->setPageBlockShow('block_msg_form_error');
				$emailanswersfrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
			}
	}
elseif ($emailanswersfrm->isFormGETed($_GET, 'invited'))
	{
		$emailanswersfrm->setPageBlockShow('block_msg_form_success');
		$emailanswersfrm->setCommonSuccessMsg($LANG['emailanswers_success']);
	}
//<<<<<----------------- Code ends -------------------//
//--------------- Page block templates begins ------------>>>>>//
if ($emailanswersfrm->isShowPageBlock('form_contactus_show'))
	{
		$emailanswersfrm->form_contactus_show['condition']	=	($emailanswersfrm->CFG['user']['user_id'] > 0) ? ' readonly':'';
		$emailanswersfrm->form_contactus_show['popupWindow']	=	'return popupWindow(\''.getUrl('importer','?frmname=form_contactus_show&fname=to_emails', '?frmname=form_contactus_show&fname=to_emails','root').'\')';
	}
$imported_emails = array();
$emailSetNames = $emailanswersfrm->getFormField('email_set_names');
foreach($emailanswersfrm->getFormField('email_set') as $key=>$email)
	{
		$imported_emails[$email] = $emailSetNames[$key];
	}
$smartyObj->assign('imported_emails', $imported_emails);

$emailanswersfrm->topic_name = $LANG['emailanswers_title'];
$emailanswersfrm->import_url = getUrl('importer', '?frmname=form_contactus_show&fname=to_emails', '?frmname=form_contactus_show&fname=to_emails', 'root');
//include the header file
$emailanswersfrm->includeHeader();
//include the content of the page
setTemplateFolder('general/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('inviteFriends.tpl');
//includ the footer of the page
?>
<script language="javascript" type="text/javascript">
	function popupWindow(url)
		{
			 window.open (url, "","status=0,toolbar=0,resizable=0,scrollbars=1");
			 return false;
		}
</script>
<script type="text/javascript">
	needLiveValidation('userename',Array('Presence'));
	needLiveValidation('message',Array('Presence'));
</script>
<?php
//<<<<<<--------------------Page block templates Ends--------------------//
$emailanswersfrm->includeFooter();
?>