<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('ej')) {
    function ej($x)
    {
        echo json_encode($x);
    }
}

if (!function_exists('d')) {
    function d($x)
    {
        return die(json_encode($x));
    }
}

if (!function_exists('debuging')) {
    function debuging($x)
    {
        echo "<pre>";
        print_r($x);
        echo "</pre>";
        // exit;
    }
}

if (!function_exists('currency')) {
    function currency($x)
    {
        return number_format($x, 0, ',', '.');
    }
}

if (!function_exists('generator')) {
    function generator($length = 7)
    {
        return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
    }
}

if (!function_exists('replace_lower')) {
    function replace_lower($string = "")
    {
        return preg_replace("/[^A-Za-z0-9]/", "_", strtolower($string));
    }
}

if (!function_exists('ce')) {
    function ce($string = "")
    {
        return ucwords(strtolower($string));
    }
}

if (!function_exists('sc')) {
    function sc($string = "")
    {
        return ucfirst(strtolower($string));
    }
}

if (!function_exists("set")) {
    function set(&$string)
    {
        return isset($string) ? $string : FALSE;
    }
}

if (!function_exists('tanggal_tampil')) {
    function tanggal_tampil($tanggal = "")
    {
        $originalDate = $tanggal;
        $newDate = date("m/d/Y", strtotime($originalDate));
        return $newDate;
    }
}

if (!function_exists('insert_tanggal')) {
    function insert_tanggal($tanggal = "")
    {
        $newDate = date("Y-m-d", strtotime($tanggal));
        return $newDate;
    }
}

if (!function_exists('alphanumspace')) {
    function alphanumspace($string = "")
    {
        return preg_replace("/[^a-zA-Z0-9 ]+/", "", remove_duplicate_space($string));
    }
}

if (!function_exists('alphanum')) {
    function alphanum($string = "")
    {
        return preg_replace("/[^a-zA-Z0-9_]+/", "", remove_duplicate_space($string));
    }
}

if (!function_exists("remove_duplicate_space")) {
    function remove_duplicate_space($string = "")
    {
        return preg_replace('/\s+/', ' ', $string);
    }
}

if (!function_exists("dash")) {
    function dash($string = "")
    {
        return str_replace(" ", "-", $string);
    }
}

if (!function_exists("slug")) {
    function slug($string = "")
    {
        return strtolower(dash(remove_duplicate_space(alphanumspace($string))));
    }
}

if (!function_exists("remove_line_break")) {
    function remove_line_break($string = "")
    {
        return preg_replace("/\r|\n/", "", $string);
    }
}

if (!function_exists("validasi_input_artikel")) {
    function validasi_input_artikel($string = "")
    {
        return str_replace("'", "", remove_line_break($string));
    }
}

if (!function_exists('back')) {
    function back()
    {
        if (!empty($_SERVER['HTTP_REFERER'])) {
            return $_SERVER['HTTP_REFERER'];
        } else {
            return base_url();
        }
    }
}

if (!function_exists("d_success")) {
    function d_success($data)
    {
        d([
            "code"      => 200,
            "message"   => isset($data["message"]) ? $data["message"] : "Berhasil",
            "data"      => isset($data["data"]) ? $data["data"] : [],
        ]);
    }
}

if (!function_exists("d_error")) {
    function d_error($data)
    {
        d([
            "code"      => 201,
            "message"   => isset($data["message"]) ? $data["message"] : "Gagal",
            "data"      => isset($data["data"]) ? $data["data"] : [],
        ]);
    }
}

if (!function_exists('form_val_error_helper')) {
    function form_val_error_helper()
    {
        $CI = &get_instance();
        $indexError = 0;
        if (!empty($CI->form_validation->error_array())) {
            foreach ($CI->form_validation->error_array() as $row) {
                $message[$indexError] = $row . "<br>";
                $indexError++;
            }
            d_error([
                "message"   => implode(" ", $message),
                "data"      => []
            ]);
        } else {
            d_error([
                "message"   => "",
                "data"      => []
            ]);
        }
    }
}

if (!function_exists('bulan')) {
    function bulan($bln)
    {
        $nm = '';
        switch ($bln) {
            case '1':
                $nm = 'Januari';
                break;
            case '2':
                $nm = 'Februari';
                break;
            case '3':
                $nm = 'Maret';
                break;
            case '4':
                $nm = 'April';
                break;
            case '5':
                $nm = 'Mei';
                break;
            case '6':
                $nm = 'Juni';
                break;
            case '7':
                $nm = 'Juli';
                break;
            case '8':
                $nm = 'Agustus';
                break;
            case '9':
                $nm = 'September';
                break;
            case '10':
                $nm = 'Oktober';
                break;
            case '11':
                $nm = 'November';
                break;
            case '12':
                $nm = 'Desember';
                break;
            default:
                $nm = '';
                break;
        }
        return $nm;
    }
}

if (!function_exists('longdate_indo')) {
    function longdate_indo($tanggal = NULL, $pakaiHari = FALSE)
    {
        $date   = date_create($tanggal);
        $date   = date_format($date, "Y-m-d");
        $ubah = gmdate($date, time() + 60 * 60 * 8);
        $pecah = explode("-", $ubah);
        $tgl = $pecah[2];
        $bln = $pecah[1];
        $thn = $pecah[0];
        $bulan = bulan($pecah[1]);

        $nama = date("l", mktime(0, 0, 0, $bln, $tgl, $thn));
        $nama_hari = "";
        if ($nama == "Sunday") {
            $nama_hari = "Minggu";
        } else if ($nama == "Monday") {
            $nama_hari = "Senin";
        } else if ($nama == "Tuesday") {
            $nama_hari = "Selasa";
        } else if ($nama == "Wednesday") {
            $nama_hari = "Rabu";
        } else if ($nama == "Thursday") {
            $nama_hari = "Kamis";
        } else if ($nama == "Friday") {
            $nama_hari = "Jumat";
        } else if ($nama == "Saturday") {
            $nama_hari = "Sabtu";
        }
        if ($tanggal != null) {
            if ($pakaiHari) {
                return $nama_hari . ', ' . $tgl . ' ' . $bulan . ' ' . $thn;
            } else {
                return $tgl . ' ' . $bulan . ' ' . $thn;
            }
        } else {
            return "";
        }
    }
}

if (!function_exists("response_api")) {
    function response_api($data = [], $url = null)
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS      => $data,
        ));

        $response = curl_exec($curl);

        curl_close($curl);

        $json =  str_replace('\r\n     ', "---0", $response);
        $json =  str_replace('\r\n', "---", $json);
        $json = json_decode($json);
        return $json;
    }
}
