<?php

if (!function_exists('random_string')) {
    function random_string($type = 'alnum', $len = 8)
    {
        switch ($type) {
            case 'basic'    :
                return mt_rand();
                break;
            case 'alnum'    :
            case 'numeric'    :
            case 'nozero'    :
            case 'alpha'    :

                switch ($type) {
                    case 'alpha'    :
                        $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                        break;
                    case 'alnum'    :
                        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                        break;
                    case 'numeric'    :
                        $pool = '0123456789';
                        break;
                    case 'nozero'    :
                        $pool = '123456789';
                        break;
                }

                $str = '';
                for ($i = 0; $i < $len; $i++) {
                    $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
                }
                return $str;
                break;
            case 'unique'    :
            case 'md5'        :
                return md5(uniqid(mt_rand()));
                break;
            case 'encrypt'    :
            case 'sha1'    :
                return sha1(uniqid(mt_rand(), true));
                break;
        }
    }
}

if (!function_exists('make_replacements')) {
    function make_replacements($phrase, array $replace)
    {
        foreach ($replace as $key => $value) {
            $phrase = str_replace('{'.$key.'}', $value, $phrase);
        }

        return $phrase;
    }
}

if (!function_exists('yes_no')) {
    function yes_no($value)
    {
        if ($value == true) {
            return "yes";
        } else {
            return "no";
        }
    }
}

if (!function_exists('get_to_text_area')) {
    function get_to_text_area($content)
    {
        if (empty($content)) {
            return $content;
        }

        return str_replace("<br/>", "\n", $content);
    }
}

if (!function_exists('get_from_text_area')) {
    function get_from_text_area($content)
    {
        if (empty($content)) {
            return $content;
        }

        $final_content = str_replace("\r\n", "<br/>", $content);
        return str_replace("\n", "<br/>", $final_content);
    }
}

if (!function_exists('urlize')) {
    function urlize($s)
    {

        $s = utf8_decode($s);
        $s = _convert_high_ascii($s);  ## convert high-ASCII chars to 7bit.
        $s = strtolower($s);           ## lower-case.
        $s = strip_tags($s);       ## remove HTML tags.
        $s = preg_replace('!&[^;\s]+;!', '', $s);
        $s = preg_replace('!&[^;\s]+;!', '', $s);         ## remove HTML entities.
        $s = preg_replace('![^\w\s-]!', '', $s);           ## remove non-word/space chars.
        $s = preg_replace('![\s_]+!', '-', $s);               ## change space chars to underscores.
        $s = urlencode($s);
        return $s;
    }
}


//@todo when possible review this to improve performance
if (!function_exists('_convert_high_ascii')) {
    function _convert_high_ascii($s)
    {
        $HighASCII = array(
            "!\xc0!" => 'A',    # A`
            "!\xe0!" => 'a',    # a`
            "!\xc1!" => 'A',    # A'
            "!\xe1!" => 'a',    # a'
            "!\xc2!" => 'A',    # A^
            "!\xe2!" => 'a',    # a^
            "!\xc4!" => 'Ae',   # A:
            "!\xe4!" => 'ae',   # a:
            "!\xc3!" => 'A',    # A~
            "!\xe3!" => 'a',    # a~
            "!\xc8!" => 'E',    # E`
            "!\xe8!" => 'e',    # e`
            "!\xc9!" => 'E',    # E'
            "!\xe9!" => 'e',    # e'
            "!\xca!" => 'E',    # E^
            "!\xea!" => 'e',    # e^
            "!\xcb!" => 'Ee',   # E:
            "!\xeb!" => 'ee',   # e:
            "!\xcc!" => 'I',    # I`
            "!\xec!" => 'i',    # i`
            "!\xcd!" => 'I',    # I'
            "!\xed!" => 'i',    # i'
            "!\xce!" => 'I',    # I^
            "!\xee!" => 'i',    # i^
            "!\xcf!" => 'Ie',   # I:
            "!\xef!" => 'ie',   # i:
            "!\xd2!" => 'O',    # O`
            "!\xf2!" => 'o',    # o`
            "!\xd3!" => 'O',    # O'
            "!\xf3!" => 'o',    # o'
            "!\xd4!" => 'O',    # O^
            "!\xf4!" => 'o',    # o^
            "!\xd6!" => 'Oe',   # O:
            "!\xf6!" => 'oe',   # o:
            "!\xd5!" => 'O',    # O~
            "!\xf5!" => 'o',    # o~
            "!\xd8!" => 'Oe',   # O/
            "!\xf8!" => 'oe',   # o/
            "!\xd9!" => 'U',    # U`
            "!\xf9!" => 'u',    # u`
            "!\xda!" => 'U',    # U'
            "!\xfa!" => 'u',    # u'
            "!\xdb!" => 'U',    # U^
            "!\xfb!" => 'u',    # u^
            "!\xdc!" => 'Ue',   # U:
            "!\xfc!" => 'ue',   # u:
            "!\xc7!" => 'C',    # ,C
            "!\xe7!" => 'c',    # ,c
            "!\xd1!" => 'N',    # N~
            "!\xf1!" => 'n',    # n~
            "!\xdf!" => 'ss'
        );
        $find = array_keys($HighASCII);
        $replace = array_values($HighASCII);
        $s = preg_replace($find, $replace, $s);
        return $s;
    }
}

if (!function_exists('cut_string')) {
    function cut_string($value, $cut_point, $suffix = "")
    {
        if (empty($value) || trim($value) == "") {
            return $value;
        }

        if (strlen($value) <= $cut_point) {
            return $value;
        }

        $final_value = substr($value, 0, $cut_point - strlen($suffix));
        $last_space_pos = strripos($final_value, " ");
        if ($last_space_pos === false) {
            return $final_value.$suffix;
        }

        return substr($final_value, 0, $last_space_pos).$suffix;
    }
}

if (!function_exists('get_values_string')) {
    function get_values_string($values)
    {
        $titles = array();
        foreach ($values as $value) {
            array_push($titles, $value->title);
        }
        return concat_strings($titles);
    }
}

if (!function_exists('concat_strings')) {
    function concat_strings($strings, $with_commercial_e = true)
    {
        $glue = ", ";
        $size = count($strings);
        if (empty($strings)) {
            return "";
        }

        if ($with_commercial_e && $size > 1) {
            $last = array_pop($strings);
            $start = join($glue, $strings);
            return "{$start} & {$last}";
        } else {
            return join($glue, $strings);
        }
    }
}