You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1113 lines
44 KiB
1113 lines
44 KiB
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
class User_model extends CI_Model {
|
|
|
|
protected $column_order = array('subscriber','u.email_address','telephone_number','mobile_number', 'address', 'noshow_complete', 'noshow_partial', 'noshow_moderation', 'status', null); //set column field database for datatable orderable
|
|
protected $column_search = array('CONCAT(u.first_name, " ", u.last_name)','u.email_address','us.address'); //set column field database for datatable searchable just firstname , lastname , address are searchable
|
|
protected $order = array('id' => 'desc'); // default order
|
|
|
|
public function __construct() {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function set_datatable_columns(){
|
|
$this->column_order = array(null, 'employee','u.email_address','u.username','ur.name','u.status', null); //set column field database for datatable orderable
|
|
$this->column_search = array('CONCAT(u.first_name, " ", u.last_name)','u.email_address','u.username', 'ur.name', 'u.status'); //set column field database for datatable searchable just firstname , lastname , address are searchable
|
|
$this->order = array('u.user_id' => 'desc'); // default order
|
|
}
|
|
|
|
private function subscribers_list_query($listType = 3){
|
|
$addWhere= "";
|
|
|
|
if($listType == 4) {
|
|
$addWhere = "
|
|
,DATE_FORMAT(us.subscription_date, '%d/%m/%Y %Hh%i') as subscription_date_op,
|
|
@number_of_registration := (SELECT COUNT(er.registration_id) FROM event_registration er WHERE er.subscriber = u.user_id and er.status = 1) AS number_of_registration,
|
|
@number_of_user_and_guest := (SELECT SUM(er.number_of_guest+1) FROM event_registration er WHERE er.subscriber = u.user_id and er.status = 1) AS number_of_user_and_guest,
|
|
ROUND((@number_of_user_and_guest / @number_of_registration), 2) AS avg_number_of_registration,
|
|
(SELECT DATE_FORMAT(MAX(er.date_time), '%d/%m/%Y %H:%i') FROM event_registration er WHERE er.subscriber = u.user_id AND er.status = 1) AS last_datetime_reserved,
|
|
(SELECT ev.title FROM event_registration erg INNER JOIN event_schedule esc USING (event_schedule_id) INNER JOIN event ev ON esc.event_id = ev.event_id WHERE erg.subscriber = u.user_id AND erg.status = 1 ORDER BY erg.date_time DESC LIMIT 1) last_event_reserved,
|
|
(SELECT DATE_FORMAT(MAX(date_time), '%d/%m/%Y %H:%i') FROM user_login_history WHERE user_id = u.user_id AND description = 'LOGIN') last_login,
|
|
(SELECT SUM(ew.number_of_places) FROM event_wait_list ew WHERE ew.wait_list_subscriber = u.user_id and ew.status = 1) AS number_of_places_reserved_on_wl,
|
|
(SELECT COUNT(ew.wait_list_id) FROM event_wait_list ew WHERE ew.wait_list_subscriber = u.user_id and ew.status = 1) AS total_number_registered_on_wl,
|
|
(SELECT DATE_FORMAT(MAX(ew.date_time), '%d/%m/%Y %H:%i') FROM event_wait_list ew WHERE ew.wait_list_subscriber = u.user_id AND ew.status = 1) AS last_date_reservation_on_wl,
|
|
(SELECT COUNT(edr.registration_id) FROM event_deregistration edr INNER JOIN event_registration er USING (registration_id) WHERE edr.subscriber = u.user_id AND edr.registration_id = er.registration_id) AS total_number_of_reservations_cancelled,
|
|
(SELECT SUM(edr.number_of_place) FROM event_deregistration edr INNER JOIN event_registration er USING (registration_id) WHERE edr.subscriber = u.user_id AND edr.registration_id = er.registration_id) AS total_number_of_reservations_places_cancelled,
|
|
(SELECT COUNT(ewdr.wait_list_id) FROM event_wait_list_deregistration ewdr INNER JOIN event_wait_list ew USING (wait_list_id) WHERE ewdr.wait_list_subscriber = u.user_id AND ewdr.wait_list_id = ew.wait_list_id) AS total_number_of_cancellation_on_wl";
|
|
}
|
|
|
|
return "SELECT
|
|
u.first_name,
|
|
u.last_name,
|
|
u.email_address,
|
|
u.user_id as subscriber_id,
|
|
us.subscriber as sub_id,
|
|
us.postal_code as code_postal,
|
|
DATE_FORMAT(us.birthday,'%d/%m/%Y') as birth_date,
|
|
us.city,
|
|
us.civility,
|
|
(CASE WHEN u.status = 1 THEN 'Activé' ELSE 'Désactivé' END) AS status,
|
|
-- us.additional_address,
|
|
us.address,
|
|
us.phone as telephone_number,
|
|
us.mobile as mobile_number
|
|
-- us.isSubscribedToMarketingAutomation,
|
|
-- (SELECT count(evta.attendance_id) FROM `event_registration` AS `evtreg`, `event_attendance` AS `evta`, `event` AS `evt`, `event_schedule` AS `evts` WHERE evts.event_schedule_id = evtreg.event_schedule_id AND evta.registration_id = evtreg.registration_id AND evtreg.subscriber = `u`.user_id AND evts.no_show_stat = 1 AND evt.event_id = evts.event_id AND evta.attendance_type = 0 AND evtreg.status = 1 AND evta.is_attended = 0) AS `noshow_complete`,
|
|
-- (SELECT count(evta.attendance_id) FROM `event_registration` AS `evtreg`, `event_attendance` AS `evta`, `event` AS `evt`, `event_schedule` AS `evts` WHERE evts.event_schedule_id = evtreg.event_schedule_id AND evta.registration_id = evtreg.registration_id AND evtreg.subscriber = `u`.user_id AND evts.no_show_stat = 1 AND evt.event_id = evts.event_id AND evta.attendance_type = 0 AND evtreg.status = 1 AND evta.is_attended = 2 ) AS `noshow_partial`,
|
|
-- (SELECT count(evta.attendance_id) FROM `event_registration` AS `evtreg`, `event_attendance` AS `evta`, `event` AS `evt`, `event_schedule` AS `evts`INNER JOIN gm_evsched_setting ges USING (event_schedule_id)WHERE evts.event_schedule_id = evtreg.event_schedule_id AND evta.registration_id = evtreg.registration_id AND evtreg.subscriber = `u`.user_id AND evts.no_show_stat = 1 AND evt.event_id = evts.event_id AND evta.attendance_type = 0 AND evtreg.status = 1 AND evta.is_attended = 0 AND evts.start_date_time > DATE_SUB(NOW(), INTERVAL (SELECT nshow_period FROM gm_subsettings_view) DAY)AND ges.mod_stat = 1) AS `noshow_moderation`
|
|
{$addWhere}
|
|
FROM
|
|
user u,
|
|
user_subscriber us,
|
|
user_role ur
|
|
WHERE
|
|
u.status != 0 and
|
|
us.subscriber = u.user_id and
|
|
ur.role_id = u.role_id and
|
|
u.role_id = 3 and
|
|
u.user_id IN (SELECT DISTINCT(er.subscriber) FROM event_registration er WHERE er.status=1)";
|
|
}
|
|
|
|
private function lemonde_users_list_query(){
|
|
return "SELECT
|
|
u.user_id,
|
|
ur.role_id,
|
|
u.first_name,
|
|
u.last_name,
|
|
u.username,
|
|
ur.name as role_name,
|
|
u.email_address,
|
|
u.status as user_status,
|
|
CONCAT((u.first_name), (' '),( u.last_name)) AS employee,
|
|
(CASE
|
|
WHEN u.status = 1 THEN 'Activé'
|
|
ELSE 'Désactivé'
|
|
END) AS status
|
|
FROM user u
|
|
LEFT JOIN user_role ur on ur.role_id = u.role_id
|
|
WHERE u.status != 0
|
|
AND u.role_id != 3
|
|
AND u.user_id != ?";
|
|
}
|
|
|
|
public function _get_datatables_query($user_id, $data_source, $listType){
|
|
$_query = ""; $_search = ""; $_ordey_by = ""; $data = array();
|
|
|
|
// This is originally a usertype, listType >=3 means get the list of user with role == 3
|
|
if($listType >= 3){
|
|
$_query = $this->subscribers_list_query($listType);
|
|
} else {
|
|
$_query = $this->lemonde_users_list_query();
|
|
array_push($data, $user_id);
|
|
}
|
|
|
|
// if datatable send POST for search
|
|
if($data_source['search']['value']) {
|
|
$i = 0;
|
|
foreach ($this->column_search as $item) {// loop column
|
|
if($i===0){ // first loop
|
|
array_push($data, "%".$data_source['search']['value']."%");
|
|
$_search .= " AND ( ".$item." LIKE ? ";
|
|
} else {
|
|
array_push($data, "%".$data_source['search']['value']."%");
|
|
$_search .= " OR ".$item." LIKE ? ";
|
|
}
|
|
$i++;
|
|
}
|
|
$_search .= ")";
|
|
}
|
|
|
|
if(isset($data_source['order'])) { // here order processing
|
|
if(isset($data_source['order']['0']['dir'])) {
|
|
$_ordey_by .= " ORDER BY ".$this->column_order[$data_source['order']['0']['column']]." ".$data_source['order']['0']['dir'];
|
|
}
|
|
} else if(isset($this->order)) {
|
|
$_ordey_by .= " ORDER BY ".key($this->order)." ".$this->order[key($this->order)];
|
|
}else{
|
|
$_ordey_by="";
|
|
}
|
|
|
|
return array("query"=>$_query.$_search.$_ordey_by, "data" =>$data);
|
|
}
|
|
|
|
public function get_user_iban($user_id)
|
|
{
|
|
|
|
if (empty($user_id)) {
|
|
return null;
|
|
}
|
|
|
|
// Applique la condition pour sélectionner l'utilisateur par ID
|
|
$this->db->select('iban'); // Sélectionne uniquement la colonne IBAN
|
|
$this->db->where('user_id', $user_id);
|
|
$query = $this->db->get('user');
|
|
|
|
// Vérifie si des résultats existent
|
|
if ($query->num_rows() > 0) {
|
|
return $query->row_array()['iban'];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
public function get_subscriber_information_list_export($user_id, $data_source, $user_type){
|
|
$_query = $this->_get_datatables_query($user_id, $data_source, $user_type);
|
|
$res= $this->db->query($_query["query"], $_query["data"])->result();
|
|
return $res;
|
|
}
|
|
|
|
public function get_datatables($user_id, $data_source, $user_type){
|
|
if($user_type!=3) { //not a subscriber
|
|
$this->set_datatable_columns(); //initialize columns
|
|
}
|
|
$_query = $this->_get_datatables_query($user_id, $data_source, $user_type);
|
|
|
|
if($data_source['length'] != -1){
|
|
$_query["query"] .= " LIMIT ".$data_source['start'].", ".$data_source['length'];
|
|
}
|
|
$data = $this->db->query($_query["query"], $_query["data"]);
|
|
return $data->result();
|
|
}
|
|
|
|
public function count_filtered($user_id, $data_source, $user_type){
|
|
if($user_type!=3) { //not a subscriber
|
|
$this->set_datatable_columns(); //initialize columns
|
|
}
|
|
$_query = $this->_get_datatables_query($user_id, $data_source, $user_type);
|
|
return $this->db->query($_query["query"], $_query["data"])->num_rows();
|
|
}
|
|
|
|
public function count_all($user_id, $data_source, $user_type){
|
|
if($user_type!=3) { //not a subscriber
|
|
$this->set_datatable_columns(); //initialize columns
|
|
}
|
|
$_query = $this->_get_datatables_query($user_id, $data_source, $user_type);
|
|
return $this->db->query($_query["query"], $_query["data"])->num_rows();
|
|
}
|
|
|
|
public function get_userinfo_by_email($email, $utype)
|
|
{
|
|
$this->db->select('*');
|
|
$this->db->where("email_address", $email);
|
|
$this->db->where("status", 1);
|
|
$this->db->where_in("role_id", $utype);
|
|
$this->db->limit(1);
|
|
$query = $this->db->get("user");
|
|
if($query->num_rows() == 1) {
|
|
return $query->row();
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function authenticate($user, $password, $isRememberMeTurnedON, $isBOUser=0)
|
|
{
|
|
// Filter by role_ids
|
|
$BORoles = $this->UserRole->allBORoles();
|
|
$tmp = [];
|
|
foreach($BORoles as $k => $v) {
|
|
$tmp[] = $v['role_id'];
|
|
}
|
|
$where = $isBOUser ? " IN (".implode(", ", $tmp).") " : " NOT IN (".implode(", ", $tmp).") ";
|
|
|
|
$this->db->select("u.*, p.privs, ur.isBOUser");
|
|
$this->db->join("user_role_privs p", "u.role_id = p.role_id", "left");
|
|
$this->db->join("user_role ur", "ur.role_id = ur.role_id AND ur.isBOUser = {$isBOUser} AND ur.role_id {$where} ", "left");
|
|
$this->db->where("ur.isBOUser", $isBOUser);
|
|
$this->db->where("ur.role_id $where");
|
|
$this->db->where("u.role_id $where");
|
|
$this->db->where('p.deletedAt IS NULL');
|
|
$this->db->where($isBOUser ? "u.username = '$user'":"u.email_address = '$user'");
|
|
$this->db->where('u.status', 1);
|
|
$this->db->limit(1);
|
|
$result = $this->db->get("user u");
|
|
|
|
if (!$result->num_rows() || !$result->row()) {
|
|
return false;
|
|
}
|
|
|
|
$row = $result->row();
|
|
|
|
// Regular password not valid
|
|
if (!$isRememberMeTurnedON && !password_verify($password, $row->password)) {
|
|
return false;
|
|
}
|
|
|
|
// Remember token is not valid
|
|
if ($isRememberMeTurnedON && ($password !== $row->rememberMeToken && !password_verify($password, $row->password))) {
|
|
return false;
|
|
}
|
|
|
|
// Doesnt have access to BO
|
|
if ($isBOUser && !$this->hasAbilityToLoginToBO($result)) {
|
|
return false;
|
|
}
|
|
return $row;
|
|
}
|
|
|
|
/**
|
|
* Check if BO User has ability to login to BO
|
|
*/
|
|
public function hasAbilityToLoginToBO($result) {
|
|
$row = $result->row();
|
|
$privs = json_decode($row->privs);
|
|
|
|
if (!$privs || !count($privs->misc) || !in_array(1, $privs->misc)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function password_reEnryption($user_id, $userRole, $password) {
|
|
$this->db->where('user_id', $user_id);
|
|
$this->db->where_in('role_id', $userRole);
|
|
$this->db->update('user', array('isPasswordReEncrypted' => 1, 'password' => hash_password($password)));
|
|
}
|
|
|
|
public function get_subscriber_profile($phone_number)
|
|
{
|
|
$this->db->select('*');
|
|
$this->db->where('phone_number', $phone_number);
|
|
$this->db->where('role_id', 3);
|
|
$this->db->where('status', 1);
|
|
$this->db->limit(1);
|
|
$query = $this->db->get("user");
|
|
if($query->num_rows() == 1) {
|
|
return $query->row();
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
public function authenticate_token($user_id, $utype)
|
|
{
|
|
$this->db->select('*');
|
|
$this->db->where('user_id',$user_id);
|
|
$this->db->where_not_in("role_id", $utype);
|
|
$this->db->where('status', 1);
|
|
$this->db->limit(1);
|
|
|
|
$query = $this->db->get("user");
|
|
if($query->num_rows()>0) {
|
|
return $query->row();
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
public function token_user_id_exist($user_id)
|
|
{
|
|
$this->db->select('user_id');
|
|
$this->db->where('user_id',$user_id);
|
|
$this->db->limit(1);
|
|
$query = $this->db->get('user');
|
|
if($query->num_rows() > 0)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
public function insert_access_token($tokens = array())
|
|
{
|
|
$this->db->query("INSERT INTO
|
|
user_access_token(subscriber, access_token, login_by, expiration)
|
|
VALUES(?, ?, ?, DATE_ADD(NOW(), INTERVAL 30 MINUTE))", $tokens);
|
|
|
|
return $this->db->insert_id();
|
|
}
|
|
public function update_access_token( $access_token = null )
|
|
{
|
|
$this->db->query("
|
|
UPDATE user_access_token
|
|
SET date_logout = NOW(), is_active = 0
|
|
WHERE access_token_id = ?", array($access_token));
|
|
return true;
|
|
}
|
|
public function get_user_data($user_id, $role_id)
|
|
{
|
|
$this->db->select('*');
|
|
$this->db->where('user_id', $user_id);
|
|
$this->db->where('role_id', $role_id);
|
|
$this->db->where('status', 1);
|
|
$this->db->limit(1);
|
|
$query = $this->db->get("user");
|
|
if($query->num_rows() == 1) {
|
|
return $query->row();
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function check_password($user_id, $old_password){
|
|
//check if old password is correct
|
|
/*$this->db->where("user_id", $user_id);
|
|
$this->db->where("password", sha1($old_password));
|
|
$this->db->where("status !=", 0);
|
|
$this->db->limit(1);
|
|
$check_pwd = $this->db->get("user")->num_rows();
|
|
return $check_pwd;*/
|
|
|
|
$this->db->where("user_id", $user_id);
|
|
$this->db->where("status !=", 0);
|
|
$this->db->limit(1);
|
|
$query = $this->db->get("user");
|
|
|
|
$row = $query->row();
|
|
|
|
/**
|
|
* Check if encrypted then use password_verify algo
|
|
*/
|
|
if($row->isPasswordReEncrypted > 0 && password_verify($old_password, $row->password)) {
|
|
return true;
|
|
} else {
|
|
/**
|
|
* Compare for old password
|
|
*/
|
|
if(sha1($old_password) !== $row->password){
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return false if No password match in record
|
|
*/
|
|
return false;
|
|
}
|
|
|
|
public function check_fullname($user_id, $role_id, $first_name, $last_name){
|
|
$this->db->where("user_id !=", $user_id);
|
|
$this->db->where("role_id", $role_id);
|
|
$this->db->where("first_name", $first_name);
|
|
$this->db->where("last_name", $last_name);
|
|
$this->db->limit(1);
|
|
return $this->db->get("user")->num_rows();
|
|
}
|
|
|
|
public function client_checkdb($email, $password)
|
|
{
|
|
$this->db->select("*");
|
|
$this->db->where("email_address", $email);
|
|
$this->db->where("password", sha1($password));
|
|
$this->db->where("status !=", 0);
|
|
if(isset($user_id) && !empty($user_id)){
|
|
$this->db->where("user_id !=", $user_id);
|
|
}
|
|
$this->db->limit(1);
|
|
$query = $this->db->get("user");
|
|
|
|
if($query->num_rows() == 1) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function check_email($email, $user_id=null, $user_type=array())
|
|
{
|
|
$this->db->select("email_address");
|
|
$this->db->where("email_address", $email);
|
|
$this->db->where("status !=", 0);
|
|
if(isset($user_id) && !empty($user_id)){
|
|
$this->db->where("user_id !=", $user_id);
|
|
}
|
|
if(sizeof($user_type) > 0){
|
|
$this->db->where_in("role_id", $user_type);
|
|
}
|
|
$this->db->limit(1);
|
|
$result = $this->db->get("user")->num_rows();
|
|
|
|
if($result > 0) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function identify_email($email, $user_role)
|
|
{
|
|
$this->db->select("role_id");
|
|
$this->db->where("email_address", $email);
|
|
$this->db->where("status !=", 0);
|
|
if(isset($user_role)) {
|
|
$this->db->where_in("role_id", $user_role);
|
|
}
|
|
$this->db->limit(1);
|
|
$result = $this->db->get("user");
|
|
|
|
if($result->num_rows() > 0){
|
|
$row = $result->row();
|
|
return $row->role_id;
|
|
}else{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public function get_my_email_forgot_pass($email)
|
|
{
|
|
$this->db->select("first_name");
|
|
$this->db->where("email_address", $email);
|
|
$result = $this->db->get("user");
|
|
|
|
if($result->num_rows() > 0){
|
|
$row = $result->row();
|
|
return $row->first_name;
|
|
}else{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public function check_username($username, $user_id=null)
|
|
{
|
|
$this->db->select("username");
|
|
$this->db->where("username", $username);
|
|
if(isset($user_id) && !empty($user_id)){
|
|
$this->db->where("user_id !=", $user_id);
|
|
}
|
|
$this->db->where("status !=", 0);
|
|
$this->db->limit(1);
|
|
$query = $this->db->get("user");
|
|
|
|
if($query->num_rows() == 1) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function check_if_something_is_changed($user_id, $data, $where_field_is)
|
|
{
|
|
$this->db->where("user_id", $user_id);
|
|
$this->db->where($where_field_is, $data);
|
|
$this->db->where("status !=", 0);
|
|
$this->db->limit(1);
|
|
$query = $this->db->get("user");
|
|
if($query->num_rows() == 1){
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function check_if_fullname_is_changed($user_id, $first_name, $last_name)
|
|
{
|
|
$this->db->where("user_id", $user_id);
|
|
$this->db->where("first_name", $first_name);
|
|
$this->db->where("last_name", $last_name);
|
|
$this->db->where("status !=", 0);
|
|
$this->db->limit(1);
|
|
$query = $this->db->get("user");
|
|
if($query->num_rows() == 1){
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function update_user_profile($sess_data, $data){
|
|
$update_data = array(
|
|
"first_name" => ucwords($data["first_name"]),
|
|
"last_name" => ucwords($data["last_name"]),
|
|
"email_address" => $data["email_address"]
|
|
);
|
|
|
|
//check for change in password
|
|
if($data["new_password"]!= "" && $data["confirm_new_password"] != ""){
|
|
//udpate password
|
|
$update_data["password"] = hash_password($data["confirm_new_password"]);
|
|
}
|
|
//check for change in username
|
|
if(!$this->check_if_something_is_changed($sess_data["user_id"], $data["username"], "username")){
|
|
//udpate username
|
|
$update_data["username"] = $data["username"];
|
|
}
|
|
|
|
$this->db->where("user_id", $sess_data["user_id"]);
|
|
$update = $this->db->update("user", $update_data);
|
|
|
|
return $update;
|
|
}
|
|
|
|
public function get_user_information_list($user_id, $role){
|
|
$result = $this->db->query("
|
|
SELECT
|
|
u.user_id,
|
|
u.first_name,
|
|
u.last_name,
|
|
u.username,
|
|
u.email_address,
|
|
ur.name as role_name,
|
|
ur.role_id as role,
|
|
u.status
|
|
FROM
|
|
user u
|
|
LEFT JOIN
|
|
user_role ur ON u.role_id = ur.role_id
|
|
WHERE
|
|
u.user_id != ?
|
|
AND u.status != 0
|
|
AND u.role_id IN(?)", array($user_id, implode("', '", $role)));
|
|
|
|
return $result->result();
|
|
}
|
|
|
|
public function get_user_information_by_user_id($user_id, $role){
|
|
$result = $this->db->query("
|
|
SELECT
|
|
u.user_id,
|
|
u.first_name,
|
|
u.last_name,
|
|
u.username,
|
|
u.email_address,
|
|
ur.name as role_name,
|
|
u.status
|
|
FROM
|
|
user u
|
|
LEFT JOIN
|
|
user_role ur ON u.role_id = ur.role_id
|
|
WHERE
|
|
u.user_id = ?
|
|
AND
|
|
u.role_id IN ?
|
|
LIMIT 1", array($user_id, $role));
|
|
|
|
return $result->row();
|
|
}
|
|
|
|
public function check_fullname_by_user_role($first_name, $last_name){
|
|
$this->db->where("role_id", BO_USER_ROLES);
|
|
$this->db->where("first_name", $first_name);
|
|
$this->db->where("last_name", $last_name);
|
|
$this->db->where("status !=", 0);
|
|
$this->db->limit(1);
|
|
return $this->db->get("user")->num_rows();
|
|
}
|
|
|
|
public function add_new_user($data){
|
|
|
|
$this->db->insert("user",
|
|
array(
|
|
"first_name" => ucwords($data["first_name"]),
|
|
"last_name" => ucwords($data["last_name"]),
|
|
"username" => $data["username"],
|
|
"email_address" => $data["email_address"],
|
|
"password" => hash_password($data["password"]),
|
|
"role_id" => $data["user_role"],
|
|
"status" => 1,
|
|
"iban" => $data["iban"],
|
|
"phone_number" => $data["telephone"]
|
|
));
|
|
return $this->db->insert_id();
|
|
}
|
|
|
|
public function add_new_user_admin($data) {
|
|
$this->db->insert("user",
|
|
array(
|
|
"first_name" => ucwords($data["first_name"]),
|
|
"last_name" => ucwords($data["last_name"]),
|
|
"username" => $data["username"],
|
|
"email_address" => $data["email_address"],
|
|
"password" => hash_password($data["password"]),
|
|
"role_id" => $data["user_role"],
|
|
"status" => 1,
|
|
"phone_number" => $data["telephone"]
|
|
));
|
|
return $this->db->insert_id();
|
|
}
|
|
|
|
public function create_new_user($data){
|
|
|
|
$this->db->insert("user",$data);
|
|
return $this->db->insert_id();
|
|
}
|
|
|
|
public function edit_user_account($user_id, $data) {
|
|
|
|
$update_data = array(
|
|
"first_name" => ucwords($data["first_name"]),
|
|
"last_name" => ucwords($data["last_name"]),
|
|
"role_id" => $data["user_role"],
|
|
"status" => 1,
|
|
"isPasswordReEncrypted" => 1
|
|
);
|
|
|
|
if(isset($data["username"]) && !empty($data["username"])){
|
|
$update_data["username"] = $data["username"];
|
|
}
|
|
if(isset($data["password"]) && !empty($data["confirm_password"])){
|
|
$update_data["password"] = hash_password($data["password"]);
|
|
}
|
|
if(isset($data["email_address"]) && !empty($data["email_address"])){
|
|
$update_data["email_address"] = $data["email_address"];
|
|
}
|
|
|
|
$this->db->where("user_id", $user_id);
|
|
$update = $this->db->update("user",$update_data);
|
|
return $update;
|
|
}
|
|
|
|
public function delete_subscribers( $user_id = null, $status = array() )
|
|
{
|
|
/**
|
|
* Reservation Sequence
|
|
*
|
|
* event_attendance [registration_id] //it could be a waitinglist or registration
|
|
* event_attendance_invitee [guest_id]
|
|
*
|
|
* user [user_id] - parent table (Last table to delete user from)
|
|
* event_attendance_noshow [user_id]
|
|
*
|
|
* event_email_recipient_other_detail [email_recipient_id] ----> registration and waitlist
|
|
* event_email_recipient pk:[email_recipient_id] fk:[reference_id]
|
|
*
|
|
* event_deregistration [susbcriber] [registration_id] --> event_registration
|
|
* event_registration [subscriber]
|
|
*
|
|
* event_subscriber_guest_deregistration [inviter_id] [guest_id] [event_schedule_id]
|
|
* event_subscriber_guest [inviter_id] [event_schedule_id]
|
|
*
|
|
* event_wait_list_deregistration [wait_list_subscriber] [wait_list_id] [event_schedule_id]
|
|
* event_wait_list [wait_list_subscriber] [event_schedule_id]
|
|
*
|
|
* event_concurrent_process [login_id] ---> user_login_history
|
|
* user_login_history [user_id] [history_id/login_id]
|
|
*
|
|
* user_subscriber_event_preference [subscriber_id]
|
|
* user_subscriber [subscriber]
|
|
* user_activity_log [user_id]
|
|
* user_access_token [subscriber]
|
|
*/
|
|
|
|
/**
|
|
* Delete user record from Table : user_subscriber_event_preference
|
|
*/
|
|
// $this->db->query("DELETE FROM user_subscriber_event_preference
|
|
// WHERE subscriber_id IN(SELECT us.subscriber_id FROM user_subscriber us WHERE us.subscriber = ? )
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_concurrent_process
|
|
// */
|
|
// $this->db->query("DELETE FROM event_concurrent_process
|
|
// WHERE login_id IN(SELECT ulh.history_id FROM user_login_history ulh WHERE ulh.user_id = ? )
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : user_activity_log
|
|
// */
|
|
// $this->db->where("user_id", $user_id );
|
|
// $this->db->delete("user_activity_log");
|
|
// /**
|
|
// * Delete record from Table : user_subscriber
|
|
// */
|
|
// $this->db->where("subscriber", $user_id );
|
|
// $this->db->delete("user_subscriber");
|
|
// /**
|
|
// * Delete record from Table : user_access_token
|
|
// */
|
|
// $this->db->where("subscriber", $user_id );
|
|
// $this->db->delete("user_access_token");
|
|
// /**
|
|
// * Delete record from Table : user_login_history
|
|
// */
|
|
// $this->db->where("user_id", $user_id );
|
|
// $this->db->delete("user_login_history");
|
|
// /**
|
|
// * Delete record from Table : event_attendance_noshow
|
|
// */
|
|
// // $this->db->where("user_id", $user_id );
|
|
// // $this->db->delete("event_attendance_noshow");
|
|
// /**
|
|
// * Delete record from Table : event_subscriber_guest_deregistration
|
|
// */
|
|
// $this->db->query("DELETE FROM event_subscriber_guest_deregistration
|
|
// WHERE guest_id IN(SELECT esg.guest_id FROM event_subscriber_guest esg WHERE esg.inviter_id = ? )
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_attendance_invitee
|
|
// */
|
|
// $this->db->query("DELETE FROM event_attendance_invitee
|
|
// WHERE guest_id IN(SELECT esg.guest_id FROM event_subscriber_guest esg WHERE esg.inviter_id = ? )
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_subscriber_guest
|
|
// */
|
|
// $this->db->where("inviter_id", $user_id );
|
|
// $this->db->delete("event_subscriber_guest");
|
|
// /**
|
|
// * Delete record from Table : event_wait_list_deregistration
|
|
// */
|
|
// $this->db->query("DELETE FROM event_wait_list_deregistration
|
|
// WHERE wait_list_id IN(SELECT ewl.wait_list_id FROM event_wait_list ewl WHERE ewl.wait_list_subscriber = ? )
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_deregistration
|
|
// */
|
|
// $this->db->query("DELETE FROM event_deregistration
|
|
// WHERE registration_id IN(SELECT er.registration_id FROM event_registration er WHERE er.subscriber = ? )
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_attendance using event_registration
|
|
// */
|
|
// $this->db->query("DELETE FROM event_attendance
|
|
// WHERE registration_id IN(SELECT ers.registration_id FROM event_registration ers WHERE ers.subscriber = ?)
|
|
// AND attendance_type = 0
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_attendance using event_wait_list
|
|
// */
|
|
// $this->db->query("DELETE FROM event_attendance
|
|
// WHERE registration_id IN(SELECT ewl.wait_list_id FROM event_wait_list ewl WHERE ewl.wait_list_subscriber = ?)
|
|
// AND attendance_type = 1
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_email_recipient_other_detail using event_registration
|
|
// */
|
|
// $this->db->query("DELETE eerod
|
|
// FROM event_email_recipient_other_detail as eerod, event_email_recipient as eer
|
|
// WHERE eer.email_recipient_id = eerod.email_recipient_id
|
|
// AND eer.reference_id IN(SELECT ers.registration_id FROM event_registration ers WHERE ers.subscriber = ?)
|
|
// AND eer.email_type_id = 3
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_email_recipient_other_detail using event_wait_list
|
|
// */
|
|
// $this->db->query("DELETE eerod
|
|
// FROM event_email_recipient_other_detail as eerod, event_email_recipient as eer
|
|
// WHERE eer.email_recipient_id = eerod.email_recipient_id
|
|
// AND eer.reference_id IN(SELECT ewl.wait_list_id FROM event_wait_list as ewl WHERE ewl.wait_list_subscriber = ?)
|
|
// AND eer.email_type_id = 6
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_email_recipient using event_registration
|
|
// */
|
|
// $this->db->query("DELETE FROM event_email_recipient
|
|
// WHERE reference_id IN(SELECT ers.registration_id FROM event_registration as ers WHERE ers.subscriber = ?)
|
|
// AND email_type_id = 3
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_email_recipient using event_wait_list
|
|
// */
|
|
// $this->db->query("DELETE FROM event_email_recipient
|
|
// WHERE reference_id IN(SELECT ewl.wait_list_id FROM event_wait_list as ewl WHERE ewl.wait_list_subscriber = ?)
|
|
// AND email_type_id = 6
|
|
// ", array($user_id));
|
|
// /**
|
|
// * Delete record from Table : event_registration
|
|
// */
|
|
// $this->db->where("subscriber", $user_id);
|
|
// $this->db->delete("event_registration");
|
|
// /**
|
|
// * Delete record from Table : event_wait_list
|
|
// */
|
|
// $this->db->where("wait_list_subscriber", $user_id);
|
|
// $this->db->delete("event_wait_list");
|
|
// /**
|
|
// * Delete record from Table : user
|
|
// */
|
|
// $this->db->where("user_id", $user_id);
|
|
// $this->db->delete("user");
|
|
|
|
/**
|
|
* Update user info to Unknown
|
|
*/
|
|
|
|
$this->db->where("user_id", $user_id );
|
|
$this->db->update("user" , array(
|
|
"status" => 0,
|
|
"first_name" => "Unknown",
|
|
// "salutation" => "Unknown",
|
|
"last_name" => "Unknown",
|
|
"email_address" => "Unknown",
|
|
"username" => null,
|
|
"password" => null,
|
|
) );
|
|
|
|
$this->db->where("subscriber", $user_id );
|
|
$this->db->update("user_subscriber" , array(
|
|
"address" => null,
|
|
//"additional_address" => null,
|
|
"email_address" => null,
|
|
"civility" => null,
|
|
"birthday" => null,
|
|
"postal_code" => null,
|
|
"country" => null,
|
|
"city" => null,
|
|
"phone" => null,
|
|
//"mobile" => null,
|
|
//"subscription_type" => 0,
|
|
//"isModerationDisabled" => 0,
|
|
//"notify_for_moderation_feature" => 0
|
|
) );
|
|
|
|
return $this->db->affected_rows();
|
|
}
|
|
|
|
public function deactivate_activate_user_account($user_id, $action){
|
|
$this->db->where("user_id", $user_id);
|
|
$deactivate_activate = $this->db->update("user", array("status" => $action));
|
|
return $deactivate_activate;
|
|
}
|
|
|
|
public function delete_user_account($user_id){
|
|
$this->db->where("user_id", $user_id);
|
|
$deleted = $this->db->update("user", array("status" => 0));
|
|
return $deleted;
|
|
}
|
|
|
|
public function new_password($email, $password, $user_role=array(3))
|
|
{
|
|
$entry = array('password' => hash_password($password));
|
|
$this->db->where('email_address', $email);
|
|
$this->db->where_in('role_id', $user_role);
|
|
$this->db->update('user', $entry);
|
|
}
|
|
|
|
public function create_client_account($first, $last, $email, $username, $password)
|
|
{
|
|
$this->db->insert('user',
|
|
array(
|
|
'first_name' => ucwords($first),
|
|
'last_name' => ucwords($last),
|
|
'email_address' => $email,
|
|
'role_id' => 3,
|
|
'username' => $username,
|
|
'password' => hash_password($password)
|
|
)
|
|
);
|
|
return $this->db->insert_id();
|
|
}
|
|
|
|
public function check_if_session_is_active($logged_in) {
|
|
|
|
//check if login_id/session has been logged out/expired already
|
|
$this->db->where("user_id", $logged_in["user_id"]);
|
|
$this->db->where("description", "LOGOUT");
|
|
$this->db->where("login_id", $logged_in["login_id"]);
|
|
$this->db->where("login_id IS NOT NULL");
|
|
$this->db->where("status",1);
|
|
$this->db->order_by('history_id', 'desc');
|
|
$check_logout_session = $this->db->get("user_login_history")->num_rows();
|
|
|
|
if($check_logout_session > 0){
|
|
//session is logged out
|
|
return false;
|
|
}else {
|
|
//check if login is active
|
|
if(!isset($logged_in["token_id"]) && empty($logged_in["token_id"])){
|
|
$this->db->flush_cache();
|
|
$this->db->where("user_id", $logged_in["user_id"]);
|
|
$this->db->where("description", "LOGIN");
|
|
$this->db->where("history_id", $logged_in["login_id"]);
|
|
$this->db->where("status",1);
|
|
$this->db->order_by('history_id', 'desc');
|
|
$this->db->limit(1);
|
|
if($this->db->get("user_login_history")->num_rows() > 0){
|
|
$this->db->flush_cache();
|
|
$this->db->query("UPDATE user_login_history
|
|
SET date_time = NOW()
|
|
WHERE user_id = ?
|
|
AND description = ?
|
|
AND status = ?
|
|
AND history_id = ?
|
|
", array($logged_in["user_id"], 'LOGIN', 1, $logged_in["login_id"]));
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} else {
|
|
//if account is used with access token
|
|
if(isset($logged_in["token_id"]) && !empty($logged_in["token_id"])){
|
|
$this->db->flush_cache();
|
|
$this->db->where("subscriber", $logged_in["user_id"]);
|
|
$this->db->where("access_token_id", $logged_in["token_id"]);
|
|
$this->db->where("is_active",0);
|
|
$this->db->order_by('date_login', 'desc');
|
|
$this->db->limit(1);
|
|
//if access token already expired
|
|
return $this->db->get("user_access_token")->num_rows() ? false : true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function get_user_fullname($user_id)
|
|
{
|
|
$query = $this->db->select('first_name, last_name')
|
|
->from('user')
|
|
->where('user_id', $user_id)
|
|
->limit(1)
|
|
->get()
|
|
->row();
|
|
return $query;
|
|
}
|
|
|
|
public function user_details($user_id)
|
|
{
|
|
$query = $this->db->select('*')
|
|
->from('user')
|
|
->where('user_id', $user_id)
|
|
->limit(1)
|
|
->get()
|
|
->row();
|
|
return $query;
|
|
}
|
|
|
|
public function verify_remember_me(int $user_id, int $role_id, string $remToken = null, bool $isBOUser=true)
|
|
{
|
|
$user = $isBOUser ? "username" : "email_address";
|
|
return $this->db->select($user.', user_id, role_id, rememberMeToken')
|
|
->from('user')
|
|
->where('user_id', $user_id)
|
|
->where('role_id', $role_id)
|
|
->where('rememberMeToken', $remToken)
|
|
->get()
|
|
->row();
|
|
}
|
|
|
|
public function update_remember_me(int $user_id, int $role_id, string $remToken = null)
|
|
{
|
|
$this->db->where("user_id", $user_id);
|
|
$this->db->where("role_id", $role_id);
|
|
$this->db->update("user", array("rememberMeToken" => $remToken));
|
|
}
|
|
|
|
public function get_subscribers($s_id = null)
|
|
{
|
|
$this->db->select('
|
|
user.user_id,
|
|
user.status,
|
|
user.email_address,
|
|
user.first_name,
|
|
user.last_name,
|
|
user_subscriber.civility,
|
|
user_subscriber.phone,
|
|
user_subscriber.address,
|
|
user_subscriber.postal_code,
|
|
user_subscriber.city
|
|
')
|
|
->from('user')
|
|
->where('subscriber', $s_id)
|
|
->join('user_subscriber', 'user.user_id = user_subscriber.subscriber');
|
|
|
|
$result = $this->db->get();
|
|
if($result->num_rows() > 0){
|
|
return $result->row();
|
|
}
|
|
}
|
|
public function put_subscribers($userId, $user)
|
|
{
|
|
$this->db->where("user_id", $userId );
|
|
$this->db->update("user" , array(
|
|
'first_name' => $user["first_name"],
|
|
'last_name' => $user["last_name"],
|
|
// 'email_address' => $this->input->post("email_address")
|
|
));
|
|
|
|
$this->db->where("subscriber", $userId );
|
|
$this->db->update("user_subscriber" , array(
|
|
'civility' => $user["civility"],
|
|
'address' => $user["address"],
|
|
'postal_code' => $user["postal_code"],
|
|
'city' => $user["city"],
|
|
'phone' => $user["phone"]
|
|
));
|
|
}
|
|
|
|
public function logout_users(){
|
|
$this->db->query("INSERT INTO user_login_history(session_id, user_id, description, login_id, user_agent, platform, status) SELECT '".$this->db->escape_str(session_id())."', ulh.user_id, 'LOGOUT', ulh.history_id, 'cron', 'server', 1
|
|
FROM user_login_history ulh
|
|
LEFT JOIN user u ON u.user_id = ulh.user_id AND u.role_id = 3
|
|
WHERE timestampdiff(MINUTE, ulh.date_time, NOW()) > 29
|
|
AND u.user_id = ulh.user_id
|
|
AND u.role_id = 3
|
|
AND ulh.stay_connected = 0
|
|
AND ulh.access_token_id IS NULL
|
|
AND ulh.description = 'LOGIN'
|
|
AND ulh.status =1");
|
|
$this->db->flush_cache();
|
|
//then, update their statuses
|
|
$this->db->query("UPDATE user_login_history ulh
|
|
LEFT JOIN user u ON u.user_id = ulh.user_id AND u.role_id = 3
|
|
SET ulh.status = 0
|
|
WHERE timestampdiff(MINUTE, ulh.date_time, NOW()) > 29
|
|
AND u.user_id = ulh.user_id
|
|
AND u.role_id = 3
|
|
AND ulh.stay_connected=0
|
|
AND ulh.access_token_id IS NULL
|
|
AND ulh.description = 'LOGIN'
|
|
AND ulh.status =1"); //logout users
|
|
|
|
$updated_rows = $this->db->affected_rows();
|
|
if($updated_rows > 0){
|
|
return $updated_rows;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function access_token_has_expired(){
|
|
$this->db->query("INSERT INTO user_login_history(session_id, user_id, description, login_id, user_agent, platform, status, access_token_id) SELECT '".$this->db->escape_str(session_id())."', ulh.user_id, 'LOGOUT', ulh.history_id, 'cron', 'server', 1, ulh.access_token_id
|
|
FROM user_login_history ulh
|
|
LEFT JOIN user_access_token uat
|
|
ON uat.access_token_id = ulh.access_token_id
|
|
WHERE uat.expiration < NOW()
|
|
AND uat.is_active = 1
|
|
AND ulh.access_token_id IS NOT NULL
|
|
AND ulh.user_id = uat.subscriber
|
|
AND ulh.description = 'LOGIN'
|
|
AND ulh.status =1");
|
|
|
|
$this->db->flush_cache();
|
|
//then, update their statuses
|
|
$this->db->query("UPDATE user_login_history ulh
|
|
LEFT JOIN user_access_token uat
|
|
ON uat.access_token_id = ulh.access_token_id
|
|
SET ulh.status = 0
|
|
WHERE uat.expiration < NOW()
|
|
AND uat.is_active = 1
|
|
AND ulh.access_token_id IS NOT NULL
|
|
AND ulh.user_id = uat.subscriber
|
|
AND ulh.description = 'LOGIN'
|
|
AND ulh.status =1"); //logout users
|
|
|
|
$this->db->flush_cache();
|
|
//then, update access token logout
|
|
$this->db->query("UPDATE user_access_token
|
|
SET is_active = 0, date_logout = NOW()
|
|
WHERE expiration < NOW()
|
|
AND is_active = 1"); //logout users
|
|
$updated_rows = $this->db->affected_rows();
|
|
if($updated_rows > 0){
|
|
return $updated_rows;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function get_user_by_email_or_magento_id($email, $magento_id, $mage_id = null) {
|
|
$this->db->select('user_id');
|
|
$this->db->from('user');
|
|
$this->db->where('email_address', $email);
|
|
$this->db->where('status', 1);
|
|
$this->db->where('role_id', 3);
|
|
$this->db->limit('1');
|
|
$this->db->where('magento_id', $magento_id);
|
|
if ($mage_id)
|
|
$this->db->where('mage_id', $mage_id);
|
|
return $this->db->get()->row();
|
|
}
|
|
|
|
public function update_user_magento_info($user_id = null, $sso = null ) {
|
|
if ($user_id && isset($sso['magento_id'])) {
|
|
$this->db->where("user_id", $user_id );
|
|
$this->db->update("user" , ['magento_id' => $sso['magento_id'], 'mage_id' => isset($sso['mage_id']) ? $sso['mage_id'] : null]);
|
|
}
|
|
}
|
|
|
|
public function get_user_by_user_id($user_id = null) {
|
|
$this->db->select('magento_id, mage_id');
|
|
$this->db->from('user');
|
|
$this->db->where('user_id', $user_id);
|
|
$this->db->where('magento_id', null);
|
|
$this->db->where('mage_id', null);
|
|
$this->db->where('status', 1);
|
|
$this->db->where('role_id', 3);
|
|
$this->db->limit('1');
|
|
return $this->db->get()->row();
|
|
}
|
|
}
|
|
|