From 37e81bff8402c049e9d6e4b5cd6bc5c47b2e28f2 Mon Sep 17 00:00:00 2001
From: Benjamin Renard <benjamin.renard@akka.eu>
Date: Tue, 15 Sep 2015 13:02:10 +0200
Subject: [PATCH] Direct save of plots in interactive session

---
 js/app/views/PlotTabResultUI.js | 53 ++++++++++++++++++++++++++++++++++++++++++++---------
 php/downloadPlot.php            | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 9 deletions(-)
 create mode 100644 php/downloadPlot.php

diff --git a/js/app/views/PlotTabResultUI.js b/js/app/views/PlotTabResultUI.js
index ba95057..cf10a97 100644
--- a/js/app/views/PlotTabResultUI.js
+++ b/js/app/views/PlotTabResultUI.js
@@ -31,6 +31,8 @@ Ext.define('amdaUI.PlotTabResultUI', {
     isTTNavBar : false,
     crtTTFileIndex : 0,
     
+    hiddenForm: null,
+    
     constructor: function(config) {
         this.addEvents({'pagesize':true});
         
@@ -285,15 +287,48 @@ Ext.define('amdaUI.PlotTabResultUI', {
 				if (me.contextualMenu.items.getCount() > 0)
 					me.contextualMenu.add('-');
 					
-				me.contextualMenu.add({
-					text:'Extend/Shift Time request',
-					handler : function ()
-					{
-						var extendShiftPlugin = this.getPlugin('plot-extendshift-plugin-id');
-						extendShiftPlugin.show(me.tabId);
-					},
-					scope: me
-				});
+				me.contextualMenu.add([
+				                       {
+				                    	   text:'Extend/Shift Time request',
+				                    	   handler : function ()
+				                    	   {
+				                    		   var extendShiftPlugin = this.getPlugin('plot-extendshift-plugin-id');
+				                    		   extendShiftPlugin.show(me.tabId);
+				                    	   },
+				                    	   scope: me
+				                       },
+				                       '-',
+				                       {
+				                    	   text: 'Save Plot',
+				                    	   handler : function ()
+				                    	   {
+				                    		   if (me.hiddenForm == null)
+				                    			   me.hiddenForm = Ext.create('Ext.form.Panel', {
+				                    				   title:'hiddenForm',
+				                    				   renderTo: Ext.getBody(),
+				                    				   standardSubmit: true,
+				                    				   url: 'php/downloadPlot.php',
+				                    				   timeout: 120000,
+				                    				   height:100,
+				                    				   width: 100,
+				                    				   hidden:true,
+				                    				   items:[
+				                    				   ]
+				                    			   });
+
+				                    		   me.hiddenForm.getForm().submit({
+				                    			   params: {
+				                    				   sessionId: sessionID,
+				                    				   tabId : me.tabId
+				                    			   },
+				                                   success: function(form, action) {
+				                                   },
+				                                   failure: function(form, action) {
+				                                   }
+				                    		   });
+				                    	   }
+				                       }
+				]);
 				
 				me.contextualMenu.showAt(absoluteX, absoluteY);
 			}
diff --git a/php/downloadPlot.php b/php/downloadPlot.php
new file mode 100644
index 0000000..7e6889f
--- /dev/null
+++ b/php/downloadPlot.php
@@ -0,0 +1,67 @@
+<?php
+
+require_once 'config.php';
+
+if (!isset($_POST['sessionId']))
+{
+	header("HTTP/1.0 400 Bad Request");
+	echo json_encode(array("success" => false, "error" => "Unknown session Id"));
+	exit;
+}
+$sessionId = $_POST['sessionId'];
+
+if (!isset($_POST['tabId']))
+{
+	header("HTTP/1.0 400 Bad Request");
+	echo json_encode(array("success" => false, "error" => "Unknown tab Id"));
+	exit;
+}
+$tabId     = $_POST['tabId'];
+
+download_plot($sessionId, $tabId);
+
+function download_plot($sessionId, $tabId)
+{
+	// Must be fresh start
+	if( headers_sent() )
+	{
+		header("HTTP/1.0 400 Bad Request");
+		echo json_encode(array("success" => false, "error" => "Headers Sent"));
+		return;
+	}
+
+	// Required for some browsers
+	if(ini_get('zlib.output_compression'))
+		ini_set('zlib.output_compression', 'Off');
+
+	//Build file path
+	$fullPath = USERPATH."/".$sessionId."/RES/Plot_/".$tabId.".png";
+	// File Exists?
+	if( file_exists($fullPath) ){
+
+		// Parse Info / Get Extension
+		$fsize = filesize($fullPath);
+		$path_parts = pathinfo($fullPath);
+		$ctype="image/png";
+		
+		header("Pragma: public"); // required
+		header("Expires: 0");
+		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
+		header("Cache-Control: private",false); // required for certain browsers
+		header("Content-Type: $ctype");
+		header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
+		header("Content-Transfer-Encoding: binary");
+		header("Content-Length: ".$fsize);
+		ob_clean();
+		flush();
+		readfile( $fullPath );
+
+	} else
+	{
+		header("HTTP/1.0 400 Bad Request");
+		echo json_encode(array("success" => false, "error" => "No existing plot"));
+		return;
+	}
+}
+
+?>
\ No newline at end of file
--
libgit2 0.21.2