HEX
Server: Apache
System: Linux v38079.2is.nl 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: democfellows (10015)
PHP: 8.1.34
Disabled: opcache_get_status
Upload Files
File: /var/www/vhosts/creativefellows.nl/sht.creativefellows.nl/classes/CassetteController.php
<?php

	class CassetteController extends BaseController{
		
		function __construct($db_connection,$router=null,$view=null,$settings=null)
		{
			$this->db 				= $db_connection;
			$this->router			= $router;
			$this->view 			= $view;
			$this->settings			= $settings;	
			
			$client_settings 		= $this->getClientSettings();
				
			$this->config			= new BaseController($settings,$client_settings);				
			
		}


		/*
		 * View page 
		 */
		public function viewPage($request, $response, $args)
		{
			
			// get topbar navigation
			$navigation = $this->getMainNavigation(true,$args["name"]);
				
			// get url parameters
			$this->section_name 	= isset($args["name"]) ? $args["name"] : null;
			$this->category_name 	= isset($args["category"]) ? $args["category"] : null;
			$current_pagination 	= isset($args["pagination"]) ? $args["pagination"] : 1;
			
			
			// get page contents
			$page 	= $this->getPage($this->section_name,$this->category_name,0,$current_pagination);
				
			
			// return 404 if no page has been found
			if(empty($page)) return $this->get404Page($request, $response, $args);
			
			
			// return the page
			return $this->view->render($response, $this->getPageCanvas($page[0]->page_canvas), array(
				"cassette"		=> $this,
				"config" 		=> $this->config,
				"router" 		=> $this->router, 
				"page" 			=> $page, 
				"navigation" 	=> $navigation,
				"section_name"	=> $this->section_name,
				"pagination" 	=> ["pagination_rows_total" => $this->pagination_rows_total, "pagination_count" => $this->pagination_count, "current_page" => $current_pagination, "next_page" => $current_pagination + 1 , "previous_page" => $current_pagination - 1 ]
				//"pagecount"		=> $this->getPageCount()
			));
			
		}
		
		public function viewCategory($request, $response, $args)
		{
			// get url parameters
			$this->section_name 		= isset($args["name"]) ? $args["name"] : null;
			$this->category_name 		= isset($args["category"]) ? $args["category"] : null;
			$this->entry_name			= isset($args["entry"]) ? $args["entry"] : null;
			
			// get topbar navigation
			$navigation 		= $this->getMainNavigation(true,$this->section_name);
			
			// get page contents
			$page 				= $this->getCategoryEntries($this->section_name,$this->category_name,$this->entry_name);
			
			// return 404 if no page has been found
			if(empty($page)) return $this->get404Page($request, $response, $args);
			
			
			// return the page
			return $this->view->render($response, $this->getPageCanvas($page[0]->page_canvas), array(
				"cassette"		=> $this,
				"config" 		=> $this->config,
				"router" 		=> $this->router, 
				"page" 			=> $page, 
				"navigation" 	=> $navigation,
				"category_name"	=> $this->category_name,
				"entry_name"	=> $this->entry_name,
				"pagination" 	=> ["pagination_rows_total" => $this->pagination_rows_total, "pagination_count" => $this->pagination_count, "current_page" => $current_pagination, "next_page" => $current_pagination + 1 , "previous_page" => $current_pagination - 1 ]
				//"pagecount"		=> $this->getPageCount()
			));
			
			
		}
		
		
		
		private function getPageCount()
		{
			return $this->pageCount;
		}
		
		/*
		 * Return a 404 page 
		 */
		private function get404Page($request, $response, $args)
		{
			$page 	= [];
			$page[] = new EntryController(null,null,null,null,null);
			
			// get topbar navigation
			$navigation = $this->getMainNavigation(true,$args["name"]);
					
			// render 404 page				
			$this->view->render($response, '404.php',array(
				"cassette"		=> $this,
				"config"		=> $this->config,
				"page"			=> $page,
				"navigation" 	=> $navigation
			));
			
			return $response->withStatus(404); 
		}
		
		
		/*
		 * Get the client settings
		 */
		private function getClientSettings()
		{
			
			$sql 		= "SELECT * FROM `instellingen` WHERE `id`='1'";
			$sth 		= $this->db->prepare($sql);
			$settings 	= $sth->execute();
				
			if($settings) return $sth->fetch();
			
		}
		
		
		/*
		 * Get uid data on post url
		 */
		private function getPageData($post_url)
		{
			$sql = "SELECT * FROM unique_ids WHERE url = :url AND status = 2";
			$sth = $this->db->prepare($sql);
			
			$sth->execute(["url" => $post_url]);
			
			return $sth->fetch();
		}
		
		
		/*
		 * Get uid data by id
		 */
		private function getPageDataById($uid)
		{
			$sql = "SELECT * FROM unique_ids WHERE unique_id = :uid";
			$sth = $this->db->prepare($sql);
			
			$sth->execute(["uid" => $uid]);
			
			return $sth->fetch();
		}
		
		
		private function getPageCanvas($canvas_name=null)
		{
			return "view.". ($canvas_name != null ? $canvas_name : "default") .".php";
		}		
		
		private function getCategoryId($sectie_id,$category_name)
		{

			$sql = "SELECT * FROM categories WHERE naam LIKE :cname AND sectie_id = :section_id AND active =1";
			
			$sth = $this->db->prepare($sql);
			$sth->execute([
				"cname" => $this->validDBpar($category_name),
				"section_id" => $sectie_id
			]);	
			
			$category = $sth->fetch();
			return $category["category_id"];
			
		}
		
		
		private function getCategoryEntries($section_name=null,$category_name=null,$entry_name=null)
		{
			
			if($entry_name != null) return $this->getPage($entry_name);
			
			$section 		= $this->getSection($section_name);
			$category_id 	= $this->getCategoryId($section["sectie_id"],$category_name);
			
			$sql = "SELECT *,unique_ids.unique_id as uid 
				FROM `entry_categories`,`unique_ids`
				WHERE `entry_categories`.`unique_id` = `unique_ids`.`unique_id` 
				AND category_id = :cat_id
				AND `status`='2' ";
				
			$sth = $this->db->prepare($sql);
			$sth->execute([
				"cat_id" => $category_id
			]);
					
			$uids = [];
		
	        while($entry = $sth->fetch())
			{	
			
				// template fields
				$fields 	= $this->getTemplateFields($entry["template_id"]);	
		
		
				// get tablename
				$table 		= $this->getTableName($entry["template_id"]);
		
		
				// get entry data 
				$entry_data = $this->getEntryData($table,$entry["entry_id"]);
			

				// get the form data
				$form 		= $this->getForm($entry_data["form_id"]);
			
			
				foreach($fields as $i => $f)
				{
					$fields[$i]["html"]  = $this->getFieldHTML($entry["uid"],$f["field_naam"],$f["form_element"],$entry_data[$f["field_naam"]],$f["template_field_id"]);
				}
			
				//die();
				$uids[] = new EntryController($entry,$fields,$entry_data,$form,$section["naam"]);	
			
			}
			return $uids; 
											
			
		}
		
		
		public function getPage($post_url=null,$post_category=null, $entry_id=0, $current_pagination=0,$set_pagination=true)
		{
			
		
			/*
			 * Default homepage, not post data
			 */
			if($post_url === null && $entry_id === 0)
			{
				$section 	= $this->settings["defaultHomeSection"];	
				$section 	= $this->getSection($section);
							
				$uid_array 	= null;
			}
			elseif($entry_id != 0)
			{
				/*
				 * Get entry data on post url
				 */
				$uid_array = $this->getPageDataById($entry_id);	
		
				
				if( !empty($uid_array) ) $section = $this->getSection($uid_array["sectie_id"]);
				else return null;
				
			}
			else
			{
		
				/*
				 * Check if post is a section
				 */
				$section = $this->getSection($post_url);
				
				
				if( empty($section) )
				{
					/*
					 * Get entry data on post url
					 */
					$uid_array = $this->getPageData($post_url);	
					
					
					if( !empty($uid_array) ) $section = $this->getSection($uid_array["sectie_id"]);
					else return null;
				
				}
				
			}
						
			/*
			 * Get section sort & limit
			 */
			$sort_query 	= $this->getSectionSort($section["sort"]);
			$limit_query 	= $set_pagination == true ? $this->getQueryLimit($current_pagination) : "";
			
			
			/*
			 * Get toplevel entries
			 */
			if($uid_array == null)
			{
				
				$sql = "SELECT *,unique_ids.unique_id as uid 
					FROM unique_ids 
					LEFT JOIN entry_categories ON unique_ids.unique_id = entry_categories.unique_id 
					WHERE unique_ids.sectie_id = :sectie_id 
					AND unique_ids.status = 2 
					AND unique_ids.language_id = '0' 
					AND entry_categories.entry_category_id IS NULL";
					
				$sth = $this->db->prepare($sql . $sort_query . $limit_query);
				$sth->execute([
					"sectie_id" => $section["sectie_id"]
				]);
				
								
				$count_sth = $this->db->prepare($sql);
				$count_sth->execute([
					"sectie_id" => $section["sectie_id"]
				]);
				
				if($set_pagination == true) $this->setPagination($sth->rowCount(),$count_sth->rowCount());
				
			}
			else{
				
				//d($uid_array);
				
				$sql = "SELECT *,unique_ids.unique_id as uid
					FROM unique_ids 
					WHERE unique_ids.sectie_id = :sectie_id 
					AND unique_ids.status = 2 
					AND unique_ids.language_id = '0' 
					AND unique_ids.unique_id = :uid ";
					
				$sth = $this->db->prepare($sql);
				$sth->execute([
					"sectie_id" => $section["sectie_id"],
					"uid" => $uid_array["unique_id"]
				]);
					
				if($set_pagination == true) $this->setPagination($sth->rowCount(),$sth->rowCount());
					
			}
				
			$uids = [];
			
	        while($entry = $sth->fetch())
			{	
			
				$uids[] = $this->getEntry($entry,$section);
				
			}
			
			//d($uids);
			
			return $uids; 
			
		}
		
		
		private function getEntry($entry,$section_arr=null)
		{
			
						
			// template fields
			$fields 	= $this->getTemplateFields($entry["template_id"]);	
		
		
			// get tablename
			$table 		= $this->getTableName($entry["template_id"]);
		
		
			// get entry data 
			$entry_data = $this->getEntryData($table,$entry["entry_id"]);
			

			// get the form data
			$form 		= $this->getForm($entry_data["form_id"]);
			
			
			foreach($fields as $i => $f)
			{
				$fields[$i]["html"]  = $this->getFieldHTML($entry["uid"],$f["field_naam"],$f["form_element"],$entry_data[$f["field_naam"]],$f["template_field_id"]);
			}
			
			//die();
			return new EntryController($entry,$fields,$entry_data,$form,$section_arr);	
			
			
		}
		
		
		/*
		 * Set pagination
		 */
		private function setPagination($rows,$total_rows)
		{
			$this->pagination_rows_total = $total_rows;
			$this->pagination_count = ceil($total_rows / $this->settings["items_per_page"]);
		}
		
		
		/*
		 * Get form
		 */
		public function getForm($form_id)
		{
					
			if($form_id == 0) return null;
			
			
			$form_data 		= $this->getFormData($form_id);						
			$form_fields 	= $this->getFormFields($form_id,$form_data["unique_id"]);

			return new CassetteForm($this->settings,$form_data,$form_fields);
		}
		
		private function getFormData($form_id)
		{
			
			$sql = "SELECT * FROM forms WHERE form_id = :form_id";
			$sth = $this->db->prepare($sql);
			
			$sth->execute(["form_id" => $form_id]);
			
			return $sth->fetch();
		}
		
		private function getFormFields($form_id, $unique_id)
		{
			$sql = "SELECT * FROM form_elements WHERE unique_id = :unique_id ORDER BY position ASC";
			$sth = $this->db->prepare($sql);
			
			$sth->execute(["unique_id" => $unique_id]);
			
			$fields = [];
			while($field = $sth->fetch())
			{
				
				$e = array();
				$e["elementId"] 	= $field["form_element_id"];
				$e["type"] 			= $field["field_id"];
				$e["required"] 		= $field["value"];
				

				$field_data 		= $this->getFormField($field);
				foreach($field_data as $t => $v){
					$e[$t] = $v;
				}
				
				$fields[] = $e;
				
			}
			
			// add form id
			$fields[] = $this->addFormId($form_id);		
		
			// add form id
			if($this->settings["captcha_sitekey"] != null) $fields[] = $this->addCaptcha();	
		
			// add a csrf			
			$fields[] = $this->addCsrf();		

			// add a button			
			$fields[] = $this->addButton();			
						
			return $fields;
		}
		
		private function addCaptcha()
		{
			$d 				= array();
			$d["type"] 		= "captcha";
			$d["name"] 		= $this->settings["captcha_sitekey"];
			$d["desc"] 		= $this->settings["captcha_sitekey"];

			return $d;
		}

		private function addMsg()
		{
			$d 				= array();
			$d["type"] 		= "msg";
			$d["name"] 		= "*) Mandatory field";
			$d["desc"] 		= "";

			return $d;
		}
	
		private function addButton()
		{
			$d 				= array();
			$d["type"] 		= "submit";
			$d["name"] 		= "";//$naam;
			$d["desc"] 		= "";//$desc;

			return $d;
		}

		private function addFormId($form_id)
		{
			$d 				= array();
			$d["type"] 		= "hidden";
			$d["name"] 		= "formid";
			$d["desc"] 		= $form_id;

			return $d;
		}
	
		private function addCsrf()
		{
			$d 				= array();
			$d["type"] 		= "hidden";
			$d["name"] 		= "csrf";
			$d["desc"] 		= $this->settings["csrf_token"];

			return $d;
		}
		
		private function getFormField($field_array)
		{
			
			$form_el_id		= $field_array["form_element_id"];
			$field_id		= $field_array["field_id"];
			$naam			= $field_array["name"];
			$desc			= $field_array["description"];
				
			$d 				= [];
			$d["valueName"] = $form_el_id . preg_replace("/[^0-9a-zA-Z]/","",strtolower($naam));
			
			$elementName 	= preg_replace("/ /","",$form_el_id."-".$naam);

			switch($field_id){
				// input
				case 1:
			
					$d["type"] = "input";
					$d["name"] = $naam;
					$d["desc"] = $desc;
				
				break;
				
				// input
				case 68:
			
					$d["type"] = "inactiveinput";
					$d["name"] = $naam;
					$d["desc"] = $desc;
				
				break;

				//dropdown
				case 28:
			
					$d["type"] = "select";
					$d["name"] = $naam;
					$d["desc"] = $desc;

					$o = array();
				
					$element_options = $this->getFormElementOptions($form_el_id); 
					foreach($element_options as $r)
					{
						array_push($o,$r['value']);
					}
					$d["options"] = $o;

				break;

				//radio
				case 31:
			
					$d["type"] = "radio";
					$d["name"] = $naam;
					$d["desc"] = $desc;

					$o = array();
					$element_options = $this->getFormElementOptions($form_el_id); 
					foreach($element_options as $r)
					{
						array_push($o,$r['value']);
					}
					$d["options"] = $o;
				
				break;

				//checkbox
				case 24:
					$d["type"] = "checkbox";
					$d["name"] = $naam;
					$d["desc"] = $desc;

					$o = array();
					$element_options = $this->getFormElementOptions($form_el_id); 
					foreach($element_options as $r){
						array_push($o,$r['value']);
					}
					$d["options"] = $o;
				break;

				//multi text line
				case 5:
					$d["type"] = "textarea";
					$d["name"] = $naam;
					$d["desc"] = $desc;
				break;

				case 15:
					$d["type"] = "deactivatedfield";
					$d["name"] = $naam;
					$d["desc"] = $desc;
				break;

				case 14:
					$d["type"] = "datepicker";
					$d["name"] = $naam;
					$d["desc"] = $desc;
				break;

				case 16:
					$d["type"] = "inactiveinput";
					$d["name"] = $naam;
					$d["desc"] = $desc;
				break;

				case 32:
					$d["type"] = "tussenkop";
					$d["name"] = $naam;
					$d["desc"] = $desc;
				break;
				case 18:
					$d["type"] = "bijlage";
					$d["name"] = $naam;
					$d["desc"] = $desc;
				break;

			}
			return $d;	
			
		}
		
		private function getFormElementOptions($form_element_id)
		{
			$sql = "SELECT * FROM form_elements_options WHERE form_element_id = :form_el_id ORDER BY form_option_id ASC";
			$sth = $this->db->prepare($sql);
			
			$sth->execute(["form_el_id" => $form_element_id]);
			
			return $sth->fetchAll();
			
		}
		
		
		/*
		 * Get the html contens on a db field 
		 */
		private function getFieldHTML($unique_id,$field_user,$element,$value,$template_field_id=null)
		{
			$field_user 	= preg_replace("/[^0-9a-zA-Z]/","_",strtolower($field_user));
			$value 			= stripslashes($value);
			$data 			= array();
			
						
			switch($element)
			{
				default:
					return $value;
				break;
				
				case "image":
					return explode("|*|",$value);
				break;
				
				case "files":
				
					$files 	= explode("|*|",$value);
					foreach($files as $f){

						$file_data 	= explode("|-|",$f);
						if($file_data[0] == "") continue;

						if(end($file_data) == ""){
							$parts = explode("/",preg_replace("/_/"," ",$file_data[0]));
							$file_data[1] = end($parts);
						}
						array_push($data,$file_data);
					}
					return $data;
					
				break;
				
				case "link":
				case "cols":				
					if(trim($value) != "")
					{
						$files 	= explode("|*|",$value);
						foreach($files as $f)
						{
							$file_data 	= explode("|-|",$f);							
							array_push($data,$file_data);
						}
						
					}
					else $data = array();
				
					return $data;
				break;
				
				case "checkbox":
				case "radio":
					return explode("|*|",$value);
				break;
				
				case "graph":
					
					$values = explode("|*|",$value);
					
					
					$data 	= array();
					
					foreach($values as $group)
					{	
						$arr = explode("	",$group);	
						
						$data[] = $arr;
					}
					
					
					return $data;
					
					/*
					$data 	= array();
					
					
					foreach($values as $group)
					{	
						
						$d = array();
						
						$group_fields = explode("|-|",$group);
						foreach($group_fields as $gf)
						{
							//echo ">>>>".$gf;
							$d[] = explode("	",$gf);	
						}
						
						$data[] = $d;	
					}
					
					//d($data);
					
					return $data;//explode("|*|",$value);*/
					
				break;
				
				case "dragdrop":
				
					// get toplevel sections				
					$sections = $this->getSectionContent($unique_id);
					
					$html = "";
										
					foreach($sections as $i => $s)
					{
						
						
						// set html
						$section_data_html = "";
						
						// subitems
						$section_subs = $this->getSectionContent($unique_id,$s["page_section_id"]);

						// background style
						$background_style	= $s["css"] != "" ? $s["css"].";" : "";
						
						// linked data
						if($s["link_data"] != 0){
							$s["html"] = $this->getLinkedData($s);		
						}
						
						// add first class if first item
						$s["element_class"] = $i == 0 ? $s["element_class"]." first-section" : $s["element_class"];
						
						// item is fullpage
						if( $s["fullpage"] == 0 )
						{
							$section_data_html .= preg_replace(
								array("/{section_htmlwrapper}/","/{style}/","/{element-id}/","/{element-class}/","/{data-attributes}/"),
								array($s["html"],$background_style,$s["element_id"],$s["element_class"],""),
								$this->settings["group_wrapper"]
							);							
						}
						else
						{	
							$section_data_html .= preg_replace(
								array("/{section_htmlwrapper}/","/{style}/","/{element-id}/","/{element-class}/","/{data-attributes}/"),
								array($s["html"],$background_style,$s["element_id"],$s["element_class"],""),$this->settings["default_wrapper"]
							);	
						}
						
						// sub items
						$section_sub_html = "";
						foreach($section_subs as $ss)
						{
							$bg	= $s["css"] != "" ? $s["css"].";" : "";

							// get linked data
							if($ss["link_data"] != 0){
								//die("linkdata");
								$ss["html"] = $this->getLinkedData($ss);		
							}
					
							$section_sub_html .= preg_replace(
								array("/{section_htmlwrapper}/","/{style}/","/{element-id}/","/{element-class}/","/{data-attributes}/"),
								array($ss["html"],$bg,$ss["element_id"],$ss["element_class"],""),
								$this->settings["nested_wrapper"]
							);	
							
						}
						
						// set html to property			
						$html .= preg_replace("/{contents}/",$section_sub_html,$section_data_html);		
		
					}
					
					return $html;
						
				break;
				
			}
			
		}
		
		
		private function getSectionContent($unique_id,$sub_of=0)
		{
			
			$sql = "SELECT * FROM page_section_content WHERE page_id = :uid AND sub_of = :sub_of AND status = '1' ORDER BY position ASC";
			$sth = $this->db->prepare($sql);
			
			$sth->execute(["uid" => $unique_id, "sub_of" => $sub_of]);
			
			return $sth->fetchAll();
			
		}
		
		
		private function getLinkData($link_data_id)
		{
			$sql = "SELECT * FROM link_data WHERE link_data_id = :link_data_id";
			$sth = $this->db->prepare($sql);
			
			$sth->execute(["link_data_id" => $link_data_id]);
			
			return $sth->fetch();
		}
		
		
		private function getLinkDataFields($link_data_id)
		{
			$sql = "SELECT * FROM `link_data_fields` WHERE `template_data_id` = :link_data_id";
			$sth = $this->db->prepare($sql);
			
			$sth->execute(["link_data_id" => $link_data_id]);
			
			return $sth->fetchAll();
		}
				
				
		private function getLinkedData($section_data)
		{
			
			// org content
			$original_content  = $section_data["html"];
			
			// template
			$link_data  = $this->getLinkData($section_data["link_data"]);
			
			// field to replace in template
			$template_fields  = $this->getLinkDataFields($section_data["link_data"]);
			
			// get entries		
			$entries = $this->getPage($link_data["section"],null,$link_data["entry"],0,false);

			
			// replace all 1:1
			if($link_data["repeat_items"] == 0)
			{	

				$replace_total = substr_count($original_content, $link_data["find"]);
										
				for($i=0; $i<$replace_total; $i++)
				{	
					// set org template
					$template   = $link_data["template"];
												
					// find and replace fields in template
					foreach($template_fields as $tp_fld){
						if(!$tp_fld["field"]) continue;
					

						$field 	= $tp_fld["field"];
						$value 	= $entries[$i]->$field;
					
					
						if( $tp_fld["array"] == 1 && $tp_fld["function"] == 0 ){
							$value = $value[$tp_fld["index"]];
						}
						
										
						$value 		= $tp_fld["intro"] != 0 ? $this->makeIntro($value,$tp_fld["intro"]) : $value;
						
						if( $tp_fld["function"] == 1 )
						{	
							$arr_values = array();
							$header 	= array();
							
							//d($value);
							
							foreach($value[0] as $y => $xas_value)
							{	
								$row 		= array();
								$row[] 		= $xas_value;//intval(substr($xas_value,0,4));//$y;
								$header[] 	= $xas_value;
								
								for($x=1; $x<count($value); $x++)
								{	
									//$str = 
									$row[] = floatval( str_replace( array("%",","), array("","."), $value[$x][$y]) );
									
									if($y == 0) $header[] = $value[$x][$y];
									
								}
								
								//$row = array($xas_value,"x1","x2");
								if($y == 0) $arr_values[] = "['". implode("','",$header) ."']";
								
								else{
									$str = "";
									foreach($row as $z => $column){
										$str .=  ($z == 0) ? "'". $column."',": $column.",";//implode(",",$row) ;
									}
									$arr_values[] = "[". substr($str,0,-1) ."]";
								} 
								
							}							
							
							//d($arr_values);
							
							$value = implode(", ",$arr_values);
					
						}
						

						
						$template   = preg_replace("/".$tp_fld["template"]."/i", $value, $template);
					
					}
				
					$original_content = $this->str_replace_nth($link_data["find"], $template, $original_content, $i);
				}
				
			}
			// replace single template by all entries
			else
			{
				$content_str = "";
				for($i=0; $i<$link_data["repeat_items"]; $i++)
				{

					// set org template
					$template   = $link_data["template"];
												
					// find and replace fields in template
					foreach($template_fields as $tp_fld)
					{						
						if(!$tp_fld["field"]) continue;
					
						$field 	= $tp_fld["field"];
						$value 	= $entries[$i]->$field;
					
						if( $tp_fld["array"] == 1 ) $value = $value[$tp_fld["index"]];
					
						$value 		= $tp_fld["intro"] != 0 ? $this->makeIntro($value,$tp_fld["intro"]) : $value;
						
						if( $tp_fld["function"] == 2)
						{
							$value = strftime('<span class="day">%e</span><span class="month">%b</span>',strtotime($value));
						}
						elseif( $tp_fld["function"] == 3)
						{
							$value = strftime('%e %B %Y',strtotime($value));
						}
						
						$template   = preg_replace("/".$tp_fld["template"]."/i", $value, $template);
					
					}
					$content_str .= $template;
					
					//$original_content = $this->str_replace_nth($link_data["find"], $template, $original_content, $i);
				}
				
				$original_content = preg_replace("/".$link_data["find"]."/i", $content_str,$original_content);
				
				
			}							
		
			
			return $original_content;
			
		}
		
		
		private function str_replace_nth($search, $replace, $subject, $nth)
		{	
		    $found = preg_match_all('/'.preg_quote($search).'/', $subject, $matches, PREG_OFFSET_CAPTURE);
		    if ($found !== false) {
		        return substr_replace($subject, $replace, $matches[0][0][1], strlen($search));
		    }
		    return $subject;
		}
		
		
		private function getEntryData($table,$table_entry_id)
		{
			$sql = "SELECT * FROM $table WHERE entry_id = :entry_id ORDER BY position ASC";
			$sth = $this->db->prepare($sql);
			$sth->execute(["entry_id" => $table_entry_id]);
				
			return $sth->fetch();	
		}
		
		
		private function getTableName($template_id)
		{
			
			$sql = "SELECT naam FROM templates WHERE template_id = :template_id";
			$sth = $this->db->prepare($sql);
			$sth->execute(["template_id" => $template_id]);
			
			$data = $sth->fetch();
			
			return "td_".$data["naam"];			
			
		}
		
		
		private function getTemplateFields($template_id,$subof=null)
		{
			
			$sub_query = ($subof != null) ? "AND sub_of=$subof" : "AND sub_of=0";
			$sql = "SELECT * FROM template_fields LEFT JOIN field_types ON template_fields.field_id = field_types.field_id WHERE template_id = :template_id $sub_query AND active=1 ORDER BY template_fields.position ASC";
			$sth = $this->db->prepare($sql);
			$sth->execute(["template_id" => $template_id]);
				
			return $sth->fetchAll();			
			
		}
		
		
		private function getSection($name,$return=null)
		{
			
			if(is_numeric($name)) $sql 	= "SELECT *,secties.naam AS section_name FROM `secties` LEFT JOIN stramiens ON secties.stramien = stramiens.stramien_id WHERE secties.active = '1' AND secties.sectie_id = :name";
			else $sql 	= "SELECT *,secties.naam AS section_name FROM secties LEFT JOIN stramiens ON secties.stramien = stramiens.stramien_id WHERE secties.active = '1' AND (secties.naam LIKE :name OR secties.url LIKE :name)";	
			
			//echo $sql;
			
			$sth 	= $this->db->prepare($sql);
			$sth->execute(["name" => $this->validDBpar($name)]);
				
			$data = $sth->fetch();	
			return $return == null ? $data : $data[$return];
			
		}
		
		
		private function getSectionSort($sort_id)
		{
			
			// get section sort
			switch($sort_id)
			{
				case 1;
					$sort_query = " ORDER BY `created` DESC";
				break;
		
				case 2;
					$sort_query = " ORDER BY `created` ASC";
				break;
		
				default;
					$sort_query = " ORDER BY `position` ASC, `created` DESC";
				break;
				
			}
			return $sort_query;
			
		}
		
		
		private function getMainNavigation($show_visible=true,$active_page_name=null)
		{
			if($show_visible == true)
			{
				$sql 	= "SELECT * FROM `secties` WHERE `zichtbaar` = '1' AND `active` = '1' ORDER BY `position` ASC";
				$sth 	= $this->db->prepare($sql);
				$sth->execute();
				
				$nav_elements = [];
				
		        while($nav = $sth->fetch())
				{
					$nav_elements[] = new NavigationController($this->db,"",$nav,$active_page_name);	
				}
				
				return $nav_elements; 
					
			}
			//else $navs = $this->db->run("SELECT * FROM `secties` WHERE `active` = '1' ORDER BY `position` ASC");
		}
		
		function getSideBarNavigation($active_page_name=null)
		{
			// section
			$sql 	= "SELECT * FROM secties WHERE naam = :name AND active = 1 ORDER BY position ASC";
			$sth 	= $this->db->prepare($sql);
			$sth->execute([
				"name" => $active_page_name
			]);
			
			$nav_elements = [];
			
	        while($nav = $sth->fetch())
			{
				$nav_elements[] = new NavigationController($this->db,"",$nav,$active_page_name);	
			}
			
			return $nav_elements; 
				
		
			//else $navs = $this->db->run("SELECT * FROM `secties` WHERE `active` = '1' ORDER BY `position` ASC");
		}
		
		
		public function getSectionName()
		{
			return preg_replace("/-/"," ",$this->section_name);
		}
		
		public function getNavigation($positie_id)
		{	
			// new array
			$data 	= array();
			
			// entries
			$sql 	= "SELECT * FROM unique_ids,entry_posities WHERE unique_ids.unique_id = entry_posities.unique_id AND entry_posities.positie_id = :positie_id AND unique_ids.status = 2 ORDER BY unique_ids.position ASC";
			$sth 	= $this->db->prepare($sql);
			$sth->execute(["positie_id" => $positie_id]);
			
	        while($entry = $sth->fetch())
			{
				$data[] = $this->getEntry($entry);
			}
						
			return $data;
		}
		

		
		
	}
	
?>