<?php
// File: $Id: pnuser.php 18056 2006-03-06 13:19:38Z markwest $
// ----------------------------------------------------------------------
// PostNuke Content Management System
// Copyright (C) 2002 by the PostNuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// Based on:
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// 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.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Mark West
// Purpose of file:  Members List User Interface
// ----------------------------------------------------------------------

/**
 * @package PostNuke_ResourcePack_Modules
 * @subpackage Members_List
 * @license http://www.gnu.org/copyleft/gpl.html
*/
/**
 * the main user function
 * This function is the default function, and is called whenever the module is
 * initiated without defining arguments.  As such it can be used for a number
 * of things, but most commonly it either just shows the module menu and
 * returns or calls whatever the module designer feels should be the default
 * function (often this is the view() function)
 * @author Mark West
 * @return string HTML string
 */
function Members_List_user_main()
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing.  
    if (!pnSecAuthAction(0, 'Members_List::', '::', ACCESS_READ)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

    // Create output object - this object will store all of our output so that
    // we can return it easily when required
	$pnRender =& new pnRender('Members_List');

	// since we're caching the view function based on a number of factors
	// we musn't cache the output here
	$pnRender->caching=false;

    // redirect to initial function
    $pnRender->assign('main', pnModFunc('Members_List', 'user', 'view'));
	
    // Return the output that has been generated by this function
    return $pnRender->fetch('memberslist_user_main.htm');
}

/**
 * view members list
 * This function provides the main members list view
 * @author Mark West
 * @return string HTML string
 */
function Members_List_user_view($args)
{
    // Get parameters from whatever input we need.  All arguments to this
    // function should be obtained from pnVarCleanFromInput(), getting them
    // from other places such as the environment is not allowed, as that makes
    // assumptions that will not hold in future versions of PostNuke
    list($startnum, $sortby, $searchby) = pnVarCleanFromInput('startnum', 'sortby', 'searchby');

    // User functions of this type can be called by other modules.  If this
    // happens then the calling module will be able to pass in arguments to
    // this function through the $args parameter.  Hence we extract these
    // arguments *after* we have obtained any form-based input through
    // pnVarCleanFromInput().
    extract($args);
	
	// Set some defaults
    if (empty($sortby)) {
        $sortby = 'uname';
    }

    $letter = pnVarCleanFromInput('letter');
    if (empty($letter)) {
        $letter = _ALL;
    }

    if (empty($startnum)) {
        $startnum = 1;
    }

    // Create output object - this object will store all of our output so that
    // we can return it easily when required
	$pnRender =& new pnRender('Members_List');

    // get some permissions to use in the cache id and later to filter template output
    if ( pnSecAuthAction(0, 'Users::', '::', ACCESS_DELETE) ) {
        $edit=1; $delete=1;
    } elseif ( pnSecAuthAction(0, 'Users::', '::', ACCESS_EDIT) ) {
        $edit=1; $delete=0;
    } else {
        $edit=0; $delete=0;
    }

	// set the cache id
	$pnRender->cache_id = md5((int)$edit.(int)$delete.$startnum.$letter.$sortby);
	
	// check out if the contents are cached.
	// If this is the case, we do not need to make DB queries.
	// Note that we print out "cached:" in front of a chached output --
	// of course, this is here to illustrate caching and needs to be removed!
	if ($pnRender->is_cached('memberslist_user_view.htm')) {
	   return $pnRender->fetch('memberslist_user_view.htm');
	}

    // get the number of users to show per page from the module vars
    $itemsperpage = pnModGetVar('Members_List', 'memberslistitemsperpage');

    // The API function is called.  The arguments to the function are passed in
    // as their own arguments array
    $reguserscount = pnModAPIFunc('Members_List',
                                   'user',
                                   'countitems');
    $regusersonline = pnModAPIFunc('Members_List',
                                   'user',
                                   'getregisteredonline');
    $latestreguser = pnModAPIFunc('Members_List',
                                  'user',
                                  'getlatestuser');

    //output users online
    $pnRender->assign('memberslistreg', $reguserscount);
	$pnRender->assign('memberslistonline', $regusersonline);
	$pnRender->assign('memberslistnewest', pnUserGetVar('uname',$latestreguser));

    // get full list of user id's
    $users = pnModAPIFunc('Members_List',
                          'user',
                          'getall',
                          array('letter' => $letter,
                                'sortby' => $sortby,
                                'searchby' => $searchby,
                                'startnum' => $startnum,
                                'numitems'=> $itemsperpage));

    $userscount = pnModAPIFunc('Members_List',
                               'user',
                               'countitems',
	                           array('letter' => $letter));

    // Is current user online
	$pnRender->assign('loggedin', pnUserLoggedIn());

	// check if we should show the extra admin column
    $pnRender->assign('adminedit', $edit);
    $pnRender->assign('admindelete', $delete);

    $pagedusers = array();
    foreach($users as $userid) {

        $user = pnUserGetVars($userid['uid']);
        $isonline = pnModAPIFunc('Members_List',
                                 'user',
                                 'isonline',
                                 array('userid' => $userid['uid']));

        // is this user online
        if ($isonline){
            $user['onlinestatus'] = 1;
        } else {
            $user['onlinestatus'] = 0;
        }

        // filter out any dummy url's
        if(!$user['url'] or $user['url']=="http://" or $user['url']=="http:///" )  {
            $user['url'] = '';
        }

		// add user to results array
        $pagedusers[] =  $user;
    }
    $pnRender->assign('users', $pagedusers);

    // Assign the values for the smarty plugin to produce a pager in case of there
    // being many items to display.
    //
    // Note that this function includes another user API function.  The
    // function returns a simple count of the total number of items in the item
    // table so that the pager function can do its job properly
	$pnRender->assign('pager', array('numitems' => pnModAPIFunc('Members_List', 'user', 'countitems', array('letter' => $letter)),
	                           'itemsperpage' => pnModGetVar('Members_List', 'memberslistitemsperpage')));

    // Return the output that has been generated by this function
    return $pnRender->fetch('memberslist_user_view.htm');

}

/**
 * Displays last 10 registered users
 * This function displays the last 10 users who registered at this PN site
 * available from the module.
 * @author Mark West
 * @return string HTML string
 */
function Members_List_user_viewlast10($args)
{
    // User functions of this type can be called by other modules.  If this
    // happens then the calling module will be able to pass in arguments to
    // this function through the $args parameter.  Hence we extract these
    // arguments *after* we have obtained any form-based input through
    // pnVarCleanFromInput().
    extract($args);
	
    // Create output object - this object will store all of our output so that
    // we can return it easily when required
	$pnRender =& new pnRender('Members_List');

	// set the cache id
	$pnRender->cache_id = pnUserLoggedIn();

	// check out if the contents are cached.
	// If this is the case, we do not need to make DB queries.
	// Note that we print out "cached:" in front of a chached output --
	// of course, this is here to illustrate caching and needs to be removed!
	if ($pnRender->is_cached('memberslist_user_viewlast10.htm')) {
		return $pnRender->fetch('memberslist_user_viewlast10.htm');
	}

    // get last 10 user id's
    $users = pnModAPIFunc('Members_List',
                          'user',
                          'getall',
                          array('sortby' => 'user_regdate',
                                'numitems' => 10,
                                'sortorder' => 'DESC'));
								 
    // Is current user online
	$pnRender->assign('loggedin', pnUserLoggedIn());

    $pagedusers = array();
    foreach($users as $userid) {

        $user = pnUserGetVars($userid['uid']);
        $isonline = pnModAPIFunc('Members_List',
                                 'user',
                                 'isonline',
                                 array('userid' => $userid['uid']));

        // display online status
        if ($isonline){
		  $user['onlinestatus'] = 1;
        } else {
		  $user['onlinestatus'] = 0;
        }

        // display registration date
        $user['pn_user_regdate'] =  ml_ftime(_DATEBRIEF, $user['pn_user_regdate']);
		
        $pagedusers[] = $user;
    }
    $pnRender->assign('users', $pagedusers);

    // Return the output that has been generated by this function
    return $pnRender->fetch('memberslist_user_viewlast10.htm');

}

/**
 * View users online
 * This function displays the currently online users
 * @author Mark West
 * @return string HTML string
 */
function Members_List_user_viewcurrentonline($args)
{
    // User functions of this type can be called by other modules.  If this
    // happens then the calling module will be able to pass in arguments to
    // this function through the $args parameter.  Hence we extract these
    // arguments *after* we have obtained any form-based input through
    // pnVarCleanFromInput().
    extract($args);

    // Create output object - this object will store all of our output so that
    // we can return it easily when required
	$pnRender =& new pnRender('Members_List');

	// set the cache id
	$pnRender->cache_id = pnUserLoggedIn();

	// check out if the contents are cached.
	// If this is the case, we do not need to make DB queries.
	// Note that we print out "cached:" in front of a chached output --
	// of course, this is here to illustrate caching and needs to be removed!
	if ($pnRender->is_cached('memberslist_user_currentonline.htm')) {
	   return $pnRender->fetch('memberslist_user_currentonline.htm');
	}

    // Load API.  All of the actual work for obtaining information on the items
    // get last 10 user id's
    $users = pnModAPIFunc('Members_List', 'user', 'whosonline');

    // Current user status
	$pnRender->assign('loggedin', pnUserLoggedIn());

    $pagedusers = array();
    foreach($users as $userid) {
        $user = pnUserGetVars($userid['uid']);
        $pagedusers[] = $user; 
    }
    $pnRender->assign('users', $pagedusers);

    // Return the output that has been generated by this function
    return $pnRender->fetch('memberslist_user_currentonline.htm');

}

?>