/var/www/onesupportsys.onesolution.hk/customerProfileView/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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
global $dbh;

require_once(
'../inc/configure.php');
    
//require_once('../inc/configure.php');
    //session_start();
    
    
            //var_dump(!empty($_POST["actionCode"]), $_GET, isGet(), $_POST, isPost());
            //throw new Exception("sql prepare statement failure: $sql");
            
            
    //Ajax Function
    
if(!empty($_POST["actionCode"])){
        
$nowdate date("Y-m-d H:i:s");
        
        
//Get a saved record from customerProfileOtherInfo 
        
if ( $_POST["actionCode"] == "R" ) {
            
$sql "select * from customerProfileOtherInfo where profile_id = ? and status = 0";
            
$result tableInfo::recordData($sql$_POST["profileId"], "Y");
            
$idx 0;
            foreach(
$result as $r){ 
                
$r["login_pw"] = security::encrypt_decrypt('decrypt'$r["login_pw"]);
                
$result[$idx] = $r;
                
$idx++;
            }
            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) {
                    if ( 
$column == "login_pw" ) {
                        
$value security::encrypt_decrypt('encrypt'$value);
                    }
                    
$columns[] = $column;
                    
$parameters[] = !strlen($value) ? null $value;
                    
$values[] = '?';
                }
                
                
$columns array_merge($columns, array('created_at''created_by'));
                
$values array_merge($values,  array('?''?'));
                
$parameters array_merge($parameters, array($nowdate$_SESSION['webadmin']['id']));
                
                
$sql "INSERT customerProfileOtherInfo (" implode(', '$columns) . ") VALUES (" implode(', '$values) . ")";    
            } else {
                
var_dump($_POST);
                
//$oAct = tableInfo::recordData("select * from customerProfileOtherInfo where id = ?", $_POST["id"])[0];
                
$result tableInfo::recordData("select * from customerProfileOtherInfo where id = ?"$_POST["id"], "N");
                
$oAct $result[0];
                foreach (
$temp as $column => $value) {
                    if (
$column != 'id' && $column != 'profile_id') {
                        switch (
$column) {
                            case 
'loginpw': {
                                if (!empty(
$value)) {
                                    
$parameters[] = md5($value);
                                    
$values[] = "`$column` = ?";
                                }
                                break;
                            }
                            case 
'login_pw': {
                                
$value security::encrypt_decrypt('encrypt'$value);
                                
$parameters[] = $value;
                                
$values[] = "`$column` = ?";
                                break;
                            }                            
                            default: {
                                
$parameters[] = !strlen($value) ? null $value;
                                
$values[] = "`$column` = ?";
                                break;
                            }
                        }
                        
                        
$oldValue $oAct[$column];        
                        if ( 
$value != $oldValue ) {
                            
$changes[] = array("customerProfileOtherInfo"$column$_POST["id"], $oldValue$value);
                        }
                    }
                }        
                if (
$_POST['profile_delete'] == "Y") { 
                    
$changes[] = array("customerProfileOtherInfo""status"$_POST["id"], "0""1");
                    
$values array_merge($values,  array("`deleted_at` = ?""`deleted_by` = ?""`status` = ?"));
                    
$parameters array_merge($parameters, array($nowdate$_SESSION['webadmin']['id'], 1));                    
                } else {
                    
$values array_merge($values,  array("`updated_at` = ?""`updated_by` = ?"));
                    
$parameters array_merge($parameters, array($nowdate$_SESSION['webadmin']['id']));
                }
                
$sql "UPDATE customerProfileOtherInfo SET " implode(', '$values) . " WHERE id = ?";
                
$parameters[] = $_POST["id"];        
        
                if ( !
is_null($changes) ) {
                    
//Do log changes here
                    
logChanges($changes);
                }
            }
            
$result tableInfo::updateRecord($sql$parameters);                
        
            
$outValue $result;
            echo 
$outValue;
            exit;
        }    
    }
    
    
    if(!empty(
$_SESSION['webadmin'])){
        if (
isPost()) {
            
$cust_id $_POST["cust_id"];
            
$ids $_POST["line_id"];
            
$nowdate date("Y-m-d H:i:s");
            
$message "";
            
            
//var_dump($ids, "------", $_POST);
            //throw new Exception("sql prepare statement failure: $sql");
            
            
            
$columns tableInfo::getColumns("customerProfile");
            
            foreach(
$ids as $cId){
                unset(
$parameters);
                unset(
$values);
                unset(
$changes);

                
//$ocp = tableInfo::recordData("select * from customerProfile where id = ?", $cId)[0];
                
                
unset($ocp);
                
                
$result tableInfo::recordData("select * from customerProfile where id = ?"$cId"N");
                
$ocp $result[0];
                
                
//var_dump("Current id = $cId ---- select * from customerProfile where id = $cId");
                
                //if ($cId == "7") {
                //    var_dump($ocp, "__________");
                //}
        
        //var_dump("---", $ocp);
                
                
foreach ($columns as $column) {
                    
$column $column['column_name'];
                    if (isset(
$_POST[$column])) {
                        
$cp[$column] = $_POST[$column][$cId];
                        
                        
$abc $_POST[$column][$cId];
                        
                        
$value $cp[$column];
                        
$oldValue $ocp[$column];
                        
                        
//if ( $column == "location" && $cId == "7" ) {
                            //var_dump("(id)", $ids, "----Current", $cId, "----Name", $column, "----value", $cp[$column]);
                            //var_dump($abc, $value, $oldValue);
                            //var_dump(" ~~~~~~~~~~~~~~~~~~~~~~~~ ", $_POST[$column]["7"]);
                        //}
                        
                        
                        
if ( $value != $oldValue ) {
                            

                        
//var_dump("----Current", $cId, "----Name", $column, "----value", $value, "----old value", $oldValue, "select * from customerProfile where id = $cId");
                        //throw new Exception("------");                            
                            
                            
$changes[] = array("customerProfile"$column$cId$oldValue$value);
                            
                            
                            
$parameters[] = !strlen($value) ? null $value;
                            
$values[] = "`$column` = ?";
                            
                            if ( 
$column == "status" ) {
                                
$updateColumn $value == "1" "deleted_at" "updated_at";
                                
$parameters[] = $nowdate;
                                
$values[] = "`$updateColumn` = ?";
                            }
                        }
                    }
                }        
                
                if ( !
is_null($changes) ) {
                    if (
$updateColumn == "deleted_at") { 
                        
$values array_merge($values,  array("`deleted_at` = ?""`deleted_by` = ?"));
                        
$parameters array_merge($parameters, array($nowdate$_SESSION['webadmin']['id']));                    
                    } else {
                        
$values array_merge($values,  array("`updated_at` = ?""`updated_by` = ?"));
                        
$parameters array_merge($parameters, array($nowdate$_SESSION['webadmin']['id']));
                    }                
                    
//var_dump("Chanages = ", $changes);
                    
$message $message updateDatabase($cId$parameters$values"Modify"$changes);
                }
            }
            
            
//throw new Exception("------");
            
            
if ($_POST["formAction"] == "Add") {
                unset(
$parameters);
                unset(
$values);
                
$parameters[] = $cust_id;
                
$parameters[] = $_SESSION['webadmin']['id'];
                
$parameters[] = $nowdate;
                
$values[] = "?";
                
$values[] = "?";
                
$values[] = "?";
                
$message $message updateDatabase($cId$parameters$values"Add"null);
            }
            
            
            
            
header("Location: modifyform.php?id=" $cust_id "&message=" . ($message == "" "Update completed." $message) );
        }

    }
    
    
//-----------------------------------------------------------------------------
    // Return array parameters
    //-----------------------------------------------------------------------------
    
return array(
        
'customer' => $customer,
        
'message' => $_GET['message'],
    );
    
    function 
updateDatabase($id$inParameters$inValues$inAction$inChanges) {
        
$message "";
        
$sql "update customerProfile set " implode(', '$inValues) . " where id = $id";
        
        if (
$inAction == "Add") {
            
$sql "insert customerProfile ( customer_id, created_by, created_at ) values ( " implode(', '$inValues) . ")";
        }
        try {
            
$result tableInfo::updateRecord($sql$inParameters);
        }
        catch(
Exception $e) {
            
$message $message $e->getMessage();
        }

        
$message $message logChanges($inChanges);
        
//foreach ($inChanges as $inChange) {
        //    $nowdate = date("Y-m-d H:i:s");
        //    $sql = "insert changeLog values (?, ?,?,?,?,?,?,?)";
        //    $par = array('0', $inChange[0], $inChange[1], $inChange[2], $inChange[3], $inChange[4], $_SESSION['webadmin']['id'], $nowdate );
        //    try {
        //        $result = tableInfo::updateRecord($sql, $par);
        //    }
        //    catch(Exception $e) {
        //        $message = $message . $e->getMessage();
        //    }
        //    unset($par);
        //}

        
return $message;
    }
    
    function 
logChanges($inChanges) {
        
$message "";
        foreach (
$inChanges as $inChange) {
            
$nowdate date("Y-m-d H:i:s");
            
$sql "insert changeLog values (?, ?,?,?,?,?,?,?)";
            
$par = array('0'$inChange[0], $inChange[1], $inChange[2], $inChange[3], $inChange[4], $_SESSION['webadmin']['id'], $nowdate );
            try {
                
$result tableInfo::updateRecord($sql$par);
            }
            catch(
Exception $e) {
                
$message $message $e->getMessage();
            }
            unset(
$par);
        }        
        return 
$message;
    }