<?php

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

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

include('common.php');

define('CLASS_NAME','PowerBBLatestMOD');

class PowerBBLatestMOD
{
	function run()
	{
		global $PowerBB;
 		if (!$PowerBB->_CONF['info_row']['active_subject_today'])
		{
		exit;
        }
		$this->_GetJumpSectionsList();

		if ($PowerBB->_GET['today'])
		{
			$this->_TodaySubject();
		}
		else
		{
            $PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['subject_today']);
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}

		$PowerBB->functions->GetFooter();
	}

	function _TodaySubject()
	{
		global $PowerBB;

		$PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['subject_today']);


		$day 	= 	date('j');
		$month 	= 	date('n');
		$year 	= 	date('Y');

		$from 	= 	mktime(0,0,0,$month,$day,$year);
		$to 	= 	mktime(23,59,59,$month,$day,$year);

		 /**
		 * Ok , are you ready to get subjects list ? :)
		 */
 		$PowerBB->_GET['count'] = (!isset($PowerBB->_GET['count'])) ? 0 : $PowerBB->_GET['count'];
 		$PowerBB->_GET['count'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['count'],'intval');
        $forum_not = $PowerBB->_CONF['info_row']['last_subject_writer_not_in'];

        $subject_today_nm = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['subject'] . " WHERE native_write_time BETWEEN " . $from . " AND " . $to . " AND section not in (" .$forum_not. ") AND review_subject<>1 AND delete_topic<>1 "));

       $subject_today_nmbr  = $subject_today_nm;

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

       $SubjectArr = array();
       $SubjectArr['where']              =    array();
       $SubjectArr['where'][0]           =    array();
       $SubjectArr['where'][0]['name']    =    'sec_section';
       $SubjectArr['where'][0]['oper']    =    '<>';
       $SubjectArr['where'][0]['value']    =    1;
       $SubjectArr['where'][0]           =    array();
       //$SubjectArr['where'][0]['con']       =    'AND';
       $SubjectArr['where'][0]['name']    =    'native_write_time';
       $SubjectArr['where'][0]['oper']    =    'BETWEEN';
       $SubjectArr['where'][0]['value']    =    $from . ' AND ' . $to;

       $SubjectArr['where'][1]           =    array();
       $SubjectArr['where'][1]['con']       =    'AND';
       $SubjectArr['where'][1]['name']    =    'section not in (' .$forum_not. ') AND review_subject<>1 AND delete_topic';
       $SubjectArr['where'][1]['oper']    =    '<>';
       $SubjectArr['where'][1]['value']    =    '1';
       $SubjectArr['order'] = array();
       $SubjectArr['order']['field']        =    'id';
       $SubjectArr['order']['type']        =    'DESC';


       $SubjectArr['proc']                    =    array();
       // Ok Mr.** go to hell !
       $SubjectArr['proc']['*']                 =    array('method'=>'clean','param'=>'html');
       $SubjectArr['proc']['native_write_time']    =    array('method'=>'date','store'=>'write_date','type'=>$PowerBB->_CONF['info_row']['timesystem']);
       $SubjectArr['proc']['write_time']           =    array('method'=>'date','store'=>'reply_date','type'=>$PowerBB->_CONF['info_row']['timesystem']);
        // Pager setup


       $SubjectArr['pager']              =    array();
       $SubjectArr['pager']['total']       =    $subject_today_nm;
       $SubjectArr['pager']['perpage']    =    $PowerBB->_CONF['info_row']['perpage'];
       $SubjectArr['pager']['count']        =    $PowerBB->_GET['count'];
       $SubjectArr['pager']['location']    =    'index.php?page=latest&today=1';
       $SubjectArr['pager']['var']        =    'count';


       $PowerBB->_CONF['template']['while']['subject_list'] = $PowerBB->subject->GetSubjectList($SubjectArr);
      if ($subject_today_nm > $PowerBB->_CONF['info_row']['perpage'])
       {
         $PowerBB->template->assign('pager',$PowerBB->pager->show());
       }
       $PowerBB->template->assign('subject_today_nm',$subject_today_nmbr);

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

	}

	    /**
	 * Get the Jump Sections List
	 */
	function _GetJumpSectionsList()
    {
		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
		$catsy = $PowerBB->section->GetSectionsList($SecArr);

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

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

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

	            	if (!empty($caty['forums_cache']))
					{
						$forumsy = unserialize(base64_decode($caty['forums_cache']));

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

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

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

									if (!empty($forumy['forums_cache']))
									{
										$subsy = unserialize(base64_decode($forumy['forums_cache']));

										if (is_array($subsy))
										{
											foreach ($subsy as $suby)
											{
												if (is_array($suby['groups'][$PowerBB->_CONF['group_info']['id']]))
												{
													if ($suby['groups'][$PowerBB->_CONF['group_info']['id']]['view_section'])
													{
														if (!$forumy['is_sub'])
														{
															$forumy['is_sub'] = 1;
														}

														$forumy['sub'] .= ('<option value="index.php?page=forum&amp;show=1&amp;id=' . $suby['id'] . '">---'  . $suby['title'] . '</option>');
													}
												}
											}
										}
									}


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

			unset($groups);


		} // end foreach ($catsy)

		//////////
   }
}

?>
