<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Users_model extends CI_Model {

    public function get_user_data($id) {
        if (!is_numeric($id))
            return null;
        $this->db->select('u.id as user_id,u.email,u.phone,u.company,us.*, g.name as grupa, u.first_name, u.last_name');
        $this->db->from('users u');
        $this->db->join('users_szczegoly us', 'us.id_users = u.id');
        $this->db->join('users_groups ug', 'ug.user_id = u.id');
        $this->db->join('groups g', 'g.id = ug.group_id');
        $this->db->where('u.id', $id);
        $this->db->limit(1);
        return $this->db->get()->row_array();
    }

    public function get_users_list($per_page = 10, $page = 1, $check_count, $group = '') {
        $this->db->select('u.id as user_id,u.first_name,u.last_name ,u.email,u.phone,u.company,us.*, g.name as grupa');
        $this->db->from('users u');
        $this->db->join('users_szczegoly us', 'us.id_users = u.id');
        $this->db->join('users_groups ug', 'ug.user_id = u.id');
        $this->db->join('groups g', 'g.id = ug.group_id');
        if (!empty($group))
            $this->db->where('g.name', $group);

        if ($check_count)
            return $this->db->get()->num_rows();
        else {
            if ($per_page != '')
                $this->db->limit($per_page);
            if ($page != 0) {
                $offset = ($page == 1) ? 0 : ($page - 1) * $per_page;
                $this->db->offset($offset);
            }
            $result = $this->db->get()->result_array();
            return $result;
        }
    }
    
    public function update_user_szczegoly($user_id, $tablica){
        $wynik = $this->db->update('users_szczegoly', $tablica, "id = ".$user_id);  
        if($wynik){
            return true;
        }
        else{
            return false;
        }
    }
    
    
    

}
