parent
d2a1d60df0
commit
5823ec15e2
@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
use League\Csv\Writer;
|
||||
use League\Csv\HTMLConverter;
|
||||
use odsPhpGenerator\ods;
|
||||
use odsPhpGenerator\odsTable;
|
||||
use odsPhpGenerator\odsTableRow;
|
||||
use odsPhpGenerator\odsTableColumn;
|
||||
use odsPhpGenerator\odsTableCellString;
|
||||
use odsPhpGenerator\odsStyleTableColumn;
|
||||
use odsPhpGenerator\odsStyleTableCell;
|
||||
|
||||
class Report {
|
||||
|
||||
private $title = "";
|
||||
private $header = [];
|
||||
private $data = [];
|
||||
|
||||
public function __construct(string $title = "", array $header = [], array $data = []) {
|
||||
$this->title = $title;
|
||||
$this->header = $header;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function setHeader(array $header) {
|
||||
$this->header = $header;
|
||||
}
|
||||
|
||||
public function addDataRow(array $columns) {
|
||||
$this->data[] = $columns;
|
||||
}
|
||||
|
||||
public function getHeader(): array {
|
||||
return $this->header;
|
||||
}
|
||||
|
||||
public function getData(): array {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function output(string $format) {
|
||||
switch ($format) {
|
||||
case "ods":
|
||||
$this->toODS();
|
||||
break;
|
||||
case "html":
|
||||
$this->toHTML();
|
||||
break;
|
||||
case "csv":
|
||||
default:
|
||||
$this->toCSV();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private function toODS() {
|
||||
$ods = new ods();
|
||||
$styleColumn = new odsStyleTableColumn();
|
||||
$styleColumn->setUseOptimalColumnWidth(true);
|
||||
$headerstyle = new odsStyleTableCell();
|
||||
$headerstyle->setFontWeight("bold");
|
||||
$table = new odsTable($this->title);
|
||||
|
||||
for ($i = 0; $i < count($this->header); $i++) {
|
||||
$table->addTableColumn(new odsTableColumn($styleColumn));
|
||||
}
|
||||
|
||||
$row = new odsTableRow();
|
||||
foreach ($this->header as $cell) {
|
||||
$row->addCell(new odsTableCellString($cell, $headerstyle));
|
||||
}
|
||||
$table->addRow($row);
|
||||
|
||||
foreach ($this->data as $cols) {
|
||||
$row = new odsTableRow();
|
||||
foreach ($cols as $cell) {
|
||||
$row->addCell(new odsTableCellString($cell));
|
||||
}
|
||||
$table->addRow($row);
|
||||
}
|
||||
$ods->addTable($table);
|
||||
// The @ is a workaround to silence the tempnam notice,
|
||||
// which breaks the file. This is apparently the intended behavior:
|
||||
// https://bugs.php.net/bug.php?id=69489
|
||||
@$ods->downloadOdsFile($this->title . "_" . date("Y-m-d_Hi") . ".ods");
|
||||
}
|
||||
|
||||
private function toHTML() {
|
||||
global $SECURE_NONCE;
|
||||
$data = array_merge([$this->header], $this->data);
|
||||
// HTML exporter doesn't like null values
|
||||
for ($i = 0; $i < count($data); $i++) {
|
||||
for ($j = 0; $j < count($data[$i]); $j++) {
|
||||
if (is_null($data[$i][$j])) {
|
||||
$data[$i][$j] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
header('Content-type: text/html');
|
||||
$converter = new HTMLConverter();
|
||||
$out = "<!DOCTYPE html>\n"
|
||||
. "<meta charset=\"utf-8\">\n"
|
||||
. "<meta name=\"viewport\" content=\"width=device-width\">\n"
|
||||
. "<title>" . $this->title . "_" . date("Y-m-d_Hi") . "</title>\n"
|
||||
. <<<STYLE
|
||||
<style nonce="$SECURE_NONCE">
|
||||
.table-csv-data {
|
||||
border-collapse: collapse;
|
||||
}
|
||||
.table-csv-data tr:first-child {
|
||||
font-weight: bold;
|
||||
}
|
||||
.table-csv-data tr td {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
STYLE
|
||||
. $converter->convert($data);
|
||||
echo $out;
|
||||
}
|
||||
|
||||
private function toCSV() {
|
||||
$csv = Writer::createFromString('');
|
||||
$data = array_merge([$this->header], $this->data);
|
||||
$csv->insertAll($data);
|
||||
header('Content-type: text/csv');
|
||||
header('Content-Disposition: attachment; filename="' . $this->title . "_" . date("Y-m-d_Hi") . ".csv" . '"');
|
||||
echo $csv;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
|
||||
// Detect if loaded by the user or by PHP
|
||||
if (count(get_included_files()) == 1) {
|
||||
define("LOADED", true);
|
||||
} else {
|
||||
define("LOADED", false);
|
||||
}
|
||||
|
||||
require_once __DIR__ . "/../required.php";
|
||||
|
||||
dieifnotloggedin();
|
||||
|
||||
if (LOADED) {
|
||||
if (isset($VARS['type']) && isset($VARS['format'])) {
|
||||
generateReport($VARS['type'], $VARS['format']);
|
||||
die();
|
||||
} else {
|
||||
$Strings->get("invalid parameters");
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a 2d array of the families in the database.
|
||||
* @global type $database
|
||||
* @param array $filter Medoo WHERE clause.
|
||||
* @return string
|
||||
*/
|
||||
function getMemberReport($filter = []): Report {
|
||||
global $database, $Strings;
|
||||
|
||||
if (empty($filter)) {
|
||||
$report = new Report($Strings->get("Families", false));
|
||||
$filter = ["ORDER" => ["familyname" => "ASC"]];
|
||||
} else {
|
||||
$report = new Report($Strings->get("Expiring Memberships", false));
|
||||
}
|
||||
|
||||
$familyids = $database->select(
|
||||
"families", "familyid", $filter
|
||||
);
|
||||
|
||||
$report->setHeader([
|
||||
$Strings->get("Name", false),
|
||||
$Strings->get("Father", false),
|
||||
$Strings->get("Mother", false),
|
||||
$Strings->get("Phone", false),
|
||||
$Strings->get("Email", false),
|
||||
$Strings->get("Address", false),
|
||||
$Strings->get("City", false),
|
||||
$Strings->get("State", false),
|
||||
$Strings->get("ZIP", false),
|
||||
$Strings->get("Photo Permission", false),
|
||||
$Strings->get("Newsletter", false),
|
||||
$Strings->get("Expires", false),
|
||||
$Strings->get("Children", false)
|
||||
]);
|
||||
$families = [];
|
||||
foreach ($familyids as $id) {
|
||||
$families[] = (new Family())->load($id);
|
||||
}
|
||||
foreach ($families as $f) {
|
||||
$newsletter = "";
|
||||
switch ($f->getNewsletter()) {
|
||||
case 1:
|
||||
$newsletter = $Strings->get("Email", false);
|
||||
break;
|
||||
case 2:
|
||||
$newsletter = $Strings->get("Print", false);
|
||||
break;
|
||||
case 3:
|
||||
$newsletter = $Strings->get("Email+Print", false);
|
||||
break;
|
||||
}
|
||||
$children = [];
|
||||
foreach ($f->getChildren() as $c) {
|
||||
$children[] = $c->getName() . " (" . date("n/d", $c->getBirthday()) . ")";
|
||||
}
|
||||
$report->addDataRow([
|
||||
$f->getName(),
|
||||
$f->getFather(),
|
||||
$f->getMother(),
|
||||
$f->getPhone() . "",
|
||||
$f->getEmail(),
|
||||
$f->getAddress(),
|
||||
$f->getCity(),
|
||||
$f->getState(),
|
||||
$f->getZip() . "",
|
||||
$f->getPhotoPermission() ? $Strings->get("Yes", false) : $Strings->get("No", false),
|
||||
$newsletter,
|
||||
date("Y-m-d", $f->getExpires()),
|
||||
implode(", ", $children)
|
||||
]);
|
||||
}
|
||||
return $report;
|
||||
}
|
||||
|
||||
function getReport($type): Report {
|
||||
switch ($type) {
|
||||
case "members":
|
||||
return getMemberReport();
|
||||
break;
|
||||
case "expiring":
|
||||
return getMemberReport(["expires[<]" => date("Y-m-d", strtotime("+1 month")), "ORDER" => ["expires" => "ASC"]]);
|
||||
break;
|
||||
default:
|
||||
return new Report("error", ["ERROR"], ["Invalid report type."]);
|
||||
}
|
||||
}
|
||||
|
||||
function generateReport($type, $format) {
|
||||
$report = getReport($type);
|
||||
$report->output($format);
|
||||
}
|
Loading…
Reference in New Issue