File: D:/HostingSpaces/SBogers10/topswtw.komma.pro/app/helpers.php
<?php
/**
* This function will print the data in a beatiful way
* The arrays are going to be collapsanble with js
*
* @param $data
*/
function mooi($data, $die = true)
{
echo '<pre>';
// capture the output of print_r
$out = print_r($data, true);
// replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
$out = preg_replace_callback('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iU', 'callback_replace', $out);
// replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
$out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);
// print the javascript function toggleDisplay() and then the transformed output
echo '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>' . "\n$out";
echo '</pre>';
if ($die) {
die();
}
}
function viewPrefix(){
$prefix = \Config::get('view.prefix');
if( $prefix != '') $prefix = $prefix .'.';
return $prefix;
}
//form Macros
\Form::macro('tel', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="tel" id="'.$name.'" name="'. $name .'" ';
if ($default) {
$item .= 'value="'. $default .'" ';
}
if (is_array($attrs)) {
foreach ($attrs as $a => $v)
$item .= $a .'="'. $v .'" ';
}
$item .= ">";
return $item;
});
\Form::macro('number', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="number" id="'.$name.'" name="'. $name .'" ';
if ($default) {
$item .= 'value="'. $default .'" ';
}
if (is_array($attrs)) {
foreach ($attrs as $a => $v)
$item .= $a .'="'. $v .'" ';
}
$item .= ">";
return $item;
});
function callback_replace($matches)
{
// "'\\1<a href=\"javascript:toggleDisplay(\'".($id = substr(md5(rand().'\\0'), 0, 7))."\');\">\\2</a><div id=\"'.\$id.'\" style=\"\">'"
$id = rand();
return $matches[1] . '<a href="javascript:toggleDisplay(' . $id . ')">' . $matches[2] . '</a><div id="' . $id . '" style="display:none;">';
}
/**
* This function will collect the last query
*
* @return mixed
*/
function last_query($steps_back = 0)
{
$queries = \DB::getQueryLog();
$query = end($queries);
if ($steps_back == 0) {
return $query;
}
for ($i = 1; $i <= $steps_back; $i++) {
$query = prev($queries);
}
return $query;
}
/**
* This function encodes the given string to UTF-8
* After that we replace the special characters
*
* @param $text
* @return string
*/
function helper_prepare_pdo_string($text)
{
$text = trim($text);
//First we a going to convert to UTF-8
//Is it already utf-8?
if (mb_detect_encoding($text) != 'UTF-8') {
//convert to utf-8
$text = utf8_decode($text);
}
//Check if the string contains more then alpfanumeric spaces, or dashes
if (!preg_match('#^[a-zA-Z0-9_\ \s-]*$#', $text)) {
//It is possible there are special letters in the string, change these to normal letters
$text = strtr(utf8_decode($text), utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'), 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
}
return strtolower($text);
}
/**
* This function will localize the date
*
* @param $date
* @return DateTime
*/
function helper_localize_date($date, $format = 'd-m-Y H:i')
{
date_default_timezone_set('UTC');
$datetime = strtotime($date) && strtotime($date) >= 0 ? new DateTime($date) : new DateTime('now');
$la_time = new DateTimeZone('Europe/Amsterdam');
$datetime->setTimezone($la_time);
return $datetime->format($format);
}
function komma_ip()
{
if ($_SERVER['REMOTE_ADDR'] == '5.172.219.238') return true;
return false;
}
/**
* This method will check if the given string is an JSON
*
* @param $string
* @return bool
*/
function helper_isJSON($string)
{
return is_string($string) && is_array(json_decode($string, true)) ? true : false;
}
function helper_checkOrCreateDir($path)
{
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
}
function getFormData($field, $default = '', $object = null, $objectField = null)
{
//First, check if the field exist in the input, use this field
if (\Request::old($field)) return \Request::old($field);
//Check if the object is null; Yes return default
if (!$object) return $default;
//Check if objectField is no set if not. check if the object has an item with the name $field; if true, return this
if (!$objectField && isset($object->$field)) return $object->$field;
//Check if objectField is set if not. check if the object has an item with the name $objectField; if true, return this
if ($objectField && isset($object->$objectField)) return $object->$objectField;
return $default;
}
function helper_fullDomain()
{
$route = (\Config::get('komma/tops.https') == true ? 'https://' : 'http://');
$route .= (\Config::get('komma/tops.www') == true ? 'www.' : '');
$route .= \Shop::getDomain()->get();
return $route;
}
function helper_underscoreAll($string)
{
$string = \Str::slug($string);
return preg_replace('#([\s| |-])#', '_', $string);
}
function helper_underscore_to_space($string)
{
return ucfirst(preg_replace('#(_)#', ' ', $string));
}