global_functions.php 3.39 KB
<?php
/**
 * Created by PhpStorm.
 * User: aleksandarhristov
 * Date: 29.01.15
 * Time: 09:54
 */

include "Parsedown.php";
include "ParsedownExtra.php";

//function that returns all the pages in the database in an array
function get_pages()
{
	global $db;
	$pages = $db->query("SELECT * FROM pages");

	$db_page_title=array();

	$i=0;
	while ($row=$pages->fetchArray())
	{
		$db_page_title[$i] = $row[1];
		$i++;
	}

	return $db_page_title;
}

//function that returns all the page id's in the database in an array
function get_pages_id()
{
	global $db;
	$i=0;
	$query=$db->query("SELECT * FROM pages");
	while($row=$query->fetchArray())
	{
		$pages_id[$i]=$row[0];
		$i++;
	}
	return $pages_id;
}

//function that returns all the sections in the database in an array in format "page: section"
function get_sections()
{
	global $db;
	$pages = $db->query("SELECT * FROM sections");
	$page_section=array();

	$i=0;
	while ($row=$pages->fetchArray())
	{
		$db_section_title = $row[2];
		$db_page_id = $row[1];
		$db_page_title = $db->querySingle("SELECT title FROM pages WHERE id = '$db_page_id'");

		$page_section[$i]=$db_page_title.": ".$db_section_title;

		$i++;
	}

	return $page_section;
}

//function that returns the current page as string; needed in the admin sidebar wrapper template to give the active class and color the active page <li>
function current_page()
{
	global $current_page;
	if(strpos($current_page, "create") !== false)
	{
		$current_page = "create";
	}
	else if(strpos($current_page, "edit") !== false)
	{
		$current_page = "edit";
	}
	else if(strpos($current_page, "remove") !== false)
	{
		$current_page = "remove";
	}
	else
		$current_page = "faq";

	return $current_page;
}

//function that returns all the sections titles that belong to a specific page
function page_sections($page)
{
	global $db;

	$page_id = $db->querySingle("SELECT id FROM pages WHERE title='$page'");

	$query = $db->query("SELECT * FROM sections WHERE page_id = '$page_id'");

	$sections = array();
	$i=0;

	while ($row=$query->fetchArray())
	{
		$sections[$i] = $row[2];
		$i++;
	}
	return $sections;

}

//function that returns section id
function return_section_id($section_title)
{
	global $db;

	$query = $db->query("SELECT * FROM sections");
	$i=0;

	while ($row=$query->fetchArray())
	{
		if ($row[3]==$section_title)
		{
			break;
		}
	}

	return $i;

}

function return_page_id($page_title)
{
	global $db;

	$page_id = $db->querySingle("SELECT id FROM pages WHERE title = '$page_title'");

	return $page_id;

}

function main_page($return_this, $current_page)
{
	global $db;
	$parsedown = new ParsedownExtra();
	$query = $db->query("SELECT * FROM pages WHERE id = '$current_page'");

	while($row = $query->fetchArray())
	{
		$page_id = $row[0];
		$page_title = $row[1];
		$page_text = $parsedown->text($row[2]);
		$sections = $db->query("SELECT * FROM sections WHERE page_id = '$page_id'");
		$i = 0;
		while($row1 = $sections->fetchArray())
		{
			$section_id[$i] = $row1[0];
			$section_title[$i] = $row1[2];
			$section_text[$i] = $parsedown->text($row1[3]);
			$i++;
		}
	}

	switch($return_this)
	{
		case "page_id":
			return $page_id;
			break;
		case "page_title":
			return $page_title;
			break;
		case "page_text":
			return $page_text;
			break;
		case "section_id":
			return $section_id;
			break;
		case "section_title":
			return $section_title;
			break;
		case "section_text":
			return $section_text;
			break;
		default:
			echo "Error!";
	}
}