<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board v1.2
|   ========================================
|   by Matthew Mecham
|   (c) 2001 - 2003 Invision Power Services
|   http://www.invisionpower.com
|   ========================================
|   Web: http://www.invisionboard.com
|   Email: matt@invisionpower.com
|   Licence Info: http://www.invisionboard.com/?license
+---------------------------------------------------------------------------
|
|   > Help Control functions
|   > Module written by Matt Mecham
|   > Date started: 2nd April 2002
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/




$idx = new ad_settings();


class ad_settings {

	var $base_url;

	function ad_settings() {
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		//---------------------------------------
		// Kill globals - globals bad, Homer good.
		//---------------------------------------
		
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( $tmp_in as $k => $v )
		{
			unset($$k);
		}
		
		//---------------------------------------

		switch($IN['code'])
		{
			case 'edit':
				$this->show_form('edit');
				break;
			case 'new':
				$this->show_form('new');
				break;
			
			case 'doedit':
				$this->doedit();
				break;
				
			case 'donew':
				$this->doadd();
				break;
				
			case 'remove':
				$this->remove();
				break;
			
			//-------------------------
			default:
				$this->list_files();
				break;
		}
		
	}
	
	//-------------------------------------------------------------
	// HELP FILE FUNCTIONS
	//-------------------------------------------------------------
	
	function doedit()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須輸入一個有效的表情圖示 id，笨蛋！");
		}
		
		$text  = preg_replace( "/\n/", "<br>", $HTTP_POST_VARS['text']  ); /* 中文修正 */
		$title = preg_replace( "/\n/", "<br>", $HTTP_POST_VARS['title']  ); /* 中文修正 */
		$desc  = preg_replace( "/\n/", "<br>", $HTTP_POST_VARS['description']  ); /* 中文修正 */
		
//		$text  = preg_replace( "/\\\/", "&#092;", $text ); /* 中文修正 */
		
		$db_string = $DB->compile_db_update_string( array( 'title'       => $title,
														   'text'        => $text,
														   'description' => $desc,
												  )      );
												  
		$DB->query("UPDATE ibf_faq SET $db_string WHERE id='".$IN['id']."'");
		
		$ADMIN->save_log("編輯說明檔案");
		
		$std->boink_it($SKIN->base_url."&act=help");
		exit();
			
		
	}
	
	//=====================================================
	
	
	function show_form($type='new')
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
	
		$ADMIN->page_detail = "您可以在下面增加/編輯及移除說明檔案";
		$ADMIN->page_title  = "說明檔案管理";
		
		//+-------------------------------
		
		if ($type != 'new')
		{
		
			if ($IN['id'] == "")
			{
				$ADMIN->error("您必須輸入一個有效的說明檔案 id，笨蛋！");
			}
		
			//+-------------------------------
		
			$DB->query("SELECT * FROM ibf_faq WHERE id='".$IN['id']."'");
		
			if ( ! $r = $DB->fetch_row() )
			{
				$ADMIN->error("無法在資料庫中找到該說明檔案");
			}
		
			//+-------------------------------
			
			$button = '編輯此說明檔案';
			$code   = 'doedit';
		}
		else
		{
			$r = array();
			$button = '增加此說明檔案';
			$code   = 'donew';
		}
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , $code ),
												  2 => array( 'act'   , 'help'     ),
												  3 => array( 'id'    , $IN['id'] ),
									     )      );
		
		
		
		$SKIN->td_header[] = array( "&nbsp;"  , "20%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "80%" );
		
		$r['text'] = preg_replace( "/<br>/i", "\n", $r['text'] ); /* 中文修正 */
 		
 		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( $button );
		
		
		$ADMIN->html .= $SKIN->add_td_row( array( "說明檔案標題",
												  $SKIN->form_input('title'  , $r['title'] ), /* 中文修正 */
										 )      );
										 
		$ADMIN->html .= $SKIN->add_td_row( array( "說明檔案描述",
												  $SKIN->form_textarea('description', $r['description'] ), /* 中文修正 */
										 )      );
										 
		$ADMIN->html .= $SKIN->add_td_row( array( "說明檔案內容",
												  $SKIN->form_textarea('text', $r['text'], "60", "10" ),
										 )      );
										 
		$ADMIN->html .= $SKIN->end_form($button);
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
	
	}
	
	//=====================================================
	
	function remove()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("您必須輸入一個有效的說明檔案 id，笨蛋！");
		}
		
		$DB->query("DELETE FROM ibf_faq WHERE id='".$IN['id']."'");
		
		$ADMIN->save_log("移除一個說明檔案");
		
		$std->boink_it($SKIN->base_url."&act=help");
		exit();
			
		
	}
	
	//=====================================================
	
	function doadd()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['title'] == "")
		{
			$ADMIN->error("您必須輸入一個名稱，笨蛋！");
		}
		
		
		
		$text  = preg_replace( "/\n/", "<br>", $HTTP_POST_VARS['text']  ); /* 中文修正 */
		$title = preg_replace( "/\n/", "<br>", $HTTP_POST_VARS['title']  ); /* 中文修正 */
		$desc  = preg_replace( "/\n/", "<br>", $HTTP_POST_VARS['description']  ); /* 中文修正 */
		
//		$text  = preg_replace( "/\\\/", "&#092;", $text ); /* 中文修正 */
		
		$db_string = $DB->compile_db_insert_string( array( 'title'       => $title,
														   'text'        => $text,
														   'description' => $desc,
												  )      );
												  
		$DB->query("INSERT INTO ibf_faq (".$db_string['FIELD_NAMES'].") VALUES(".$db_string['FIELD_VALUES'].")");
		
		$ADMIN->save_log("增加說明檔案");
		
		$std->boink_it($SKIN->base_url."&act=help");
		exit();
			
		
	}
	
	//=====================================================
	
	function list_files()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
	
		$ADMIN->page_detail = "您可以在下面增加/編輯及移除說明檔案";
		$ADMIN->page_title  = "說明檔案管理";
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "標題"  , "50%" );
		$SKIN->td_header[] = array( "編輯"   , "30%" );
		$SKIN->td_header[] = array( "移除" , "20%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "目前的說明檔案" );
		
		$DB->query("SELECT * from ibf_faq ORDER BY id ASC");
		
		if ( $DB->get_num_rows() )
		{
			while ( $r = $DB->fetch_row() )
			{
				
				$ADMIN->html .= $SKIN->add_td_row( array( "<b>".$r['title']."</b><br>".$r['description'], /* 中文修正 */
														  "<center><a href='".$SKIN->base_url."&act=help&code=edit&id={$r['id']}'>編輯</a></center>",
														  "<center><a href='".$SKIN->base_url."&act=help&code=remove&id={$r['id']}'>移除</a></center>",
												 )      );
												   
			
				
			}
		}
		
		$ADMIN->html .= $SKIN->add_td_basic("<a href='".$SKIN->base_url."&act=help&code=new'>新增說明檔案</a>", "center", "title" );
		
		$ADMIN->html .= $SKIN->end_table();
		
		//+-------------------------------
		
		$ADMIN->output();
	
	}
	
	
}


?>