Location: PHPKode > scripts > Noostr > _engine/includes/stories.php
<?php defined('SYSPATH') OR die('No direct access to this file is allowed.');

function get_ranked_stories($start = 0) {
	//return get_nodes($start, -1, null, 'ranking desc, submissionDate desc');
	return get_nodes(0, -1, get_classid('story'), null, true);
}

function get_stories($start = 0) {
	//return get_nodes($start, -1);
	return get_nodes(0, -1, get_classid('story'));
}

function get_story($uid = null) {
	global $ns;
	$return = array();
	if (!isset($uid)) {
		global $site;
		if (isset($site->uri[1])) {
			$uid = $site->uri[1];
		} else {
			$uid = 0;
		}
	}
	$return = get_nodes(0, 1, get_classid('story'), $uid);
	if (!is_array($return)) {
		$return = array(0 => $return);
	}
	if (count($return) < 1) {
		$return[0] = null;
	} else {
		$ns->story = $return[0];
	}
	return $return[0];
}

function get_comment($uid = null) {
	global $ns;
	$return = array();
	if (!isset($uid)) {
		global $site;
		if (isset($site->uri[1])) {
			$uid = $site->uri[1];
		} else {
			$uid = 0;
		}
	}
	$return = get_nodes(0, 1, get_classid('comment'), $uid);
	if (!is_array($return)) {
		$return = array(0 => $return);
	}
	if (count($return) < 1) {
		$return[0] = null;
	} else {
		$return[0]['children'] = '';
		$ns->comment = $return[0];
	}
	return $return[0];
}

function get_comments($lft, $rgt, $start = 0) {
	$return = array();
	$return = get_nodes(0, -1, get_classid('comment'), null, true, $lft, $rgt);
	if (!is_array($return)) {
		$return = array($return);
	}
	return arrange_comment_threads($return, $lft, $rgt);
}

function arrange_comment_threads(&$mixed, $lft, $rgt) {
	$return = false;
	for ($i = 0, $c = count($mixed); $i < $c; $i++) {
		if ($mixed[$i]['lft'] > $lft && $mixed[$i]['rgt'] < $rgt && !isset($mixed[$i]['used']) && $mixed[$i]['deleted'] == 0) {
			$return[$i] = $mixed[$i];
			$mixed[$i]['used'] = true;
			if ($mixed[$i]['children'] > 0) {
				$return[$i]['children'] = arrange_comment_threads($mixed, $mixed[$i]['lft'], $mixed[$i]['rgt']);
			} else {
				$return[$i]['children'] = false;
			}
		}
	}
	if (is_array($return)) {
		$return = array_merge($return);
	}
	return $return;
}

function get_nodes($start = 0, $end = null, $class = null, $uid = null, $ranked = false, $lft = null, $rgt = null, $mod = false) {
	global $db, $site, $user, $timerstart;
	$return = null;
	$data = array();
	$customfields = '';
	$customwhere = '';
	if (!isset($end)) {
		$end = $site->storiesperpage;
	} else {
		if ($end == -1) {
			$start = null;
			$end = null;
		}
	}
	if (!isset($lft)) {
		$lft = 0;
	}
	if ($ranked) {
		// Default to Noostr ranking, in case no default is selected.
		$ranking = 'cast(pow(%v / (%t + 2), %g) as decimal(16, 6))';
		$sql = 'select algorithm from '.PREFIX.'settings_ranking where `default` = 1 limit 0, 1';
		$result = $db->query($sql);
		if (is_array($result) && isset($result[0])) {
			$ranking = strtolower($result[0]['algorithm']);
		}
		// Check for '%t' algorithm var, as it requires an extra query.
		$nodecount = 0;
		if (strpos($ranking, '%t') !== false) {
			$sql = 'select stories from '.PREFIX.'classes where classid = '.$class;
			$result = $db->query($sql);
			if (is_array($result) && isset($result[0])) {
				$nodecount = $result[0]['stories'];
			}
		}
		// Now find algorithm vars and replace with SQL code
		$avars = array('%v', '%t', '%h', '%g');
		$avals = array('n.votes', $nodecount, 'timestampdiff(HOUR, from_unixtime(n.submissionDate), NOW())', $site->multiplier);
		$ranking = str_ireplace($avars, $avals, $ranking);
		$customfields .= ', '.$ranking.' as ranking';
		if (isset($rgt)) {
			$customfields .= ', (COUNT(p.uid) - 1) AS depth';
		}
	}
	if (isset($uid)) {
		$customwhere .= ' and n.uid = ?';
	}
	if (!$mod) {
		$customwhere .= ' and n.flags < ?';
	}
	$customwhere .= ' and n.deleted = 0';
	$customwhere .= ' and (n.publishDate is null or '.$timerstart.' >= n.publishDate)';
	$customwhere .= ' and (n.expiryDate is null or '.$timerstart.' <= n.expiryDate)';
	$sql = 'select n.*, cast(((n.rgt - n.lft - 1) / 2) as unsigned) as children, u.nick, v.direction, f.flagid'.$customfields.' from ';
	if (isset($rgt)) {
		$sql .= PREFIX.'nodes p, '.PREFIX.'nodes n left join '.PREFIX.'users u on n.userid = u.uid left join '.PREFIX.'users_votes v on v.nodeid = n.uid and v.userid = ? left join '.PREFIX.'users_flags f on f.nodeid = n.uid and f.userid = ? where n.lft BETWEEN p.lft AND p.rgt and n.lft between ? and ?'.$customwhere.' group by n.uid order by ';
		$data[] = $user->uid;
		$data[] = $user->uid;
		$data[] = $lft;
		$data[] = $rgt;
	} else {
		$sql .= PREFIX.'nodes n left join '.PREFIX.'users u on n.userid = u.uid left join '.PREFIX.'users_votes v on v.nodeid = n.uid and v.userid = ? left join '.PREFIX.'users_flags f on f.nodeid = n.uid and f.userid = ? where n.lft > ? and n.classid = ?'.$customwhere.' order by ';
		$data[] = $user->uid;
		$data[] = $user->uid;
		$data[] = $lft;
		$data[] = $class;
	}
	if (isset($uid)) {
		$data[] = $uid;
	}
	if (!$mod) {
		$data[] = $site->flaghidethreshhold_story;
	}
	if (isset($rgt)) {
		$sql .= 'depth asc, ';
	}
	if ($ranked) {
		$sql .= 'ranking desc, ';
	}
	$sql .= 'n.publishDate desc';
	//echo $sql."<br />\n";
	//print_r($data);
	$return = $db->query($sql, $data, $start, $end);
	return $return;
}

function get_classid($class) {
	global $db;
	$return = null;
	$sql = 'select uid from '.PREFIX.'classes where stub = ?';
	$result = $db->query($sql, strtolower($class));
	if (is_array($result) && isset($result[0])) {
		$return = $result[0]['uid'];
	}
	return $return;
}

function get_flagid($flag) {
	global $db;
	$return = null;
	$sql = 'select uid from '.PREFIX.'flags where stub = ?';
	$result = $db->query($sql, strtolower($flag));
	if (is_array($result) && isset($result[0])) {
		$return = $result[0]['uid'];
	}
	return $return;
}

function vote($ownerid, $direction) {
	global $user, $acl;
	$return = null;
	if ($ownerid == $user->uid) {
		$return = 'owner';
	} elseif (($ownerid != $user->uid && $acl->vote && $direction == 0) || $user->uid == null) {
		$return = 'show';
	} else {
		$return = 'hide';
	}
	return $return;
}
Return current item: Noostr