File: /var/www/vhosts/creativefellows.nl/dhwd.creativefellows.nl/src/CassetteController.php
<?php
class CassetteController extends BaseController{
var $show_toplevel = true;
var $has_looped = false;
var $deeplink = true;
private $db;
public function __construct($settings=null,$open_db_connection=null)
{
// set settings
$this->settings = $settings;
$this->items_per_page = $settings["items_per_page"];
$this->language_base = $settings["language_base"];
$this->lang_id = $settings["lang_id"];
// Connect to new DB
if($open_db_connection == null) $this->db = new DatabaseController($settings["database"]);
// use open DB
elseif($open_db_connection != null) $this->db = $open_db_connection;
}
public function getForm($form_id,$csrf_token=null,$google_captcha=null){
$form = new CassetteForm($this->db,$form_id,$csrf_token,$google_captcha);
return $form;
}
public function getSettings()
{
$settings = $this->db->run("SELECT * FROM `instellingen` WHERE `id`='1'",false);
return $settings;
}
public function getInfo($table,$column,$value,$return=null)
{
$page_name = $this->db->get($table,$column,$value,$return);
return $page_name;
}
function getEntries($post_section=null,$post_category=null,$uid=null,$view_page=null,$search_array=null,$filter_navigation=false,$navigation_postion=null,$limit=null)
{
$this->wrapSections = isset($this->wrap) && $this->wrap == 1 ? 1 : 0;
$this->post_section = $post_section;
if($uid != null)
{
if(is_numeric($uid)) $uniqids = $this->db->run("SELECT * FROM `unique_ids` WHERE `unique_id` = '".$this->db->esc($uid)."' AND status = 2");
else $uniqids = $this->db->run("SELECT * FROM `unique_ids` WHERE `url` = '".$this->db->esc($uid)."' AND status = 2");
}
else
{
$start = ($view_page == null) ? 0 : ($view_page-1)*$this->items_per_page;
$limit_query = ($this->items_per_page != null && $limit == null) ? " LIMIT $start,$this->items_per_page" : ($limit == null ? null: " LIMIT 0,$limit");
// get data on section
if($post_section != null)
{
if($this->lang_id != 0)
{
$translation_arr = $this->getTranslation("value",$post_section,"section");
$sectie_id = $translation_arr["element_id"];
if($translation_arr)
{
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `sectie_id` = '$sectie_id' AND `active` = '1'",false);
$post_section = $sectie_info["naam"];
}
}
if(!is_numeric($post_section))
{
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `naam` LIKE '".preg_replace("/( |-)/","%",$this->db->esc($post_section))."%' AND `active` = '1'",false);
}
else
{
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `sectie_id` = '". $this->db->esc($post_section) ."' AND `active` = '1'",false);
}
// get section sort
$sort = $this->db->get("secties","sectie_id",$sectie_info["sectie_id"],"sort");
switch($sort)
{
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;
}
}
if($post_category == null && $uid == null)
{
$uniqids = $this->db->run("SELECT *,`unique_ids`.`unique_id` as `temp_uid` FROM `unique_ids` LEFT JOIN `entry_categories` ON `unique_ids`.`unique_id` = `entry_categories`.`unique_id` WHERE `unique_ids`.`sectie_id` = '". $sectie_info["sectie_id"] ."' AND `unique_ids`.`status` = 2 AND`unique_ids`.`language_id` = '0' AND `entry_categories`.`entry_category_id` IS NULL ".$sort_query.$limit_query);
foreach($uniqids as $i => $uid){
$uniqids[$i]["unique_id"] = $uid["temp_uid"];
}
}
// post category
elseif($post_category != null && $post_category != "0")
{
if($this->lang_id != 0)
{
if(!is_numeric($post_category))
{
// search for translation
$translation_arr = $this->getTranslation("value",$post_category,"category",$sectie_info["sectie_id"]);
//print_r($translation_arr);
if($translation_arr)
{
$category_id = $translation_arr["element_id"];
}
// no translation -> use org category
else
{
$category_info = $this->db->run("SELECT * FROM `categories` WHERE `naam` LIKE '".preg_replace("/[^0-9a-zA-Z]/","%",$this->db->esc($post_category))."' AND `sectie_id`='".$sectie_info["sectie_id"]."' AND `active`='1'",false);
$category_id = $category_info["category_id"];
}
}
else
{
$translation_arr = $this->getTranslation("element_id",$post_category,"category",$sectie_info["sectie_id"]);
if($translation_arr) $category_id = $translation_arr["element_id"];
else $category_id = $post_category;
}
$category_info = $this->db->run("SELECT * FROM `categories` WHERE `category_id` = '$category_id' AND `active` = '1' AND `sectie_id`='". $sectie_info["sectie_id"] ."'",false);
}
else{
if(!is_numeric($post_category))
{
$category_info = $this->db->run("SELECT * FROM `categories` WHERE `naam` LIKE '".preg_replace("/[^0-9a-zA-Z]/","%",$this->db->esc($post_category))."' AND `sectie_id`='".$sectie_info["sectie_id"]."' AND `active`='1'",false);
}
else
{
//echo $post_category;
$category_info = $this->db->run("SELECT * FROM `categories` WHERE `category_id` = '". $this->db->esc($post_category) ."' AND `sectie_id`='". $sectie_info["sectie_id"] ."' AND `active`='1'",false);
}
}
$search = $sort_query;
if($filter_navigation == true)
{
$search_lang = null;
$query = "SELECT * FROM `entry_categories`,`unique_ids`,`entry_posities` WHERE `entry_categories`.`unique_id` = `unique_ids`.`unique_id` AND `unique_ids`.`unique_id` = `entry_posities`.`unique_id` AND `positie_id` = '$navigation_postion' AND `category_id` = '".$category_info["category_id"]."' AND `status`='2' ".$search_lang.$search.$limit_query;
}
else
{
//.. hier
$query = "SELECT * FROM `entry_categories`,`unique_ids` WHERE `entry_categories`.`unique_id` = `unique_ids`.`unique_id` AND category_id = '".$category_info["category_id"]."' AND `status`='2' ".$search.$limit_query;
}
$uniqids = $this->db->run($query);
}
// post section
elseif($post_section != null)
{
$this->sectie_id = $sectie_info["sectie_id"];
if($this->lang_id == 0)
{
$search = $uid == null ? "WHERE `status` = '2' AND `sectie_id`='$this->sectie_id' AND `language_id` = '0' $sort_query $limit_query" : "WHERE `unique_id`='$uid'";
$this->pagination = $this->countRecords("SELECT COUNT(*) as `count` FROM `unique_ids` WHERE `sectie_id` = '$this->sectie_id' AND `language_id` = '0' ");
$uniqids = $this->db->run("SELECT * FROM `unique_ids` $search",true);
}
else
{
$query = "SELECT *
FROM unique_ids as master_ids
WHERE EXISTS
(
SELECT *
FROM unique_ids
WHERE master_ids.unique_id = unique_ids.translation_of_uid
AND language_id = $this->lang_id
AND status = 2
)
AND language_id = 0
AND sectie_id = $this->sectie_id
AND status = 2
$sort_query
$limit_query";
$uniqids = $this->db->run($query,true);
$this->pagination = $this->countRecords("SELECT COUNT(*) as count FROM unique_ids WHERE sectie_id = $this->sectie_id AND language_id = $this->lang_id ");
}
}
}
$html_arr = array();
foreach((array) $uniqids as $id)
{
// get tablename
$table = "td_".$this->db->get("templates","template_id",$id["template_id"],"naam");
// template fields
$fields = $this->templateFields($id["template_id"]);
// get data
$entry_data = $this->db->run("SELECT * FROM `$table` WHERE entry_id = ".$id["entry_id"]." ORDER BY position ASC",false);
// check for translation
if($this->lang_id != 0)
{
//echo "translate ".$this->lang_id;
// search for translated item
$translation = $this->db->run("SELECT * FROM `unique_ids` WHERE `translation_of_uid` = '".$id["unique_id"]."' AND language_id = '". $this->lang_id ."'",false);
// hide items without translations
if($sectie_info["show_translations"] == 0 && $translation == null && $uid == null) continue;
// override master data with transalation
if($translation != null)
{
// translation data
$tranlation_data = $this->db->run("SELECT * FROM `$table` WHERE entry_id = ".$translation["entry_id"]." ORDER BY position ASC",false);
// fields that have translation
$trans_fields = $this->templateFields($translation["template_id"]);
foreach($trans_fields as $fld)
{
// continue if no translation has been found
if($fld["translate"] == 0) continue;
$field_user = preg_replace("/[^0-9a-zA-Z]/","_",strtolower($fld["field_user"]));
// override master with translated fields
$entry_data[$field_user] = $tranlation_data[$field_user];
}
$entry_data["form_id"] = $tranlation_data["form_id"];
$entry_data["meta_title"] = $tranlation_data["meta_title"];
$entry_data["meta_description"] = $tranlation_data["meta_description"];
$entry_data["translation_uid"] = $tranlation_data["unique_id"];
}else continue;
}
// new entry data object
$ed = new entryData($this->db,$this->settings,$fields,$entry_data,$id["unique_id"],null,$this->wrapSections,$this->getLinkBase(),$this->lang_id,$this->deeplink);
//push into return array
array_push($html_arr,$ed);
}
// no direct entries found on post name
if(count($uniqids) == 0 && $this->has_looped == false)
{
$this->has_looped = true;
return $html_arr = $this->getEntries($post_section,$uid,null,$view_page);
}
else return $html_arr;
}
public function getMainNavigation()
{
$navs = $this->db->run("SELECT * FROM `secties` WHERE `zichtbaar` = '1' AND `active` = '1' ORDER BY `position` ASC");
$nav_elements = [];
foreach($navs as $nav)
{
if($this->lang_id != 0)
{
$translation = $this->getTranslation("element_id",$nav["sectie_id"],"section");
if(!$translation) continue;
//$section_name = $translation != false ? $translation["value"] : $nav["naam"];
//$section_link = $cassette->link_base.vl2( $translation["value"] );
}
$nav_elements[] = new NavigationController($this->db,$this->settings["language_base"],$nav);
}
return $nav_elements;
}
function categories($post_section,$show = "AND `zichtbaar` = '1'")
{
if($this->lang_id != 0){
$translation_arr = $this->getTranslation("value",$post_section,"section");
$sectie_id = $translation_arr["element_id"];
if($translation_arr)
{
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `sectie_id` = '$sectie_id' AND `active` = '1'",false);
$post_section = $sectie_info["naam"];
}
$categories_sql =
"SELECT
IF (translations.value IS NULL,categories.naam,translations.value) AS naam,
category_id,
sectie_id,
position,
sub_of,
zichtbaar,
active
FROM categories
LEFT JOIN translations
ON categories.category_id = translations.element_id
WHERE categories.sectie_id = '". $sectie_info["sectie_id"] ."'
AND categories.active = '1' $show ORDER BY position ASC";
}
else
{
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `naam` LIKE '".preg_replace("/( |-)/","%",$this->db->esc($post_section))."%'",false);
$categories_sql = "SELECT * FROM `categories` WHERE `sectie_id` = '". $sectie_info["sectie_id"] ."' AND `active`='1' $show ORDER BY position ASC";
}
$categories = $this->db->run($categories_sql);
return $categories;
}
function getTranslation($search_column,$search_value,$type,$sectie_id=null)
{
$search_value = preg_replace("/[^0-9a-zA-Z]/","%",$this->db->esc($search_value));
if($type == "section" || $type == "category" && $sectie_id == null)
{
$translation_arr = $this->db->run("SELECT * FROM `translations` WHERE `language_id` = '$this->lang_id' AND `$search_column` LIKE '". $search_value ."' AND type='". $type ."' ",false);
}
else
{
$translation_arr = $this->db->run("SELECT * FROM `translations` LEFT JOIN categories ON translations.element_id = categories.category_id WHERE `language_id` = '$this->lang_id' AND `$search_column` LIKE '". $search_value ."' AND type='". $type ."' AND `sectie_id` = '".$sectie_id."'",false);
}
return ($translation_arr != null ? $translation_arr : false);
}
function countRecords($mysql)
{
$row_count = $this->db->run($mysql,false);
$page_count = ceil($row_count["count"] / $this->items_per_page);
return $pagination = ($page_count > 1) ? $page_count : false;
}
/*function search($table,$search_column_array,$search_values_array)
{
$search = "WHERE ";
foreach($search_column_array as $i => $post_name){
$field_value = $search_values_array[$i];
$field_name = $post_name;
if($field_value == "") continue;
$search .= "`$field_name` LIKE '%".$field_value."%' OR";
}
$search = substr($search,0,-3);
$results = $this->data($table,$search);
$html_arr = array();
foreach($results as $entry){
$fields = $this->templateFields($entry["template_id"]);
$ed = new entryData($fields,$entry,$entry["unique_id"],$entry["template_id"]);
array_push($html_arr,$ed);
}
return $html_arr;
}*/
function templateFields($template_id,$subof=null)
{
$sub_query = ($subof != null) ? "AND sub_of=$subof" : "AND sub_of=0";
return $this->db->run("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");
}
function entriesInCategory($category_id)
{
$html_arr = array();
$entries_in_cat = $this->db->run("SELECT * FROM `entry_categories`,`unique_ids` WHERE `entry_categories`.`unique_id` = `unique_ids`.`unique_id` AND category_id = $category_id AND `unique_ids`.`status` = 2 ORDER BY created DESC");
foreach($entries_in_cat as $ec)
{
$table = "td_".$this->db->get("templates","template_id",$ec["template_id"],"naam");
$fields = $this->templateFields($ec["template_id"]);
$data = $this->db->run("SELECT * FROM `$table` WHERE entry_id = ".$ec["entry_id"]." ORDER BY position ASC");
$ed = new entryData($this->db,$this->settings,$fields,$data[0],$ec["unique_id"],$ec["template_id"]);
array_push($html_arr,$ed);
}
return $html_arr;
}
function getEntryCategory($unique_id)
{
$category = $this->db->run(
"SELECT `naam` FROM `entry_categories`,`categories`
WHERE `categories`.`category_id` = `entry_categories`.`category_id`
AND `entry_categories`.`unique_id` = '$unique_id'"
,false
);
return $category ? $category["naam"] : null;
}
function getNavigation($positie_id,$sectie=null,$category_name=null,$show_category=false,$show_toplevel=false)
{
if($show_toplevel == true)
{
//echo "test";
if($this->lang_id != 0)
{
$translation_arr = $this->getTranslation("value",$sectie,"section");
$sectie_id = $translation_arr["element_id"];
if($translation_arr)
{
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `sectie_id` = '$sectie_id' AND `active` = '1'",false);
}
}
else
{
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `naam` LIKE '". preg_replace("/( |-)/","%",$this->db->esc($sectie))."%'",false);
}
if(empty($sectie_info)) return false;
// categories
$cats = $this->db->run(
"SELECT *
FROM `categories`, `entry_categories`
WHERE `entry_categories`.`category_id` = `categories`.`category_id`
AND `categories`.`sectie_id` = ". $sectie_info["sectie_id"] ."
AND `categories`.`active`='1'
AND `zichtbaar` = '1'
GROUP BY entry_categories.category_id
ORDER BY position ASC
");
$uniqids = $this->db->run(
"SELECT *,`unique_ids`.`unique_id` AS `uid`
FROM `unique_ids`
LEFT JOIN `entry_posities`
ON `unique_ids`.`unique_id` = `entry_posities`.`unique_id`
LEFT JOIN `entry_categories`
ON `unique_ids`.`unique_id` = `entry_categories`.`unique_id`
WHERE `unique_ids`.`sectie_id` = '". $sectie_info["sectie_id"] ."'
AND `unique_ids`.`status` = 2
AND `entry_posities`.`positie_id` = $positie_id
AND `entry_categories`.`entry_category_id` IS NULL
ORDER BY `unique_ids`.`position`".$limit_query
);
//d(array($uniqids,$cats));
return array($uniqids,$cats);
}
elseif($category_name != null)
{
//get category ID
$cat_info = $this->db->run("SELECT * FROM `categories` WHERE `naam` LIKE '".preg_replace("/( |-)/","%",$this->db->esc($category_name))."' AND `active` = '1' OR `url` LIKE '%". preg_replace("/( |-)/","%",$this->db->esc($category_name)) ."%' AND `active` = '1'",false);
$cats = $this->db->run("SELECT * FROM `categories`, `entry_categories`,`entry_posities` WHERE `entry_categories`.`category_id` = `categories`.`category_id` AND `entry_categories`.`unique_id` = `entry_posities`.`unique_id` AND `categories`.`category_id` = ".$cat_info["category_id"]." AND `categories`.`active`='1' AND `zichtbaar` = '1' AND `entry_posities`.`positie_id` = '$positie_id' ORDER BY position ASC");
return $cats;
}
elseif($show_category == true)
{
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `naam` LIKE '". preg_replace("/( |-)/","%",$this->db->esc($sectie))."%'",false);
if(empty($sectie_info)) return false;
$cats = $this->db->run("SELECT * FROM `categories`, `entry_categories` WHERE `entry_categories`.`category_id` = `categories`.`category_id` AND `categories`.`sectie_id` = ". $sectie_info["sectie_id"] ." AND `categories`.`active`='1' AND `categories`.`active`='1' AND `zichtbaar` = '1' GROUP BY entry_categories.category_id ORDER BY position ASC");
return $cats;
}
elseif($sectie == null)
{
$entries = $this->db->run("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");
return $entries;
}
else
{
// new array
$data = array();
// secion data
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `naam` LIKE '". preg_replace("/( |-)/","%",$this->db->esc($sectie))."%'",false);
// categories
$data[] = $this->db->run("SELECT * FROM `categories` WHERE `sectie_id` = '". $sectie_info["sectie_id"] ."' AND `active`='1' AND `zichtbaar` = '1' ORDER BY position ASC");
// entries
$data[] = $this->db->run("SELECT * FROM `unique_ids`,`entry_posities` WHERE `unique_ids`.`unique_id` = `entry_posities`.`unique_id` AND `unique_ids`.`sectie_id` = '". $sectie_info["sectie_id"] ."' AND `entry_posities`.`positie_id` = '$positie_id' AND `unique_ids`.`status` = 2 ORDER BY `unique_ids`.`position` ASC");
return $data;
}
}
function fieldHTML($element,$value,$default_html=null,$css_class=null)
{
if(empty($value)) return;
switch($element){
case "h1":
$html = '<div class="'.$css_class.'">';
$html .= '<header class="header-box">';
$html .= '<h1>'.stripslashes($value).'</h1>';
$html .= '</header>';
$html .= '</div>';
break;
case "text":
$html = '<div class="'.$css_class.'">';
$html .= '<h2>'.$value.'</h2>';
$html .= '</div>';
break;
case "image":
$html = '<div class="'.$css_class.'">';
$html .= '<figure class="article-image">'.$this->createImage($value[0],"h",600,220).'</figure>';
$html .= '</div>';
break;
case "cols":
if(empty($value)) return;
$count = count($value);
$width = "col".(24/count($value));
$html = "";
$html .= '<div class="col24"><div class="devider"></div></div>';
foreach($value as $link)
{
$html .= '<div class="'.$width.'">';
$html .= '<div class="dotted-box rounded">';
//$html .= '<figure>'.$this->createImage($data->foto[0],"bl",75,75).'</figure>';
$html .= '<div class="block-text float-fix">';
$html .= '<h3>'.$link[2].'</h3>';
$html .= '<p>'.$link[1].'<a class="read-more" href="'.$link[3].'">'.$bl->link_label.'</a></p>';
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
}
break;
case "link":
$html .= '<a href="'.$value[0].'" class="'.$css_class.'">'.$value[1].'</a>';
break;
case "form":
$form = new cassetteForm($value);
// form fields
$fields = $form->getFields();
$html = '<div class="'.$css_class.'">';
$html .= "<form class='form' action='handle-form' $onsubmit id='form$e->formulier' method='post' enctype='multipart/form-data'><div>";
foreach($fields as $el){
$html .= $form->cassetteFormElement($el);
}
$html .= "</div></form>";
$html .= '<div class="feedback"></div>';
$html .= '</div>';
break;
default:
$html = '<div class="'.$css_class.'">';
$html .= '<section class="article-details">';
switch($element){
case "em":
$html .= '<p><em>'.$value.'</em></p>';
break;
case "share":
$html .= '<div class="social-share">'.$default_html.'</div>';
break;
default:
$html .= $value;
break;
}
$html .= '</section>';
$html .= '</div>';
break;
}
return $html;
}
function getBezorgKosten($selected_id=null,$total=null)
{
$config = $this->db->get("instellingen","id",1);
return $config["verzendkosten"];
}
function btwBedrag($bedrag)
{
return ($bedrag/121)*21;
}
function iDealAmount($bedrag)
{
return $bedrag*100;
}
function orderTotaal($bedrag)
{
$_SESSION[$this->company_short]["totaalbedrag"] = $this->iDealAmount($bedrag);
}
function createImage($img_url,$prefix,$width,$height,$resize_type=1,$class=null,$alt=null,$style=null)
{
if($img_url == ""){
//$img_url = ($override_default_img == null) ? $this->base.$this->defaultImage : $override_default_img;
$img_url = $this->base.$this->defaultImage;// : $override_default_img;
}
$prefix = preg_replace("/ /","_",$prefix);
$img_arr = explode("/",$img_url);
$img_name = end($img_arr);
// check img url op CMS URL
$pref_url = $this->getBaseUrl() . IMAGE_DIR . $prefix ."-". $img_name;
$response = get_headers($pref_url, 1);
if((strpos($response[0], "404") === false) && $this->saveImages() == true) return '<img src="'.IMAGE_DIR.preg_replace("/ /","-",$prefix.'-'.$img_name).'" class="'.$class.'" alt="'.$alt.'" />';
else return '<img src="image.php?prefix='.$prefix.'&path='.$img_url.'&w='.$width.'&h='.$height.'&type='.$resize_type.'&d='. IMAGE_DIR .'" class="'.$class.'" alt="'.$alt.'" />';
}
function getLink($link_to_uid)
{
if($link_to_uid == "") return;
$r = $this->db->run("SELECT * FROM `unique_ids`,`secties` WHERE `unique_ids`.`sectie_id` = `secties`.`sectie_id` AND `unique_id` = '$link_to_uid'",false);
$table = "td_".$this->db->get("templates","template_id",$r["template_id"],"naam");
$fields = $this->templateFields($r["template_id"]);
$entry_titel = $this->db->get($table,"entry_id",$r["entry_id"],$fields[0]["field_naam"]);
return $r["naam"]."/".$entry_titel."-".$link_to_uid;
}
function sectieInfo($sectie)
{
if($this->lang_id != 0)
{
// get translation
$translation_arr = $this->getTranslation("value",$sectie,"section");
if($translation_arr)
{
$sectie_id = $translation_arr["element_id"];
$sectie_info = $this->db->run("SELECT * FROM `secties` WHERE `secties`.`sectie_id` = '". $sectie_id ."'",false);
$sectie = $sectie_info["naam"];
}
}
$sectie_info = $this->db->run("SELECT * FROM `secties` LEFT JOIN `stramiens` ON `secties`.`stramien` = `stramiens`.`stramien_id` WHERE `secties`.`naam` LIKE '". $this->validDBpar($sectie) ."' AND `secties`.`active` = '1'",false);
return $sectie_info;
}
function validDBpar($str)
{
return preg_replace("/( |-)/","%", $this->db->esc($str) );
}
function getCategories($sectie_naam)
{
// get categories
$sectie_id = $this->getSectionId($sectie_naam);
$categories = $this->db->run("SELECT * FROM `categories` WHERE `sectie_id`='". $sectie_id ."' AND `active` = '1'");
if($this->lang_id != 0)
{
foreach($categories as $i => $c)
{
// get translation
$translation_arr = $this->getTranslation("value",$c["naam"],"category");
if($translation_arr)
{
$sectie_id = $translation_arr["element_id"];
$sectie_info = $this->db->run("SELECT * FROM `secties` LEFT JOIN `stramiens` ON `secties`.`stramien` = `stramiens`.`stramien_id` WHERE `secties`.`sectie_id` = '". $sectie_id ."'",false);
$sectie_naam = $sectie_info["naam"];
}
else unset($categories[$i]);
}
}
else return $categories;
}
function getSectionId($sectie_naam)
{
$sectie_arr = $this->db->run("SELECT `sectie_id` FROM `secties` WHERE `naam` LIKE '".preg_replace("/( |-)/","%",$this->db->esc($sectie_naam))."%'",false);
return $sectie_arr["sectie_id"];
}
public function getSearchResults($search_table_array,$search_column_array,$search_values,$refine_search,$view_page=1)
{
$results_arr = array();
$i = 0;
foreach($search_table_array as $identifier => $search_table)
{
$refine_sql = "";
if($refine_search != null)
{
foreach($refine_search as $arr)
{
if(empty($arr[1])) continue;
if($arr[0] == "industry")
{
$refine_sql .= " AND $arr[1] = $arr[3]";
}
else
{
$refine_sql .= " AND $arr[0] $arr[2]";
if($arr[2] == " = ") $refine_sql .= "'";
$refine_sql .= "$arr[1]";
if($arr[2] == " = ") $refine_sql .= "'";
}
}
}
$limit_query = $this->db->getQueryLimit($view_page);
$search_columns = implode(",",$search_column_array[$i]);
$sql = "SELECT *, MATCH ($search_columns) AGAINST ('$search_values') AS `relevance` FROM `$search_table` LEFT JOIN `unique_ids` ON `unique_ids`.`unique_id` = `$search_table`.`$identifier` WHERE MATCH ($search_columns) AGAINST ('$search_values') GROUP BY unique_ids.unique_id HAVING relevance > 2 AND unique_ids.language_id = 0 AND unique_ids.status = 2 $refine_sql";
// basic fallback search for short terms
if($search_values == "") $sql = "SELECT *,position AS relevance FROM `$search_table` WHERE language_id = 0 $refine_sql";
// count all records in query
$this->getProductCount($sql);
$rows = $this->db->run($sql.$limit_query);
foreach($rows as $row)
{
$results_arr[] = $row;
foreach ($row as $key => $value)
{
${$key}[] = $value;
}
}
$i++;
}
// sort array on relevance
if($results_arr) array_multisort($relevance, SORT_DESC, $results_arr);
return $results_arr;
}
/*
* count rows
*/
public function getProductCount($sql)
{
$rowCount = $this->db->rowCount($sql);
$page_count = ceil($rowCount/$this->items_per_page);
$this->product_pagination = ($page_count > 1) ? $page_count : false;
$this->product_count = $rowCount;
}
function formatPrice($str,$show_sign=true)
{
return ($show_sign == true ? "€ " : "") . number_format($str,2,',','');
}
function makeEmail($body)
{
$settings = $this->db->run("SELECT * FROM instellingen WHERE `id`='1'",false);
$message = "<html><head></head><body>";
$message .= "<table border='0' cellspacing='0' cellpadding='5' style='width:600px; font-size:12px;font-family:Helvetica,arial, sans-serif; color:#333;' align='center'>";
$message .= "<tr><td colspan='4'> </td></tr>";
$message .= "<tr><td colspan='4' style='height:124x;'><img src='". $this->base . $settings["logo"] ."'/></td></tr>";
$message .= "<tr><td colspan='4' style='border-bottom:solid 1px #d9d9d9;'> </td></tr>";
$message .= "<tr><td colspan='4'> </td></tr>";
// add body
$message .= "<tr><td colspan='4' style='border-bottom:solid 1px #d9d9d9;'>". $body ."</td></tr>";
// footer
$message .= "<tr><td colspan='4'> </td></tr>";
$message .= "<tr><td colspan='4' style='width:260px;vertical-align:top;line-height:16px;'>";
$message .= "<table border='0' cellspacing='0' cellpadding='3' style='width:100%; font-size:13px;font-family:Helvetica,arial, sans-serif; color:#333;'>";
$message .= "<tr>";
$message .= "<td style='width:300px;vertical-align:top;font-size:12px;'>
<strong>".$settings["bedrijfsnaam"]."</strong><br />
".$settings["adres"]."<br />
".$settings["postcode_woonplaats"]."<br />
<a href='". $settings["website"] ."'>". preg_replace("/http:\/\//","",$settings["website"]) ."</a><br />
<a href='mailto:". $settings["master_email"] ."'>". $settings["master_email"] ."</td>";
$message .= "<td style='vertical-align:top;font-size:12px;'>BTW nummer ".$settings["btw"]."<br />KvK nummer ".$settings["kvk"]."<br />Bank ".$settings["bank"]."<br />IBAN ".$settings["bank_iban"]."<br />BIC ".$settings["bank_bic"]."</td>";
$message .= "</tr>";
$message .= "</table>";
$message .= "</td></tr>";
$message .= "<tr><td colspan='4'> </td></tr>";
$message .= "<tr><td colspan='4'> </td></tr>";
$message .= "</table>";
$message .= "</body></html>";
return $message;
}
/*
* Check if user is logged in
*/
function login_check($login_table)
{
// Check if all session variables are set
if (isset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['login_string'])) {
$user_id = $_SESSION['user_id'];
$account_id = $_SESSION['account_id'];
$login_string = $_SESSION['login_string'];
$username = $_SESSION['username'];
// Get the user-agent string of the user.
$user_browser = $_SERVER['HTTP_USER_AGENT'];
$user_data = $this->db->run("SELECT * FROM `$login_table` WHERE `klant_id` = '". $user_id ."'",false);
$login_check = hash('sha512', $user_data["password"] . $user_browser);
if ($login_check == $login_string) {
// Logged In!!!!
return true;
}else{
// Not logged in
return false;
}
}
else return false;
}
/*
* Get random string
*/
function randomString()
{
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 20; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass);
}
/*
* Delete password reset
*/
function deleteUserPasswordReset($user_id=null)
{
if($user_id == null) return;
$attempts = $this->db->run("SELECT * FROM `heme_pw_reset` WHERE `user_id` = '$user_id'");
foreach((array)$attempts as $att)
{
//echo "DELETE FROM `heme_pw_reset` WHERE `reset_id` = '". $att["reset_id"] ."'";
$this->update("DELETE FROM `heme_pw_reset` WHERE `reset_id` = '". $att["reset_id"] ."'");
}
}
/*
* Check if password reset is valid
*/
function checkValidPasswordReset($post_sec_code)
{
$now = date('Y-m-d h:m:s');
$reset = $this->db->run("SELECT * FROM `heme_pw_reset` WHERE `code` = '". $post_sec_code ."' AND `valid_until` > '$now'");
if(count($reset) == 1) return true;
else return false;
}
function getColumnOffset($count,$i)
{
if($i != 0) return;
switch($count){
default:
$col_offset = "";
break;
case 1:
$col_offset = " offset8";
break;
case 2:
$col_offset = " offset4";
break;
}
return $col_offset;
}
public function getFilesAsList($uploads_arr, $header, $class=null,$wrapper_element_class=false)
{
$html = $header;
$html .= '<ul class="'. $class .'">';
foreach((array)$uploads_arr as $file){
$html .= "<li>";
$html .= '<a href="'. $file[0] .'" target="_blank">'. $file[1] .'</a>';
$html .= "</li>";
}
$html .= "</ul>";
if($wrapper_element_class != false) $html = '<div class="'. $wrapper_element_class .'">'. $html .'</div>';
return empty($uploads_arr) ? null : $html;
}
public function getTagsAsList($tag_arr,$base_link,$class=null)
{
$html = '<ul class="tags">';
foreach($tag_arr as $tag)
{
$html .= '<li>';
$html .= '<a href="'. $base_link .'?tag='. urlencode(trim($tag)) .'">'.$tag.'</a>';
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
}
class entryData extends BaseController{
function __construct($connection,$settings,$fields,$data,$unique_id,$post_category=null,$wrapSections=false,$link_base=null,$lang_id=0,$deeplink=true){
$this->db = $connection;
$this->settings = $settings;
$this->wrapSections = $wrapSections;
$this->link_base = $link_base;
//echo "lb = ". $this->link_base;
$this->unique_id = $unique_id;
$this->lang_id = $lang_id;
$this->deeplink = $deeplink;
$this->entry_id = $data["entry_id"];
$this->status = $data["status"];
$this->position = $data["position"];
$this->last_edit = $data["last_edit"];
$this->form_id = $data["form_id"];
$this->meta_title = $data["meta_title"];
$this->meta_desc = $data["meta_desc"];
$this->page_redirect= $data["page_redirect"];
$this->stramien_id = $data["stramien_id"];
$this->transltn_uid = isset($data["translation_uid"]) ? $data["translation_uid"] : 0;
$unique_data = $this->db->get("unique_ids","unique_id",$this->unique_id);
$this->url = $unique_data["url"];
$this->sectie_id = $unique_data["sectie_id"];
$this->template_id = $unique_data["template_id"];
$this->template = preg_replace("/ /","_",$this->db->get("templates","template_id",$this->template_id,"naam"));
$this->sectie = $this->lang_id == 0 ? $this->db->get("secties","sectie_id",$this->sectie_id,"naam") : $this->getSectionTranslation();
$this->entry_link = strtolower($this->link_base . $this->vl($this->sectie). "/" . ( $this->url ? $this->vl($this->url) : $this->vl( preg_replace("/\//","",$data[$fields[0]["field_naam"]]) ) ));
$this->first_field = null;
foreach($fields as $i => $f)
{
if($f["type"] == 1)
{
$field_user = preg_replace("/[^0-9a-zA-Z]/","_",strtolower($f["field_user"]));
//echo $i." = ".$field_user." - ";
if($i == 0) $this->first_field = $field_user;
$this->fieldHTML($f["field_naam"],$f["form_element"],$data[$f["field_naam"]],$f["template_field_id"]);
}
else
{
$this->fieldHTML($f["field_naam"],$f["form_element"],$f["template_field_id"]);
}
}
}
function getSectionTranslation()
{
$translation_arr = $this->db->run("SELECT * FROM `translations` WHERE `language_id` = '$this->lang_id' AND `element_id` = '". $this->sectie_id ."' AND type='section' ",false);
return $translation_arr["value"];
}
function fieldHtml($field_user,$element,$value,$template_field_id=null)
{
$field_user = preg_replace("/[^0-9a-zA-Z]/","_",strtolower($field_user));
$load_uid = $this->transltn_uid != 0 ? $this->transltn_uid : $this->unique_id;
$data = array();
$value = stripslashes($value);
switch($element){
case "shop":
$staffel_array = $this->db->run("SELECT * FROM `prijzen_product` WHERE `unique_id` = '$load_uid'");
$html = '<div class="product-price">';
$html .= '<p><a class="bestellen-button rounded" href="winkelwagen?uid='.$this->unique_id.'">Bestellen</a></p>';
$html .= '</div>';
$this->$field_user = $html;
$this->prijs_voor = $staffel_array[0]["voor"];
$this->prijs_van = $staffel_array[0]["van"];
$this->prijs_desc = $staffel_array[0]["naam"];
break;
case "synoniem":
$syns = $this->db->run("SELECT * FROM `entry_syns` WHERE `uid` = '$this->unique_id' AND `field_id` = '$template_field_id'");
$array = array();
foreach($syns as $syn )
{
array_push($array,$syn["syn_id"]);
}
$this->$field_user = $array;
break;
case "link":
case "cols":
if(trim($value) != ""){
$files = explode("|*|",$value);
$data = array();
foreach($files as $f){
$file_data = explode("|-|",$f);
// $cassette = new cassetteFrontend();
// $entry = $cassette->entries(null,null,$file_data[0]);
// $fieldName = $entry->first_field;
// $file_data[2] = $entry->$fieldName;
// $file_data[3] = $entry->entry_link;
array_push($data,$file_data);
}
}else $data = array();
$this->$field_user = $data;
break;
case "image":
$this->$field_user = explode("|*|",$value);
break;
case "files":
$files = explode("|*|",$value);
$data = array();
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);
}
$this->$field_user = $data;
break;
case "dragdrop":
$this->$field_user = "";
// check for translation data id
$load_uid = $this->transltn_uid == 0 ? $this->unique_id : $this->transltn_uid;
$sections = $this->db->run("SELECT * FROM `page_section_content` WHERE `page_id` = '". $load_uid ."' AND `sub_of` = 0 AND `status` = '1' ORDER BY `position` ASC");
foreach($sections as $i => $s){
// set html
$section_data_html = "";
// subitems
$section_subs = $this->db->run("SELECT * FROM `page_section_content` WHERE `page_id` = '". $load_uid ."' AND `sub_of` = '". $s["page_section_id"] ."' AND `status` = '1' ORDER BY `position` ASC");
$bg = $s["css"] != "" ? $s["css"].";" : "";
// linked data
if($s["link_data"] != 0) $s["html"] = $this->getLinkedData($s);
$s["element_class"] = $i == 0 ? $s["element_class"]." first-section" : $s["element_class"];
// set to html
if($this->wrapSections == 0 || $s["fullpage"] == 1 || count($section_subs) >= 1)
{
//$section_data_html .= '<div id="'. $s["element_id"] .'" class="row expanded '. $s["element_class"] .'" style="'. $bg .'">'. $s["html"] .'</div>';
if($s["fullpage"] == 1){
$s["element_class"] .= " full-page";
}
$section_data_html .= preg_replace(array("/{section_htmlwrapper}/","/{style}/","/{element-id}/","/{element-class}/"),array($s["html"],$bg,$s["element_id"],$s["element_class"]),$this->settings["group_wrapper"]);
}
else{
$section_data_html .= preg_replace(array("/{section_htmlwrapper}/","/{style}/","/{element-id}/","/{element-class}/"),array($s["html"],$bg,$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) $ss["html"] = $this->getLinkedData($ss);
$section_sub_html .= preg_replace(array("/{section_htmlwrapper}/","/{style}/","/{element-id}/","/{element-class}/"),array($ss["html"],$bg,$ss["element_id"],$ss["element_class"]),$this->settings["nested_wrapper"]);
}
// set html to property
$this->$field_user .= preg_replace("/{contents}/",$section_sub_html,$section_data_html);
//'group_wrapper' => '<div id="{element-id}" class="row expanded row-default{element-class}" style="{style}">{section_htmlwrapper}</div>',
//'nested_wrapper' => '<div class="row {element-class}" style="{style}" id="{element-id}">{section_htmlwrapper}</div>',
//'default_wrapper'
}
break;
case "checkbox":
$this->$field_user = explode("|*|",$value);
break;
default:
$this->$field_user = $value;
break;
}
}
function getLinkedData($section_data)
{
if($this->deeplink == false) return;
$cassette = new CassetteController($this->settings,$this->db);
$cassette->deeplink = false;
// org content
$original_content = $section_data["html"];
// template
$link_data = $this->db->run("SELECT * FROM `link_data` WHERE `link_data_id` = '". $section_data["link_data"] ."'",false);
// field to replace in template
$template_fields = $this->db->run("SELECT * FROM `link_data_fields` WHERE `template_data_id` = '". $section_data["link_data"] ."'");
// entries in linked category
$cassette->has_looped = true;
$entries = $cassette->getEntries($link_data["section"],$link_data["category"]);
// 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 ) $value = $value[$tp_fld["index"]];
$value = $tp_fld["intro"] != 0 ? $this->makeIntro($value,$tp_fld["intro"]) : $value;
$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;
$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;
}
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;
}
function formatPrice($str,$show_sign=true)
{
return ($show_sign == true ? "€ " : "") . number_format($str,2,',','');
}
}
?>