<?php



require_once APP_DIR . 'libs/Database.php';

class AuthModel

{

    private $table  = TBL_USERS;

    private $column_pk = 'usr_id';

    private $_db;

    public $_set_colum = "*";

    private $_update_colum = "";



    public function __construct()

    {

        $this->_db = new Database();

    }

    public function resetPk()

    {



        $this->column_pk = 'id';

    }



    public function checkUser($type = 'id', $data = '')

    {

        if ($type == 'user') {

            $this->column_pk = 'usr_name';

        }

        $this->_db->query("SELECT " . $this->_set_colum . " FROM " . $this->table . " WHERE " . $this->column_pk . " = '" . $data . "'");

        $res =  $this->_db->single();

        return $res;

    }



    public function getUserDetails($uid)

    {

        $this->_db->query("SELECT " . $this->_set_colum . " FROM " . $this->table . " WHERE " . $this->column_pk . " = '" . $uid . "'");

        $res =  $this->_db->single();

        return $res;

    }



    public function resetColum()

    {

        $this->_set_colum = "";

        $this->_update_colum = "";

    }



    public function UpdateUserDetails($data, $id, $type = 'id')

    {

        if ($type == 'user') {

            $this->column_pk = 'usr_name';

        }

        foreach ($data as $key => $val) {

            $this->_update_colum .= $key . " = :" . $key;

            if (!($key == array_key_last($data))) {

                $this->_update_colum .= ",";

            }

        }

        $sql = " UPDATE " . $this->table  . " SET " . $this->_update_colum . " WHERE " . $this->column_pk . " = '" . $id . "' ";



        try {

            if (!$this->_db->inTransaction()) {

                $this->_db->beginTransaction();

            }

            $this->_db->query($sql);

            foreach ($data as $key => $val) {

                $this->_db->bind(":" . $key, $val);

            }

            $retVal = $this->_db->execute();

            if ($retVal) {

                $this->_db->commit();

            }

            return $retVal;

        } catch (Exception $ex) {

            $this->_db->rollBack();

            return false;

        }

    }

}

