Subversion Repositories cheapmusic

Rev

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