-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.php
executable file
·270 lines (232 loc) · 13.2 KB
/
import.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
<?php
require_once('includes/config.php');
ini_set('auto_detect_line_endings',TRUE);
ob_start();
include('includes/sc-includes.php');
$pagetitle = 'Import Spreadsheet';
function data_empty($dataRow, $type) {
if($type == "donation") {
$data_empty = true;
foreach($dataRow as $data_value) {
if($data_value != '')
$data_empty = false;
}
return $data_empty;
}
else {
return !($dataRow[2] && $dataRow[3] && $dataRow[4]);
}
}
function duplicate_donor($data) {
$matching_full_names = "SELECT * FROM contacts WHERE contact_first = '".addslashes($data[2])."' AND contact_last = '".addslashes($data[3])."'";
$result_matching_full_names = mysql_query($matching_full_names);
$row_matching_full_names = mysql_fetch_assoc($result_matching_full_names);
return ($row_matching_full_names && ($row_matching_full_names['contact_email'] == addslashes($data[4])));
}
function duplicate_donation($data) {
$recipt_number = $data[11];
$receipt_number_query = "SELECT * FROM donations WHERE receipt_number = '". $recipt_number . "' AND match_company_name = '" . $data[17] . "'";
$result_receipt_number = mysql_query($receipt_number_query);
return (mysql_num_rows($result_receipt_number) > 0);
}
function find_donor($data) {
$email = $data[15];
$name_to_match = $data[1];
$donors_query = "SELECT * FROM contacts";
$donors_result = mysql_query($donors_query);
$donor_id = -1;
if ($donors_result) {
while($row = mysql_fetch_assoc($donors_result)) {
$possible_match = ltrim($row['contact_first']) . " " . $row['contact_last'];
if(strpos($name_to_match, $possible_match) > -1) {
$donor_id = $row['contact_id'];
}
}
}
return $donor_id;
}
if (!empty($_GET['csv']) && $_GET['csv'] == 'import' && $_FILES['csv']['tmp_name']) {
$row = 1;
$handle = fopen ($_FILES['csv']['tmp_name'],"r");
$cf = array();
$csv_type = "donation";
$duplicate_donations_array = array();
$failed_imports = array();
while ($data = fgetcsv($handle, 1000, ",")) {
//custom field array
if ($row == 1) {
foreach ($data as $key => $value) {
if ($key > 200) {
$cf[$key] = $value;
}
// if the last value (since we are in a foreach loop is "Comments", then we know that we are trying to import a donor csv)
if($value == "Comments")
$csv_type = "donor";
}
}
//
//end add extra fields
if($row > 1 && !data_empty($data, $csv_type)) {
// we are importing new donors
if($csv_type == "donor"){
if(!duplicate_donor($data)) {
$donor_insert_query = mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES
(
'".addslashes(ltrim(rtrim($data[2])))."',
'".addslashes(ltrim(rtrim($data[3])))."',
'".addslashes(ltrim(rtrim($data[4])))."'
)");
if(!$donor_insert_query){
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $donor_insert_query;
die($message);
}
$donor_id = mysql_insert_id();
$contact_field = mysql_fetch_assoc(mysql_query("SELECT * FROM fields WHERE field_title = 'contact'"));
$anonymous_field = $field = mysql_fetch_assoc(mysql_query("SELECT * FROM fields WHERE field_title = 'anonymous'"));
$cf_query = mysql_query("INSERT INTO fields_assoc (cfield_contact, cfield_field, cfield_value) VALUES
(
'".$donor_id."',
'".$anonymous_field['field_id']."',
'".addslashes(ltrim(rtrim($data[5])))."'
)");
$cf_query = mysql_query("INSERT INTO fields_assoc (cfield_contact, cfield_field, cfield_value) VALUES
(
'".$donor_id."',
'".$contact_field['field_id']."',
'".addslashes(ltrim(rtrim($data[6])))."'
)");
$donor_has_no_donations = mysql_query("INSERT INTO notes (note_contact, note_text, note_date, note_status, note_user, note_pin)
VALUES ('" . $donor_id . "', '" . 'This donor has no donations!' . "', '" . time() . "', '1', '0', 1)");
if($data[7]){
$note_insert_query = mysql_query("INSERT INTO notes (note_contact, note_text, note_date, note_status, note_user)
VALUES ('" . $donor_id . "', '" . addslashes(ltrim(rtrim($data[7]))) . "', '" . time() . "', '1', '0')");
if(!$note_insert_query){
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $note_insert_query;
die($message);
}
}
}
}
// we imported a donor spreadsheet
else {
if(!(duplicate_donation($data))) {
$donor_id = find_donor($data);
if($donor_id != -1) {
$get_donor_information = mysql_query("SELECT * FROM contacts WHERE contact_id = '" . $donor_id . "'");
$donor_information = mysql_fetch_assoc($get_donor_information);
// pull in the latest email, address, city, and state from the CMU spreadsheet
if(!$donor_information['contact_email']) {
mysql_query("UPDATE contacts SET contact_email = " . addslashes($data[15]) . " WHERE contact_id = '" . $donor_id . "'");
}
if(!$donor_information['contact_street']) {
mysql_query("UPDATE contacts SET contact_street = '" .addslashes($data[3]). "' WHERE contact_id = '" . $donor_id . "'");
}
if(!$donor_information['contact_city']) {
mysql_query("UPDATE contacts SET contact_city = '" .addslashes($data[12]). "' WHERE contact_id = '" . $donor_id . "'");
}
if(!$donor_information['contact_state']) {
mysql_query("UPDATE contacts SET contact_state = '" .addslashes($data[13]). "' WHERE contact_id = '" . $donor_id . "'");
}
// format the dates
$php_dt_date_record = strtotime(ltrim(rtrim($data[4])));
$mysql_dt_date_record = date('Y-m-d H:i:s', $php_dt_date_record);
$php_date_added = strtotime(ltrim(rtrim($data[5])));
$mysql_added = date('Y-m-d H:i:s', $php_date_added);
// insert the donation
$donation_query = "INSERT INTO donations
VALUES ('',
'".addslashes(ltrim(rtrim($data[0])))."',
'".$mysql_dt_date_record."',
'".$mysql_added."',
'".addslashes(ltrim(rtrim($data[6])))."',
'".addslashes(ltrim(rtrim($data[7])))."',
'".addslashes(ltrim(rtrim($data[8])))."',
'".addslashes(ltrim(rtrim($data[9])))."',
'".addslashes(ltrim(rtrim($data[10])))."',
'".addslashes(ltrim(rtrim($data[11])))."',
'".addslashes(ltrim(rtrim($data[16])))."',
'".addslashes(ltrim(rtrim($data[17])))."',
'".addslashes(ltrim(rtrim($donor_id)))."');";
$result = mysql_query($donation_query);
if (!$result) {
array_push($failed_imports, $data);
} else {
$find_no_donations_note = mysql_query("SELECT * FROM notes WHERE note_contact = '" . $donor_id . "' AND note_text = 'This donor has no donations!'");
$find_no_donations_result = mysql_fetch_assoc($find_no_donations_note);
if($find_no_donations_result) {
mysql_query("DELETE FROM notes WHERE note_id = " . $find_no_donations_result['note_id']);
}
}
} else {
// we couldn't find a donor for the donation
array_push($failed_imports, $data);
}
} else {
// we have a duplicate donation
array_push($duplicate_donations_array, $data);
}
}
}
$row++;
}
$_SESSION['duplicate_donations_array'] = $duplicate_donations_array;
$_SESSION['failed_imports'] = $failed_imports;
var_dump($_SESSION['failed_imports']);
// exit;
header('Location: contacts.php?import=success');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $pagetitle; ?></title>
<script src="includes/lib/prototype.js" type="text/javascript"></script>
<script src="includes/src/effects.js" type="text/javascript"></script>
<script src="includes/validation.js" type="text/javascript"></script>
<script src="includes/src/scriptaculous.js" type="text/javascript"></script>
<link href="includes/style.css" rel="stylesheet" type="text/css" />
<link href="includes/simplecustomer.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include('includes/header.php'); ?>
<div class="container">
<div class="leftcolumn">
<h2> Import Donations </h2>
<table width="540" border="0" cellpadding="0" cellspacing="0">
<tr>
</tr>
<tr>
<td colspan="2">
<form name="form1" id="form1" enctype="multipart/form-data" method="post" action="?csv=import">
<input name="csv" type="file" id="csv" size="40" />
<br />
<input name="submit" type="submit" value="Import File" />
<a href="csv.php"></a>
<?php
if(!$_FILES['csv']['tmp_name'] && isset($_GET['csv'])){
echo "<br><br>You didn't select any file to upload. Please try uploading your file again.";
}
?>
</form>
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><a href="csv.php"><strong>+ Export Donors</strong></a></td>
</tr>
</table>
<p> </p>
<p> </p>
<p> </p>
</div>
<?php include('includes/right-column.php'); ?>
<br clear="all" />
</div>
<?php include('includes/footer.php'); ?>
</body>
</html>