<?php

/*
 * Copyright (c) 2018 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 *  contact@earworks.co.id
 */
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description of Users_model
 *
 * @author Fajar Dwi Rianto <fajar@earworks.co.id>
 * @since 1.0
 */
class Users_model extends CI_Model {

    const TABLE_NAME = 'users';

    public function get($userid = null) {
        $this->db->select('t0.userid, '
                . 't0.username, '
                . 't0.useremail, '
                . 't0.userstatus, '
                . 't0.created, '
                . 't1.profilename, '
                . 't1.profileaddress, '
                . 't1.profilemobile, '
                . 't3.rolename, '
                .  't3.roleid');
        $this->db->from(self::TABLE_NAME . ' `t0`');
        $this->db->join('profiles `t1`', 't0.userid = t1.userid', 'left');
        $this->db->join('userrole `t2`', 't0.userid = t2.userid', 'left');
        $this->db->join('roles `t3`', 't2.roleid = t3.roleid', 'left');
        $this->db->where_in('t2.roleid', array(2,3,4));
        if ($userid) {
            $this->db->where('t0.userid', $userid);
        }
        $query = $this->db->get();
        $function = ($userid) ? 'row_array' : 'result_array';
        return $query->$function();
    }

    public function check($userid = null) {
        $this->db->from(self::TABLE_NAME);
        $this->db->where('userid', $userid);
        return $this->db->count_all_results();
    }

    public function create($input = array()) {
        $this->db->trans_begin();
        $this->db->insert(self::TABLE_NAME, array(
            'username' => $input['username'],
            'useremail' => $input['useremail'],
            'userpassword' => $input['userpassword'],
            'userstatus' => 1,
            'created' => time(),
            'modified' => time()
        ));
        $userid = $this->db->insert_id();
        $this->db->insert('profiles', array(
            'userid' => $userid,
            'profilename' => $input['profilename'],
            'profilemobile' => $input['profilemobile'],
            'profileaddress' => $input['profileaddress'],
            'profilebirth' => $input['profilebirth'],
            'profileprovinsi' => $input['profileprovinsi'],
            'profilekodepost' => $input['profilekodepost'],
            'created' => time(),
            'modified' => time()
        ));
        $this->db->insert('userrole', array(
            'userid' => $userid,
            'roleid' => $input['roleid'],
            'created' => time(),
            'modified' => time()
        ));
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
            return true;
        }
    }

    public function update($input = array(), $userid = null) {
        $this->db->trans_begin();
        $data_user = array();
        if ($input['username']) {
            $data_user['username'] = $input['username'];
        }
        if ($input['useremail']) {
            $data_user['useremail'] = $input['useremail'];
        }
        if ($input['userpassword']) {
            $data_user['userpassword'] = $input['userpassword'];
        }
        $data_user['userstatus'] = 1;
        $data_user['modified'] = time();
        $this->db->set($data_user);
        $this->db->where('userid', $userid);
        $this->db->update(self::TABLE_NAME);

        $this->db->set(array(
            'profilename' => $input['profilename'],
            'profilemobile' => $input['profilemobile'],
            'profileaddress' => $input['profileaddress'],
            'modified' => time()
        ));
        $this->db->where('userid', $userid);
        $this->db->update('profiles');
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
            return true;
        }
    }

    public function delete($userid = null) {
        $this->db->where('userid', $userid);
        return $this->db->delete(self::TABLE_NAME);
    }

    public function suspend($userid = null, $status) {
        $this->db->set(array('userstatus' => $status));
        $this->db->where('userid', $userid);
        return $this->db->update(self::TABLE_NAME);
    }

}
