Blame view

src/InputOutput/IHMImpl/TimeTables/IHMInputOutputTTClass.php 4.72 KB
0b6b2080   Elena.Budnik   TT download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

/**
 * @class IHMInputOutputTTClass
 * @brief 
 * @details
*/
class IHMInputOutputTTClass implements InputOutputInterface
{
 
/*
	* @brief Constructor
*/
	function __construct()
	{
		 
	}

/*
	* @brief translate input data from IHM client to AMDA_Integration module for a tt process request
*/
	public function getInputData($input, $function, $requestId="")
	{
		if (isset($this->processDatas))
			unset($this->processDatas);
7d33380d   Benjamin Renard   Send TT and catal...
26
27
28
29

		$sendToSamp = isset($input->sendToSamp) && $input->sendToSamp;
		$compression = !$sendToSamp && ($input->compression !== 'none');
		$format = $sendToSamp ? 'vot' : $input->fileformat;
0b6b2080   Elena.Budnik   TT download
30
31
32
33
34
35
36
37
38
39
40
41
			
		$final = "";
		
		if (is_dir(IHMConfigClass::getDownloadTmpPath())) {
			foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.zip') as $filename)  unlink($filename);
			foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.tar.gz') as $filename)  unlink($filename);
		}
		
		foreach($input->list as $tt)
		{
			//create a list of data to treat
			$processData = new TTRequestDataClass();
946ea36f   Elena.Budnik   redmine 6884 shar...
42
43
44
45
46
			
			if (strpos($tt->id,"sharedtimeTable_") === 0 || strpos($tt->id,"sharedcatalog_") === 0) {
				//Shared TimeTable /Catalog
				$objectType = strpos($tt->id,"timeTable") != 0 ? "timeTable" : "catalog";
				$result = IHMSharedObjectsClass::getPath($objectType, $tt->id);
881bd225   Benjamin Renard   Add shared TT sup...
47
				if (!$result['success'])
946ea36f   Elena.Budnik   redmine 6884 shar...
48
					throw new Exception('Shared '.$objectType.' '.$tt->name.' not found');
881bd225   Benjamin Renard   Add shared TT sup...
49
50
51
				$ttName = $result['path'];
			}
			else {
946ea36f   Elena.Budnik   redmine 6884 shar...
52
				//User TimeTable / Catalog
881bd225   Benjamin Renard   Add shared TT sup...
53
54
				$ttName = IHMConfigClass::getUserTTPath().$tt->id.'.xml';
			}
0b6b2080   Elena.Budnik   TT download
55
56
57
58
59
60
			if (!file_exists($ttName))
				throw new Exception('TimeTable '.$tt->name.' not found');
			$processData->setInputFileName($ttName);
			$processData->setInputFileFormat(TTRequestFileFormatEnum::INTERNAL);			 
			$processData->setOutputDir(IHMConfigClass::getDownloadTmpPath());
			
7d33380d   Benjamin Renard   Send TT and catal...
61
			switch ($format) {
0b6b2080   Elena.Budnik   TT download
62
63
64
65
66
67
68
69
70
71
72
73
74
75
				case 'vot' :	
					$processData->setOutputFileFormat(TTRequestFileFormatEnum::VOTABLE);
					$outputName = $tt->name.'.xml';
					break;
				default    :
					$processData->setOutputFileFormat(TTRequestFileFormatEnum::ASCII);
					$outputName = $tt->name;
			}	
			
		//	$processData->setOutputTimeFormat($input->timeformat);
			$processData->setOutputFileName($outputName);
			$final .= " ".$outputName;
			$processData->setType($function);
			$processData->setCmd("ttConversion");
7d33380d   Benjamin Renard   Send TT and catal...
76
			$processData->setSendToSamp($sendToSamp);
0b6b2080   Elena.Budnik   TT download
77
78
79
80
81
82
83
									
			$this->processDatas[] = $processData;
		}
		
		$this->postProcessTT = new PostProcessTTClass();
		$this->postProcessTT->setOutputDirAlias($this->processDatas[0]->getOutputDir());
		
7d33380d   Benjamin Renard   Send TT and catal...
84
		if ($compression) {
0b6b2080   Elena.Budnik   TT download
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
			$this->postProcessTT->compression = true;
			$archive = "Timetable_archive_".date('Ymd_Hi');
			switch ($input->compression) {
				case 'zip' :
					$postCmd = "zip -Dj ".$archive.".zip ".$final." 1> /dev/null 2> /dev/null";
					$outputFileName = $archive.".zip";					
					break;
				case 'tar+gzip':				       
				default :
					$postCmd="tar cfz ".$archive.".tar.gz --directory=".$this->processDatas[0]->getOutputDir()." ".$final;
					$outputFileName = $archive.".tar.gz";
					break;				
			}				
			$this->postProcessTT->setPostCmd($postCmd);
			$this->postProcessTT->setOutputFileName($outputFileName);
			$this->postProcessTT->setOutputDir($this->processDatas[0]->getOutputDir());			
		}
		
		return $this->processDatas;
	}

/*
	* @brief translate output data from AMDA_Integration module to IHM client  request
*/
	public function getOutput($data)
	{
		if ( $this->postProcessTT->compression ) {
		
ddb6bf06   Elena.Budnik   wait for process ...
113
114
		      $timeStart = time();
		      
0b6b2080   Elena.Budnik   TT download
115
116
			$cmd = $this->postProcessTT->getPostCmd();			
			system($cmd);
ddb6bf06   Elena.Budnik   wait for process ...
117
118
119
120
			
		 	while (!file_exists($this->postProcessTT->getOutputFileName()) && ( time() - $timeStart ) < KernelConfigClass::getTimeToBatchMode())
						sleep(1);	 
				 		 	                     
0b6b2080   Elena.Budnik   TT download
121
122
123
124
125
126
127
128
129
130
131
132
133
134
			
			foreach ($data as $d)
			{
				$rawFileName = $d->getOutputDir().$d->getOutputFileName();				
				if (file_exists($rawFileName)) 							 
					unlink($rawFileName);
			}
			
			$result  = array('success'=>true,
				'download'=>$this->postProcessTT->getOutputDirAlias().$this->postProcessTT->getOutputFileName(), 
				'compression' => $this->postProcessTT->compression);
				
			
		}
7d33380d   Benjamin Renard   Send TT and catal...
135
136
137
138
139
140
141
142
		else if ($data[0]->getSendToSamp()) {
			$donwload = array();
			foreach ($data as $d) {
				$donwload[] = $this->postProcessTT->getOutputDirAlias().$d->getOutputFileName();
			}
			$result  = array('success'=>$data[0]->getSuccess(), 'sendToSamp' => TRUE,
				'download' => $donwload);
		}
0b6b2080   Elena.Budnik   TT download
143
144
145
146
147
148
149
150
151
152
		else {
			$result  = array('success'=>$data[0]->getSuccess(), 
				'download' => $this->postProcessTT->getOutputDirAlias().$data[0]->getOutputFileName());
		}
				 		
		return $result;
	}
} 

?>