<?php

# LemurCMS by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Jan. 12, 2009

class themeManager extends functions {
	public $nav_link, $r;
	public function __construct ($id = "") {
		$this->id = $id;
		$this->db_connect();
	}
	public function arrayStaticContent () {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_template WHERE template = '".$this->getSetting('Theme')."' ORDER by id";
		$run = $this->db_query($sql)or die(mysql_error());
		while($rec = mysql_fetch_assoc($run)) {
			$r[$rec['name'].'|'.$rec['id']] = stripslashes($rec['content']);
		}
		return $r;
	}
	public function array_navigation($oneline = false) {
		$this->r = array();
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages ORDER by orderingid";
		$run = $this->db_query($sql)or die(mysql_error());
		if($oneline == true) {
			while($rec = mysql_fetch_assoc($run)) {
				if($rec['parent_id'] != "0") {
					$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE parent_id = ".$rec['parent_id']." ORDER by orderingid";
					$test = $this->db_query($sql)or die(mysql_error());
					while($row2 = mysql_fetch_assoc($test)) {
						if(mysql_num_rows($test) != "0") {
							$x = $this->r['&#8212;'.$row2['menutitle']] = $row2['id'];
						}
						else {
							$x = $this->r[$row2['menutitle']] = $row2['id'];
						}
					}
				}
				else {
					$this->r[$rec['menutitle']] = $rec['id'];
				}
			}
		}
		if($oneline == false) {
			while($rec = mysql_fetch_assoc($run)) {
				if($rec['parent_id'] == "0") {
					$this->r[$rec['menutitle']] = $rec['orderingid'].'|'.$rec['id'];	
					$this->array_navigationParent($rec['id'], $rec['menutitle']);
				}
			}
		}
	}
	public function array_navigationParent($parentid = HOME_PAGE_ID, $menutitle, $oneline = false, $depth = "2") {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE parent_id = ".$parentid." ORDER by orderingid";
		$run = $this->db_query($sql)or die(mysql_error());
			while ($rec = mysql_fetch_assoc($run)) {
				if($depth == 2) {
					$this->r[$menutitle][$rec['menutitle']] = $rec['id'];
				}
				if($depth == 3) {
					$this->r[$menutitle][$rec['menutitle']] = $rec['id'];
				}
				$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE parent_id = ".$rec['parent_id']." ORDER by orderingid";
				$test = $this->db_query($sql)or die(mysql_error());
				$num_rows = mysql_num_rows($test);
				while($row = mysql_fetch_assoc($test)) {
					if($num_rows != "0") {
//						$this->r[$menutitle][$rec['menutitle']][$row['menutitle']] = "2";	
						$this->array_navigationParent($row['id'], $row['menutitle'], false, "3");
					}
				}
			}
			//return $x;
	}
	public function buildNavArray($oneline = false) {
		$this->array_navigation($oneline);
		return $this->r;
	}
	public function navigation ($ul_id ="ul-navigation", $sub_ul_class ="ul-sub-navigation", $lis = "selected", $link = true, $one_row = false, $array = false) {
		$pageid = $this->page_info['id'];
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages ORDER by orderingid, menutitle";
		$run = $this->db_query($sql);
		$cleanUrls = $this->getSetting("Clean Urls");
		if($one_row == true) {
			$r .= "\n<ul id=\"".$ul_id."\">";
			$x = 0;
			while ($rec = mysql_fetch_assoc($run)) {
				$r .= "\n\t<li id='item_$x'";
				if($this->id == $rec['id']) {
					$r .= " class='$lis'";
				}
				$r .= ">";
				if($link == true) {
					if($cleanUrls == "Enabled") {
						$r .= "<a href='".THEME_URL."content/".make_clean_url($rec['title'])."/".$rec['id']."'>";
					}
					else {
						$r .= "<a href='".THEME_URL."?page=".$rec['id']."'>";					
					}
				}
				$r .= $rec['menutitle'];
				if($link == true) {
					$r .= "</a>";
				}
				$r .= '</li>';
				$x++;
			}
			$r .= "\n</ul>\n";		
			return $r;	
		}
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages ORDER by orderingid, menutitle";
		$run = $this->db_query($sql);
			if($array == true) {
				while ($rec = mysql_fetch_assoc($run)) {
					$r[$rec['menutitle']] .= $rec['title']."&&||&&".$rec['content'];
					
				}
			}
		$r .= "\n<ul id=\"".$ul_id."\">";
		$xy = 0;
		while ($rec = mysql_fetch_assoc($run)) {
			if($rec['parent_id'] == "0") {
				$r .= "\n\t<li id='node$xy'";
				if($this->id == $rec['id']) {
					$r .= " class='$lis'";
				}
				$r .= ">";
				if($link == true) {
					if($cleanUrls == "Enabled") {
						$r .= "<a href='".THEME_URL."content/".make_clean_url($rec['title'])."/".$rec['id']."'>";
					}
					else {
						$r .= "<a href='".THEME_URL."?page=".$rec['id']."'>";					
					}
				}
				$r .= $rec['menutitle'];
				if($link == true) {
					$r .= "</a>";
				}
				$r .= $this->navigationParent($rec['id'], $sub_ul_class, $lis, $link, false, $xy).'</li>';
				$xy++;
			}
		}
		$r .= "\n</ul>\n";		
		return $r;
	}
	public function navigationParent ($parentid = HOME_PAGE_ID, $class="ul-sub-navigation", $lis = "selected", $link = true, $array = false, $q) {
		$cleanUrls = $this->getSetting("Clean Urls");
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE parent_id = ".$parentid." ORDER by orderingid, menutitle";
		$run = $this->db_query($sql);
		$x = mysql_num_rows($run);
		if($x !== 0) {
			$r .= "\n\t\t<ul class='".$class."'>";
		}
		$qz = 0;
		while ($rec = mysql_fetch_assoc($run)) {
			$r .= "\n\t\t\t<li id='node$q-$qz'";
			if($this->id == $rec['id']) {
				$r .= " class='".$lis."'";
			}
			$r .= ">";
				if($link == true) {
					if($cleanUrls == "Enabled") {
						$r .= "<a href='".THEME_URL."content/".make_clean_url($rec['title'])."/".$rec['id']."'>";
					}
					else {
						$r .= "<a href='".THEME_URL."?page=".$rec['id']."'>";					
					}
				}
			$r .= $rec['menutitle'];
			if($link == true) {
				$r .= "</a>";
			}
			$qz++;
			$r .= $this->navigationParent($rec['id'], $class, $lis, $link, false, $q).'</li>';
		}
		if($x !== 0) {
			$r .= "\n\t\t</ul>";
		}
		return $r;
	}
	public function static_content ($editable, $default) {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_template WHERE name = '".$editable."' AND template = '".$this->getSetting('Theme')."' ORDER by id";
		$run = $this->db_query($sql) or die(mysql_error());
		$rec = mysql_fetch_assoc($run);
		if(mysql_num_rows($run) == 0) {
			$sql = "INSERT INTO ".DBTABLEPREFIX."content_template (name, content, template) VALUES ('".$editable."', '".$default."', '".$this->getSetting('Theme')."')";
			$run = $this->db_query($sql) or die(mysql_error());
			$rec['content'] = $default;
		}
		$r = $rec['content'];
		return $r;
	}
	public function buildNavigation () {
		
	}
}
?>