<?php
//
// +---------------------------------------------------------------------------+
// | Universal Plugin 1.0 for Geeklog - The Ultimate Weblog                    |
// +---------------------------------------------------------------------------+
// | functions.inc                                                             |
// | This file does two things: 1) it implements the necessary Geeklog Plugin  |
// | API method and 2) implements all the common code needed by the {plugin}   |
// | PHP files.                                                                |
// |                                                                           |
// | This is a generic functions.inc for Geeklog 1.3.6.  Edit this file with   |
// | your favorite editor and replace every instance of {plugin} with the name |
// | of your plugin.  You will then have a skeletal working functions.inc.     |
// | See documentation to fill in API implementations you man need.            |
// | You must also fill in the two arrays in the uninstall routine for it to   |
// | work properly.                                                            |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2002 by the following authors:                              |
// |                                                                           |
// | Author:                                                                   |
// | Constructed with the Universal Plugin                                     |
// | Copyright (C) 2002 by the following authors:                              |
// | Tom Willett                 -    tomw@pigstye.net                         |
// | Blaine Lang                 -    langmail@sympatico.ca                    |
// | The Universal Plugin is based on prior work by:                           |
// | Tony Bibbs                  -    tony@tonybibbs.com                       |
// +---------------------------------------------------------------------------+
// |                                                                           |
// | This program is free software; you can redistribute it and/or             |
// | modify it under the terms of the GNU General Public License               |
// | 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.                              |
// |                                                                           |
// | You should have received a copy of the GNU General Public License         |
// | along with this program; if not, write to the Free Software Foundation,   |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
// |                                                                           |
// +---------------------------------------------------------------------------+
//

/**
* This is the generic functions.inc for the Universal Plugin Toolkit for Geeklog.
* You need to change all instances of {plugin} with the name of your plugin.
*/

/**
* Language file Include
*/
$langfile = $_CONF['path'] . 'plugins/{plugin}/language/' . $_CONF['language'] . '.php';

if (
file_exists ($langfile))
{
    include_once (
$langfile);
}
else
{
    include_once (
$_CONF['path'] . 'plugins/{plugin}/language/english.php');
}

/*
* Include {plugin} config file
*/
require_once ($_CONF['path'] . 'plugins/{plugin}/config.php');

// +---------------------------------------------------------------------------+
// | Geeklog Plugin API Implementations                                        |
// +---------------------------------------------------------------------------+

/**
* Returns the items for this plugin that should appear on the main menu
*
* NOTE: this MUST return the url/value pairs in the following format
* $<arrayname>[<label>] = <url>
*
* Universal Plugin:  Uncomment and fill in variables;
*
* @return    array   Associative array of menu items to be added to menu
*
*/
function plugin_getmenuitems_{plugin}()
{
//    global $_CONF;
//
//    $menulabel = "{plugin}";                // Put your menu label here
//    $menurul = $_CONF['site_url'] . "/{plugin}/index.php";     // Menu url here
//    
    
$menuitems = array();
//    if (SEC_hasRights('{plugin}.view')) {
//        $menuitems["$menulabel"] = $menurul;
//    }
    
return $menuitems;

}

/**
* Check to see if this Plugin uses comments
*
* Geeklog is checking to see if this plugin supports comments, tell it no!
* NOTE: to support comments you must use the same date/time based ID for your
* widget.  In other words, to make primary keys for your plugin you should call
* makesid().  Comments are keyed off of that...it is a limitation on how geeklog
* does comments.
*
* @return   boolean true or false
*
*/
function plugin_commentsupport_{plugin}()
{
    return
false;
}

/**
* Shows the statistics for the plugin on stats.php.  If
* $showsitestats is 1 then we are to only print the overall stats in the 'site
* statistics' box otherwise we show the detailed stats for the plugin
*
* Universal Plugin:  Modify/uncomment use it
*
* @param    int showsitestate   Flag to let us know which stats to get
* @return   string  returns formatted HTML to insert in stats page
*
*/
function plugin_showstats_{plugin}($showsitestats)
{
//    global $_CONF, $_TABLES;

//    $stat_templates = new Template($_CONF['path_layout'] . 'stats');
//    $stat_templates->set_file(array('itemstats'=>'itemstatistics.thtml',
//                            'statrow'=>'singlestat.thtml'));
        
$retval='';
//    if ($showsitestats == 1) {
        // This shows in the summary box
//        $total_pages=DB_count($_TABLES['table1']);     // Fill this with count for summary
//        $summary_label = "{plugin}";                   // Fill this with label displayed
        
//        $retval = "<table border = '0' width='100%' cellspacing='0' cellpadding='0'>";
//        $retval .= "<tr><td>$summary_label</td>";
//        $retval .= "<td align='right'>" . $total_pages . "&nbsp;&nbsp </td></tr></table>";
//    } else {
        // This fills the top the box  Use the following as a template
//        $result = DB_query("SELECT id, title, hits from {$_TABLES['table1'] WHERE hits > 0 ORDER BY hits desc LIMIT 10");
//        $nrows  = DB_numRows($result);
//        $retval .= COM_startBlock("Top Ten Static Pages");
//        if ($nrows > 0) {
//            $stat_templates->set_var('item_label',"Page Title");
//            $stat_templates->set_var('stat_name',"Hits");
//            for ($i = 0; $i < $nrows; $i++) {
//                $A = DB_fetchArray($result);
//                $stat_templates->set_var('item_url', $_CONF['site_url'] . '/{plugin}/index.php?item=' . $A['id']);
//                $stat_templates->set_var('item_text', $A['title']);
//                $stat_templates->set_var('item_stat', $A['hits']);
//                $stat_templates->parse('stat_row','statrow',true);
//            }
//            $stat_templates->parse('output','itemstats');
//            $retval .= $stat_templates->finish($stat_templates->get_var('output'));
//        } else {
//            $retval .= 'It appears that there are no {plugin}s on this site or no one has ever viewed them.';
//        }    
//        $retval .= COM_endBlock();
//    }
    
return $retval;
}

/**
* This will put an option for the plugin in the command and control block on moderation.php
*
* Add the plugin name, icon and link to the command and control block in moderation.php
*
* @return   array   Array containing (plugin name, admin url, url of plugin icon)
*
*/
function plugin_cclabel_{plugin}()
{
    global
$_CONF;

    return array({
plugin},$_CONF['site_admin_url'] . '/plugins/{plugin}/index.php',$_CONF['site_url'] . '/{plugin}/images/{plugin}.gif');
}

/**
* Returns the administrative option for this plugin
*
* Adds the plugin to the Admin menu
*
* Universal Plugin:  Modify/Uncomment use it
*
* @return   array   Array containing (plugin name, plugin admin url, # of items in plugin or '')
*
*/
function plugin_getadminoption_{plugin}()
{
//    global $_CONF, $_FILES;

//    if (SEC_hasRights('{plugin}.admin')) {
//        return array('{plugin}', $_CONF['site_admin_url'] . '/plugins/{plugin}/index.php', DB_count($_FILES['table1']));
//    }
}

/**
* Returns the user option for this plugin
*
* Adds the plugin to the user menu
*
* Universal Plugin:  modify/uncomment use it
*
* @return   array   Array containing (plugin name, plugin user url, 0)
*
*/
function plugin_getuseroption_{plugin}()
{
//    global $_CONF0;

//    if (SEC_hasRights('{plugin}.edit,{plugin}.view','OR')) {
//        return array('{plugin}', $_CONF['site_url'] . '/{plugin}/index.php', 0);
//    }
}

/**
* Removes the datastructures for this plugin from the Geeklog database.
* This routine will get called from the Plugin install program if user select De-Install or if Delete is used in the Plugin Editor.
* The Plugin Installer will also call this routine upon and install error to remove anything it has created.
* The Plugin installer will pass the optional parameter which will then double check that plugin has first been disabled.
*
* Returns True if all Plugin related data is removed without error
*
* @param    string   $installCheck     Defult is blank but if set, check if plugin is disabled first
* @return   boolean True if successful false otherwise
*
*/  
function plugin_uninstall_{plugin}($installCheck='')
{
    global
$_TABLES, $LANG_PL00;

    
// The {plugin}s need to be changed to the name of your plugin
    // Enter the names of your plugin table names for table1 ...
    
$pi_name='{plugin}';
    
$FEATURES = array ( '{plugin}.admin', '{plugin}.view');
    
$TABLES = array ('table1','table2');
    
    
// Check and see if plugin is still enabled - if so display warning and exit
    
if ($installCheck != '' && DB_getItem($_TABLES['plugins'],'pi_enabled', 'pi_name = "' .$pi_name. '"')) {
        
COM_errorLog("Plugin is installed and enabled. Disable first if you want to de-install it",1);
        
$display .= COM_startBlock($LANG_PL00['warning']);
        
$display .= $LANG_PL00['enabled'];
        
$display .= COM_endBlock();
        echo
$display;
        return
false;
        exit;
    }
           
    
// Ok to proceed and delete plugin - Unregister the plugin with Geeklog
    
COM_errorLog('Attempting to unregister the {plugin} plugin from Geeklog',1);
    
DB_query("DELETE FROM {$_TABLES['plugins']} WHERE pi_name = '{plugin}'",1);

    
// Drop {plugin} tables
    
foreach($TABLES as $table) {
        
$t = $_TABLES["$table"];
        
COM_errorLog("Removing Table $t",1);
        
DB_query("DROP TABLE $t",1);
    }

    
// Remove Security for this plugin
    
$grp_id = DB_getItem($_TABLES['vars'], 'value', "name = '{$pi_name}_group_id'");
    
    
COM_errorLog("Removing $pi_name Admin Group", 1);
    
DB_query("DELETE FROM {$_TABLES['groups']} WHERE grp_id = $grp_id",1);
    
DB_query("DELETE FROM {$_TABLES['vars']} WHERE name = '{$pi_name}_group_id'");
    
COM_errorLog("Removing root users from admin of $pi_name");
    
DB_query("DELETE FROM {$_TABLES['group_assignments']} WHERE ug_main_grp_id = $grp_id",1);
    
    
// Remove all the associated features -- access rights
    
foreach ($FEATURES as $feature) {
        
COM_errorLog("Removing $feature feature and rights to it",1);
        
$feat_id = DB_getItem($_TABLES['features'], 'ft_id', "ft_name = '$feature'");
        
DB_query("DELETE FROM {$_TABLES['access']} WHERE acc_ft_id = $feat_id",1);
        
DB_query("DELETE FROM {$_TABLES['features']} WHERE ft_id = $feat_id",1);
    }
        
    
COM_errorLog('...success',1);
    return
true;
}

?>