displayablegraph.class.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. <?php
  2. // Copyright (C) 2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Special kind of Graph for producing some nice output
  20. *
  21. * @copyright Copyright (C) 2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class DisplayableNode extends GraphNode
  25. {
  26. public $x;
  27. public $y;
  28. /**
  29. * Create a new node inside a graph
  30. * @param SimpleGraph $oGraph
  31. * @param string $sId The unique identifier of this node inside the graph
  32. * @param number $x Horizontal position
  33. * @param number $y Vertical position
  34. */
  35. public function __construct(SimpleGraph $oGraph, $sId, $x = 0, $y = 0)
  36. {
  37. parent::__construct($oGraph, $sId);
  38. $this->x = $x;
  39. $this->y = $y;
  40. $this->bFiltered = false;
  41. }
  42. public function GetIconURL()
  43. {
  44. return $this->GetProperty('icon_url', '');
  45. }
  46. public function GetLabel()
  47. {
  48. return $this->GetProperty('label', $this->sId);
  49. }
  50. public function GetWidth()
  51. {
  52. return max(32, 5*strlen($this->GetProperty('label'))); // approximation of the text's bounding box
  53. }
  54. public function GetHeight()
  55. {
  56. return 32;
  57. }
  58. public function Distance2(DisplayableNode $oNode)
  59. {
  60. $dx = $this->x - $oNode->x;
  61. $dy = $this->y - $oNode->y;
  62. $d2 = $dx*$dx + $dy*$dy - $this->GetHeight()*$this->GetHeight();
  63. if ($d2 < 40)
  64. {
  65. $d2 = 40;
  66. }
  67. return $d2;
  68. }
  69. public function Distance(DisplayableNode $oNode)
  70. {
  71. return sqrt($this->Distance2($oNode));
  72. }
  73. public function GetForRaphael()
  74. {
  75. $aNode = array();
  76. $aNode['shape'] = 'icon';
  77. $aNode['icon_url'] = $this->GetIconURL();
  78. $aNode['width'] = 32;
  79. $aNode['source'] = ($this->GetProperty('source') == true);
  80. $aNode['obj_class'] = get_class($this->GetProperty('object'));
  81. $aNode['obj_key'] = $this->GetProperty('object')->GetKey();
  82. $aNode['sink'] = ($this->GetProperty('sink') == true);
  83. $aNode['x'] = $this->x;
  84. $aNode['y']= $this->y;
  85. $aNode['label'] = $this->GetLabel();
  86. $aNode['id'] = $this->GetId();
  87. $fOpacity = ($this->GetProperty('is_reached') ? 1 : 0.4);
  88. $aNode['icon_attr'] = array('opacity' => $fOpacity);
  89. $aNode['text_attr'] = array('opacity' => $fOpacity);
  90. return $aNode;
  91. }
  92. public function RenderAsPDF(TCPDF $oPdf, DisplayableGraph $oGraph, $fScale)
  93. {
  94. $Alpha = 1.0;
  95. $oPdf->SetFillColor(200, 200, 200);
  96. $oPdf->setAlpha(1);
  97. $sIconUrl = $this->GetProperty('icon_url');
  98. $sIconPath = str_replace(utils::GetAbsoluteUrlModulesRoot(), APPROOT.'env-production/', $sIconUrl);
  99. if ($this->GetProperty('source'))
  100. {
  101. $oPdf->SetLineStyle(array('width' => 2*$fScale, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => array(204, 51, 51)));
  102. $oPdf->Circle($this->x * $fScale, $this->y * $fScale, 16 * 1.25 * $fScale, 0, 360, 'D');
  103. }
  104. else if ($this->GetProperty('sink'))
  105. {
  106. $oPdf->SetLineStyle(array('width' => 2*$fScale, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => array(51, 51, 204)));
  107. $oPdf->Circle($this->x * $fScale, $this->y * $fScale, 16 * 1.25 * $fScale, 0, 360, 'D');
  108. }
  109. if (!$this->GetProperty('is_reached'))
  110. {
  111. $sTempImageName = $this->CreateWhiteIcon($oGraph, $sIconPath);
  112. if ($sTempImageName != null)
  113. {
  114. $oPdf->Image($sTempImageName, ($this->x - 16)*$fScale, ($this->y - 16)*$fScale, 32*$fScale, 32*$fScale, 'PNG');
  115. }
  116. $Alpha = 0.4;
  117. $oPdf->setAlpha($Alpha);
  118. }
  119. $oPdf->Image($sIconPath, ($this->x - 16)*$fScale, ($this->y - 16)*$fScale, 32*$fScale, 32*$fScale);
  120. $oPdf->SetFont('dejavusans', '', 24 * $fScale, '', true);
  121. $width = $oPdf->GetStringWidth($this->GetProperty('label'));
  122. $height = $oPdf->GetStringHeight(1000, $this->GetProperty('label'));
  123. $oPdf->setAlpha(0.6 * $Alpha);
  124. $oPdf->SetFillColor(255, 255, 255);
  125. $oPdf->SetDrawColor(255, 255, 255);
  126. $oPdf->Rect($this->x*$fScale - $width/2, ($this->y + 18)*$fScale, $width, $height, 'DF');
  127. $oPdf->setAlpha($Alpha);
  128. $oPdf->SetTextColor(0, 0, 0);
  129. $oPdf->Text($this->x*$fScale - $width/2, ($this->y + 18)*$fScale, $this->GetProperty('label'));
  130. }
  131. /**
  132. * Create a "whitened" version of the icon (retaining the transparency) to be used a background for masking the underlying lines
  133. * @param string $sIconFile The path to the file containing the icon
  134. * @return NULL|string The path to a temporary file containing the white version of the icon
  135. */
  136. protected function CreateWhiteIcon(DisplayableGraph $oGraph, $sIconFile)
  137. {
  138. $aInfo = getimagesize($sIconFile);
  139. $im = null;
  140. switch($aInfo['mime'])
  141. {
  142. case 'image/png':
  143. if (function_exists('imagecreatefrompng'))
  144. {
  145. $im = imagecreatefrompng($sIconFile);
  146. }
  147. break;
  148. case 'image/gif':
  149. if (function_exists('imagecreatefromgif'))
  150. {
  151. $im = imagecreatefromgif($sIconFile);
  152. }
  153. break;
  154. case 'image/jpeg':
  155. case 'image/jpg':
  156. if (function_exists('imagecreatefromjpeg'))
  157. {
  158. $im = imagecreatefromjpeg($sIconFile);
  159. }
  160. break;
  161. default:
  162. return null;
  163. }
  164. if($im && imagefilter($im, IMG_FILTER_COLORIZE, 255, 255, 255))
  165. {
  166. $sTempImageName = $oGraph->GetTempImageName();
  167. imagesavealpha($im, true);
  168. imagepng($im, $sTempImageName);
  169. imagedestroy($im);
  170. return $sTempImageName;
  171. }
  172. else
  173. {
  174. return null;
  175. }
  176. }
  177. /**
  178. * Group together (as a special kind of nodes) all the similar neighbours of the current node
  179. * @param DisplayableGraph $oGraph
  180. * @param int $iThresholdCount
  181. * @param boolean $bDirectionUp
  182. * @param boolean $bDirectionDown
  183. */
  184. public function GroupSimilarNeighbours(DisplayableGraph $oGraph, $iThresholdCount, $bDirectionUp = false, $bDirectionDown = true)
  185. {
  186. //echo "<p>".$this->GetProperty('label').":</p>";
  187. if ($this->GetProperty('grouped') === true) return;
  188. $this->SetProperty('grouped', true);
  189. if ($bDirectionDown)
  190. {
  191. $aNodesPerClass = array();
  192. foreach($this->GetOutgoingEdges() as $oEdge)
  193. {
  194. $oNode = $oEdge->GetSinkNode();
  195. if ($oNode->GetProperty('class') !== null)
  196. {
  197. $sClass = $oNode->GetProperty('class');
  198. if (($sClass!== null) && (!array_key_exists($sClass, $aNodesPerClass)))
  199. {
  200. $aNodesPerClass[$sClass] = array(
  201. 'reached' => array(
  202. 'count' => 0,
  203. 'nodes' => array(),
  204. 'icon_url' => $oNode->GetProperty('icon_url'),
  205. ),
  206. 'not_reached' => array(
  207. 'count' => 0,
  208. 'nodes' => array(),
  209. 'icon_url' => $oNode->GetProperty('icon_url'),
  210. )
  211. );
  212. }
  213. $sKey = $oNode->GetProperty('is_reached') ? 'reached' : 'not_reached';
  214. if (!array_key_exists($oNode->GetId(), $aNodesPerClass[$sClass][$sKey]['nodes']))
  215. {
  216. $aNodesPerClass[$sClass][$sKey]['nodes'][$oNode->GetId()] = $oNode;
  217. $aNodesPerClass[$sClass][$sKey]['count'] += (int)$oNode->GetProperty('count', 1);
  218. //echo "<p>New count: ".$aNodesPerClass[$sClass][$sKey]['count']."</p>";
  219. }
  220. }
  221. else
  222. {
  223. $oNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  224. }
  225. }
  226. foreach($aNodesPerClass as $sClass => $aDefs)
  227. {
  228. foreach($aDefs as $sStatus => $aGroupProps)
  229. {
  230. //echo "<p>$sClass/$sStatus: {$aGroupProps['count']} object(s), actually: ".count($aGroupProps['nodes'])."</p>";
  231. if (count($aGroupProps['nodes']) >= $iThresholdCount)
  232. {
  233. $oNewNode = new DisplayableGroupNode($oGraph, $this->GetId().'::'.$sClass);
  234. $oNewNode->SetProperty('label', 'x'.$aGroupProps['count']);
  235. $oNewNode->SetProperty('icon_url', $aGroupProps['icon_url']);
  236. $oNewNode->SetProperty('class', $sClass);
  237. $oNewNode->SetProperty('is_reached', ($sStatus == 'reached'));
  238. $oNewNode->SetProperty('count', $aGroupProps['count']);
  239. //$oNewNode->SetProperty('grouped', true);
  240. $oIncomingEdge = new DisplayableEdge($oGraph, $this->GetId().'-'.$oNewNode->GetId(), $this, $oNewNode);
  241. foreach($aGroupProps['nodes'] as $oNode)
  242. {
  243. foreach($oNode->GetIncomingEdges() as $oEdge)
  244. {
  245. if ($oEdge->GetSourceNode()->GetId() !== $this->GetId())
  246. {
  247. $oNewEdge = new DisplayableEdge($oGraph, $oEdge->GetId().'::'.$sClass, $oEdge->GetSourceNode(), $oNewNode);
  248. }
  249. }
  250. foreach($oNode->GetOutgoingEdges() as $oEdge)
  251. {
  252. $aOutgoing[] = $oEdge->GetSinkNode();
  253. try
  254. {
  255. $oNewEdge = new DisplayableEdge($oGraph, $oEdge->GetId().'::'.$sClass, $oNewNode, $oEdge->GetSinkNode());
  256. }
  257. catch(Exception $e)
  258. {
  259. // ignore this edge
  260. }
  261. }
  262. if ($oGraph->GetNode($oNode->GetId()))
  263. {
  264. $oGraph->_RemoveNode($oNode);
  265. $oNewNode->AddObject($oNode->GetProperty('object'));
  266. }
  267. }
  268. $oNewNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  269. }
  270. else
  271. {
  272. foreach($aGroupProps['nodes'] as $oNode)
  273. {
  274. $oNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. class DisplayableRedundancyNode extends DisplayableNode
  283. {
  284. public function GetWidth()
  285. {
  286. return 24;
  287. }
  288. public function GetForRaphael()
  289. {
  290. $aNode = array();
  291. $aNode['shape'] = 'disc';
  292. $aNode['icon_url'] = $this->GetIconURL();
  293. $aNode['source'] = ($this->GetProperty('source') == true);
  294. $aNode['width'] = $this->GetWidth();
  295. $aNode['x'] = $this->x;
  296. $aNode['y']= $this->y;
  297. $aNode['label'] = $this->GetLabel();
  298. $aNode['id'] = $this->GetId();
  299. $fDiscOpacity = ($this->GetProperty('is_reached') ? 1 : 0.2);
  300. $aNode['disc_attr'] = array('stroke-width' => 3, 'stroke' => '#000', 'fill' => '#c33', 'opacity' => $fDiscOpacity);
  301. $fTextOpacity = ($this->GetProperty('is_reached') ? 1 : 0.4);
  302. $aNode['text_attr'] = array('fill' => '#fff', 'opacity' => $fTextOpacity);
  303. return $aNode;
  304. }
  305. public function RenderAsPDF(TCPDF $oPdf, DisplayableGraph $oGraph, $fScale)
  306. {
  307. $oPdf->SetAlpha(1);
  308. $oPdf->SetFillColor(200, 0, 0);
  309. $oPdf->SetDrawColor(0, 0, 0);
  310. $oPdf->Circle($this->x*$fScale, $this->y*$fScale, 16*$fScale, 0, 360, 'DF');
  311. $oPdf->SetTextColor(255, 255, 255);
  312. $oPdf->SetFont('dejavusans', '', 28 * $fScale, '', true);
  313. $sLabel = (string)$this->GetProperty('label');
  314. $width = $oPdf->GetStringWidth($sLabel, 'dejavusans', 'B', 24*$fScale);
  315. $height = $oPdf->GetStringHeight(1000, $sLabel);
  316. $xPos = (float)$this->x*$fScale - $width/2;
  317. $yPos = (float)$this->y*$fScale - $height/2;
  318. $oPdf->SetXY(($this->x - 16)*$fScale, ($this->y - 16)*$fScale);
  319. $oPdf->Cell(32*$fScale, 32*$fScale, $sLabel, 0, 0, 'C', 0, '', 0, false, 'T', 'C');
  320. }
  321. /**
  322. * @see DisplayableNode::GroupSimilarNeighbours()
  323. */
  324. public function GroupSimilarNeighbours(DisplayableGraph $oGraph, $iThresholdCount, $bDirectionUp = false, $bDirectionDown = true)
  325. {
  326. parent::GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  327. if ($bDirectionUp)
  328. {
  329. $aNodesPerClass = array();
  330. foreach($this->GetIncomingEdges() as $oEdge)
  331. {
  332. $oNode = $oEdge->GetSourceNode();
  333. if (($oNode->GetProperty('class') !== null) && (!$oNode->GetProperty('is_reached')))
  334. {
  335. $sClass = $oNode->GetProperty('class');
  336. if (!array_key_exists($sClass, $aNodesPerClass))
  337. {
  338. $aNodesPerClass[$sClass] = array('reached' => array(), 'not_reached' => array());
  339. }
  340. $aNodesPerClass[$sClass][$oNode->GetProperty('is_reached') ? 'reached' : 'not_reached'][] = $oNode;
  341. }
  342. else
  343. {
  344. //$oNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  345. }
  346. }
  347. foreach($aNodesPerClass as $sClass => $aDefs)
  348. {
  349. foreach($aDefs as $sStatus => $aNodes)
  350. {
  351. //echo "<p>".$this->GetId().' has '.count($aNodes)." neighbours of class $sClass in status $sStatus\n";
  352. if (count($aNodes) >= $iThresholdCount)
  353. {
  354. $oNewNode = new DisplayableGroupNode($oGraph, '-'.$this->GetId().'::'.$sClass.'/'.$sStatus);
  355. $oNewNode->SetProperty('label', 'x'.count($aNodes));
  356. $oNewNode->SetProperty('icon_url', $aNodes[0]->GetProperty('icon_url'));
  357. $oNewNode->SetProperty('is_reached', $aNodes[0]->GetProperty('is_reached'));
  358. $oOutgoingEdge = new DisplayableEdge($oGraph, '-'.$this->GetId().'-'.$oNewNode->GetId().'/'.$sStatus, $oNewNode, $this);
  359. foreach($aNodes as $oNode)
  360. {
  361. foreach($oNode->GetIncomingEdges() as $oEdge)
  362. {
  363. $oNewEdge = new DisplayableEdge($oGraph, '-'.$oEdge->GetId().'::'.$sClass, $oEdge->GetSourceNode(), $oNewNode);
  364. }
  365. foreach($oNode->GetOutgoingEdges() as $oEdge)
  366. {
  367. if ($oEdge->GetSinkNode()->GetId() !== $this->GetId())
  368. {
  369. $aOutgoing[] = $oEdge->GetSinkNode();
  370. $oNewEdge = new DisplayableEdge($oGraph, '-'.$oEdge->GetId().'::'.$sClass.'/'.$sStatus, $oNewNode, $oEdge->GetSinkNode());
  371. }
  372. }
  373. //echo "<p>Replacing ".$oNode->GetId().' by '.$oNewNode->GetId()."\n";
  374. $oGraph->_RemoveNode($oNode);
  375. $oNewNode->AddObject($oNode->GetProperty('object'));
  376. }
  377. //$oNewNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  378. }
  379. else
  380. {
  381. foreach($aNodes as $oNode)
  382. {
  383. //$oNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. }
  391. class DisplayableEdge extends GraphEdge
  392. {
  393. public function RenderAsPDF(TCPDF $oPdf, DisplayableGraph $oGraph, $fScale)
  394. {
  395. $xStart = $this->GetSourceNode()->x * $fScale;
  396. $yStart = $this->GetSourceNode()->y * $fScale;
  397. $xEnd = $this->GetSinkNode()->x * $fScale;
  398. $yEnd = $this->GetSinkNode()->y * $fScale;
  399. $bReached = ($this->GetSourceNode()->GetProperty('is_reached') && $this->GetSinkNode()->GetProperty('is_reached'));
  400. $oPdf->setAlpha(1);
  401. if ($bReached)
  402. {
  403. $aColor = array(100, 100, 100);
  404. }
  405. else
  406. {
  407. $aColor = array(200, 200, 200);
  408. }
  409. $oPdf->SetLineStyle(array('width' => 2*$fScale, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => $aColor));
  410. $oPdf->Line($xStart, $yStart, $xEnd, $yEnd);
  411. $vx = $xEnd - $xStart;
  412. $vy = $yEnd - $yStart;
  413. $l = sqrt($vx*$vx + $vy*$vy);
  414. $vx = $vx / $l;
  415. $vy = $vy / $l;
  416. $ux = -$vy;
  417. $uy = $vx;
  418. $lPos = max($l/2, $l - 40*$fScale);
  419. $iArrowSize = 5*$fScale;
  420. $x = $xStart + $lPos * $vx;
  421. $y = $yStart + $lPos * $vy;
  422. $oPdf->Line($x, $y, $x + $iArrowSize * ($ux-$vx), $y + $iArrowSize * ($uy-$vy));
  423. $oPdf->Line($x, $y, $x - $iArrowSize * ($ux+$vx), $y - $iArrowSize * ($uy+$vy));
  424. }
  425. }
  426. class DisplayableGroupNode extends DisplayableNode
  427. {
  428. protected $aObjects;
  429. public function __construct(SimpleGraph $oGraph, $sId, $x = 0, $y = 0)
  430. {
  431. parent::__construct($oGraph, $sId, $x, $y);
  432. $this->aObjects = array();
  433. }
  434. public function AddObject(DBObject $oObj)
  435. {
  436. $this->aObjects[$oObj->GetKey()] = $oObj;
  437. }
  438. public function GetObjects()
  439. {
  440. return $this->aObjects;
  441. }
  442. public function GetWidth()
  443. {
  444. return 50;
  445. }
  446. public function GetForRaphael()
  447. {
  448. $aNode = array();
  449. $aNode['shape'] = 'group';
  450. $aNode['icon_url'] = $this->GetIconURL();
  451. $aNode['source'] = ($this->GetProperty('source') == true);
  452. $aNode['width'] = $this->GetWidth();
  453. $aNode['x'] = $this->x;
  454. $aNode['y']= $this->y;
  455. $aNode['label'] = $this->GetLabel();
  456. $aNode['id'] = $this->GetId();
  457. $aNode['group_index'] = $this->GetProperty('group_index'); // if supplied
  458. $fDiscOpacity = ($this->GetProperty('is_reached') ? 1 : 0.2);
  459. $fTextOpacity = ($this->GetProperty('is_reached') ? 1 : 0.4);
  460. $aNode['icon_attr'] = array('opacity' => $fTextOpacity);
  461. $aNode['disc_attr'] = array('stroke-width' => 3, 'stroke' => '#000', 'fill' => '#fff', 'opacity' => $fDiscOpacity);
  462. $aNode['text_attr'] = array('fill' => '#000', 'opacity' => $fTextOpacity);
  463. return $aNode;
  464. }
  465. public function RenderAsPDF(TCPDF $oPdf, DisplayableGraph $oGraph, $fScale)
  466. {
  467. $bReached = $this->GetProperty('is_reached');
  468. $oPdf->SetFillColor(255, 255, 255);
  469. if ($bReached)
  470. {
  471. $aBorderColor = array(100, 100, 100);
  472. }
  473. else
  474. {
  475. $aBorderColor = array(200, 200, 200);
  476. }
  477. $oPdf->SetLineStyle(array('width' => 2*$fScale, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => $aBorderColor));
  478. $sIconUrl = $this->GetProperty('icon_url');
  479. $sIconPath = str_replace(utils::GetAbsoluteUrlModulesRoot(), APPROOT.'env-production/', $sIconUrl);
  480. $oPdf->SetAlpha(1);
  481. $oPdf->Circle($this->x*$fScale, $this->y*$fScale, $this->GetWidth() / 2 * $fScale, 0, 360, 'DF');
  482. if ($bReached)
  483. {
  484. $oPdf->SetAlpha(1);
  485. }
  486. else
  487. {
  488. $oPdf->SetAlpha(0.4);
  489. }
  490. $oPdf->Image($sIconPath, ($this->x - 17)*$fScale, ($this->y - 17)*$fScale, 16*$fScale, 16*$fScale);
  491. $oPdf->Image($sIconPath, ($this->x + 1)*$fScale, ($this->y - 17)*$fScale, 16*$fScale, 16*$fScale);
  492. $oPdf->Image($sIconPath, ($this->x -8)*$fScale, ($this->y +1)*$fScale, 16*$fScale, 16*$fScale);
  493. $oPdf->SetFont('dejavusans', '', 24 * $fScale, '', true);
  494. $width = $oPdf->GetStringWidth($this->GetProperty('label'));
  495. $oPdf->SetTextColor(0, 0, 0);
  496. $oPdf->Text($this->x*$fScale - $width/2, ($this->y + 25)*$fScale, $this->GetProperty('label'));
  497. }
  498. }
  499. /**
  500. * A Graph that can be displayed interactively using Raphael JS or saved as a PDF document
  501. */
  502. class DisplayableGraph extends SimpleGraph
  503. {
  504. protected $sDirection;
  505. protected $aTempImages;
  506. public function __construct()
  507. {
  508. parent::__construct();
  509. $this->aTempImages = array();
  510. }
  511. public function GetTempImageName()
  512. {
  513. $sNewTempName = tempnam(APPROOT.'data', 'img-');
  514. $this->aTempImages[] = $sNewTempName;
  515. return $sNewTempName;
  516. }
  517. public function __destruct()
  518. {
  519. foreach($this->aTempImages as $sTempFile)
  520. {
  521. @unlink($sTempFile);
  522. }
  523. }
  524. public static function FromRelationGraph(RelationGraph $oGraph, $iGroupingThreshold = 20, $bDirectionDown = true)
  525. {
  526. $oNewGraph = new DisplayableGraph();
  527. $oNodesIter = new RelationTypeIterator($oGraph, 'Node');
  528. foreach($oNodesIter as $oNode)
  529. {
  530. switch(get_class($oNode))
  531. {
  532. case 'RelationObjectNode':
  533. $oNewNode = new DisplayableNode($oNewGraph, $oNode->GetId(), 0, 0);
  534. if ($oNode->GetProperty('source'))
  535. {
  536. $oNewNode->SetProperty('source', true);
  537. }
  538. if ($oNode->GetProperty('sink'))
  539. {
  540. $oNewNode->SetProperty('sink', true);
  541. }
  542. $oObj = $oNode->GetProperty('object');
  543. $oNewNode->SetProperty('class', get_class($oObj));
  544. $oNewNode->SetProperty('object', $oObj);
  545. $oNewNode->SetProperty('icon_url', $oObj->GetIcon(false));
  546. $oNewNode->SetProperty('label', $oObj->GetRawName());
  547. $oNewNode->SetProperty('is_reached', $bDirectionDown ? $oNode->GetProperty('is_reached') : true); // When going "up" is_reached does not matter
  548. $oNewNode->SetProperty('developped', $oNode->GetProperty('developped'));
  549. break;
  550. default:
  551. $oNewNode = new DisplayableRedundancyNode($oNewGraph, $oNode->GetId(), 0, 0);
  552. $oNewNode->SetProperty('label', $oNode->GetProperty('min_up'));
  553. $oNewNode->SetProperty('is_reached', true);
  554. }
  555. }
  556. $oEdgesIter = new RelationTypeIterator($oGraph, 'Edge');
  557. foreach($oEdgesIter as $oEdge)
  558. {
  559. $oSourceNode = $oNewGraph->GetNode($oEdge->GetSourceNode()->GetId());
  560. $oSinkNode = $oNewGraph->GetNode($oEdge->GetSinkNode()->GetId());
  561. $oNewEdge = new DisplayableEdge($oNewGraph, $oEdge->GetId(), $oSourceNode, $oSinkNode);
  562. }
  563. // Remove duplicate edges between two nodes
  564. $oEdgesIter = new RelationTypeIterator($oNewGraph, 'Edge');
  565. $aEdgeKeys = array();
  566. foreach($oEdgesIter as $oEdge)
  567. {
  568. $sSourceId = $oEdge->GetSourceNode()->GetId();
  569. $sSinkId = $oEdge->GetSinkNode()->GetId();
  570. if ($sSourceId == $sSinkId)
  571. {
  572. // Remove self referring edges
  573. $oNewGraph->_RemoveEdge($oEdge);
  574. }
  575. else
  576. {
  577. $sKey = $sSourceId.'//'.$sSinkId;
  578. if (array_key_exists($sKey, $aEdgeKeys))
  579. {
  580. // Remove duplicate edges
  581. $oNewGraph->_RemoveEdge($oEdge);
  582. }
  583. else
  584. {
  585. $aEdgeKeys[$sKey] = true;
  586. }
  587. }
  588. }
  589. $iNbGrouping = 1;
  590. //for($iter=0; $iter<$iNbGrouping; $iter++)
  591. {
  592. $oNodesIter = new RelationTypeIterator($oNewGraph, 'Node');
  593. foreach($oNodesIter as $oNode)
  594. {
  595. if ($oNode->GetProperty('source'))
  596. {
  597. $oNode->GroupSimilarNeighbours($oNewGraph, $iGroupingThreshold, true, true);
  598. }
  599. }
  600. }
  601. // Remove duplicate edges between two nodes
  602. $oEdgesIter = new RelationTypeIterator($oNewGraph, 'Edge');
  603. $aEdgeKeys = array();
  604. foreach($oEdgesIter as $oEdge)
  605. {
  606. $sSourceId = $oEdge->GetSourceNode()->GetId();
  607. $sSinkId = $oEdge->GetSinkNode()->GetId();
  608. if ($sSourceId == $sSinkId)
  609. {
  610. // Remove self referring edges
  611. $oNewGraph->_RemoveEdge($oEdge);
  612. }
  613. else
  614. {
  615. $sKey = $sSourceId.'//'.$sSinkId;
  616. if (array_key_exists($sKey, $aEdgeKeys))
  617. {
  618. // Remove duplicate edges
  619. $oNewGraph->_RemoveEdge($oEdge);
  620. }
  621. else
  622. {
  623. $aEdgeKeys[$sKey] = true;
  624. }
  625. }
  626. }
  627. return $oNewGraph;
  628. }
  629. public function InitFromGraphviz()
  630. {
  631. $sDot = $this->DumpAsXDot();
  632. if (strpos($sDot, 'digraph') === false)
  633. {
  634. throw new Exception($sDot);
  635. }
  636. $sDot = preg_replace('/.*label=.*,/', '', $sDot); // Get rid of label lines since they may contain weird characters than can break the split and pattern matching below
  637. $aChunks = explode(";", $sDot);
  638. foreach($aChunks as $sChunk)
  639. {
  640. //echo "<p>$sChunk</p>";
  641. if(preg_match('/"([^"]+)".+pos="([0-9\\.]+),([0-9\\.]+)"/ms', $sChunk, $aMatches))
  642. {
  643. $sId = $aMatches[1];
  644. $xPos = $aMatches[2];
  645. $yPos = $aMatches[3];
  646. $oNode = $this->GetNode($sId);
  647. $oNode->x = (float)$xPos;
  648. $oNode->y = (float)$yPos;
  649. //echo "<p>$sId at $xPos,$yPos</p>";
  650. }
  651. else
  652. {
  653. //echo "<p>No match</p>";
  654. }
  655. }
  656. }
  657. public function GetBoundingBox()
  658. {
  659. $xMin = null;
  660. $xMax = null;
  661. $yMin = null;
  662. $yMax = null;
  663. $oIterator = new RelationTypeIterator($this, 'Node');
  664. foreach($oIterator as $sId => $oNode)
  665. {
  666. if ($xMin === null) // First element in the loop
  667. {
  668. $xMin = $oNode->x - $oNode->GetWidth();
  669. $xMax = $oNode->x + $oNode->GetWidth();
  670. $yMin = $oNode->y - $oNode->GetHeight();
  671. $yMax = $oNode->y + $oNode->GetHeight();
  672. }
  673. else
  674. {
  675. $xMin = min($xMin, $oNode->x - $oNode->GetWidth() / 2);
  676. $xMax = max($xMax, $oNode->x + $oNode->GetWidth() / 2);
  677. $yMin = min($yMin, $oNode->y - $oNode->GetHeight() / 2);
  678. $yMax = max($yMax, $oNode->y + $oNode->GetHeight() / 2);
  679. }
  680. }
  681. return array('xmin' => $xMin, 'xmax' => $xMax, 'ymin' => $yMin, 'ymax' => $yMax);
  682. }
  683. function Translate($dx, $dy)
  684. {
  685. $oIterator = new RelationTypeIterator($this, 'Node');
  686. foreach($oIterator as $sId => $oNode)
  687. {
  688. $oNode->x += $dx;
  689. $oNode->y += $dy;
  690. }
  691. }
  692. public function UpdatePositions($aPositions)
  693. {
  694. foreach($aPositions as $sNodeId => $aPos)
  695. {
  696. $oNode = $this->GetNode($sNodeId);
  697. if ($oNode != null)
  698. {
  699. $oNode->x = $aPos['x'];
  700. $oNode->y = $aPos['y'];
  701. }
  702. }
  703. }
  704. function RenderAsRaphael(WebPage $oP, $sId = null, $sExportAsPdfURL, $sExportAsDocumentURL, $sDrillDownURL)
  705. {
  706. if ($sId == null)
  707. {
  708. $sId = 'graph';
  709. }
  710. $aBB = $this->GetBoundingBox();
  711. $oP->add('<div id="'.$sId.'" class="simple-graph"></div>');
  712. $aParams = array(
  713. 'xmin' => $aBB['xmin'],
  714. 'xmax' => $aBB['xmax'],
  715. 'ymin' => $aBB['ymin'],
  716. 'ymax' => $aBB['ymax'],
  717. 'export_as_pdf_url' => $sExportAsPdfURL,
  718. 'export_as_document_url' => $sExportAsDocumentURL,
  719. 'drill_down_url' => $sDrillDownURL,
  720. );
  721. $oP->add_ready_script("var oGraph = $('#$sId').simple_graph(".json_encode($aParams).");");
  722. $oIterator = new RelationTypeIterator($this, 'Node');
  723. foreach($oIterator as $sId => $oNode)
  724. {
  725. $aNode = $oNode->GetForRaphael();
  726. $sJSNode = json_encode($aNode);
  727. $oP->add_ready_script("oGraph.simple_graph('add_node', $sJSNode);");
  728. }
  729. $oIterator = new RelationTypeIterator($this, 'Edge');
  730. foreach($oIterator as $sId => $oEdge)
  731. {
  732. $aEdge = array();
  733. $aEdge['id'] = $oEdge->GetId();
  734. $aEdge['source_node_id'] = $oEdge->GetSourceNode()->GetId();
  735. $aEdge['sink_node_id'] = $oEdge->GetSinkNode()->GetId();
  736. $fOpacity = ($oEdge->GetSinkNode()->GetProperty('is_reached') && $oEdge->GetSourceNode()->GetProperty('is_reached') ? 1 : 0.2);
  737. $aEdge['attr'] = array('opacity' => $fOpacity, 'stroke' => '#000');
  738. $sJSEdge = json_encode($aEdge);
  739. $oP->add_ready_script("oGraph.simple_graph('add_edge', $sJSEdge);");
  740. }
  741. $oP->add_ready_script("oGraph.simple_graph('draw');");
  742. }
  743. function RenderAsPDF(WebPage $oP, $sTitle = 'Untitled', $sPageFormat = 'A4', $sPageOrientation = 'P')
  744. {
  745. require_once(APPROOT.'lib/tcpdf/tcpdf.php');
  746. $oPdf = new TCPDF($sPageOrientation, 'mm', $sPageFormat, true, 'UTF-8', false);
  747. // set document information
  748. $oPdf->SetCreator(PDF_CREATOR);
  749. $oPdf->SetAuthor('iTop');
  750. $oPdf->SetTitle($sTitle);
  751. $oPdf->setFontSubsetting(true);
  752. // Set font
  753. // dejavusans is a UTF-8 Unicode font, if you only need to
  754. // print standard ASCII chars, you can use core fonts like
  755. // helvetica or times to reduce file size.
  756. $oPdf->SetFont('dejavusans', '', 14, '', true);
  757. // set auto page breaks
  758. $oPdf->SetAutoPageBreak(false);
  759. // Add a page
  760. // This method has several options, check the source code documentation for more information.
  761. $oPdf->AddPage();
  762. $aBB = $this->GetBoundingBox();
  763. $this->Translate(-$aBB['xmin'], -$aBB['ymin']);
  764. if ($sPageOrientation == 'P')
  765. {
  766. // Portrait mode
  767. $fHMargin = 10; // mm
  768. $fVMargin = 15; // mm
  769. }
  770. else
  771. {
  772. // Landscape mode
  773. $fHMargin = 15; // mm
  774. $fVMargin = 10; // mm
  775. }
  776. $fPageW = $oPdf->getPageWidth() - 2 * $fHMargin;
  777. $fPageH = $oPdf->getPageHeight() - 2 * $fVMargin;
  778. $w = $aBB['xmax'] - $aBB['xmin'];
  779. $h = $aBB['ymax'] - $aBB['ymin'] + 10; // Extra space for the labels which may appear "below" the icons
  780. $fScale = min($fPageW / $w, $fPageH / $h);
  781. $dx = ($fPageW - $fScale * $w) / 2;
  782. $dy = ($fPageH - $fScale * $h) / 2;
  783. $this->Translate(($fHMargin + $dx)/$fScale, ($fVMargin + $dy)/$fScale);
  784. $oIterator = new RelationTypeIterator($this, 'Edge');
  785. foreach($oIterator as $sId => $oEdge)
  786. {
  787. $oEdge->RenderAsPDF($oPdf, $this, $fScale);
  788. }
  789. $oIterator = new RelationTypeIterator($this, 'Node');
  790. foreach($oIterator as $sId => $oNode)
  791. {
  792. $oNode->RenderAsPDF($oPdf, $this, $fScale);
  793. }
  794. $oP->add($oPdf->Output('iTop.pdf', 'S'));
  795. }
  796. }