<?php
/**
 * File to create forum title
 *
 * Ther user can create a category
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Forums add title###
 * @author 		shankar_76ag08
 * @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
 */
/**
 * File having common configuration variables required for the entire project
 */
require_once('../../common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/'.$CFG['admin']['index']['forum_module'].'/admin/forumsAddTitle.php';
$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';

$CFG['mods']['include_files'][] = 'common/classes/class_ForumHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.php';
//compulsory
$CFG['site']['is_module_page']='forums';
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
/**
 * File to include the header file, database access file, session management file, help file and necessary functions
 */
require_once($CFG['site']['project_path'].'common/application_top.inc.php');
class ForumFormHandler extends AnovaHandler
	{
		//Variable to store group forum details
		public $forum_details_arr;

		/**
		 * To check the length of given url
		 *
		 * @param 		string $field_name form field name
		 * @param 		string $err_tip error tip
		 * @access public
		 * @return Boolean
		 **/
		public function chkLength($field_name, $err_tip='')
			{
				$url_len = strlen($this->fields_arr[$field_name]);
				if ($url_len < 3 || $url_len > 20)
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * To check if the forum id is valid
		 *
		 * @param 		string $table_name forums
		 * @param 		integer $forum_id
		 * @param 		string $err_tip error tip
		 * @access public
		 * @return Boolean
		 **/
		public function isValidForumId($forum_table, $forum_id, $err_tip='')
			{
				$sql = 'SELECT forum_id, forum_title, forum_description, publish_status, forum_status '.
						'FROM '.$forum_table.' '.
						'WHERE forum_id = '.$this->dbObj->Param($forum_id).' ';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($forum_id));
				if (!$rs)
					trigger_db_error($this->dbObj);

				if ($this->forum_details_arr = $rs->FetchRow())
					{
						$this->setFormField('forum_title', stripslashes($this->forum_details_arr['forum_title']));
						$this->setFormField('forum_status', $this->forum_details_arr['forum_status']);
						$this->setFormField('publish', $this->forum_details_arr['publish_status']);
						$this->setFormField('forum_description', stripslashes($this->forum_details_arr['forum_description']));
						return true;
				    }
				$this->setCommonErrorMsg($err_tip);
				return false;
			}

		/**
		 * To Check if the forum title exists
		 *
		 * @param 		string $forum_title forum title
		 * @param		integer $forum_id
		 * @param 		string $table_name forums
		 * @param 		string $err_tip error tip
		 * @access public
		 * @return boolean
		 **/
		public function chkIsNotExists($forum_title, $forum_id, $forum_table, $err_tip='')
			{
				$sql = 'SELECT COUNT(forum_id) AS count '.
						'FROM '.$forum_table.' '.
						'WHERE forum_title = '.$this->dbObj->Param($forum_title).' ';
				$fields_value_arr[] = $forum_title;
				if ($forum_id)
					{
						$sql .= ' AND forum_id != '.$this->dbObj->Param($forum_id).' ';
						$fields_value_arr[] = $forum_id;
					}

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

				$row = $rs->FetchRow();
				if ($row['count'])
					{
						$this->fields_err_tip_arr['forum_title'] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * To create a group
		 *
		 * @param 		string $table_name forums
		 * @param 		integer $forum_id
		 * @access public
		 * @return void
		 **/
		public function createForumTitle($forum_id)
			{
				$this->setFormField('seo_title', $this->getSeoTitleForForum($this->fields_arr['forum_title'], $this->fields_arr['forum_id']));
				if ($forum_id)
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['forums'].' SET '.
								'forum_title = '.$this->dbObj->Param($this->fields_arr['forum_title']).', '.
								'seo_title = '.$this->dbObj->Param($this->fields_arr['seo_title']).', '.
								'forum_description = '.$this->dbObj->Param($this->fields_arr['forum_description']).', '.
								'publish_status = '.$this->dbObj->Param($this->fields_arr['publish']).', '.
								'forum_status = '.$this->dbObj->Param($this->fields_arr['forum_status']).' '.
								'WHERE forum_id = '.$this->dbObj->Param($forum_id).'';
						$fields_value_arr = array($this->fields_arr['forum_title'], $this->fields_arr['seo_title'], $this->fields_arr['forum_description'],
												  $this->fields_arr['publish'],$this->fields_arr['forum_status'], $forum_id);
				    }
					else
						{
							$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['forums'].' SET '.
									'forum_title = '.$this->dbObj->Param($this->fields_arr['forum_title']).', '.
									'seo_title = '.$this->dbObj->Param($this->fields_arr['seo_title']).', '.
									'forum_description = '.$this->dbObj->Param($this->fields_arr['forum_description']).', '.
									'forum_userid = '.$this->dbObj->Param('forum_userid').', '.
									'forum_status = '.$this->dbObj->Param($this->fields_arr['forum_status']).', '.
									'publish_status = '.$this->dbObj->Param('publish').
									',date_added = now()';
							$fields_value_arr = array($this->fields_arr['forum_title'], $this->fields_arr['seo_title'],
													  $this->fields_arr['forum_description'],
													  $this->CFG['user']['user_id'],
													  $this->fields_arr['forum_status'],
													  $this->fields_arr['publish']
													  );
						}

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

				return true;
			}

		/**
		 * To set error to the form field
		 *
		 * @param string $field_name
		 * @param string $err_tip
		 * @access public
		 * @return void
		 **/
		public function setError($field_name, $err_tip='')
			{
				$this->fields_err_tip_arr[$field_name] = $err_tip;
			}
	}
//<<<<<-------------- Class ForumFormHandler begins ---------------//
//-------------------- Code begins -------------->>>>>//
$forum = new ForumFormHandler();
$forum->left_navigation_div = 'forumsForums';
$forum->setPageBlockNames(array('form_create_forum'));
//default form fields and values...
$forum->setFormField('forum_id', '');
$forum->setFormField('forum_title', '');
$forum->setFormField('seo_title', '');
$forum->setFormField('forum_status', '');
$forum->setFormField('forum_description', '');
$forum->setFormField('publish', 'Yes');
$forum->setAllPageBlocksHide();
if ($forum->isFormPOSTed($_POST, 'forum_submit'))
	{
		$forum->sanitizeFormInputs($_POST);
		//validations...
		$forum->chkIsNotEmpty('forum_title', $LANG['err_tip_compulsory'])and
			$forum->chkIsNotExists($forum->getFormField('forum_title'), $forum->getFormField('forum_id'), $CFG['db']['tbl']['forums'], $LANG['forum_err_tip_forum_title_exists']);
		$forum->chkIsNotEmpty('forum_status', $LANG['err_tip_compulsory']);
		$forum->chkIsNotEmpty('forum_description', $LANG['err_tip_compulsory']);
		$forum->getFormField('forum_id')and
			$forum->chkIsNotEmpty('forum_id', $LANG['err_tip_compulsory'])and
				$forum->chkIsNumeric('forum_id', $LANG['err_tip_compulsory']);
		$forum->isValidFormInputs()and
			$forum->createForumTitle($forum->getFormField('forum_id'));
		$forum->setAllPageBlocksHide();
		if ($forum->isValidFormInputs())
			{
				$forum->setPageBlockShow('block_msg_form_success');
				//now redirect to members page...
				if($forum->getFormField('forum_id'))
					Redirect2URL('forums.php?msg=edit_success');
				else
					Redirect2URL('forums.php?msg=add_success');
			}
		else //error in form inputs
			{
				$forum->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$forum->setPageBlockShow('block_msg_form_error');
			}
	}
elseif ($forum->isFormPOSTed($_POST, 'forum_cancel'))
 	{
		//now redirect to members page...
		Redirect2URL('forums.php');
 	}
elseif ($forum->isFormGETed($_GET, 'forum_id'))
	{
		$forum->sanitizeFormInputs($_GET);
		$forum->chkIsNotEmpty('forum_id', $LANG['err_tip_compulsory'])and
			$forum->chkIsNumeric('forum_id', $LANG['err_tip_compulsory'])and
				$forum->isValidForumId($CFG['db']['tbl']['forums'], $forum->getFormField('forum_id'), $LANG['forums_err_tip_invalid_forum_id']);
		if (!$forum->isValidFormInputs())
			{
				$forum->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$forum->setPageBlockShow('block_msg_form_error');
			}
	}
if ($forum->isFormGETed($_GET, 'forum_id'))
	{
		$forum->LANG['forumslinks_add_title'] = $LANG['forumslinks_edit_title'];
	}
$forum->setPageBlockShow('form_create_forum'); //default page block. show it. All others hidden
if ($forum->isShowPageBlock('form_create_forum'))
	{
	    $forum->form_create_forum['forum_status_arr'] = array("Yes" => $LANG['common_display_active'], "No" => $LANG['common_display_inactive']);
	}
//<<<<<-------------------- Code ends----------------------//
//-------------------- Page block templates begins -------------------->>>>>//
//include the header file
$forum->includeHeader();
//include the content of the page
setTemplateFolder('admin/', $CFG['admin']['index']['forum_module']);
$smartyObj->display('forumsAddTitle.tpl');
//<<<<<-------------------- Page block templates ends -------------------//
$forum->includeFooter();
?>