Subversion Repositories cheapmusic

Rev

Rev 31 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
25 - 1
<?php
2
/*
3
 * User Class
4
 * This class is used for database related (connect fetch, insert, and update) operations
5
 * @author    CodexWorld.com
6
 * @url       http://www.codexworld.com
7
 * @license   http://www.codexworld.com/license
8
 */
9
class User{
10
    private $dbHost     = DB_HOST;
11
    private $dbUsername = DB_USERNAME;
12
    private $dbPassword = DB_PASSWORD;
13
    private $dbName     = DB_NAME;
14
    private $userTbl    = DB_USER_TBL;
15
 
16
    public function __construct(){
17
        if(!isset($this->db)){
18
            // Connect to the database
19
            $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
20
            if($conn->connect_error){
21
                die("Failed to connect with MySQL: " . $conn->connect_error);
22
            }else{
23
                $this->db = $conn;
24
            }
25
        }
26
    }
27
 
28
    /*
29
     * Returns rows from the database based on the conditions
30
     * @param array select, where, order_by, limit and return_type conditions
31
     */
32
    public function getRows($conditions = array()){
33
        $sql = 'SELECT ';
34
        $sql .= array_key_exists("select",$conditions)?$conditions['select']:'*';
35
        $sql .= ' FROM '.$this->userTbl;
36
        if(array_key_exists("where",$conditions)){
37
            $sql .= ' WHERE ';
38
            $i = 0;
39
            foreach($conditions['where'] as $key => $value){
40
                $pre = ($i > 0)?' AND ':'';
41
                $sql .= $pre.$key." = '".$value."'";
42
                $i++;
43
            }
44
        }
45
 
46
        if(array_key_exists("where_not",$conditions)){
47
            $sql .= (strpos($sql, 'WHERE') === false)?' WHERE ':' AND ';
48
            $i = 0;
49
            foreach($conditions['where_not'] as $key => $value){
50
                $pre = ($i > 0)?' AND ':'';
51
                $sql .= $pre.$key." != '".$value."'";
52
                $i++;
53
            }
54
        }
55
 
56
        if(array_key_exists("order_by",$conditions)){
57
            $sql .= ' ORDER BY '.$conditions['order_by'];
58
        }
59
 
60
        if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
61
            $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit'];
62
        }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
63
            $sql .= ' LIMIT '.$conditions['limit'];
64
        }
65
 
66
        $result = $this->db->query($sql);
67
 
68
        if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
69
            switch($conditions['return_type']){
70
                case 'count':
71
                    $data = $result->num_rows;
72
                    break;
73
                case 'single':
74
                    $data = ($result->num_rows > 0)?$result->fetch_assoc():false;
75
                    break;
76
                default:
77
                    $data = '';
78
            }
79
        }else{
80
            if($result->num_rows > 0){
81
                while($row = $result->fetch_assoc()){
82
                    $data[] = $row;
83
                }
84
            }
85
        }
86
        return !empty($data)?$data:false;
87
    }
88
 
89
    /*
90
     * Insert data into the database
91
     * @param array the data for inserting into the table
92
     */
93
    public function insert($data){
94
        if(!empty($data) && is_array($data)){
95
            $columns = '';
96
            $values  = '';
97
            $i = 0;
98
            if(!array_key_exists('created',$data)){
99
                $data['created'] = date("Y-m-d H:i:s");
100
            }
101
            if(!array_key_exists('modified',$data)){
102
                $data['modified'] = date("Y-m-d H:i:s");
103
            }
104
            foreach($data as $key=>$val){
105
                $pre = ($i > 0)?', ':'';
106
                $columns .= $pre.$key;
107
                $values  .= $pre."'".$this->db->real_escape_string($val)."'";
108
                $i++;
109
            }
110
            $query = "INSERT INTO ".$this->userTbl." (".$columns.") VALUES (".$values.")";
111
            $insert = $this->db->query($query);
112
            return $insert?$this->db->insert_id:false;
113
        }else{
114
            return false;
115
        }
116
    }
117
 
118
    /*
119
     * Update data into the database
120
     * @param array the data to update into the table
121
     * @param array where condition on updating data
122
     */
123
    public function update($data, $conditions){
124
        if(!empty($data) && is_array($data) && !empty($conditions)){
125
            //prepare columns and values sql
126
            $cols_vals = '';
127
            $i = 0;
128
            if(!array_key_exists('modified',$data)){
129
                $data['modified'] = date("Y-m-d H:i:s");
130
            }
131
            foreach($data as $key=>$val){
132
                $pre = ($i > 0)?', ':'';
133
                $cols_vals .= $pre.$key." = '".$this->db->real_escape_string($val)."'";
134
                $i++;
135
            }
136
 
137
            //prepare where conditions
138
            $whereSql = '';
139
            $ci = 0;
140
            foreach($conditions as $key => $value){
141
                $pre = ($ci > 0)?' AND ':'';
142
                $whereSql .= $pre.$key." = '".$value."'";
143
                $ci++;
144
            }
145
 
146
            //prepare sql query
147
            $query = "UPDATE ".$this->userTbl." SET ".$cols_vals." WHERE ".$whereSql;
148
 
149
            //update data
150
            $update = $this->db->query($query);
151
            return $update?true:false;
152
        }else{
153
            return false;
154
        }
155
    }
156
 
157
	/*
158
     * Insert / Update social user data into the database
159
     * @param array the data to insert or update into the table
160
     */
161
	function checkUser($userData = array()){
162
		if(!empty($userData)){
163
			// Check whether user data already exists in database with same oauth info
164
			$prevQuery = "SELECT * FROM ".$this->userTbl." WHERE oauth_provider = '".$userData['oauth_provider']."' AND oauth_uid = '".$userData['oauth_uid']."'";
165
			$prevResult = $this->db->query($prevQuery);
166
 
167
			// Check whether user data already exists in database with same email
168
			$prevQuery2 = "SELECT * FROM ".$this->userTbl." WHERE email != '' AND email = '".$userData['email']."'";
169
			$prevResult2 = $this->db->query($prevQuery2);
170
 
171
			if($prevResult->num_rows > 0){
172
                $cols_vals = '';
173
                $i = 0;
174
				// Update user data if already exists
175
                if(!array_key_exists('modified',$userData)){
176
                    $userData['modified'] = date("Y-m-d H:i:s");
177
                }
178
                foreach($userData as $key=>$val){
179
                    $pre = ($i > 0)?', ':'';
180
                    $cols_vals .= $pre.$key." = '".$this->db->real_escape_string($val)."'";
181
                    $i++;
182
                }
183
                //prepare sql query
184
                $query = "UPDATE ".$this->userTbl." SET ".$cols_vals." WHERE oauth_provider = '".$userData['oauth_provider']."' AND oauth_uid = '".$userData['oauth_uid']."'";
185
 
186
                //update data
187
                $update = $this->db->query($query);
188
			}elseif($prevResult2->num_rows > 0){
189
				// Update user data if already exists
190
                if(!array_key_exists('modified',$userData)){
191
                    $userData['modified'] = date("Y-m-d H:i:s");
192
                }
193
 
194
                //prepare sql query
195
                $query = "UPDATE ".$this->userTbl." SET oauth_provider = '".$userData['oauth_provider']."', oauth_uid = '".$userData['oauth_uid']."', modified = '".$userData['modified']."' WHERE email = '".$userData['email']."'";
196
 
197
                //update data
198
                $update = $this->db->query($query);
199
			}else{
200
                $columns = '';
201
                $values  = '';
202
                $i = 0;
203
				// Insert user data
204
                $userData['activated'] = '1';
205
                $userData['status'] = '1';
206
                if(!array_key_exists('created',$userData)){
207
                    $userData['created'] = date("Y-m-d H:i:s");
208
                }
209
                if(!array_key_exists('modified',$userData)){
210
                    $userData['modified'] = date("Y-m-d H:i:s");
211
                }
212
                foreach($userData as $key=>$val){
213
                    $pre = ($i > 0)?', ':'';
214
                    $columns .= $pre.$key;
215
                    $values  .= $pre."'".$this->db->real_escape_string($val)."'";
216
                    $i++;
217
                }
218
                $query = "INSERT INTO ".$this->userTbl." (".$columns.") VALUES (".$values.")";
219
                $insert = $this->db->query($query);
220
			}
221
 
222
			// Get user data from the database
223
			$result = $this->db->query($prevQuery);
224
			$userData = $result->fetch_assoc();
225
		}
226
 
227
		// Return user data
228
		return $userData;
229
	}
230
 
231
}