File: D:/HostingSpaces/SBogers10/komma-mediadesign.nl/wwwroot/beheer/app/models/m_template.php
<?php
/*
Template.php // Controler
Handles all global tasks such as load, redirect setData, getData, setAlerts, getAlerts
*/
class Template
{
private $_data;
private $_alertTypes;
public function __construct()
{
// constructer
$this->_data = array();
$this->_alertTypes = array('succes','warning','error');
}
/*
SETER / GETTER
*/
/**
* Sets data for later use by the view
*
* @access public
* @param string, string, boolean
* @return null
*/
public function setData($name,$value,$clean = FALSE)
{
if(!$clean)
{
$this->_data[$name] = htmlentities($value, ENT_QUOTES);
}
else
{
$this->_data[$name] = $value;
}
}
/**
* Retrieves data based on provided name for acces by view
*
* @access public
* @param
* @return string, null
*/
public function getData($name, $echo=TRUE)
{
if(isset($this->_data[$name]))
{
if($echo)
{
echo $this->_data[$name];
}
else{
return $this->_data[$name];
}
}
return '';
}
/*
LOAD / REDIRECT
*/
/**
* Loads specified url
*
* @access public
* @param string
* @return null
*/
public function load($url, $title)
{
if( ! empty($title))
{
$this->_data['page_title'] = $title;
}
include $url;
}
/**
* Redirects to specified url
*
* @acces public
* @params string
* @return null
*/
public function redirect($url)
{
header('location:'.$url);
exit;
}
/**
* Redirects to specified url
*
* @acces public
* @params string
* @return null
*/
public function notFound()
{
header("HTTP/1.0 404 Not Found");
header('location:'.SITE_ROOT.'404');
exit;
}
/*
Get / Set Alerts
*/
/**
* Sets an alert message, stored in a session variable
*
* @acces public
* @params string, string(optional)
* @return null
*/
public function setAlert($value, $type='succes')
{
$_SESSION[$type][] = $value;
}
/**
* Returns string containing a div block with multiple list items of alerts
*
* @acces public
* @params
* @return string
*/
public function getAlerts($echo = TRUE)
{
$output = '';
foreach($this->_alertTypes as $alert)
{
if(isset($_SESSION[$alert]))
{
$output .= '<div class="alert js-disappear '.$alert.'">';
$output .= '<h1>'.strtoupper($alert).'</h1>';
$output .= '<ul>';
foreach($_SESSION[$alert] as $value)
{
$output .= '<li>'.$value.'</li>';
}
$output .= '</ul>';
unset($_SESSION[$alert]);
$output .= '</div>';
}
}
if($echo)
{
echo $output;
}
else
{
return $output;
}
}
/*
Convert Data
*/
/**
* Converts string into url string.
*
* @access public
* @param string
* @return string
*/
public function encodeUrl($input)
{
$output = trim($input);
$output = str_replace(' ','-',$output);
//remove these characters
$forbidden = array("'", '"', '\\', '/', ';', ';', '|', '>', '<', '[', ']', '!','?', '@', '#', '$', '%', '^', '&', '*', '(', ')','+','=','{','}','`', '~', '.', ',');
foreach($forbidden as $key => $value){
$output = str_replace($value, '', $output);
}
$output = strtolower($output);
return $output;
}
/**
* Encodes string into a valid Database name
*
* @access public
* @param string
* @return string
*/
public function encodeDbName($input)
{
$input = $this->encodeUrl($input);
$words = explode('-',$input);
$dbName = '';
foreach($words as $key => $word)
{
if($key > 0)
{
$word = ucfirst($word);
}
$dbName .= $word;
}
return $dbName;
}
}