<?php /*

 ocPortal
 Copyright (c) ocProducts, 2004-2012

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license		http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright	ocProducts Ltd
 * @package		ocf_forum
 */

class Block_main_bottom_bar
{

	/**
	 * Standard modular info function.
	 *
	 * @return ?array	Map of module info (NULL: module is disabled).
	 */
	function info()
	{
		$info=array();
		$info['author']='Chris Graham';
		$info['organisation']='ocProducts';
		$info['hacked_by']=NULL;
		$info['hack_version']=NULL;
		$info['version']=2;
		$info['locked']=false;
		$info['parameters']=array();
		return $info;
	}

	/**
	 * Standard modular run function.
	 *
	 * @param  array		A map of parameters.
	 * @return tempcode	The result of execution.
	 */
	function run($map)
	{
		if (get_forum_type()!='ocf') return new ocp_tempcode();

		require_code('ocf_general');
		require_code('ocf_groups');
		require_css('ocf');

		require_lang('ocf');

		$stats=ocf_get_forums_stats();

		// Users online
		$users_online=new ocp_tempcode();
		$count=0;
		$members=get_online_members(false,NULL,$count);
		$groups_seen=array();
		if (!is_null($members))
		{
			//$members=collapse_2d_complexity('the_user','cache_username',$members);
			$guests=0;
			foreach ($members as $bits)
			{
				$member=$bits['the_user'];
				$username=$bits['cache_username'];

				if ($member==$GLOBALS['OCF_DRIVER']->get_guest_id())
				{
					$guests++;
					continue;
				}
				if (is_null($username)) continue;
				$url=$GLOBALS['OCF_DRIVER']->member_profile_url($member,false,true);
				if (!array_key_exists('m_primary_group',$bits))
					$bits['m_primary_group']=$GLOBALS['FORUM_DRIVER']->get_member_row_field($member,'m_primary_group');
				$pgid=$bits['m_primary_group'];//$GLOBALS['FORUM_DRIVER']->get_member_row_field($member,'m_primary_group');
				if (is_null($pgid)) continue; // Deleted member
				$groups_seen[$pgid]=1;
				$col=get_group_colour($pgid);
				$usergroup=ocf_get_group_name($pgid);
				if (get_value('disable_user_online_groups')==='1')
				{
					$usergroup=NULL;
					$col=NULL;
					$groups_seen=array();
				}
				$users_online->attach(do_template('OCF_USER_MEMBER',array('_GUID'=>'a9cb1af2a04b14edd70749c944495bff','COLOUR'=>$col,'PROFILE_URL'=>$url,'USERNAME'=>$username,'USERGROUP'=>$usergroup)));
			}
			if ($guests!=0)
			{
				if (!$users_online->is_empty()) $users_online->attach(do_lang_tempcode('LIST_SEP'));
				$users_online->attach(do_lang_tempcode('NUM_GUESTS',integer_format($guests)));
			}
		}

		// Birthdays
		$_birthdays=ocf_find_birthdays();
		$birthdays=new ocp_tempcode();
		foreach ($_birthdays as $_birthday)
		{
			$birthday_link=build_url(array('page'=>'topics','type'=>'birthday','id'=>$_birthday['username']),get_module_zone('topics'));
			$birthday=do_template('OCF_BIRTHDAY_LINK',array('_GUID'=>'a98959187d37d80e134d47db7e3a52fa','AGE'=>array_key_exists('age',$_birthday)?integer_format($_birthday['age']):NULL,'PROFILE_URL'=>$GLOBALS['OCF_DRIVER']->member_profile_url($_birthday['id'],false,true),'USERNAME'=>$_birthday['username'],'BIRTHDAY_URL'=>$birthday_link));
			$birthdays->attach($birthday);
		}
		if (!$birthdays->is_empty()) $birthdays=do_template('OCF_BIRTHDAYS',array('_GUID'=>'03da2c0d46e76407d63bff22aac354bd','BIRTHDAYS'=>$birthdays));

		// Usergroup keys
		$groups=array();
		$all_groups=$GLOBALS['FORUM_DRIVER']->get_usergroup_list(true,false,false,NULL,NULL,true);
		foreach ($all_groups as $gid=>$gtitle)
		{
			if ($gid==db_get_first_id()) continue; // Throw out the first, guest
			if (array_key_exists($gid,$groups_seen))
				$groups[]=array('GCOLOUR'=>get_group_colour($gid),'GID'=>strval($gid),'GTITLE'=>$gtitle);
		}

		return do_template('BLOCK_MAIN_BOTTOM_BAR',array(
			'_GUID'=>'sdflkdlfd303frksdf',
			'NEWEST_MEMBER_PROFILE_URL'=>$GLOBALS['OCF_DRIVER']->member_profile_url($stats['newest_member_id'],false,true),
			'NEWEST_MEMBER_USERNAME'=>$stats['newest_member_username'],
			'NUM_MEMBERS'=>integer_format($stats['num_members']),
			'NUM_TOPICS'=>integer_format($stats['num_topics']),
			'NUM_POSTS'=>integer_format($stats['num_posts']),
			'BIRTHDAYS'=>$birthdays,
			'USERS_ONLINE'=>$users_online,
			'USERS_ONLINE_URL'=>has_actual_page_access(get_member(),'onlinemembers')?build_url(array('page'=>'onlinemembers'),get_module_zone('onlinemembers')):new ocp_tempcode(),
			'GROUPS'=>$groups
		));
	}

}

