function saveTemplate($data)
 {
     My_Logger::log("in Ajax_TemplateEditorProcessor saveTemplate()");
     $expected = array('filename', 'type', 'time', 'concepts', 'instructions', 'problem');
     $filename = $this->get($data['filename']);
     if (empty($filename)) {
         throw new Exception('Missing Filename');
     }
     My_Logger::log("filename is " . $filename);
     foreach ($expected as $field) {
         $toCheck = $this->get($data[$field]);
         if (empty($toCheck)) {
             //if (empty($this->get($data[$field]))){
             throw new Exception('Missing field: ' . $field);
         }
     }
     //build xml
     $xml = new SimpleXMLExtended('<question/>');
     $xml->addAttribute('type', $this->get($data['type']));
     $xml->addChild('estimated_time', $this->get($data['time']));
     $concepts = $xml->addChild('concepts');
     $concepts->addChild('concept', $this->get($data['concepts']));
     $xml->addChild('difficulty', $this->get($data['difficulty']));
     $xml->addChild('instructions', $this->get($data['instructions']));
     //$xml->problem = null;
     //$xml->problem->addCData($this->get($data['problem']));
     $xml->addCData('problem', $this->get($data['problem']));
     $sc = $xml->addChild('substitutions');
     $subs = $this->get($data['s']);
     if ($subs) {
         foreach ($subs as $sd) {
             if (empty($sd['name']) || empty($sd['value'])) {
                 continue;
             }
             $s = $sc->addCData('substitution', $sd['value']);
             $s->addAttribute('val', $sd['name']);
         }
     }
     $config = new Zend_Config_Ini(APPLICATION_PATH . "/configs/application.ini", APPLICATION_ENV);
     $path = $config->xml->import_path;
     $full_filename = $path . DIRECTORY_SEPARATOR . $filename;
     My_Logger::log("Saving to {$full_filename}");
     $xml->saveXML($full_filename);
     chmod($full_filename, 0666);
     return array('result' => 'success', 'msg' => "File '{$filename}' saved correctly");
 }
 /**
  * add the errors to an existing xml document
  *
  * @param String $encoding encoding
  *
  * @return SimpleXmlElement
  */
 public function errorsAddToXML($encoding)
 {
     $dom = new DOMDocument('1.0', 'UTF-8');
     $root = $dom->createElement("Errors");
     $dom->appendChild($root);
     $xml = simplexml_import_dom($dom);
     $xmlerr = new SimpleXMLExtended($xml, $encoding);
     foreach ($this->_arrErrorList as $arrLine) {
         $error = $xmlerr->addCData('Error', $arrLine['message']);
         $error->addAttribute('Function', $arrLine['command']);
     }
     return $xmlerr->getSimpleXmlElement();
 }
 /**
  * @brief Create the XML output of the AREL Object and send it to the junaio server.
  * @param ArelObject $oObject Object to be sent out
  */
 public static function outputObject($oObject)
 {
     $object = new SimpleXMLExtended("<object></object>");
     $object->addAttribute('id', (string) $oObject->getID());
     if ($oObject->getTitle()) {
         $object->addCData('title', $oObject->getTitle());
     }
     if ($oObject->getThumbnail()) {
         $object->addCData('thumbnail', $oObject->getThumbnail());
     }
     if ($oObject->getIcon()) {
         $object->addCData('icon', $oObject->getIcon());
     }
     //location
     if ($oObject->getLocation()) {
         $location = $object->addChild("location");
         $oLocation = $oObject->getLocation();
         try {
             $location->addChild('lat', $oLocation[0]);
             $location->addChild('lon', $oLocation[1]);
             if (isset($oLocation[2])) {
                 $location->addChild('alt', $oLocation[2]);
             } else {
                 $location->addChild('alt', 0);
             }
         } catch (Exception $e) {
             return $e;
         }
     }
     //popup
     if ($oObject->getPopup()) {
         $popup = $object->addChild("popup");
         $oPopUp = $oObject->getPopup();
         if ($oPopUp->getDescription()) {
             $popup->addCData('description', $oPopUp->getDescription());
         }
         if ($oPopUp->getButtons()) {
             $buttons = $popup->addChild("buttons");
             $aButtons = $oPopUp->getButtons();
             foreach ($aButtons as $oButton) {
                 $button = $buttons->addCData("button", $oButton[2]);
                 $button->addAttribute("id", $oButton[1]);
                 $button->addAttribute("name", $oButton[0]);
             }
         }
     }
     if ($oObject instanceof ArelObjectModel3D) {
         //assets3D
         $assets3D = $object->addChild("assets3d");
         if ($oObject->getModel()) {
             $assets3D->addCData('model', $oObject->getModel());
         }
         if ($oObject->getMovie()) {
             $assets3D->addCData('movie', $oObject->getMovie());
         }
         if ($oObject->getTexture()) {
             $assets3D->addCData('texture', $oObject->getTexture());
         }
         //transform
         $transform = $assets3D->addChild("transform");
         $oTransform = $oObject->getTransformParent();
         if (isset($oTransform)) {
             $transform->addAttribute("parent", $oTransform);
         }
         try {
             //translation
             $translation = $transform->addChild("translation");
             $oTranslation = $oObject->getTranslation();
             $translation->addChild("x", $oTranslation[0]);
             $translation->addChild("y", $oTranslation[1]);
             $translation->addChild("z", $oTranslation[2]);
             //rotation
             $rotation = $transform->addChild("rotation");
             $oRotationElement = $oObject->getRotation();
             $oRotation = $oRotationElement->getRotationValues();
             $oRotationType = $oRotationElement->getRotationType();
             $rotation->addAttribute("type", $oRotationType);
             if ($oRotationType !== ArelRotation::ROTATION_MATRIX) {
                 $rotation->addChild("x", $oRotation[0]);
                 $rotation->addChild("y", $oRotation[1]);
                 $rotation->addChild("z", $oRotation[2]);
                 if ($oRotationType == ArelRotation::ROTATION_QUATERNION) {
                     $rotation->addChild("w", $oRotation[3]);
                 } else {
                     if ($oRotationType == ArelRotation::ROTATION_AXISANGLE) {
                         $rotation->addChild("angle", $oRotation[3]);
                     }
                 }
             } else {
                 $rotation->addChild("m0", $oRotation[0]);
                 $rotation->addChild("m1", $oRotation[1]);
                 $rotation->addChild("m2", $oRotation[2]);
                 $rotation->addChild("m3", $oRotation[3]);
                 $rotation->addChild("m4", $oRotation[4]);
                 $rotation->addChild("m5", $oRotation[5]);
                 $rotation->addChild("m6", $oRotation[6]);
                 $rotation->addChild("m7", $oRotation[7]);
                 $rotation->addChild("m8", $oRotation[8]);
             }
             //scale
             $scale = $transform->addChild("scale");
             $oScale = $oObject->getScale();
             $scale->addChild("x", $oScale[0]);
             $scale->addChild("y", $oScale[1]);
             $scale->addChild("z", $oScale[2]);
         } catch (Exception $e) {
             return $e;
         }
         //properties
         $pickingEnabled = $oObject->isPickingEnabled();
         $cosID = $oObject->getCoordinateSystemID();
         $shaderMaterial = $oObject->getShaderMaterial();
         $occluding = $oObject->isOccluding();
         $transparency = $oObject->getTransparency();
         $renderPosition = $oObject->getRenderOrderPosition();
         $screenAnchor = $oObject->getScreenAnchor();
         if (isset($cosID) || isset($shaderMaterial) || isset($occluding) || isset($pickingEnabled) || isset($screenAnchor) || isset($transparency) || isset($renderPosition)) {
             $properties = $assets3D->addChild("properties");
             if (isset($cosID)) {
                 $properties->addChild("coordinatesystemid", $cosID);
             }
             if (isset($shaderMaterial)) {
                 $properties->addChild("shadermaterial", $shaderMaterial);
             }
             if ($occluding) {
                 $properties->addChild("occluding", "true");
             }
             if (isset($transparency) && $transparency > 0) {
                 $properties->addChild("transparency", $oObject->getTransparency());
             }
             if (isset($pickingEnabled) && !$pickingEnabled) {
                 $properties->addChild("pickingenabled", "false");
             }
             if (isset($renderPosition)) {
                 $properties->addChild("renderorder", $oObject->getRenderOrderPosition());
             }
             if (isset($screenAnchor)) {
                 $screenAnchorProperty = $properties->addChild("screenanchor", $oObject->getScreenAnchor());
                 if ($oObject->getScreenAnchorFlag() != NULL) {
                     $screenAnchorProperty->addAttribute("flags", $oObject->getScreenAnchorFlag(), null);
                 }
             }
         }
     }
     //viewparameters
     if ($oObject->getVisibility() || $oObject->getMinAccuracy() || $oObject->getMinDistance() || $oObject->getMaxDistance()) {
         $viewparameter = $object->addChild("viewparameters");
         if ($oObject->getVisibility()) {
             $visibility = $viewparameter->addChild("visibility");
             $oVisibility = $oObject->getVisibility();
             if (isset($oVisibility["liveview"]) && !$oVisibility["liveview"]) {
                 $visibility->addChild("liveview", "false");
             }
             if (isset($oVisibility["maplist"]) && !$oVisibility["maplist"]) {
                 $visibility->addChild("maplist", "false");
             }
             if (isset($oVisibility["radar"]) && !$oVisibility["radar"]) {
                 $visibility->addChild("radar", "false");
             }
             //alternatively for 0,1,2
             if (isset($oVisibility[0]) && !$oVisibility[0]) {
                 $visibility->addChild("liveview", "false");
             }
             if (isset($oVisibility[1]) && !$oVisibility[1]) {
                 $visibility->addChild("maplist", "false");
             }
             if (isset($oVisibility[2]) && !$oVisibility[2]) {
                 $visibility->addChild("radar", "false");
             }
         }
         if ($oObject->getMinAccuracy()) {
             $viewparameter->addChild("minaccuracy", $oObject->getMinAccuracy());
         }
         if ($oObject->getMinDistance()) {
             $viewparameter->addChild("mindistance", $oObject->getMinDistance());
         }
         if ($oObject->getMaxDistance()) {
             $viewparameter->addChild("maxdistance", $oObject->getMaxDistance());
         }
     }
     //parameters
     if ($oObject->getParameters()) {
         $parameters = $object->addChild("parameters");
         foreach ($oObject->getParameters() as $key => $parValue) {
             $parameter = $parameters->addCData("parameter", $parValue);
             $parameter->addAttribute("key", $key);
         }
     }
     $out = $object->asXML();
     $pos = strpos($out, "?>");
     echo utf8_encode(trim(substr($out, $pos + 2)));
     ob_flush();
 }