function makepdf($userid) { if ($userid !== login_userid() && login_privileges() !== 2) { pb_replace_with("main", "<p>Access denied for this information.</p>"); return; } $userData = db_queryWith("SELECT * FROM users WHERE userid = :userid", array("userid" => $userid)); if ($userData->rowCount() === 0) { pb_replace_with("main", "<p>Access denied for this information.</p>"); return; } $userData = $userData->fetch(); if ($userData["isEmployee"]) { pb_replace_with("main", "<p>This ({$userData['name']}) is an employee.</p>"); return; } $transactions = db_queryWith("SELECT tran.unixtime, sour.userId as sourceID, sour.name as source, targ.name as target, tran.volume as volume, tran.description as description " . "FROM transactions as tran, users as sour, users as targ " . "WHERE (tran.sourceAccount = :userid OR tran.targetAccount = :userid) AND tran.isVerified " . "AND sour.userId = tran.sourceAccount AND targ.userId = tran.targetAccount " . "ORDER BY tran.unixtime DESC ", array("userid" => $userid)); if ($transactions->rowCount() === 0) { pb_replace_with("main", "<p>No transactions.</p>"); return; } require_once "transactionpdf.php"; // column titles $header = array('Date', 'Description', 'Other Party', 'Volume'); // date, taken from the database $data = array(); foreach ($transactions as $t) { $volume = $t["volume"] / 100.0; if ($t["source"] === $t["target"]) { $volume = 0; } $other = $t["target"]; if ($t["sourceID"] === $userid) { $volume = -$volume; } else { $other = $t["source"]; } $data[] = array(date("Y-m-d H:i", $t["unixtime"]), $t["description"], $other, $volume); } // Create the PDF and print it $pdf = TransactionPDF::create($header, $data); $pdf->Output('transaction-hstory.pdf', 'I'); // After printing the pdf we don't want to end with an HTML file, so exit immediately exit(0); }
/** * Fills the pdf with a table of transactions * TODO: Make it pretty */ public static function createTable($pdf, $header, $data) { $dimensions = $pdf->getPageDimensions(); $cellWidth = 45; $needsHeader = true; // The header should be on every page's top $currentPage = 1; foreach($data as $row) { // Height of the text in lines $lineCount = TransactionPDF::getLineCount($pdf, $row, $cellWidth); $startY = $pdf->GetY(); if (($startY + $lineCount * 6) + $dimensions['bm'] > ($dimensions['hk'])) { //this row would cause a page break, so go to the next page $pdf->AddPage(); $currentPage++; $pdf->SetPage($currentPage); $needsHeader = true; } // Add the header if needed if ($needsHeader) { $borders = 'LRTB'; $lineCount = TransactionPDF::getLineCount($pdf, $header, $cellWidth); for ($i = 0; $i < sizeof($row); $i++) $pdf->MultiCell($cellWidth, $lineCount * 6, $header[$i], $borders, 'L', 0, 0); $pdf->Ln(); $needsHeader = false; } // Now draw the row $borders = 'LRB'; $lineCount = TransactionPDF::getLineCount($pdf, $row, $cellWidth); for ($i = 0; $i < sizeof($row); $i++) $pdf->MultiCell($cellWidth, $lineCount * 6, $row[$i], $borders, 'L', 0, 0); $pdf->Ln(); } }