-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReportable.php
72 lines (53 loc) · 1.35 KB
/
Reportable.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
<?php
class Reportable {
public $headers, $rows;
public $tableClass = 'reportable';
public $tableID;
public $reportsPath = 'reports';
public $fileName;
public $currentRow = 'even';
function __construct($ID = null) {
$this->tableID = $ID ? $ID : 'report-' . time();
}
public function addHeader($title) {
$this->headers[] = $title;
}
public function addRow($content) {
$this->rows[] = func_get_args();
}
/*
* HTML Handling
*/
public function showTable() {
require_once('Reportable.template.php');
}
public function rowClass() {
$this->currentRow = $this->currentRow == 'even' ? 'odd' : 'even';
return $this->currentRow;
}
/*
* CSV Handling
*/
public function setFileName() {
$this->fileName = $this->fileName ? $this->fileName : $this->tableID . '.csv';
}
public function filePath() {
return $this->reportsPath . '/' . $this->fileName;
}
public function generateCSV() {
$this->setFileName();
$report = fopen($this->filePath(), 'w+');
fputcsv($report, array_map('strip_tags', $this->headers));
foreach($this->rows as $row) {
fputcsv($report, array_map('strip_tags', $row));
}
fclose($report);
}
public function getCSV() {
return file_get_contents($this->filePath());
}
public function linkToCSV() {
return $this->filePath();
}
}
?>