Blame view

src/InputOutput/IHMImpl/TimeTables/IHMInputOutputTTClass.php 5.63 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

		$sendToSamp = isset($input->sendToSamp) && $input->sendToSamp;
		$compression = !$sendToSamp && ($input->compression !== 'none');
a0d6e953   Benjamin Renard   Update TT/Catalog...
29
30
31
		$formatstr = $sendToSamp ? 'vot' : $input->fileformat;
		$tmp = new TTRequestDataClass(); // just to be sure that TTRequestDataClass is loaded with the 'autoload' => TTRequestDataClass is defined
		$outputformat = $this->getFormatFromString(!empty($formatstr) ? $formatstr : 'vot');
0b6b2080   Elena.Budnik   TT download
32
33
34
35
36
37
38
			
		$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);
		}
a0d6e953   Benjamin Renard   Update TT/Catalog...
39

0b6b2080   Elena.Budnik   TT download
40
41
42
43
		foreach($input->list as $tt)
		{
			//create a list of data to treat
			$processData = new TTRequestDataClass();
a0d6e953   Benjamin Renard   Update TT/Catalog...
44
45
46
47
48
49

			if (!isset($tt->id) && !empty($tt->path))  {
				$ttName = $tt->path;
				$inputformat = $this->getFormatFromString(!empty($tt->format) ? $tt->format : 'internal');
			}
			else if (strpos($tt->id,"sharedtimeTable_") === 0 || strpos($tt->id,"sharedcatalog_") === 0) {
946ea36f   Elena.Budnik   redmine 6884 shar...
50
51
52
				//Shared TimeTable /Catalog
				$objectType = strpos($tt->id,"timeTable") != 0 ? "timeTable" : "catalog";
				$result = IHMSharedObjectsClass::getPath($objectType, $tt->id);
881bd225   Benjamin Renard   Add shared TT sup...
53
				if (!$result['success'])
946ea36f   Elena.Budnik   redmine 6884 shar...
54
					throw new Exception('Shared '.$objectType.' '.$tt->name.' not found');
881bd225   Benjamin Renard   Add shared TT sup...
55
				$ttName = $result['path'];
a0d6e953   Benjamin Renard   Update TT/Catalog...
56
				$inputformat = TTRequestFileFormatEnum::INTERNAL;
881bd225   Benjamin Renard   Add shared TT sup...
57
58
			}
			else {
946ea36f   Elena.Budnik   redmine 6884 shar...
59
				//User TimeTable / Catalog
881bd225   Benjamin Renard   Add shared TT sup...
60
				$ttName = IHMConfigClass::getUserTTPath().$tt->id.'.xml';
a0d6e953   Benjamin Renard   Update TT/Catalog...
61
				$inputformat = TTRequestFileFormatEnum::INTERNAL;
881bd225   Benjamin Renard   Add shared TT sup...
62
			}
0b6b2080   Elena.Budnik   TT download
63
			if (!file_exists($ttName))
a0d6e953   Benjamin Renard   Update TT/Catalog...
64
				throw new Exception('TimeTable or Catalog '.$tt->name.' not found');
0b6b2080   Elena.Budnik   TT download
65
			$processData->setInputFileName($ttName);
a0d6e953   Benjamin Renard   Update TT/Catalog...
66
			$processData->setInputFileFormat($inputformat);			 
0b6b2080   Elena.Budnik   TT download
67
			$processData->setOutputDir(IHMConfigClass::getDownloadTmpPath());
a0d6e953   Benjamin Renard   Update TT/Catalog...
68
			$processData->setOutputFileFormat($outputformat);
0b6b2080   Elena.Budnik   TT download
69
			
a0d6e953   Benjamin Renard   Update TT/Catalog...
70
71
			switch ($outputformat) {
				case TTRequestFileFormatEnum::VOTABLE :	
0b6b2080   Elena.Budnik   TT download
72
73
					$outputName = $tt->name.'.xml';
					break;
a0d6e953   Benjamin Renard   Update TT/Catalog...
74
75
76
				case TTRequestFileFormatEnum::ASCII :
					$outputName = $tt->name.'.txt';
					break;
0b6b2080   Elena.Budnik   TT download
77
				default    :
a0d6e953   Benjamin Renard   Update TT/Catalog...
78
					$outputName = $tt->name.'.xml';
0b6b2080   Elena.Budnik   TT download
79
80
81
82
83
84
85
			}	
			
		//	$processData->setOutputTimeFormat($input->timeformat);
			$processData->setOutputFileName($outputName);
			$final .= " ".$outputName;
			$processData->setType($function);
			$processData->setCmd("ttConversion");
7d33380d   Benjamin Renard   Send TT and catal...
86
			$processData->setSendToSamp($sendToSamp);
0b6b2080   Elena.Budnik   TT download
87
88
89
90
91
92
93
									
			$this->processDatas[] = $processData;
		}
		
		$this->postProcessTT = new PostProcessTTClass();
		$this->postProcessTT->setOutputDirAlias($this->processDatas[0]->getOutputDir());
		
7d33380d   Benjamin Renard   Send TT and catal...
94
		if ($compression) {
0b6b2080   Elena.Budnik   TT download
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
			$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;
	}

a0d6e953   Benjamin Renard   Update TT/Catalog...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
	/*
	 * @brief Get format from input string
	 */
	private function getFormatFromString($str) {
		switch (strtolower($str)) {
			case 'vot':
			case 'votable':
				return TTRequestFileFormatEnum::VOTABLE;
			case 'ascii':
			case 'text':
			case 'txt':
			case 'asc':
				return TTRequestFileFormatEnum::ASCII;
		}
		return TTRequestFileFormatEnum::INTERNAL;
	}

0b6b2080   Elena.Budnik   TT download
133
134
135
136
137
138
139
/*
	* @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 ...
140
141
		      $timeStart = time();
		      
0b6b2080   Elena.Budnik   TT download
142
143
			$cmd = $this->postProcessTT->getPostCmd();			
			system($cmd);
ddb6bf06   Elena.Budnik   wait for process ...
144
145
146
147
			
		 	while (!file_exists($this->postProcessTT->getOutputFileName()) && ( time() - $timeStart ) < KernelConfigClass::getTimeToBatchMode())
						sleep(1);	 
				 		 	                     
0b6b2080   Elena.Budnik   TT download
148
149
150
151
152
153
154
155
156
157
158
159
160
161
			
			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...
162
163
164
165
166
167
168
169
		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
170
171
172
173
174
175
176
177
178
179
		else {
			$result  = array('success'=>$data[0]->getSuccess(), 
				'download' => $this->postProcessTT->getOutputDirAlias().$data[0]->getOutputFileName());
		}
				 		
		return $result;
	}
} 

?>