Select Git revision
relay_gen_java.py
Forked from
Anders Blomdell / LabComm
Source project has a limited visibility.
class.RegisterUtils.php 5.30 KiB
<?php
class RegisterUtils {
private static $spreadsheetName = '00spreadsheet.csv';
public static $mandatoryKeys = array('last', 'first', 'email');
public static $optionalKeys = array('affiliation','vat','invoice',
'constraints');
private $mealKeys = array();
private $selectedBoxes = array();
private $checkBoxes = array();
public $errorString = '';
private $directory = '';
private $mandatory = array();
private $optional = array();
public $values = array();
function __construct($directory = NULL) {
clearstatcache();
$this->errorString='';
if (!$directory) {
$this->errorString = 'No result directory specified';
return;
}
//echo '<pre>'; print_r($directory); echo'</pre>';
if (is_dir($directory) && is_writable($directory)) {
} else {
$this->errorString = sprintf('Directory %s not writable!',$directory);
return;
}
$this->directory = $directory;
}
public function getMealKeys() {
return $this->mealKeys;
}
public function getSelectedBoxes(){
return $this->selectedBoxes;
}
public function getCheckBoxes() {
return $this->checkBoxes;
}
public function valuesFromParams($params) {
$mealString = trim($params['meals']);
if ($mealString) {
$this->mealKeys = explode('|',$mealString);
}
foreach (self::$mandatoryKeys as $key) {
$this->values[$key] = '';
if(isset($params[$key])) {
$val = htmlspecialchars_decode(trim($params[$key]));
$this->mandatory[$key] = $val;
$this->values[$key] = $val;
}
}
foreach (self::$optionalKeys as $key) {
$this->values[$key] = '';
if(isset($params[$key])) {
$val = htmlspecialchars_decode(trim($params[$key]));
$this->optional[$key] = $val;
$this->values[$key] = $val;
}
}
$i = 0;
foreach ($this->mealKeys as $key) {
$check = 'meal'.$i++;
$this->checkBoxes[$key] = $check;
$this->selectedBoxes[$key] = '';
if (isset($params[$check])) {
$res = $params[$check];
if ($res) $this->selectedBoxes[$key] = $res;
}
}
//echo '<pre>'; print_r($this->values); echo '</pre>';
}
public function getNumParticipants(){
$numParticipants = 0;
$files = scandir($this->directory);
foreach ($files as $f) {
if ($f == '.' || $f == '..') continue;
// ignore the spreadsheet file.
if ($f == self::$spreadsheetName) continue;
// ignore the MacOSX special files
$pos = strpos($f,'._');
if (($pos !== false) && ($pos == 0)) { continue; }
// end MacOSX
$numParticipants++;
}
return $numParticipants;
}
public function saveRegistration() {
$last = $this->mandatory{'last'};
$first = $this->mandatory{'first'};
$date = substr(date(DateTime::ISO8601),0,-5);
$filename = $this->directory.'/'.$last . '_' . $first . '_' . $date;
$contents = '';
foreach ($this->values as $key=>$value) {
if ($key == 'invoice') continue;
if ($value) $contents .= sprintf('%s: %s'.PHP_EOL,$key,$value);
}
foreach ($this->selectedBoxes as $key=>$value) {
if ($value) $contents .= sprintf('%s: %s'.PHP_EOL,$key,$value);
}
if($this->optional['invoice']) {
$contents .= 'invoice:'. PHP_EOL . $this->optional['invoice'].PHP_EOL;
}
$ret = file_put_contents($filename,$contents);
if ($ret === false) die('Cannot write '. $filename);
$this->contents = $contents;
}
public function makeSpreadsheet() {
$dirname = $this->directory;
$fileSpec = sprintf('%s/%s',$dirname,self::$spreadsheetName);
$outFile = fopen($fileSpec, 'w');
$allKeys = array_merge(self::$mandatoryKeys,self::$optionalKeys,
$this->mealKeys);
$outLine = join(';',$allKeys).PHP_EOL;
fwrite($outFile,$outLine);
$fileList = scandir($this->directory);
foreach ($fileList as $f) {
//fwrite($outFile,$f.PHP_EOL);
if (preg_match('|(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)|',$f,$matches)) {
} else {
continue;
}
// ignore the MacOSX special files
$pos = strpos($f,'._');
if (($pos !== false) && ($pos == 0)) { continue; }
// end MacOSX
$datetime = $matches[1];
$values = array();
$allKeys = array_merge(self::$mandatoryKeys, self::$optionalKeys,
$this->mealKeys);
foreach ($allKeys as $key) {
//fwrite($outFile,$key.PHP_EOL);
$values[$key] = '';
}
$fs = sprintf('%s/%s',$dirname,$f);
$lines = file($fs);
$readInvoice = false;
foreach ($lines as $line) {
if ($readInvoice) {
$values['invoice'] .= $line;
continue;
}
$line = rtrim($line);
list($key,$val) = explode(':',$line) + Array(null, null);
$val = ltrim($val);
if ($key == 'invoice') {
$readInvoice = true;
$values['invoice'] = '';
continue;
}
if (in_array($key,$allKeys)) {
$values[$key] = $val;
continue;
}
}
$fields = array(); $dquot = '"'; $ddquot = '""';
foreach ($allKeys as $key) {
$value = str_replace($dquot,$ddquot,$values[$key]);
$value = sprintf('"%s"',$value);
$fields[] = $value;
}
$outLine = join(';',$fields).PHP_EOL;
fwrite($outFile,$outLine);
}
fclose($outFile);
}
}
?>