-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
166 lines (144 loc) · 3.83 KB
/
export.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
<?php
/**
* export.php
*
* Exports data from the database to a CSV file or displays the data in a textarea
*
* @author Angus Vine <[email protected]>
* @version 1.0
* @since 5.5 (The php version used)
*/
require_once 'core/init.php';
ob_start();
include 'includes/head.inc.php';
?>
<script>
/**
* function
*
* Displays a calendar for easy date selection and displays in the correct format
*/
$(function() {
$( "#start_date" ).datepicker({dateFormat: 'yy-mm-dd'});
$( "#start_date" ).datepicker("setDate", new Date('yy-mm-dd'));
$( "#end_date" ).datepicker({dateFormat: 'yy-mm-dd'});
$( "#end_date" ).datepicker("setDate", new Date('yy-mm-dd'));
});
</script>
<div class="sub-headerContainer">
<div class="sub-header">
<h2>Export Contacts</h2>
</div>
</div>
<?php
//Check if POST exists
if (Input::exists()) {
$validate = new Validation();
//Validate data
$validation = $validate->check($_POST, array(
'start_date' => array(
'required' => true,
'before_end' => Input::get('end_date')
),
'end_date' => array(
'required' => true,
),
'export_option' => array(
'required' => true
)
));
if($validation->passed()) {
$contact = new Contact();
try {
//search data
$results = $contact->export(Input::get('start_date'), Input::get('end_date'));
if (!$results) {
echo '<ul class="error"><li>No records were found.</li></ul>';
} else {
//Displays results in textarea
if (Input::get('export_option') == 'web') {
?> <div class="ta-div">
<textarea name="notes" id="notes" class="ta-notes" readonly>
<?php foreach ($results as $result) {
echo $result->email.' ';
}
?> </textarea>
</div>
<?php //Exports data to a CSV file
} else if (Input::get('export_option') == 'csv') {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="kindred_contacts.csv"');
ob_end_clean();
$fp = fopen('php://output', 'w');
$a = objectToArray($results);
fputcsv($fp, array_keys(reset($a)));
foreach ( $results as $result ) {
$array = objectToArray($result);
fputcsv($fp, $array);
}
fclose($fp);
exit();
}
}
} catch (Exception $e) {
Session::flash('home', $e->getMessage());
Redirect::to('index.php'); //redirect the user to the home page and display the error
exit();
}
} else {
//Display validation errors
echo '<ul class="error">';
foreach($validation->errors() as $error) {
echo '<li>' . $error . '</li>';
}
echo '</ul>';
}
}
?>
<form id="contact-form" class="form" action="" method="post">
<div class="formContainer">
<div class="field">
<label>
<div class="floatLeft">
<span class="inputLabel">Start Date</span>
</div>
<div class="floatRight">
<input type="text" name="start_date" id="start_date" value="" autocomplete="off" />
</div>
</label>
</div>
<div class="clear"></div>
<div class="field">
<label>
<div class="floatLeft">
<span class="inputLabel">End Date</span>
</div>
<div class="floatRight">
<input type="text" name="end_date" id="end_date" value="" autocomplete="off" />
</div>
</label>
</div>
<div class="clear"></div>
<br />
<span class="sub-header" style="border-bottom: 1px solid black; margin-bottom: 5px;">Select Output</span>
<div class="field">
<br />
<br />
<label>
<input type="radio" name="export_option" id ="export_option" value="csv">CSV file
</label>
<br />
<label>
<input type="radio" name="export_option" id ="export_option" value="web" checked="checked">Print on screen
</label>
</div>
</div>
<div class="button-padding" >
<div class="field-submit">
<input type="submit" name="export" value="Export">
</div>
</div>
</form>
<?php
include 'includes/foot.inc.php';
?>