201 lines
9.4 KiB
PHP
201 lines
9.4 KiB
PHP
<?php
|
|
|
|
namespace HermannTelser\Gutachten\Uploads;
|
|
|
|
use ForceUTF8\Encoding;
|
|
use HermannTelser\Gutachten\DB\DbFunction;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
class UploadNutzflaechen
|
|
{
|
|
/**
|
|
* @return false
|
|
*/
|
|
public static function upload()
|
|
{
|
|
// DbFunction::$debug = 1;
|
|
|
|
$phpFileUploadErrors = [
|
|
0 => 'There is no error, the file uploaded with success',
|
|
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
|
|
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
|
|
3 => 'The uploaded file was only partially uploaded',
|
|
4 => 'No file was uploaded',
|
|
6 => 'Missing a temporary folder',
|
|
7 => 'Failed to write file to disk.',
|
|
8 => 'A PHP extension stopped the file upload.',
|
|
];
|
|
|
|
$userid = $GLOBALS['TSFE']->fe_user->user['uid'];
|
|
$projektid = GeneralUtility::_GP('pid');
|
|
|
|
\define('SITE_ROOT', realpath(__DIR__));
|
|
|
|
$files = glob(SITE_ROOT.'/uploaded_files/*'); // get all file names
|
|
foreach ($files as $file) { // iterate files
|
|
if (is_file($file)) {
|
|
unlink($file);
|
|
} // delete file
|
|
}
|
|
$response = [];
|
|
$response['message'] = '<h3>Nutzflächen</h3>';
|
|
// $response['message'] = '<i class="fa fa-exclamation-triangle rot"></i> <b>Falscher Dokumenteninhalt für Nutzflächen .</b>';
|
|
// return $response;
|
|
|
|
if (isset($_FILES['uploadedfile']) && UPLOAD_ERR_OK == $_FILES['uploadedfile']['error']) {
|
|
$fileTmpPath = $_FILES['uploadedfile']['tmp_name'];
|
|
$fileName = $_FILES['uploadedfile']['name'];
|
|
$fileSize = $_FILES['uploadedfile']['size'];
|
|
$fileType = $_FILES['uploadedfile']['type'];
|
|
$fileNameCmps = explode('.', $fileName);
|
|
$fileExtension = strtolower(end($fileNameCmps));
|
|
// sanitize file-name
|
|
$newFileName = md5(time().$fileName).'.'.$fileExtension;
|
|
// check if file has one of the following extensions
|
|
//$allowedfileExtensions = array('jpg', 'gif', 'png', 'zip', 'txt', 'xls', 'doc','rtf','csv');
|
|
$allowedfileExtensions = ['csv', 'txt'];
|
|
if (\in_array($fileExtension, $allowedfileExtensions)) {
|
|
// directory in which the uploaded file will be moved
|
|
$uploadFileDir = SITE_ROOT.'/uploaded_files/';
|
|
$dest_path = $uploadFileDir.$newFileName;
|
|
if (move_uploaded_file($fileTmpPath, $dest_path)) {
|
|
if (DbFunction::$debug) {
|
|
echo "Die Datei wurde erfolgreich hochgeladen.\n";
|
|
}
|
|
// DbFunction::delete('tx_gutachten_allgemein', ['pid' => $projektid]);
|
|
|
|
$file = file_get_contents($dest_path);
|
|
// echo $file; exit;
|
|
$file = Encoding::toUTF8($file, Encoding::ICONV_TRANSLIT);
|
|
$arr = preg_split('/\r\n|\r|\n/', $file);
|
|
|
|
// Order by topNr
|
|
$_ordered = [];
|
|
foreach ($arr as $key => $value) {
|
|
$row = explode('|', $value);
|
|
$result = array_map('trim', $row);
|
|
if (\count($result) > 4) {
|
|
$response['message'] = 'Mehr als Vier Spalten im Dokument.';
|
|
$response['class'] = 'error';
|
|
|
|
return $response;
|
|
} elseif (\count($result) < 4) {
|
|
$response['message'] = 'Weniger als Vier Spalten im Dokument.';
|
|
$response['class'] = 'error';
|
|
|
|
return $response;
|
|
}
|
|
if ('' != $value) {
|
|
$topNr = $result[0];
|
|
if ('' != $topNr) {
|
|
$_ordered[$topNr][] = $result;
|
|
}
|
|
}
|
|
}
|
|
|
|
$sum_raume_all_vorher = DbFunction::count('tx_gutachten_raume_all', ['pid' => $projektid]);
|
|
|
|
$_raeume = [];
|
|
foreach ($_ordered as $topNr => $daten) {
|
|
$tt_raume_basis_id = DbFunction::get('tx_gutachten_raume_basis', 'id', ['AND' => ['pid' => $projektid, 'topNr' => $topNr]]);
|
|
|
|
if (!$tt_raume_basis_id) {
|
|
$insert_array = [];
|
|
$insert_array['pid'] = $projektid;
|
|
$insert_array['uid'] = $userid;
|
|
$insert_array['topNr'] = $topNr;
|
|
$tt_raume_basis_id = DbFunction::secureInsert('tx_gutachten_raume_basis', $insert_array);
|
|
}
|
|
|
|
// tx_gutachten_raume_all
|
|
foreach ($daten as $key => $value) {
|
|
$geschoss = $value[1];
|
|
$raum = $value[2];
|
|
$flaeche = $value[3];
|
|
$flaeche = str_replace(',', '.', $flaeche);
|
|
$_raeume[] = $raum;
|
|
|
|
$tmp = DbFunction::select('tx_gutachten_raume_all', 'id', ['AND' => ['pid' => $projektid, 'raum' => $raum, 'tt_raume_basis_id' => $tt_raume_basis_id]]);
|
|
if (!\count($tmp)) {
|
|
$insert_array = [];
|
|
$insert_array['tt_raume_basis_id'] = $tt_raume_basis_id;
|
|
$insert_array['uid'] = $userid;
|
|
$insert_array['pid'] = $projektid;
|
|
$insert_array['geschoss'] = $geschoss;
|
|
$insert_array['raum'] = $raum;
|
|
$insert_array['qm'] = $flaeche;
|
|
DbFunction::secureInsert('tx_gutachten_raume_all', $insert_array);
|
|
} else {
|
|
$update_array = [];
|
|
$update_array['qm'] = $flaeche;
|
|
$update_array['geschoss'] = $geschoss;
|
|
DbFunction::secureUpdate('tx_gutachten_raume_all', $update_array, ['id' => $tmp[0]]);
|
|
}
|
|
}
|
|
}
|
|
$_raeume = array_unique($_raeume);
|
|
|
|
$_obsolet = [];
|
|
$tmp = DbFunction::select('tx_gutachten_raume_generell', 'raum', ['pid' => $projektid]);
|
|
|
|
foreach ($tmp as $key => $raum) {
|
|
if (!\in_array($raum, $_raeume)) {
|
|
$_obsolet[] = $raum;
|
|
}
|
|
}
|
|
foreach ($_obsolet as $key => $raum) {
|
|
DbFunction::delete('tx_gutachten_raume_generell', ['AND' => ['pid' => $projektid, 'raum' => $raum]]);
|
|
}
|
|
|
|
foreach ($_raeume as $key => $raum) {
|
|
$tmp = DbFunction::select('tx_gutachten_raume_generell', ['id', 'raum'], ['AND' => ['pid' => $projektid, 'raum' => $raum], 'ORDER' => 'id']);
|
|
if (0 == \count($tmp)) {
|
|
$insert_array = [];
|
|
$insert_array['pid'] = $projektid;
|
|
$insert_array['uid'] = $userid;
|
|
$insert_array['raum'] = $raum;
|
|
DbFunction::secureInsert('tx_gutachten_raume_generell', $insert_array);
|
|
} elseif (\count($tmp) > 1) {
|
|
foreach ($tmp as $k => $v) {
|
|
if ($k > 0) {
|
|
DbFunction::delete('tx_gutachten_raume_generell', ['id' => $v['id']]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$sum_raume_all_nachher = DbFunction::count('tx_gutachten_raume_all', ['pid' => $projektid]);
|
|
|
|
$response['message'] .= "<b>Die Datei wurde erfolgreich hochgeladen.</b>";
|
|
$response['message'] .= "<br>Summe Objekte vorher: $sum_raume_all_vorher";
|
|
$response['message'] .= "; nachher: $sum_raume_all_nachher";
|
|
$diff = abs($sum_raume_all_vorher - $sum_raume_all_nachher);
|
|
$response['message'] .= "; <b>Differenz: $diff </b>";
|
|
$response['class'] = 'ok';
|
|
|
|
return $response;
|
|
}
|
|
$response['message'] = $phpFileUploadErrors[$_FILES['uploadedfile']['error']];
|
|
$response['file'] = $uploadfile;
|
|
$response['class'] = 'error';
|
|
$response['dir'] = __DIR__;
|
|
$response['tmp'] = sys_get_temp_dir();
|
|
|
|
return $response;
|
|
}
|
|
$response['message'] = 'Dokument muss txt oder csv sein';
|
|
$response['class'] = 'error';
|
|
|
|
return $response;
|
|
}
|
|
$response['message'] = $phpFileUploadErrors[$_FILES['uploadedfile']['error']];
|
|
$response['file'] = $_FILES['uploadedfile']['name'];
|
|
$response['class'] = 'error';
|
|
$response['dir'] = __DIR__;
|
|
$response['tmp'] = sys_get_temp_dir();
|
|
$response['time'] = time();
|
|
|
|
return $response;
|
|
}
|
|
}
|