<?php
/**
 * Add editable messages
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Manage Static Pages###
 * @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-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/manageStaticPages.php';

$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';
$CFG['mods']['include_files'][] = 'common/classes/class_ListRecordsHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.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 ManageEditableMessagesFormHandler --------------->>>//
/**
 * ManageEditableMessagesFormHandler is to add/update type
 **/
class ManageEditableMessagesFormHandler extends AnovaHandler
	{

		//to store user_id
		public $user_id = '';
		public $staticPageTitle = '';

		/**
		 * set the condition
		 *
		 * @access public
		 * @return void
		 **/
		public function buildConditionQuery()
			{
				$this->sql_condition = '';
			}

		/**
		 * To build sorting query
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function buildSortQuery()
			{
				$this->sql_sort = $this->fields_arr['orderby_field'].' '.$this->fields_arr['orderby'];
			}

	   /**
		 * To add/update the event types image format
		 *
		 * @param 	string	$tablename
		 * @param 	int		$id
		 * @return 	boolean
		 * @access public
		 **/
		public function getEditableMessages($table_name,$id)
			{
				$sql = 'SELECT page_id, page_title, page_display_title, page_content, page_status, link_in_footer FROM '. $table_name .
						' WHERE page_id = \''.$id.'\'';

				$stmt = $this->dbObj->Prepare($sql);
				$result_set = $this->dbObj->Execute($stmt);
				if (!$result_set)
					trigger_db_error($this->dbObj);

				$is_ok = false;
				if($row = $result_set->FetchRow())
					{
					 	$this->setFormField('page_id',$row['page_id']);
					 	$this->setFormField('page_title',$row['page_title']);
					 	$this->setFormField('page_display_title',$row['page_display_title']);
					 	$this->setFormField('page_content',$row['page_content']);
					 	$this->setFormField('page_status',$row['page_status']);
					 	$this->setFormField('link_in_footer',$row['link_in_footer']);
						$is_ok = true;
					}
				return ($is_ok);
			}

		/**
		 * 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 '.$tableName.' SET page_added_date=NOW(), ';
				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, ','));

				$stmt = $this->dbObj->Prepare($sql);
				$result_set = $this->dbObj->Execute($stmt);
				if (!$result_set)
					trigger_db_error($this->dbObj);

				return($this->dbObj->Insert_ID());
			}

		/**
		 * update data in table
		 *
		 * @param string $tableName
		 * @param array $fields_to_insert_arr
		 * @return void
		 * @access public
		 **/
		public function updateFormFieldsInTable($tableName, $fields_to_insert_arr=array())
			{
				$sql = 'UPDATE '.$tableName.' SET page_added_date=NOW(), ';
				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, ','));
				$sql .= 'WHERE '.
						'page_id=\''.$this->fields_arr['id'].'\'';

				$stmt = $this->dbObj->Prepare($sql);
				$result_set = $this->dbObj->Execute($stmt);
				if (!$result_set)
					trigger_db_error($this->dbObj);
			}
		/**
		 * To delete type
		 *
		 * @param string $tableName
		 * @param int $id
		 * @return void
		 * @access public
		 **/
		public function deleteMessages($tableName, $did)
			{
				$id = $did;

				$sql = 'DELETE FROM ' . $tableName.' WHERE page_id IN('.$id.')';

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

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function chkIsTitleExists($table_name, $err_tip='')
		    {
				$sql = 'SELECT page_id FROM '.$table_name.' WHERE page_title='.$this->dbObj->Param('page_title');
				if ($this->fields_arr['id'])
					$sql .= ' AND page_id!=\''.addslashes($this->fields_arr['id']).'\'';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['page_title']));
				if (!$rs)
					trigger_db_error($this->dbObj);

				if (!$rs->PO_RecordCount())
					return true;
				$this->setCommonErrorMsg($err_tip);
				$this->fields_err_tip_arr['page_title'] = $err_tip;
				return false;
		    }

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function dateWithTime($dateTimeValue)
			{
				if ($dateTimeValue != '')
					{
						$formatDate = date("M jS, h.i a", strtotime($dateTimeValue));
					}
				else
					{
						$formatDate = '';
					}
				return($formatDate);
			}

		/**
		 * ManageEditableMessagesFormHandler::resetFieldsArray()
		 *
		 * @return
		 */
		public function resetFieldsArray()
			{
				$this->setFormField('id', 0);
				$this->setFormField('page_title', '');
				$this->setFormField('seo_title', '');
				$this->setFormField('page_display_title', '');
				$this->setFormField('page_content', '');
				$this->setFormField('page_status', 'Yes');
				$this->setFormField('link_in_footer', 'Yes');
			}
	}
//<<<<<---------------class ManageEditableMessagesFormHandler ends -------------///
//--------------------Code begins-------------->>>>>//
$editablefrm = new ManageEditableMessagesFormHandler();
$editablefrm->setLang(array('sort_ascending' => $LANG['list_sort_ascending'],
						'sort_descending' => $LANG['list_sort_descending']
						));
$editablefrm->setPageBlockNames(array('form_add_messages', 'form_disp_messages', 'form_confirm'));
$editablefrm->setColumnSortUrl($_SERVER['REQUEST_URI']);

$editablefrm->setSearchFormFieldNames('start', 'numpg', 'dsc', 'asc'); //, 'q');
$editablefrm->setFormField('page_id', '');
$editablefrm->resetFieldsArray();
$editablefrm->setFormField('delete', array());
$editablefrm->setFormField('to_do', '');
$editablefrm->setFormField('action', '');
$editablefrm->tabindex = 995;
$editablefrm->setFormField('asc', '');
$editablefrm->setFormField('dsc', '');
$editablefrm->setFormField('start', 0);
$editablefrm->setFormField('orderby_field', 'page_id');
$editablefrm->setFormField('orderby', 'ASC');
$editablefrm->setFormField('numpg', $CFG['data_tbl']['numpg']);//
$editablefrm->setMinRecordSelectLimit($CFG['data_tbl']['min_record_select_limit']);
$editablefrm->setMaxRecordSelectLimit($CFG['data_tbl']['max_record_select_limit']);
$editablefrm->setNumPerPageListArr($CFG['data_tbl']['numperpage_list_arr']);
$editablefrm->setTableNames(array($CFG['db']['tbl']['static_pages']));
$editablefrm->setReturnColumns(array('page_id', 'page_title', 'page_display_title', 'page_content', 'page_status', 'link_in_footer', 'page_added_date'));
$editablefrm->setAllPageBlocksHide();
$editablefrm->setPageBlockShow('form_add_messages');
$editablefrm->sanitizeFormInputs($_REQUEST);
$editablefrm->staticPageTitle = $LANG['editable_messages_page_add_title'];
if($editablefrm->isFormGETed($_GET,'showadd'))
	{
		$editablefrm->setPageBlockShow('form_add_messages');
		$editablefrm->setPageBlockShow('form_disp_messages');
	}
elseif($editablefrm->isFormPOSTed($_POST,'canceleditablemessage'))
	{
		Redirect2URL($CFG['site']['url']."admin/manageStaticPages.php");
	}
elseif(isDemoSite() AND ($editablefrm->isFormPOSTed($_POST,'addeditablemessage') OR $editablefrm->isFormPOSTed($_POST,'confirm_action')))
	{
		$editablefrm->setCommonErrorMsg($LANG['no_permission_for_demo_user']);
		$editablefrm->setPageBlockShow('block_msg_form_error');
	}
elseif($editablefrm->isFormPOSTed($_POST, 'addeditablemessage'))
	{
		$editablefrm->chkIsNotEmpty('page_title', $LANG['err_tip_compulsory'])and
			$editablefrm->chkIsTitleExists($CFG['db']['tbl']['static_pages'], $LANG['editable_messages_err_tip_title_exists']) ;
	    $editablefrm->chkIsNotEmpty('page_content', $LANG['err_tip_compulsory']);
		$editablefrm->chkIsNotEmpty('page_status', $LANG['err_tip_compulsory']);
		if($editablefrm->isValidFormInputs())
			{
				$editablefrm->setAllPageBlocksHide();
				$succesMsg = '';
				$editablefrm->setPageBlockShow('block_msg_form_success');
				$editablefrm->setPageBlockShow('form_add_messages');
				$editablefrm->setPageBlockShow('form_disp_messages');
				$page_content = html_entity_decode($editablefrm->getFormField('page_content'));
				#$page_content = strip_tags ($page_content, $CFG['html']['allowed_tags']);
				$editablefrm->setFormField('page_content', $page_content);
				$editablefrm->setFormField('seo_title', $editablefrm->getSeoTitleForStaticPage($editablefrm->getFormField('page_title'), $editablefrm->getFormField('id')));
				if(trim($editablefrm->getFormField('id')) == '0')
					{
						$inputArr = array('page_title', 'page_display_title', 'seo_title', 'page_content','page_status', 'link_in_footer');
						$currentEventId = $editablefrm->insertFormFieldsInTable($CFG['db']['tbl']['static_pages'], $inputArr);
						$succesMsg = $LANG['editable_messages_success_add_message'];
						$editablefrm->setCommonSuccessMsg($succesMsg);
					}
				else
					{
						$inputArr = array('page_title', 'page_display_title', 'seo_title', 'page_content','page_status', 'link_in_footer');
						$editablefrm->updateFormFieldsInTable($CFG['db']['tbl']['static_pages'], $inputArr);
						$succesMsg = $LANG['editable_messages_success_update_message'];
						$editablefrm->setCommonSuccessMsg($succesMsg);
					}
				$editablefrm->resetFieldsArray();
			}
		else
			{
				if(trim($editablefrm->getFormField('id')) != '0')
					$editablefrm->staticPageTitle = $LANG['editable_messages_page_edit_title'];
				$editablefrm->setAllPageBlocksHide();
				$editablefrm->setPageBlockShow('block_msg_form_error');
				$editablefrm->setCommonErrorMsg($LANG['common_err_tip_error']);
				$editablefrm->setPageBlockShow('form_disp_messages');
				$editablefrm->setPageBlockShow('form_add_messages');
			}
	}
elseif($editablefrm->isFormPOSTed($_POST, 'confirm_action'))
	{
		if($editablefrm->getFormField('action') == 1)
			$editablefrm->deleteMessages($CFG['db']['tbl']['static_pages'], $editablefrm->getFormField('delete'));
		Redirect2URL($CFG['site']['url']."admin/manageStaticPages.php?delete");
	}
elseif($editablefrm->isFormGETed($_GET,'id'))
	{
	    $editablefrm->staticPageTitle = $LANG['editable_messages_page_edit_title'];
	    $id = addslashes($editablefrm->getFormField('id'));
		$editablefrm->setFormField('id', $id);
		if($editablefrm->getEditableMessages($CFG['db']['tbl']['static_pages'], $editablefrm->getFormField('id')))
			{
				$editablefrm->setPageBlockShow('form_disp_messages');
				$editablefrm->setPageBlockShow('form_add_messages');
			}
		else
			{
				$editablefrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$editablefrm->setPageBlockShow('block_msg_form_error');
			}
	}
if($editablefrm->isFormGETed($_GET, 'delete'))
	{
		$editablefrm->setAllPageBlocksHide();
		$editablefrm->setPageBlockShow('block_msg_form_success');
		$editablefrm->setPageBlockShow('form_add_messages');
		$editablefrm->setPageBlockShow('form_disp_messages');
		$succesMsg = $LANG['editable_messages_success_delete_message'];
		$editablefrm->setCommonSuccessMsg($succesMsg);
	}
$editablefrm->buildSelectQuery();
$editablefrm->buildConditionQuery();
$editablefrm->buildSortQuery();
$editablefrm->buildQuery();
$editablefrm->executeQuery();
$editablefrm->setPageBlockShow('form_disp_messages');

if ($editablefrm->isShowPageBlock('form_add_messages'))
	{
		$editablefrm->form_add_messages['submitName']	=	$submitName = '';
		if($editablefrm->getFormField('id') != 0)
			$editablefrm->form_add_messages['submitName']	=	$submitName = $LANG['common_update'];
		else
			$editablefrm->form_add_messages['submitName']	=	$submitName = $LANG['common_add'];
	}
if ($editablefrm->isShowPageBlock('form_disp_messages'))
	{
		$editablefrm->form_disp_messages_arr['msgConfirmForm_hidden_arr']	=	array('start', 'orderby_field', 'orderby');
		if ($editablefrm->isResultsFound())
			{
				$i = 1;
				$inc=0;
				$editablefrm->form_disp_messages_arr['row']	=	 array();
				while($row = $editablefrm->fetchResultRecord())
					{
						$editablefrm->form_disp_messages_arr['row'][$inc]['record']	=	$row;
						$editablefrm->form_disp_messages_arr['row'][$inc]['dateWithTime']	=	$editablefrm->dateWithTime($row['page_added_date']);
						$editablefrm->form_disp_messages_arr['row'][$inc]['manageStaticPages_rul']	=	'manageStaticPages.php?id='.$row['page_id'];
						$i++;
						$inc++;
	 			    }
				$editablefrm->form_disp_messages['getMultiCheckBoxValue']	=	'if(getMultiCheckBoxValue(\'form_disp_editable_message\', \'checkall\', \''.$LANG['select_a_title'].'\')){Confirmation(\'selMsgConfirm\', \'msgConfirmform\', Array(\'delete1\', \'action\', \'confirmMessage\'), Array(multiCheckValue, document.form_disp_editable_message.to_do.value, \''.$editablefrm->LANG['editable_messages_confirm'].'\'), Array(\'value\', \'value\', \'innerHTML\', \'form_disp_editable_message\'));}';
				$smartyObj->assign('smarty_paging_list', $editablefrm->populatePageLinksGET($editablefrm->getFormField('start'),array('orderby_field','orderby')));
 			}
	}
//include the header file
$editablefrm->includeHeader();
//include the content of the page
setTemplateFolder('admin/');
$smartyObj->display('manageStaticPages.tpl');
//includ the footer of the page
?>
<script type="text/javascript" language="javascript">
<?php if ($editablefrm->getFormField('id') == 0) { ?>
	needLiveValidation('page_title',Array('Presence'));
<?php } ?>
	needLiveValidation('page_content',Array('Presence'));
</script>
<script language="javascript" type="text/javascript">
	var block_arr= new Array('selMsgConfirm');
	var replace_url = '<?php echo $CFG['site']['url'];?>login.php';
</script>
<?php
//<<<<<<--------------------Page block templates Ends--------------------//
$editablefrm->includeFooter();
?>