<?php
// $Id: pnadmin.php,v 1.24 2005/01/10 14:37:56 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: Mark West
// Purpose of file:  Emphemerids administration display functions
// ----------------------------------------------------------------------

/**
 * @package PostNuke_Content_Modules
 * @subpackage Ephemerids
 * @license http://www.gnu.org/copyleft/gpl.html
*/
/**
 * the main administration 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 Ephemerids_admin_main()
{
    // 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 edit privilege
    // for some item within this component, or else they won't be able to do
    // anything and so we refuse access altogether.  The lowest level of access
    // for administration depends on the particular module, but it is generally
    // either 'edit' or 'delete'
    if (!pnSecAuthAction(0, 'Ephemerids::', '::', ACCESS_EDIT)) {
        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('Ephemerids');

	// As Admin output changes often, we do not want caching.
	$pnRender->caching = false;

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

/**
 * Add a new ephemerid
 * This is a standard function that is called whenever an administrator
 * wishes to create a new module item
 * @author Mark West
 * @return string HTML string
 */
function Ephemerids_admin_new()
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing
    if (!pnSecAuthAction(0, 'Ephemerids::Item', '::', ACCESS_ADD)) {
        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('Ephemerids');

	// As Admin output changes often, we do not want caching.
	$pnRender->caching = false;

    // Language
    $pnRender->assign('languages', languagelist());

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

/**
 * This is a standard function that is called with the results of the
 * form supplied by Ephemerids_admin_new() to create a new item
 * @author Mark West
 * @param 'did' the day of the emphererid
 * @param 'mid' the month of the emphererid
 * @param 'yid' the year of the emphererid
 * @param 'content' the ephmerid description
 * @param 'language' the language of the ephemerid
 * @return mixed ephemerid id on success, false on failiure
 */
function Ephemerids_admin_create($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($did,
         $mid,
         $yid,
         $content,
         $language) = pnVarCleanFromInput('Date_Day',
                                          'Date_Month',
                                          'Date_Year',
                                          'content',
                                          'language');

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

    // Confirm authorisation code.  This checks that the form had a valid
    // authorisation code attached to it.  If it did not then the function will
    // proceed no further as it is possible that this is an attempt at sending
    // in false data to the system
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL('Ephemerids', 'admin', 'view'));
        return true;
    }

    // Notable by its absence there is no security check here.  This is because
    // the security check is carried out within the API function and as such we
    // do not duplicate the work here

    // The API function is called.  Note that the name of the API function and
    // the name of this function are identical, this helps a lot when
    // programming more complex modules.  The arguments to the function are
    // passed in as their own arguments array
    $eid = pnModAPIFunc('Ephemerids',
                        'admin',
                        'create',
                        array('did' => $did,
                              'mid' => $mid,
                              'yid' => $yid,
                              'content' => $content,
                              'language' => $language));

    // 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 ($eid != false) {
        // Success
        pnSessionSetVar('statusmsg', _EPHEMERIDSCREATED);
    }

    // This function generated no output, and so now it is complete we redirect
    // the user to an appropriate page for them to carry on their work
    pnRedirect(pnModURL('Ephemerids', 'admin', 'view'));

    // Return
    return true;
}

/**
 * Modify an ephemerid
 * This is a standard function that is called whenever an administrator
 * wishes to modify a current module item
 * @author Mark West
 * @param 'eid' the id of the item to be modified
 * @param 'objectid' generic object id maps to eid if presents
 * @return string HTML string
 */
function Ephemerids_admin_modify($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($eid,
         $objectid)= pnVarCleanFromInput('eid',
                                         'objectid');

                           
    // Admin 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 $objectid, 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 $tid
    //
    // Note that this module couuld just use $objectid 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($objectid)) {
        $eid = $objectid;
    }                       

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

	// As Admin output changes often, we do not want caching.
	$pnRender->caching = false;

    // The user API function is called.  This takes the item ID which we
    // obtained from the input and gets us the information on the appropriate
    // item.  If the item does not exist we post an appropriate message and
    // return
    $item = pnModAPIFunc('Ephemerids',
                         'user',
                         'get',
                         array('eid' => $eid));

    if ($item == false) {
        return pnVarPrepHTMLDisplay(_EPHEMERIDSNOSUCHITEM);
    }

    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing.  However,
    // in this case we had to wait until we could obtain the item name to
    // complete the instance information so this is the first chance we get to
    // do the check
    if (!pnSecAuthAction(0, 'Ephemerids::', "$item[content]::$eid", ACCESS_EDIT)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

    // calulate date for use in template
	$item['date'] = $item['yid'] . '-' . $item['mid'] . '-' . $item['did'];

    // Language list
    $pnRender->assign('languages', languagelist());
	
	// assign the item to the template
	$pnRender->assign('item', $item);
    
    // Return the output that has been generated by this function
    return $pnRender->fetch('ephemerids_admin_modify.htm');
}


/**
 * This is a standard function that is called with the results of the
 * form supplied by Ephemerids_admin_modify() to update a current item
 * @author Mark West
 * @param 'eid' the id of the ephemerid
 * @param 'objectid' generic object id maps to eid if present
 * @param 'did' the day of the emphererid
 * @param 'mid' the month of the emphererid
 * @param 'yid' the year of the emphererid
 * @param 'content' the ephmerid description
 * @param 'language' the language of the ephemerid
 * @return bool true on update success, false on failiure
 */
function Ephemerids_admin_update($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($eid,
         $objectid,
         $did,
         $mid,
         $yid,
         $content,
         $language) = pnVarCleanFromInput('eid',
                                          'objectid',
                                          'Date_Day',
                                          'Date_Month',
                                          'Date_Year',
                                          'content',
                                          'language');

    // 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 $objectid, 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 $tid
    //
    // Note that this module couuld just use $objectid 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($objectid)) {
        $eid = $objectid;
    }                       

    // Confirm authorisation code.  This checks that the form had a valid
    // authorisation code attached to it.  If it did not then the function will
    // proceed no further as it is possible that this is an attempt at sending
    // in false data to the system
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL('Ephemerids', 'admin', 'view'));
        return true;
    }

    // Notable by its absence there is no security check here.  This is because
    // the security check is carried out within the API function and as such we
    // do not duplicate the work here

    // The API function is called.  Note that the name of the API function and
    // the name of this function are identical, this helps a lot when
    // programming more complex modules.  The arguments to the function are
    // passed in as their own arguments array.
    //
    // 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(pnModAPIFunc('Ephemerids',
                    'admin',
                    'update',
                    array('eid' => $eid,
                          'did' => $did,
                          'mid' => $mid,
                          'yid' => $yid,
                          'content' => $content,
                          'language' => $language))) {
        // Success
        pnSessionSetVar('statusmsg', _EPHEMERIDSUPDATED);
    }

    // This function generated no output, and so now it is complete we redirect
    // the user to an appropriate page for them to carry on their work
    pnRedirect(pnModURL('Ephemerids', 'admin', 'view'));

    // Return
    return true;
}

/**
 * Delete an ephemerid
 * This is a standard function that is called whenever an administrator
 * wishes to delete a current module item.  Note that this function is
 * the equivalent of both of the modify() and update() functions above as
 * it both creates a form and processes its output.  This is fine for
 * simpler functions, but for more complex operations such as creation and
 * modification it is generally easier to separate them into separate
 * functions.  There is no requirement in the PostNuke MDG to do one or the
 * other, so either or both can be used as seen appropriate by the module
 * developer
 * @author Mark West
 * @param 'eid' the id of the ephemerid to be deleted
 * @param 'objectid' generic object id maps to eid if present
 * @param 'confirmation' confirmation that this item can be deleted
 * @return mixed HTML string if no confirmation, true if delete successful, false otherwise
 */
function Ephemerids_admin_delete($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($eid,
         $objectid,
         $confirmation) = pnVarCleanFromInput('eid',
                                              'objectid',
                                              'confirmation');


    // 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 $objectid, 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 $tid
     //
     // Note that this module couuld just use $objectid 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($objectid)) {
         $eid = $objectid;
     }                     

    // The user API function is called.  This takes the item ID which we
    // obtained from the input and gets us the information on the appropriate
    // item.  If the item does not exist we post an appropriate message and
    // return
    $item = pnModAPIFunc('Ephemerids',
                         'user',
                         'get',
                         array('eid' => $eid));

    if ($item == false) {
        return pnVarPrepHTMLDisplay(_EPHEMERIDSNOSUCHITEM);
    }

    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing.  However,
    // in this case we had to wait until we could obtain the item name to
    // complete the instance information so this is the first chance we get to
    // do the check
    if (!pnSecAuthAction(0, 'Ephemerids::Item', "$item[content]::$eid", ACCESS_DELETE)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

    // Check for confirmation. 
    if (empty($confirmation)) {
        // No confirmation yet - display a suitable form to obtain confirmation
        // of this action from the user

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

	    // As Admin output changes often, we do not want caching.
    	$pnRender->caching = false;

		// Ephemerid ID
        $pnRender->assign('eid', $eid);

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

    // If we get here it means that the user has confirmed the action

    // Confirm authorisation code.  This checks that the form had a valid
    // authorisation code attached to it.  If it did not then the function will
    // proceed no further as it is possible that this is an attempt at sending
    // in false data to the system
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL('Ephemerids', 'admin', 'view'));
        return true;
    }

    // The API function is called.  Note that the name of the API function and
    // the name of this function are identical, this helps a lot when
    // programming more complex modules.  The arguments to the function are
    // passed in as their own arguments array.
    //
    // 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 (pnModAPIFunc('Ephemerids',
                     'admin',
                     'delete',
                     array('eid' => $eid))) {
        // Success
        pnSessionSetVar('statusmsg', _EPHEMERIDSDELETED);
    }

    // This function generated no output, and so now it is complete we redirect
    // the user to an appropriate page for them to carry on their work
    pnRedirect(pnModURL('Ephemerids', 'admin', 'view'));
    
    // Return
    return true;
}

/**
 * View ephemerids
 * This is a standard function called to present the administrator with a list
 * of all items held by the module.
 * @author Mark West
 * @return string HTML string
 */
function Ephemerids_admin_view()
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing.  However,
    // in this case we had to wait until we could obtain the item name to
    // complete the instance information so this is the first chance we get to
    // do the check
    if (!pnSecAuthAction(0, 'Ephemerids::', '::', ACCESS_EDIT)) {
        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
    $startnum = pnVarCleanFromInput('startnum');

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

	// As Admin output changes often, we do not want caching.
	$pnRender->caching = false;

    // The user API function is called.  This takes the number of items
    // required and the first number in the list of all items, which we
    // obtained from the input and gets us the information on the appropriate
    // items.
    $items = pnModAPIFunc('Ephemerids',
                          'user',
                          'getall',
                          array('startnum' => $startnum,
                                'numitems' => pnModGetVar('Ephemerids',
                                                          'itemsperpage')));

    $ephemerids = array();
    if(isset($items) && is_array($items)) {
        foreach ($items as $item) {

            $row = array();

            if (pnSecAuthAction(0, 'Ephemerids::', "$item[content]::$item[eid]", ACCESS_READ)) {
    
                // Options for the item.  Note that each item has the appropriate
                // levels of authentication checked to ensure that it is suitable
                // for display
                $options = array();
                if (pnSecAuthAction(0, 'Ephemerids::', "$item[content]::$item[eid]", ACCESS_EDIT)) {
                    $options[] = array('url' => pnModURL('Ephemerids', 'admin', 'modify', array('eid' => $item['eid'])),
                                       'title' => _EDIT);
                    if (pnSecAuthAction(0, 'Ephemerids::', "$item[content]::$item[eid]", ACCESS_DELETE)) {
                        $options[] = array('url' => pnModURL('Ephemerids', 'admin', 'delete', array('eid' => $item['eid'])),
                                           'title' => _DELETE);
                    }
                }
                $item['options'] = $options;
                $ephemerids[] = $item;
			}
        }
    }
    $pnRender->assign('ephemerids', $ephemerids);

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

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

/**
 * This is a standard function to modify the configuration parameters of the
 * module
 * @author Mark West
 * @return stringHTML string
 */
function Ephemerids_admin_modifyconfig()
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing
    if (!pnSecAuthAction(0, 'Ephemerids::', '::', ACCESS_ADMIN)) {
        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('Ephemerids');

	// As Admin output changes often, we do not want caching.
	$pnRender->caching = false;

    // Number of items to display per page
    $pnRender->assign(pnModGetVar('Ephemerids'));

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

/**
 * This is a standard function to update the configuration parameters of the
 * module given the information passed back by the modification form
 * @author Mark West
 * @return bool true if update successful, false otherwise
 */
function Ephemerids_admin_updateconfig()
{
    // 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
    $itemsperpage = pnVarCleanFromInput('itemsperpage');

    // Confirm authorisation code.  This checks that the form had a valid
    // authorisation code attached to it.  If it did not then the function will
    // proceed no further as it is possible that this is an attempt at sending
    // in false data to the system
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL('Ephemerids', 'admin', 'view'));
        return true;
    }

    // Update module variables.  Note that depending on the HTML structure used
    // to obtain the information from the user it is possible that the values
    // might be unset, so it is important to check them all and assign them
    // default values if required
    if (!isset($itemsperpage)) {
        $itemsperpage = 10;
    }
    pnModSetVar('Ephemerids', 'itemsperpage', $itemsperpage);

    // Let any other modules know that the modules configuration has been updated
    pnModCallHooks('module','updateconfig','Ephemerids', array('module' => 'Ephemerids'));

	// the module configuration has been updated successfuly
	pnSessionSetVar('statusmsg', _CONFIGUPDATED);

    // This function generated no output, and so now it is complete we redirect
    // the user to an appropriate page for them to carry on their work
    pnRedirect(pnModURL('Ephemerids', 'admin', 'view'));

    // Return
    return true;
}

?>