Completed
Push — master ( 056f30...ecabd3 )
by Angus
02:46
created

User_Model::hideLatestNotice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 17
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class User_Model extends CI_Model {
4
	public $id;
5
	public $username;
6
	public $email;
7
8 115
	public function __construct() {
9 115
		parent::__construct();
10
11
		//CHECK: Should this be placed elsewhere?
12 115
		if($this->logged_in()) {
13 3
			if(!$this->session->userdata('username')) {
14 3
				$this->session->set_userdata('username', $this->ion_auth->user()->row()->username);
15
			}
16 3
			if(!$this->session->userdata('email')) {
17
				//CHECK: This seems like a bad idea?
18 3
				$this->session->set_userdata('email', $this->ion_auth->user()->row()->email);
19
			}
20
		}
21
22 115
		$this->id       = (int) $this->ion_auth->get_user_id();
23 115
		$this->username = $this->session->userdata('username');
24 115
		$this->email    = $this->session->userdata('email');
25 115
	}
26
27 115
	public function logged_in() : bool {
28 115
		return $this->ion_auth->logged_in();
29
	}
30
31 9
	public function login_redirect() {
32 9
		if(!strpos(current_url(), '/import_list') && !strpos(current_url(), '/export_list')) {
33 7
			$this->session->set_flashdata('referred_from', current_url());
34
		}
35
		//FIXME: We should handle the redirect here too, but it causes issues with tests
36
		//redirect('user/login');
37 9
	}
38
39 2
	public function username_exists(string $username) : bool {
40 2
		$this->load->database();
41
42 2
		$query = $this->db->select('*')
43 2
		                  ->from('auth_users')
44 2
		                  ->where('username', $username)
45 2
		                  ->get();
46
47 2
		return (bool) $query->num_rows();
48
	}
49
50
	/**
51
	 * @param $identity
52
	 *
53
	 * @return mixed
54
	 */
55 4 View Code Duplication
	public function find_email_from_identity(string $identity) {
56
		//login allows using email or username, but ion_auth doesn't support this
57
		//check if identity is email, and if not, try and find it
58
		//returns: email or FALSE
59
		//CHECK: How should we handle invalid emails being passed to this?
60 4
		$email = $identity;
61
62 4
		if(!strpos($identity, '@')) {
63
			//identity does not contain @, assume username
64 2
			$this->load->database();
65
66 2
			$query = $this->db->select('email')
67 2
			                  ->from('auth_users')
68 2
			                  ->where('username', $identity)
69 2
			                  ->get();
70
71 2
			if($query->num_rows() > 0) {
72
				//username exists, grab email
73 1
				$email = $query->row('email');
74
			}else{
75
				//username doesn't exist, return FALSE
76 1
				$email = FALSE;
77
			}
78
		}
79
80 4
		return $email;
81
	}
82
83 2 View Code Duplication
	public function get_user_by_username(string $username) {
84 2
		$user = NULL;
85
86 2
		$query = $this->db->select('*')
87 2
		                  ->from('auth_users')
88 2
		                  ->where('username', $username)
89 2
		                  ->get();
90
91 2
		if($query->num_rows() > 0) {
92 1
			$user = $query->row();
93
		}
94 2
		return $user;
95
	}
96
97 1
	public function get_gravatar_url($email = NULL, $size = NULL) : string {
98 1
		$email = $email ?? $this->email;
99
		//TODO: FIXME ON PROFILE PAGES
100 1
		return $this->gravatar->get($email, $size);
101
	}
102
103
	public function get_new_api_key() : string {
104
		$api_key = NULL;
105
		if($this->logged_in()) {
106
			$api_key = substr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", mt_rand(0, 51), 1) . substr(md5((string) time()), 1);
107
108
			$this->db->where('id', $this->id);
109
			$this->db->update('auth_users', ['api_key' => $api_key]);
110
		}
111
112
		return $api_key;
113
	}
114
	
115 View Code Duplication
	public function get_id_from_api_key(string $api_key) {
116
		$query = $this->db->select('id')
117
		                  ->from('auth_users')
118
		                  ->where('api_key', $api_key)
119
		                  ->get();
120
121
		if($query->num_rows() > 0) {
122
			$userID = $query->row('id');
123
		}
124
125
		return $userID ?? FALSE;
126
	}
127
128
	/** NOTICES **/
129
130
	public function getLatestNotice() {
131
		$query = $this->db
132
			->select('tn.notice, tn.created_at')
133
			->from('tracker_notices AS tn')
134
			->where("id > IFNULL((SELECT hidden_notice_id FROM tracker_user_notices WHERE user_id = {$this->User->id}), '0')", NULL, FALSE)
135
			->order_by('tn.id DESC')
136
			->limit(1)
137
			->get();
138
139
		$noticeData = [];
140
		if($query->num_rows() > 0) {
141
			$row = $query->row();
142
143
			$noticeData = [
144
				'date' => str_replace('-', '/', substr($row->created_at, 0, 10)),
145
				'text' => str_replace('|', '<br/>', htmlspecialchars($row->notice))
146
			];
147
		}
148
149
		return $noticeData;
150
	}
151
	public function hideLatestNotice() {
152
		$idQuery = $this->db->select('1')
153
		                    ->where('user_id', $this->User->id)
154
		                    ->get('tracker_user_notices');
155
		if($idQuery->num_rows() > 0) {
156
			$success = (bool) $this->db->set('hidden_notice_id', '(SELECT id FROM tracker_notices ORDER BY id DESC LIMIT 1)', FALSE)
157
			                           ->where('user_id', $this->User->id)
158
			                           ->update('tracker_user_notices');
159
		} else {
160
			$success = (bool) $this->db->insert('tracker_user_notices', [
161
				'user_id'           => $this->User->id,
162
				'hidden_notice_id'  => '(SELECT id FROM tracker_notices ORDER BY id DESC LIMIT 1)'
163
			], FALSE);
164
		}
165
166
		return $success;
167
	}
168
}
169