<commit_before><?php
/**
 * Does all the heavy lifting for themes
 * @author <NAME> <<EMAIL>>
 * 
 * 
 */
class TemplateEngine
{

	private $database = null;
	private $router = null;
	private $pageData = null;
	private $postCategory = null;
	private $postTag = null;
	private $arrayPosition = 0;
	private $errorText = null;
	private $pageDataCt = null;
	private $siteTitle = "";
	private $siteDescription = "";
	private $themeName = "";
	private $siteUrl = "";
	private $postFormat = "";
	private $postsPerPage = 0;
	private $baseLocation = "";
	private $templateData = null;
	private $currentUrl = null;
	private $siteBaseUrl = null;
	
	
	
	/**
	 * __construct function.
	 * 
	 * @access public
	 * @param GenericDatabase $database
	 * @param Router $router
	 * @param mixed $siteTitle
	 * @param mixed $siteDescription
	 * @param mixed $themeName
	 * @param mixed $siteUrl
	 * @param mixed $postsPerPage
	 * @param mixed $baseLocation
	 * @param TemplateData $templateData
	 * @return void
	 */
	public function __construct(GenericDatabase $database, Router $router, $siteTitle, $siteDescription, $themeName, SiteUrlGenerator $siteUrlGenerator, $postsPerPage, $baseLocation, TemplateData $templateData)
	{
		$this->database = $database;
		$this->router = $router;
		$this->siteTitle = $siteTitle;
		$this->siteDescription = $siteDescription;
		$this->themeName = $themeName;
		$this->postsPerPage = (int)$postsPerPage;
		$this->baseLocation = $baseLocation;
		$this->siteUrl = $siteUrlGenerator->generateSiteUrl();
		$this->templateData = $templateData;
		$this->currentUrl = $siteUrlGenerator->currentAddressWithoutPageUrl();
		$this->siteBaseUrl = $siteUrlGenerator->generateSiteBaseUrl();
	}
	
	
	/**
	 * processTemplateData function.
	 * 
	 * Grabs the already processed TemplateData and sets it up to be the pageData and gets a count of items in the array
	 * @access public
	 * @return void
	 */
	public function processTemplateData()
	{
		$this->pageData = $this->templateData->getData();
		$this->pageDataCt = count($this->pageData);
	}
	
	/**
	 * getPageData function.
	 * 
	 * Returns the array of page data.
	 * @access public
	 * @return array|null array of page data or null if nothing exists
	 */
	public function getPageData()
	{
		return $this->pageData;
	}
	
	/**
	 * haveNextPost function.
	 * 
	 * Returns true if there are more posts to display, false if there are no new posts
	 * if there is a next post it moves to that one so you can call the rest of the functions to get it's info
	 * @access public
	 * @return boolean True if there are more posts or false if there are none left
	 */
	public function haveNextPost()
	{
		$return = null;
		// since we are starting at 0 so everything else works we need this to start at -1 so that we don't miss the first page when we are in a loop
		// this allows us to not have to call other functions when we are doing a page/single post
		// because haveNextPost is called before the first post we need to make this one less than it is since we are going to increment it anyway
		// the static value remembers what it was set at the previous run
		static $runOnce = false;
		
		if(!$runOnce)
		{
			$this->arrayPosition--;
		}
		
		
		if($this->arrayPosition + 1 < $this->pageDataCt && $this->pageDataCt != 0)
		{
			$this->arrayPosition++;
			$return = true;
			$runOnce = true;
		}
		else
			$return = false;
			
		return $return;
	}
	
	/**
	 * getPostTitle function.
	 * 
	 * Returns the title of the current post.
	 * @access public
	 * @return string|null string if the title exists or null if it doesn't
	 */
	public function getPostTitle()
	{
		$return = null;
		
		if(isset($this->pageData[$this->arrayPosition]["Title"]))
		{
			$return = $this->pageData[$this->arrayPosition]["Title"];
		}
		return $return;
	}
	
	/**
	 * getPostUri function.
	 * 
	 * Returns only the URI of the post
	 * @access public
	 * @return string|null string if the post has a uri or null if it doesn't
	 */
	public function getPostUri()
	{
		$return = null;
		
		if(isset($this->pageData[$this->arrayPosition]["URI"]))
		{
			$return = $this->pageData[$this->arrayPosition]["URI"];
		}
		return $return;
	}
	
	/**
	 * getPostUrl function.
	 * 
	 * Returns the full URI of a post
	 * @access public
	 * @return string|null URL of the post or null if no post
	 */
	public function getPostUrl()
	{

		if(isset($this->pageData[$this->arrayPosition]["URI"]))
		{
			$return = $this->siteUrl . $this->pageData[$this->arrayPosition]["URI"];
		}
		else
		{
			$return = null;
		}
		
		return $return;
	}
	
	/**
	 * getHtmlTitle function.
	 * 
	 * Makes a nice title that is to be used inside the <title> tags
	 * @access public
	 * @return string|null string with the sites title or null if one doesn't exist
	 */
	public function getHtmlTitle()
	{
		// need to add logic to this so it can decide what title to return based on the page that is loaded
		$return = null;
		
		if(isset($this->pageData[$this->arrayPosition]["Title"]))
		{
			$return = $this->pageData[$this->arrayPosition]["Title"] . " :: " . $this->siteTitle;
		}
		else
		{
			$return = $this->siteTitle;
		}
		
		return $return;
	}
	
	/**
	 * getPostBody function.
	 * 
	 * Returns just the body of a post with no formatting, so html won't act like html<commit_msg>Attempting to fix some issues where the most recent post title is displayed as the title of the page when on a page or category.  Have yet to test the change as I am currently offline.<commit_after><?php
/**
 * Does all the heavy lifting for themes
 * @author <NAME> <<EMAIL>>
 * 
 * 
 */
class TemplateEngine
{

	private $database = null;
	private $router = null;
	private $pageData = null;
	private $postCategory = null;
	private $postTag = null;
	private $arrayPosition = 0;
	private $errorText = null;
	private $pageDataCt = null;
	private $siteTitle = "";
	private $siteDescription = "";
	private $themeName = "";
	private $siteUrl = "";
	private $postFormat = "";
	private $postsPerPage = 0;
	private $baseLocation = "";
	private $templateData = null;
	private $currentUrl = null;
	private $siteBaseUrl = null;
	
	
	
	/**
	 * __construct function.
	 * 
	 * @access public
	 * @param GenericDatabase $database
	 * @param Router $router
	 * @param mixed $siteTitle
	 * @param mixed $siteDescription
	 * @param mixed $themeName
	 * @param mixed $siteUrl
	 * @param mixed $postsPerPage
	 * @param mixed $baseLocation
	 * @param TemplateData $templateData
	 * @return void
	 */
	public function __construct(GenericDatabase $database, Router $router, $siteTitle, $siteDescription, $themeName, SiteUrlGenerator $siteUrlGenerator, $postsPerPage, $baseLocation, TemplateData $templateData)
	{
		$this->database = $database;
		$this->router = $router;
		$this->siteTitle = $siteTitle;
		$this->siteDescription = $siteDescription;
		$this->themeName = $themeName;
		$this->postsPerPage = (int)$postsPerPage;
		$this->baseLocation = $baseLocation;
		$this->siteUrl = $siteUrlGenerator->generateSiteUrl();
		$this->templateData = $templateData;
		$this->currentUrl = $siteUrlGenerator->currentAddressWithoutPageUrl();
		$this->siteBaseUrl = $siteUrlGenerator->generateSiteBaseUrl();
	}
	
	
	/**
	 * processTemplateData function.
	 * 
	 * Grabs the already processed TemplateData and sets it up to be the pageData and gets a count of items in the array
	 * @access public
	 * @return void
	 */
	public function processTemplateData()
	{
		$this->pageData = $this->templateData->getData();
		$this->pageDataCt = count($this->pageData);
	}
	
	/**
	 * getPageData function.
	 * 
	 * Returns the array of page data.
	 * @access public
	 * @return array|null array of page data or null if nothing exists
	 */
	public function getPageData()
	{
		return $this->pageData;
	}
	
	/**
	 * haveNextPost function.
	 * 
	 * Returns true if there are more posts to display, false if there are no new posts
	 * if there is a next post it moves to that one so you can call the rest of the functions to get it's info
	 * @access public
	 * @return boolean True if there are more posts or false if there are none left
	 */
	public function haveNextPost()
	{
		$return = null;
		// since we are starting at 0 so everything else works we need this to start at -1 so that we don't miss the first page when we are in a loop
		// this allows us to not have to call other functions when we are doing a page/single post
		// because haveNextPost is called before the first post we need to make this one less than it is since we are going to increment it anyway
		// the static value remembers what it was set at the previous run
		static $runOnce = false;
		
		if(!$runOnce)
		{
			$this->arrayPosition--;
		}
		
		
		if($this->arrayPosition + 1 < $this->pageDataCt && $this->pageDataCt != 0)
		{
			$this->arrayPosition++;
			$return = true;
			$runOnce = true;
		}
		else
			$return = false;
			
		return $return;
	}
	
	/**
	 * getPostTitle function.
	 * 
	 * Returns the title of the current post.
	 * @access public
	 * @return string|null string if the title exists or null if it doesn't
	 */
	public function getPostTitle()
	{
		$return = null;
		
		if(isset($this->pageData[$this->arrayPosition]["Title"]))
		{
			$return = $this->pageData[$this->arrayPosition]["Title"];
		}
		return $return;
	}
	
	/**
	 * getPostUri function.
	 * 
	 * Returns only the URI of the post
	 * @access public
	 * @return string|null string if the post has a uri or null if it doesn't
	 */
	public function getPostUri()
	{
		$return = null;
		
		if(isset($this->pageData[$this->arrayPosition]["URI"]))
		{
			$return = $this->pageData[$this->arrayPosition]["URI"];
		}
		return $return;
	}
	
	/**
	 * getPostUrl function.
	 * 
	 * Returns the full URI of a post
	 * @access public
	 * @return string|null URL of the post or null if no post
	 */
	public function getPostUrl()
	{

		if(isset($this->pageData[$this->arrayPosition]["URI"]))
		{
			$return = $this->siteUrl . $this->pageData[$this->arrayPosition]["URI"];
		}
		else
		{
			$return = null;
		}
		
		return $return;
	}
	
	/**
	 * getHtmlTitle function.
	 * 
	 * Makes a nice title that is to be used inside the <title> tags
	 * @access public
	 * @return string|null string with the sites title or null if one doesn't exist
	 */
	public function getHtmlTitle()
	{
		// need to add logic to this so it can decide what title to return based on the page that is loaded
		$return = null;
		$type = $this->router->getPageType();
		
		if($type === "page")
		{
			// needs to be tested
			$return = "Page " . $this->router->getUriPosition(2) . " :: " . $this->siteTitle;
		}
		else if($type === "tag" || $type === "category")
		{
			$return = $type . " " . $this->router->getUriPosition(2);
			
			try
			{
				$page = $this->router->getPageOffset();
				
				$return .= " Page " . $page;
			}
			catch(exception $e)
			{
				// do nothing
			}
			
			$return .= " :: " . $this->siteTitle;
			
		}
		else if(isset($this->pageData[$this->arrayPosition]["Title"]))
		{
			$return = $this->pageData[$this->arrayPosition]["Title"] . " :: " . $this->siteTitle;
		}
		else
		{
			$return = $this->siteTitle;
		}
		
		return $return;
	}
	
	/**
	 * getPostBody function.
	 * 
	 * Returns just the body of a post with no formatting, so html won't act like html
{'fraction_non_alphanumeric': 0.12428774928774929, 'fraction_numerical': 0.0012464387464387464, 'mean_word_length': 3.333719135802469, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7539537', 'n_tokens_mistral': 3844, 'n_tokens_neox': 3368, 'n_words': 1161}