<?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
+---------------------------------------------------------------------------
|
|   > Custom profile field functions
|   > Module written by Matt Mecham
|   > Date started: 24th June 2002
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/




$idx = new ad_fields();


class ad_fields {

	var $base_url;

	function ad_fields() {
		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 'add':
				$this->main_form('add');
				break;
				
			case 'doadd':
				$this->main_save('add');
				break;
				
			case 'edit':
				$this->main_form('edit');
				break;
				
			case 'doedit':
				$this->main_save('edit');
				break;
				
			case 'delete':
				$this->delete_form();
				break;
				
			case 'dodelete':
				$this->do_delete();
				break;
						
			default:
				$this->main_screen();
				break;
		}
		
	}
	
	
	
	//+---------------------------------------------------------------------------------
	//
	// Delete a group
	//
	//+---------------------------------------------------------------------------------
	
	function delete_form()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("无法解析群组 ID，请重试");
		}
		
		$ADMIN->page_title = "删除自订个人资料栏位";
		
		$ADMIN->page_detail = "请检查确定您正尝试移除正确的自订个人资料栏位，<b>所有资料将会丢失！</b>.";
		
		
		//+-------------------------------
		
		$DB->query("SELECT ftitle, fid FROM ibf_pfields_data WHERE fid='".$IN['id']."'");
		
		if ( ! $field = $DB->fetch_row() )
		{
			$ADMIN->error("无法从资料库取得此栏位");
		}
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , 'dodelete'  ),
												  2 => array( 'act'   , 'field'     ),
												  3 => array( 'id'    , $IN['id']   ),
									     )      );
									     
		
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "移除确认" );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>要移除的自订个人资料栏位</b>" ,
												  "<b>".$field['ftitle']."</b>",
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form("删除这个自订栏位");
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
			
			
	}
	
	function do_delete()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("无法解析栏位 ID，请重试");
		}
		
		
		// Check to make sure that the relevant groups exist.
		
		$DB->query("SELECT ftitle, fid FROM ibf_pfields_data WHERE fid='".$IN['id']."'");
		
		if ( ! $row = $DB->fetch_row() )
		{
			$ADMIN->error("无法解析要删除的 ID");
		}
		
		$DB->query("ALTER TABLE ibf_pfields_content DROP field_{$row['fid']}");
		
		$DB->query("DELETE FROM ibf_pfields_data WHERE fid='".$IN['id']."'");
		
		$ADMIN->done_screen("已移除个人资料栏位", "自订个人资料栏位控制", "act=field" );
		
	}
	
	
	//+---------------------------------------------------------------------------------
	//
	// Save changes to DB
	//
	//+---------------------------------------------------------------------------------
	
	function main_save($type='edit')
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		if ($IN['ftitle'] == "")
		{
			$ADMIN->error("您必须输入一个栏位名称");
		}
		
		if ($type == 'edit')
		{
			if ($IN['id'] == "")
			{
				$ADMIN->error("无法解析栏位 id");
			}
			
		}
		
		$content = "";
		
		if ($HTTP_POST_VARS['fcontent'] != "")
		{
			$content = str_replace( "\n", '|', str_replace( "\n\n", "\n", trim($HTTP_POST_VARS['fcontent']) ) );
		}
		
		$db_string = array( 'ftitle'    => $IN['ftitle'],
						    'fdesc'     => $IN['fdesc'],
						    'fcontent'  => stripslashes($content),
						    'ftype'     => $IN['ftype'],
						    'freq'      => $IN['freq'],
						    'fhide'     => $IN['fhide'],
						    'fmaxinput' => $IN['fmaxinput'],
						    'fedit'     => $IN['fedit'],
						    'forder'    => $IN['forder'],
						    'fshowreg'  => $IN['fshowreg'],
						  );
		
						  
		if ($type == 'edit')
		{
			$rstring = $DB->compile_db_update_string( $db_string );
			
			$DB->query("UPDATE ibf_pfields_data SET $rstring WHERE fid='".$IN['id']."'");
			
			$ADMIN->done_screen("已删除个人资料栏位", "自订个人资料栏位控制", "act=field" );
			
		}
		else
		{
			$rstring = $DB->compile_db_insert_string( $db_string );
			
			$DB->query("INSERT INTO ibf_pfields_data (" .$rstring['FIELD_NAMES']. ") VALUES (". $rstring['FIELD_VALUES'] .")");
			
			$new_id = $DB->get_insert_id();
			
			$DB->query("ALTER TABLE ibf_pfields_content ADD field_$new_id text default ''");
			
			$DB->query("OPTIMIZE TABLE ibf_pfields_content");
			
			$ADMIN->done_screen("已增加个人资料栏位", "自订个人资料栏位控制", "act=field" );
		}
	}
	
	
	//+---------------------------------------------------------------------------------
	//
	// Add / edit group
	//
	//+---------------------------------------------------------------------------------
	
	function main_form($type='edit')
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($type == 'edit')
		{
			if ($IN['id'] == "")
			{
				$ADMIN->error("资料库中没有可选的群组 id，请重试");
			}
			
			$form_code = 'doedit';
			$button    = '完成编辑';
				
		}
		else
		{
			$form_code = 'doadd';
			$button    = '增加栏位';
		}
		
		if ($IN['id'] != "")
		{
			$DB->query("SELECT * FROM ibf_pfields_data WHERE fid='".$IN['id']."'");
			$fields = $DB->fetch_row();
		}
		else
		{
			$fields = array();
		}
		
		if ($type == 'edit')
		{
			$ADMIN->page_title = "编辑个人资料栏位 ".$fields['ftitle'];
		}
		else
		{
			$ADMIN->page_title = '新增个人资料栏位';
			$fields['ftitle'] = '';
		}
		
		$ADMIN->page_detail = "请在送出表单之前仔细检查资讯";
		
		
		
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , $form_code  ),
												  2 => array( 'act'   , 'field'     ),
												  3 => array( 'id'    , $IN['id']   ),
									     )  );
									     
		$fields['fcontent'] = str_replace( '|', "\n", $fields['fcontent'] );
		
		//+-------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "栏位设定" );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>栏位名称</b><br>字元上限： 200" ,
												  $SKIN->form_input("ftitle", $fields['ftitle'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>描述</b><br>字元上限： 250<br>可以用来提示 隐藏/必要 状态" ,
												  $SKIN->form_input("fdesc", $fields['fdesc'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>栏位形态</b>" ,
												  $SKIN->form_dropdown("ftype",
												  					   array(
												  					   			0 => array( 'text' , '文字输入' ),
												  					   			1 => array( 'drop' , '下拉选单' ),
												  					   			2 => array( 'area' , '文字框' ),
												  					   		),
												  					   $fields['ftype'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>输入字元上限 (文字输入及文字框)</b>" ,
												  $SKIN->form_input("fmaxinput", $fields['fmaxinput'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>显示顺序 (当编辑及显示时) 数字 1 最低" ,
												  $SKIN->form_input("forder", $fields['forder'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>选项内容 (下拉选单)</b><br>设定时，每项一列<br>例如 '性别' 栏位：<br>m=男性<br>f=女性<br>u=保密<br>会产生：<br><select name='pants'><option value='m'>男性</option><option value='f'>女性</option><option value='u'>保密</option></select><br>m,f or u 储存于资料库，当在个人资料中显示栏位时，会使用配对的值 (f=女性，显示 '女性')" ,
												  $SKIN->form_textarea("fcontent", $fields['fcontent'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>包含在注册页面？</b>" ,
												  $SKIN->form_yes_no("fshowreg", $fields["fshowreg"] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>栏位必须完成不能留空？</b><br>(若您在底下选择隐藏则不适用)" ,
												  $SKIN->form_yes_no("freq", $fields['freq'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>从会员的个人资料中隐藏？</b><br>如果是，只有管理员及超级板主可以看到，使用者仍然可以编辑。" ,
												  $SKIN->form_yes_no("fhide", $fields['fhide'] )
									     )      );						     							     
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>栏位可被会员编辑？</b><br>如果否，使用者无法编辑资讯，栏位只能被管理员及超级板主看到，管理员可以经由 ACP 编辑资讯。" ,
												  $SKIN->form_yes_no("fedit", $fields['fedit'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form($button);
										 
		$ADMIN->html .= $SKIN->end_table();
		
		$ADMIN->output();
			
			
	}

	//+---------------------------------------------------------------------------------
	//
	// Show "Management Screen
	//
	//+---------------------------------------------------------------------------------
	
	function main_screen()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		$ADMIN->page_title = "自订个人资料栏位";
		
		$ADMIN->page_detail = "自订个人资料栏位可用于注册或编辑个人资料时增加可选或必须完成的栏位，这对于您想记录论坛没有提供的会员资料非常有用。";
		
		$ADMIN->page_detail .= "<br /><br /><strong>在主题检视中使用自订个人资料栏位</strong><br /><br />
								当您将此功能打开之后 (经由系统设定 -> CPU 节省)，您可以在主题检视面板使用自定栏位，
								<br />只要在 '发表条目' 中您想要让它显示的地方加上
								<strong>\$author[field_1]</strong> (或任何对应 '主题检视变数' 者) 即可。";
		
		$SKIN->td_header[] = array( "栏位名称"    , "20%" );
		$SKIN->td_header[] = array( "形态"           , "10%" );
		$SKIN->td_header[] = array( "主题检视变数" , "20%" );
		$SKIN->td_header[] = array( "必要"       , "10%" );
		$SKIN->td_header[] = array( "隐藏"         , "10%" );
		$SKIN->td_header[] = array( "注册显示"       , "10%" );
		$SKIN->td_header[] = array( "编辑"           , "10%" );
		$SKIN->td_header[] = array( "删除"         , "10%" );
		
		//+-------------------------------
		
		$ADMIN->html .= $SKIN->start_table( "自订个人资料栏位管理" );
		
		$real_types = array( 'drop' => '下拉选单',
							 'area' => '文字框',
							 'text' => '文字输入',
						   );
		
		$DB->query("SELECT * FROM ibf_pfields_data");
		
		if ( $DB->get_num_rows() )
		{
			while ( $r = $DB->fetch_row() )
			{
			
				$hide   = '&nbsp;';
				$req    = '&nbsp;';
				$regi   = '&nbsp;';
				
				"<center><a href='{$ADMIN->base_url}&act=group&code=delete&id=".$r['g_id']."'>删除</a></center>";
				
				//-----------------------------------
				if ($r['fhide'] == 1)
				{
					$hide = '<center><span style="color:red">Y</span></center>';
				}
				//-----------------------------------
				if ($r['freq'] == 1)
				{
					$req = '<center><span style="color:red">Y</span></center>';
				}
				
				if ($r['fshowreg'] == 1)
				{
					$regi = '<center><span style="color:red">Y</span></center>';
				}
				
				
				$ADMIN->html .= $SKIN->add_td_row( array( "<b>{$r['ftitle']}</b>" ,
														  "<center>{$real_types[$r['ftype']]}</center>",
														  "<center>field_".$r['fid']."</center>",
														  $req,
														  $hide,
														  $regi,
														  "<center><a href='{$ADMIN->base_url}&act=field&code=edit&id=".$r['fid']."'>编辑</a></center>",
														  "<center><a href='{$ADMIN->base_url}&act=field&code=delete&id=".$r['fid']."'>删除</a></center>",
											 )      );
											 
			}
		}
		else
		{
			$ADMIN->html .= $SKIN->add_td_basic("未发现", "center", "pformstrip");
		}
		
		$ADMIN->html .= $SKIN->add_td_basic("<a href='{$ADMIN->base_url}&act=field&code=add' class='fauxbutton'>新增栏位</a></center>", "center", "pformstrip");

		$ADMIN->html .= $SKIN->end_table();
		
		
		$ADMIN->output();
		
		
	}
	
		
}


?>