HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/NVonken/mijneigenlied.com/wwwroot/Core/Components/admin.com.php
<?php
	
	class admin
	{
	
		private $_tpl;
	
		public function __construct()
		{
			General::Load("User");
			General::Load("Menu");
			General::Load("Page");
			General::Load("Column");
            General::Load("Hatch");
			General::Load("MenuHatch");
			General::Load("Photo");
            General::Load("HatchPage");
            General::Load("HatchTranslation");
            General::Load("Subscriber");
            General::Load("File");

            $this->_tpl = new tpl();
		}
	
		public function run()
		{
			if(User::CheckLogin())
			{
				$user = User::GetUserInfo();
				if($user->IsAdmin == 1)
				{
					
					if(!defined("GET_2"))
					{
						define("GET_2", "index");
					}
					
					if(method_exists($this, '_action_'.GET_2))
						$pageContent = call_user_func(array($this, '_action_'.GET_2));
					else
						$pageContent = $this->_action_index();
					
					$components = General::LoadComponents();
					$componentMenuItems = array();
					foreach($components as $component)
					{
						if($component["HasAdmin"])
						{
							foreach($component["AdminOptions"] as $key => $name)
							{
								$componentMenuItems[] = array(	"Name" => $component["DisplayName"]." - ".$name,
																"Component" => substr($component["Filename"], 0, strpos($component["Filename"], ".")),
																"Option" => $key);
							}
						}
					}
					
					if(count($componentMenuItems) != 0)
					{
						$componentsMenu = "";
						foreach($componentMenuItems as $item)
						{
							$this->_tpl->assign("menuUrl",BASE_URL_COMPONENT."/component/".$item["Component"]."/".$item["Option"]);
							$this->_tpl->assign("menuTitle", $item["Name"]);
                            $this->_tpl->assign("menuTarget", "");
                            $this->_tpl->assign("subItems", "");
							$componentsMenu .= $this->_tpl->parse("Menu/item");
						}
						$componentsMenu = "<ul class='sub_nav'>".$componentsMenu."</ul>";
						$this->_tpl->assign("componentsAdmin", $componentsMenu);
					}
					
					$this->_tpl->assign("file", SETTING_ROOT."/js/json.js");
					$this->_tpl->assign("scriptTags", $this->_tpl->parse("General/script-tag"));
					$this->_tpl->assign("activeUser", $user->Name);
					
					//assign to layout
					$this->_tpl->assign("Content", $pageContent);
					$this->_tpl->assign("pageTitle", "Administratie");
                    $this->_tpl->assign("siteName", SETTING_SITENAME);
                    $this->_tpl->assign("siteUrl", SETTING_ROOT."/");
                    return $this->_tpl->parse("Layout/admin");
				}
				else
				{
					header("Location: ".SETTING_ROOT."/account/login.html?reason=admin");
				}
			}
			else
			{
				header("Location: ".SETTING_ROOT."/account/login.html?reason=admin");
			}			
		}
		
		private function _action_index()
		{
			return $this->_tpl->parse("Admin/index");
		}

        private function _action_hatch()
        {
            if (defined("GET_3") && strtolower(GET_3) == 'new') { //new
                $message = "";
                $languages = General::LoadLanguages();
                $hatchTranslations = array();

                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    $valid = true;
                    $hatch = new Hatch();

                    foreach ($languages as $language) {
                        $hatchTranslations[$language] = new HatchTranslation();

                        if (!isset($_POST["ddlPage_" . $language]) || !is_numeric($_POST["ddlPage_" . $language])) {
                            $valid = false;
                            $errorMessage = "Er is geen geldige start pagina gekozen.";
                        }

                        if (!isset($_POST["txtName_" . $language]) || $_POST["txtName_" . $language] == "") {
                            $valid = false;
                            $errorMessage = "Er is geen geldige naam gekozen voor " . $language;
                        } else $hatchTranslations[$language]->Name = $_POST["txtName_" . $language];

                        if ($valid) {
                            if ($hatch->Id == null)
                                $hatch->Id = $hatch->Insert();

                            $hatchTranslations[$language]->HatchId = $hatch->Id;
                            $hatchTranslations[$language]->Lang = $language;
                            $hatchTranslations[$language]->Insert();
                        }
                    }

                    if ($valid) {
                        foreach ($languages as $language) {
                            $hatchPage = new HatchPage();
                            $hatchPage->PageId = $_POST["ddlPage_" . $language];
                            $hatchPage->HatchId = $hatch->Id;
                            $hatchPage->Language = $language;
                            $hatchPage->Insert();
                        }

                        $this->_tpl->assign("message", "Het luik is succesvol toegevoegd.");
                        $message = $this->_tpl->parse("General/success");
                        //reset form
                        unset($_POST);
                        $hatchTranslations = array();
                    } else {
                        $this->_tpl->assign("message", $errorMessage);
                        $message = $this->_tpl->parse("General/error");
                    }
                }

                $pageList = "";
                $languageFields = "";
                foreach ($languages as $language) {
                    $selectedPage = "";
                    if (isset($_POST["ddlPage_" . $language]))
                        $selectedPage = $_POST["ddlPage_" . $language];

                    $this->_tpl->assign("ddlPageOptions", $this->_generatePageOptions($selectedPage));
                    $this->_tpl->assign("pageLang", $language);
                    $pageList .= $this->_tpl->parse("Admin/Hatch/page");

                    $this->_tpl->assign("txtName", isset($_POST["txtName_" . $language]) ? $_POST["txtName_" . $language] : (isset($hatchTranslations[$language]->Name) ? $hatchTranslations[$language]->Name : ""));
                    $languageFields .= $this->_tpl->parse("Admin/Hatch/language-fields");
                }

                $this->_tpl->assign("language-fields", $languageFields);
                $this->_tpl->assign("pageList", $pageList);
                $this->_tpl->assign("message", $message);

                return $this->_tpl->parse("Admin/Hatch/newedit");
            } elseif (defined("GET_3") && strtolower(GET_3) == 'edit') { //edit
                $hatch = Hatch::Select(GET_4);
                $hatchTranslations = General::MapArray(HatchTranslation::SelectAllByHatch($hatch->Id), "Lang");
                $message = "";
                $languages = General::LoadLanguages();

                if ($hatch == null) {
                    header("Location: " . SETTING_ROOT . "/admin/hatch.html");
                    die();
                }

                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    $valid = true;

                    foreach ($languages as $language) {
                        $new = false;
                        if($hatchTranslations[$language] == null){
                            $hatchTranslations[$language] = new HatchTranslation();
                            $new = true;
                        }

                        if (!isset($_POST["ddlPage_" . $language]) || !is_numeric($_POST["ddlPage_" . $language])) {
                            $valid = false;
                            $errorMessage = "Er is geen geldige start pagina gekozen. voor " . $language;
                        }

                        if (!isset($_POST["txtName_" . $language]) || $_POST["txtName_" . $language] == "") {
                            $valid = false;
                            $errorMessage = "Er is geen geldige naam gekozen voor " . $language;
                        } else $hatchTranslations[$language]->Name = $_POST["txtName_" . $language];

                        if ($valid) {
                            if(!$new)
                                $hatchTranslations[$language]->Update();
                            else {
                                $hatchTranslations[$language]->HatchId = $hatch->Id;
                                $hatchTranslations[$language]->Lang = $language;
                                $hatchTranslations[$language]->Insert();
                            }
                        } else {
                            break;
                        }
                    }

                    if ($valid) {
                        $hatch->Update();
                        $this->_tpl->assign("message", "Het luik is succesvol gewijzigd.");
                        $message = $this->_tpl->parse("General/success");
                        //save the hatchpages
                        HatchPage::DeleteByHatch($hatch->Id);
                        foreach ($languages as $language) {
                            $hatchPage = new HatchPage();
                            $hatchPage->PageId = $_POST["ddlPage_" . $language];
                            $hatchPage->HatchId = $hatch->Id;
                            $hatchPage->Language = $language;
                            $hatchPage->Insert();
                        }
                    } else {
                        $this->_tpl->assign("message", $errorMessage);
                        $message = $this->_tpl->parse("General/error");
                    }
                }

                //get the start page
                $pages = General::MapArray(HatchPage::SelectByHatch($hatch->Id), "Language");

                //assign the language list
                $pageList = "";
                $languageFields = "";
                foreach ($languages as $language) {
                    $this->_tpl->assign("pageLang", $language);
                    $selectedPage = "";
                    if (isset($pages[$language]))
                        $selectedPage = $pages[$language]->PageId;

                    if (isset($_POST["ddlPage_" . $language]))
                        $selectedPage = $_POST["ddlPage_" . $language];

                    $this->_tpl->assign("lang", $language);
                    $this->_tpl->assign("txtName", isset($_POST["txtName_" . $language]) ? $_POST["txtName_" . $language] : (isset($hatchTranslations[$language]->Name) ? $hatchTranslations[$language]->Name : ""));
                    $languageFields .= $this->_tpl->parse("Admin/Hatch/language-fields");

                    $this->_tpl->assign("ddlPageOptions", $this->_generatePageOptions($selectedPage));
                    $pageList .= $this->_tpl->parse("Admin/Hatch/page");
                }
                $this->_tpl->assign("language-fields", $languageFields);

                $this->_tpl->assign("pageList", $pageList);
                $this->_tpl->assign("message", $message);

                return $this->_tpl->parse("Admin/Hatch/newedit");

            } elseif (defined("GET_3") && strtolower(GET_3) == 'sort') {
                if (defined("GET_5") && is_numeric(GET_5)) {
                    if (defined("GET_4") && (GET_4 == "up" || GET_4 == "down")) {
                        if (GET_4 == "up") {
                            $hatchItem = Hatch::Select(GET_5);
                            if ($hatchItem != null && $hatchItem->Sort - 1 > 0) {

                                $hatchItemSwitch = Hatch::SelectBySort($hatchItem->Sort - 1);
                                //optional, for self restore
                                if ($hatchItemSwitch != null) {
                                    $hatchItemSwitch->Sort = $hatchItem->Sort;
                                    $hatchItemSwitch->Update();
                                }
                                $hatchItem->Sort -= 1;
                                $hatchItem->Update();
                            }

                        } elseif (GET_4 == "down") {
                            $hatchItem = Hatch::Select(GET_5);
                            if ($hatchItem != null && $hatchItem->Sort + 1 <= Hatch::SelectLastSort()) {

                                $hatchItemSwitch = Hatch::SelectBySort($hatchItem->Sort + 1);
                                //optional, for self restore
                                if ($hatchItemSwitch != null) {
                                    $hatchItemSwitch->Sort = $hatchItem->Sort;
                                    $hatchItemSwitch->Update();
                                }
                                $hatchItem->Sort += 1;
                                $hatchItem->Update();
                            }
                        }
                    }
                }
            }
            $message = "";
            if (defined("GET_3") && GET_3 == "delete" && defined("GET_4")) {
                $hatch = Hatch::Select(GET_4);
                if ($hatch != null) {
                    $hatch->Delete();
                    $this->_tpl->assign("message", "Het luik is succesvol verwijderd!");
                    $message = $this->_tpl->parse("General/success");
                }
            }
            $hatches = Hatch::SelectAll();
            $hatchTranslations = General::MapArray(HatchTranslation::SelectRange(General::GetField($hatches, "Id"), DEFAULT_LANGUAGE), "HatchId");
            $hatchList = "";
            foreach ($hatches as $hatch) {
                $this->_tpl->assign("hatchId", $hatch->Id);
                $this->_tpl->assign("hatchName", $hatchTranslations[$hatch->Id]->Name);

                $hatchList .= $this->_tpl->parse("Admin/Hatch/overview-item");
            }
            $this->_tpl->assign("message", $message);
            $this->_tpl->assign("hatchList", $hatchList);
            return $this->_tpl->parse("Admin/Hatch/overview");
        }
		#region Page Editing
		
		private function _action_page()
	    {
	        if (defined("GET_3") && strtolower(GET_3) == 'new') { //new
	            $message = "";
	            if ($_SERVER["REQUEST_METHOD"] == "POST") {
	                $valid = true;
	                $columnContainer = array();
	                $page = new Page();
	
	                if (!isset($_POST["txtTitle"]) || $_POST["txtTitle"] == "") {
	                    $valid = false;
	                    $errorMessage = "Geen geldige titel.";
	                }
	                else $page->PageTitle = $_POST["txtTitle"];
	
	                if (!isset($_POST["txtDescription"]) || $_POST["txtDescription"] == "") {
	                    $valid = false;
	                    $errorMessage = "Geen geldige omschrijving.";
	                }
	                else $page->PageDescription = $_POST["txtDescription"];
	
	                if (!isset($_POST["ddlLanguage"]) || $_POST["ddlLanguage"] == "") {
	                    $valid = false;
	                    $errorMessage = "Geen geldige taal";
	                }
	                else $page->Language = $_POST["ddlLanguage"];
	
	                if (!isset($_POST["ddlLayout"]) || !in_array($_POST["ddlLayout"], General::LoadLayouts())) {
	                    $valid = false;
	                    $errorMessage = "Geen geldige layout gekozen.";
	                }
	                else $page->Template = $_POST["ddlLayout"];
	
	                $page->Active = isset($_POST["cbActive"]) ? 1 : 0;
	                $page->HorizontalColumns = isset($_POST["cbHorizontalColumns"]) ? 1 : 0;
	
	                //check the content type
	                if (isset($_POST["rbContent"])) {
	                    if ($_POST["rbContent"] == "columns") {
	                        $page->ContentType = "columns";
	                        //loop columns
	                        if (!isset($_POST["txtColumnCount"]) || !is_numeric($_POST["txtColumnCount"])) {
	                            $valid = false;
	                            $errorMessage = "Er zijn geen inhoudsvlakken aangemaakt.";
	                        }
	                        else
	                        {
	                            $columnCount = intval($_POST["txtColumnCount"]);
	                            for ($i = 1; $i <= $columnCount; $i++)
	                            {
	                                //check if there is not an delete column
	                                if (isset($_POST["ddlType_" . $i])) {
	                                    $columnType = $_POST["ddlType_" . $i];
	                                    $columnId = $i;
	                                    //create column
	                                    $column = new Column();
	                                    $column->Type = $columnType;
	                                    switch ($columnType)
	                                    {
	                                        case "text":
	                                            $column->Text = $_POST["txtContent_" . $columnId];
	                                            $columnContainer[] = $column;
	                                            break;
	                                        case "photo":
	                                            $photoList = "";
	                                            foreach ($_POST["ddlPhotos"] as $photoId)
	                                            {
	                                                if (is_numeric($photoId))
	                                                    $photoList .= $photoId . ",";
	                                            }
	                                            $column->Photos = $photoList;
	                                            $columnContainer[] = $column;
	                                            break;
	                                        case "element":
	                                            $elements = General::LoadElements();
	                                            $files = array();
	                                            foreach ($elements as $element)
	                                                $files[] = $element["Filename"];
	
	                                            if (in_array($_POST["ddlElement_" . $columnId], $files)) {
	                                                $column->Element = $_POST["ddlElement_" . $columnId];
	                                            }
	                                            else
	                                            {
	                                                $valid = false;
	                                                $errorMessage = "Geen geldig element geselecteerd.";
	                                            }
	                                            $columnContainer[] = $column;
	                                            break;
	                                    }
	                                }
	                            }
	                        }
	                    }
	                    elseif ($_POST["rbContent"] == "component")
	                    {
	                        $components = General::LoadComponents();
	                        $comFiles = array();
	                        foreach ($components as $com)
	                        {
	                            if (isset($com["ContentOptions"]))
	                                foreach ($com["ContentOptions"] as $key => $name)
	                                    $comFiles[] = $com["Filename"] . ";" . $key;
	                        }
	
	
	                        //validate the selected component
	                        if (!isset($_POST["ddlComponent"])) {
	                            $valid = false;
	                            $errorMessage = "Geen geldig component geselecteerd.";
	                        }
	                        elseif (!in_array($_POST["ddlComponent"], $comFiles)) {
	                            $valid = false;
	                            $errorMessage = "Geen geldig component geselecteerd";
	                        }
	                        else
	                        {
	                            $page->ContentType = "component";
	                            $page->Component = $_POST["ddlComponent"];
	                        }
	                    }
	                    else
	                    {
	                        $valid = false;
	                        $errorMessage = "Er is geen inhoudstype geselecteerd";
	                    }
	                }
	                else
	                {
	                    $valid = false;
	                    $errorMessage = "Er is geen inhoudstype geselecteerd.";
	                }
	
	                if ($valid) {
	                    $page->Slug = General::UrlSafe($page->PageTitle);
	                    $page->Id = $page->Insert();
	                    if ($page->ContentType == "columns") {
	                        foreach ($columnContainer as $column)
	                        {
	                            $column->PageId = $page->Id;
	                            $column->Insert();
	                        }
	                    }
	
	                    if (isset($_POST["cbCreateMenu"])) {
	                        $menu = new Menu();
	                        $menu->Language = $page->Language;
	                        $menu->PageId = $page->Id;
	                        $menu->Title = $page->PageTitle;
	                        $menu->Target = "_self";
	                        $menu->Sort = 0;
	                        $menu->MenuTypeId = 0;
	                        $menu->Insert();
	                    }
	
	                    $this->_tpl->assign("message", "De pagina is succesvol opgeslagen.");
	                    $message = $this->_tpl->parse("General/success");
	                    unset($_POST);
	                }
	                else
	                {
	                    //clean photo's if any
	                    if ($page->ContentType == "columns") {
	                        foreach ($columnContainer as $column)
	                        {
	                            if ($column->Type == "photo") {
	                                $photoData = explode(";", $column->Photos);
	                                foreach ($photoData as $data)
	                                {
	                                    $info = explode(",", $data);
	                                    unlink("uploads/" . $info[1] . ".jpg");
	                                    unlink("uploads/thumbs/" . $info[1] . ".jpg");
	                                }
	                            }
	                        }
	                    }
	
	                    $jsonData = array();
	                    //set up json object to rebuild columns
	                    if (isset($columnContainer)) {
	                        foreach ($columnContainer as $column)
	                        {
	                            $jsonData[] = array("type" => $column->Type,
	                                "text" => General::ToJSLineEndings($column->Text),
	                                "element" => $column->Element,
	                                "photos" => $column->Photos);
	                        }
	                    }
	                    $this->_tpl->assign("message", $errorMessage);
	                    $message = $this->_tpl->parse("General/error");
	                }
	            }
	
	            $this->_tpl->assign("message", $message);
	            $this->_tpl->assign("txtTitle", isset($_POST["txtTitle"]) ? $_POST["txtTitle"] : "");
	            $this->_tpl->assign("txtDescription", isset($_POST["txtDescription"]) ? $_POST["txtDescription"] : "");
	            $this->_tpl->assign("cbActiveChecked", isset($_POST["cbActive"]) ? "checked='checked'" : "");
	            $this->_tpl->assign("cbHorizontalColumnsChecked", isset($_POST["cbHorizontalColumns"]) ? "checked='checked'" : "");
	
	
	            //language list
	            $this->_tpl->assign("ddlLanguageOptions", $this->_generateLanguageOptions(isset($_POST["ddlLanguage"]) ? $_POST["ddlLanguage"] : ""));
	            //component list
	            $this->_tpl->assign("ddlComponentOptions", $this->_generateComponentOptions(isset($_POST["ddlComponent"]) ? $_POST["ddlComponent"] : ""));
	            //element list
	            $this->_tpl->assign("ddlElementOptions", $this->_generateElementOptions(""));
	            //layoutlist
	            $this->_tpl->assign("ddlLayoutOptions", $this->_generateLayoutOptions(isset($_POST["ddlLayout"]) ? $_POST["ddlLayout"] : ""));
	            //photo list
	            $this->_tpl->assign("ddlPhotoOptions", $this->_generatePhotoOptions(""));
	
	
	            //load the default hint for the column admin
	            $this->_tpl->assign("columns", $this->_tpl->parse("Admin/Page/template-column-hint"));
	
	            //open the right content admin
	            $this->_tpl->assign("setupScripts", "");
	
	            //create columndata containr
	            $columnHTML = "";
	
	            if (isset($_POST["rbContent"])) {
	                $rebuildTag = "";
	                if (isset($columnContainer)) {
	                    $i = 0;
	                    foreach ($columnContainer as $columnData)
	                    {
	                        $this->_tpl->assign("i", $i);
	                        $this->_tpl->assign("j", ($i+1));
	
	                        $this->_tpl->assign("selectedText", "");
	                        $this->_tpl->assign("selectedPhoto", "");
	                        $this->_tpl->assign("selectedElement", "");
	
	                        switch ($columnData->Type)
	                        {
	                            case "text":
	                                $this->_tpl->assign("selectedText", "selected='selected'");
	                                $this->_tpl->assign("txtContent", $columnData->Text);
	                                $this->_tpl->assign("columnSettings", $this->_tpl->parse("Admin/Page/template-column-text"));
	                                $columnHTML .= $this->_tpl->parse("Admin/Page/template-column-container");
	                                break;
	                            case "element":
	                                $this->_tpl->assign("selectedElement", "selected='selected'");
	                                $this->_tpl->assign("ddlElementOptions", $this->_generateElementOptions($columnData->Element));
	                                $this->_tpl->assign("columnSettings", $this->_tpl->parse("Admin/Page/template-column-element"));
	                                $columnHTML .= $this->_tpl->parse("Admin/Page/template-column-container");
	                                break;
	                            case "photo":
	                                $this->_tpl->assign("selectedPhoto", "selected='selected'");
	                                $this->_tpl->assign("ddlPhotoOptions", $this->_generatePhotoOptions(explode(",",$columnData->Photos)));
	                                $this->_tpl->assign("columnSettings", $this->_tpl->parse("Admin/Page/template-column-photo"));
	                                $columnHTML .= $this->_tpl->parse("Admin/Page/template-column-container");
	                                break;
	                        }
	                        $i++;
	                    }
	                }
	
	                if ($_POST["rbContent"] == "columns")
	                    $this->_tpl->assign("setupScripts", "showColumnAdmin();");
	
	                if ($_POST["rbContent"] == "component")
	                    $this->_tpl->assign("setupScripts", "showComponentAdmin();");
	
	                //assign the builded column overview
	                $this->_tpl->assign("columns", $columnHTML);
	                //assign the builded column overview
	                $this->_tpl->assign("txtColumnCount", $i);
	            }
	
	            return $this->_tpl->parse("Admin/Page/newedit");
	        }
	        elseif (defined("GET_3") && strtolower(GET_3) == 'edit')
	        { //edit
	            $page = Page::Select(GET_4);
	            $message = "";
	
	            if ($page != null) {
	                if ($_SERVER["REQUEST_METHOD"] == "POST") {
	                    $valid = true;
	                    $columnsContainer = array();
	
	                    if (!isset($_POST["txtTitle"]) || $_POST["txtTitle"] == "") {
	                        $valid = false;
	                        $errorMessage = "Geen geldige titel.";
	                    }
	                    else $page->PageTitle = $_POST["txtTitle"];
	
	                    if (!isset($_POST["txtDescription"]) || $_POST["txtDescription"] == "") {
	                        $valid = false;
	                        $errorMessage = "Geen geldige omschrijving.";
	                    }
	                    else $page->PageDescription = $_POST["txtDescription"];
	
	                    if (!isset($_POST["ddlLanguage"]) || $_POST["ddlLanguage"] == "") {
	                        $valid = false;
	                        $errorMessage = "Geen geldige taal";
	                    }
	                    else $page->Language = $_POST["ddlLanguage"];
	
	                    if (!isset($_POST["ddlLayout"]) || !in_array($_POST["ddlLayout"], General::LoadLayouts())) {
	                        $valid = false;
	                        $errorMessage = "Geen geldige layout gekozen.";
	                    }
	                    else $page->Template = $_POST["ddlLayout"];
	
	                    $page->Active = isset($_POST["cbActive"]) ? 1 : 0;
	                    $page->HorizontalColumns = isset($_POST["cbHorizontalColumns"]) ? 1 : 0;
	
	                    if (isset($_POST["rbContent"])) {
	                        if ($_POST["rbContent"] == "columns") {
	                            $page->ContentType = "columns";
	                            //loop columns
	                            if (!isset($_POST["txtColumnCount"]) || !is_numeric($_POST["txtColumnCount"])) {
	                                $valid = false;
	                                $errorMessage = "Er zijn geen inhoudsvlakken aangemaakt.";
	                            }
	                            else
	                            {
	                                $columnCount = intval($_POST["txtColumnCount"]);
	                                for ($i = 1; $i <= $columnCount; $i++)
	                                {
	                                    //check if there is not an delete column
	                                    if (isset($_POST["ddlType_" . $i])) {
	                                        $columnType = $_POST["ddlType_" . $i];
	                                        $columnId = $i;
	                                        //create column
	                                        $column = new Column();
	                                        $column->Type = $columnType;
	                                        switch ($columnType)
	                                        {
	                                            case "text":
	                                                $column->Text = ($_POST["txtContent_" . $columnId]);
	                                                $columnContainer[] = $column;
	                                                break;
	                                            case "photo":
	                                                $photoList = "";
	                                                foreach ($_POST["ddlPhotos_" . $columnId] as $photoId)
	                                                {
	                                                    if (is_numeric($photoId))
	                                                        $photoList .= $photoId . ",";
	                                                }
	                                                $column->Photos = $photoList;
	                                                $columnContainer[] = $column;
	                                                break;
	                                            case "element":
	                                                $elements = General::LoadElements();
	                                                $files = array();
	                                                foreach ($elements as $element)
	                                                    $files[] = $element["Filename"];
	
	                                                if (in_array($_POST["ddlElement_" . $columnId], $files)) {
	                                                    $column->Element = $_POST["ddlElement_" . $columnId];
	                                                }
	                                                else
	                                                {
	                                                    $valid = false;
	                                                    $errorMessage = "Geen geldig element geselecteerd.";
	                                                }
	                                                $columnContainer[] = $column;
	                                                break;
	                                        }
	                                    }
	                                }
	                            }
	                        }
	                        elseif ($_POST["rbContent"] == "component")
	                        {
	                            $components = General::LoadComponents();
	                            $comFiles = array();
	                            foreach ($components as $com)
	                            {
	                                if (isset($com["ContentOptions"])) {
	                                    foreach ($com["ContentOptions"] as $key => $name)
	                                        $comFiles[] = $com["Filename"] . ";" . $key;
	                                }
	                            }
	
	                            //validate the selected component
	                            if (!isset($_POST["ddlComponent"])) {
	                                $valid = false;
	                                $errorMessage = "Geen geldig component geselecteerd.";
	                            }
	                            elseif (!in_array($_POST["ddlComponent"], $comFiles)) {
	                                $valid = false;
	                                $errorMessage = "Geen geldig component geselecteerd";
	                            }
	                            else
	                            {
	                                $page->ContentType = "component";
	                                $page->Component = $_POST["ddlComponent"];
	                            }
	                        }
	                        else
	                        {
	                            $valid = false;
	                            $errorMessage = "Er is geen inhoudstype geselecteerd";
	                        }
	                    }
	                    else
	                    {
	                        $valid = false;
	                        $errorMessage = "Er is geen inhoudstype geselecteerd.";
	                    }
	
	
	                    if ($valid) {
	                        $page->Slug = General::UrlSafe($page->PageTitle);
	                        $page->Update();
	
	                        //delete all columns
	                        Column::DeleteByPage($page->Id);
	
	                        if ($page->ContentType == "columns") {
	                            foreach ($columnContainer as $column)
	                            {
	                                $column->PageId = $page->Id;
	                                $column->Insert();
	                            }
	                        }
	
	                        if (isset($_POST["cbCreateMenu"])) {
	                            $menu = new Menu();
	                            $menu->Language = $page->Language;
	                            $menu->PageId = $page->Id;
	                            $menu->Title = $page->PageTitle;
	                            $menu->Target = "_self";
	                            $menu->Sort = 0;
	                            $menu->MenuTypeId = 0;
	                            $menu->Insert();
	                        }
	
	                        $this->_tpl->assign("message", "De wijzigingen zijn succesvol opgeslagen.");
	                        $message = $this->_tpl->parse("General/success");
	                    }
	                    else
	                    {
	                        $this->_tpl->assign("message", $errorMessage);
	                        $message = $this->_tpl->parse("General/error");
	                    }
	                }
	
	                $this->_tpl->assign("message", $message);
	                $this->_tpl->assign("txtTitle", isset($_POST["txtTitle"]) ? $_POST["txtTitle"] : $page->PageTitle);
	                $this->_tpl->assign("txtDescription", isset($_POST["txtDescription"]) ? $_POST["txtDescription"] : $page->PageDescription);
	                $this->_tpl->assign("cbActiveChecked", isset($_POST["cbActive"]) || $page->Active == 1 ? "checked='checked'" : "");
	                $this->_tpl->assign("cbHorizontalColumnsChecked", isset($_POST["cbHorizontalColumns"]) || $page->HorizontalColumns == 1 ? "checked='checked'" : "");
	
	                //language list
	                $this->_tpl->assign("ddlLanguageOptions", $this->_generateLanguageOptions(isset($_POST["ddlLanguage"]) ? $_POST["ddlLanguage"] : $page->Language));
	                //component list
	                $this->_tpl->assign("ddlComponentOptions", $this->_generateComponentOptions(isset($_POST["ddlComponent"]) ? $_POST["ddlComponent"] : $page->Component));
	                //element list
	                $this->_tpl->assign("ddlElementOptions", $this->_generateElementOptions(""));
	                //layoutlist
	                $this->_tpl->assign("ddlLayoutOptions", $this->_generateLayoutOptions(isset($_POST["ddlLayout"]) ? $_POST["ddlLayout"] : $page->Template));
	                //photo options
	                $this->_tpl->assign("ddlPhotoOptions", $this->_generatePhotoOptions(""));
	
	                //load the default hint for the column admin
	                $this->_tpl->assign("columns", $this->_tpl->parse("Admin/Page/template-column-hint"));
	
	                //create columndata containr
	                $columnHTML = "";
	
	                //open the right content admin
	                $this->_tpl->assign("setupScripts", "");
	                if (isset($_POST["rbContent"])) {//assign data usring post data
	                    $i = 0;
	
	                    if ($_POST["rbContent"] == "columns") {
	                        foreach ($columnContainer as $columnData)
	                        {
	                            $this->_tpl->assign("i", $i);
	                            $this->_tpl->assign("j", ($i+1));
	
	                            $this->_tpl->assign("selectedText", "");
	                            $this->_tpl->assign("selectedPhoto", "");
	                            $this->_tpl->assign("selectedElement", "");
	
	                            switch ($columnData->Type)
	                            {
	                                case "text":
	                                    $this->_tpl->assign("selectedText", "selected='selected'");
	                                    $this->_tpl->assign("txtContent", $columnData->Text);
	                                    $this->_tpl->assign("columnSettings", $this->_tpl->parse("Admin/Page/template-column-text"));
	                                    $columnHTML .= $this->_tpl->parse("Admin/Page/template-column-container");
	                                    break;
	                                case "element":
	                                    $this->_tpl->assign("selectedElement", "selected='selected'");
	                                    $this->_tpl->assign("ddlElementOptions", $this->_generateElementOptions($columnData->Element));
	                                    $this->_tpl->assign("columnSettings", $this->_tpl->parse("Admin/Page/template-column-element"));
	                                    $columnHTML .= $this->_tpl->parse("Admin/Page/template-column-container");
	                                    break;
	                                case "photo":
	                                    $this->_tpl->assign("selectedPhoto", "selected='selected'");
	                                    $this->_tpl->assign("ddlPhotoOptions", $this->_generatePhotoOptions(explode(",",$columnData->Photos)));
	                                    $this->_tpl->assign("columnSettings", $this->_tpl->parse("Admin/Page/template-column-photo"));
	                                    $columnHTML .= $this->_tpl->parse("Admin/Page/template-column-container");
	                                    break;
	                            }
	                            $i++;
	                        }
	
	                    }
	                    if ($_POST["rbContent"] == "columns")
	                        $this->_tpl->assign("setupScripts", "showColumnAdmin();");
	
	                    if ($_POST["rbContent"] == "component")
	                        $this->_tpl->assign("setupScripts", "showComponentAdmin();");
	
	                    //assign the builded column overview
	                    $this->_tpl->assign("columns", $columnHTML);
	                    //assign the builded column overview
	                    $this->_tpl->assign("txtColumnCount", $i);
	
	                }
	                else
	                {//asssing data using DB
	
	                    if ($page->ContentType == "columns") {
	                        //select columns
	                        $columns = Column::SelectByPage($page->Id);
	                        foreach ($columns as $columnData)
	                        {
	                            $this->_tpl->assign("i", $i);
	                            $this->_tpl->assign("j", ($i+1));
	
	                            $this->_tpl->assign("selectedText", "");
	                            $this->_tpl->assign("selectedPhoto", "");
	                            $this->_tpl->assign("selectedElement", "");
	
	                            switch ($columnData->Type)
	                            {
	                                case "text":
	                                    $this->_tpl->assign("selectedText", "selected='selected'");
	                                    $this->_tpl->assign("txtContent", $columnData->Text);
	                                    $this->_tpl->assign("columnSettings", $this->_tpl->parse("Admin/Page/template-column-text"));
	                                    $columnHTML .= $this->_tpl->parse("Admin/Page/template-column-container");
	                                    break;
	                                case "element":
	                                    $this->_tpl->assign("selectedElement", "selected='selected'");
	                                    $this->_tpl->assign("ddlElementOptions", $this->_generateElementOptions($columnData->Element));
	                                    $this->_tpl->assign("columnSettings", $this->_tpl->parse("Admin/Page/template-column-element"));
	                                    $columnHTML .= $this->_tpl->parse("Admin/Page/template-column-container");
	                                    break;
	                                case "photo":
	                                    $this->_tpl->assign("selectedPhoto", "selected='selected'");
	                                    $this->_tpl->assign("ddlPhotoOptions", $this->_generatePhotoOptions(explode(",",$columnData->Photos)));
	                                    $this->_tpl->assign("columnSettings", $this->_tpl->parse("Admin/Page/template-column-photo"));
	                                    $columnHTML .= $this->_tpl->parse("Admin/Page/template-column-container");
	                                    break;
	                            }
	                            $i++;
	                        }
	
	                        $this->_tpl->assign("setupScripts", "showColumnAdmin();");
	                    }
	
	                    //assign the builded column overview
	                    $this->_tpl->assign("columns", $columnHTML);
	                    //assign the builded column overview
	                    $this->_tpl->assign("txtColumnCount", $i);
	
	                    if ($page->ContentType == "component")
	                        $this->_tpl->assign("setupScripts", "showComponentAdmin();");
	                }
	
	                return $this->_tpl->parse("Admin/Page/newedit");
	
	            }
	        }
	        else
	        { //overview
	            $message = "";
	            //delete function
	            if (defined("GET_3") && GET_3 == "delete") {
	                $page = Page::Select(GET_4);
	                if ($page != null) {
	                    if ($page->ContentType == "columns") {
	                        $columns = Column::SelectByPage($page->Id);
	                        foreach ($columns as $column)
	                        {
	                            $column->Delete();
	                        }
	                    }
	                    $page->Delete();
	                    $this->_tpl->assign("message", "Pagina is succesvol verwijderd!");
	                    $message = $this->_tpl->parse("General/success");
	                }
	            }
	            $languages = General::LoadLanguages();
	            $tables = "";
	            foreach ($languages as $lang)
	            {
	                $pages = Page::SelectByLang($lang);
	                $pageList = "";
	                foreach ($pages as $page)
	                {
	                    $this->_tpl->assign("pageId", $page->Id);
	                    $this->_tpl->assign("pageLanguage", $page->Language);
	                    $this->_tpl->assign("pageTitle", $page->PageTitle);
	                    $this->_tpl->assign("pageDescription", substr($page->PageDescription, 0, 50));
	                    $pageList .= $this->_tpl->parse("Admin/Page/overview-item");
	                }
	                $this->_tpl->assign("pageList", $pageList);
	                $tables .= $this->_tpl->parse("Admin/Page/overview-item-container");
	            }
	            $this->_tpl->assign("tables", $tables);
	            $this->_tpl->assign("message", $message);
	            return $this->_tpl->parse("Admin/Page/overview");
	        }
	    }
			
		#endregion
		
		private function _action_menu()
		{
			if(defined("GET_3") && strtolower(GET_3) == 'new')
			{	//new
				$message = "";
				if($_SERVER["REQUEST_METHOD"] == "POST")
				{
					//var_dump($_POST);
					$menu = new Menu();
					$valid = true;
					
					//set default
					$menu->MenuTypeId = 0;
					
					if(!isset($_POST["ddlLanguage"]) || !in_array($_POST["ddlLanguage"], General::LoadLanguages())){$valid = false; $errorMessage = "Geen geldige taal geselecteerd,"; }
					else $menu->Language = $_POST["ddlLanguage"];
					
					if(!isset($_POST["txtTitle"]) || $_POST["txtTitle"] == ""){$valid = false; $errorMessage = "Geen geldige titel ingevoerd"; }
					else $menu->Title = $_POST["txtTitle"];
					
					if(!isset($_POST["ddlTarget"]) || !in_array($_POST["ddlTarget"], array("_self", "_blank"))){$valid = false; $errorMessage = "Geen geldige 'Openen in' geselecteerd.";}
					else $menu->Target = $_POST["ddlTarget"];
					
					if(!isset($_POST["ddlParent"]) || ($_POST["ddlParent"] != "" && !is_numeric($_POST["ddlParent"]))){$valid = false; $errorMessage = "Geen geldige 'sub-menu van' geselecteerd" ;}
					else $menu->ParentId = $_POST["ddlParent"] == 0 ? null : intval($_POST["ddlParent"]);
					
					//hatch validation
					if(!isset($_POST["ddlHatch"]) || count($_POST["ddlHatch"]) == 0){$valid = false; $errorMessage = "Er zijn geen luiken geselecteerd";}
					
					if( (!isset($_POST["ddlPage"]) || !is_numeric($_POST["ddlPage"])) && (!isset($_POST["txtURL"]) || $_POST["txtURL"] == "")){$valid = false; $errorMessage = "Een keuze tussen URL en Pagina is verplicht."; }
					else
					{
						if(isset($_POST["ddlPage"]) && is_numeric($_POST["ddlPage"]))
						{
							$menu->PageId = $_POST['ddlPage'];
							$menu->Url = "";
						}
						elseif(isset($_POST["txtURL"]) && $_POST["txtURL"] != "")
						{
							$menu->Url = $_POST["txtURL"];
							$menu->PageId = 0;
						}
						else
						{
							$valid = false; 
							$errorMessage = "Een keuze tussen URL en Pagina is verplicht.";
						}
					}
					
					if($valid)
					{
						$menu->Sort = Menu::SelectLastSort($menu->ParentId, $menu->Language) + 1;	
						$menu->Id = $menu->Insert();
						foreach($_POST["ddlHatch"] as $hatchId)
						{
							$menuHatch = new MenuHatch();
							$menuHatch->MenuId = $menu->Id;
							$menuHatch->HatchId = intval($hatchId);
							$menuHatch->Insert();
						}
						$this->_tpl->assign("message", "Het menu-item is met success toegevoegd.");
						$message = $this->_tpl->parse("General/success");
						unset($_POST);
					}
					else
					{
						$this->_tpl->assign("message", $errorMessage);
						$message = $this->_tpl->parse("General/error");
					}
				}
				$this->_tpl->assign("message", $message);
				$this->_tpl->assign("ddlLanguageOptions", $this->_generateLanguageOptions(isset($_POST["ddlLanguage"]) ? $_POST["ddlLanguage"] : ""));
				$this->_tpl->assign("ddlTargetOptions", $this->_generateTargetOptions(isset($_POST["ddlTarget"]) ? $_POST["ddlTarget"] : ""));
				$this->_tpl->assign("ddlParentOptions", $this->_generateMenuOptions(Menu::SelectAllByParent(null) ,isset($_POST["ddlParent"]) ? $_POST["ddlParent"] : ""));
				$this->_tpl->assign("ddlPageOptions", $this->_generatePageOptions(isset($_POST["ddlPage"]) ? $_POST["ddlPage"] : ""));
				$this->_tpl->assign("ddlHatchOptions", $this->_generateHatchOptions(isset($_POST["ddlHatch"]) ? $_POST["ddlHatch"] : "0"));
				
				$this->_tpl->assign("txtTitle", isset($_POST['txtTitle']) ? $_POST["txtTitle"] : "");
				$this->_tpl->assign("txtURL", isset($_POST['txtURL']) ? $_POST["txtURL"] : "");
				
				return $this->_tpl->parse("Admin/Menu/newedit");
			}
			elseif(defined("GET_3") && strtolower(GET_3) == 'edit')
			{	//edit
				$menu = Menu::Select(GET_4);
                $message = "";

				if($menu == null)
				{
					header("Location: ".SETTING_ROOT."/admin/menu.html");
					die();
				}
					
					
				if($_SERVER["REQUEST_METHOD"] == "POST")
				{
						
					$valid = true;
					
					if(!isset($_POST["ddlLanguage"]) || !in_array($_POST["ddlLanguage"], General::LoadLanguages())){$valid = false; $errorMessage = "Geen geldige taal geselecteerd,"; }
					else $menu->Language = $_POST["ddlLanguage"];
					
					if(!isset($_POST["txtTitle"]) || $_POST["txtTitle"] == ""){$valid = false; $errorMessage = "Geen geldige titel ingevoerd"; }
					else $menu->Title = $_POST["txtTitle"];
					
					if(!isset($_POST["ddlTarget"]) || !in_array($_POST["ddlTarget"], array("_self", "_blank"))){$valid = false; $errorMessage = "Geen geldige 'Openen in' geselecteerd.";}
					else $menu->Target = $_POST["ddlTarget"];
					
					if($menu->Id == $_POST["ddlParent"]){$valid = false; $errorMessage = "Het menu kan geen submenu van zichzelf zijn."; }

                    if(!isset($_POST["ddlParent"]) || ($_POST["ddlParent"] != "" && !is_numeric($_POST["ddlParent"]))){$valid = false; $errorMessage = "Geen geldige 'sub-menu van' geselecteerd" ;}
					else
                    {
                        //if is a new parent, set the sort order to max of the new parent
                        if($menu->ParentId != $_POST["ddlParent"])
                        {
                            $menu->Sort = Menu::SelectLastSort($_POST["ddlParent"] == 0 ? null : intval($_POST["ddlParent"]), $_POST["ddlLanguage"]) + 1;
                        }
                        $menu->ParentId = $_POST["ddlParent"] == 0 ? null : intval($_POST["ddlParent"]);
                    }
					
					//hatch validation
					if(!isset($_POST["ddlHatch"]) || count($_POST["ddlHatch"]) == 0){$valid = false; $errorMessage = "Er zijn geen luiken geselecteerd";}
										
					if( (!isset($_POST["ddlPage"]) || !is_numeric($_POST["ddlPage"])) && (!isset($_POST["txtURL"]) || $_POST["txtURL"] == "")){$valid = false; $errorMessage = "Een keuze tussen URL en Pagina is verplicht."; }
					else
					{
						if(isset($_POST["ddlPage"]) && is_numeric($_POST["ddlPage"]))
						{
							$menu->PageId = $_POST['ddlPage'];
							$menu->Url = "";
						}
						elseif(isset($_POST["txtURL"]) && $_POST["txtURL"] != "")
						{
							$menu->Url = $_POST["txtURL"];
							$menu->PageId = 0;
						}
						else
						{
							$valid = false; 
							$errorMessage = "Een keuze tussen URL en Pagina is verplicht.";
						}
					}
					
					if($valid)
					{
						$menu->Update();
						MenuHatch::DeleteAllByMenu($menu->Id);
						foreach($_POST["ddlHatch"] as $hatchId)
						{
							$menuHatch = new MenuHatch();
							$menuHatch->MenuId = $menu->Id;
							$menuHatch->HatchId = intval($hatchId);
							$menuHatch->Insert();
						} 
						$this->_tpl->assign("message", "Het menu-item is met success gewijzigd.");
						$message = $this->_tpl->parse("General/success");
						unset($_POST);
					}
					else
					{
						$this->_tpl->assign("message", $errorMessage);
						$message = $this->_tpl->parse("General/error");
					} 
				}
				$this->_tpl->assign("message", $message);
				$this->_tpl->assign("ddlLanguageOptions", $this->_generateLanguageOptions(isset($_POST["ddlLanguage"]) ? $_POST["ddlLanguage"] : $menu->Language));
				$this->_tpl->assign("ddlTargetOptions", $this->_generateTargetOptions(isset($_POST["ddlTarget"]) ? $_POST["ddlTarget"] : $menu->Target));
				$this->_tpl->assign("ddlParentOptions", $this->_generateMenuOptions(Menu::SelectAllByParent(null) ,isset($_POST["ddlParent"]) ? $_POST["ddlParent"] : $menu->ParentId));
				$this->_tpl->assign("ddlPageOptions", $this->_generatePageOptions(isset($_POST["ddlPage"]) ? $_POST["ddlPage"] : $menu->PageId));
				
				//select hatched
				$hatches = MenuHatch::SelectByMenu($menu->Id);
				$hatchList = array();
				foreach($hatches as $hatch)
					$hatchList[] = $hatch->HatchId;
					
				$this->_tpl->assign("ddlHatchOptions", $this->_generateHatchOptions(isset($_POST["ddlHatch"]) ? $_POST["ddlHatch"] : $hatchList));
								
				$this->_tpl->assign("txtTitle", isset($_POST['txtTitle']) ? $_POST["txtTitle"] : $menu->Title);
				$this->_tpl->assign("txtURL", isset($_POST['txtURL']) ? $_POST["txtURL"] : $menu->Url);
				
				return $this->_tpl->parse("Admin/Menu/newedit");
				
			}
			else
			{	//overview
			
				//sort actions
				if(defined("GET_3") && GET_3 == "sort")
				{
					if(defined("GET_5") && is_numeric(GET_5))
					{
						if(defined("GET_4") && (GET_4 == "up" || GET_4 == "down"))
						{
							if(GET_4 == "up")
							{
								$menuItem = Menu::Select(GET_5);
								if($menuItem != null && $menuItem->Sort - 1 > 0)
								{
									
									$menuItemSwitch = Menu::SelectBySort($menuItem->ParentId, $menuItem->Language, $menuItem->Sort - 1);
									//optional, for self restore
									if($menuItemSwitch != null)
									{
										$menuItemSwitch->Sort = $menuItem->Sort;
										$menuItemSwitch->Update();
									}
									$menuItem->Sort -= 1;
									$menuItem->Update();
								}
								
							}elseif(GET_4 == "down")
							{
								$menuItem = Menu::Select(GET_5);
								if($menuItem != null && $menuItem->Sort + 1 <= Menu::SelectLastSort($menuItem->ParentId, $menuItem->Language))
								{
									
									$menuItemSwitch = Menu::SelectBySort($menuItem->ParentId, $menuItem->Language, $menuItem->Sort + 1);
									//optional, for self restore
									if($menuItemSwitch != null)
									{
										$menuItemSwitch->Sort = $menuItem->Sort;
										$menuItemSwitch->Update();
									}
									$menuItem->Sort += 1;
									$menuItem->Update();
								}
							}
						}
					}
				}
				
				//delete function
				$message = "";
                if(defined("GET_3") && GET_3 == "delete" && defined("GET_4"))
				{
					$menu = Menu::Select(GET_4);
					if($menu != null)
					{
						$menu->Delete();
						$this->_tpl->assign("message", "Menu item is succesvol verwijderd!");
						$message = $this->_tpl->parse("General/success");
					}
				}
				
				//build overview
				$menus = Menu::SelectAllByParent(null);
				$menuList = $this->_generateMenuStructure($menus);
				$this->_tpl->assign("menuList", $menuList);
                $this->_tpl->assign("message", $message);
				return $this->_tpl->parse("Admin/Menu/overview");
			}
		}
		
		private function _action_component()
		{
			//some security
			if(defined("GET_3") && strpos(GET_3, ".") == false && file_exists("Core/Components/".GET_3.".com.php") && defined("GET_4"))
			{

				include_once("Core/Components/".GET_3.".com.php");
				$com = GET_3;
				$com = new $com();
				if(method_exists($com, "Settings"))
				{
					$settings = $com->Settings();
					if($settings["HasAdmin"])
					{
						return $com->Admin(GET_4);
					}
				}
			}
			return $this->_tpl->parse("Admin/Components/empty");
		}

        private function _action_subscribers()
        {
            if (defined("GET_3") && strtolower(GET_3) == 'new') { //new
                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    $valid = true;
                    $errorMessage = "";
                    $subscriber = new Subscriber();

                    if (!isset($_POST["txtFirstname"]) || trim($_POST["txtFirstname"]) == "") {
                        $valid = false;
                        $errorMessage = "Geen geldige";
                    }
                    else $subscriber->Firstname = $_POST["txtFirstname"];

                    if (!isset($_POST["txtLastname"]) || trim($_POST["txtLastname"]) == "") {
                        $valid = false;
                        $errorMessage = "Geen geldige";
                    }
                    else $subscriber->Lastname = $_POST["txtLastname"];

                    if (!isset($_POST["txtEmail"]) || trim($_POST["txtEmail"]) == "") {
                        $valid = false;
                        $errorMessage = "Geen geldige";
                    }
                    else $subscriber->Email = $_POST["txtEmail"];

                    if ($valid) {
                        $subscriber->Insert();
                        $this->_tpl->assign("message", "Het nieuwsbrief lid is succesvol toegevoegd.");
                        $message = $this->_tpl->parse("General/success");
                        unset($_POST);
                    }
                    else
                    {
                        $this->_tpl->assign("message", $errorMessage);
                        $message = $this->_tpl->parse("General/error");
                    }
                }

                $this->_tpl->assign("message", $message);
                $this->_tpl->assign("txtEmail", isset($_POST["txtEmail"]) ? $_POST["txtEmail"] : "");
                $this->_tpl->assign("txtFirstname", isset($_POST["txtFirstname"]) ? $_POST["txtFirstname"] : "");
                $this->_tpl->assign("txtLastname", isset($_POST["txtLastname"]) ? $_POST["txtLastname"] : "");
                return $this->_tpl->parse("Admin/Subscribers/newedit");

            }
            elseif (defined("GET_3") && strtolower(GET_3) == 'edit')
            { //edit
                $subscriber = Subscriber::Select(GET_4);
                if ($subscriber != null) {
                    if ($_SERVER["REQUEST_METHOD"] == "POST") {
                        $valid = true;
                        $errorMessage = "";

                        if (!isset($_POST["txtFirstname"]) || trim($_POST["txtFirstname"]) == "") {
                            $valid = false;
                            $errorMessage = "Geen geldige";
                        }
                        else $subscriber->Firstname = $_POST["txtFirstname"];

                        if (!isset($_POST["txtLastname"]) || trim($_POST["txtLastname"]) == "") {
                            $valid = false;
                            $errorMessage = "Geen geldige";
                        }
                        else $subscriber->Lastname = $_POST["txtLastname"];

                        if (!isset($_POST["txtEmail"]) || trim($_POST["txtEmail"]) == "") {
                            $valid = false;
                            $errorMessage = "Geen geldige";
                        }
                        else $subscriber->Email = $_POST["txtEmail"];

                        if ($valid) {
                            $subscriber->Update();
                            $this->_tpl->assign("message", "Het nieuwsbrief lid is succesvol aangepast.");
                            $message = $this->_tpl->parse("General/success");
                            unset($_POST);
                        }
                        else
                        {
                            $this->_tpl->assign("message", $errorMessage);
                            $message = $this->_tpl->parse("General/error");
                        }
                    }

                    $this->_tpl->assign("message", $message);
                    $this->_tpl->assign("txtEmail", isset($_POST["txtEmail"]) ? $_POST["txtEmail"] : $subscriber->Email);
                    $this->_tpl->assign("txtFirstname", isset($_POST["txtFirstname"]) ? $_POST["txtFirstname"] : $subscriber->Firstname);
                    $this->_tpl->assign("txtLastname", isset($_POST["txtLastname"]) ? $_POST["txtLastname"] : $subscriber->Lastname);
                    return $this->_tpl->parse("Admin/Subscribers/newedit");
                }
                else
                {
                    header("Location: ".BASE_URL_COMPONENT."/admin/subscribers.html");
                    die();
                }
            }
            else
            { //overview
                $message = "";
                if (defined("GET_3") && GET_3 == "delete" && defined("GET_4") && is_numeric(GET_4)) {
                    $subscriber = Subscriber::Select(GET_4);
                    if ($subscriber != null) {
                        $subscriber->Delete();
                        $this->_tpl->assign("message", "Gebruiker is succesvol verwijderd!");
                        $message = $this->_tpl->parse("General/success");
                    }
                }

                $subscribers = Subscriber::SelectAll();
                $subscriberList = "";
                foreach ($subscribers as $subscriber)
                {
                    $this->_tpl->assign("subscriberId", $subscriber->Id);
                    $this->_tpl->assign("subscriberFirstname", $subscriber->Firstname);
                    $this->_tpl->assign("subscriberLastname", $subscriber->Lastname);
                    $this->_tpl->assign("subscriberEmail", $subscriber->Email);
                    $subscriberList .= $this->_tpl->parse("Admin/Subscribers/overview-item");
                }

                $this->_tpl->assign("message", $message);
                $this->_tpl->assign("subscriberList", $subscriberList);
                return $this->_tpl->parse("Admin/Subscribers/overview");
            }
        }

        private function _action_users()
        {
            if (defined("GET_3")) {
                if (strtolower(GET_3) == "new" || (strtolower(GET_3) == "edit" && defined("GET_4") && is_numeric(GET_4))) {
                    $edit = false;
                    $user = new User();

                    if (is_numeric(GET_4)) {
                        $user = User::Select(GET_4);
                        $edit = true;
                    }

                    if ($_SERVER["REQUEST_METHOD"] == "POST") {

                    }

                    $this->_tpl->assign("txtFirstName", isset($_POST["txtFirstName"]) ? $_POST["txtFirstName"] : ($user->FirstName != "" ? $user->FirstName : ""));
                    $this->_tpl->assign("txtLastName", isset($_POST["txtLastName"]) ? $_POST["txtLastName"] : ($user->LastName != "" ? $user->LastName : ""));
                    $this->_tpl->assign("txtEmail", isset($_POST["txtEmail"]) ? $_POST["txtEmail"] : ($user->Email != "" ? $user->Email : ""));
                    $this->_tpl->assign("checkCcChecked", isset($_POST["checkCc"]));



                    if ($edit) {
                        $this->_tpl->assign("displayCc", "display:none;");
                        $this->_tpl->assign("checkCcChecked", "");
                    }

                    return $this->_tpl->parse("Admin/Users/newedit");
                } elseif (strtolower(GET_3) == "delete" && defined("GET_4") && is_numeric(GET_4)) { //delete user
                    $user = User::Select(GET_4);
                    if ($user != null) {
                        $user->Delete();
                        $this->_tpl->assign("message", "Gebruiker met succes verwijderd");
                        $this->_tpl->assign("message", $this->_tpl->parse("General/error"));
                    }
                }
            }

            $users = User::SelectAll();
            if (count($users) > 0) {
                $this->_generateUserList($users);
            } else {
                $this->_tpl->assign("colspan", 5);
                $this->_tpl->assign("userList", $this->_tpl->parse("General/empty-tr"));
            }
            return $this->_tpl->parse("Admin/Users/overview");
        }

        private function _generateUserList($users)
        {
            $userList = "";

            foreach ($users as $user) {
                $this->_tpl->assign("userId", $user->Id);
                $this->_tpl->assign("userName", $user->FirstName. " " . $user->LastName);
                $this->_tpl->assign("userEmail", General::SafeEmail($user->Email));

                if ($user->LastLogOn != 0) {
                    $this->_tpl->assign("userLastLoginTime", date("d-m-Y H:i:s", $user->LastLogOn));
                    $this->_tpl->assign("userLastLogIn", date("d-m-Y", $user->LastLogOn));
                } else $this->_tpl->assign("userLastLogIn", "Geen datum bekend");

                if ($user->Created != 0) {
                    $this->_tpl->assign("userCreatedTime", date("d-m-Y H:i:s", $user->Created));
                    $this->_tpl->assign("userCreated", date("d-m-Y", $user->Created));
                } else $this->_tpl->assign("userCreated", "Geen datum bekend");

                $userList .= $this->_tpl->Parse("Admin/Users/overview-item");
            }
            $this->_tpl->assign("userList", $userList);
        }

        private function _action_photo()
        {
            $message = "";
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (isset($_POST["btnUpload"])) {
                    if (isset($_FILES["flPhoto"]["name"]) && $_FILES["flPhoto"]["name"] != "") {
                        if ($_FILES["flPhoto"]["size"] <= (1024 * 1024)) {
                            $allowedTypes = array();
                            $allowedTypes[] = 'image/jpg';
                            $allowedTypes[] = 'image/jpeg';
                            $allowedTypes[] = 'image/pjpeg';
                            $allowedTypes[] = 'image/gif';
                            $allowedTypes[] = 'image/png';

                            if (in_array($_FILES["flPhoto"]["type"], $allowedTypes)) {
                                $filename = sha1(microtime());
                                $filepath = "uploads/" . $filename . '.jpg';
                                if (move_uploaded_file($_FILES["flPhoto"]["tmp_name"], $filepath)) {
                                    General::ResizePhoto($filepath, 900);
                                    General::CreateThumbnail($filepath, "uploads/thumbs/" . $filename . ".jpg", 300);
                                    $photo = new Photo();
                                    $photo->Filename = $filename;
                                    $photo->Title = isset($_POST["txtTitle"]) && $_POST["txtTitle"] != "" ? $_POST["txtTitle"] : $_FILES["flPhoto"]["name"];
                                    $photo->Insert();

                                    $this->_tpl->assign("message", "De afbeelding is met succes geupload.");
                                    $message = $this->_tpl->parse("General/success");
                                } else {
                                    $this->_tpl->assign("message", "De afbeelding is om onbekende reden niet geupload.");
                                    $message = $this->_tpl->parse("General/error");
                                }
                            } else {
                                $this->_tpl->assign("message", "De afbeelding dient van het type JPG te zijn.");
                                $message = $this->_tpl->parse("General/error");
                            }
                        } else {
                            $this->_tpl->assign("message", "De afbeelding is te groot, maximaal 1Mb.");
                            $message = $this->_tpl->parse("General/error");
                        }
                    }
                } elseif (isset($_POST["btnUpdate"])) {
                    foreach ($_POST as $key => $value) {
                        if (substr($key, 0, 8) == "txtTitle") {
                            $photoId = substr($key, 9);
                            if (is_numeric($photoId)) {
                                $photo = Photo::Select($photoId);
                                if ($photo != null) {
                                    $photo->Title = $value;
                                    $photo->Update();
                                }
                            }
                        }
                    }
                    $this->_tpl->assign("message", "De wijzigingen zijn succesvol opgeslagen.");
                    $message = $this->_tpl->parse("General/success");
                }
            }

            if (defined("GET_3") && GET_3 == "delete" && defined("GET_4") && is_numeric(GET_4)) {
                $photo = Photo::Select(GET_4);
                if ($photo != null) {
                    $photo->Delete();
                    $this->_tpl->assign("message", "De foto is succesvol verwijderd.");
                    $message = $this->_tpl->parse("General/success");
                }
            }

            $photos = Photo::SelectAll();
            $photoList = "";
            foreach ($photos as $photo) {
                $this->_tpl->assign("photoId", $photo->Id);
                $this->_tpl->assign("photoFilename", $photo->Filename);
                $this->_tpl->assign("photoTitle", htmlspecialchars($photo->Title));
                $photoList .= $this->_tpl->parse("Admin/Photo/overview-item");
            }

            $this->_tpl->assign("message", $message);
            $this->_tpl->assign("photoList", $photoList);
            return $this->_tpl->parse("Admin/Photo/overview");
        }

        private function _action_file()
        {
            if (defined("GET_3") && strtolower(GET_3) == "delete" && defined("GET_4") && is_numeric(GET_4)) {
                $fileObject = File::Select(GET_4);
                if ($fileObject != null) {
                    @unlink("uploads/files/" . $fileObject->Name);
                    $fileObject->Delete();

                    $this->_tpl->assign("message", "Bestand met succes verwijderd");
                    $this->_tpl->assign("message", $this->_tpl->parse("General/success"));
                }
            }
            if (defined("GET_3") && strtolower(GET_3) == "download" && defined("GET_4") && is_numeric(GET_4)) {
                header("Content-Type: application/octet-stream");

                $fileObject = File::Select(GET_4);

                if ($fileObject != null) {
                    $file = $fileObject->Name;

                    $filePath = "uploads/files/" . $file;

                    header("Content-Disposition: attachment; filename=" . urlencode($file));
                    header("Content-Type: application/force-download");
                    header("Content-Type: application/octet-stream");
                    header("Content-Type: application/download");
                    header("Content-Description: File Transfer");
                    header("Content-Length: " . filesize($filePath));
                    flush();
                    $fp = fopen($filePath, "r");
                    while (!feof($fp)) {
                        echo fread($fp, 65536);
                        flush();
                    }
                    fclose($fp);
                }
            }
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                $errorMessage = "";
                $valid = true;

                if (in_array($_FILES['flFile']["type"], General::$FileMimeTypes)) {
                    $file = new File();
                    $fileExtension = end(explode('.', $_FILES['flFile']["name"]));
                    $filename = $_POST["txtFileName"] . "." . $fileExtension;

                    $existingFile = File::SelectByName($filename);

                    if ($existingFile == null) {
                        $target = "uploads/files/" . $filename;
                        //upload
                        if (@move_uploaded_file($_FILES["flFile"]["tmp_name"], $target)) {
                            $file->Extension = $fileExtension;
                            $file->Name = $filename;
                            $file->Created = microtime();
                            $file->Insert();
                        } else {
                            $valid = false;
                            $errorMessage = "Onbekende fout tijdens het uploaden";
                        }
                    } else {
                        $fileObject->Modified = microtime();
                        $fileObject->Update();
                    }
                } else {
                    $valid = false;
                    $errorMessage = "Onbekend bestandsformaat: " . $_FILES['flFile']["type"];
                }

                if (!$valid) {
                    $this->_tpl->assign("message", $errorMessage);
                    $this->_tpl->assign("message", $this->_tpl->parse("General/error"));
                } else {
                    $this->_tpl->assign("message", "Bestand is met succes geupload");
                    $this->_tpl->assign("message", $this->_tpl->parse("General/success"));
                }
            }
            $files = File::SelectAll();

            $fileList = "";
            if (count($files) > 0) {
                foreach ($files as $file) {
                    $this->_tpl->assign("fileLink", SETTING_ROOT."/uploads/files/" . $file->Name);
                    $this->_tpl->assign("fileId", $file->Id);
                    $this->_tpl->assign("fileName", $file->Name);
                    $this->_tpl->assign("fileCreated", date("d-m-Y", $file->Created));
                    $fileList .= $this->_tpl->parse("Admin/File/overview-item");
                }

            } else {
                $this->_tpl->assign("colspan", 5);
                $this->_tpl->assign("message", "Geen bestanden gevonden");
                $fileList = $this->_tpl->parse("General/empty-tr");
            }

            $this->_tpl->assign("fileList", $fileList);
            return $this->_tpl->parse("Admin/File/overview");
        }

        private $Lang;

        /**
         * This function is responsible for the language (array) in de the folder Core/Languages
         * The function produces columns of the language items in the available languages
         * @return string Returns the right Admin template
         */
        private function _action_siteitems()
        {
            #@var $lang array
            /**
             * @var $lang array
             */
            $languages = General::LoadLanguages();
            $headerItems = "";
            $langList = array();
            foreach ($languages as $langId) { // Check for existing language files
                if (file_exists("Core/Languages/" . $langId . "/" . strtolower($langId) . ".lang.php"))
                    require("Core/Languages/" . $langId . "/" . strtolower($langId) . ".lang.php");
                $langList[$langId] = $lang; // Language header i.e. (NL || EN)
                $this->_tpl->assign("headerItemName", $langId); // Represents the column
                $headerItems .= $this->_tpl->parse("Admin/SiteItems/header-lang-item");
            }
            $this->_tpl->assign("headerItems", $headerItems);

            $langListItems = array();
            foreach ($langList[DEFAULT_LANGUAGE] as $langListItem => $data) { // Checks the language items for the default language
                if (!strstr($langListItem, "Admin") || false)
                    $langListItems[] = $langListItem;
            }
            $this->_tpl->assign("displaySuperAdmin", true ? "display:none;" : ""); // Notice about appending new languages

            $tableBody = "";
            foreach ($langListItems as $langListItem) { // Produces language table body, all language items
                $bodyItems = "";
                foreach ($languages as $langId) {
                    $this->_tpl->assign("langItemName", $langListItem);
                    $this->_tpl->assign("lang", $langId);
                    $this->_tpl->assign("bodyLangItem", $langList[$langId][$langListItem]);

                    $bodyItems .= $this->_tpl->parse("Admin/SiteItems/body-lang-item");
                }
                $this->_tpl->assign("langItems", $bodyItems);
                $this->_tpl->assign("langItemName", $langListItem);

                $tableBody .= $this->_tpl->parse("Admin/SiteItems/overview-item");
            }
            $this->_tpl->assign("siteItemsList", $tableBody);
            return $this->_tpl->parse("Admin/SiteItems/overview");
        }

		private function _generateMenuStructure($menus, $level = 0)
		{
			//temp storage of all hatches
			$hatches = Hatch::SelectAll();
            $hatchTranslations = HatchTranslation::SelectAll();

			$hatchList = array();
			foreach($hatches as $hatch)
				$hatchList[$hatch->Id] = $hatch;
			


			$menuList = "";
			foreach($menus as $menu)
			{
				$this->_tpl->assign("menuId", $menu->Id);
				$this->_tpl->assign("menuLanguage", $menu->Language);
				if($menu->PageId > 0)
				{
					$this->_tpl->assign("menuLink", $menu->_otherFields["PageTitle"]);
				}
				else
				{
					$this->_tpl->assign("menuLink", $menu->Url);
				}
				
				if($level > 0)
				{
					$icon = $this->_tpl->parse("Admin/Menu/sub-icon");
					$spacer = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					$space = "";
					for($i = 0; $i < $level - 1; $i++)
						$space .= $spacer;
					
					$this->_tpl->assign("menuTitle", $space.$icon.$menu->Title);
				}
				else
				{
					$this->_tpl->assign("menuTitle", $menu->Title);
				}
				
				$menuHatches = MenuHatch::SelectByMenu($menu->Id);
				$hatchString = "";
				foreach($menuHatches as $menuHatch)
                {
                    $hatchName = 'Unkown';
                    foreach ($hatchTranslations as $ht) {
                        if ($ht->HatchId == $menuHatch->HatchId && $ht->Lang == DEFAULT_LANGUAGE) {
                            $hatchName = $ht->Name;
                            break;
                        }
                    }
                    $hatchString .= htmlspecialchars($hatchName).", ";
                }
					
				if($hatchString != "")
					$hatchString = substr($hatchString, 0, strlen($hatchString) -2);
				
				$this->_tpl->assign("menuHatch", $hatchString);
				$menuList .= $this->_tpl->parse("Admin/Menu/overview-item");
				
				$subs = Menu::SelectAllByParent($menu->Id);
				if($subs != null)
					$menuList .= $this->_generateMenuStructure($subs, ($level + 1));			
			}
			return $menuList;
		}
		
		private function _generateLanguageOptions($selectedId)
		{
			$languages = General::LoadLanguages();
			$list = "";
			foreach($languages as $lang)
			{
				$this->_tpl->assign("value",$lang);
				$this->_tpl->assign("name",$lang);
				$this->_tpl->assign("selected", $lang == $selectedId ? "selected" : "");
				$list .= $this->_tpl->parse("General/select-option");
			}
			return $list;
		}
		
		private function _generateComponentOptions($selectedId)
		{
			$components = General::LoadComponents();
			$list = "";
			foreach($components as $component)
			{
				if($component["Pageable"])
				{
					foreach($component["ContentOptions"] as $key => $name)
					{
						$value = $component["Filename"].";".$key;
						$this->_tpl->assign("value",$value);
						$this->_tpl->assign("name",$component["DisplayName"] . " - ". $name ." (V". $component["Version"]. ")" );
						$this->_tpl->assign("selected", $value == $selectedId ? "selected" : "");
						$list .= $this->_tpl->parse("General/select-option");
					}
				}
			}
			return $list;
		}
		
		private function _generateTargetOptions($selectedId)
		{
			$options = array("_blank" => "Nieuw venster",
							 "_self" => "Zelfde venster");
			$list = "";
			foreach($options as $key => $value)
			{
				$this->_tpl->assign("value",$key);
				$this->_tpl->assign("name", $value );
				$this->_tpl->assign("selected", $key == $selectedId ? "selected" : "");
				$list .= $this->_tpl->parse("General/select-option");
			}
			
			return $list;
		}

		private function _generateMenuOptions($menus, $selectedId, $level = 0)
		{
			$menuList = "";
			foreach($menus as $menu)
			{
				$this->_tpl->assign("value", $menu->Id);
				
				if($level > 0)
				{
					$spacer = "-> ";
					$space = "";
					for($i = 0; $i < $level; $i++)
						$space .= $spacer;
					
					$this->_tpl->assign("name",  $space.$menu->Title." (".$menu->Language.")");
				
				}
				else
				{
					$this->_tpl->assign("name",$menu->Title." (".$menu->Language.")");
				}
				
				$this->_tpl->assign("selected", $menu->Id == $selectedId ? "selected" : "");
				$menuList .=  $this->_tpl->parse("General/select-option");
				
				$subs = Menu::SelectAllByParent($menu->Id);
				if($subs != null)
					$menuList .= $this->_generateMenuOptions($subs, $selectedId,($level + 1));			
			}
			return $menuList;
		}
		
		private function _generatePageOptions($selectedId)
		{
			$pages = Page::SelectAll();
			$list = "";
			foreach($pages as $page)
			{
				$this->_tpl->assign("value",$page->Id);
				$this->_tpl->assign("name",$page->PageTitle . " (". $page->Language.")");
				$this->_tpl->assign("selected", $page->Id == $selectedId ? "selected" : "");
				$list .= $this->_tpl->parse("General/select-option");
			}
			return $list;
		}

        private function _generateHatchOptions($selectedIds)
        {
            $hatches = Hatch::SelectAll();
            $hatchTranslations = HatchTranslation::SelectAll();
            $list = "";

            foreach ($hatches as $hatch) {
                $hatchName = 'Unkown';
                foreach ($hatchTranslations as $ht) {
                    if ($ht->HatchId == $hatch->Id && $ht->Lang == DEFAULT_LANGUAGE) {
                        $hatchName = $ht->Name;
                        break;
                    }
                }

                $this->_tpl->assign("value", $hatch->Id);
                $this->_tpl->assign("name", $hatchName);

                if (is_array($selectedIds))
                    $this->_tpl->assign("selected", in_array($hatch->Id, $selectedIds) ? "selected" : "");
                else
                    $this->_tpl->assign("selected", "");

                $list .= $this->_tpl->parse("General/select-option");
            }
            return $list;

        }
		
		private function _generateElementOptions($selectedId)
		{
			$elements = General::LoadElements();
			$list = "";
			foreach($elements as $element)
			{
				if($element["Pageable"])
				{
					$this->_tpl->assign("value",$element["Filename"]);
					$this->_tpl->assign("name",$element["DisplayName"] . " (V". $element["Version"]. ")" );
					$this->_tpl->assign("selected", $element["Filename"] == $selectedId ? "selected" : "");
					$list .= $this->_tpl->parse("General/select-option");
				}
			}
			return $list;
		}
		
		private function _generateLayoutOptions($selectedId)
		{
			$layouts = General::LoadLayouts();
			$list = "";
			foreach($layouts as $layout)
			{
				$this->_tpl->assign("value",$layout);
				$this->_tpl->assign("name", $layout);
				$this->_tpl->assign("selected", $layout == $selectedId ? "selected" : "");
				$list .= $this->_tpl->parse("General/select-option");
			}
			return $list;
		}
		
		private function _generatePhotoOptions($selectedIds)
		{
			$photos = Photo::SelectAll();
			$list = "";
			foreach($photos as $photo)
			{
				$this->_tpl->assign("value",$photo->Id);
				$this->_tpl->assign("name",$photo->Title." - (#".$photo->Id.")");
				
				if(is_array($selectedIds))
					$this->_tpl->assign("selected", in_array($photo->Id, $selectedIds) ? "selected" : "");
				else
					$this->_tpl->assign("selected", "");
				
				$list .= $this->_tpl->parse("General/select-option");
			}
			return $list;
		}
	} 

?>