<?php
// $Id: pnuser.php,v 1.18 2005/06/03 09:23:58 markwest Exp $
// ----------------------------------------------------------------------
// 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: Jim McDonald
// Purpose of file:  RSS user display functions
// ----------------------------------------------------------------------

/**
 * @package PostNuke_Content_Modules
 * @subpackage RSS
 * @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)
 */
function RSS_user_main()
{
    // Create output object - this object will store all of our output so that
    // we can return it easily when required
	$pnRender =& new pnRender('RSS');
    
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing.  For the
    // main function we want to check that the user has at least overview
    // privilege for some item within this component, or else they won't be
    // able to see anything and so we refuse access altogether.  The lowest
    // level of access for administration depends on the particular module, but
    // it is generally either 'overview' or 'read'
    if (!pnSecAuthAction(0, 'RSS::', '::', ACCESS_READ)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

	// assign the main function
	$pnRender->assign('main', pnModFunc('RSS', 'user', 'view'));

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

/**
 * view items
 * This is a standard function to provide an overview of all of the items
 * available from the module.
 */
function RSS_user_view()
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing
    if (!pnSecAuthAction(0, 'RSS::', '::', ACCESS_OVERVIEW)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

    // 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,$cid) = pnVarCleanFromInput('startnum', 'cid');
    if (empty($startnum)) {
        $startnum = 1;
    }
	if (!is_numeric($startnum)) {
        return pnVarPrepHTMLDisplay(_MODARGSERROR);
    }

	// get the category id
    if ((pnModAvailable('Categories')) && pnModIsHooked('Categories', 'RSS')) {
		if (empty($cid)) {
		    $cids = pnModAPIFunc('Categories', 'user', 'getmastercids', array('modname' => 'RSS'));
		    if (is_array($cids)) {
		   	   $cid = $cids[0];
		    } else {
		       $cid = $cids;
			}
		}
	}

    // The API function is called.  The arguments to the function are passed in
    // as their own arguments array
    $items = pnModAPIFunc('RSS',
                          'user',
                          'getall',
                          array('startnum' => $startnum,
                                'numitems' => pnModGetVar('RSS',
                                                          'itemsperpage'),
						  'cid' => $cid));

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

	// assign the start number
	$pnRender->assign('startnum', $startnum);

    // Loop through each item and display it.  Note the use of pnVarCensor() to
    // remove any words from the name that the administrator has deemed
    // unsuitable for the site
	$RSSitems = array();
    foreach ($items as $item) {
        if (pnSecAuthAction(0, 'RSS::', "$item[feedname]::$item[fid]", ACCESS_READ)) {
            $RSSitems[] = array('url' => pnModURL('RSS', 'user', 'display',  array('fid' => $item['fid'])),
                                'title' => $item['feedname']);
        }
    }
    $pnRender->assign('items', $RSSitems);

    // 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('RSS', 'user', 'countitems'),
	                                 'itemsperpage' => pnModGetVar('RSS', 'itemsperpage')));

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

/**
 * display item
 * This is a standard function to provide detailed informtion on a single item
 * available from the module.
 */
function RSS_user_display($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($fid,
         $objecfid) = pnVarCleanFromInput('fid',
                                          'objecfid');

    // 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);

    // At this stage we check to see if we have been passed $objecfid, the
    // generic item identifier.  This could have been passed in by a hook or
    // through some other function calling this as part of a larger module, but
    // if it exists it overrides $fid
    //
    // Note that this module couuld just use $objecfid everywhere to avoid all
    // of this munging of variables, but then the resultant code is less
    // descriptive, especially where multiple objects are being used.  The
    // decision of which of these ways to go is up to the module developer
    if (!empty($objecfid)) {
        $fid = $objecfid;
    }

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

	// For caching reasons you must pass a cache ID. This could be done as a 
	// separate parameter to every method that uses caching (like fetch, is_cached
	// etc.) or by assigning the ID to the cache_id property like it is done in
	// this case. 
	// The item ID is appropriate to be used as the cache ID.
	$pnRender->cache_id = $fid;

	// 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('rss_user_display.htm')) {
	   return $pnRender->fetch('rss_user_display.htm');
	}

    // The API function is called.  The arguments to the function are passed in
    // as their own arguments array
    $item = pnModAPIFunc('RSS',
                          'user',
                          'get',
                          array('fid' => $fid));

    // The API function is called.  The arguments to the function are passed in
    // as their own arguments array
    $feed = pnModAPIFunc('RSS',
                         'user',
                         'getfeed',
                         array('fid' => $fid));

    // The return value of the function is checked here, and if the function
    // suceeded then an appropriate message is posted.  Note that if the
    // function did not succeed then the API function should have already
    // posted a failure message so no action is required
    if ($item == false) {
        return pnVarPrepHTMLDisplay(_RSSITEMFAILED);
    }

    // Assign bold module var to RSS to we can use this to decide how to
	// display the item
    $pnRender->assign('bold', pnModGetVar('RSS', 'bold'));

	// Show links in a new browser?
	$pnRender->assign('openinnewwindow', pnModGetVar('RSS', 'openinnewwindow'));

    // Display details of the item.
    $pnRender->assign('item', $item);
    $pnRender->assign('feed', $feed);

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

?>