<?php

/* -------------------------------------------------------------------
 *
 * XS Developments : Database routines
 * Copyright (c) 2011 XS developments. All Rights Reserved.
 * Author: Roy Sherfan
 *
 * Synopsis: These routines make connections to a database.
 *
 */

$dbopen = false;


// renders everything in the messages table if it exists in the text.
function xsdb_render($thisString) {
	global $dbsql;
	$tmpstr = $thisString;
	// make sure string aint blank
   	if (strlen($thisString)>8) {
    	$result = mysqli_query($dbsql,"SELECT * from `messages`;")
        or die(mysqli_error());
    	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
			$tmpstr = str_replace($row{'Name'}, $row{'Description'}, $tmpstr);
    	}
	}
	return $tmpstr;
}

function xsdb_getpagebody($thispage) {
	global $dbsql;
	$result = mysqli_query($dbsql,"SELECT * from `pages` where `SEO_ID`='".$thispage."';")
    or die(mysqli_error());
	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
		  $mybody = "".$row{'Body'}."";
	}
	return $mybody; 
}

// renders everything in the messages table if it exists in the text.
function xsdb_getemail($thisString) {
	global $dbsql;
	$tmpstr = $thisString;
	// make sure string aint blank
   	if (strlen($thisString)>3) {
    	$result = mysqli_query($dbsql,"SELECT * from `messages` where Name='".$thisString."';")
        or die(mysqli_error());
    	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
			$tmpstr = $row{'Description'};
    	}
	}
	return $tmpstr;
}

// renders everything in the messages table if it exists in the text.
function xsdb_getmessage($thisString) {
	global $dbsql;
	$tmpstr = $thisString;
	// make sure string aint blank
   	if (strlen($thisString)>8) {
    	$result = mysqli_query($dbsql,"SELECT * from `messages` where Name='".$thisString."';")
        or die(mysqli_error());
    	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
			$tmpstr = $row{'Description'};
    	}
	}
	return $tmpstr;
}

// grabs the background image background AND titlearea text.
function xsdb_getpage_attributes($thispage) {
	global $dbsql, $PAGE_Name, $PAGE_TitleArea, $PAGE_Banner, $PAGE_BannerImage, $PAGE_Body;
	global $PAGE_Image1, $PAGE_Image2, $PAGE_Image3, $PAGE_Image4;
	global $PAGE_Video1, $PAGE_Video2, $PAGE_Video3, $PAGE_Video4;
	global $PAGE_Caption1, $PAGE_Caption2, $PAGE_Caption3, $PAGE_Caption4;
	$result = mysqli_query($dbsql,"SELECT * from `pages` where `SEO_ID`='".$thispage."';")
    or die(mysqli_error());
	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
		  $PAGE_Name = "".$row{'Name'}."";
		  $PAGE_TitleArea = "".$row{'TitleArea'}."";
		  $PAGE_BannerImage = "".$row{'Banner'}."";
		  $PAGE_Banner = "".$row{'BannerImage'}."";

		  $PAGE_Body = "".$row{'Body'}."";
		  $PAGE_Image1 = "".$row{'Image1'}."";
		  $PAGE_Image2 = "".$row{'Image2'}."";
		  $PAGE_Image3 = "".$row{'Image3'}."";
		  $PAGE_Image4 = "".$row{'Image4'}."";

		  $PAGE_Video1 = "".$row{'Video1'}."";
		  $PAGE_Video2 = "".$row{'Video2'}."";
		  $PAGE_Video3 = "".$row{'Video3'}."";
		  $PAGE_Video4 = "".$row{'Video4'}."";

  		  $PAGE_Caption1 = "".$row{'Caption1'}."";
  		  $PAGE_Caption2 = "".$row{'Caption2'}."";
  		  $PAGE_Caption3 = "".$row{'Caption3'}."";
  		  $PAGE_Caption4 = "".$row{'Caption4'}."";
	}
}


function xsdb_open($thisdsn) {
	global $dbsql, $dbhost, $dbuser, $dbpass, $dbopen;
	$dbsql = mysqli_connect($dbhost, $dbuser, $dbpass);
	if (!$dbsql)
	{
		die('Could not connect: ' . mysqli_error());
	} else {
		$dbopen = true;
	}
	mysqli_select_db($dbsql, $thisdsn);
}



function xsdb_close() {
	global $dbsql, $dbopen;
	if ($dbopen) {
		mysqli_close($dbsql);
		$dbopen = false;
	}
}



function xsdb_getpage($thispage) {
	global $dbsql;
	$result = mysqli_query($dbsql,"SELECT * from `pages` where `SEO_ID`='".$thispage."';")
    or die(mysqli_error());
	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
	print "".$row{'Body'}."";
	}
}


function xsdb_getpageSEO($thisSEOid) {
	global $dbsql, $SEO_Description, $SEO_Keywords, $SEO_Title, $SEO_ID, $SEO_Robots, $SEO_Revisit, $SEO_Googlebot, $SEO_Copyright, $SEO_Rating;

	$result = mysqli_query($dbsql,"SELECT * from `pages` where `SEO_ID`='".$thisSEOid."';")
    or die(mysqli_error());
	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    	$SEO_Description = $row{'SEO_Description'};
    	$SEO_Keywords = "".$row{'SEO_Keywords'}."";
    	$SEO_Title = "".$row{'SEO_Title'}."";
    	$SEO_ID = "".$row{'SEO_ID'}."";
    	$SEO_Robots = "".$row{'SEO_Robots'}."";
    	$SEO_Revisit = "".$row{'SEO_Revisit'}."";
    	$SEO_Googlebot = "".$row{'SEO_Googlebot'}."";
    	$SEO_Copyright = "".$row{'SEO_Copyright'}."";
    	$SEO_Rating = "".$row{'SEO_Rating'}."";
	}
}


function display_index($thisquery, $thisURL, $thisstyle, $showdate) {
	global $dbsql;
	$myoutput = "";
	$result = mysqli_query($dbsql,$thisquery)
    or die(mysqli_error());
	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
		  $myURL = $row{'URL'};
		  $myTitle = $row{'Title'};
		  $myDate = $row{'theDate'};
	$myoutput = $myoutput."<div style=\"".$thisstyle."\"><A HREF=\"".$thisURL.$myURL."\">".$myTitle."</A> [TDATE] </div>\n";
	if ($showdate=='Y') { $myoutput = str_replace("[TDATE]", date_format(timezoneadjust($myDate),"jS M Y"), $myoutput); }	
	}
    return $myoutput;
}



function xsdb_getnewsSEO($thisSEOid) {
	global $dbsql, $SEO_Description, $SEO_Keywords, $SEO_Title, $SEO_ID, $SEO_Robots, $SEO_Revisit, $SEO_Googlebot, $SEO_Copyright, $SEO_Rating;
	global $news_date, $news_Body, $news_Image1, $news_Image2, $news_Image3, $news_Image4, $news_PDF;

	$result = mysqli_query($dbsql,"SELECT * from `news` where `SEO_ID`='".$thisSEOid."';")
    or die(mysqli_error());
	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
		$news_date   = $row{'PublishDate'};
		$news_Body   = $row{'Body'};
		$news_Image1 = $row{'Image1'};
		$news_Image2 = $row{'Image2'};
		$news_Image3 = $row{'Image3'};
		$news_Image4 = $row{'Image4'};
		$news_PDF	 = $row{'PDF'};
    	$SEO_Description = $row{'SEO_Description'};
    	$SEO_Keywords = "".$row{'SEO_Keywords'}."";
    	$SEO_Title = "".$row{'SEO_Title'}."";
    	$SEO_ID = "".$row{'SEO_ID'}."";
    	$SEO_Robots = "".$row{'SEO_Robots'}."";
    	$SEO_Revisit = "".$row{'SEO_Revisit'}."";
    	$SEO_Googlebot = "".$row{'SEO_Googlebot'}."";
    	$SEO_Copyright = "".$row{'SEO_Copyright'}."";
    	$SEO_Rating = "".$row{'SEO_Rating'}."";
	}
}

function xsdb_geteventsSEO($thisSEOid) {
	global $dbsql, $SEO_Description, $SEO_Keywords, $SEO_Title, $SEO_ID, $SEO_Robots, $SEO_Revisit, $SEO_Googlebot, $SEO_Copyright, $SEO_Rating;
	global $events_date, $events_Body, $events_Image1, $events_Image2, $events_Image3, $events_Image4, $events_PDF;

	$result = mysqli_query($dbsql,"SELECT * from `events` where `SEO_ID`='".$thisSEOid."';")
    or die(mysqli_error());
	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
		$events_date   = $row{'PublishDate'};
		$events_Body   = $row{'Body'};
		$events_Image1 = $row{'Image1'};
		$events_Image2 = $row{'Image2'};
		$events_Image3 = $row{'Image3'};
		$events_Image4 = $row{'Image4'};
		$events_PDF	 = $row{'PDF'};
    	$SEO_Description = $row{'SEO_Description'};
    	$SEO_Keywords = "".$row{'SEO_Keywords'}."";
    	$SEO_Title = "".$row{'SEO_Title'}."";
    	$SEO_ID = "".$row{'SEO_ID'}."";
    	$SEO_Robots = "".$row{'SEO_Robots'}."";
    	$SEO_Revisit = "".$row{'SEO_Revisit'}."";
    	$SEO_Googlebot = "".$row{'SEO_Googlebot'}."";
    	$SEO_Copyright = "".$row{'SEO_Copyright'}."";
    	$SEO_Rating = "".$row{'SEO_Rating'}."";
	}
}


function xsdb_getpage_padded($thispage, $thispadding) {
	global $dbsql;
	$result = mysqli_query($dbsql,"SELECT * from `pages` where `SEO_ID`='".$thispage."';")
    or die(mysqli_error());
	while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
	?>
	<table border="0" cellspacing="0" cellpadding="<?=$thispadding?>"><tr><td>
    	<?=$row{'Body'};?>
	</td></tr></table>
	<?
	}
}



function xsdb_runSQL($thisSQL) {
	global $dbsql;
	$result = mysqli_query($dbsql,$thisSQL)
	  or die(mysqli_error());
}



?>