<?php

if ( ! @include_once( './mainfile.php' ) ) { exit( '!! INCLUDE ERROR. ACCESS DENIED !!' ); }
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: class.myfunctions.php,v 1.4.2.2 2004/04/18 12:45:25 ajvwaay Exp $



class MyFunctions
{


var $table = ""; //table with parent-child structure
var $id = ""; //unique id for records in table $table
var $pid = "pid";  // name of parent id used in table $table
var $itemtable = "";  //items table with each item associated with an id in table $table
var $itemid=""; //unique id for records in table $itemtable
var $title ="title"; // name of a column in table $table which will be used for ordering queried results and creating selection box
var $ratingtable=""; // table storing rating information for each item in itemtable


// returns an array of all first child of a given id
function getFirstChild($sel_id){
    $arr =array();
    $result = mysql_query("select * from ".$this->table." where ".$this->pid."=".$sel_id."");
    $count = mysql_num_rows($result);
    if($count==0){
        return 0;
    }
    while($myrow=mysql_fetch_object($result)){
        array_push($arr, $myrow);
    }
    return $arr;
}

//returns an array of all child category ids for a given table $table id
function getAllChildId($sel_id,$idarray = array()){
    $result=mysql_query("SELECT ".$this->id." FROM ".$this->table." WHERE ".$this->pid."=".$sel_id." ORDER BY ".$this->title."");
    $count = mysql_num_rows($result);
    if($count==0){
        return $idarray;
    }
    while(list($r_id)=mysql_fetch_row($result)){
        array_push($idarray, $r_id);
        $idarray=$this->getAllChildId($r_id,$idarray);
    }
    return $idarray;
}


//returns the total number of items in items table that are accociated with a given table $table id
function getTotalItems($sel_id, $status=""){
    $count = 0;
    $arr = array();
    $query = "select count(*) from ".$this->itemtable." where ".$this->id."=".$sel_id."";
    if($status!=""){
        $query .= " and status>=$status";
    }
    $result = mysql_query($query);
    list($thing) = mysql_fetch_row($result);
    $count = $thing;
    $arr = $this->getAllChildId($sel_id);
    for($i=0;$i<sizeof($arr);$i++){
        $query2 = "select count(*) from ".$this->itemtable." where ".$this->id."=$arr[$i]";
        if($status!=""){
            $query2 .= " and status>=$status";
        }
        $result2 = mysql_query($query2);
        list($thing) = mysql_fetch_row($result2);
        $count += $thing;
    }
    return $count;
}


//generates path from the root id to a given id
// the path is delimetered with "/"
function getPathFromId($sel_id, $path=""){
    $result = mysql_query("select ".$this->pid.", ".$this->title." from ".$this->table." where ".$this->id."=$sel_id");
    list($parentid,$name)=mysql_fetch_row($result);

    $name=stripslashes($name);
    if(mysql_num_rows($result)==0){
        return $path;
    }
    $path = "/".$name.$path."";
    $path = $this->getPathFromId($parentid, $path);
    return $path;
}


//makes a nicely ordered selection box
//$preset_id is used to specify a preselected item
function makeMySelBox($preset_id=0){
    echo "<select name=\"".$this->id."\">";
    $result=mysql_query("select ".$this->id.", ".$this->title." from ".$this->table." where ".$this->pid."=0 order by ".$this->title."");
    while(list($catid, $name)=mysql_fetch_row($result)){
        if($catid==$preset_id){
            $sel = " selected";
        }
        echo "<option value=\"$catid\"$sel>$name</option>";
        $sel = "";
        $arr=$this->getAllChildId($catid);
        for($i=0;$i<sizeof($arr);$i++){
            $catpath = $this->getPathFromId($arr[$i]);
            $catpath = substr($catpath, 1);
            if($arr[$i]==$preset_id){
                $sel = " selected";
            }
            echo "<option value=\"$arr[$i]\"$sel>$catpath</option>";            $sel = "";
        }
    }
    echo "</select>";
}


//generates nicely formatted linked path from the root id to a given id
function getNicePathFromId($sel_id, $funcURL, $path=""){
    $result = mysql_query("select ".$this->pid.", ".$this->title." from ".$this->table." where ".$this->id."=$sel_id");
    list($parentid,$name)=mysql_fetch_row($result);
    if(mysql_num_rows($result)==0){
        return $path;
    }

    $name=stripslashes($name);
    $path = "<a href=\"".$funcURL."&".$this->id."=".$sel_id."\">".$name."</a>&nbsp;:&nbsp;".$path."";
    $path = $this->getNicePathFromId($parentid, $funcURL, $path);
    return $path;
}

//updates rating data in itemtable for a given item
function updaterating($sel_itemid){
    $query = "select rating FROM ".$this->ratingtable." WHERE ".$this->itemid." = $sel_itemid";
    $voteresult = mysql_query($query);
        $votesDB = mysql_num_rows($voteresult);
    $totalrating = 0;
        while(list($rating)=mysql_fetch_row($voteresult)){
        $totalrating += $rating;
    }
    $finalrating = $totalrating/$votesDB;
    $finalrating = number_format($finalrating, 4);
    $query =  "UPDATE ".$this->itemtable." SET rating=$finalrating, votes=$votesDB WHERE ".$this->itemid." = $sel_itemid";
        mysql_query($query);
}



}


?>
