<?php
/**
 * @version 1.0
 * @package Simplemembership LastOnline Users
 * @copyright 2012 OrdaSoft
 * @powered by OrdaSoft(akbet@mail.ru)
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @description Last Online Users List for Simplemembership
*/

if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

global $database, $my, $mosConfig_live_site,$mosConfig_absolute_path;
$mosConfig_absolute_path = $GLOBALS['mosConfig_absolute_path']	= JPATH_SITE;
require_once($mosConfig_absolute_path."/administrator/components/com_simplemembership/compat.joomla1.5.php");
// load language
$lang =& JFactory::getLanguage();
JFactory::getLanguage()->load('com_users', JPATH_SITE, $lang->getTag(), true);//add language for com_users
$doc =& JFactory::getDocument();  
	$doc->addStyleSheet( $mosConfig_live_site.'/administrator/components/com_simplemembership/includes/simplemembership.css' );
	$database = &JFactory::getDBO();
	$user =& JFactory::getUser();

$sufix = $params->get('moduleclass_sfx');
$Item = $params->get('Item_id');
if($Item){
	$Itemid = $Item;
}
else{
	$database = & JFactory::getDBO();
	$database->setQuery("SELECT id
               FROM `#__menu`
               WHERE `alias` LIKE 'simple-membership' ");
	$Itemid= $database->loadResult();
}
$database = &JFactory::getDBO();
$query = "SELECT * FROM #__users ORDER BY lastvisitDate";
$database->setQuery($query);
$usersList = $database->LoadObjectList();	

foreach($usersList as $user)
{
	if($user->lastvisitDate != "0000-00-00 00:00:00")
	{
		$ListUser[0][]=$user->id;
		$ListUser[1][]=$user->name;
	}
	
	$user_id=$user->id;
	$joom_user = new JUser();
		 $joom_user->load($user_id);
		 $dispatcher = JDispatcher::getInstance();
		 JPluginHelper::importPlugin('user');
		 JForm::addFormPath($mosConfig_absolute_path.'/components/com_simplemembership/forms');
		$user_profile_form = JForm::getInstance('com_users.module', 'registration');
		$results2 = $dispatcher->trigger('onContentPrepareData', array('com_users.module', $joom_user));
		 $results = $dispatcher->trigger('onContentPrepareForm', array($user_profile_form, $joom_user));
		 $user_profile_form->bind($joom_user);
		 $profile_image=$user_profile_form->getValue('file','profile','');
		$mas_img[1][] = " <img src='".$mosConfig_live_site.$user_profile_form->getValue('file','profile','')."' style='height:".$params->get('img_height')."px; width:".$params->get('img_width')."px;'>";
		$mas_img[0][] = $user->id;
}
		 
$i=count($ListUser[0]);
$j = $i;
$max_user=$params->get('max_user');
?>
<div class="profileTable<?php echo $sufix;?>">
<table  class="profileTable<?php echo $sufix;?>">
<?php
if(($params->get('view'))=='0')
{
	do
	{			
		if(((count($ListUser[0]))-$i)!=$max_user){}
		else break;
		$i--;
		if(($params->get('users_pic'))=='0')
		{
			?>
			<tr>
				<td>
					<a href='./index.php?option=com_simplemembership&Itemid=<?php echo $Itemid;?>&task=showUsersProfile&userId=<?php echo $ListUser[0][$i]; ?>' style="text-decoration: none;">	
					<?php echo ($ListUser[1][$i])."<br>"; ?> </a> 
				</td>
			</tr>
			<?php
		}else
		{
			?>
			<tr>
				<td>
					<?php
						echo $mas_img[1][$i]; 
					?><a href='./index.php?option=com_simplemembership&Itemid=<?php echo $Itemid;?>&task=showUsersProfile&userId=<?php echo $ListUser[0][$i]; ?>' style="text-decoration: none; position:relative; bottom:25px;">	
					<?php echo ($ListUser[1][$i])."<br>"; ?> </a> 
				</td>
			</tr>
			<?php
		}
	}while($i >0);
}else{
	?> <tr> <?php
	if(($params->get('users_pic'))=='1')
	{
		do{	
			if(((count($ListUser[0]))-$i)!=$max_user){}
			else break;
			$i--;
				?>	
				<td>
					<?php echo $mas_img[1][$i]."<br>"; ?> 
				</td>
				<?php 
			}while($i >0);
		?></tr><tr> <?php 
		do{
			if(((count($ListUser[0]))-$j)!=$max_user){}
			else break;
			$j--;
			?>
				<td align="center" width="<?php echo $params->get('img_width');?>">
					<a href='./index.php?option=com_simplemembership&Itemid=<?php echo $Itemid;?>&task=showUsersProfile&userId=<?php echo $ListUser[0][$j]; ?>' style="text-decoration: none;">	
					<?php  echo $ListUser[1][$j]; ?> </a> 
				</td>
			<?php
		}while($j >0);
	}else
	{
		do{
			if(((count($ListUser[0]))-$j)!=$max_user){}
			else break;
			$j--;
			?>
				<td>
					<a href='./index.php?option=com_simplemembership&Itemid=<?php echo $Itemid;?>&task=showUsersProfile&userId=<?php echo $ListUser[0][$j]; ?>' style="text-decoration: none; position:relative; left:20px;">	
					<?php  echo $ListUser[1][$j]; ?> </a> 
				</td>
			<?php
		}while($j >0);
	}
} ?>
</table>	
</div>