<?php
 namespace Shared;

 class DBUser
 {
    public static $tableName="user";
    
    public static $colID="id";
    public static $colUserName="username";
    public static $colDisplayName="displayname";
    public static $colType="type";
    public static $colDomain="domain";
    public static $colActive="active";
    public static $colPasswordHash="passwordHash";
    public static $colAdmin="admin";



    public static function GetAllUsersFromDB()
    {
        $rows= \Shared\MySQL::GetMySQL()->get(self::$tableName,self::GetColsForSelect());
        $users=array();
        if(!is_array($rows))
            return $users;
        foreach($rows as $key=>$row)
        {
            $row[self::$colAdmin]=$row[self::$colAdmin]==1;
            $row[self::$colActive]=$row[self::$colActive]==1;
            $users[$key]= $row;
        }    
        return $users;
    }

    public static function GetUserByID($id)
    {
        $rows= \Shared\MySQL::GetMySQL()->where(self::$colID, $id)->get(self::$tableName,self::GetColsForSelect());
        if(is_array($rows) && count($rows)==1)
            return $rows[0];
    }

    public static function GetActiveUserByUsernameFromDB($uname)
    {
        $msql=\Shared\MySQL::GetMySQL();
        $rows= $msql->where(self::$colUserName, $uname)->where(self::$colActive,true)->get(self::$tableName,self::GetColsForSelect());
        if(is_array($rows))
        {
            $rows[0][self::$colAdmin]=$rows[0][self::$colAdmin]==1;
            $rows[0][self::$colActive]=$rows[0][self::$colActive]==1;
            return $rows[0];
        }
        return null;
    }

    public static function GetActiveUserByUsernameAndDomainFromDB($uname,$domain)
    {
        $msql=\Shared\MySQL::GetMySQL();
        $rows= $msql->where(self::$colUserName, $uname)->where(self::$colDomain,$domain)->where(self::$colActive,true)->get(self::$tableName,self::GetColsForSelect());
        //var_dump($msql->last_query());
        //var_dump($rows);
        if(is_array($rows))
        {
            $rows[0][self::$colAdmin]=$rows[0][self::$colAdmin]==1;
            $rows[0][self::$colActive]=$rows[0][self::$colActive]==1;
            return $rows[0];
        }
        return null;
    }


    private static function GetColsForSelect()
    {
        return array(        
            self::$colID,
            self::$colUserName,
            self::$colDisplayName,
            self::$colType,
            self::$colDomain,
            self::$colActive,
            self::$colPasswordHash,
            self::$colAdmin
        );
    }

    public static function UpdateOrSave($id,$dataArray)
    {
        unset($dataArray[self::$colID]);

        if($id!=null)
        {
           return $sql=\Shared\MySQL::GetMySQL()->where(self::$colID, $id)->update(self::$tableName, $dataArray);              
        }
        else
        {
           return $sql=\Shared\MySQL::GetMySQL()->insert(self::$tableName, $dataArray);  
        }
    }

    public static function Delete($id)
    {
        return $sql=\Shared\MySQL::GetMySQL()->where(self::$colID, $id)->delete(self::$tableName);     
    }

 }