Subversion Repositories cheapmusic

Rev

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