<?php

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

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

define('JAVASCRIPT_PowerCode',true);

include('common.php');

define('CLASS_NAME','PowerBBPrivateMassegeListMOD');

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

       $this->_GetJumpSectionsList();

		if (!$PowerBB->_CONF['info_row']['pm_feature'])
		{
            $PowerBB->functions->ShowHeader();
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['no_pm']);
		}

		/** Can't use the private massege system **/
		if (!$PowerBB->_CONF['rows']['group_info']['use_pm'])
		{
        	$PowerBB->functions->ShowHeader();
		     /** Visitor can't use the private massege system **/
			if (!$PowerBB->_CONF['member_permission'])
			{
				  $PowerBB->template->display('login');
	              $PowerBB->functions->error_stop();
			 }
		    else
            {
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Cant_use_pm']);
	        }
		}

		/** **/

		/** **/

		/** Get the list of masseges **/
		if ($PowerBB->_GET['list'])
		{
			$this->_ShowList();
		}
		/** **/

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

	/**
	 * Get the list of masseges
	 */
	function _ShowList()
	{
		global $PowerBB;

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

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


		$PowerBB->_GET['count'] = (!isset($PowerBB->_GET['count'])) ? 0 : $PowerBB->_GET['count'];
		$PowerBB->_GET['count'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['count'],'intval');

		//////////

		$NumArr 						= 	array();
		$NumArr['where'] 				= 	array();

		$NumArr['where'][0] 			= 	array();
		$NumArr['where'][0]['name'] 	= 	($PowerBB->_GET['folder'] == 'inbox') ? 'user_to' : 'user_from';
		$NumArr['where'][0]['oper'] 	= 	'=';
		$NumArr['where'][0]['value'] 	= 	$PowerBB->_CONF['member_row']['username'];

		$NumArr['where'][1] 			= 	array();
		$NumArr['where'][1]['con'] 		= 	'AND';
		$NumArr['where'][1]['name'] 	= 	'folder';
		$NumArr['where'][1]['oper'] 	= 	'=';
		$NumArr['where'][1]['value'] 	= 	($PowerBB->_GET['folder'] == 'inbox') ? 'inbox' : 'sent';

		//////////

		$MsgArr = array();

		$MsgArr['username'] 			= 	$PowerBB->_CONF['member_row']['username'];

		$MsgArr['proc'] 				= 	array();
		$MsgArr['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');
		$MsgArr['proc']['date'] 		= 	array('method'=>'date','store'=>'date','type'=>$PowerBB->_CONF['info_row']['timesystem']);

		$MsgArr['order']				=	array();
		$MsgArr['order']['field']		=	'id';
		$MsgArr['order']['type']		=	'DESC';

		// Pager setup
		$MsgArr['pager'] 				= 	array();
		$MsgArr['pager']['total']		= 	$PowerBB->pm->GetPrivateMassegeNumber($NumArr);
		$MsgArr['pager']['perpage'] 	= 	$PowerBB->_CONF['info_row']['perpage'];
		$MsgArr['pager']['count'] 		= 	$PowerBB->_GET['count'];
		if ($PowerBB->_GET['folder'] == 'sent')
		{
		$MsgArr['pager']['location'] 	= 	'index.php?page=pm_list&list=1&folder=sent';
		}
		else
		{
		$MsgArr['pager']['location'] 	= 	'index.php?page=pm_list&list=1&folder=inbox';
		}
		$MsgArr['pager']['var'] 		= 	'count';

		if ($PowerBB->_GET['folder'] == 'sent')
		{
            $PowerBB->template->assign('folder','sent');
			$GetMassegeList = $PowerBB->pm->GetSentList($MsgArr);
			$PowerBB->_CONF['template']['while']['MassegeList'] = $GetMassegeList;
		}
		else
		{
			$GetMassegeList = $PowerBB->pm->GetInboxList($MsgArr);
			$PowerBB->template->assign('folder','inbox');
		}

		$PowerBB->_CONF['template']['while']['MassegeList'] = $GetMassegeList;
           $AllPmNum = $PowerBB->pm->GetAllPmNum($MsgArr);
           $PowerBB->template->assign('AllPmNum',$AllPmNum);
           $PowerBB->template->assign('AllowedPmNum',$PowerBB->_CONF['group_info']['max_pm']);
           $PowerBB->template->assign('MsgsNum',$PowerBB->pm->GetPrivateMassegeNumber($NumArr));
		if ($PowerBB->pm->GetPrivateMassegeNumber($NumArr) > $PowerBB->_CONF['info_row']['perpage'])
		{
		$PowerBB->template->assign('pager',$PowerBB->pager->show());
		}
		$PowerBB->template->assign('folder',$PowerBB->_GET['folder']);

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

     /**
    * 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&show=1&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)

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

?>
