<?php

class User extends ActiveRecord\Model {
    static $primary_key = "id";
    static $has_many = array(
        array('user_roles'),
        array('roles', 'through' => 'user_roles')
            //,array('documents')
    );

    /* static $has_one = array(
      array('user_departments'),
      array('departments', 'through' => 'user_departments')
      ); */

    static function get_user($id, $session) {
        return self::find(array('id' => $id, 'session' => $session));
    }

    function get_full_name() {
        return $this->first_name . ' ' . $this->last_name;
    }
	static function NewInsert($data){
		$sql ="
INSERT INTO [dbo].[users]
           ([first_name]
           ,[last_name]
           ,[login]
           ,[password]
           ,[email]
		   ,[profit_id]
           ,[status_id]
           ,[connected]
           ,[session]
           ,[last_login]
           ,[avatar]
           ,[color]
           ,[created_at]
           ,[updated_at])
     VALUES
           (?
           ,?
           ,?
           ,convert(varbinary,?)
           ,''
		   ,?
           ,1
           ,0
           ,''
           ,''
           ,'default-avatar.png'
           ,''
           ,GETDATE()
           ,GETDATE())"; 
			$pdo = self::connection()->connection;
			$Exe = $pdo->prepare($sql);
			return $Exe->execute(array($data['first_name'],$data['last_name'],$data['login'],$data['password'],$data['profit_id']));
		   
	}
	static function NewUpdateProfile($data){
		$sql ="UPDATE [dbo].[users]
   SET [first_name] =?
      ,[last_name] = ?
      ,[login] = ?
	  ,[profit_id] = ?
      ,[updated_at] = GETDATE()
 WHERE id = ?"; 
			$pdo = self::connection()->connection;
			$Exe = $pdo->prepare($sql);
			return $Exe->execute(array($data['first_name'],$data['last_name'],$data['login'],$data['profit_id'],$data['id']));
		   
	}
	static function NewUpdatePass($data){
		$sql ="UPDATE [dbo].[users]
   SET [password] = PWDENCRYPT(?)
      ,[updated_at] = GETDATE()
 WHERE id = ?"; 
			$pdo = self::connection()->connection;
			$Exe = $pdo->prepare($sql);
			return $Exe->execute(array($data['password'],$data['id']));
		   
	}

}
