示例#1
0
    public function load($p)
    {
        $rez = array('success' => true, 'data' => array('html' => '<div class="msg">Please select a case ...</div>'));
        if (empty($p['path'])) {
            return $rez;
        }
        $this->params = $p;
        $this->pathProperties = Path::getPathProperties($p['path']);
        $pp =& $this->pathProperties;
        if (empty($pp['case_id'])) {
            return $rez;
        }
        /* define graph title */
        $graphTitle = empty($pp['name']) ? 'Graph' : $pp['name'];
        $graphTitle = str_replace(array(' ', '.', '\'', '"', '-', '/', '\\', '~'), '_', $graphTitle);
        /* end of define graph title */
        $solrNodes = $this->getGraphNodes($pp['case_id']);
        if (empty($solrNodes)) {
            return array('success' => true, 'data' => array('html' => '<div class="msg">Nothing to display.</div>'));
        }
        $graphNodes = $solrNodes;
        $this->prepareGraphNodes($graphNodes);
        $this->setGraphTimeline($graphNodes);
        $rez = '';
        /* print graph timeline nodes */
        $rez .= "{\n node [shape=plaintext, fontsize=7, fontcolor=\"gray\", tooltip=\"timeline\"];\n /* the time-line graph */ \n";
        $rez .= 't' . implode(' -> t', array_keys($this->timeline)) . " [dir=none, color=\"gray\"];\n}\n";
        for ($i = 0; $i < sizeof($this->timeline); $i++) {
            $rez .= 't' . $i . '[label="' . $this->timeline[$i] . '"];' . "\n";
        }
        /* end of print graph timeline nodes */
        /* align graph nodes to corresponding timeline node */
        foreach ($graphNodes as $node) {
            $rez .= "\n{rank=same; " . $node['t'] . "; n" . $node['id'] . "; }";
        }
        /* end of align graph nodes to corresponding timeline node */
        /* defining nodes */
        foreach ($graphNodes as $node) {
            /* check if its a composite node with left and/or right sides
               and replace it's label with corresponding nodes table */
            $leftSide = '';
            if (!empty($node['leftSideNodes'])) {
                foreach ($node['leftSideNodes'] as $subnode) {
                    $leftSide .= '<td href="#' . $subnode['id'] . '"' . ' bgcolor="' . $subnode['bgcolor'] . '"' . ' PORT="n' . $subnode['id'] . '_' . $node['id'] . '"' . (empty($subnode[$this->hintField]) ? '' : ' tooltip="' . $subnode[$this->hintField] . '"') . ' valign="middle">' . $subnode[$this->labelField] . '</td>';
                }
            }
            $rightSide = '';
            if (!empty($node['rightSideNodes'])) {
                foreach ($node['rightSideNodes'] as $subnode) {
                    $rightSide .= '<td href="#' . $subnode['id'] . '"' . ' bgcolor="' . $subnode['bgcolor'] . '"' . ' PORT="n' . $subnode['id'] . '_' . $node['id'] . '"' . (empty($subnode[$this->hintField]) ? '' : ' tooltip="' . $subnode[$this->hintField] . '"') . ' valign="middle">' . $subnode[$this->labelField] . '</td>';
                }
            }
            if (!empty($leftSide) || !empty($rightSide)) {
                $node[$this->labelField] = '< <table border="0" cellborder="1" cellspacing="0" cellpadding="4" ' . 'style="border: 0 !important; border-collapse: collapse !important"><tr>' . $leftSide . '<td bgcolor="' . $this->colors['yellow'] . '"' . ' PORT="n' . $node['id'] . '"' . ' valign="middle"' . (empty($node[$this->hintField]) ?: ' tooltip="' . addslashes($node[$this->hintField]) . '"') . '>' . $node[$this->labelField] . '</td>' . $rightSide . '</tr></table> >';
            } else {
                $node[$this->labelField] = '"' . addslashes($node[$this->labelField]) . '"';
            }
            /* end of check if its a composite node with left and/or right sides
               and replace it's label with corresponding nodes table */
            $rez .= "\n n" . $node['id'] . ' [shape="' . ($node['shape'] ? $node['shape'] : 'ellipse') . '"' . (empty($node['style']) ? '' : ', style="' . $node['style'] . '"') . (empty($node['fillcolor']) ? '' : ', fillcolor="' . $node['fillcolor'] . '"') . (isset($node['margin']) ? ', margin="' . $node['margin'] . '"' : '') . (isset($node['penwidth']) ? ', penwidth="' . $node['penwidth'] . '"' : '') . (empty($node[$this->hintField]) ? '' : ', tooltip="' . addslashes($node[$this->hintField]) . '"') . ', label=' . $node[$this->labelField] . ', URL="#' . $node['id'] . '"];';
            $linkedNodeIds = $this->getNodeLinks($node['id']);
            foreach ($linkedNodeIds as $ln) {
                $rez .= "\n" . 'n' . $ln . ' -> n' . $node['id'] . ';';
            }
        }
        // enclosing all nodes data in header and footer
        $rez = 'digraph "' . $graphTitle . '" {
            spline=false;
            node [margin="0.05,0.05", shape=plaintext, fontname="tahoma", fontsize=8, height=0.2, width=0.2];
            edge [arrowsize=0.5, fontsize=7];

            ranksep=0.25;
            nodesep=0.25;
            ' . $rez . "\n}";
        /* save result to a temporary file for passing it to graphviz */
        $t = tempnam(sys_get_temp_dir(), 'gv');
        file_put_contents($t, $rez);
        //also store gv file in core files path for any case
        /* if parametter "d" is passed in GET, then save graph is requested.
           Generate png file and write it directly to output */
        if (@$_GET['d'] == 1) {
            $cmd = "dot  -Tpng -o" . $t . ".png " . $t;
            exec($cmd);
            header('Content-Type: image/png');
            header('Content-Disposition: attachment; filename="' . date('Y-m-d') . ' - Граф дела ' . addslashes($graphTitle) . '.png"');
            echo file_get_contents($t . '.png');
        } else {
            //generate and return svg
            $cmd = "dot  -Tsvg -o" . $t . ".svg " . $t;
            exec($cmd);
            $rez = file_get_contents($t . '.svg');
        }
        /* delete temporary files */
        unlink($t);
        unlink($t . '.svg');
        /* end of delete temporary files */
        return array('success' => true, 'data' => array('html' => $rez, 'nodes' => $solrNodes));
    }