3 * Name: ConversionManager class
\r
4 * Author: Antonio Gamba Bari - antonio.gambabari@utoronto.ca. ARTC, University of Toronto.
\r
5 * Last Update: July 22, 2009
\r
7 * Description: This class performs all the conversion functions
\r
8 * 1. Start a conversion
\r
9 * 2. Retrieve a caption file
\r
10 * 3. Load all caption formats inc files
\r
11 * 3. Autodetect caption format
\r
12 * 4. Build a Collection of Captions
\r
13 * 5. Call export method from the proper CaptionFormat sub-class
\r
16 class ConversionManager
\r
18 private $ccStringTarget; // Caption Target file as String
\r
19 private $ccSourceFileName; // File Name of the Source caption
\r
20 private $ccTargetExt; // File Extension of the caption file
\r
21 private $ccTypeSource; // Class name of the source caption format
\r
22 private $ccTypeTarget; // Class name of the target caption format
\r
23 private $ccFormats = Array(); // array containing all the class names of the supported caption formats
\r
24 private $ccTargetUrl; // relative path to target caption file
\r
25 public $ccImportedCollection; // The target CaptionCollection Object
\r
29 * Class Constructor: Receives a caption string, parameters and returns the target caption as string
\r
31 * @param String $theCaption The Caption File as String
\r
32 * @param String $theTarget The target Type = Class name of target sub-class
\r
33 * @param String $theFileName The name of the source caption file
\r
35 public function __construct($theCaption,$theTarget,$theFileName)
\r
37 global $rosettaCCSettings;
\r
41 // assign parameters to class members
\r
42 $this->ccTypeTarget = $theTarget;
\r
43 $this->ccSourceFileName = $theFileName;
\r
44 $this->ccTypeSource = ''; // initialize $ccTypeSource = ''
\r
46 // load caption formats
\r
47 $this->ccFormats = CcUtilVital::ccFormatsLoad();
\r
50 * run detection function on imported caption string
\r
51 * Note: the type of the source caption is not a parametter. This means that for stability
\r
52 * purposes the conversion tool MUST identify the format. Otherwhise, many unexpected and
\r
53 * incorrect conversions may occur
\r
55 $this->ccTypeSource = $this->_ccAutoDetect($theCaption);
\r
57 // proceed only if caption format is detected
\r
58 if($this->ccTypeSource!='')
\r
60 //echo '<br/><br/> Caption format Detected: <b>'.$this->ccTypeSource.'</b>';
\r
63 * Invoques import function in caption format subclass
\r
64 * Note: here we know the name of the Caption format class from the $this->ccTypeSource (the value assigned by the autodetection function)
\r
66 $ccSourceObj = new $this->ccTypeSource();
\r
68 // get a CaptionCollection Object
\r
69 $this->ccImportedCollection = $ccSourceObj->importCC($theCaption);
\r
71 //echo '<br/><br/>Uploaded Caption file was loaded into a CaptionCollection';
\r
73 // Initialize exported collection string
\r
74 $this->ccStringTarget = '';
\r
76 // run export function
\r
77 $this->ccStringTarget = $this->_ccExport();
\r
79 // print target format in a form for debuging purposes only
\r
80 //echo '<form><textarea name="textarea" cols="120" rows="10">'. $this->ccStringTarget . '</textarea></form>';
\r
82 //$this->_ccExport();
\r
84 // verify if the Target format has been provided
\r
85 if ($this->ccStringTarget!='')
\r
87 // Save collection in DB as serialized string
\r
88 //DbTools::collectionInBdSave($this->ccImportedCollection, $theFileName);
\r
90 // FINALLY !! if all goes right save exported caption into a file
\r
91 $theConvertedFile = $this->_saveExported();
\r
92 // display player test option
\r
93 //PlayerTools::displayPlayerOp($theConvertedFile);
\r
95 // force downlaod: only when no comments or debuging mode
\r
96 //TxtFileTools::downloadFile($theConvertedFile);
\r
98 // set the relative path of the target caption file
\r
99 $this->ccTargetUrl = $theConvertedFile;
\r
105 echo 'The format of source Caption was not recognized.';
\r
108 // return $ccSourceFormat
\r
110 }// end __construct()
\r
112 public function getCcTargetUrl()
\r
114 return $this->ccTargetUrl;
\r
118 * Gets the file name without extension of a file
\r
120 * @param String $theFilename Name of the file
\r
121 * @return String $targetFileName
\r
123 private function _buildExportFileName()
\r
125 $targetFileName = substr($this->ccSourceFileName, 0, -3);
\r
126 return $targetFileName;
\r
129 private function _saveExported()
\r
131 //global $rosettaCCSettings;
\r
132 // build the proper name to save converted caption
\r
133 $theCCname = $this->_buildExportFileName().$this->ccTargetExt;
\r
135 //$fullFileUrl = $rosettaCCSettings['uploadDir'].'/'.$theCCname;
\r
137 // save exported collection in a file
\r
138 TxtFileTools::stringToFile($theCCname,$this->ccStringTarget);
\r
140 // return the generated file name
\r
143 // download the generated caption file
\r
144 //CcUtil::downloadFile($fullFileUrl);
\r
149 * Gets a CaptionCollection object and return the target caption file as string
\r
153 private function _ccExport()
\r
155 $ccTargetString = '';
\r
157 //echo '<br/><br/>...creating an object of the target format: '.$this->ccTypeTarget;
\r
158 //echo '<br/><br/>I AM a '.$ccObjExp->getName().' Object';
\r
160 if ($this->ccTypeTarget!='0')
\r
162 // create an instance of the target Caption Format
\r
163 $ccObjExp = new $this->ccTypeTarget();
\r
165 // get the extension of the target Caption
\r
166 $this->ccTargetExt = $ccObjExp->getFileExtension();
\r
167 //echo '<br/>The extension of the Target Caption is: '. $this->ccTargetExt;
\r
169 // call export method in caption format instance
\r
170 $ccTargetString = $ccObjExp->exportCC($this->ccImportedCollection);
\r
174 //echo '<br/>Target Collection: <br/>'.$ccTargetString;
\r
175 return $ccTargetString;
\r
176 } // end _ccExport()
\r
179 * Import caption as a sting and return a Caption Collection
\r
180 * @param String $theCaption Caption file as a String
\r
181 * @param String $theCcFormatClassName The class name of the caption source
\r
182 * @return CaptionCollection $importedCollection Return a Caption Collection Object
\r
184 private function _ccImport($theCaption,$theCcFormatClassName)
\r
186 // Create an instance of the source Caption Format
\r
187 $myCcSourceObj = new $theCcFormatClassName();
\r
189 // import the caption into a CaptionCollection Object
\r
190 return $myCcSourceObj->importCC($theCaption);
\r
192 } // _ccImport() end
\r
195 * Auto detect format of a caption string using the unique pattern provided by each caption format
\r
196 * @param String $theCaption Caption file as a String
\r
197 * @return String $formatfound Return detected caption format or '' if not found
\r
199 private function _ccAutoDetect($theCaption)
\r
201 //echo '<br/>Total Formats to Auto-detect: '.count($this->ccFormats);
\r
205 // instanciate each caption format sub-class and call getIdPattern()
\r
206 foreach ($this->ccFormats as $ccId)
\r
208 // testing here a polymorphic behaviour
\r
209 //echo '<br/>.....Detecting format: '.$ccId;
\r
210 $theCcIdPattern = '';
\r
211 $ccObj = new $ccId(); // Create an instance of a caption format
\r
212 $theCcIdPattern = $ccObj->getIdPattern(); // get the pattern identifying the caption format
\r
213 //echo '<br/>***** '.$ccId.' = '.$theCcIdPattern;
\r
215 // look for the pattern in the caption string
\r
216 if (preg_match($theCcIdPattern,$theCaption)==1)
\r
218 //echo '<br/>!! Caption Format Dettected!! ***** '.$ccId.' = '.$theCcIdPattern;
\r
219 //echo '<br/>Caption Format Dettected: <b>'.$ccId.'</b>';
\r
221 // set the detected format
\r
222 $formatfound = $ccId;
\r
224 return $formatfound;
\r
225 break; // stop detecting caption format
\r
230 return $formatfound;
\r
232 } // _ccAutoDetect() end
\r
234 public function getCaptionFormats()
\r
236 return $this->ccFormats;
\r
239 public function getImportedCollection()
\r
241 return $this->ccImportedCollection;
\r