public function saveToDocument(ezcWorkflow $workflow, $workflowVersion)
{
$document = new DOMDocument('1.0', 'UTF-8');
$document->formatOutput = true;
$root = $document->createElement('workflow');
$document->appendChild($root);
$root->setAttribute('name', $workflow->name);
$root->setAttribute('version', $workflowVersion);
$nodes = $workflow->nodes;
$numNodes = count($nodes);
// Workaround for foreach() bug in PHP 5.2.1.
// http://bugs.php.net/bug.php?id=40608
$keys = array_keys($nodes);
for ($i = 0; $i < $numNodes; $i++) {
$id = $keys[$i];
$node = $nodes[$id];
$nodeClass = get_class($node);
$xmlNode = $document->createElement('node');
$xmlNode->setAttribute('id', $id);
$xmlNode->setAttribute('type', str_replace('ezcWorkflowNode', '', get_class($node)));
$node->configurationtoXML($xmlNode);
$root->appendChild($xmlNode);
$outNodes = $node->getOutNodes();
$_keys = array_keys($outNodes);
$numOutNodes = count($_keys);
for ($j = 0; $j < $numOutNodes; $j++) {
foreach ($nodes as $outNodeId => $_node) {
if ($_node === $outNodes[$_keys[$j]]) {
break;
}
}
$xmlOutNode = $document->createElement('outNode');
$xmlOutNode->setAttribute('id', $outNodeId);
if (is_subclass_of($nodeClass, 'ezcWorkflowNodeConditionalBranch') && ($condition = $node->getCondition($outNodes[$_keys[$j]]))) {
if (!$node->isElse($outNodes[$_keys[$j]])) {
$xmlCondition = self::conditionToXml($condition, $document);
$xmlCondition->appendChild($xmlOutNode);
$xmlNode->appendChild($xmlCondition);
} else {
$xmlElse = $xmlCondition->appendChild($document->createElement('else'));
$xmlElse->appendChild($xmlOutNode);
}
} else {
$xmlNode->appendChild($xmlOutNode);
}
}
}
foreach ($workflow->getVariableHandlers() as $variable => $class) {
$variableHandler = $root->appendChild($document->createElement('variableHandler'));
$variableHandler->setAttribute('variable', $variable);
$variableHandler->setAttribute('class', $class);
}
return $document;
}