<?php
/**
 * Add news
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Add newa letter###
 * @author 		selvaraj_47ag04
 * @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-23
 * @filesource
 **/
 /**
  * Including the config file to get the global data for site
  */
require_once('../common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/admin/addNewsLetter.php';
//Gender List
//Search Options Array List
$CFG['mods']['include_files'][] = 'common/classes/class_ListRecordsHandler.lib.php';
$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';
//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 addNewsLetter --------------->>>//
/**
 * addNewsLetter is to add/update type
 **/
class addNewsLetter extends FormHandler
	{
		/**
		 * Insert data in table
		 *
		 * @param string $tableName
		 * @param array $fields_to_insert_arr
		 * @return integer
		 **/
		public function insertFormFieldsInTable($tableName, $fields_to_insert_arr=array())
			{
				$sql = 'INSERT INTO '.$tableName.' SET date_added = NOW(), ';// sql_condition = '.$this->fields_arr['sql_condition'].' , ';
				foreach($fields_to_insert_arr as $field_name)
					if(isset($this->fields_arr[$field_name]))
						$sql .= $field_name.'=\''.addslashes($this->fields_arr[$field_name]).'\', ';
				$sql = substr($sql, 0, strrpos($sql, ','));
				// prepare sql
				$stmt = $this->dbObj->Prepare($sql);
				// execute sql
				$result_set = $this->dbObj->Execute($stmt);
				//raise user error... fatal
				if (!$result_set)
						trigger_db_error($this->dbObj);
				return($this->dbObj->Insert_ID());
			}

		/**
		 * addNewsLetter::isEmpty()
		 *
		 * @param mixed $value
		 * @return
		 */
		public function isEmpty($value)
			{
				$is_not_empty = is_string($value)?trim($value)=='':empty($value);
				return $is_not_empty;
			}

		/**
		 * addNewsLetter::buildConditionQuery()
		 *
		 * @return
		 */
		public function buildConditionQuery()
			{
				$sql_condition = " usr_status!='Deleted'";
				$condition = '';
				if (!$this->isEmpty($this->fields_arr["uname"]))
				    {
						$sql_condition .= " AND name LIKE '%".addslashes($this->fields_arr['uname'])."%' ";
				    }
				if (!$this->isEmpty($this->fields_arr["email"]))
				    {
						$sql_condition .= " AND email LIKE '%".addslashes($this->fields_arr['email'])."%' ";
				    }
				if (!$this->isEmpty($this->fields_arr['gender']))
				    {
						$sql_condition .= " AND sex='".addslashes($this->fields_arr['gender'])."'";
				    }
				if (!$this->isEmpty($this->fields_arr['doj_start']))
				    {
						$dojStart  = $this->fields_arr['doj_start'];
						$sql_condition .= " AND doj >= '".addslashes($dojStart)."'";
				    }
				if (!$this->isEmpty($this->fields_arr['doj_end']))
				    {
						$dojEnd  = $this->fields_arr['doj_end'];
						$sql_condition .= " AND doj <= '".addslashes($dojEnd)."'";
				    }
				if (!$this->isEmpty($this->fields_arr['login_start']))
				    {
						$loginStart  = $this->fields_arr['login_start'];
						$sql_condition .= " AND last_logged <= '".addslashes($loginStart)."'";
				    }
				$statusOk = $this->fields_arr['user_status_Ok'];
				$statusDeActivate = $this->fields_arr['user_status_ToActivate'];
				$statusLocked = $this->fields_arr['user_status_Locked'];
				if ($statusOk OR $statusDeActivate OR $statusLocked)
				    {
						$statusCondition = '( 0 ';
						$statusCondition .= $statusOk?" OR usr_status='Ok'":'';
						$statusCondition .= $statusDeActivate?" OR usr_status='ToActivate'":'';
						$statusCondition .= $statusLocked?" OR usr_status='Locked'":'';
						$statusCondition .= ')';
						$sql_condition .= ' AND '.$statusCondition;
				    }
				$this->fields_arr['sql_condition'] = $sql_condition ;
			}

		/**
		 * addNewsLetter::populateYearsListCurrent()
		 *
		 * @param mixed $highlight_y
		 * @param mixed $min_year
		 * @param mixed $max_year
		 * @return
		 */
		public function populateYearsListCurrent($highlight_y, $min_year, $max_year)
			{
				for($y=$min_year; $y<=$max_year; ++$y)
					{
?>
	<option value="<?php echo $y;?>"<?php echo($highlight_y==$y)? ' selected="selected"' : '';?>><?php echo $y;?></option>
<?php
					}
			}
	}
//<<<<<---------------class addNewsLetter ends -------------///
//--------------------Code begins-------------->>>>>//
$addnewsletter = new addNewsLetter();
$addnewsletter->setPageBlockNames(array('block_form_add_letter', 'block_form_confirm','block_form_search'));
$addnewsletter->setFormField('subject', '');
$addnewsletter->setFormField('body', '');
$addnewsletter->setFormField('email', '');
$addnewsletter->setFormField('uname', '');
$addnewsletter->setFormField('tagz', '');
$addnewsletter->setFormField('gender', '');
$addnewsletter->setFormField('sql_condition', '');
$addnewsletter->setFormField('user_status_Ok', '');
$addnewsletter->setFormField('user_status_ToActivate', '');
$addnewsletter->setFormField('user_status_Locked', '');
$addnewsletter->setFormField('test_users', '');
$addnewsletter->setFormField('mail_error', '');
$addnewsletter->setFormField('doj_start', '');
$addnewsletter->setFormField('doj_end', '');
$addnewsletter->setFormField('login_start', '');
$addnewsletter->setFormField('withPhotoVideoFriends', '');
$addnewsletter->setFormField('search' ,'');
$addnewsletter->setYearsListMinMax(date('Y')-1, date('Y')+1);
$addnewsletter->setAllPageBlocksHide();
$addnewsletter->setPageBlockShow('block_form_search');
$addnewsletter->setPageBlockShow('block_form_add_letter');
if(isDemoSite() AND ($addnewsletter->isFormPOSTed($_POST, 'addstock') OR $addnewsletter->isFormPOSTed($_POST, 'submit_confirm')))
	{
		$addnewsletter->setAllPageBlocksHide();
		$addnewsletter->setCommonErrorMsg($LANG['no_permission_for_demo_user']);
		$addnewsletter->setPageBlockShow('block_msg_form_error');
		$addnewsletter->setPageBlockShow('block_form_add_letter');
	}
elseif($addnewsletter->isFormPOSTed($_POST, 'addstock'))
	{
		$addnewsletter->sanitizeFormInputs($_POST);
		$addnewsletter->chkIsNotEmpty('subject', $addnewsletter->LANG['err_tip_compulsory']);
	    $addnewsletter->chkIsNotEmpty('body', $addnewsletter->LANG['err_tip_compulsory']);
		$addnewsletter->getFormField('email') and $addnewsletter->chkIsValidEmail('email', $LANG['err_tip_invalid_email']);
		$addnewsletter->getFormField('doj_start') and $addnewsletter->chkIsDateGreaterThanNow('doj_start', $addnewsletter->LANG['addletter_err_tip_invalid_date']);
		$addnewsletter->getFormField('doj_end') and $addnewsletter->chkIsDateGreaterThanNow('doj_end', $addnewsletter->LANG['addletter_err_tip_invalid_date']);
		(!($addnewsletter->getFormFieldErrorTip('doj_start') or $addnewsletter->getFormFieldErrorTip('doj_end')))?($addnewsletter->chkIsDateDiff('doj_start', 'doj_end', $addnewsletter->LANG['addletter_err_tip_invalid_date_diff'])):('');
		$addnewsletter->getFormField('login_start') and $addnewsletter->chkIsDateGreaterThanNow('login_start', $addnewsletter->LANG['addletter_err_tip_invalid_date']);
	    if($addnewsletter->isValidFormInputs())
			{
				$addnewsletter->buildConditionQuery();
				$addnewsletter->setPageBlockShow('block_form_confirm');
				$addnewsletter->setPageBlockShow('block_form_add_letter');
			}
		else
			{
				$addnewsletter->setAllPageBlocksHide();
				$addnewsletter->setCommonErrorMsg($addnewsletter->LANG['common_err_tip_error']);
				$addnewsletter->setPageBlockShow('block_msg_form_error');
				$addnewsletter->setPageBlockShow('block_form_add_letter');
			}
	}
elseif($addnewsletter->isFormPOSTed($_POST,'cancelstock'))
	{
		Redirect2URL($addnewsletter->CFG['site']['url']."admin/addNewsLetter.php");
	}
elseif($addnewsletter->isFormPOSTed($_POST, 'submit_confirm'))
	{
		$addnewsletter->sanitizeFormInputs($_POST);
		$addnewsletter->chkIsNotEmpty('subject', $addnewsletter->LANG['err_tip_compulsory']);
	    $addnewsletter->chkIsNotEmpty('body', $addnewsletter->LANG['err_tip_compulsory']);
	    if($addnewsletter->isValidFormInputs())
			{
			//	$addnewsletter->buildConditionQuery();
				$comment = htmlentitydecode($addnewsletter->getFormField('body'));
				//$comment = strip_tags ($comment, $addnewsletter->CFG['html']['allowed_tags']);
				$addnewsletter->setFormField('body', $comment);
				$addnewsletter->setAllPageBlocksHide();
				$addnewsletter->setCommonSuccessMsg($addnewsletter->LANG['addletter_success_message']);
				$addnewsletter->setPageBlockShow('block_msg_form_success');
				$inputArr = array('subject','body', 'sql_condition');
				$currentEventId = $addnewsletter->insertFormFieldsInTable($addnewsletter->CFG['db']['tbl']['news_letter'], $inputArr);
				$addnewsletter->setFormField('subject', '');
				$addnewsletter->setFormField('body', '');
				$addnewsletter->setPageBlockShow('block_form_add_letter');
			}
		else
			{
				$addnewsletter->setAllPageBlocksHide();
				$addnewsletter->setCommonErrorMsg($addnewsletter->LANG['common_err_tip_error']);
				$addnewsletter->setPageBlockShow('block_msg_form_error');
				$addnewsletter->setPageBlockShow('block_form_add_letter');
			}
	}
//<<<<<-------------------- Code ends----------------------//
//-------------------- Page block templates begins -------------------->>>>>//
//include the header file
$addnewsletter->includeHeader();
$smartyObj->assign('form_search_ad_hidden_arr', array('subject', 'body', 'sql_condition'));
$smartyObj->assign('gender_list_arr', array('Male'=>$addnewsletter->LANG['common_male_option'], 'Female'=>$addnewsletter->LANG['common_female_option']));
//include the content of the page
setTemplateFolder('admin/');
$smartyObj->display('addNewsLetter.tpl');
//<<<<<-------------------- Page block templates ends -------------------//
$addnewsletter->includeFooter();
?>