<?php

/*
 * *************************************************
 * Copyright (C) 2020 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

defined('BASEPATH') OR exit('No direct script access allowed');

if (!function_exists('ancillary_object')) {

    function ancillary_object() {
        $ancillary = & get_instance();
        return $ancillary;
    }

}
if (!function_exists('nm')) {

    function nm($number, $point = 0) {
        if ($number < 0) {
            $number = $number * -1;
            return '(' . number_format($number, $point, ',', '.') . ')';
        } else {
            return number_format($number, $point, ',', '.');
        }
    }

}
if (!function_exists('nm2')) {

    function nm2($number) {
        return nm($number, 2);
    }

}
if (!function_exists('diff_percent')) {

    function diff_percent($num1, $num2) {
        $diff = $num2 - $num1;
        if ($num1 == $num2) {
            return 0;
        }
        return $num1 != 0 ? ($diff / $num1) * 100 : 100;
    }

}
if (!function_exists('target_view')) {

    function target_view($method_string) {
        if (strpos($method_string, '::') !== false) {
            $view_string = strtolower(str_replace('::', '/', $method_string));
        } else {
            $view_string = $method_string;
        }
        return $view_string;
    }

}
if (!function_exists('phone')) {

    function phone($text) {
        // /^\+?(62|65)/ // more than 1 area code
//        $sanitize_phone = preg_replace("/^\+?62/", '', str_replace('-','',$text));
//        return preg_replace("/^0/", '', $sanitize_phone); // sanitize again
        return preg_replace("/^(?:62|021|0|\+62|\+6221)+/", '', preg_replace("/[^\d]/", '', $text));
    }

}
if (!function_exists('generate_token')) {

    function generate_token($use_timestamp = true) {
        Ci::helper('string');
        // first section
        $token = random_string('numeric', 5);
        // second section
        if ($use_timestamp === true) {
            $token .= time();
        } else {
            $token .= random_string('numeric', 10);
        }
        // last section
        $token .= random_string('numeric', 5);

        // the result is 20 length of random alpha numeric character
        return $token;
    }

}
if (!function_exists('extract_timestamp')) {

    function extract_timestamp($token) {
        $timestamp = substr($token, 5, 10);
        if (((string) (int) $timestamp === $timestamp) && ($timestamp <= PHP_INT_MAX) && ($timestamp >= ~PHP_INT_MAX)) {
            return $timestamp;
        } else {
            return false;
        }
    }

}
if (!function_exists('time_beauty')) {

    function time_beauty($stamp = null, $full = false) {
        if (!$stamp) {
            return;
        }
        $now = new DateTime;
        $ago = new DateTime(is_numeric($stamp) ? '@' . $stamp : $stamp);
        $diff = $now->diff($ago);

        $diff->w = floor($diff->d / 7);
        $diff->d -= $diff->w * 7;

        $string = array(
            'y' => lang('year'),
            'm' => lang('month'),
            'w' => lang('week'),
            'd' => lang('day'),
            'h' => lang('hour'),
            'i' => lang('minute'),
            's' => lang('second'),
        );
        foreach ($string as $k => &$v) {
            if ($diff->$k) {
                $v = $diff->$k . ' ' . $v;
            } else {
                unset($string[$k]);
            }
        }

        if (!$full)
            $string = array_slice($string, 0, 1);
        return $string ? implode(', ', $string) : lang('just_now');
    }

}
if (!function_exists('show_file')) {

    function show_file($file, $url = 'img/uploads', $class = 'img img-responsive') {
        if (!$file) {
            return'-';
        }
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        $is_img = in_array(strtolower($ext), ['png', 'jpg', 'jpeg', 'gif', 'webp', 'tiff']);
        $is_video = in_array(strtolower($ext), ['mp4', 'mov', 'wmv', 'avi', 'flv', 'mkv', 'mpeg']);
        $html = '-';
        if ($is_img) {
            $html = '<img src="' . base_url("{$url}/{$file}") . '" id="gif_img" class="' . $class . '" />';
        }
        if ($is_video) {
            $html = '<video id="gif_img" class="' . $class . ' img-video">'
                . '<source src="' . base_url("{$url}/{$file}") . '" type="video/' . $ext . '">'
                . 'Your browser does not support HTML video.'
                . '</video>';
        }
        return $html;
    }

}