<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Master_universal_user
 *
 * @author zmq163
 */
class Master_universal_user extends CI_Model {

    //put your code here
    var $mdb;

    public function __construct() {
        parent::__construct();
        $this->mdb = $this->load->database('mdb', TRUE);
        $this->load->model('Mira_applicatio_model');
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function updatePasswordSubmitModel($upadatePasswordData = NULL) {
        if ($upadatePasswordData != NULL) {
            if ($this->checkPassword($upadatePasswordData['autoId'], $upadatePasswordData['oldPassword'])) {
                $this->mdb->set('user_password', $upadatePasswordData['newPassword']);
                $this->mdb->where('auto_id', $upadatePasswordData['autoId']);
                $this->mdb->update('master_login_user');
                return ($this->mdb->affected_rows() > 0) ? TRUE : FALSE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    private function checkPassword($autoId = NULL, $password = NULL) {

        if ($autoId != NULL and $password != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('master_login_user');
            $this->mdb->where(array('auto_id' => $autoId, 'user_password' => $password));
            $query = $this->mdb->get();
            if ($query->num_rows() > 0) {
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function updateUserProfilePictureModel($profile_image_data = NULL) {
        if ($profile_image_data != NULL) {
            if ($this->checkUserProfile($profile_image_data['user_login_id'])) {
                $this->mdb->set('image', $profile_image_data['image']);
                $this->mdb->set('image_name', $profile_image_data['image_name']);
                $this->mdb->where('user_login_id', $profile_image_data['user_login_id']);
                $this->mdb->update('mira_worker_profile');
            } else {
                $this->mdb->insert('mira_worker_profile', $profile_image_data);
            }

            return ($this->mdb->affected_rows() > 0) ? TRUE : FALSE;
        } else {
            return FALSE;
        }
    }

    private function checkUserProfile($login_user_id) {
        $this->mdb->select('*');
        $this->mdb->from('mira_worker_profile');
        $this->mdb->where('user_login_id', $login_user_id);
        $query = $this->mdb->get();
        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getUserProfileModel($_regionCode = NULL) {
        if ($_regionCode != NULL) {
            $this->mdb->select(',master_user_details.*,master_login_user.*,mira_worker_profile.*,master_area_create_by_super.*');
            $this->mdb->from('master_login_user');
            $this->mdb->join('master_area_create_by_super', 'master_login_user.create_region_id=master_area_create_by_super.region_code', 'INNER');
            $this->mdb->join('master_user_details', 'master_login_user.user_login_id=master_user_details.user_login_id', 'INNER');
            $this->mdb->join('mira_worker_profile', 'master_login_user.user_login_id=mira_worker_profile.user_login_id', 'LEFT');
            $this->mdb->where('master_login_user.create_region_id', $_regionCode);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * This function check the user credential.
     * Return the data array according to user level.
     * 
     */
    public function universalLoginModel($_user_login_id = NULL, $_user_password = NULL) {
        $this->load->model('User_model');
        if ($_user_login_id != NULL and $_user_password != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('master_login_user');
            $this->mdb->where(array('user_login_id' => $_user_login_id, 'user_password' => $_user_password, 'user_activation_status' => 1));
            $user = $this->getData($this->mdb->get());
            if ($user) {
                if ($user[0]['user_level']) {
                    // This Block return the data below the super admin user.
                    $appadmin_and_user_right = NULL;
                    $app_detail = $this->Mira_applicatio_model->getApplicationByCondition(array('app_id' => $user[0]['app_id']));
                    $app_hirareky_level = $this->Mira_applicatio_model->getApplicationHirecharchyLevel($user[0]['app_id'], $user[0]['user_level']);
                    if ($user[0]['user_level'] == 1) {
                        $appadmin_and_user_right = $this->Mira_applicatio_model->getAppRightModel(array('app_id' => $user[0]['app_id'], 'mapp_right_status' => 1));
                    } else {
                        $appadmin_and_user_right = $this->User_model->getUserRightModel($user[0]['app_id'], $user[0]['create_region_id']);
                    }
                    return array('logo_url' => $app_detail[0]['app_logo_path'], 'create_region_id' => $user[0]['create_region_id'], 'app_id' => $user[0]['app_id'], 'status' => TRUE, 'user_level' => $user[0]['user_level'], 'usre' => $user, 'right' => $appadmin_and_user_right, 'app_level' => $app_hirareky_level, 'app_total_level' => $app_detail[0]['app_total_level']);
                } else {
                    // This Block return the data for super admin user.
                    return array('status' => TRUE, 'user_level' => $user[0]['user_level']);
                }
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     */
    public function getData($_query = NULL) {
        if ($_query != NULL and $_query->num_rows() > 0) {
            return $_query->result_array();
        } else {
            return FALSE;
        }
    }

}
