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"); }
private function saveMenu($post) { // initialization $return = $nodes = $saved = array(); foreach ($post as $key => $val) { if (is_array($val)) { $nodes[] = $key; } } // sets up array foreach ($post['level'] as $key => $level) { foreach ($nodes as $node) { $return[$key][$node] = $post[$node][$key]; } // fill empty fields if (empty($return[$key]['slug'])) { $return[$key]['slug'] = $this->strtoslug($return[$key]['title']); } // final formatting $return[$key]['slug'] = $this->strtoslug($return[$key]['slug']); $return[$key]['url'] = $this->transliterate($return[$key]['url']); // checks to see slug doesn't already exist if (in_array($return[$key]['slug'], $saved)) { $return[$key]['slug'] = $return[$key]['slug'] . '-' . rand(0, 100); } // add to saved array $saved[] = $return[$key]['slug']; } // build xml file $xml = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><channel/>'); $cdata = array('title', 'url'); // menu items $menu = $xml->addChild('menu'); foreach ($return as $key => $item) { $itemxml = $menu->addChild('item'); foreach ($item as $field => $val) { if (in_array($field, $cdata)) { $itemxml->{$field} = null; $itemxml->{$field}->addCData($val); } else { $itemxml->addChild($field, $val); } } } // settings $settings = $xml->addChild('settings'); // format the xml file (beautify) $dom = new DOMDocument(); $dom->preserveWhiteSpace = false; $dom->loadXML($xml->saveXML()); $dom->formatOutput = true; // save to file $post['name'] = $this->strtoslug($post['name']); $newfile = GSDATAOTHERPATH . self::FILE . '/' . $post['name'] . '.xml'; if (isset($post['oldname'])) { $oldfile = GSDATAOTHERPATH . self::FILE . '/' . $post['oldname'] . '.xml'; if (file_exists($oldfile) && !file_exists($newfile)) { unlink($oldfile); } } return $dom->save($newfile); }
<?php // http://coffeerings.posterous.com/php-simplexml-and-cdata class SimpleXMLExtended extends SimpleXMLElement { public function addCData($cdata_text) { $node = dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($cdata_text)); } } $xmlFile = 'config.xml'; // instead of $xml = new SimpleXMLElement('<site/>'); $xml = new SimpleXMLExtended('<site/>'); $xml->title = NULL; // VERY IMPORTANT! We need a node where to append $xml->title->addCData('Site Title'); $xml->title->addAttribute('lang', 'en'); $xml->saveXML($xmlFile);