contentRootId = 'catalog-treeRootNode'; $this->contentRootTag = 'catalogList'; $this->attributes = array('name' => '', 'intervals' => ''); // + 'parameters' $this->optionalAttributes = array(); $this->objTagName = 'catalog'; $this->id_prefix = 'cat_'; // 'tt_' ? if (!$sharedObject && !file_exists($this->xmlName)) { $this->createDom(); $this->xp = new domxpath($this->contentDom); } } public function getUploadedObject($name, $format, $onlyDescription = false) { if ($format == 'VOT') { $attributesToReturn = $this->vot2amda(USERTEMPDIR.$name, $onlyDescription); } $attributesToReturn['objName'] = $name; $attributesToReturn['objFormat'] = $format; return $attributesToReturn; } public function getTmpObject($folderId, $name, $onlyDescription = false) { $filePath = USERWORKINGDIR.$folderId.'/'.$name.'.xml'; if (!file_exists($filePath)) return array('error' => 'Cannot find result file'); $dom = new DomDocument('1.0'); $dom->formatOutput = true; if (!$dom -> load($filePath)) return array('error' => 'Cannot load result file'); $nameNodes = $dom->getElementsByTagName('name'); if ($nameNodes->length > 0) $attributesToReturn['name'] = $nameNodes->item(0)->nodeValue; $descNodes = $dom->getElementsByTagName('description'); if ($descNodes->length > 0) $attributesToReturn['description'] = $descNodes->item(0)->nodeValue; $creatNodes = $dom->getElementsByTagName('created'); if ($creatNodes->length > 0) $attributesToReturn['created'] = $creatNodes->item(0)->nodeValue; $histNodes = $dom->getElementsByTagName('history'); if ($histNodes->length > 0) $attributesToReturn['history'] = $histNodes->item(0)->nodeValue; $attributesToReturn['objName'] = $name; $attributesToReturn['folderId'] = $folderId; $attributesToReturn['success'] = true; if (!$onlyDescription) { $intNodes = $dom->getElementsByTagName('intervals'); foreach ($intNodes as $intNode) { $startNodes = $intNode->getElementsByTagName('start'); if ($startNodes->length <= 0) return array('error' => 'Error detected in result file'); $stopNodes = $intNode->getElementsByTagName('stop'); if ($stopNodes->length <= 0) return array('error' => 'Error detected in result file'); // for catalog $paramNodes = $intNode->getElementsByTagName('param'); $params = array(); if ($paramNodes->length > 0) foreach ( $paramNodes as $paramNode ) $params[] = $paramNode->nodeValue; $attributesToReturn['intervals'][] = array('start' => $startNodes->item(0)->nodeValue, 'stop' => $stopNodes->item(0)->nodeValue, 'paramTable' => $params); } // for catalog $paramsNodes = $dom->getElementsByTagName('parameter'); if ($paramsNodes->length > 0){ $paramsArray = array(); foreach ($paramsNodes as $paramNode) { $oneParam = array(); foreach ($paramNode->attributes as $attr) $oneParam[$attr->nodeName] = $attr->nodeValue; if (substr($paramNode->getAttribute('id'),0,8) == 'stat_cov') { $oneParam['size'] = '1'; $oneParam['name'] = 'Flag'; } $paramsArray[] = $oneParam; } $attributesToReturn['success'] = true; $attributesToReturn['parameters'] = $paramsArray; } else return array('error' => 'No information on parameters in result file'); } return $attributesToReturn; } public function loadIntervalsFromTT($id, $typeTT, $start = NULL, $limit = NULL) { if ($typeTT == 'sharedcatalog') { //Shared object $sharedObjMgr = new SharedObjectsMgr(); $path = $sharedObjMgr->getDataFilePath('catalog', $id); } else { $path = USERTTDIR.$id.'.xml'; } //load intervals from TT id if (!file_exists($path)) return array('success' => false, 'message' => "Cannot find Catalog file ".$typeTT); $this->objectDom->load($path); if (!($objToGet = $this->objectDom->getElementById($id))) return array('success' => false, 'message' => NO_SUCH_ID." ".$id); $xpath = new DOMXPath($this->objectDom); $intervals = $xpath->query('//intervals'); $result = array(); if (!isset($start) || !isset($limit)) { foreach ($intervals as $interval) { $startTime = $interval->getElementsByTagName('start')->item(0)->nodeValue; $stopTime = $interval->getElementsByTagName('stop')->item(0)->nodeValue; // for catalog $paramNodes = $interval->getElementsByTagName('param'); $params = array(); if ($paramNodes->length > 0) foreach ( $paramNodes as $paramNode ) $params[] = $paramNode->nodeValue; array_push($result, array('start' => $startTime, 'stop' => $stopTime,'paramTable' => $params)); } } else { for ($i = 0; $i < $limit; ++$i) { if ($start+$i >= $intervals->length) break; $startTime = $intervals->item($start+$i)->getElementsByTagName('start')->item(0)->nodeValue; $stopTime = $intervals->item($start+$i)->getElementsByTagName('stop')->item(0)->nodeValue; // for catalog $paramNodes = $intervals->item($start+$i)->getElementsByTagName('param'); $params = array(); if ($paramNodes->length > 0) foreach ( $paramNodes as $paramNode ) $params[] = $paramNode->nodeValue; array_push($result, array('start' => $startTime, 'stop' => $stopTime,'paramTable' => $params)); } } // for catalog : params header $paramsNodes = $xpath->query('//parameters/parameter'); $paramsArray = array(); if ($paramsNodes->length > 0) { foreach ($paramsNodes as $paramNode) { $oneParam = array(); foreach ($paramNode->attributes as $attr) $oneParam[$attr->nodeName] = $attr->nodeValue; $paramsArray[] = $oneParam; } } return array( 'totalCount' => $intervals->length, 'intervals' => $result, 'parameters' => $paramsArray, 'start' => isset($start) ? $start : 0, 'limit' => isset($limit) ? $limit : 0, 'success' => true ); } /* * catalog header */ protected function setParamDescription($params) { $paramsElement = $this->objectDom->createElement('parameters'); foreach ($params as $param) { $paramElement = $this->objectDom->createElement('parameter'); $attrArray = (array)$param; foreach ($attrArray as $key => $value) $paramElement->setAttribute($key, $value); $paramsElement->appendChild($paramElement); } return $paramsElement; } protected function createIntervalElement($interval) { $newInterval = $this->objectDom->createElement('intervals'); $newInterval->appendChild($this->objectDom->createElement('start',$interval['start'])); $newInterval->appendChild($this->objectDom->createElement('stop',$interval['stop'])); foreach ($interval as $key =>$value) { if (substr($key,0,5) == 'param') $newInterval->appendChild($this->objectDom->createElement('param', $value)); } return $newInterval; } public function createObject($p, $folder) { if ($p -> leaf) { $result = $this->createParameter($p, $folder); if ($result['error']) return $result; $cacheMgr = new CatalogCacheMgr(); if (isset($p->cacheToken) && ($p->cacheToken != '')) { $resultSaveInt = $cacheMgr->saveInTT($result['id'], "update", $p->cacheToken); if (!$resultSaveInt['success']) { if ($resultSaveInt['message']) return array('error' => $resultSaveInt['message']); else return array('error' => 'Unknown error during intervals save'); } } return $result; } // else return $this->createFolder($p); //TODO check if this is possible? else return array('error' => 'createFolder should be called from RENAME'); } public function initForChart($id, $name, $isTmpObject, $typeTT) { $intervals_res = $this->getCatalogParamDescription($id, $name, $isTmpObject, $typeTT); if (!$intervals_res['success']) return $intervals_res; $paramHeaders = array(); foreach ( $intervals_res['parameters'] as $param ) { if ($param['size'] > 1) { for ($i = 0; $i < $param['size']; $i++) { $paramComp = array(); $paramComp['id'] = $param['id'].'_'.$i; $paramComp['name'] = $param['name'].'_'.$i; // $paramComp['size'] = 1; $paramHeaders[] = $paramComp; } } else { $paramHeaders[] = $param; } } // unset($intervals_res); return array('success' => true, 'parameters' => $paramHeaders, 'totalCount' => $intervals_res['totalCount'], 'name' => $intervals_res['name']); } public function getIntervalsForChart($id, $name, $isTmpObject, $type) { if ($isTmpObject) $intervals_res = $this->getTmpObject($id, $name); else $intervals_res = $this->loadIntervalsFromTT($id,$type); if (!$intervals_res['success']) return $intervals_res; $newIntervals = array(); foreach ($intervals_res['intervals'] as $interval) { $newIntervalComp = array(); $k = 0; for ( $j = 0; $j < count($interval['paramTable']); $j++ ) { $param = $interval['paramTable'][$j]; $tempArr = explode(',',$param); if (count($tempArr) > 1) { for ($i = 0; $i < count($tempArr); $i++) { $newIntervalComp['param'.$k] = $tempArr[$i]; $k++; } } else { $newIntervalComp['param'.$k] = $param; $k++; } } $newIntervals[] = $newIntervalComp; } return array('success' => true, 'intervals' => $newIntervals); } public function getCatalogParamDescription($id, $name, $isTmpObject, $typeTT) { if ($typeTT == 'sharedcatalog') { //Shared object $sharedObjMgr = new SharedObjectsMgr(); $path = $sharedObjMgr->getDataFilePath('catalog', $id); } elseif (!$isTmpObject) { $path = USERTTDIR.$id.'.xml'; } else { $path = USERWORKINGDIR.$id.'/'.$name.'.xml'; } //load intervals from Catalog id if (!file_exists($path)) return array('success' => false, 'message' => "Cannot find Catalog file ".$id); if (!$this->objectDom->load($path)) return array('success' => false, 'message' => "Cannot load Catalog file ".$id); // if (!($objToGet = $this->objectDom->getElementById($id))) // return array('success' => false, 'message' => NO_SUCH_ID." ".$id); $nameNodes = $this->objectDom->getElementsByTagName('name'); if ($nameNodes->length > 0) $nameReal = $nameNodes->item(0)->nodeValue; else $nameReal = $name; $xpath = new DOMXPath($this->objectDom); $intervals = $xpath->query('//intervals'); // params header $paramsNodes = $xpath->query('//parameters/parameter'); $paramsArray = array(); if ($paramsNodes->length > 0) { foreach ($paramsNodes as $paramNode) { $oneParam = array(); foreach ($paramNode->attributes as $attr) $oneParam[$attr->nodeName] = $attr->nodeValue; $paramsArray[] = $oneParam; } } return array( 'name' => $nameReal, 'totalCount' => $intervals->length, 'parameters' => $paramsArray, 'success' => true ); } // public function modifyObject($p) { // $folder = $this->getObjectFolder($p->id); // // //Copy TT in a tempory file // $ttFilePath = USERTTDIR.$p->id.'.xml'; // $tmpFileExist = FALSE; // if (file_exists($ttFilePath)) // $tmpFileExist = copy($ttFilePath,$ttFilePath.".tmp"); // // //Delete TT // $this->deleteObject($p); // // //Save modifications // try { // $result = $this->createObject($p, $folder); // if ($result['error']) // throw new Exception($result['error']); // if ($tmpFileExist) // unlink($ttFilePath.".tmp"); // return array('id' => $p->id, 'info' => $result['nbIntervals'].' intervals' ); // } // catch (Exception $e) { // //Restore TT file // if ($tmpFileExist) // { // copy($ttFilePath.".tmp", $ttFilePath); // unlink($ttFilePath.".tmp"); // } // return array ('error' => $e->getMessage()); // } // } /* * Uploaded vot catalog => convert to AMDA tmp */ protected function vot2amda($tmp_file, $onlyDescription = false) { // Load Catalog $this->objectDom->load($tmp_file); $objToGet = $this->objectDom->getElementsByTagName('TABLEDATA')->item(0); $attributes = $objToGet->childNodes; $paramsNodes = $this->objectDom->getElementsByTagName('FIELD'); $paramsNumber = $paramsNodes->length; foreach($attributes as $attribute) { if ($attribute->tagName == 'TR') { $start = $attribute->getElementsByTagName('TD')->item(0)->nodeValue; $stop = $attribute->getElementsByTagName('TD')->item(1)->nodeValue; $params = array(); for ($i = 2; $i < $paramsNumber; $i++) { $param = $attribute->getElementsByTagName('TD')->item($i)->nodeValue; if ($paramsNodes->item($i)->hasAttribute('arraysize')) { $param = join(",",explode(" ",trim($param))); } $params[] = $param; } if (!$onlyDescription) $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop, 'paramTable' => $params); } } if ($paramsNumber > 2) { $paramsArray = array(); for ($i = 2; $i < $paramsNumber; $i++) { $oneParam = array(); $paramNode = $paramsNodes->item($i); if ($paramNode->hasAttribute('arraysize')) { $oneParam['size'] = $paramNode->getAttribute('arraysize'); } else { $oneParam['size'] = 1; } foreach ($paramNode->attributes as $attr) { if ($attr->nodeName == 'datatype') { $datatype = $attr->nodeValue; if ($paramNode->hasAttribute('xtype') && (($paramNode->getAttribute('xtype') == 'dateTime') || ($paramNode->getAttribute('xtype') == 'iso8601'))) { $oneParam['type'] = 1; $paramNode->setAttribute('xtype','dateTime'); $paramNode->setAttribute('size',1); } else if ($datatype == 'char') { $oneParam['type'] = 2;// string $paramNode->setAttribute('size', 1); } else { $oneParam['type'] = 0;//'Double'; } } else if ($attr->nodeName != 'arraysize') { $oneParam[$attr->nodeName] = $attr->nodeValue; } } $paramsArray[] = $oneParam; } $attributesToReturn['success'] = true; $attributesToReturn['parameters'] = $paramsArray; } $suffix = explode('.', basename($tmp_file)); $attributesToReturn['name'] = basename($tmp_file, '.'.$suffix[1]); $attributesToReturn['created'] = date('Y-m-d')."T".date('H:i:s'); $attributesToReturn['description'] = htmlspecialchars($this->objectDom->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue); return($attributesToReturn); } } ?>