<?php

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

$CALL_SYSTEM				=	array();
$CALL_SYSTEM['SUBJECT'] 	= 	true;
$CALL_SYSTEM['SECTION'] 	= 	true;
$CALL_SYSTEM['MODERATORS'] 	= 	true;
$CALL_SYSTEM['REPLY'] 		= 	true;
$CALL_SYSTEM['SUPERMEMBERLOGS'] 			= 	true;

define('JAVASCRIPT_PowerCode',true);

include('common.php');

define('CLASS_NAME','PowerBBAJAXtMOD');

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

		if ($PowerBB->_GET['management'])
		{
			$PowerBB->_POST['section'] = $PowerBB->functions->CleanVariable($PowerBB->_POST['section'],'intval');

			if ($PowerBB->functions->ModeratorCheck($PowerBB->_POST['section']))
			{
				$PowerBB->_POST['subject'] = $PowerBB->functions->CleanVariable($PowerBB->_POST['subject'],'intval');

				if (empty($PowerBB->_POST['subject']))
				{
					echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['path_not_true']. '</div>';
				}
				else
				{
					if ($PowerBB->_POST['oper'] == 'stick')
					{
						$UpdateArr 			= array();
						$UpdateArr['where'] = array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->StickSubject($UpdateArr);

						if ($update)
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Was_stick_subject']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_stick_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'unstick')
					{
						$UpdateArr 			= array();
						$UpdateArr['where'] = array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->UnstickSubject($UpdateArr);

						if ($update)
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Was_Cancel_stick_subject']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Cancel_stick_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'close')
					{
						$UpdateArr 				= 	array();
						$UpdateArr['where'] 	= 	array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->CloseSubject($UpdateArr);

						if ($update)
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Was_Close_subject']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Close_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'open')
					{
						$UpdateArr 				= 	array();
						$UpdateArr['where'] 	= 	array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->OpenSubject($UpdateArr);

						if ($update)
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Was_Opin_subject']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Opin_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'delete')
					{

						$Memberarr 			= 	array();
						$Memberarr['where'] 	= 	array('username',$PowerBB->_CONF['member_row']['username']);

						$Memberinfo = $PowerBB->member->GetMemberInfo($Memberarr);

					  	$Grparr 			= 	array();
						$Grparr['where'] 	= 	array('id',$Memberinfo['usergroup']);

						$Groupinfo = $PowerBB->group->GetGroupInfo($Grparr);
						if ($Groupinfo['id'] == '1')
						{

						 $SubjectInfoArr				=	array();
					     $SubjectInfoArr['where'] 	= 	array('id',$PowerBB->_POST['subject']);

						 $SubjectDelInfo = $PowerBB->subject->GetSubjectInfo($SubjectInfoArr);

					     $PowerBB->template->assign('SubjectDelInfo',$SubjectDelInfo);
					     $PowerBB->template->assign('subject',$PowerBB->_POST['subject']);

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

						}
						else
						{
						$UpdateArr 				= 	array();
						$UpdateArr['where'] 	= 	array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->MoveSubjectToTrash($UpdateArr);

						if ($update)
						{

							$SecArr 			= 	array();
							$SecArr['where'] 	= 	array('id',$PowerBB->_POST['section']);

							$this->SectionInfo = $PowerBB->section->GetSectionInfo($SecArr);

				           		if ($this->SectionInfo['last_subjectid'] == $PowerBB->_POST['subject'])
						      {

				              $section_id = $PowerBB->_POST['section'];
				              $LastSubjec_nm = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['subject'] . " WHERE section='$section_id'"));

				              $UpdateLastSubject				=	array();
						      $UpdateLastSubject['where'] 	= 	array('id',$this->SectionInfo['last_subjectid']);

							    $LastSubject = $PowerBB->subject->GetSubjectInfo($UpdateLastSubject);

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

						     	$UpdateArr['field']['last_subject'] 	= 	$PowerBB->_CONF['template']['lang']['deletd'].$LastSubject['title'];
					     		$UpdateArr['where']					    = 	array('id',$section_id);

					     		$UpdateSubjectNumber = $PowerBB->section->UpdateSection($UpdateArr);
				                $update_cache = $PowerBB->section->UpdateSectionsCache($UpdateArr);
				                $cache = $PowerBB->section->UpdateSectionsCache(array('parent'=>$this->SectionInfo['parent']));

						     }


					          	// INSERT moderators Action
								$EditAction				=	array();
							    $EditAction['where'] 	= 	array('id',$PowerBB->_POST['subject']);

								$action = $PowerBB->subject->GetSubjectInfo($EditAction);

							    $subject_title = $action['title'];
							    $time=time()+$PowerBB->_CONF['info_row']['timestamp'];

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

								$SmLogsArr['field']['username'] 	= 	$PowerBB->_CONF['member_row']['username'];
								$SmLogsArr['field']['edit_action'] 	= 	$PowerBB->_CONF['template']['lang']['Was_Trasht1'];
								$SmLogsArr['field']['subject_title']= 	$subject_title;
								$SmLogsArr['field']['subject_id'] 	= 	$PowerBB->_POST['subject'];
								$SmLogsArr['field']['edit_date'] 	= 	date("d/m/Y", $time);

								$insert = $PowerBB->supermemberlogs->InsertSupermemberlogs($SmLogsArr);

							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Was_Trasht']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Trasht']. '</div>';
						}
					 }
					}
					else if ($PowerBB->_POST['oper'] == 'up')
					{
						$UpdateArr 							= 	array();
						$UpdateArr['field']['write_time'] 	= 	time() - ( intval('-42') );
						$UpdateArr['where'] 				= 	array('id',$PowerBB->_POST['subject']);

						$update =  $PowerBB->subject->UpdateSubject($UpdateArr);

						if ($update)
						{
							// INSERT moderators Action
							$EditAction				=	array();
							$EditAction['where'] 	= 	array('id',$PowerBB->_POST['subject']);

							$action = $PowerBB->subject->GetSubjectInfo($EditAction);

							$subject_title = $action['title'];
							$time=time()+$PowerBB->_CONF['info_row']['timestamp'];

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

							$SmLogsArr['field']['username'] 	= 	$PowerBB->_CONF['member_row']['username'];
							$SmLogsArr['field']['edit_action'] 	= 	$PowerBB->_CONF['template']['lang']['topic_Up'];
							$SmLogsArr['field']['subject_title']= 	$PowerBB->functions->CleanVariable($subject_title,'sql');
							$SmLogsArr['field']['subject_id'] 	= 	$PowerBB->_POST['subject'];
							$SmLogsArr['field']['edit_date'] 	= 	date("d/m/Y", $time);

							$insert = $PowerBB->supermemberlogs->InsertSupermemberlogs($SmLogsArr);

							// Update Section Cache

							// Update Last subject's information in Section Form
							$UpdateLastFormSecArr = array();
							$UpdateLastFormSecArr['field']			=	array();

							$UpdateLastFormSecArr['field']['last_writer'] 		= 	$action['writer'];
							$UpdateLastFormSecArr['field']['last_subject'] 		= 	$action['title'];
							$UpdateLastFormSecArr['field']['last_subjectid'] 	= 	$PowerBB->_POST['subject'];
							$UpdateLastFormSecArr['field']['last_date'] 	= 	$action['write_time'];
							$UpdateLastFormSecArr['field']['last_time'] 	= 	$action['write_time'];
							$UpdateLastFormSecArr['field']['icon'] 		    = 	$action['icon'];
							$UpdateLastFormSecArr['field']['last_reply'] 	= 	0;
							$UpdateLastFormSecArr['field']['last_berpage_nm']  = 	0;

							$UpdateLastFormSecArr['where'] 		        = 	array('id',$action['section']);

							// Update Last Form Sec subject's information
							$UpdateLastFormSec = $PowerBB->section->UpdateSection($UpdateLastFormSecArr);

							 // Get Section Info
							$SecArr 			= 	array();
							$SecArr['where'] 	= 	array('id',$action['section']);

							$this->SectionInfo = $PowerBB->section->GetSectionInfo($SecArr);

							// Update section's cache
							$UpdateArr 				= 	array();
							$UpdateArr['parent'] 	= 	$this->SectionInfo['parent'];

							$update_cache = $PowerBB->section->UpdateSectionsCache($UpdateArr);

							unset($UpdateArr);
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Was_Up_subject']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Up_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'down')
					{
						$UpdateArr 							= 	array();
						$UpdateArr['field']['write_time'] 	= 	time() - ( intval('420000000000000000000') );
						$UpdateArr['where'] 				= 	array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->UpdateSubject($UpdateArr);

						if ($update)
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Was_Down_subject']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Down_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'special')
					{
						$UpdateArr = array();
						$UpdateArr['field'] = array();
						$UpdateArr['field']['special'] = 1;
						$UpdateArr['where'] = array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->UpdateSubject($UpdateArr);

						if ($update)
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['special_subject_successfully']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Down_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'nospecial')
					{
						$UpdateArr = array();
						$UpdateArr['field'] = array();
						$UpdateArr['field']['special'] = 0;
						$UpdateArr['where'] = array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->UpdateSubject($UpdateArr);

						if ($update)
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['nospecial_subject_successfully']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Down_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'unreview_subject')
					{
						$UpdateArr = array();
						$UpdateArr['field'] = array();
						$UpdateArr['field']['review_subject'] = 0;
						$UpdateArr['where'] = array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->UpdateSubject($UpdateArr);

						if ($update)
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Was_approved_on_the_subject_successfully']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Down_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'review_subject')
					{
						$UpdateArr = array();
						$UpdateArr['field'] = array();
						$UpdateArr['field']['review_subject'] = 1;
						$UpdateArr['where'] = array('id',$PowerBB->_POST['subject']);

						$update = $PowerBB->subject->UpdateSubject($UpdateArr);

						if ($update)
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Subject_hide_successfully']. '</div>';
						}
						else
						{
							echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['Not_Was_Down_subject']. '</div>';
						}
					}
					else if ($PowerBB->_POST['oper'] == 'move')
					{
						$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']));

						if (is_array($groups[$PowerBB->_CONF['group_info']['id']]))
						{
							if ($groups[$PowerBB->_CONF['group_info']['id']]['view_section'])
							{
								$PowerBB->_CONF['template']['foreach']['forums_list'][$cat['id'] . '_m'] = $cat;
							}
						}

						unset($groups);

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

							foreach ($forums as $forum)
							{
								if (is_array($forum['groups'][$PowerBB->_CONF['group_info']['id']]))
								{
						if ($forum['groups'][$PowerBB->_CONF['group_info']['id']]['view_section'])
						{
							//////////

							// Get the first-level sub forums as a _link_ and store it in $forum['sub']

							$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 (is_array($sub['groups'][$PowerBB->_CONF['group_info']['id']]))
										{
											if ($sub['groups'][$PowerBB->_CONF['group_info']['id']]['view_section'])
											{
												if (!$forum['is_sub'])
												{
													$forum['is_sub'] = 1;
												}

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


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

	                  	//////////

						$PowerBB->template->assign('section',$PowerBB->_POST['section']);
						$PowerBB->template->assign('subject',$PowerBB->_POST['subject']);
                    	$PowerBB->template->display('subject_move_index');
					}
					else if ($PowerBB->_POST['oper'] == 'edit')
		 			{

				      $edit_page =  ('<div class="info_bar"><a href="index.php?page=management&subject=1&section='.$PowerBB->_POST['section'].'&subject_id='.$PowerBB->_POST['subject'].'&operator=edit">' . $PowerBB->_CONF['template']['lang']['Click_here_to_be_able_to_go_to_the_Edit_Subject']. '</a></div>');
				       echo $edit_page;

					}
					else if ($PowerBB->_POST['oper'] == 'repeated')
		 			{

						$PowerBB->template->assign('subject',$PowerBB->_POST['subject']);

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

					}
				 }
			}
			else
			{
				echo '<div class="info_bar">' . $PowerBB->_CONF['template']['lang']['not_allowed_access']. '</div>';
			}
		}
	}
}

?>
