-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfprint.php
157 lines (140 loc) · 4.12 KB
/
pdfprint.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
<?php
// *************************************************************
// file: pdfprint.php
// created by: Alex Gordon, Elliott Staude
// date: 04-6-2014
// purpose: A general purpose page that allows users to print out search data in PDF format, or copy-paste a selection of search data into a .csv file
//
// *************************************************************
require 'fpdf.php';
include 'symbolic_values.php';
session_start();
if(!isset($_SESSION['user']))
{
header('Location: login.php');
}
// Manager or User
if($_SESSION['access']==ADMIN_PERMISSION OR $_SESSION['access']==USER_PERMISSION)
{
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',10);
$realterms = "NULL";
// THIS GUY HERE holds the posted info so the session data can be cleaned safely
$posted;
if (isset($_SESSION['pdfpost']['searchTerms']) AND $_SESSION['pdfpost']['searchTerms'] != "")
{
$posted = $_SESSION['pdfpost'];
$_SESSION['pdfpost'] = NULL;
$pdf->Cell(30,10,"GQUIP, the Gordon College equipment database");
$pdf->Ln();
$pdf->Ln();
$pdf->Cell(30,10,"Searched terms: ");
$pdf->Ln();
$realTerms = str_replace(",", " ", $posted['searchTerms']);
$realTerms = str_replace(" ", " ", $realTerms);
$realTerms = explode(" ", $realTerms);
foreach ( $realTerms as $thisTerm )
{
$thisTerm = trim($thisTerm);
$pdf->Cell(30,10,$thisTerm . " ");
$pdf->Ln();
}
}
else
{
$posted = NULL;
header('Location: search.php');
}
$pdf->Ln();
$query;
$result;
//Check if the tables to search are set
if (isset($posted['searchTables']))
{
$pdf->Cell(30,10,"Searched tables: ");
$pdf->Ln();
foreach ($posted['searchTables'] as $table)
{
$pdf->Cell(30,10,$tableReadableArray[$table]);
$pdf->Ln();
}
foreach ($posted['searchTables'] as $table)
{
$query[$table] = "SELECT *
FROM " . $table . " WHERE ";
$numberCols = count($columnArray[$table]);
$numberTerms = count($realTerms);
//Ask all of the columns if they have a row containing this value
if ($numberCols > 1)
{
for($colIndex = 0; $colIndex < ($numberCols - 1); $colIndex++)
{
for($realIndex = 0; $realIndex < ($numberTerms); $realIndex++)
{
$query[$table] .= $columnArray[$table][$colIndex] . " LIKE '%" . $realTerms[$realIndex] . "%' OR ";
}
}
}
if ($numberTerms > 1)
{
for($realIndex = 0; $realIndex < ($numberTerms - 1); $realIndex++)
{
$query[$table] .= $columnArray[$table][$numberCols-1] . " LIKE '%" . $realTerms[$realIndex] . "%' OR ";
}
}
$query[$table] .= $columnArray[$table][$numberCols-1] . " LIKE '%" . $realTerms[$numberTerms-1] . "%';";
}
$pdf->Ln();
$pdf->Ln();
$pdf->Cell(30,10,"Results:");
$pdf->Ln();
$pdf->Ln();
include 'open_db.php';
//Go through each table's query, get the relevant data, display it
foreach ( $posted['searchTables'] as $table )
{
// Get the row data from each table
$result[$table] = sqlsrv_query($conn, $query[$table], array(), array( "Scrollable" => 'static' ));
$row_count = sqlsrv_num_rows($result[$table]);
sqlsrvErrorLinguist($result[$table], "Problem with getting results");
// Set up the pdf section header
$pdf->Cell(30,10,"~~~~~~~~~~~~~~" . $tableReadableArray[$table]);
$pdf->Ln();
$pdf->Cell(30,10,"Result count: " . $row_count);
//Ensure there are some results before starting to show tables
if ($row_count > 0)
{
$pdf->Ln();
$pdf->Ln();
//Output this table's data
while($row[$table] = sqlsrv_fetch_array($result[$table]))
{
foreach ( $columnArray[$table] as $tableRowValue )
{
//Make sure that the data here can be printed
if (!($row[$table][$tableRowValue] instanceof DateTime))
{
$pdf->Cell($columnSizesArray[$table][$tableRowValue],5,$tableRowValue . ": " . $row[$table][$tableRowValue]);
}
else
{
$pdf->Cell($columnSizesArray[$table][$tableRowValue],5,$tableRowValue . ": " . $row[$table][$tableRowValue]->format('Y-m-d H:i:s'));
}
$pdf->Ln();
}
$pdf->Ln();
}
$pdf->Ln();
}
else
{
$pdf->Cell(20,10,"N/A");
$pdf->Ln();
$pdf->Ln();
}
}
}
$pdf->Output();
}
?>