<?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
+---------------------------------------------------------------------------
|
|   > Topic Multi-Moderation
|   > Module written by Matt Mecham
|   > Date started: 14th May 2003
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/


// Ensure we've not accessed this script directly:
// ...or not



$idx = new ad_multimod();


class ad_multimod
{

	var $base_url;

	function ad_multimod()
	{
		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);
		}
		
		$ADMIN->nav[] = array( 'act=multimod', '主題複合管理首頁' );
		
		switch($IN['code'])
		{
		
			case 'list':
				$this->list_current();
				break;
				
			case 'new':
				$this->do_form('new');
				break;
				
			case 'edit':
				$this->do_form('edit');
				break;
				
			case 'donew':
				$this->do_save('new');
				break;
				
			case 'doedit':
				$this->do_save('edit');
				break;
				
			case 'delete':
				$this->do_delete();
				break;
				
			//-------------------------
			
			default:
				$this->list_current();
				break;
		}
		
	}
	
	//-------------------------------------------------------------
	// DELETE!
	//-------------------------------------------------------------
	
	function do_delete()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
		
		if ($IN['id'] == "")
		{
			$ADMIN->error("無法解析 MMOD ID，請重試。");
		}
		
		$DB->query("DELETE FROM ibf_topic_mmod WHERE mm_id='".$IN['id']."'");
		
		$mm_id = intval($IN['id']);
		
		//------------------------------------
		// Remove the MMOD from relevant forums
		//------------------------------------
		
		$final   = array();
		
		$DB->query("SELECT id,name,topic_mm_id FROM ibf_forums WHERE topic_mm_id LIKE '%,$mm_id,%'");
		
		while( $r = $DB->fetch_row() )
		{
			$final[ $r['id'] ] = str_replace( ",$mm_id,", ",", $r['topic_mm_id'] );
		}
		
		// now lets update the affected forum_id's.
		
		if ( count( $final ) > 0 )
		{
			foreach( $final as $fid => $raw_mm_id )
			{
				$new_mm_id = "," . $std->clean_perm_string($raw_mm_id) . ",";
				
				if ( $new_mm_id == ',,' )
				{
					$new_mm_id = '';
				}
				
				$DB->query("UPDATE ibf_forums SET topic_mm_id='{$new_mm_id}' WHERE id=$fid");
			}
		}
		
		$ADMIN->save_log("已移除主題複合管理");
		
		$std->boink_it($SKIN->base_url."&act=multimod");
		
	}
	
	//-------------------------------------------------------------
	// SAVE!
	//-------------------------------------------------------------
	
	function do_save($type='new')
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
		
		$forums = array();
		
		$IN['id'] = intval($IN['id']);
		
		if ( $type == 'edit' )
		{
			if ( $IN['id'] < 1 )
			{
				$ADMIN->error("你必須使用有效的 ID");
			}
		}
		
		if ( $IN['mm_title'] == "" )
		{
			$ADMIN->error("您必須輸入有效的名稱");
		}
		
		//----------------------------------------
		// Check for forums...
		//----------------------------------------
		
		$forums = $this->get_activein_forums();
		
		if ( count($forums) < 1 )
		{
			$ADMIN->error("您必須選擇某些討論區來啟用此複合管理組件");
		}
		
		if ( $IN['topic_move'] == 'n' )
		{
			$ADMIN->error("在主題複合管理的 '搬移到' 中所選擇的討論區不正確。請注意，您不能選擇將主題搬移到類別中。");
		}
			
		$save = array(
						'mm_title'              => $IN['mm_title'],
						'mm_enabled'            => 1,
						'topic_state'           => $IN['topic_state'],
						'topic_pin'	            => $IN['topic_pin'],
						'topic_move'            => $IN['topic_move'],
						'topic_move_link'       => $IN['topic_move_link'],
						'topic_title_st'        => $ADMIN->make_safe($HTTP_POST_VARS['topic_title_st']),
						'topic_title_end'       => $ADMIN->make_safe($HTTP_POST_VARS['topic_title_end']),
						'topic_reply'           => $IN['topic_reply'],
						'topic_reply_content'   => $ADMIN->make_safe($HTTP_POST_VARS['topic_reply_content']),
						'topic_reply_postcount' => $IN['topic_reply_postcount'],
					 );
					 
		if ( $type == 'edit' )
		{
			$str = $DB->compile_db_update_string( $save );
			
			$mm_id = $IN['id'];
			
			$DB->query("UPDATE ibf_topic_mmod SET $str WHERE mm_id=$mm_id");
			
		}
		else
		{
			$str = $DB->compile_db_insert_string( $save );
			
			$DB->query("INSERT INTO ibf_topic_mmod ({$str['FIELD_NAMES']}) VALUES({$str['FIELD_VALUES']})");
			
			$mm_id = $DB->get_insert_id();
		}
		
		//------------------------------------
		// Sort out the forum id things
		//------------------------------------
		
		$final   = array();
		$fstring = ','.implode( ",", $forums ).',';
		
		$DB->query("SELECT id,name,topic_mm_id FROM ibf_forums");
		
		while( $r = $DB->fetch_row() )
		{
			if ( $r['topic_mm_id'] == "" )
			{
				// Is this forum_id one of the forums to activate?
				
				if ( stristr( $fstring, ','.$r['id'].',' ) )
				{
					$final[ $r['id'] ] = $mm_id;
				}
				else
				{
					continue;
				}
			}
			
			// Is this mm_id already active?
			
			else if ( stristr( $r['topic_mm_id'], ','.$mm_id.',' ) )
			{
				// Should it still be?
				
				if ( ! stristr( $fstring, ','.$r['id'].',' ) )
				{
					// No? Remove it at once!
					
					$final[ $r['id'] ] = str_replace( ",$mm_id,", ",", $r['topic_mm_id'] );
				}
					// No else, there's no need to update this forum_id's mm_id's
			}
			else if ( stristr( $fstring, ','.$r['id'].',' ) )
			{
				// forums' topic_mm_id is not blank and mm_id is not in that topic_mm_id, so...
				
				$final[ $r['id'] ] = $r['topic_mm_id'] . ','.$mm_id.',';
			}
		}
		
		// Phew - ok, now lets update the affected forum_id's.
		
		if ( count( $final ) > 0 )
		{
			foreach( $final as $fid => $raw_mm_id )
			{
				$new_mm_id = "," . $std->clean_perm_string($raw_mm_id) . ",";
				
				$DB->query("UPDATE ibf_forums SET topic_mm_id='{$new_mm_id}' WHERE id=$fid");
			}
		}
		
		$ADMIN->save_log("更新主題複合管理條目 ($type)");
		
		$std->boink_it($ADMIN->base_url."&act=multimod");
		
		
	}
	
	//-------------------------------------------------------------
	// SHOW MM FORM
	//-------------------------------------------------------------
	
	function do_form($type='new')
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
	
		$ADMIN->page_detail = "複合管理可以讓您結合多個管理動作將數個管理選項建立成一個易於使用的捷徑。";
		$ADMIN->page_title  = "主題複合管理";
		
		$form_code   = 'donew';
		$description = '新增主題複合管理';
		$button      = "新增複合管理";
		if ( $type == 'edit' )
		{
			$id = intval($IN['id']);
			
			$DB->query("SELECT * FROM ibf_topic_mmod WHERE mm_id=$id");
			
			if ( ! $topic_mm = $DB->fetch_row() )
			{
				$ADMIN->error("無法取得該資訊 ($id)");
			}
			
			$form_code   = 'doedit';
			$description = '編輯主題複合管理';
			$button      = "編輯複合管理";
		}
		
		//---------------------------------------------------------
		
		$ADMIN->html .= $SKIN->start_form( array( 1 => array( 'code'  , $form_code ),
												  2 => array( 'act'   , 'multimod' ),
												  3 => array( 'id'    , $id        ),
									     )      );
		
		//---------------------------------------------------------
		
		$state_dd = array(
						  0 => array( 'leave', '保留' ),
						  1 => array( 'close', '關閉' ),
						  2 => array( 'open' , '開放'  ),
					   );
					  
		$pin_dd   = array(
						  0 => array( 'leave', '保留' ),
						  1 => array( 'pin'  , '固定'   ),
						  2 => array( 'unpin', '解固' ),
					    );
					  
		//---------------------------------------------------------
		
		$db_cats     = array();
		$db_children = array();
		$db_forums   = array();
		$last_cat_id = -1;
		
		$DB->query("SELECT f.id as forum_id, f.name as forum_name, f.position, f.read_perms, f.parent_id, f.subwrap, f.sub_can_post, f.redirect_on, f.topic_mm_id, c.id as cat_id, c.name as cat_name from ibf_forums f, ibf_categories c where c.id=f.category ORDER BY c.position, f.position");
		
		$forums     = "<select name='forums[]' class='textinput' size='15' multiple='multiple'>\n";
		
		$forum_jump = array();
		
		while ( $r = $DB->fetch_row() )
		{
			if ($last_cat_id != $r['cat_id'])
        	{
        		$db_cats[ $r['cat_id'] ] = array( 'id'   => $r['cat_id'],
        										  'name' => $r['cat_name'],
        										);
        										   
        		$last_cat_id = $r['cat_id'];
        	}
        	
        	if ($r['parent_id'] > 0)
			{
				$db_children[ $r['parent_id'] ][$r['forum_id']] = $r;
			}
			else
			{
				$db_forums[ $r['forum_id'] ] = $r;
			}
			
		}
				
		
		$last_cat_id = -1;
			
		foreach ( $db_forums as $an_eye_for_an => $i )
		{
			
			if ( stristr( $i['topic_mm_id'], ",".$id."," ) )
			{
				$selected = ' selected="selected"';
			}
			else
			{
				$selected = "";
			}
			
			if ( $i['redirect_on'] == 1 )
			{
				continue;
			}
			
			if ($last_cat_id != $i['cat_id'])
        	{
        		$forums     .= "<option value=\"c_{$i['cat_id']}\" style='font-weight:bold'>{$db_cats[ $i['cat_id'] ]['name']}</option>\n";
        		$forum_jump[] = array( 'n', $db_cats[ $i['cat_id'] ]['name'] );
        		$last_cat_id = $i['cat_id'];
        	}
			
			$forums      .= "<option value=\"{$i['forum_id']}\" $selected>&middot;&middot;&nbsp;{$i['forum_name']}</option>\n";
			$forum_jump[] = array( $i['forum_id'], "&middot;&middot;&nbsp;{$i['forum_name']}" );
			
			// Add in sub cat children
			
			if ( ($i['subwrap'] == 1) and (count($db_children[ $i['forum_id'] ]) > 0) )
			{
				
				foreach( $db_children[ $i['forum_id'] ] as $idx => $cdata )
				{
					if ( $cdata['redirect_on'] == 1 OR ( $cdata['subwrap'] and ( ! $cdata['sub_can_post'] ) ) )
					{
						continue;
					}
					
					if ( stristr( $cdata['topic_mm_id'], ",".$id."," ) )
					{
						$selected = ' selected="selected"';
					}
					else
					{
						$selected = "";
					}
					
					$forums      .= "<option value=\"{$cdata['forum_id']}\" $selected>&middot;&middot;&middot;&middot;&nbsp;{$cdata['forum_name']}</option>\n";
					
					$forum_jump[] = array( $cdata['forum_id'], "&middot;&middot;&middot;&middot;&nbsp;{$cdata['forum_name']}" );
				}
			}
		}
		
		$forums .= "</select>";
		
		//---------------------------------------------------------
		
		$SKIN->td_header[] = array( "&nbsp;"   , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"   , "60%" );

		$ADMIN->html .= $SKIN->start_table( "主題複合管理", $description );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>此複合管理組件的名稱？</b>" ,
												  $SKIN->form_input("mm_title", $topic_mm['mm_title'] )
									     )      );
		
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>要啟用的討論區...</b><br>您可以選擇一個以上" ,
												  $forums
									     )      );							     
		
		$ADMIN->html .= $SKIN->add_td_basic( '管理選項', 'left', 'pformstrip' );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>附加主題標題 <i>開頭</i>？</b>" ,
												  $SKIN->form_input("topic_title_st", $topic_mm['topic_title_st'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>附加主題標題 <i>結尾</i>？</b>" ,
												  $SKIN->form_input("topic_title_end", $topic_mm['topic_title_end'] )
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>改變主題狀態？</b>" ,
												  $SKIN->form_dropdown("topic_state", $state_dd, $topic_mm['topic_state'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>改變固定狀態？</b>" ,
												  $SKIN->form_dropdown("topic_pin", $pin_dd, $topic_mm['topic_pin'] )
									     )      );
									     
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>搬移主題？</b>" ,
					    						  $SKIN->form_dropdown("topic_move", array_merge( array( 0 => array('-1', '不要搬移' ) ), $forum_jump ), $topic_mm['topic_move'] )
					    						  ."<br />".$SKIN->form_checkbox('topic_move_link', $topic_mm['topic_move_link'] )."<strong>留下指向原始主題的連結？</strong>"
									     )      );
		
		$ADMIN->html .= $SKIN->add_td_basic( '發表選項', 'left', 'pformstrip' );
	
		
		$ADMIN->html .= $SKIN->add_td_row( array( "<b>加入回覆到主題中？</b><br>HTML 啟用" ,
												  "啟用此回覆？ &nbsp;".$SKIN->form_yes_no('topic_reply', $topic_mm['topic_reply'] )
												  ."<br />"
												  . $SKIN->form_textarea("topic_reply_content", $topic_mm['topic_reply_content'] )
												  ."<br />".$SKIN->form_checkbox('topic_reply_postcount', $topic_mm['topic_reply_postcount'] )."<strong>增加發表者的文章計數？</strong>"
									     )      );
									     
		$ADMIN->html .= $SKIN->end_form($button);
		
		$ADMIN->html .= $SKIN->end_table();
		
		
		
		$ADMIN->output();
	
	}
	
	
	//-------------------------------------------------------------
	// SHOW ALL AVAILABLE MM's
	//-------------------------------------------------------------
	
	function list_current()
	{
		global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
	
		$ADMIN->page_detail = "複合管理可以讓您結合多個管理動作將數個管理選項建立成一個易於使用的捷徑。";
		$ADMIN->page_title  = "主題複合管理";
		
		
		$SKIN->td_header[] = array( "名稱"  , "50%" );
		$SKIN->td_header[] = array( "編輯"   , "25%" );
		$SKIN->td_header[] = array( "移除" , "25%" );

		$ADMIN->html .= $SKIN->start_table( "目前的主題複合管理" );
		
		$DB->query("SELECT * FROM ibf_topic_mmod ORDER BY mm_title");
		
		if ( $DB->get_num_rows() )
		{
			while ( $row = $DB->fetch_row() )
			{
			
				$ADMIN->html .= $SKIN->add_td_row( array( 
														  "<strong>{$row['mm_title']}</strong>",
														  "<center><a href='{$ADMIN->base_url}&amp;act=multimod&amp;code=edit&amp;id={$row['mm_id']}'>編輯</a></center>",
														  "<center><a href='{$ADMIN->base_url}&amp;act=multimod&amp;code=delete&amp;id={$row['mm_id']}'>移除</a></center>",
												 )      );
			
			
			}
		}
		else
		{
			$ADMIN->html .= $SKIN->add_td_basic("<center>未設定</center>");
		}
		
		$ADMIN->html .= $SKIN->add_td_basic("<a href='{$ADMIN->base_url}&amp;act=multimod&amp;code=new' class='fauxbutton'>新增</a>", 'center', 'pformstrip');
		
		
		$ADMIN->html .= $SKIN->end_table();
		
		
		$ADMIN->output();
	
	}
	
	
	//------------------------------------------------------
    // Get the active in forums
    //------------------------------------------------------    
        
    function get_activein_forums()
    {
    	global $IN, $INFO, $DB, $std, $HTTP_POST_VARS;
    	
    	$forum_array  = array();
    	$forum_string = "";
    	$sql_query    = "";
    	$check_sub    = 0;
    	
    	$cats         = array();
    	$forums       = array();
    	
    	// If we have an array of "forums", loop
    	// through and build our *SQL IN( ) statement.
    	
    	//------------------------------------------------
    	// Check for an array
    	//------------------------------------------------
    	
    	if ( is_array( $HTTP_POST_VARS['forums'] )  )
    	{
    	
			 //--------------------------------------------
			 // Go loopy loo
			 //--------------------------------------------
			 
			 foreach( $HTTP_POST_VARS['forums'] as $l )
			 {
				 if ( preg_match( "/^c_/", $l ) )
				 {
					 $cats[] = intval( str_replace( "c_", "", $l ) );
				 }
				 else
				 {
					 $forums[] = intval($l);
				 }
			 }
			 
			 //--------------------------------------------
			 // Do we have cats? Give 'em to Charles!
			 //--------------------------------------------
			 
			 if ( count( $cats ) )
			 {
				 $sql_query = "SELECT id, read_perms, password, subwrap from ibf_forums WHERE category IN(".implode(",",$cats).")";
				 $boolean   = "OR";
			 }
			 else
			 {
				 $sql_query = "SELECT id, read_perms, password, subwrap from ibf_forums";
				 $boolean   = "WHERE";
			 }
			 
			 if ( count( $forums ) )
			 {
				  $sql_query .= " $boolean (id IN(".implode(",",$forums).") or parent_id IN(".implode(",",$forums).") )";
			 }
			 
			 if ( $sql_query == "" )
			 {
				 // Return empty..
				 
				 return;
			 }
  
    		
    		//--------------------------------------------
    		// Run query and finish up..
    		//--------------------------------------------
			
			$DB->query( $sql_query );
				
			while ($i = $DB->fetch_row())
			{
				$forum_array[] = $i['id'];
			}
		}
		else
		{
			//--------------------------------------------
			// Not an array...
			//--------------------------------------------
			
			if ( $IN['forums'] != "" )
			{
				$l = $IN['forums'];
				
				//--------------------------------------------
				// Single  Cat
				//--------------------------------------------
				
				if ( preg_match( "/^c_/", $l ) )
				{
					$c = intval( str_replace( "c_", "", $l ) );
					
					if ($c)
					{
						$DB->query("SELECT id, read_perms, password FROM ibf_forums WHERE category=$c");
						
						while ($i = $DB->fetch_row())
						{
							$forum_array[] = $i['id'];
						}
					}
				}
				else
				{
					//--------------------------------------------
					// Single forum
					//--------------------------------------------
				
					$f = intval($l);
					
					if ($f)
					{
						$qe = " OR parent_id=$f ";
						
						$DB->query("SELECT id, read_perms, password FROM ibf_forums WHERE id=$f".$qe);
						
						while ($i = $DB->fetch_row())
						{
							$forum_array[] = $i['id'];
						}
					}
				}
			}
		}
    					
    	return $forum_array;
    	
    }
	
	
	
}


?>