Файл: core/recursion.php
Строк: 248
<?php
/*
* @author mides <Mike Osendowski>
* @link http://midwm.org
* @copyright 2011-2014
*/
class countPosts {
private $module;
private $parentid;
public function __construct($module, $parentid)
{
$this->module = $module;
$this->parentid = $parentid;
}
public function countChildren($countThreads = 0) {
if (!isset($children)) $children = FALSE;
$tableName = $countThreads ? '_threads' : '_posts';
$total = DB::run()->querySingle("select count(`id`) from `".$this->module."_categs` where `parentid` = ?;", array($this->parentid));
if ($total)
{
$this->countChildsChildren($this->parentid, $countThreads);
$this->children[] = DB::run()->querySingle("select count(`id`) from `".$this->module.$tableName."` where `categid` = ?;", array($this->parentid));
}
else
{
//$tableName = $countThreads ? '_threads' : '_posts';
$this->children = DB::run()->querySingle("select count(`id`) from `".$this->module.$tableName."` where `categid` = ?;", array($this->parentid));
}
}
public function countChildsChildren($parentid, $countThreads = 0) {
$tableName = $countThreads ? '_threads' : '_posts';
$query = DB::run()->query("select * from `".$this->module."_categs` where `parentid` = ?;", array($parentid));
while ($row = $query->Fetch())
{
$countChildren = DB::run()->querySingle("select count(`id`) from `".$this->module.$tableName."` where `categid` = ?;", array($row['id']));
$this->children[] = $countChildren;
/*$subcategsCount = DB::run()->querySingle("select count(`id`) from `".$this->module."_categs` where `parentid` = ?;", array($row['id']));
if ($subcategsCount)
{
}*/
$this->countChildsChildren($row['id'], $countThreads);
}
/*$subcategsCount = DB::run()->querySingle("select count(`id`) from `".$this->module."_categs` where `parentid` = ?;", array($parentid));
if ($subcategsCount)
{
//$categ = DB::run()->queryFetch("select `parentid` from `".$this->module."_categs` where `id` = ?;", array($parentid));
$countChildren2 = DB::run()->querySingle("select count(`id`) from `".$this->module.$tableName."` where `categid` = ?;", array($parentid));
//$this->children[] = $countChildren2;
}*/
}
public function output()
{
if (is_array($this->children))
{
return array_sum($this->children);
}
else
{
return $this->children;
}
}
}
function countPosts($parentid) {
global $module;
$countPosts = new countPosts($module, $parentid);
$countPosts->countChildren();
return $countPosts->output();
}
// for module forum only
function countThreads($parentid) {
global $module;
$countThreads = new countPosts($module, $parentid);
$countThreads->countChildren(1);
return $countThreads->output();
}
// $node is the name of the node we want the path of
function getPath($nodeid) {
global $module;
// look up the parent of this node
$row = DB::run()->queryFetch("select `parentid` from `".$module."_categs` where `id` = ?;", array($nodeid));
// save the path in this array
$path = array();
// only continue if this $node isn't the root node
// (that's the node with no parent)
if ($row['parentid'] > 0) {
// the last part of the path to $node, is the name
// of the parent of $node
$path[] = $row['parentid'];
// we should add the path to the parent of this node
// to the path
$path = array_merge(getPath($row['parentid']), $path);
}
// return the path
return $path;
}
function showPath($path, $acp = FALSE) {
global $module;
$return = FALSE;
foreach ($path as $value)
{
$categ = DB::run()->queryFetch("select `name` from `".$module."_categs` where `id` = ?;", array($value));
$return .= ' » <a href="'.$acp.'/'.$module.'/viewcateg'.$value.'">'.$categ['name'].'</a>';
}
return $return;
}
function getfpath($nodeid) {
global $module;
// look up the parent of this node
$row = DB::run()->queryFetch("select `parentid` from `".$module."_categs` where `id` = ?;", array($nodeid));
// save the path in this array
$path = array();
// only continue if this $node isn't the root node
// (that's the node with no parent)
if ($row['parentid'] > 0) {
// the last part of the path to $node, is the name
// of the parent of $node
$path[] = $row['parentid'];
// we should add the path to the parent of this node
// to the path
$path = array_merge(getfpath($row['parentid']), $path);
}
// return the path
return $path;
}
function showfpath($path) {
global $module;
$return = FALSE;
foreach ($path as $value)
{
$categ = DB::run()->queryFetch("select `foldername` from `".$module."_categs` where `id` = ?;", array($value));
$return .= $categ['foldername'].'/';
}
return $return;
}
function fpath($categid, $link = 0) {
global $module;
$path = getfpath($categid);
$return = FALSE;
foreach ($path as $value)
{
$categ = DB::run()->queryFetch("select * from `".$module."_categs` where `id` = ?;", array($value));
$return .= $link ? ' » <a href="/'.$module.'/viewcateg'.$categ['id'].'">'.$categ['name'].'</a>' : $categ['foldername'].'/';
}
return $return;
}
/*function path2($categid, $complete = 0) {
global $module;
$categ = DB::run()->queryFetch("select * from `".$module."_categs` where `id` = ?;", array($categid));
if ($categ)
{
if ($categ['parentid'])
{
return showfpath(getfpath($categ['id'])).($complete ? $categ['foldername'].'/' : FALSE);
}
else
{
return $complete ? $categ['foldername'].'/' : FALSE;
}
}
return FALSE;
}*/
function displayCategs($parentid, $level, $list = 0, $currentCategid = 0) {
global $module;
// retrieve all children of $parent
$query = DB::run()->query("select `id`, `name` from `".$module."_categs` where `parentid` = ? order by `order`;", array($parentid));
while ($categ = $query->Fetch())
{
// indent and display the title of this child
if ($list)
{
if ($list == 1)
{
$selected = $currentCategid == $categ['id'] ? ' selected="selected"' : FALSE;
}
else
{
$selected = access('forum'.$categ['id'], $currentCategid) ? ' selected="selected"' : FALSE;
}
echo '<option value="'.$categ['id'].'"'.$selected.'>'.str_repeat('-', $level).' '.$categ['name'].'</option>';
}
else
{
echo str_repeat('-', $level).' <a href="/'.$module.'/viewcateg'.$categ['id'].'"><b>'.$categ['name'].'</b></a> <a href="sortcateg'.$categ['id'].'/up"><img src="{IMGPATH}up.png" alt="up"></a> <a href="sortcateg'.$categ['id'].'/down"><img src="{IMGPATH}down.png" alt="down"></a> <a href="editcateg'.$categ['id'].'">{LANG.Edit_}</a> <a href="delcateg'.$categ['id'].'">{LANG.DEL}</a><br>';
}
// call this function again to display this
// child's children
displayCategs($categ['id'], $level + 1, $list, $currentCategid);
}
}
?>