<?php
/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 5/10/2018
 * Time: 2:14 PM
 */

function Search_Quad($search_query, $table, $roll = null)
{
    mysqli_set_charset($GLOBALS['link'], "utf8");
    $query = "SELECT " . $table . ".* FROM " . $table . " WHERE " . $search_query;
    if ($roll != null) {
        $query .= $roll;
    }
    $result = mysqli_query($GLOBALS['link'], $query);

    return $result;
}

function bubbleSort(array $arr, $key, $mod)
{
    $sorted = false;
    while (false === $sorted) {
        $sorted = true;
        for ($i = 0; $i < count($arr) - 1; ++$i) {
            $temp1 = $arr[$i];
            $temp2 = $arr[$i + 1];
            $current = $arr[$i][$key];
            $next = $arr[$i + 1][$key];
            if ($mod) {
                if ($next < $current) {
                    $arr[$i] = $temp2;
                    $arr[$i + 1] = $temp1;
                    $sorted = false;
                }
            } else {
                if ($next > $current) {
                    $arr[$i] = $temp2;
                    $arr[$i + 1] = $temp1;
                    $sorted = false;
                }
            }
        }
    }

    return $arr;
}

function QrGenerator($number)
{
    require_once 'QR/BarcodeQR.php';
    if (isset($number)) {
        $qr = new BarcodeQR();
        $qr->text($number);
        $qr->draw();
    } else {
        $qr = new BarcodeQR();
        $qr->text('HackHackHackHack');
        $qr->draw();
    }
}

/**
 * convert string array image to one arrat
 */
function ConvertStringToImageArray($string, $first_item = false, $KeyToArray = '[#]')
{
    $return = [];
    $oneItem = "";
    $flag = true;
    if (strlen($string) > 0) {
        if (substr_count($string, $KeyToArray) > 0) {
            if (strpos($string, $KeyToArray) == 0 AND strrpos($string, $KeyToArray) == 0) {
                $return[] = array('url' => str_replace($KeyToArray, '', $string));
                if ($flag) {
                    $flag = false;
                    $oneItem = str_replace($KeyToArray, '', $string);
                }
            } else {
                $array = explode($KeyToArray, $string);
                if (count($array) > 0) {
                    for ($i = 0; $i < count($array); $i += 1) {
                        if (strlen($array[$i]) > 0) {
                            if ($flag) {
                                $flag = false;
                                $oneItem = str_replace($KeyToArray, '', $array[$i]);
                            }
                            $return[] = array('url' => str_replace($KeyToArray, '', $array[$i]));
                        }
                    }
                }
            }
        } else {
            $return[] = array('url' => str_replace($KeyToArray, '', $string));
            if ($flag) {
                $flag = false;
                $oneItem = str_replace($KeyToArray, '', $string);
            }
        }
    }
    return ($first_item ? $oneItem : $return);
}

function is_image($path)
{

    if (stripos($path, ".jpg") OR strpos($path, ".jpeg") OR stripos($path, ".png")) {
        return true;
    }
//    $a = getimagesize($path);
//    $image_type = $a[2];
//
//    if (in_array($image_type, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP))) {
//        return true;
//    }
    return false;
}


/**
 * @param mixed $string <p>
 * @param string $interval (true to English) and (False to Persian)
 *
 * @return mixed|string
 */
function convert($string, $interval = 'true')
{
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    $num1 = range(0, 9);
    $return = '';
    if ($interval) {
        $convertedPersianNums = str_replace($persian, $num1, $string);
        $return = str_replace($english, $num1, $convertedPersianNums);
    } else {
        $englishNumbersOnly = str_replace($english, $persian, $string);
        $return = str_replace($persian, $persian, $englishNumbersOnly);
    }

    return $return;
}


function TimeNowJ($d = 0)
{
    require_once 'jdf/jdf.php';
    $timezone = 0;//برای 3:30 عدد 12600 و برای 4:30 عدد 16200 را تنظیم کنید
    $now = date("Y-m-d", time() + $timezone);
    $time = date("H:i:s", time() + $timezone);
    list($year, $month, $day) = explode('-', $now);
    list($hour, $minute, $second) = explode(':', $time);
    return convert(jdate("Y/m/d", mktime($hour, $minute, $second, $month, $day + $d, $year)), true);
}

function TimeNowG()
{
    return ConvertDataToFormat(date("Y-m-d", time()));
}

function ConvertToJalali($data, $mode = false)
{
    require_once 'jdf/jdf.php';
    $timezone = 0;//برای 3:30 عدد 12600 و برای 4:30 عدد 16200 را تنظیم کنید
    $now = date("Y-m-d", time() + $timezone);
    $time = date("H:i:s", time() + $timezone);
    list($year, $month, $day) = explode('-', $now);
    list($hour, $minute, $second) = explode(':', $time);
    $timeToDay = convert(jdate("Y/m/d", mktime($hour, $minute, $second, $month, $day, $year)), true);
    $date1 = date_create($data);
    $def1 = gregorian_to_jalali($date1->format("Y"), $date1->format("m"), $date1->format("d"), '/');

    return array(
        "now" => convert($timeToDay, $mode),
        "date" => convert($def1, $mode),
        "def" => convert((int)(date_diff(date_create($def1), date_create($timeToDay))->format("%R%a")), $mode)
    );
}

function ConvertToGregorian($data, $mode = false)
{
    require_once 'jdf/jdf.php';
    $data = explode("/", $data);
    $data = jalali_to_gregorian($data[0], $data[1], $data[2]);
    return convert($data, $mode);
}

function ConvertDataToFormat($date, $format = "Y/m/d")
{
    return date_format(date_create($date), $format);
}

function DateDifferent($Start, $End)
{
    return (int)date_diff(date_create($Start), date_create($End))->format("%R%a");
}


function ConvertToCardNumber($string, $maskingCharacter = '-')
{
    return
        substr($string, 0, 4) . $maskingCharacter
        . substr($string, 4, 4) . $maskingCharacter
        . substr($string, 8, 4) . $maskingCharacter
        . substr($string, 12, 4);
}

function RemovePartOfString($string, $maskingCharacter = '-')
{
//    return substr($string, 0, 4) . str_repeat($maskingCharacter, strlen($string) - 8) . substr($string, -4);
}


function UserType($id)
{
    try {

        $select = (new DAL("constant.php"))->Select("SELECT * FROM system_user_access WHERE aua_id='{$id}'");
        if ($select)
            return $select[0]['aua_type'];
        return '';
    } catch (Exception $e) {
        return '';
    }
}

function DeSerializeArray($Array)
{
    try {
        $object = array();
        if (count($Array) > 0) {
            foreach ($Array as $item) {
                if (count($item) == 2) {
                    $object[$item['name']] = $item['value'];
                }
            }
        }
        return $object;
    } catch (Exception $exception) {
        return $exception;
    }
}

function GetParam($data)
{
    $param = array();
    if (isset($data)) {
        foreach ($data as $key => $value) {
            if (strlen($value) > 0)
                $param[$key] = $value;
        }
    }
    return $param;
}

/*

function str_replace_once($needle, $replace, $haystack)
{
    $pos = strpos($haystack, $needle);
    if ($pos === false) {
        return $haystack;
    }

    return substr_replace($haystack, $replace, $pos, strlen($needle));
}





function BigRandomNumber($min, $max)
{
    $difference = bcadd(bcsub($max, $min), 1);
    $rand_percent = bcdiv(mt_rand(), mt_getrandmax(), 8); // 0 - 1.0

    return bcadd($min, bcmul($difference, $rand_percent, 8), 0);
}



*/
/*


function SendSMS($number, $text)
{
    require_once('sms/SendMessage.php');
    $sendSms = new SendMessage(SMS_USER, SMS_PASS, SMS_NUMBER);
    $sendSms->SendOneMessage($number, $text);
}


function AddLatariPoint($ID)
{
    Connect();
    $select = Select("SELECT * FROM latari_system WHERE client_id='{$ID}'");
    if ($select AND mysqli_num_rows($select) == 1) {
        $query = ("UPDATE latari_system SET client_point=client_point+1 WHERE client_id='{$ID}'");
    } else {
        $query = ("INSERT INTO latari_system (client_id, client_point) VALUES ('{$ID}', '1')");
    }
    $query = DoCommand($query);
    DisConnect();

    return $query;
}

function GetLatariPoint($ID)
{
    Connect();
    $select = Select("SELECT latari_system.client_point FROM latari_system WHERE client_id='{$ID}'");
    DisConnect();
    if (isset($select) AND $select AND mysqli_num_rows($select) == 1) {
        $select = mysqli_fetch_assoc($select);

        return $select['client_point'];
    } else {
        return 0;
    }
}

function GetTotalDisCount($ID)
{
    Connect();
    $select = Select("SELECT client_list.cTotalDiscount FROM client_list WHERE ID='{$ID}'");
    DisConnect();
    if (isset($select) AND $select AND mysqli_num_rows($select) == 1) {
        $select = mysqli_fetch_assoc($select);

        return $select['cTotalDiscount'];
    } else {
        return 0;
    }
}

function GetTotalPay($ID)
{
    Connect();
    $select = Select("SELECT acceptor_list.aTotalPayCount FROM acceptor_list WHERE ID='{$ID}'");
    DisConnect();
    if (isset($select) AND $select AND mysqli_num_rows($select) == 1) {
        $select = mysqli_fetch_assoc($select);

        return $select['aTotalPayCount'];
    } else {
        return 0;
    }
}

function RestartLatariPoint($ID)
{
    Connect();
    DoCommand("UPDATE latari_system SET client_point=0 WHERE client_id='{$ID}'");
    DisConnect();

    return true;
}*/