/var/www/onesupportsys.onesolution.hk/profile/modify.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
global $dbh;

require_once(
'../inc/configure.php');

//Ajax Function
if(!empty($_POST["actionCode"])){
    
//Get a saved record from customerProfileOtherInfo 
    
if ( $_POST["actionCode"] == "R" ) {
        
$sql "select * from customerProfileOtherInfo where profile_id = ?";
        
$result tableInfo::recordData($sql$_POST["profileId"]);
        echo 
json_encode($result);
        exit;
    }
    
//Save new record
    
if ( $_POST["actionCode"] == "S" ) {
        
$result tableInfo::getColumns("customerProfileOtherInfo");
        
        foreach (
$result as $column) {
            
$column $column['column_name'];
            if (isset(
$_POST[$column])) {
                
$temp[$column] = $_POST[$column];
            }
        }
        
        
$columns = array();
        
$values = array();
        
$parameters = array();        
        if ( 
$_POST["id"] == '0' ) {    
            foreach (
$temp as $column => $value) {
                
$columns[] = $column;
                
$parameters[] = !strlen($value) ? null $value;
                
$values[] = '?';
            }
            
$sql "INSERT customerProfileOtherInfo (" implode(', '$columns) . ") VALUES (" implode(', '$values) . ")";    
        } else {
            if (
$_POST['profile_delete'] == "Y") {    
                
$sql "delete from customerProfileOtherInfo WHERE id = ?";
            } else {
                foreach (
$temp as $column => $value) {
                    if (
$column != 'id' && $column != 'profile_id') {
                        switch (
$column) {
                            case 
'loginpw': {
                                if (!empty(
$value)) {
                                    
$parameters[] = md5($value);
                                    
$values[] = "`$column` = ?";
                                }
                                break;
                            }
                            default: {
                                
$parameters[] = !strlen($value) ? null $value;
                                
$values[] = "`$column` = ?";
                                break;
                            }
                        }
                    }
                }        
                
$sql "UPDATE customerProfileOtherInfo SET " implode(', '$values) . " WHERE id = ?";
            }
            
$parameters[] = $_POST["id"];            
        }
        
$result tableInfo::updateRecord($sql$parameters);                
    
        
$outValue $result;
        echo 
$outValue;
        exit;
    }    
}
    
    



//-----------------------------------------------------------------------------
// Check permission
//-----------------------------------------------------------------------------
if (!Util::isAdmin()) {
    
redirectAndExit('index.php?message=No permission!');
}

//-----------------------------------------------------------------------------
// Find job by id
//-----------------------------------------------------------------------------

    
$sql "SELECT * FROM customerProfile WHERE customer_id = ?";
    
$parameters = array($id);
    if (!(
$sth $dbh->prepare($sql))) {
        throw new 
Exception("sql prepare statement failure: $sql");
    }
    
$sth->setFetchMode(PDO::FETCH_ASSOC);
    if (!
$sth->execute($parameters)) {
        throw new 
Exception("sql execute statement failure: $sql");
    }
    
$customer $sth->fetch(PDO::FETCH_ASSOC);
    
    
//Profile not yet created
    
if ( is_null($customer['customer_id']) ) {
        
$customer['id'] = "0";
        
$customer['customer_id'] = $_GET["id"];
    }
    
    
//return array(
    //    'customer' => $customer,
    //);


//-----------------------------------------------------------------------------
// Save if POST method
//-----------------------------------------------------------------------------
if (isPost()) {
    
    
$sql "SELECT column_name FROM information_schema.columns WHERE table_schema = (SELECT DATABASE()) AND table_name = ?";
    
$parameters = array('customerProfile');
    if (!(
$sth $dbh->prepare($sql))) {
        throw new 
Exception("sql prepare statement failure: $sql");
    }
    
$sth->setFetchMode(PDO::FETCH_ASSOC);
    if (!
$sth->execute($parameters)) {
        throw new 
Exception("sql execute statement failure: $sql");
    }
    
$columns $sth->fetchAll();

    foreach (
$columns as $column) {
        
$column $column['column_name'];
        if (isset(
$_POST[$column])) {
            
$customer[$column] = $_POST[$column];
        }
    }

    
// Append record time
    
$now date("Y-m-d H:i:s");
    
    if ( 
$customer['id'] <> '0' ) {
        
$customer array_merge($customer, array(
            
'updated_at' => $now,
            
'updated_by' => $_SESSION['webadmin']['id'],
        ));
        
// Update customer Profile
        
$values = array();
        
$parameters = array();
        foreach (
$customer as $column => $value) {
            if (
$column != 'id') {
                switch (
$column) {
                    case 
'loginpw': {
                        if (!empty(
$value)) {
                            
$parameters[] = md5($value);
                            
$values[] = "`$column` = ?";
                        }
                        break;
                    }
                    default: {
                        
$parameters[] = !strlen($value) ? null $value;
                        
$values[] = "`$column` = ?";
                        break;
                    }
                }
            }
        }
        
$sql "UPDATE customerProfile SET " implode(', '$values) . " WHERE id = ?";
        
$parameters[] = $customer['id'];
        if (!(
$sth $dbh->prepare($sql))) {
            throw new 
Exception("sql prepare statement failure: $sql");
        }
        
$sth->setFetchMode(PDO::FETCH_ASSOC);
        if (!
$sth->execute($parameters)) {
            throw new 
Exception("sql execute statement failure: $sql");
        }        
        
    } else {
            
$customer array_merge($customer, array(
            
'created_at' => $now,
            
'created_by' => $_SESSION['webadmin']['id'],
        ));
        
// Create customer
        
$columns = array();
        
$values = array();
        
$parameters = array();
        foreach (
$customer as $column => $value) {
            
$columns[] = $column;
            
$parameters[] = !strlen($value) ? null $value;
            
$values[] = '?';
        }
        
$sql "INSERT customerProfile (" implode(', '$columns) . ") VALUES (" implode(', '$values) . ")";
        
        
//throw new Exception($sql . " S " . implode(', ', $parameters));
        
        
if (!($sth $dbh->prepare($sql))) {
            throw new 
Exception("sql prepare statement failure: $sql");
        }
        
$sth->setFetchMode(PDO::FETCH_ASSOC);
        if (!
$sth->execute($parameters)) {
            throw new 
Exception("sql execute statement failure: $sql");
        }
        
$customer['id'] = $dbh->lastInsertId();        
    }

    
redirectAndExit('index.php?message=Saved.');
}

//-----------------------------------------------------------------------------
// Return array parameters
//-----------------------------------------------------------------------------
return array(
    
'customer' => $customer,
    
'message' => $_GET['message'],
);