/
campaign-details.php
285 lines (266 loc) · 11.3 KB
/
campaign-details.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
278
279
280
281
282
283
284
285
<?php require_once('includes/config.php');
// Copyright 2008 johnboyproductions.com
// Copyright 2010 Justin Reardon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
include('includes/sc-includes.php');
include('includes/donation.inc.php');
$pagetitle = "Campaigns";
//Set Campaign
if($_GET['campaign'] && is_numeric($_GET['campaign'])) {
$campaign = $_GET['campaign'];
} else {
header("Location: campaigns.php"); die;
}
mysql_select_db($database_contacts, $contacts);
$campaigns_result = mysql_query("SELECT * FROM campaigns WHERE campaign_id = $campaign", $contacts) or die(mysql_error());
$campaign = mysql_fetch_assoc($campaigns_result);
$campaign_row_count = mysql_num_rows($campaigns_result);
if($campaign_row_count == 0) {
header("Location: campaigns.php?"); die;
}
$where_clauses = array();
$show_donations = false;
if(isset($_GET['search'])) {
if(trim($_GET['name'])) {
$names = explode(",", $_GET['name']);
$clauses = array();
foreach($names as $name) {
if(trim($name)) {
$t = mysql_real_escape_string(trim($name));
$clauses[] = "(contact_first LIKE '%$t%' OR
contact_last LIKE '%$t%' OR
contact_company LIKE '%$t%' OR
CONCAT(contact_first, ' ', contact_last) LIKE '%$t%')";
}
}
if(count($clauses) > 0) {
$where_clauses[] = "(" . implode(" OR ", $clauses) . ")";
}
}
if(trim($_GET['donation_desc'])) {
$terms = explode(",", $_GET['donation_desc']);
$clauses = array();
foreach($terms as $term) {
if(trim($term)) {
$t = mysql_real_escape_string(trim($term));
$clauses[] = "(donation_description LIKE '%$t%')";
}
}
if(count($clauses) > 0) {
$where_clauses[] = implode(" OR ", $clauses);
$show_donations = true;
}
}
if(in_array(trim($_GET['donation_status']), array("Expected", "Pledged", "Received"))) {
$where_clauses[] = "donation_status = '" . strtolower(trim($_GET['donation_status'])) . "'";
}
}
$where = (count($where_clauses) > 0 ? " AND " . implode(" AND ", $where_clauses) : "");
$query_donations = "SELECT
donation_id,
donation_value,
donation_status,
donation_is_cash,
donation_pledge_date,
donation_received_date,
donation_description,
contacts.*,
contact_company IS NULL AS isnull
FROM donations
LEFT JOIN contacts USING (`contact_id`)
WHERE campaign_id = " . $campaign['campaign_id'] . $where .
" ORDER BY isnull, contact_company, contact_first, contact_last";
$donations = mysql_query($query_donations, $contacts) or die(mysql_error());
$row_donations = mysql_fetch_assoc($donations);
$totalRows_donations = mysql_num_rows($donations);
$query_targets = "SELECT
contact_id,
contact_first,
contact_last,
contact_title,
contact_company
FROM targets
LEFT JOIN contacts using (contact_id)
WHERE campaign_id = " . $campaign['campaign_id'] ." AND
(SELECT COUNT(*)
FROM donations
WHERE campaign_id = " . $campaign['campaign_id'] ." AND
donations.contact_id = targets.contact_id
) = 0";
$targets = mysql_query($query_targets, $contacts) or die(mysql_error());
$row_targets = mysql_fetch_assoc($targets);
$totalRows_targets = mysql_num_rows($targets);
$stats = donation_stats($campaign['campaign_id'], $where);
$title_text = "Campaign - ". $campaign['campaign_name'];
$back_track = array('title' => "Campaigns", 'url' => "campaigns.php");
$display = isset($_GET['display']) ? $_GET['display'] : "html";
if (!in_array($display, array("csv", "html"))) {
$display = "html";
}
switch ($display) {
case "csv":
$out = fopen('php://output', 'w');
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="donations.csv"');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
$fields = array("first name", "last name", "title", "company",
"street", "city", "state/province", "postal code",
"phone", "cell", "fax", "email", "website",
"is individual", "donation value", "donation description", "status", "received", "pledged");
fputcsv($out, $fields);
if(!$row_donations) {
break;
}
do {
fputcsv($out, array($row_donations["contact_first"], $row_donations["contact_last"],
$row_donations["contact_title"], $row_donations["contact_company"],
$row_donations["contact_street"], $row_donations["contact_city"],
$row_donations["contact_state"], $row_donations["contact_zip"],
$row_donations["contact_phone"], $row_donations["contact_cell"],
$row_donations["contact_fax"], $row_donations["contact_email"],
$row_donations["contact_web"], $row_donations["isnull"],
$row_donations["donation_value"], $row_donations["donation_description"],
$row_donations["donation_status"], $row_donations["donation_received_date"],
$row_donations["donation_pledge_date"]));
} while ($row_donations = mysql_fetch_assoc($donations));
break;
case "html":
?>
<?php include('includes/header.php'); ?>
<div class="container">
<div class="leftcolumn">
<span class="notices" id="notice" style="display:<?php echo $dis; ?>">
<?php display_msg(); ?>
</span>
<h2>Campaign - <?php echo $campaign['campaign_name']; ?></h2>
<a href="<?php echo $_SERVER['REQUEST_URI'] . "&display=csv"; ?>"><strong>Export</strong></a>
<br class="first"/><br /><a href="#" onclick="new Effect.toggle('search_pane', 'slide', { afterFinish: function() { $('name').focus(); }}); return false;">+Search</a>
<div id="search_pane" style="display:<?php echo isset($_GET['search']) ? "block" : "none"; ?>">
<form id="search_form" name="search_form" method="get" action="">
<fieldset class="width3">
<input type="hidden" name="campaign" id="campaign_id" value="<?php echo $campaign['campaign_id'] ?>" />
<input type="hidden" name="search" id="search" value="search" />
<label class="first column unitx2">
Name/Organization
<input type="text" id="name" name="name" value="<?php echo $_GET['name']; ?>"/>
</label>
<label class=" column unitx3">
Donation Description
<input type="text" id="donation_desc" name="donation_desc" value="<?php echo $_GET['donation_desc']; ?>"/>
</label>
<label class="column unitx1">
With Status
<select id="donation_status" name="donation_status">
<option <?php if ($_GET['donation_status'] == "Any") { echo "selected='selected'"; } ?>>Any</option>
<option <?php if ($_GET['donation_status'] == "Expected") { echo "selected='selected'"; } ?>>Expected</option>
<option <?php if ($_GET['donation_status'] == "Pledged") { echo "selected='selected'"; } ?>>Pledged</option>
<option <?php if ($_GET['donation_status'] == "Received") { echo "selected='selected'"; } ?>>Received</option>
</select>
</label>
<label class="first column unitx3">
<input type="submit" id="submit_search" name="submit_search" value="Search"/>
</label>
</fieldset>
</form>
</div>
<table class="sortable" style="width: 100%;">
<thead>
<tr>
<th class="nosort"></th>
<th class="sortfirstasc text">Donor</th>
<th class="text">Status</th>
<th class="currency">Value</th>
<th class="text centre-cell">Type</th>
<th>Pledged</th>
<th>Received</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="6" class="right-cell">Expected</td>
<td class="right-cell"><?php echo money_format("%n", $stats->expected); ?></td>
</tr>
<tr>
<td colspan="6" class="right-cell">Pledged</td>
<td class="right-cell"><?php echo money_format("%n", $stats->pledged); ?></td>
</tr>
<tr>
<td colspan="6" class="right-cell">Received</td>
<td class="right-cell"><?php echo money_format("%n", $stats->received); ?></td>
</tr>
<tr>
<td colspan="6" class="right-cell">Total</td>
<td class="right-cell"><?php echo money_format("%n", $stats->total); ?></td>
</tr>
</tfoot>
<tbody>
<?php if ($totalRows_donations > 0) { ?>
<?php do { $row_count++; ?>
<tr>
<td style="padding-right: 10px">
<a href="donation-details.php?id=<?php echo $row_donations['donation_id']; ?>" title="<?php echo htmlspecialchars($row_donations['donation_description']) ?>">Details</a>
</td>
<td><a href="contact-details.php?id=<?php echo $row_donations['contact_id']; ?>">
<?php echo display_name($row_donations); ?>
</a></td>
<td><?php echo ucwords($row_donations['donation_status']) ?></td>
<td class="one-line"><?php printf("$%.2f", $row_donations['donation_value']); ?></td>
<td class="one-line centre-cell">
<?php echo donation_kind_text($row_donations['donation_is_cash']); ?>
</td>
<td class="one-line">
<?php echo $row_donations['donation_pledge_date']
? date("M j, Y", strtotime($row_donations['donation_pledge_date']))
: $na;
?>
</td>
<td class="one-line">
<?php echo $row_donations['donation_received_date']
? date("M j, Y", strtotime($row_donations['donation_received_date']))
: $na;
?>
</td>
</tr>
<?php } while ($row_donations = mysql_fetch_assoc($donations)); ?>
<?php } else {
if(isset($_GET['search'])) {
?>
<tr><td style="text-align: center;" colspan="7">No Donations in <?php echo $campaign['campaign_name']; ?> match.</td></tr>
<?php } else { ?>
<tr><td style="text-align: center;" colspan="7">No Donations for <?php echo $campaign['campaign_name']; ?></td></tr>
<?php } } ?>
</tbody>
</table>
<br />
<?php if ($totalRows_targets > 0) { ?>
<h3>Other Targets</h3>
<ul>
<?php do { ?>
<li><a href="contact-details.php?id=<?php echo $row_targets['contact_id']; ?>">
<?php echo display_name($row_targets); ?>
</a></li>
<?php } while ($row_targets = mysql_fetch_assoc($targets)); ?>
</ul>
<?php } ?>
</div>
<?php include('includes/right-column.php'); ?>
<br clear="all" />
</div>
<?php include('includes/footer.php'); ?>
</body>
</html>
<?php
break;
}