<commit_before><?php

/**
 *  2Moons
 *  Copyright (C) 2011  Slaver
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package 2Moons
 * @author Slaver <<EMAIL>>
 * @copyright 2009 Lucky <<EMAIL>> (XGProyecto)
 * @copyright 2011 Slaver <<EMAIL>> (Fork/2Moons)
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
 * @version 1.6.1 (2011-11-19)
 * @info $Id$
 * @link http://code.google.com/p/2moons/
 */

class ShowMessagesPage extends AbstractPage
{
	public static $requireModule = MODULE_MESSAGES;

	function __construct() 
	{
		parent::__construct();
	}
	
	function view()
	{
		global $THEME, $LNG, $USER;
		$MessCategory  	= HTTP::_GP('messcat', 100);
		
		$page  			= HTTP::_GP('site', 1);
		
		$this->initTemplate();
		$this->setWindow('ajax');
		
		$MessageList	= array();
		$MesagesID		= array();
		
		if($MessCategory == 999)  {
			$MessageCount	= $GLOBALS['DATABASE']->countquery("SELECT COUNT(*) FROM ".MESSAGES." WHERE message_sender = ".$USER['id']." AND message_type != 50;");
			
			$maxPage	= max(1, ceil($MessageCount / MESSAGES_PER_PAGE));
			$page		= max(1, min($page, $maxPage));
			
			$MessageResult	= $GLOBALS['DATABASE']->query("SELECT message_id, message_time, CONCAT(username, ' [',galaxy, ':', system, ':', planet,']') as message_from, message_subject, message_sender, message_type, message_unread, message_text FROM ".MESSAGES." INNER JOIN ".USERS." ON id = message_owner WHERE message_sender = ".$USER['id']." AND message_type != 50 ORDER BY message_time DESC LIMIT ".(($page - 1) * MESSAGES_PER_PAGE).", ".MESSAGES_PER_PAGE.";");
		} else {
			$MessageCount 	= $GLOBALS['DATABASE']->countquery("SELECT COUNT(*) FROM ".MESSAGES." WHERE message_owner = ".$USER['id'].($MessCategory != 100 ? " AND message_type = ".$MessCategory : "").";");
			
			$maxPage	= max(1, ceil($MessageCount / MESSAGES_PER_PAGE));
			$page		= max(1, min($page, $maxPage));
			
			$MessageResult	= $GLOBALS['DATABASE']->query("SELECT message_id, message_time, message_from, message_subject, message_sender, message_type, message_unread, message_text FROM ".MESSAGES." WHERE message_owner = ".$USER['id'].($MessCategory != 100 ? " AND message_type = ".$MessCategory:"")." ORDER BY message_time DESC LIMIT ".(($page - 1) * MESSAGES_PER_PAGE).", ".MESSAGES_PER_PAGE.";");
		}

		while ($MessageRow = $GLOBALS['DATABASE']->fetch_array($MessageResult))
		{
			$MesagesID[]	= $MessageRow['message_id'];
			
			$MessageList[]	= array(
				'id'		=> $MessageRow['message_id'],
				'time'		=> _date($LNG['php_tdformat'], $MessageRow['message_time'], $USER['timezone']),
				'from'		=> $MessageRow['message_from'],
				'subject'	=> $MessageRow['message_subject'],
				'sender'	=> $MessageRow['message_sender'],
				'type'		=> $MessageRow['message_type'],
				'unread'	=> $MessageRow['message_unread'],
				'text'		=> $MessageRow['message_text'],
			);
		}
		
		if(!empty($MesagesID) && $MessCategory != 999) {
			$GLOBALS['DATABASE']->query("UPDATE ".MESSAGES." SET message_unread = 0 WHERE message_id IN (".implode(',', $MesagesID).") AND message_owner = ".$USER['id'].";");
		}
		
		$GLOBALS['DATABASE']->free_result($MessageResult);		

		$this->tplObj->assign_vars(array(
			'MessID'		=> $MessCategory,
			'MessageCount'	=> $MessageCount,
			'MessageList'	=> $MessageList,
			'page'			=> $page,
			'maxPage'		=> $maxPage,
		));
		
		$this->display('page.messages.view.tpl');
	}
	
	
	function action()
	{
		global $USER;
		
		$MessCategory  	= HTTP::_GP('messcat', 100);
		$page		 	= HTTP::_GP('page', 1);
		
		
		$redirectUrl	= 'game.php?page=messages&category='.$MessCategory.'&side='.$page;
		
		if(isset($_POST['submitTop']))
		{
			$action	= HTTP::_GP('actionTop', '');
		}
		elseif(isset($_POST['submitButtom']))
		{
			$action	= HTTP::_GP('actionBottom', '');
		}
		else
		{
			$this->redirectTo($redirectUrl);
		}
		
		if($action == 'deleteunmarked' && (empty($messageIDs) || !is_array($messageIDs)))
			$action	= 'deletetypeall';
		
		if($action == 'deletetypeall' && $MessCategory == 100)<commit_msg>Bottom action does not works correctly (Bug #119)<commit_after><?php

/**
 *  2Moons
 *  Copyright (C) 2011  Slaver
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package 2Moons
 * @author Slaver <<EMAIL>>
 * @copyright 2009 Lucky <<EMAIL>> (XGProyecto)
 * @copyright 2011 Slaver <<EMAIL>> (Fork/2Moons)
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
 * @version 1.6.1 (2011-11-19)
 * @info $Id$
 * @link http://code.google.com/p/2moons/
 */

class ShowMessagesPage extends AbstractPage
{
	public static $requireModule = MODULE_MESSAGES;

	function __construct() 
	{
		parent::__construct();
	}
	
	function view()
	{
		global $THEME, $LNG, $USER;
		$MessCategory  	= HTTP::_GP('messcat', 100);
		
		$page  			= HTTP::_GP('site', 1);
		
		$this->initTemplate();
		$this->setWindow('ajax');
		
		$MessageList	= array();
		$MesagesID		= array();
		
		if($MessCategory == 999)  {
			$MessageCount	= $GLOBALS['DATABASE']->countquery("SELECT COUNT(*) FROM ".MESSAGES." WHERE message_sender = ".$USER['id']." AND message_type != 50;");
			
			$maxPage	= max(1, ceil($MessageCount / MESSAGES_PER_PAGE));
			$page		= max(1, min($page, $maxPage));
			
			$MessageResult	= $GLOBALS['DATABASE']->query("SELECT message_id, message_time, CONCAT(username, ' [',galaxy, ':', system, ':', planet,']') as message_from, message_subject, message_sender, message_type, message_unread, message_text FROM ".MESSAGES." INNER JOIN ".USERS." ON id = message_owner WHERE message_sender = ".$USER['id']." AND message_type != 50 ORDER BY message_time DESC LIMIT ".(($page - 1) * MESSAGES_PER_PAGE).", ".MESSAGES_PER_PAGE.";");
		} else {
			$MessageCount 	= $GLOBALS['DATABASE']->countquery("SELECT COUNT(*) FROM ".MESSAGES." WHERE message_owner = ".$USER['id'].($MessCategory != 100 ? " AND message_type = ".$MessCategory : "").";");
			
			$maxPage	= max(1, ceil($MessageCount / MESSAGES_PER_PAGE));
			$page		= max(1, min($page, $maxPage));
			
			$MessageResult	= $GLOBALS['DATABASE']->query("SELECT message_id, message_time, message_from, message_subject, message_sender, message_type, message_unread, message_text FROM ".MESSAGES." WHERE message_owner = ".$USER['id'].($MessCategory != 100 ? " AND message_type = ".$MessCategory:"")." ORDER BY message_time DESC LIMIT ".(($page - 1) * MESSAGES_PER_PAGE).", ".MESSAGES_PER_PAGE.";");
		}

		while ($MessageRow = $GLOBALS['DATABASE']->fetch_array($MessageResult))
		{
			$MesagesID[]	= $MessageRow['message_id'];
			
			$MessageList[]	= array(
				'id'		=> $MessageRow['message_id'],
				'time'		=> _date($LNG['php_tdformat'], $MessageRow['message_time'], $USER['timezone']),
				'from'		=> $MessageRow['message_from'],
				'subject'	=> $MessageRow['message_subject'],
				'sender'	=> $MessageRow['message_sender'],
				'type'		=> $MessageRow['message_type'],
				'unread'	=> $MessageRow['message_unread'],
				'text'		=> $MessageRow['message_text'],
			);
		}
		
		if(!empty($MesagesID) && $MessCategory != 999) {
			$GLOBALS['DATABASE']->query("UPDATE ".MESSAGES." SET message_unread = 0 WHERE message_id IN (".implode(',', $MesagesID).") AND message_owner = ".$USER['id'].";");
		}
		
		$GLOBALS['DATABASE']->free_result($MessageResult);		

		$this->tplObj->assign_vars(array(
			'MessID'		=> $MessCategory,
			'MessageCount'	=> $MessageCount,
			'MessageList'	=> $MessageList,
			'page'			=> $page,
			'maxPage'		=> $maxPage,
		));
		
		$this->display('page.messages.view.tpl');
	}
	
	
	function action()
	{
		global $USER;
		
		$MessCategory  	= HTTP::_GP('messcat', 100);
		$page		 	= HTTP::_GP('page', 1);
		
		
		$redirectUrl	= 'game.php?page=messages&category='.$MessCategory.'&side='.$page;
		
		if(isset($_POST['submitTop']))
		{
			$action	= HTTP::_GP('actionTop', '');
		}
		elseif(isset($_POST['submitBottom']))
		{
			$action	= HTTP::_GP('actionBottom', '');
		}
		else
		{
			$this->redirectTo($redirectUrl);
		}
		
		if($action == 'deleteunmarked' && (empty($messageIDs) || !is_array($messageIDs)))
			$action	= 'deletetypeall';
		
		if($action == 'deletetypeall' && $MessCategory == 100)
{'fraction_non_alphanumeric': 0.17981038569273863, 'fraction_numerical': 0.013897866839043309, 'mean_word_length': 4.31672394043528, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6336916', 'n_tokens_mistral': 3617, 'n_tokens_neox': 3119, 'n_words': 712}