<?php

(!defined('IN_PowerBB')) ? die() : '';

define('IN_ADMIN',true);

$CALL_SYSTEM			        =	array();
$CALL_SYSTEM['TOPICMOD']           =   true;

define('JAVASCRIPT_PowerCode',true);

include('common.php');

define('CLASS_NAME','PowerBBTopicmodrMod');

class PowerBBTopicmodrMod
{
	function run()
	{
		global $PowerBB;

		if ($PowerBB->_CONF['member_permission'])
		{
			$PowerBB->template->display('header');


			if ($PowerBB->_CONF['rows']['group_info']['admincp_multi_moderation'] == '0')
			{
			  $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['error_permission']);
			}


			if ($PowerBB->_GET['control'])
			{
				if ($PowerBB->_GET['main'])
				{
					$this->_ControlMain();
				}
			}
			elseif ($PowerBB->_GET['add'])
			{
              	if ($PowerBB->_GET['main'])
				{
					$this->_AddTopicModMain();
				}
                elseif ($PowerBB->_GET['start'])
				{
					$this->_AddTopicModStart();
				}
			}
			elseif ($PowerBB->_GET['edit'])
			{
				if ($PowerBB->_GET['main'])
				{
					$this->_EditMain();
				}
				elseif ($PowerBB->_GET['start'])
				{
					$this->_EditStart();
				}
			}
			elseif ($PowerBB->_GET['del'])
			{
                if ($PowerBB->_GET['start'])
				{
					$this->_DelStart();
				}
			}

		$PowerBB->template->display('footer');
		}

	}


	/**
	 * add TopicMod Main
	 */

	function _AddTopicModMain()
	{
		global $PowerBB;
		//////////

        $SecArr 						= 	array();
		$SecArr['get_from']				=	'db';

		$SecArr['proc'] 				= 	array();
		$SecArr['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');

		$SecArr['order']				=	array();
		$SecArr['order']['field']		=	'sort';
		$SecArr['order']['type']		=	'ASC';

		$SecArr['where']				=	array();
		$SecArr['where'][0]['name']		= 	'parent';
		$SecArr['where'][0]['oper']		= 	'=';
		$SecArr['where'][0]['value']	= 	'0';

		// Get main sections
		$cats = $PowerBB->section->GetSectionsList($SecArr);

		// We will use forums_list to store list of forums which will view in main page
		$PowerBB->_CONF['template']['foreach']['forums_list'] = array();

		// Loop to read the information of main sections
		foreach ($cats as $cat)
		{
			// Get the groups information to know view this section or not
			$groups = unserialize(base64_decode($cat['sectiongroup_cache']));


					$PowerBB->_CONF['template']['foreach']['forums_list'][$cat['id'] . '_m'] = $cat;


			if (!empty($cat['forums_cache']))
			{
				$forums = unserialize(base64_decode($cat['forums_cache']));

				foreach ($forums as $forum)
				{


							$forum['is_sub'] 	= 	0;
							$forum['sub']		=	'';

							if (!empty($forum['forums_cache']))
							{
								$subs = unserialize(base64_decode($forum['forums_cache']));

								if (is_array($subs))
								{
									foreach ($subs as $sub)
									{

												if (!$forum['is_sub'])
												{
													$forum['is_sub'] = 1;
												}

												$forum['sub'] .= ('<option value="' .$sub['id'] . '" >---'  . $sub['title'] . '</option>');

									}
								}
							}


							$PowerBB->_CONF['template']['foreach']['forums_list'][$forum['id'] . '_f'] = $forum;
					} // end if is_array
				} // end foreach ($forums)
			} // end !empty($forums_cache)

		$PowerBB->template->display('topictmod_add');

    }

	/**
	 * add TopicMod Start
	 */
	function _AddTopicModStart()
	{
		global $PowerBB;

		if (empty($PowerBB->_POST['title']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_fill_in_all_the_information']);
		}

		$forums = $this->get_activein_forums();

		if ( ! $forums )
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_must_select_the_forum']);
		}

			$TopicModArr 			= 	array();
			$TopicModArr['field']	=	array();

			$TopicModArr['field']['title']            = 	 $PowerBB->_POST['title'];
			$TopicModArr['field']['forums'] 		= 	$forums;
			$TopicModArr['field']['title_st'] 		    = 	$PowerBB->_POST['title_st'];
			$TopicModArr['field']['title_end'] 	    	= 	$PowerBB->_POST['title_end'];
			$TopicModArr['field']['state'] 		    = 	$PowerBB->_POST['state'];
			$TopicModArr['field']['pin'] 		    = 	$PowerBB->_POST['pin'];
			$TopicModArr['field']['enabled'] 		    = 	1;
			$TopicModArr['field']['approve'] 		    = 	$PowerBB->_POST['approve'];
			$TopicModArr['field']['move'] 		    = 	$PowerBB->_POST['move'];
			$TopicModArr['field']['reply'] 		    = 	$PowerBB->_POST['reply'];
			$TopicModArr['field']['reply_content'] 		    = 	$PowerBB->_POST['reply_content'];

			$insert = $PowerBB->topicmod->InsertTopicMod($TopicModArr);

			if ($insert)
			{
	          $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Supervisory_property_was_added_successfully']);
               $PowerBB->functions->redirect('admin.php?page=topic_mod&amp;control=1&amp;main=1');
			}

	}

	function _ControlMain()
	{
		global $PowerBB;

        // show TopicMod List
		$TopicModArr 					= 	array();
		$TopicModArr['order']			=	array();
		$TopicModArr['order']['field']	=	'id';
		$TopicModArr['order']['type']	=	'DESC';
		$TopicModArr['proc'] 			= 	array();
		$TopicModArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

		$PowerBB->_CONF['template']['while']['TopicModsList'] = $PowerBB->topicmod->GetTopicModList($TopicModArr);

		$PowerBB->template->display('topictmod_main');
	}




	function _EditMain()
	{
		global $PowerBB;

			if (empty($PowerBB->_GET['id']))
			{
				$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Supervisory_property_requested_does_not_exist']);
			}

			$TopicModEditArr				=	array();
		    $TopicModEditArr['where'] 	= 	array('id',$PowerBB->_GET['id']);

			$TopicModEdit = $PowerBB->topicmod->GetTopicModInfo($TopicModEditArr);

		//////////

	  $SecArr 						= 	array();
		$SecArr['get_from']				=	'db';

		$SecArr['proc'] 				= 	array();
		$SecArr['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');

		$SecArr['order']				=	array();
		$SecArr['order']['field']		=	'sort';
		$SecArr['order']['type']		=	'ASC';

		$SecArr['where']				=	array();
		$SecArr['where'][0]['name']		= 	'parent';
		$SecArr['where'][0]['oper']		= 	'=';
		$SecArr['where'][0]['value']	= 	'0';

		// Get main sections
		$cats = $PowerBB->section->GetSectionsList($SecArr);

		// We will use forums_list to store list of forums which will view in main page
		$PowerBB->_CONF['template']['foreach']['forums_list'] = array();

		// Loop to read the information of main sections
		foreach ($cats as $cat)
		{
			// Get the groups information to know view this section or not
			$groups = unserialize(base64_decode($cat['sectiongroup_cache']));


					$PowerBB->_CONF['template']['foreach']['forums_list'][$cat['id'] . '_m'] = $cat;


			if (!empty($cat['forums_cache']))
			{
				$forums = unserialize(base64_decode($cat['forums_cache']));

				foreach ($forums as $forum)
				{


							$forum['is_sub'] 	= 	0;
							$forum['sub']		=	'';

							if (!empty($forum['forums_cache']))
							{
								$subs = unserialize(base64_decode($forum['forums_cache']));

								if (is_array($subs))
								{
									foreach ($subs as $sub)
									{

												if (!$forum['is_sub'])
												{
													$forum['is_sub'] = 1;
												}

                                          $forum['sub'] .= $sub['id'];


									}


								}
							}


							$PowerBB->_CONF['template']['foreach']['forums_list'][$forum['id'] . '_f'] = $forum;
					} // end if is_array
				} // end foreach ($forums)
			} // end !empty($forums_cache)


		//////////

	  $SecArr1 						= 	array();
		$SecArr1['get_from']				=	'db';

		$SecArr1['proc'] 				= 	array();
		$SecArr1['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');

		$SecArr1['order']				=	array();
		$SecArr1['order']['field']		=	'sort';
		$SecArr1['order']['type']		=	'ASC';

		$SecArr1['where']				=	array();
		$SecArr1['where'][0]['name']		= 	'parent';
		$SecArr1['where'][0]['oper']		= 	'=';
		$SecArr1['where'][0]['value']	= 	'0';

		// Get main sections
		$cats1 = $PowerBB->section->GetSectionsList($SecArr1);

		// We will use forums_list to store list of forums which will view in main page
		$PowerBB->_CONF['template']['foreach']['forums_list1'] = array();

		// Loop to read the information of main sections
		foreach ($cats1 as $cat1)
		{
			// Get the groups information to know view this section or not
			$groups = unserialize(base64_decode($cat1['sectiongroup_cache']));


					$PowerBB->_CONF['template']['foreach']['forums_list1'][$cat1['id'] . '_m'] = $cat1;


			if (!empty($cat1['forums_cache']))
			{
				$forums1 = unserialize(base64_decode($cat1['forums_cache']));

				foreach ($forums1 as $forum1)
				{

							$forum1['is_sub'] 	= 	0;
							$forum1['sub']		=	'';

							if (!empty($forum1['forums_cache']))
							{
								$subs1 = unserialize(base64_decode($forum1['forums_cache']));

								if (is_array($subs1))
								{
									foreach ($subs1 as $sub1)
									{

												if (!$forum1['is_sub'])
												{
													$forum1['is_sub'] = 1;
												}


 			                                      $forum1['sub'] .= $sub1['id'];

									}


								}
							}


							$PowerBB->_CONF['template']['foreach']['forums_list1'][$forum1['id'] . '_f'] = $forum1;
					} // end if is_array
				} // end foreach ($forums1)
			} // end !empty($forums_cache)

			$PowerBB->template->assign('TopicModEdit',$TopicModEdit);


		$PowerBB->template->display('topictmod_edit');
	}

	function _EditStart()
	{
		global $PowerBB;

			if (empty($PowerBB->_GET['id']))
			{
				$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Supervisory_property_requested_does_not_exist']);
			}



		$forums = $this->get_activein_forums();

		if ( ! $forums )
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_must_select_the_forum']);
		}


			$TopicModArr 			= 	array();
			$TopicModArr['field']	=	array();

			$TopicModArr['field']['title']            = 	 $PowerBB->_POST['title'];
			$TopicModArr['field']['forums'] 		= 	$forums;
			$TopicModArr['field']['title_st'] 		    = 	$PowerBB->_POST['title_st'];
			$TopicModArr['field']['title_end'] 	    	= 	$PowerBB->_POST['title_end'];
			$TopicModArr['field']['state'] 		    = 	$PowerBB->_POST['state'];
			$TopicModArr['field']['pin'] 		    = 	$PowerBB->_POST['pin'];
			$TopicModArr['field']['enabled'] 		    = 	1;
			$TopicModArr['field']['approve'] 		    = 	$PowerBB->_POST['approve'];
			$TopicModArr['field']['move'] 		    = 	$PowerBB->_POST['move'];
			$TopicModArr['field']['reply'] 		    = 	$PowerBB->_POST['reply'];
			$TopicModArr['field']['reply_content'] 		    = 	$PowerBB->_POST['reply_content'];
		    $TopicModArr['where'] 				= 	array('id',$PowerBB->_GET['id']);

		$update = $PowerBB->topicmod->UpdateTopicMod($TopicModArr);

		if ($update)
		{
			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Supervisory_property_has_been_updated_successfully']);
               $PowerBB->functions->redirect('admin.php?page=topic_mod&amp;control=1&amp;main=1');
		}
	}

	function _DelStart()
	{
		global $PowerBB;

			if (empty($PowerBB->_GET['id']))
			{
				$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Supervisory_property_requested_does_not_exist']);
			}

			$DelArr 			= 	array();
			$DelArr['where'] 	= 	array('id',$PowerBB->_GET['id']);

			$del = $PowerBB->topicmod->DeleteTopicMod($DelArr);

		if ($del)
		{
			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Supervisory_property_was_deleted_successfully']);
               $PowerBB->functions->redirect('admin.php?page=topic_mod&amp;control=1&amp;main=1');

		}
	}

	//-----------------------------------------
    // Get the active in forums
    //-----------------------------------------

    function get_activein_forums()
    {

		global $PowerBB;

		$forumids = array();

    	//-----------------------------------------
    	// Check for an array
    	//-----------------------------------------

    	if ( is_array( $PowerBB->_POST['forums'] )  )
    	{

    		if ( in_array( 'all', $PowerBB->_POST['forums'] ) )
    		{
    			//-----------------------------------------
    			// Searching all forums..
    			//-----------------------------------------

    			return '*';
    		}
    		else
    		{
				//-----------------------------------------
				// Go loopy loo
				//-----------------------------------------

				foreach( $PowerBB->_POST['forums'] as $l )
				{

						$forumids[] = intval($l);
				}

				//-----------------------------------------
				// Do we have cats? Give 'em to Charles!
				//-----------------------------------------

				if ( count( $forumids  ) )
				{
					foreach( $forumids  as $f )
					{
						if ( is_array($f) and count($f) )
						{
							$forumids  = array_merge( $forumids , $f );
						}
					}
				}
				else
				{
					//-----------------------------------------
					// No forums selected / we have available
					//-----------------------------------------

					return;
				}
    		}
		}
		else
		{
			//-----------------------------------------
			// Not an array...
			//-----------------------------------------

			if ($PowerBB->_POST['forums'] == 'all' )
			{
				return '*';
			}
			else
			{
				if ( $PowerBB->_POST['forums'] != "" )
				{
					$l = intval($PowerBB->_POST['forums']);

					//-----------------------------------------
					// Single forum
					//-----------------------------------------


						$forumids[] = intval($l);


						if ( is_array($f) and count($f) )
						{
							$forumids  = array_merge( $forumids , $f );
						}
				}
			}
		}

		return implode( ",", $forumids );
    }

}

?>
