<?php
use a_modules\common\repository\Session;
use a_modules\common\repository\TT;

if(!function_exists('t')) {
    function t($string, $args = 0)
    {
        return $string;
    }
}

if(!function_exists('t_')) {
    function t_($string, $code = null, $args = 0)
    {
        return $string;
    }
}

if(!function_exists('tt')) {
    function tt($string, $module, $file = null)
    {
        return $string;
    }
}

if(!function_exists('t_a')) {
    function t_a($string, $args = null) {
        return $string;
    }
}

if(!function_exists('getCurrentUrl')) {
    function getCurrentUrl($localized = true) {
        global $protocol, $ultra_domainname, $ultra_addonname, $show_langs;
        $host = $_SERVER['HTTP_HOST'];
        $uri = $_SERVER['REQUEST_URI'];
        $request_url = $protocol . $host . $uri;

        if(!$localized) {
            $path_info = parse_url($request_url);
            $path_segments = explode("/", trim($path_info['path'], '/'));
            if(str_replace('www.', '', $host) != $ultra_domainname && $path_segments[0] == $ultra_addonname) {
                array_shift($path_segments);
            }
            if(isset($path_segments[0]) && in_array($path_segments[0], explode("-", $show_langs))) {
                $locale = array_shift($path_segments);
            }
            $unlocalized_url = implode('/', $path_segments);
            if(isset($path_info['query'])) {
                $unlocalized_url .= '?' . $path_info['query'];
            }

            return $unlocalized_url;
        }

        return $request_url;
    }
}

if(!function_exists('toArray')) {
    function toArray($d) {
        if (is_object($d)) {
            // Gets the properties of the given object
            // with get_object_vars function
            $d = get_object_vars($d);
        }

        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return array_map(__FUNCTION__, $d);
        }
        else {
            // Return array
            return $d;
        }
    }
}

if(!function_exists('old')) {
    function old($key, $default) {
        if(Session::ghas('old')) {
            $old = Session::gget('old');

            if(strpos($key, '.') !== false) {
                $keys = array_map('trim', explode(".", $key));
                $value = null;

                foreach($keys as $i=>$k) {
                    if($i == 0) {
                        if(isset($old[$k])) {
                            $value = $old[$k];
                        }
                        else {
                            return $default;
                        }
                    }
                    else {
                        if(isset($value[$k])) $value = $value[$k];
                        else return $default;
                    }
                }

                return $value;
            }

            if(isset($old[$key])) return $old[$key];
        }

        return $default;
    }
}

if(!function_exists('fileIsThere')) {
    function fileIsThere($path) {
        return ( file_exists($path) || file_exists("{$_SERVER['DOCUMENT_ROOT']}$path") );
    }
}

if(!function_exists('append2url')) {
    function append2url($url, $data) {
        $qs = '';
        if(is_array($data)) {
            $qs = http_build_query($data);
        }

        if(strpos($url, '?') !== false) {
            $url .= $qs != "" ? '&' . $qs : '';
        }
        else {
            $url .= $qs != "" ? '?' . $qs : '';
        }

        return $url;
    }
}
if(!function_exists('trim_all')) {
    function trim_all( $str , $what = NULL , $with = ' ' )
    {
        if( $what === NULL )
        {
            //  Character      Decimal      Use
            //  "\0"            0           Null Character
            //  "\t"            9           Tab
            //  "\n"           10           New line
            //  "\x0B"         11           Vertical Tab
            //  "\r"           13           New Line in Mac
            //  " "            32           Space

            $what   = "\\x00-\\x20";    //all white-spaces and control chars
        }

        return trim( preg_replace( "/[".$what."]+/" , $with , $str ) , $what );
    }
}

/**
 * Find the starting Monday for the given week (or for the current week if no date is passed)
 *
 * This is required as by default in PHP, strtotime considers Sunday the first day of a week,
 * making strtotime('Monday this week') on a Sunday return the adjacent Monday instead of the
 * previous one.
 *
 * @param string|\DateTime|null $date
 * @return \DateTime
 */
if(!function_exists('getStartOfWeekDate')) {
    function getStartOfWeekDate($date = null)
    {
        if ($date instanceof \DateTime) {
            $date = clone $date;
        } else if (!$date) {
            $date = new \DateTime();
        } else {
            $date = new \DateTime($date);
        }

        $date->setTime(0, 0, 0);

        if ($date->format('N') == 1) {
            // If the date is already a Monday, return it as-is
            return $date;
        } else {
            // Otherwise, return the date of the nearest Monday in the past
            // This includes Sunday in the previous week instead of it being the start of a new week
            return $date->modify('last monday');
        }
    }
}