Added linework export with progress bar

This commit is contained in:
Frank Schubert
2022-06-15 17:34:04 +02:00
parent c105f9926c
commit 1fddf682a1
6 changed files with 671 additions and 13 deletions

View File

@@ -3,7 +3,7 @@
class LineworkController extends mfBaseController {
protected function init() {
$this->needlogin=true;
$this->needlogin = true;
$me = mfValuecache::singleton()->get("me");
if(!$me) {
$me = new User();
@@ -32,14 +32,24 @@ class LineworkController extends mfBaseController {
$pagination['count'] = 20;
$pagination['maxItems'] = 0;
if($this->request->export) {
$this->layout()->setTemplate("Linework/export.xls");
$pagination = false;
}
if(is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
if($this->request->export) {
$this->layout()->setTemplate("Linework/export.xls");
$pagination = [];
}
if($this->request->export_progress) {
$this->layout()->setTemplate("Linework/export_progress.xls");
if($this->request->uid) {
$this->layout()->set("export_uid", $this->request->uid);
$this->layout()->set("path", BASEDIR."/var/temp/export");
}
$pagination = false;
}
//var_dump($pagination);exit;
$my_networks = [];
@@ -103,7 +113,6 @@ class LineworkController extends mfBaseController {
}
$networks = [];
$pagination['maxItems'] = TerminationModel::count($termination_search);
foreach(TerminationModel::search($termination_search, $pagination) as $term) {
if(!array_key_exists($term->building->network->name, $networks)) {
@@ -138,9 +147,7 @@ class LineworkController extends mfBaseController {
$this->layout()->set("networks", $networks);
$this->layout()->set("pagination", $pagination);
//var_dump($networks);exit;
}
private function getPreparedFilter($filter) {
@@ -165,6 +172,115 @@ class LineworkController extends mfBaseController {
return $new_filter;
}
public function startExport($request) {
$this->request = new mfRequest($request);
//var_dump($request);exit;
return $this->indexAction();
}
protected function exportAction() {
$uid = "wfExport_".uniqid()."-".mt_rand(10000,99999);
$method = "http";
if($_SERVER['HTTPS'] == "on") {
$method = "https";
}
$get = $this->request->get();
unset($get['mod']);
unset($get['action']);
$get['export_progress'] = 1;
$get['uid'] = $uid;
$url = $method."://".$_SERVER['HTTP_HOST'].$this->getUrl("Linework", "Index", $get);
$url = escapeshellarg($url);
//var_dump($url);exit;
$progress = new WorkflowExport();
$progress->uid = $uid;
$progress->create_by = $this->me->id;
$progress->edit_by = $this->me->id;
$progress->save();
/*
$cmd = "nohup curl ".$url." >/dev/null 2>&1 &";
var_dump($cmd);exit;
// start excel generation in background and dont wait
exec($cmd);
*/
$cmd = BASEDIR."/scripts/start_workflow_export.php";
$args = "";
foreach($get['filter'] as $key => $value) {
$args .= " ".escapeshellarg("--$key")." ". escapeshellarg($value);
}
$cmd = "$cmd --uid ". escapeshellarg($uid)." $args >/dev/null 2>&1 &";
exec($cmd);
// return progress
$this->returnJson(['status' => "OK", 'uid' => $uid, "progress" => 0]);
}
protected function exportProgressAction() {
$uid = $this->request->uid;
if(!$uid) {
$this->returnJson(['status' => "error", "msg" => "no uid"]);
}
$progress = new WorkflowExport();
$progress->loadByUid($this->request->uid);
if(!$progress->id) {
$this->returnJson(['status' => "error", "msg" => "export not found"]);
}
$this->returnJson(['status' => "OK", 'export_uid ' => $uid, 'progress' => $progress->progress]);
}
protected function downloadExportAction() {
$uid = $this->request->uid;
if(!$uid) {
$this->returnJson(['status' => "error", "msg" => "no uid"]);
}
$progress = new WorkflowExport();
$progress->loadByUid($this->request->uid);
if(!$progress->id) {
$this->returnJson(['status' => "error", "msg" => "export not found"]);
}
$file = BASEDIR."/var/temp/export/".$progress->filename;
if (!$fh = fopen($file, 'r')) {
$this->layout()->setFlash("Fehler beim Export.", "error");
$this->redirect("Linework");
return false;
}
set_time_limit(36000);
header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-disposition: attachment; filename="' . $progress->filename . '"');
$size = exec('stat -c %s '.escapeshellarg($file));
$this->log->debug("filename: ".$file);
$this->log->debug("filesize: ".$size);
if(strlen($size)) {
if($size < (pow(2,31))-1) {
header('Content-Length: ' . $size);
}
}
while (!feof($fh)) {
$data = fread($fh, 8192);
echo $data;
}
exit;
}
protected function saveAction() {
$r = $this->request;
//var_dump($r->get());exit;