Skip to content

Instantly share code, notes, and snippets.

@mcabreradev
Created May 6, 2016 00:16
Show Gist options
  • Save mcabreradev/e9874bd015506caeffa2119f781de3ea to your computer and use it in GitHub Desktop.
Save mcabreradev/e9874bd015506caeffa2119f781de3ea to your computer and use it in GitHub Desktop.
<?php
class Users_model extends MY_Model {
protected $table = "users";
protected $primary_key = "user_id";
protected $fields = array("location", "first_name", "last_name", "email", "gender", "facebook_id", "password", "password_hash", "deleted", "created_at", "modified_at", "university_id", "reset_token", "reset_token_expires");
public $per_page = 9;
public $per_first_page = 15;
public $before_create = array('before_create');
public $before_update = array('before_update');
public function __construct() {
parent::__construct();
$this->validate = array();
$this->validate_rules_by_controller_method = array(
'create' => array(
array('field' => 'location', 'label' => 'Reg Step', 'rules' => 'trim|xss_clean')
, array('field' => 'first_name', 'label' => 'First Name', 'rules' => 'trim|required|xss_clean')
, array('field' => 'last_name', 'label' => 'Last Name', 'rules' => 'trim|required|xss_clean')
, array('field' => 'email', 'label' => 'Email address', 'rules' => 'trim|required|max_length[255]|valid_email|is_unique[users.email]|xss_clean')
, array('field' => 'university_id', 'label' => 'School', 'rules' => 'trim|required|xss_clean')
, array('field' => 'gender', 'label' => 'Gender', 'rules' => 'trim|required|xss_clean')
, array('field' => 'birthday', 'label' => 'Birthday', 'rules' => 'required|xss_clean')
, array('field' => 'agree_with_terms', 'label' => 'Agree with Terms', 'rules' => 'trim|required|xss_clean')
, array('field' => 'facebook_id', 'label' => 'Facebook ID', 'rules' => 'xss_clean')
, array('field' => 'password', 'label' => 'Password', 'rules' => 'trim|required|min_length[1]|max_length[255]|xss_clean')
, array('field' => 'confirm_password', 'label' => 'Confirmation Password', 'rules' => 'trim|required|min_length[1]|max_length[255]|matches[password]|xss_clean')
)
, 'generate_reset_token' => array(
array('field' => 'email', 'label' => 'Email', 'rules' => 'trim|required|max_length[255]|valid_email|xss_clean')
)
, 'reset_password' => array(
array('field' => 'password', 'label' => 'Password', 'rules' => 'trim|required|min_length[1]|max_length[255]|xss_clean')
, array('field' => 'confirm_password', 'label' => 'Confirmation', 'rules' => 'trim|required|min_length[1]|max_length[255]|matches[password]|xss_clean')
, array('field' => 'reset_token', 'label' => 'Reset Token', 'rules' => 'trim|exact_length[32]|xss_clean')
)
);
if ( isset($this->validate_rules_by_controller_method[$this->router->method]) ) {
$this->validate = $this->validate_rules_by_controller_method[$this->router->method];
}
}
function before_create($user) {
$user['created_at'] = date('Y-m-d H:i:s');
if ( isset($user['password']) AND isset($user['confirm_password']) ) {
$user['password_hash'] = md5(microtime());
$user['password'] = $this->get_hashed_password($user['password'], $user['password_hash']);
$user['confirm_password'] = $this->get_hashed_password($user['confirm_password'], $user['password_hash']);
}
unset($user['birthday'], $user['agree_with_terms'],$user['university_id']);
return $user;
}
function before_update($user) {
if ( isset($user['password']) AND isset($user['confirm_password']) ) {
$user['password_hash'] = md5(microtime());
$user['confirm_password'] = $this->get_hashed_password($user['confirm_password'], $user['password_hash']);
$user['password'] = $this->get_hashed_password($user['password'], $user['password_hash']);
}
if (isset($user['birthday']['year']) && isset($user['birthday']['month']) && isset($user['birthday']['day'])) {
$date = $user['birthday']['year'].'-'.$user['birthday']['month'].'-'.$user['birthday']['day'];
$user['birthday'] = $date;
} else {
$user['birthday'] = null;
}
$user['modified_at'] = date('Y-m-d H:i:s');
return $user;
}
function search($query) {
$this->db->select("first_name,last_name,user_id");
$this->db->like('first_name', $query);
$this->db->or_like('last_name', $query);
$this->db->order_by('first_name', 'asc');
return $this->db->get($this->table)->result();
}
function get_hashed_password($password, $hash) {
return md5($password . $this->config->item('user.encryption_key') . $hash);
}
function check_password($password, $user) {
$hash_to_check = $this->get_hashed_password($password, $user->password_hash);
if( $hash_to_check !== $user->password ){
return FALSE;
}
return TRUE;
}
public function filter($page = 1, $search = false, $university_id = false, $major_id = false, $year = false, $group_id = false, $where = array()) {
if ($university_id or $major_id or $year) {
$this->db->join('user_schools us', 'us.user_id = u.user_id');
}
if ($university_id) {
$this->db->join('universities un', 'un.university_id = us.university_id')
->where('un.university_id', $university_id);
}
$year and $this->db->where('us.user_school_current_year_name_id', $year);
$major_id and $this->db->join('user_schools_majors usm', 'usm.user_school_id = us.user_school_id')
->where('usm.user_school_major_id', $major_id);
$group_id and $this->db->join('group_users gu', 'gu.user_id = u.user_id')
->where('gu.group_id', $group_id);
$search and $this->db->like('CONCAT_WS(" ", u.first_name, u.last_name )', $search);
$per_page = $page > 1 ? $this->per_page : $this->per_first_page;
$offset = $page > 1 ? $this->per_first_page + $this->per_page * ($page - 2) : 0;
$this->db->select('u.user_id, u.first_name, u.last_name, u.gender')
->limit($per_page)
->offset($offset)
->group_by('u.user_id')
->order_by('u.created_at', 'DESC');
$result = $this->db->get_where($this->table.' AS u', array('u.active' => true, 'u.deleted' => false))->result();
return $result;
}
public function random($disabled_ids = array()) {
$per_page = count($disabled_ids) > 1 ? $this->per_page : $this->per_first_page;
$this->db->select('user_id, first_name, last_name, gender')
->limit($per_page)
->where_not_in('user_id', $disabled_ids)
->order_by('RAND()');
$result = $this->db->get_where($this->table, array('active' => true, 'deleted' => false))->result();
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment