displayablegraph.class.inc.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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($aContextDefs)
  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. $aNode['tooltip'] = $this->GetTooltip($aContextDefs);
  91. $aNode['context_icons'] = array();
  92. $aContextRootCauses = $this->GetProperty('context_root_causes');
  93. if (!is_null($aContextRootCauses))
  94. {
  95. foreach($aContextRootCauses as $key => $aObjects)
  96. {
  97. $aNode['context_icons'][] = utils::GetAbsoluteUrlModulesRoot().$aContextDefs[$key]['icon'];
  98. }
  99. }
  100. return $aNode;
  101. }
  102. public function RenderAsPDF(TCPDF $oPdf, DisplayableGraph $oGraph, $fScale, $aContextDefs)
  103. {
  104. $Alpha = 1.0;
  105. $oPdf->SetFillColor(200, 200, 200);
  106. $oPdf->setAlpha(1);
  107. $sIconUrl = $this->GetProperty('icon_url');
  108. $sIconPath = str_replace(utils::GetAbsoluteUrlModulesRoot(), APPROOT.'env-'.utils::GetCurrentEnvironment().'/', $sIconUrl);
  109. if ($this->GetProperty('source'))
  110. {
  111. $oPdf->SetLineStyle(array('width' => 2*$fScale, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => array(204, 51, 51)));
  112. $oPdf->Circle($this->x * $fScale, $this->y * $fScale, 16 * 1.25 * $fScale, 0, 360, 'D');
  113. }
  114. else if ($this->GetProperty('sink'))
  115. {
  116. $oPdf->SetLineStyle(array('width' => 2*$fScale, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => array(51, 51, 204)));
  117. $oPdf->Circle($this->x * $fScale, $this->y * $fScale, 16 * 1.25 * $fScale, 0, 360, 'D');
  118. }
  119. if (!$this->GetProperty('is_reached'))
  120. {
  121. $sTempImageName = $this->CreateWhiteIcon($oGraph, $sIconPath);
  122. if ($sTempImageName != null)
  123. {
  124. $oPdf->Image($sTempImageName, ($this->x - 16)*$fScale, ($this->y - 16)*$fScale, 32*$fScale, 32*$fScale, 'PNG');
  125. }
  126. $Alpha = 0.4;
  127. $oPdf->setAlpha($Alpha);
  128. }
  129. $oPdf->Image($sIconPath, ($this->x - 16)*$fScale, ($this->y - 16)*$fScale, 32*$fScale, 32*$fScale);
  130. $aContextRootCauses = $this->GetProperty('context_root_causes');
  131. if (!is_null($aContextRootCauses))
  132. {
  133. $idx = 0;
  134. foreach($aContextRootCauses as $key => $aObjects)
  135. {
  136. $sgn = 2*($idx %2) -1;
  137. $coef = floor((1+$idx)/2) * $sgn;
  138. $alpha = $coef*pi()/4 - pi()/2;
  139. $x = $this->x * $fScale + cos($alpha) * 16*1.25 * $fScale;
  140. $y = $this->y * $fScale + sin($alpha) * 16*1.25 * $fScale;
  141. $l = 32 * $fScale / 3;
  142. $sIconPath = APPROOT.'env-'.utils::GetCurrentEnvironment().'/'.$aContextDefs[$key]['icon'];
  143. $oPdf->Image($sIconPath, $x - $l/2, $y - $l/2, $l, $l);
  144. $idx++;
  145. }
  146. }
  147. $oPdf->SetFont('dejavusans', '', 24 * $fScale, '', true);
  148. $width = $oPdf->GetStringWidth($this->GetProperty('label'));
  149. $height = $oPdf->GetStringHeight(1000, $this->GetProperty('label'));
  150. $oPdf->setAlpha(0.6 * $Alpha);
  151. $oPdf->SetFillColor(255, 255, 255);
  152. $oPdf->SetDrawColor(255, 255, 255);
  153. $oPdf->Rect($this->x*$fScale - $width/2, ($this->y + 18)*$fScale, $width, $height, 'DF');
  154. $oPdf->setAlpha($Alpha);
  155. $oPdf->SetTextColor(0, 0, 0);
  156. $oPdf->Text($this->x*$fScale - $width/2, ($this->y + 18)*$fScale, $this->GetProperty('label'));
  157. }
  158. /**
  159. * Create a "whitened" version of the icon (retaining the transparency) to be used a background for masking the underlying lines
  160. * @param string $sIconFile The path to the file containing the icon
  161. * @return NULL|string The path to a temporary file containing the white version of the icon
  162. */
  163. protected function CreateWhiteIcon(DisplayableGraph $oGraph, $sIconFile)
  164. {
  165. $aInfo = getimagesize($sIconFile);
  166. $im = null;
  167. switch($aInfo['mime'])
  168. {
  169. case 'image/png':
  170. if (function_exists('imagecreatefrompng'))
  171. {
  172. $im = imagecreatefrompng($sIconFile);
  173. }
  174. break;
  175. case 'image/gif':
  176. if (function_exists('imagecreatefromgif'))
  177. {
  178. $im = imagecreatefromgif($sIconFile);
  179. }
  180. break;
  181. case 'image/jpeg':
  182. case 'image/jpg':
  183. if (function_exists('imagecreatefromjpeg'))
  184. {
  185. $im = imagecreatefromjpeg($sIconFile);
  186. }
  187. break;
  188. default:
  189. return null;
  190. }
  191. if($im && imagefilter($im, IMG_FILTER_COLORIZE, 255, 255, 255))
  192. {
  193. $sTempImageName = $oGraph->GetTempImageName();
  194. imagesavealpha($im, true);
  195. imagepng($im, $sTempImageName);
  196. imagedestroy($im);
  197. return $sTempImageName;
  198. }
  199. else
  200. {
  201. return null;
  202. }
  203. }
  204. public function GetObjectCount()
  205. {
  206. return 1;
  207. }
  208. public function GetObjectClass()
  209. {
  210. return is_object($this->GetProperty('object', null)) ? get_class($this->GetProperty('object', null)) : null;
  211. }
  212. /**
  213. * Group together (as a special kind of nodes) all the similar neighbours of the current node
  214. * @param DisplayableGraph $oGraph
  215. * @param int $iThresholdCount
  216. * @param boolean $bDirectionUp
  217. * @param boolean $bDirectionDown
  218. */
  219. public function GroupSimilarNeighbours(DisplayableGraph $oGraph, $iThresholdCount, $bDirectionUp = false, $bDirectionDown = true)
  220. {
  221. if ($this->GetProperty('grouped') === true) return;
  222. $this->SetProperty('grouped', true);
  223. if ($bDirectionDown)
  224. {
  225. $aNodesPerClass = array();
  226. foreach($this->GetOutgoingEdges() as $oEdge)
  227. {
  228. $oNode = $oEdge->GetSinkNode();
  229. $sClass = $oNode->GetObjectClass();
  230. if ($sClass !== null)
  231. {
  232. if (!array_key_exists($sClass, $aNodesPerClass))
  233. {
  234. $aNodesPerClass[$sClass] = array(
  235. 'reached' => array(
  236. 'count' => 0,
  237. 'nodes' => array(),
  238. 'icon_url' => $oNode->GetProperty('icon_url'),
  239. ),
  240. 'not_reached' => array(
  241. 'count' => 0,
  242. 'nodes' => array(),
  243. 'icon_url' => $oNode->GetProperty('icon_url'),
  244. )
  245. );
  246. }
  247. $sKey = $oNode->GetProperty('is_reached') ? 'reached' : 'not_reached';
  248. if (!array_key_exists($oNode->GetId(), $aNodesPerClass[$sClass][$sKey]['nodes']))
  249. {
  250. $aNodesPerClass[$sClass][$sKey]['nodes'][$oNode->GetId()] = $oNode;
  251. $aNodesPerClass[$sClass][$sKey]['count'] += $oNode->GetObjectCount();
  252. }
  253. }
  254. else
  255. {
  256. $oNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  257. }
  258. }
  259. foreach($aNodesPerClass as $sClass => $aDefs)
  260. {
  261. foreach($aDefs as $sStatus => $aGroupProps)
  262. {
  263. if (count($aGroupProps['nodes']) >= $iThresholdCount)
  264. {
  265. $sNewId = $this->GetId().'::'.$sClass.'/'.(($sStatus == 'reached') ? '_reached': '');
  266. $oNewNode = $oGraph->GetNode($sNewId);
  267. if ($oNewNode == null)
  268. {
  269. $oNewNode = new DisplayableGroupNode($oGraph, $sNewId);
  270. $oNewNode->SetProperty('label', 'x'.$aGroupProps['count']);
  271. $oNewNode->SetProperty('icon_url', $aGroupProps['icon_url']);
  272. $oNewNode->SetProperty('class', $sClass);
  273. $oNewNode->SetProperty('is_reached', ($sStatus == 'reached'));
  274. $oNewNode->SetProperty('count', $aGroupProps['count']);
  275. }
  276. try
  277. {
  278. $oIncomingEdge = new DisplayableEdge($oGraph, $this->GetId().'-'.$oNewNode->GetId(), $this, $oNewNode);
  279. }
  280. catch(Exception $e)
  281. {
  282. // Ignore this redundant egde
  283. }
  284. foreach($aGroupProps['nodes'] as $oNode)
  285. {
  286. foreach($oNode->GetIncomingEdges() as $oEdge)
  287. {
  288. if ($oEdge->GetSourceNode()->GetId() !== $this->GetId())
  289. {
  290. try
  291. {
  292. $oNewEdge = new DisplayableEdge($oGraph, $oEdge->GetId().'::'.$sClass, $oEdge->GetSourceNode(), $oNewNode);
  293. }
  294. catch(Exception $e)
  295. {
  296. // ignore this edge
  297. }
  298. }
  299. }
  300. foreach($oNode->GetOutgoingEdges() as $oEdge)
  301. {
  302. $aOutgoing[] = $oEdge->GetSinkNode();
  303. try
  304. {
  305. $oNewEdge = new DisplayableEdge($oGraph, $oEdge->GetId().'::'.$sClass, $oNewNode, $oEdge->GetSinkNode());
  306. }
  307. catch(Exception $e)
  308. {
  309. // ignore this edge
  310. }
  311. }
  312. if ($oGraph->GetNode($oNode->GetId()))
  313. {
  314. $oGraph->_RemoveNode($oNode);
  315. if ($oNode instanceof DisplayableGroupNode)
  316. {
  317. // Copy all the objects of the previous group into the new group
  318. foreach($oNode->GetObjects() as $oObj)
  319. {
  320. $oNewNode->AddObject($oObj);
  321. }
  322. }
  323. else
  324. {
  325. $oNewNode->AddObject($oNode->GetProperty('object'));
  326. }
  327. }
  328. }
  329. $oNewNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  330. }
  331. else
  332. {
  333. foreach($aGroupProps['nodes'] as $oNode)
  334. {
  335. $oNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  336. }
  337. }
  338. }
  339. }
  340. }
  341. }
  342. public function GetTooltip($aContextDefs)
  343. {
  344. $sHtml = '';
  345. $oCurrObj = $this->GetProperty('object');
  346. $sSubClass = get_class($oCurrObj);
  347. $sHtml .= $oCurrObj->GetHyperlink()."<hr/>";
  348. $aContextRootCauses = $this->GetProperty('context_root_causes');
  349. if (!is_null($aContextRootCauses))
  350. {
  351. foreach($aContextRootCauses as $key => $aObjects)
  352. {
  353. $aContext = $aContextDefs[$key];
  354. $aRootCauses = array();
  355. foreach($aObjects as $oRootCause)
  356. {
  357. $aRootCauses[] = $oRootCause->GetHyperlink();
  358. }
  359. $sHtml .= '<p><img style="max-height: 24px; vertical-align:bottom;" src="'.utils::GetAbsoluteUrlModulesRoot().$aContext['icon'].'" title="'.htmlentities(Dict::S($aContext['dict'])).'">&nbsp;'.implode(', ', $aRootCauses).'</p>';
  360. }
  361. $sHtml .= '<hr/>';
  362. }
  363. $sHtml .= '<table><tbody>';
  364. foreach(MetaModel::GetZListItems($sSubClass, 'list') as $sAttCode)
  365. {
  366. $oAttDef = MetaModel::GetAttributeDef($sSubClass, $sAttCode);
  367. $sHtml .= '<tr><td>'.$oAttDef->GetLabel().':&nbsp;</td><td>'.$oCurrObj->GetAsHtml($sAttCode).'</td></tr>';
  368. }
  369. $sHtml .= '</tbody></table>';
  370. return $sHtml;
  371. }
  372. /**
  373. * Get the description of the node in "dot" language
  374. * Used to generate the positions in the graph, but we'd better use fake label
  375. * just to retain the space used by the node, without compromising the parsing
  376. * of the result which may occur when using the real labels (with possible weird characters in the middle)
  377. */
  378. public function GetDotAttributes($bNoLabel = false)
  379. {
  380. $sDot = '';
  381. if ($bNoLabel)
  382. {
  383. // simulate a fake label with the approximate same size as the true label
  384. $sLabel = str_repeat('x',strlen($this->GetProperty('label', $this->GetId())));
  385. $sDot = 'label="'.$sLabel.'"';
  386. }
  387. else
  388. {
  389. // actual label
  390. $sLabel = addslashes($this->GetProperty('label', $this->GetId()));
  391. $sDot = 'label="'.$sLabel.'"';
  392. }
  393. return $sDot;
  394. }
  395. }
  396. class DisplayableRedundancyNode extends DisplayableNode
  397. {
  398. public function GetWidth()
  399. {
  400. return 24;
  401. }
  402. public function GetForRaphael($aContextDefs)
  403. {
  404. $aNode = array();
  405. $aNode['shape'] = 'disc';
  406. $aNode['icon_url'] = $this->GetIconURL();
  407. $aNode['source'] = ($this->GetProperty('source') == true);
  408. $aNode['width'] = $this->GetWidth();
  409. $aNode['x'] = $this->x;
  410. $aNode['y']= $this->y;
  411. $aNode['label'] = $this->GetLabel();
  412. $aNode['id'] = $this->GetId();
  413. $fDiscOpacity = ($this->GetProperty('is_reached') ? 1 : 0.2);
  414. $sColor = ($this->GetProperty('is_reached_count') > $this->GetProperty('threshold')) ? '#c33' : '#999';
  415. $aNode['disc_attr'] = array('stroke-width' => 3, 'stroke' => '#000', 'fill' => $sColor, 'opacity' => $fDiscOpacity);
  416. $fTextOpacity = ($this->GetProperty('is_reached') ? 1 : 0.4);
  417. $aNode['text_attr'] = array('fill' => '#fff', 'opacity' => $fTextOpacity);
  418. $aNode['tooltip'] = $this->GetTooltip($aContextDefs);
  419. return $aNode;
  420. }
  421. public function RenderAsPDF(TCPDF $oPdf, DisplayableGraph $oGraph, $fScale, $aContextDefs)
  422. {
  423. $oPdf->SetAlpha(1);
  424. if($this->GetProperty('is_reached_count') > $this->GetProperty('threshold'))
  425. {
  426. $oPdf->SetFillColor(200, 0, 0);
  427. }
  428. else
  429. {
  430. $oPdf->SetFillColor(144, 144, 144);
  431. }
  432. $oPdf->SetDrawColor(0, 0, 0);
  433. $oPdf->Circle($this->x*$fScale, $this->y*$fScale, 16*$fScale, 0, 360, 'DF');
  434. $oPdf->SetTextColor(255, 255, 255);
  435. $oPdf->SetFont('dejavusans', '', 28 * $fScale, '', true);
  436. $sLabel = (string)$this->GetProperty('label');
  437. $width = $oPdf->GetStringWidth($sLabel, 'dejavusans', 'B', 24*$fScale);
  438. $height = $oPdf->GetStringHeight(1000, $sLabel);
  439. $xPos = (float)$this->x*$fScale - $width/2;
  440. $yPos = (float)$this->y*$fScale - $height/2;
  441. $oPdf->SetXY(($this->x - 16)*$fScale, ($this->y - 16)*$fScale);
  442. $oPdf->Cell(32*$fScale, 32*$fScale, $sLabel, 0, 0, 'C', 0, '', 0, false, 'T', 'C');
  443. }
  444. /**
  445. * @see DisplayableNode::GroupSimilarNeighbours()
  446. */
  447. public function GroupSimilarNeighbours(DisplayableGraph $oGraph, $iThresholdCount, $bDirectionUp = false, $bDirectionDown = true)
  448. {
  449. parent::GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  450. if ($bDirectionUp)
  451. {
  452. $aNodesPerClass = array();
  453. foreach($this->GetIncomingEdges() as $oEdge)
  454. {
  455. $oNode = $oEdge->GetSourceNode();
  456. if (($oNode->GetObjectClass() !== null) && (!$oNode->GetProperty('is_reached')))
  457. {
  458. $sClass = $oNode->GetObjectClass();
  459. if (!array_key_exists($sClass, $aNodesPerClass))
  460. {
  461. $aNodesPerClass[$sClass] = array('reached' => array(), 'not_reached' => array());
  462. }
  463. $aNodesPerClass[$sClass][$oNode->GetProperty('is_reached') ? 'reached' : 'not_reached'][] = $oNode;
  464. }
  465. else
  466. {
  467. //$oNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  468. }
  469. }
  470. foreach($aNodesPerClass as $sClass => $aDefs)
  471. {
  472. foreach($aDefs as $sStatus => $aNodes)
  473. {
  474. if (count($aNodes) >= $iThresholdCount)
  475. {
  476. $oNewNode = new DisplayableGroupNode($oGraph, '-'.$this->GetId().'::'.$sClass.'/'.$sStatus);
  477. $oNewNode->SetProperty('label', 'x'.count($aNodes));
  478. $oNewNode->SetProperty('icon_url', $aNodes[0]->GetProperty('icon_url'));
  479. $oNewNode->SetProperty('is_reached', $aNodes[0]->GetProperty('is_reached'));
  480. $oOutgoingEdge = new DisplayableEdge($oGraph, '-'.$this->GetId().'-'.$oNewNode->GetId().'/'.$sStatus, $oNewNode, $this);
  481. foreach($aNodes as $oNode)
  482. {
  483. foreach($oNode->GetIncomingEdges() as $oEdge)
  484. {
  485. $oNewEdge = new DisplayableEdge($oGraph, '-'.$oEdge->GetId().'::'.$sClass, $oEdge->GetSourceNode(), $oNewNode);
  486. }
  487. foreach($oNode->GetOutgoingEdges() as $oEdge)
  488. {
  489. if ($oEdge->GetSinkNode()->GetId() !== $this->GetId())
  490. {
  491. $aOutgoing[] = $oEdge->GetSinkNode();
  492. $oNewEdge = new DisplayableEdge($oGraph, '-'.$oEdge->GetId().'::'.$sClass.'/'.$sStatus, $oNewNode, $oEdge->GetSinkNode());
  493. }
  494. }
  495. $oGraph->_RemoveNode($oNode);
  496. $oNewNode->AddObject($oNode->GetProperty('object'));
  497. }
  498. //$oNewNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  499. }
  500. else
  501. {
  502. foreach($aNodes as $oNode)
  503. {
  504. //$oNode->GroupSimilarNeighbours($oGraph, $iThresholdCount, $bDirectionUp, $bDirectionDown);
  505. }
  506. }
  507. }
  508. }
  509. }
  510. }
  511. public function GetTooltip($aContextDefs)
  512. {
  513. $sHtml = '';
  514. $sHtml .= Dict::S('UI:RelationTooltip:Redundancy')."<hr>";
  515. $sHtml .= '<table><tbody>';
  516. $sHtml .= "<tr><td>".Dict::Format('UI:RelationTooltip:ImpactedItems_N_of_M' , $this->GetProperty('is_reached_count'), $this->GetProperty('min_up') + $this->GetProperty('threshold'))."</td></tr>";
  517. $sHtml .= "<tr><td>".Dict::Format('UI:RelationTooltip:CriticalThreshold_N_of_M' , $this->GetProperty('threshold'), $this->GetProperty('min_up') + $this->GetProperty('threshold'))."</td></tr>";
  518. $sHtml .= '</tbody></table>';
  519. return $sHtml;
  520. }
  521. public function GetObjectCount()
  522. {
  523. return 0;
  524. }
  525. public function GetObjectClass()
  526. {
  527. return null;
  528. }
  529. }
  530. class DisplayableEdge extends GraphEdge
  531. {
  532. public function RenderAsPDF(TCPDF $oPdf, DisplayableGraph $oGraph, $fScale, $aContextDefs)
  533. {
  534. $xStart = $this->GetSourceNode()->x * $fScale;
  535. $yStart = $this->GetSourceNode()->y * $fScale;
  536. $xEnd = $this->GetSinkNode()->x * $fScale;
  537. $yEnd = $this->GetSinkNode()->y * $fScale;
  538. $bReached = ($this->GetSourceNode()->GetProperty('is_reached') && $this->GetSinkNode()->GetProperty('is_reached'));
  539. $oPdf->setAlpha(1);
  540. if ($bReached)
  541. {
  542. $aColor = array(100, 100, 100);
  543. }
  544. else
  545. {
  546. $aColor = array(200, 200, 200);
  547. }
  548. $oPdf->SetLineStyle(array('width' => 2*$fScale, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => $aColor));
  549. $oPdf->Line($xStart, $yStart, $xEnd, $yEnd);
  550. $vx = $xEnd - $xStart;
  551. $vy = $yEnd - $yStart;
  552. $l = sqrt($vx*$vx + $vy*$vy);
  553. $vx = $vx / $l;
  554. $vy = $vy / $l;
  555. $ux = -$vy;
  556. $uy = $vx;
  557. $lPos = max($l/2, $l - 40*$fScale);
  558. $iArrowSize = 5*$fScale;
  559. $x = $xStart + $lPos * $vx;
  560. $y = $yStart + $lPos * $vy;
  561. $oPdf->Line($x, $y, $x + $iArrowSize * ($ux-$vx), $y + $iArrowSize * ($uy-$vy));
  562. $oPdf->Line($x, $y, $x - $iArrowSize * ($ux+$vx), $y - $iArrowSize * ($uy+$vy));
  563. }
  564. }
  565. class DisplayableGroupNode extends DisplayableNode
  566. {
  567. protected $aObjects;
  568. public function __construct(SimpleGraph $oGraph, $sId, $x = 0, $y = 0)
  569. {
  570. parent::__construct($oGraph, $sId, $x, $y);
  571. $this->aObjects = array();
  572. }
  573. public function AddObject(DBObject $oObj)
  574. {
  575. $sPrevClass = $this->GetObjectClass();
  576. if (($sPrevClass !== null) && (get_class($oObj) !== $sPrevClass))
  577. {
  578. throw new Exception("Error: adding an object of class '".get_class($oObj)."' to a group of '$sPrevClass' objects.");
  579. }
  580. $this->aObjects[$oObj->GetKey()] = $oObj;
  581. }
  582. public function GetObjects()
  583. {
  584. return $this->aObjects;
  585. }
  586. public function GetWidth()
  587. {
  588. return 50;
  589. }
  590. public function GetForRaphael($aContextDefs)
  591. {
  592. $aNode = array();
  593. $aNode['shape'] = 'group';
  594. $aNode['icon_url'] = $this->GetIconURL();
  595. $aNode['source'] = ($this->GetProperty('source') == true);
  596. $aNode['width'] = $this->GetWidth();
  597. $aNode['x'] = $this->x;
  598. $aNode['y']= $this->y;
  599. $aNode['label'] = $this->GetLabel();
  600. $aNode['id'] = $this->GetId();
  601. $aNode['group_index'] = $this->GetProperty('group_index'); // if supplied
  602. $fDiscOpacity = ($this->GetProperty('is_reached') ? 1 : 0.2);
  603. $fTextOpacity = ($this->GetProperty('is_reached') ? 1 : 0.4);
  604. $aNode['icon_attr'] = array('opacity' => $fTextOpacity);
  605. $aNode['disc_attr'] = array('stroke-width' => 3, 'stroke' => '#000', 'fill' => '#fff', 'opacity' => $fDiscOpacity);
  606. $aNode['text_attr'] = array('fill' => '#000', 'opacity' => $fTextOpacity);
  607. $aNode['tooltip'] = $this->GetTooltip($aContextDefs);
  608. return $aNode;
  609. }
  610. public function RenderAsPDF(TCPDF $oPdf, DisplayableGraph $oGraph, $fScale, $aContextDefs)
  611. {
  612. $bReached = $this->GetProperty('is_reached');
  613. $oPdf->SetFillColor(255, 255, 255);
  614. if ($bReached)
  615. {
  616. $aBorderColor = array(100, 100, 100);
  617. }
  618. else
  619. {
  620. $aBorderColor = array(200, 200, 200);
  621. }
  622. $oPdf->SetLineStyle(array('width' => 2*$fScale, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => $aBorderColor));
  623. $sIconUrl = $this->GetProperty('icon_url');
  624. $sIconPath = str_replace(utils::GetAbsoluteUrlModulesRoot(), APPROOT.'env-'.utils::GetCurrentEnvironment().'/', $sIconUrl);
  625. $oPdf->SetAlpha(1);
  626. $oPdf->Circle($this->x*$fScale, $this->y*$fScale, $this->GetWidth() / 2 * $fScale, 0, 360, 'DF');
  627. if ($bReached)
  628. {
  629. $oPdf->SetAlpha(1);
  630. }
  631. else
  632. {
  633. $oPdf->SetAlpha(0.4);
  634. }
  635. $oPdf->Image($sIconPath, ($this->x - 17)*$fScale, ($this->y - 17)*$fScale, 16*$fScale, 16*$fScale);
  636. $oPdf->Image($sIconPath, ($this->x + 1)*$fScale, ($this->y - 17)*$fScale, 16*$fScale, 16*$fScale);
  637. $oPdf->Image($sIconPath, ($this->x -8)*$fScale, ($this->y +1)*$fScale, 16*$fScale, 16*$fScale);
  638. $oPdf->SetFont('dejavusans', '', 24 * $fScale, '', true);
  639. $width = $oPdf->GetStringWidth($this->GetProperty('label'));
  640. $oPdf->SetTextColor(0, 0, 0);
  641. $oPdf->Text($this->x*$fScale - $width/2, ($this->y + 25)*$fScale, $this->GetProperty('label'));
  642. }
  643. public function GetTooltip($aContextDefs)
  644. {
  645. $sHtml = '';
  646. $iGroupIdx = $this->GetProperty('group_index');
  647. $sHtml .= '<a href="#" onclick="$(\'.itop-simple-graph\').simple_graph(\'show_group\', \'relation_group_'.$iGroupIdx.'\');">'.Dict::Format('UI:RelationGroupNumber_N', (1+$iGroupIdx))."</a>";
  648. $sHtml .= '<hr/>';
  649. $sHtml .= '<table><tbody><tr>';
  650. $sHtml .= '<td style="vertical-align:top;padding-right: 0.5em;"><img src="'.$this->GetProperty('icon_url').'"></td><td style="vertical-align:top">'.MetaModel::GetName($this->GetObjectClass()).'<br/>';
  651. $sHtml .= Dict::Format('UI_CountOfObjectsShort', $this->GetObjectCount()).'</td>';
  652. $sHtml .= '</tr></tbody></table>';
  653. return $sHtml;
  654. }
  655. public function GetObjectCount()
  656. {
  657. return count($this->aObjects);
  658. }
  659. public function GetObjectClass()
  660. {
  661. return ($this->GetObjectCount() > 0) ? get_class(reset($this->aObjects)) : null;
  662. }
  663. }
  664. /**
  665. * A Graph that can be displayed interactively using Raphael JS or saved as a PDF document
  666. */
  667. class DisplayableGraph extends SimpleGraph
  668. {
  669. protected $bDirectionDown;
  670. protected $aTempImages;
  671. protected $aSourceObjects;
  672. protected $aSinkObjects;
  673. public function __construct()
  674. {
  675. parent::__construct();
  676. $this->aTempImages = array();
  677. $this->aSourceObjects = array();
  678. $this->aSinkObjects = array();
  679. }
  680. public function GetTempImageName()
  681. {
  682. $sNewTempName = tempnam(APPROOT.'data', 'img-');
  683. $this->aTempImages[] = $sNewTempName;
  684. return $sNewTempName;
  685. }
  686. public function __destruct()
  687. {
  688. foreach($this->aTempImages as $sTempFile)
  689. {
  690. @unlink($sTempFile);
  691. }
  692. }
  693. /**
  694. * Build a DisplayableGraph from a RelationGraph
  695. * @param RelationGraph $oGraph
  696. * @param number $iGroupingThreshold
  697. * @param string $bDirectionDown
  698. * @return DisplayableGraph
  699. */
  700. public static function FromRelationGraph(RelationGraph $oGraph, $iGroupingThreshold = 20, $bDirectionDown = true)
  701. {
  702. $oNewGraph = new DisplayableGraph();
  703. $oNewGraph->bDirectionDown = $bDirectionDown;
  704. $iPreviousTimeLimit = ini_get('max_execution_time');
  705. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  706. $oNodesIter = new RelationTypeIterator($oGraph, 'Node');
  707. foreach($oNodesIter as $oNode)
  708. {
  709. set_time_limit($iLoopTimeLimit);
  710. switch(get_class($oNode))
  711. {
  712. case 'RelationObjectNode':
  713. $oNewNode = new DisplayableNode($oNewGraph, $oNode->GetId(), 0, 0);
  714. $oObj = $oNode->GetProperty('object');
  715. $sClass = get_class($oObj);
  716. if ($oNode->GetProperty('source'))
  717. {
  718. if (!array_key_exists($sClass, $oNewGraph->aSourceObjects))
  719. {
  720. $oNewGraph->aSourceObjects[$sClass] = array();
  721. }
  722. $oNewGraph->aSourceObjects[$sClass][] = $oObj->GetKey();
  723. $oNewNode->SetProperty('source', true);
  724. }
  725. if ($oNode->GetProperty('sink'))
  726. {
  727. if (!array_key_exists($sClass, $oNewGraph->aSinkObjects))
  728. {
  729. $oNewGraph->aSinkObjects[$sClass] = array();
  730. }
  731. $oNewGraph->aSinkObjects[$sClass][] = $oObj->GetKey();
  732. $oNewNode->SetProperty('sink', true);
  733. }
  734. $oNewNode->SetProperty('object', $oObj);
  735. $oNewNode->SetProperty('icon_url', $oObj->GetIcon(false));
  736. $oNewNode->SetProperty('label', $oObj->GetRawName());
  737. $oNewNode->SetProperty('is_reached', $bDirectionDown ? $oNode->GetProperty('is_reached') : true); // When going "up" is_reached does not matter
  738. $oNewNode->SetProperty('is_reached_allowed', $oNode->GetProperty('is_reached_allowed'));
  739. $oNewNode->SetProperty('context_root_causes', $oNode->GetProperty('context_root_causes'));
  740. break;
  741. default:
  742. $oNewNode = new DisplayableRedundancyNode($oNewGraph, $oNode->GetId(), 0, 0);
  743. $iNbReached = (is_null($oNode->GetProperty('is_reached_count'))) ? 0 : $oNode->GetProperty('is_reached_count');
  744. $oNewNode->SetProperty('label', $iNbReached."/".($oNode->GetProperty('min_up') + $oNode->GetProperty('threshold')));
  745. $oNewNode->SetProperty('min_up', $oNode->GetProperty('min_up'));
  746. $oNewNode->SetProperty('threshold', $oNode->GetProperty('threshold'));
  747. $oNewNode->SetProperty('is_reached_count', $iNbReached);
  748. $oNewNode->SetProperty('is_reached', true);
  749. }
  750. }
  751. $oEdgesIter = new RelationTypeIterator($oGraph, 'Edge');
  752. foreach($oEdgesIter as $oEdge)
  753. {
  754. set_time_limit($iLoopTimeLimit);
  755. $oSourceNode = $oNewGraph->GetNode($oEdge->GetSourceNode()->GetId());
  756. $oSinkNode = $oNewGraph->GetNode($oEdge->GetSinkNode()->GetId());
  757. $oNewEdge = new DisplayableEdge($oNewGraph, $oEdge->GetId(), $oSourceNode, $oSinkNode);
  758. }
  759. // Remove duplicate edges between two nodes
  760. $oEdgesIter = new RelationTypeIterator($oNewGraph, 'Edge');
  761. $aEdgeKeys = array();
  762. foreach($oEdgesIter as $oEdge)
  763. {
  764. set_time_limit($iLoopTimeLimit);
  765. $sSourceId = $oEdge->GetSourceNode()->GetId();
  766. $sSinkId = $oEdge->GetSinkNode()->GetId();
  767. if ($sSourceId == $sSinkId)
  768. {
  769. // Remove self referring edges
  770. $oNewGraph->_RemoveEdge($oEdge);
  771. }
  772. else
  773. {
  774. $sKey = $sSourceId.'//'.$sSinkId;
  775. if (array_key_exists($sKey, $aEdgeKeys))
  776. {
  777. // Remove duplicate edges
  778. $oNewGraph->_RemoveEdge($oEdge);
  779. }
  780. else
  781. {
  782. $aEdgeKeys[$sKey] = true;
  783. }
  784. }
  785. }
  786. $oNodesIter = new RelationTypeIterator($oNewGraph, 'Node');
  787. foreach($oNodesIter as $oNode)
  788. {
  789. set_time_limit($iLoopTimeLimit);
  790. if ($oNode->GetProperty('source'))
  791. {
  792. $oNode->GroupSimilarNeighbours($oNewGraph, $iGroupingThreshold, true, true);
  793. }
  794. }
  795. // Groups numbering
  796. $oIterator = new RelationTypeIterator($oNewGraph, 'Node');
  797. $iGroupIdx = 0;
  798. foreach($oIterator as $oNode)
  799. {
  800. set_time_limit($iLoopTimeLimit);
  801. if ($oNode instanceof DisplayableGroupNode)
  802. {
  803. $aGroups[] = $oNode->GetObjects();
  804. $oNode->SetProperty('group_index', $iGroupIdx);
  805. $iGroupIdx++;
  806. }
  807. }
  808. // Remove duplicate edges between two nodes
  809. $oEdgesIter = new RelationTypeIterator($oNewGraph, 'Edge');
  810. $aEdgeKeys = array();
  811. foreach($oEdgesIter as $oEdge)
  812. {
  813. set_time_limit($iLoopTimeLimit);
  814. $sSourceId = $oEdge->GetSourceNode()->GetId();
  815. $sSinkId = $oEdge->GetSinkNode()->GetId();
  816. if ($sSourceId == $sSinkId)
  817. {
  818. // Remove self referring edges
  819. $oNewGraph->_RemoveEdge($oEdge);
  820. }
  821. else
  822. {
  823. $sKey = $sSourceId.'//'.$sSinkId;
  824. if (array_key_exists($sKey, $aEdgeKeys))
  825. {
  826. // Remove duplicate edges
  827. $oNewGraph->_RemoveEdge($oEdge);
  828. }
  829. else
  830. {
  831. $aEdgeKeys[$sKey] = true;
  832. }
  833. }
  834. }
  835. set_time_limit($iPreviousTimeLimit);
  836. return $oNewGraph;
  837. }
  838. /**
  839. * Initializes the positions by rendering using Graphviz in xdot format
  840. * and parsing the output.
  841. * @throws Exception
  842. */
  843. public function InitFromGraphviz()
  844. {
  845. $sDot = $this->DumpAsXDot();
  846. if (strpos($sDot, 'digraph') === false)
  847. {
  848. throw new Exception($sDot);
  849. }
  850. $aChunks = explode(";", $sDot);
  851. foreach($aChunks as $sChunk)
  852. {
  853. if(preg_match('/"([^"]+)".+pos="([0-9\\.]+),([0-9\\.]+)"/ms', $sChunk, $aMatches))
  854. {
  855. $sId = $aMatches[1];
  856. $xPos = $aMatches[2];
  857. $yPos = $aMatches[3];
  858. $oNode = $this->GetNode($sId);
  859. $oNode->x = (float)$xPos;
  860. $oNode->y = (float)$yPos;
  861. }
  862. }
  863. }
  864. public function GetBoundingBox()
  865. {
  866. $xMin = null;
  867. $xMax = null;
  868. $yMin = null;
  869. $yMax = null;
  870. $oIterator = new RelationTypeIterator($this, 'Node');
  871. foreach($oIterator as $sId => $oNode)
  872. {
  873. if ($xMin === null) // First element in the loop
  874. {
  875. $xMin = $oNode->x - $oNode->GetWidth();
  876. $xMax = $oNode->x + $oNode->GetWidth();
  877. $yMin = $oNode->y - $oNode->GetHeight();
  878. $yMax = $oNode->y + $oNode->GetHeight();
  879. }
  880. else
  881. {
  882. $xMin = min($xMin, $oNode->x - $oNode->GetWidth() / 2);
  883. $xMax = max($xMax, $oNode->x + $oNode->GetWidth() / 2);
  884. $yMin = min($yMin, $oNode->y - $oNode->GetHeight() / 2);
  885. $yMax = max($yMax, $oNode->y + $oNode->GetHeight() / 2);
  886. }
  887. }
  888. return array('xmin' => $xMin, 'xmax' => $xMax, 'ymin' => $yMin, 'ymax' => $yMax);
  889. }
  890. function Translate($dx, $dy)
  891. {
  892. $oIterator = new RelationTypeIterator($this, 'Node');
  893. foreach($oIterator as $sId => $oNode)
  894. {
  895. $oNode->x += $dx;
  896. $oNode->y += $dy;
  897. }
  898. }
  899. public function UpdatePositions($aPositions)
  900. {
  901. foreach($aPositions as $sNodeId => $aPos)
  902. {
  903. $oNode = $this->GetNode($sNodeId);
  904. if ($oNode != null)
  905. {
  906. $oNode->x = $aPos['x'];
  907. $oNode->y = $aPos['y'];
  908. }
  909. }
  910. }
  911. /**
  912. * Renders as JSON string suitable for loading into the simple_graph widget
  913. */
  914. function GetAsJSON($sContextKey)
  915. {
  916. $aContextDefs = $this->GetContextDefinitions($sContextKey, false);
  917. $aData = array('nodes' => array(), 'edges' => array(), 'groups' => array());
  918. $iGroupIdx = 0;
  919. $oIterator = new RelationTypeIterator($this, 'Node');
  920. foreach($oIterator as $sId => $oNode)
  921. {
  922. if ($oNode instanceof DisplayableGroupNode)
  923. {
  924. // The contents of the "Groups" tab will be rendered
  925. // using a separate ajax call, since the content of
  926. // the page is made of a mix of HTML / CSS / JS which
  927. // cannot be conveyed easily in the JSON structure
  928. // So we just pass a list of groups, each being defined by a class and a list of keys
  929. // in order to avoid redoing the impact computation which is expensive
  930. $aObjects = $oNode->GetObjects();
  931. $aKeys = array();
  932. foreach($aObjects as $oObj)
  933. {
  934. $sClass = get_class($oObj);
  935. $aKeys[] = $oObj->GetKey();
  936. }
  937. $aData['groups'][$iGroupIdx] = array('class' => $sClass, 'keys' => $aKeys);
  938. $oNode->SetProperty('group_index', $iGroupIdx);
  939. $iGroupIdx++;
  940. }
  941. $aData['nodes'][] = $oNode->GetForRaphael($aContextDefs);
  942. }
  943. $oIterator = new RelationTypeIterator($this, 'Edge');
  944. foreach($oIterator as $sId => $oEdge)
  945. {
  946. $aEdge = array();
  947. $aEdge['id'] = $oEdge->GetId();
  948. $aEdge['source_node_id'] = $oEdge->GetSourceNode()->GetId();
  949. $aEdge['sink_node_id'] = $oEdge->GetSinkNode()->GetId();
  950. $fOpacity = ($oEdge->GetSinkNode()->GetProperty('is_reached') && $oEdge->GetSourceNode()->GetProperty('is_reached') ? 1 : 0.2);
  951. $aEdge['attr'] = array('opacity' => $fOpacity, 'stroke' => '#000');
  952. $aData['edges'][] = $aEdge;
  953. }
  954. return json_encode($aData);
  955. }
  956. /**
  957. * Renders the graph in a PDF document: centered in the current page
  958. * @param PDFPage $oPage The PDFPage representing the PDF document to draw into
  959. * @param string $sComments An optional comment to display next to the graph (HTML entities will be escaped, \n replaced by <br/>)
  960. * @param string $sContextKey The key to fetch the queries in the configuration. Example: itop-tickets/relation_context/UserRequest/impacts/down
  961. * @param float $xMin Left coordinate of the bounding box to display the graph
  962. * @param float $xMax Right coordinate of the bounding box to display the graph
  963. * @param float $yMin Top coordinate of the bounding box to display the graph
  964. * @param float $yMax Bottom coordinate of the bounding box to display the graph
  965. */
  966. function RenderAsPDF(PDFPage $oPage, $sComments = '', $sContextKey, $xMin = -1, $xMax = -1, $yMin = -1, $yMax = -1)
  967. {
  968. $aContextDefs = $this->GetContextDefinitions($sContextKey, false); // No need to develop the parameters
  969. $oPdf = $oPage->get_tcpdf();
  970. $aBB = $this->GetBoundingBox();
  971. $this->Translate(-$aBB['xmin'], -$aBB['ymin']);
  972. $aMargins = $oPdf->getMargins();
  973. if ($xMin == -1)
  974. {
  975. $xMin = $aMargins['left'];
  976. }
  977. if ($xMax == -1)
  978. {
  979. $xMax = $oPdf->getPageWidth() - $aMargins['right'];
  980. }
  981. if ($yMin == -1)
  982. {
  983. $yMin = $aMargins['top'];
  984. }
  985. if ($yMax == -1)
  986. {
  987. $yMax = $oPdf->getPageHeight() - $aMargins['bottom'];
  988. }
  989. $fBreakMargin = $oPdf->getBreakMargin();
  990. $oPdf->SetAutoPageBreak(false);
  991. $aRemainingArea = $this->RenderKey($oPdf, $sComments, $xMin, $yMin, $xMax, $yMax, $aContextDefs);
  992. $xMin = $aRemainingArea['xmin'];
  993. $xMax = $aRemainingArea['xmax'];
  994. $yMin = $aRemainingArea['ymin'];
  995. $yMax = $aRemainingArea['ymax'];
  996. //$oPdf->Rect($xMin, $yMin, $xMax - $xMin, $yMax - $yMin, 'D', array(), array(225, 50, 50));
  997. $fPageW = $xMax - $xMin;
  998. $fPageH = $yMax - $yMin;
  999. $w = $aBB['xmax'] - $aBB['xmin'];
  1000. $h = $aBB['ymax'] - $aBB['ymin'] + 10; // Extra space for the labels which may appear "below" the icons
  1001. $fScale = min($fPageW / $w, $fPageH / $h);
  1002. $dx = ($fPageW - $fScale * $w) / 2;
  1003. $dy = ($fPageH - $fScale * $h) / 2;
  1004. $this->Translate(($xMin + $dx)/$fScale, ($yMin + $dy)/$fScale);
  1005. $oIterator = new RelationTypeIterator($this, 'Edge');
  1006. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  1007. foreach($oIterator as $sId => $oEdge)
  1008. {
  1009. set_time_limit($iLoopTimeLimit);
  1010. $oEdge->RenderAsPDF($oPdf, $this, $fScale, $aContextDefs);
  1011. }
  1012. $oIterator = new RelationTypeIterator($this, 'Node');
  1013. foreach($oIterator as $sId => $oNode)
  1014. {
  1015. set_time_limit($iLoopTimeLimit);
  1016. $oNode->RenderAsPDF($oPdf, $this, $fScale, $aContextDefs);
  1017. }
  1018. $oIterator = new RelationTypeIterator($this, 'Node');
  1019. $oPdf->SetAutoPageBreak(true, $fBreakMargin);
  1020. $oPdf->SetAlpha(1);
  1021. }
  1022. /**
  1023. * Renders (in PDF) the key (legend) of the graphics vertically to the left of the specified zone (xmin,ymin, xmax,ymax),
  1024. * and the comment (if any) at the bottom of the page. Returns the position of remaining area.
  1025. * @param TCPDF $oPdf
  1026. * @param string $sComments
  1027. * @param float $xMin
  1028. * @param float $yMin
  1029. * @param float $xMax
  1030. * @param float $yMax
  1031. * @param hash $aContextDefs
  1032. * @return hash An array ('xmin' => , 'xmax' => ,'ymin' => , 'ymax' => ) of the remaining available area to paint the graph
  1033. */
  1034. protected function RenderKey(TCPDF $oPdf, $sComments, $xMin, $yMin, $xMax, $yMax, $aContextDefs)
  1035. {
  1036. $fFontSize = 7; // in mm
  1037. $fIconSize = 6; // in mm
  1038. $fPadding = 1; // in mm
  1039. $oIterator = new RelationTypeIterator($this, 'Node');
  1040. $fMaxWidth = max($oPdf->GetStringWidth(Dict::S('UI:Relation:Key')) - $fIconSize, $oPdf->GetStringWidth(Dict::S('UI:Relation:Comments')) - $fIconSize);
  1041. $aClasses = array();
  1042. $aIcons = array();
  1043. $aContexts = array();
  1044. $aContextIcons = array();
  1045. $oPdf->SetFont('dejavusans', '', $fFontSize, '', true);
  1046. foreach($oIterator as $sId => $oNode)
  1047. {
  1048. if ($sClass = $oNode->GetObjectClass())
  1049. {
  1050. if (!array_key_exists($sClass, $aClasses))
  1051. {
  1052. $sClassLabel = MetaModel::GetName($sClass);
  1053. $width = $oPdf->GetStringWidth($sClassLabel);
  1054. $fMaxWidth = max($width, $fMaxWidth);
  1055. $aClasses[$sClass] = $sClassLabel;
  1056. $sIconUrl = $oNode->GetProperty('icon_url');
  1057. $sIconPath = str_replace(utils::GetAbsoluteUrlModulesRoot(), APPROOT.'env-'.utils::GetCurrentEnvironment().'/', $sIconUrl);
  1058. $aIcons[$sClass] = $sIconPath;
  1059. }
  1060. }
  1061. $aContextRootCauses = $oNode->GetProperty('context_root_causes');
  1062. if (!is_null($aContextRootCauses))
  1063. {
  1064. foreach($aContextRootCauses as $key => $aObjects)
  1065. {
  1066. $aContexts[$key] = Dict::S($aContextDefs[$key]['dict']);
  1067. $aContextIcons[$key] = APPROOT.'env-'.utils::GetCurrentEnvironment().'/'.$aContextDefs[$key]['icon'];
  1068. }
  1069. }
  1070. }
  1071. $oPdf->SetXY($xMin + $fPadding, $yMin + $fPadding);
  1072. $yPos = $yMin + $fPadding;
  1073. $oPdf->SetFillColor(225, 225, 225);
  1074. $oPdf->Cell($fIconSize + $fPadding + $fMaxWidth, $fIconSize + $fPadding, Dict::S('UI:Relation:Key'), 0 /* border */, 1 /* ln */, 'C', true /* fill */);
  1075. $yPos += $fIconSize + 2*$fPadding;
  1076. foreach($aClasses as $sClass => $sLabel)
  1077. {
  1078. $oPdf->SetX($xMin + $fIconSize + $fPadding);
  1079. $oPdf->Cell(0, $fIconSize + 2*$fPadding, $sLabel, 0 /* border */, 1 /* ln */);
  1080. $oPdf->Image($aIcons[$sClass], $xMin+1, $yPos, $fIconSize, $fIconSize);
  1081. $yPos += $fIconSize + 2*$fPadding;
  1082. }
  1083. foreach($aContexts as $key => $sLabel)
  1084. {
  1085. $oPdf->SetX($xMin + $fIconSize + $fPadding);
  1086. $oPdf->Cell(0, $fIconSize + 2*$fPadding, $sLabel, 0 /* border */, 1 /* ln */);
  1087. $oPdf->Image($aContextIcons[$key], $xMin+1+$fIconSize*0.125, $yPos+$fIconSize*0.125, $fIconSize*0.75, $fIconSize*0.75);
  1088. $yPos += $fIconSize + 2*$fPadding;
  1089. }
  1090. $oPdf->Rect($xMin, $yMin, $fMaxWidth + $fIconSize + 3*$fPadding, $yMax - $yMin, 'D');
  1091. if ($sComments != '')
  1092. {
  1093. // Draw the comment text (surrounded by a rectangle)
  1094. $xPos = $xMin + $fMaxWidth + $fIconSize + 4*$fPadding;
  1095. $w = $xMax - $xPos - 2*$fPadding;
  1096. $iNbLines = 1;
  1097. $sText = '<p>'.str_replace("\n", '<br/>', htmlentities($sComments, ENT_QUOTES, 'UTF-8'), $iNbLines).'</p>';
  1098. $fLineHeight = $oPdf->getStringHeight($w, $sText);
  1099. $h = (1+$iNbLines) * $fLineHeight;
  1100. $yPos = $yMax - 2*$fPadding - $h;
  1101. $oPdf->writeHTMLCell($w, $h, $xPos + $fPadding, $yPos + $fPadding, $sText, 0 /* border */, 1 /* ln */);
  1102. $oPdf->Rect($xPos, $yPos, $w + 2*$fPadding, $h + 2*$fPadding, 'D');
  1103. $yMax = $yPos - $fPadding;
  1104. }
  1105. return array('xmin' => $xMin + $fMaxWidth + $fIconSize + 4*$fPadding, 'xmax' => $xMax, 'ymin' => $yMin, 'ymax' => $yMax);
  1106. }
  1107. /**
  1108. * Get the context definitions from the parameters / configuration. The format of the "key" string is:
  1109. * <module>/relation_context/<class>/<relation>/<direction>
  1110. * The values will be retrieved for the given class and all its parents and merged together as a single array.
  1111. * Entries with an invalid query are removed from the list.
  1112. * @param string $sContextKey The key to fetch the queries in the configuration. Example: itop-tickets/relation_context/UserRequest/impacts/down
  1113. * @param bool $bDevelopParams Whether or not to substitute the parameters inside the queries with the supplied "context params"
  1114. * @param array $aContextParams Arguments for the queries (via ToArgs()) if $bDevelopParams == true
  1115. * @return multitype:multitype:string
  1116. */
  1117. public function GetContextDefinitions($sContextKey, $bDevelopParams = true, $aContextParams = array())
  1118. {
  1119. $aLevels = explode('/', $sContextKey);
  1120. $sLeafClass = $aLevels[2];
  1121. $aRelationContext = MetaModel::GetConfig()->GetModuleSetting($aLevels[0], $aLevels[1], array());
  1122. $aContextDefs = array();
  1123. foreach(MetaModel::EnumParentClasses($sLeafClass, ENUM_PARENT_CLASSES_ALL) as $sClass)
  1124. {
  1125. if (isset($aRelationContext[$sClass][$aLevels[3]][$aLevels[4]]['items']))
  1126. {
  1127. $aContextDefs = array_merge($aContextDefs, $aRelationContext[$sClass][$aLevels[3]][$aLevels[4]]['items']);
  1128. }
  1129. }
  1130. // Check if the queries are valid
  1131. foreach($aContextDefs as $sKey => $sDefs)
  1132. {
  1133. $sOQL = $aContextDefs[$sKey]['oql'];
  1134. try
  1135. {
  1136. // Expand the parameters. If anything goes wrong, then the query is considered as invalid and removed from the list
  1137. $oSearch = DBObjectSearch::FromOQL($sOQL);
  1138. $aContextDefs[$sKey]['oql'] = $oSearch->ToOQL($bDevelopParams, $aContextParams);
  1139. }
  1140. catch(Exception $e)
  1141. {
  1142. unset($aContextDefs[$sKey]);
  1143. }
  1144. }
  1145. return $aContextDefs;
  1146. }
  1147. /**
  1148. * Display the graph inside the given page, with the "filter" drawer above it
  1149. * @param WebPage $oP
  1150. * @param hash $aResults
  1151. * @param string $sRelation
  1152. * @param ApplicationContext $oAppContext
  1153. * @param array $aExcludedObjects
  1154. */
  1155. function Display(WebPage $oP, $aResults, $sRelation, ApplicationContext $oAppContext, $aExcludedObjects = array(), $sObjClass = null, $iObjKey = null, $sContextKey, $aContextParams = array())
  1156. {
  1157. $aContextDefs = $this->GetContextDefinitions($sContextKey, true, $aContextParams);
  1158. $aExcludedByClass = array();
  1159. foreach($aExcludedObjects as $oObj)
  1160. {
  1161. if (!array_key_exists(get_class($oObj), $aExcludedByClass))
  1162. {
  1163. $aExcludedByClass[get_class($oObj)] = array();
  1164. }
  1165. $aExcludedByClass[get_class($oObj)][] = $oObj->GetKey();
  1166. }
  1167. $oP->add("<div class=\"not-printable\">\n");
  1168. $oP->add("<div id=\"ds_flash\" class=\"SearchDrawer\" style=\"display:none;\">\n");
  1169. $oP->add_ready_script(
  1170. <<<EOF
  1171. $( "#tabbedContent_0" ).tabs({ heightStyle: "fill" });
  1172. $("#dh_flash").click( function() {
  1173. $("#ds_flash").slideToggle('normal', function() { $("#ds_flash").parent().resize(); } );
  1174. $("#dh_flash").toggleClass('open');
  1175. });
  1176. $('#ReloadMovieBtn').button().button('disable');
  1177. EOF
  1178. );
  1179. $aSortedElements = array();
  1180. foreach($aResults as $sClassIdx => $aObjects)
  1181. {
  1182. foreach($aObjects as $oCurrObj)
  1183. {
  1184. $sSubClass = get_class($oCurrObj);
  1185. $aSortedElements[$sSubClass] = MetaModel::GetName($sSubClass);
  1186. }
  1187. }
  1188. asort($aSortedElements);
  1189. $idx = 0;
  1190. foreach($aSortedElements as $sSubClass => $sClassName)
  1191. {
  1192. $oP->add("<span style=\"padding-right:2em; white-space:nowrap;\"><input type=\"checkbox\" id=\"exclude_$idx\" name=\"excluded[]\" value=\"$sSubClass\" checked onChange=\"$('#ReloadMovieBtn').button('enable')\"><label for=\"exclude_$idx\">&nbsp;".MetaModel::GetClassIcon($sSubClass)."&nbsp;$sClassName</label></span> ");
  1193. $idx++;
  1194. }
  1195. $oP->add("<p style=\"text-align:right\"><button type=\"button\" id=\"ReloadMovieBtn\" onClick=\"DoReload()\">".Dict::S('UI:Button:Refresh')."</button></p>");
  1196. $oP->add("</div>\n");
  1197. $oP->add("<div class=\"HRDrawer\"></div>\n");
  1198. $oP->add("<div id=\"dh_flash\" class=\"DrawerHandle\">".Dict::S('UI:ElementsDisplayed')."</div>\n");
  1199. $oP->add("</div>\n"); // class="not-printable"
  1200. $aAdditionalContexts = array();
  1201. foreach($aContextDefs as $sKey => $aDefinition)
  1202. {
  1203. $aAdditionalContexts[] = array('key' => $sKey, 'label' => Dict::S($aDefinition['dict']), 'oql' => $aDefinition['oql']);
  1204. }
  1205. $sDirection = utils::ReadParam('d', 'horizontal');
  1206. $iGroupingThreshold = utils::ReadParam('g', 5);
  1207. $oP->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/fraphael.js');
  1208. $oP->add_linked_stylesheet(utils::GetAbsoluteUrlAppRoot().'css/jquery.contextMenu.css');
  1209. $oP->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.contextMenu.js');
  1210. $oP->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/simple_graph.js');
  1211. try
  1212. {
  1213. $this->InitFromGraphviz();
  1214. $sExportAsPdfURL = '';
  1215. $sExportAsPdfURL = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php?operation=relation_pdf&relation='.$sRelation.'&direction='.($this->bDirectionDown ? 'down' : 'up');
  1216. $oAppcontext = new ApplicationContext();
  1217. $sContext = $oAppContext->GetForLink();
  1218. $sDrillDownURL = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=details&class=%1$s&id=%2$s&'.$sContext;
  1219. $sExportAsDocumentURL = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php?operation=relation_attachment&relation='.$sRelation.'&direction='.($this->bDirectionDown ? 'down' : 'up');
  1220. $sLoadFromURL = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php?operation=relation_json&relation='.$sRelation.'&direction='.($this->bDirectionDown ? 'down' : 'up');
  1221. $sAttachmentExportTitle = '';
  1222. if (($sObjClass != null) && ($iObjKey != null))
  1223. {
  1224. $oTargetObj = MetaModel::GetObject($sObjClass, $iObjKey, false);
  1225. if ($oTargetObj)
  1226. {
  1227. $sAttachmentExportTitle = Dict::Format('UI:Relation:AttachmentExportOptions_Name', $oTargetObj->GetName());
  1228. }
  1229. }
  1230. $sId = 'graph';
  1231. $sStyle = '';
  1232. if ($oP->IsPrintableVersion())
  1233. {
  1234. // Optimize for printing on A4/Letter vertically
  1235. $sStyle = 'max-width: 17cm; margin-left:auto; margin-right:auto;';
  1236. }
  1237. $oP->add('<div id="'.$sId.'" class="simple-graph" style="'.$sStyle.'"></div>');
  1238. $aParams = array(
  1239. 'source_url' => $sLoadFromURL,
  1240. 'sources' => ($this->bDirectionDown ? $this->aSourceObjects : $this->aSinkObjects),
  1241. 'excluded' => $aExcludedByClass,
  1242. 'grouping_threshold' => $iGroupingThreshold,
  1243. 'export_as_pdf' => array('url' => $sExportAsPdfURL, 'label' => Dict::S('UI:Relation:ExportAsPDF')),
  1244. 'export_as_attachment' => array('url' => $sExportAsDocumentURL, 'label' => Dict::S('UI:Relation:ExportAsAttachment'), 'obj_class' => $sObjClass, 'obj_key' => $iObjKey),
  1245. 'drill_down' => array('url' => $sDrillDownURL, 'label' => Dict::S('UI:Relation:DrillDown')),
  1246. 'labels' => array(
  1247. 'export_pdf_title' => Dict::S('UI:Relation:PDFExportOptions'),
  1248. 'export_as_attachment_title' => $sAttachmentExportTitle,
  1249. 'export' => Dict::S('UI:Button:Export'),
  1250. 'cancel' => Dict::S('UI:Button:Cancel'),
  1251. 'title' => Dict::S('UI:RelationOption:Title'),
  1252. 'untitled' => Dict::S('UI:RelationOption:Untitled'),
  1253. 'include_list' => Dict::S('UI:RelationOption:IncludeList'),
  1254. 'comments' => Dict::S('UI:RelationOption:Comments'),
  1255. 'grouping_threshold' => Dict::S('UI:RelationOption:GroupingThreshold'),
  1256. 'refresh' => Dict::S('UI:Button:Refresh'),
  1257. 'check_all' => Dict::S('UI:SearchValue:CheckAll'),
  1258. 'uncheck_all' => Dict::S('UI:SearchValue:UncheckAll'),
  1259. 'none_selected' => Dict::S('UI:Relation:NoneSelected'),
  1260. 'nb_selected' => Dict::S('UI:SearchValue:NbSelected'),
  1261. 'additional_context_info' => Dict::S('UI:Relation:AdditionalContextInfo'),
  1262. 'zoom' => Dict::S('UI:Relation:Zoom'),
  1263. 'loading' => Dict::S('UI:Loading'),
  1264. ),
  1265. 'page_format' => array(
  1266. 'label' => Dict::S('UI:Relation:PDFExportPageFormat'),
  1267. 'values' => array(
  1268. 'A3' => Dict::S('UI:PageFormat_A3'),
  1269. 'A4' => Dict::S('UI:PageFormat_A4'),
  1270. 'Letter' => Dict::S('UI:PageFormat_Letter'),
  1271. ),
  1272. ),
  1273. 'page_orientation' => array(
  1274. 'label' => Dict::S('UI:Relation:PDFExportPageOrientation'),
  1275. 'values' => array(
  1276. 'P' => Dict::S('UI:PageOrientation_Portrait'),
  1277. 'L' => Dict::S('UI:PageOrientation_Landscape'),
  1278. ),
  1279. ),
  1280. 'additional_contexts' => $aAdditionalContexts,
  1281. 'context_key' => $sContextKey,
  1282. );
  1283. if (!extension_loaded('gd'))
  1284. {
  1285. // PDF export requires GD
  1286. unset($aParams['export_as_pdf']);
  1287. }
  1288. if (!extension_loaded('gd') || is_null($sObjClass) || is_null($iObjKey))
  1289. {
  1290. // Export as Attachment requires GD (for building the PDF) AND a valid objclass/objkey couple
  1291. unset($aParams['export_as_attachment']);
  1292. }
  1293. $oP->add_ready_script("$('#$sId').simple_graph(".json_encode($aParams).");");
  1294. }
  1295. catch(Exception $e)
  1296. {
  1297. $oP->add('<div>'.$e->getMessage().'</div>');
  1298. }
  1299. $oP->add_script(
  1300. <<<EOF
  1301. function DoReload()
  1302. {
  1303. $('#ReloadMovieBtn').button('disable');
  1304. try
  1305. {
  1306. var aExcluded = [];
  1307. $('input[name^=excluded]').each( function() {
  1308. if (!$(this).prop('checked'))
  1309. {
  1310. aExcluded.push($(this).val());
  1311. }
  1312. } );
  1313. $('#graph').simple_graph('option', {excluded_classes: aExcluded});
  1314. $('#graph').simple_graph('reload');
  1315. }
  1316. catch(err)
  1317. {
  1318. alert(err);
  1319. }
  1320. }
  1321. EOF
  1322. );
  1323. }
  1324. }