<?php

/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 10/6/2018
 * Time: 3:49 PM
 */
class DAL
{

    protected $link;
    protected $mysqli;

    protected $ServerName;
    protected $DBName;
    protected $User;
    protected $Pass;

    public function __construct($config)
    {
        require_once($config);

        $this->ServerName = DB_SERVER;
        $this->DBName = DB_NAME;
        $this->User = DB_USER;
        $this->Pass = DB_PASS;
        return $this;
    }


    public function DAL($config)
    {
        require_once($config);
        $this->ServerName = DB_SERVER;
        $this->DBName = DB_NAME;
        $this->User = DB_USER;
        $this->Pass = DB_PASS;
        return $this;
    }

    protected function Connect()
    {

        $this->link = mysqli_connect($this->ServerName, $this->User, $this->Pass, $this->DBName);
        $this->mysqli = new mysqli($this->ServerName, $this->User, $this->Pass, $this->DBName);
        mysqli_set_charset($this->link, "utf8");
        mysqli_select_db($this->link, $this->DBName);
        return $this->mysqli;
    }

    public function Select($SelectSql)
    {
//        mysqli_fetch_assoc
        $this->Connect();
        $result = null;
        mysqli_set_charset($this->link, "utf8");
        $result = mysqli_query($this->link, $SelectSql);
        mysqli_set_charset($this->link, "utf8");
        $this->DisConnect();
        try {
            if (!empty($result)) {
                $template = array();
                $return = array();
                $finfo = $result->fetch_fields();
                foreach ($finfo as $val):
                    $template[] = $val->name;
                endforeach;
                while ($row = mysqli_fetch_assoc($result)):
                    $return[] = array();
                    $index = count($return) - 1;
                    for ($i = 0; $i < count($template); $i += 1):
                        $return[$index][$template[$i]] = $row[$template[$i]];
                    endfor;
                endwhile;
                $result->free();

                if ((isset($return)) AND (!empty($return)) AND (count($return) > 0)) {
                    return $return;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch
        (Exception $e) {
            return false;
        }
    }

    public function DoCommand($sql)
    {
        $this->Connect();
        if (mysqli_query($this->link, $sql)) {
            $this->DisConnect();
            return true;
        } else {
            $this->DisConnect();
            return false;
        }
    }

    public function DoCommandID($sql)
    {
        $this->Connect();
        if (mysqli_query($this->link, $sql)) {
            $id = mysqli_insert_id($this->link);
            $this->DisConnect();
            return $id;
        } else {
            $this->DisConnect();
            return false;
        }
    }

    public function Connection($comment)
    {

        if ($comment == "open")
            $result = $this->Connect();
        if ($comment == 'close')
            $result = $this->DisConnect();
        return $result;
    }

    protected function DisConnect()
    {
        if (mysqli_close($this->link)) {
            return true;
        } else {
            return false;
        }
    }


    public function str_to_hex($string)
    {
        $hexstr = unpack('H*', $string);
        return array_shift($hexstr);
    }

    public function hex_to_str($string)
    {
        return hex2bin("$string");
    }
}