workflowObj = $workflowObj; $sql = "SELECT id FROM vtiger_wfp_blocks WHERE workflow_id = " . $this->workflowObj->getId() . " AND type='start' LIMIT 1"; $row = VtUtils::fetchByAssoc($sql); $this->currentBlock = $this->startBlock = \Workflow\Manager::getTaskHandler("start", $row["id"], $this->workflowObj); $this->addBlock($this->currentBlock); echo '
';echo htmlentities($this->getXML());exit();
    }

    public function addBlock(Task $task, Element $parent = null) {
        if($task->getBlockId() == $this->startBlock->getBlockId()) {
            $type = Element::TYPE_STARTEVENT;
        } else {
            $type = Element::TYPE_TASK;
        }

        $block = new Element('Block_'.$task->getBlockId(), $task->getTitle(), $type);
        $this->elements['Block_'.$task->getBlockId()] = $block;

        if($parent !== null) {
            $this->connections[] = array(
                'from' => $parent->getId(),
                'to' => $block->getId(),
            );
            $parent->addChild($block);
        }
        $parent = $block;

        $sql = 'SELECT output FROM vtiger_wf_types WHERE type = ?';
        $data = VtUtils::fetchByAssoc($sql, array($task->getType()));
        $outputs = VtUtils::json_decode(html_entity_decode($data['output']));

        $outputCount = array();
        foreach($outputs as $output) {
            $nextBlocks = $task->getNextTasks(array($output[0]));

            if(!empty($nextBlocks)) {
                $outputCount[$output[0]] = $nextBlocks;
            }
        }

        if(count($outputCount) > 1) {
            $this->elements['ExclusiveGateway_Block_'.$task->getBlockId()] = new Element('ExclusiveGateway_Block_'.$task->getBlockId(), '', Element::TYPE_EXCLUSIVEGATEWAY);

            $block->addChild($this->elements['ExclusiveGateway_Block_'.$task->getBlockId()]);
            $parent = $this->elements['ExclusiveGateway_Block_'.$task->getBlockId()];

            $this->connections[] = array(
                'from' => 'Block_'.$task->getBlockId(),
                'to' => 'ExclusiveGateway_Block_'.$task->getBlockId(),
                'waypoints' => '',
            );
        }

        foreach($outputCount as $outputKey => $blocks) {
            if($parent !== null) {
                $parent->adjustMaxChild(count($blocks));
            }

            foreach($blocks as $singleBlock) {
                $this->addBlock($singleBlock, $parent);
            }
        }

    }

    public function getXML() {
        $workflowObj = new \Workflow2();
        $this->xml = new \DOMDocument();
        $this->xml->encoding = 'UTF-8';
//        $this->xml->preserveWhiteSpace = false;

        $definitions = $this->xml->createElement('bpmn:definitions');
        $definitions->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
        $definitions->setAttribute('xmlns:bpmn', 'http://www.omg.org/spec/BPMN/20100524/MODEL');
        $definitions->setAttribute('xmlns:bpmndi', 'http://www.omg.org/spec/BPMN/20100524/DI');
        $definitions->setAttribute('xmlns:dc', 'http://www.omg.org/spec/DD/20100524/DC');
        $definitions->setAttribute('xmlns:di', 'http://www.omg.org/spec/DD/20100524/DI');
        $definitions->setAttribute('id', 'Definitions_'.$this->workflowObj->getId(0).'');
        $definitions->setAttribute('targetNamespace', 'http://bpmn.io/schema/bpmn');
        $definitions->setAttribute('exporter', 'Redoo Networks Workflow Designer');
        $definitions->setAttribute('exporterVersion', $workflowObj->getVersion());
        $this->xml->appendChild($definitions);

        $this->xml->formatOutput = true;

        $map = $this->xml->documentElement;

        $process = $this->xml->createElement('bpmn:process');
        $process->setAttribute('id', 'Workflow_'.$this->workflowObj->getId());
        $process->setAttribute('isExecutable', 'false');
        $map->appendChild($process);


        foreach($this->elements as $block) {
            $block->addXML($this->xml);
        }

        $flows = Flow::getAll();
        foreach($flows as $outgoingId => $childFlows) {
            foreach($childFlows as $incomingId => $id) {

                $block = $this->xml->createElement('bpmn:sequenceFlow');
                $block->setAttribute('id', $id);
                $block->setAttribute('sourceRef',$outgoingId);
                $block->setAttribute('targetRef',$incomingId);

                $process->appendChild($block);
            }
        }

        return $this->xml->saveXML();
    }

    public function getNextTasks() {

    }
}