displayablegraph.class.inc.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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 $bDirectionDown;
  505. protected $aTempImages;
  506. protected $aSourceObjects;
  507. protected $aSinkObjects;
  508. public function __construct()
  509. {
  510. parent::__construct();
  511. $this->aTempImages = array();
  512. $this->aSourceObjects = array();
  513. $this->aSinkObjects = array();
  514. }
  515. public function GetTempImageName()
  516. {
  517. $sNewTempName = tempnam(APPROOT.'data', 'img-');
  518. $this->aTempImages[] = $sNewTempName;
  519. return $sNewTempName;
  520. }
  521. public function __destruct()
  522. {
  523. foreach($this->aTempImages as $sTempFile)
  524. {
  525. @unlink($sTempFile);
  526. }
  527. }
  528. /**
  529. * Build a DisplayableGraph from a RelationGraph
  530. * @param RelationGraph $oGraph
  531. * @param number $iGroupingThreshold
  532. * @param string $bDirectionDown
  533. * @return DisplayableGraph
  534. */
  535. public static function FromRelationGraph(RelationGraph $oGraph, $iGroupingThreshold = 20, $bDirectionDown = true)
  536. {
  537. $oNewGraph = new DisplayableGraph();
  538. $oNewGraph->bDirectionDown = $bDirectionDown;
  539. $oNodesIter = new RelationTypeIterator($oGraph, 'Node');
  540. foreach($oNodesIter as $oNode)
  541. {
  542. switch(get_class($oNode))
  543. {
  544. case 'RelationObjectNode':
  545. $oNewNode = new DisplayableNode($oNewGraph, $oNode->GetId(), 0, 0);
  546. $oObj = $oNode->GetProperty('object');
  547. $sClass = get_class($oObj);
  548. if ($oNode->GetProperty('source'))
  549. {
  550. if (!array_key_exists($sClass, $oNewGraph->aSourceObjects))
  551. {
  552. $oNewGraph->aSourceObjects[$sClass] = array();
  553. }
  554. $oNewGraph->aSourceObjects[$sClass][] = $oObj->GetKey();
  555. $oNewNode->SetProperty('source', true);
  556. }
  557. if ($oNode->GetProperty('sink'))
  558. {
  559. if (!array_key_exists($sClass, $oNewGraph->aSinkObjects))
  560. {
  561. $oNewGraph->aSinkObjects[$sClass] = array();
  562. }
  563. $oNewGraph->aSinkObjects[$sClass][] = $oObj->GetKey();
  564. $oNewNode->SetProperty('sink', true);
  565. }
  566. $oNewNode->SetProperty('class', $sClass);
  567. $oNewNode->SetProperty('object', $oObj);
  568. $oNewNode->SetProperty('icon_url', $oObj->GetIcon(false));
  569. $oNewNode->SetProperty('label', $oObj->GetRawName());
  570. $oNewNode->SetProperty('is_reached', $bDirectionDown ? $oNode->GetProperty('is_reached') : true); // When going "up" is_reached does not matter
  571. $oNewNode->SetProperty('developped', $oNode->GetProperty('developped'));
  572. break;
  573. default:
  574. $oNewNode = new DisplayableRedundancyNode($oNewGraph, $oNode->GetId(), 0, 0);
  575. $oNewNode->SetProperty('label', $oNode->GetProperty('min_up'));
  576. $oNewNode->SetProperty('is_reached', true);
  577. }
  578. }
  579. $oEdgesIter = new RelationTypeIterator($oGraph, 'Edge');
  580. foreach($oEdgesIter as $oEdge)
  581. {
  582. $oSourceNode = $oNewGraph->GetNode($oEdge->GetSourceNode()->GetId());
  583. $oSinkNode = $oNewGraph->GetNode($oEdge->GetSinkNode()->GetId());
  584. $oNewEdge = new DisplayableEdge($oNewGraph, $oEdge->GetId(), $oSourceNode, $oSinkNode);
  585. }
  586. // Remove duplicate edges between two nodes
  587. $oEdgesIter = new RelationTypeIterator($oNewGraph, 'Edge');
  588. $aEdgeKeys = array();
  589. foreach($oEdgesIter as $oEdge)
  590. {
  591. $sSourceId = $oEdge->GetSourceNode()->GetId();
  592. $sSinkId = $oEdge->GetSinkNode()->GetId();
  593. if ($sSourceId == $sSinkId)
  594. {
  595. // Remove self referring edges
  596. $oNewGraph->_RemoveEdge($oEdge);
  597. }
  598. else
  599. {
  600. $sKey = $sSourceId.'//'.$sSinkId;
  601. if (array_key_exists($sKey, $aEdgeKeys))
  602. {
  603. // Remove duplicate edges
  604. $oNewGraph->_RemoveEdge($oEdge);
  605. }
  606. else
  607. {
  608. $aEdgeKeys[$sKey] = true;
  609. }
  610. }
  611. }
  612. $iNbGrouping = 1;
  613. //for($iter=0; $iter<$iNbGrouping; $iter++)
  614. {
  615. $oNodesIter = new RelationTypeIterator($oNewGraph, 'Node');
  616. foreach($oNodesIter as $oNode)
  617. {
  618. if ($oNode->GetProperty('source'))
  619. {
  620. $oNode->GroupSimilarNeighbours($oNewGraph, $iGroupingThreshold, true, true);
  621. }
  622. }
  623. }
  624. // Remove duplicate edges between two nodes
  625. $oEdgesIter = new RelationTypeIterator($oNewGraph, 'Edge');
  626. $aEdgeKeys = array();
  627. foreach($oEdgesIter as $oEdge)
  628. {
  629. $sSourceId = $oEdge->GetSourceNode()->GetId();
  630. $sSinkId = $oEdge->GetSinkNode()->GetId();
  631. if ($sSourceId == $sSinkId)
  632. {
  633. // Remove self referring edges
  634. $oNewGraph->_RemoveEdge($oEdge);
  635. }
  636. else
  637. {
  638. $sKey = $sSourceId.'//'.$sSinkId;
  639. if (array_key_exists($sKey, $aEdgeKeys))
  640. {
  641. // Remove duplicate edges
  642. $oNewGraph->_RemoveEdge($oEdge);
  643. }
  644. else
  645. {
  646. $aEdgeKeys[$sKey] = true;
  647. }
  648. }
  649. }
  650. return $oNewGraph;
  651. }
  652. /**
  653. * Initializes the positions by rendering using Graphviz in xdot format
  654. * and parsing the output.
  655. * @throws Exception
  656. */
  657. public function InitFromGraphviz()
  658. {
  659. $sDot = $this->DumpAsXDot();
  660. if (strpos($sDot, 'digraph') === false)
  661. {
  662. throw new Exception($sDot);
  663. }
  664. $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
  665. $aChunks = explode(";", $sDot);
  666. foreach($aChunks as $sChunk)
  667. {
  668. //echo "<p>$sChunk</p>";
  669. if(preg_match('/"([^"]+)".+pos="([0-9\\.]+),([0-9\\.]+)"/ms', $sChunk, $aMatches))
  670. {
  671. $sId = $aMatches[1];
  672. $xPos = $aMatches[2];
  673. $yPos = $aMatches[3];
  674. $oNode = $this->GetNode($sId);
  675. $oNode->x = (float)$xPos;
  676. $oNode->y = (float)$yPos;
  677. //echo "<p>$sId at $xPos,$yPos</p>";
  678. }
  679. else
  680. {
  681. //echo "<p>No match</p>";
  682. }
  683. }
  684. }
  685. public function GetBoundingBox()
  686. {
  687. $xMin = null;
  688. $xMax = null;
  689. $yMin = null;
  690. $yMax = null;
  691. $oIterator = new RelationTypeIterator($this, 'Node');
  692. foreach($oIterator as $sId => $oNode)
  693. {
  694. if ($xMin === null) // First element in the loop
  695. {
  696. $xMin = $oNode->x - $oNode->GetWidth();
  697. $xMax = $oNode->x + $oNode->GetWidth();
  698. $yMin = $oNode->y - $oNode->GetHeight();
  699. $yMax = $oNode->y + $oNode->GetHeight();
  700. }
  701. else
  702. {
  703. $xMin = min($xMin, $oNode->x - $oNode->GetWidth() / 2);
  704. $xMax = max($xMax, $oNode->x + $oNode->GetWidth() / 2);
  705. $yMin = min($yMin, $oNode->y - $oNode->GetHeight() / 2);
  706. $yMax = max($yMax, $oNode->y + $oNode->GetHeight() / 2);
  707. }
  708. }
  709. return array('xmin' => $xMin, 'xmax' => $xMax, 'ymin' => $yMin, 'ymax' => $yMax);
  710. }
  711. function Translate($dx, $dy)
  712. {
  713. $oIterator = new RelationTypeIterator($this, 'Node');
  714. foreach($oIterator as $sId => $oNode)
  715. {
  716. $oNode->x += $dx;
  717. $oNode->y += $dy;
  718. }
  719. }
  720. public function UpdatePositions($aPositions)
  721. {
  722. foreach($aPositions as $sNodeId => $aPos)
  723. {
  724. $oNode = $this->GetNode($sNodeId);
  725. if ($oNode != null)
  726. {
  727. $oNode->x = $aPos['x'];
  728. $oNode->y = $aPos['y'];
  729. }
  730. }
  731. }
  732. /**
  733. * Renders as JSON string suitable for loading into the simple_graph widget
  734. */
  735. function GetAsJSON()
  736. {
  737. $aData = array('nodes' => array(), 'edges' => array());
  738. $iGroupIdx = 0;
  739. $oIterator = new RelationTypeIterator($this, 'Node');
  740. foreach($oIterator as $sId => $oNode)
  741. {
  742. if ($oNode instanceof DisplayableGroupNode)
  743. {
  744. $aGroups[] = $oNode->GetObjects();
  745. $oNode->SetProperty('group_index', $iGroupIdx);
  746. $iGroupIdx++;
  747. }
  748. $aData['nodes'][] = $oNode->GetForRaphael();
  749. }
  750. $oIterator = new RelationTypeIterator($this, 'Edge');
  751. foreach($oIterator as $sId => $oEdge)
  752. {
  753. $aEdge = array();
  754. $aEdge['id'] = $oEdge->GetId();
  755. $aEdge['source_node_id'] = $oEdge->GetSourceNode()->GetId();
  756. $aEdge['sink_node_id'] = $oEdge->GetSinkNode()->GetId();
  757. $fOpacity = ($oEdge->GetSinkNode()->GetProperty('is_reached') && $oEdge->GetSourceNode()->GetProperty('is_reached') ? 1 : 0.2);
  758. $aEdge['attr'] = array('opacity' => $fOpacity, 'stroke' => '#000');
  759. $aData['edges'][] = $aEdge;
  760. }
  761. return json_encode($aData);
  762. }
  763. /**
  764. * Renders the graph in a PDF document: centered in the current page
  765. * @param PDFPage $oPage The PDFPage representing the PDF document to draw into
  766. * @param string $sComments An optional comment to display next to the graph (HTML entities will be escaped, \n replaced by <br/>)
  767. * @param float $xMin Left coordinate of the bounding box to display the graph
  768. * @param float $xMax Right coordinate of the bounding box to display the graph
  769. * @param float $yMin Top coordinate of the bounding box to display the graph
  770. * @param float $yMax Bottom coordinate of the bounding box to display the graph
  771. */
  772. function RenderAsPDF(PDFPage $oPage, $sComments = '', $xMin = -1, $xMax = -1, $yMin = -1, $yMax = -1)
  773. {
  774. $oPdf = $oPage->get_tcpdf();
  775. $aBB = $this->GetBoundingBox();
  776. $this->Translate(-$aBB['xmin'], -$aBB['ymin']);
  777. $aMargins = $oPdf->getMargins();
  778. if ($xMin == -1)
  779. {
  780. $xMin = $aMargins['left'];
  781. }
  782. if ($xMax == -1)
  783. {
  784. $xMax = $oPdf->getPageWidth() - $aMargins['right'];
  785. }
  786. if ($yMin == -1)
  787. {
  788. $yMin = $aMargins['top'];
  789. }
  790. if ($yMax == -1)
  791. {
  792. $yMax = $oPdf->getPageHeight() - $aMargins['bottom'];
  793. }
  794. $fBreakMargin = $oPdf->getBreakMargin();
  795. $oPdf->SetAutoPageBreak(false);
  796. $aRemainingArea = $this->RenderKey($oPdf, $sComments, $xMin, $yMin, $xMax, $yMax);
  797. $xMin = $aRemainingArea['xmin'];
  798. $xMax = $aRemainingArea['xmax'];
  799. $yMin = $aRemainingArea['ymin'];
  800. $yMax = $aRemainingArea['ymax'];
  801. //$oPdf->Rect($xMin, $yMin, $xMax - $xMin, $yMax - $yMin, 'D', array(), array(225, 225, 225));
  802. $fPageW = $xMax - $xMin;
  803. $fPageH = $yMax - $yMin;
  804. $w = $aBB['xmax'] - $aBB['xmin'];
  805. $h = $aBB['ymax'] - $aBB['ymin'] + 10; // Extra space for the labels which may appear "below" the icons
  806. $fScale = min($fPageW / $w, $fPageH / $h);
  807. $dx = ($fPageW - $fScale * $w) / 2;
  808. $dy = ($fPageH - $fScale * $h) / 2;
  809. $this->Translate(($xMin + $dx)/$fScale, ($yMin + $dy)/$fScale);
  810. $oIterator = new RelationTypeIterator($this, 'Edge');
  811. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  812. foreach($oIterator as $sId => $oEdge)
  813. {
  814. set_time_limit($iLoopTimeLimit);
  815. $oEdge->RenderAsPDF($oPdf, $this, $fScale);
  816. }
  817. $oIterator = new RelationTypeIterator($this, 'Node');
  818. foreach($oIterator as $sId => $oNode)
  819. {
  820. set_time_limit($iLoopTimeLimit);
  821. $oNode->RenderAsPDF($oPdf, $this, $fScale);
  822. }
  823. $oIterator = new RelationTypeIterator($this, 'Node');
  824. $oPdf->SetAutoPageBreak(true, $fBreakMargin);
  825. $oPdf->SetAlpha(1);
  826. }
  827. /**
  828. * Renders (in PDF) the key (legend) of the graphics vertically to the left of the specified zone (xmin,ymin, xmax,ymax),
  829. * and the comment (if any) at the bottom of the page. Returns the position of remaining area.
  830. * @param TCPDF $oPdf
  831. * @param string $sComments
  832. * @param float $xMin
  833. * @param float $yMin
  834. * @param float $xMax
  835. * @param float $yMax
  836. * @return hash An array ('xmin' => , 'xmax' => ,'ymin' => , 'ymax' => ) of the remaining available area to paint the graph
  837. */
  838. protected function RenderKey(TCPDF $oPdf, $sComments, $xMin, $yMin, $xMax, $yMax)
  839. {
  840. $fFontSize = 7; // in mm
  841. $fIconSize = 6; // in mm
  842. $fPadding = 1; // in mm
  843. $oIterator = new RelationTypeIterator($this, 'Node');
  844. $fMaxWidth = max($oPdf->GetStringWidth(Dict::S('UI:Relation:Key')) - $fIconSize, $oPdf->GetStringWidth(Dict::S('UI:Relation:Comments')) - $fIconSize);
  845. $aClasses = array();
  846. $aIcons = array();
  847. $oPdf->SetFont('dejavusans', '', $fFontSize, '', true);
  848. foreach($oIterator as $sId => $oNode)
  849. {
  850. if ($sClass = $oNode->GetProperty('class'))
  851. {
  852. if (!array_key_exists($sClass, $aClasses))
  853. {
  854. $sClassLabel = MetaModel::GetName($sClass);
  855. $width = $oPdf->GetStringWidth($sClassLabel);
  856. $fMaxWidth = max($width, $fMaxWidth);
  857. $aClasses[$sClass] = $sClassLabel;
  858. $sIconUrl = $oNode->GetProperty('icon_url');
  859. $sIconPath = str_replace(utils::GetAbsoluteUrlModulesRoot(), APPROOT.'env-production/', $sIconUrl);
  860. $aIcons[$sClass] = $sIconPath;
  861. }
  862. }
  863. }
  864. $oPdf->SetXY($xMin + $fPadding, $yMin + $fPadding);
  865. $yPos = $yMin + $fPadding;
  866. $oPdf->SetFillColor(225, 225, 225);
  867. $oPdf->Cell($fIconSize + $fPadding + $fMaxWidth, $fIconSize + $fPadding, Dict::S('UI:Relation:Key'), 0 /* border */, 1 /* ln */, 'C', true /* fill */);
  868. $yPos += $fIconSize + 2*$fPadding;
  869. foreach($aClasses as $sClass => $sLabel)
  870. {
  871. $oPdf->SetX($xMin + $fIconSize + $fPadding);
  872. $oPdf->Cell(0, $fIconSize + 2*$fPadding, $sLabel, 0 /* border */, 1 /* ln */);
  873. $oPdf->Image($aIcons[$sClass], $xMin+1, $yPos, $fIconSize, $fIconSize);
  874. $yPos += $fIconSize + 2*$fPadding;
  875. }
  876. $oPdf->Rect($xMin, $yMin, $fMaxWidth + $fIconSize + 3*$fPadding, $yMax - $yMin, 'D');
  877. if ($sComments != '')
  878. {
  879. // Draw the comment text (surrounded by a rectangle)
  880. $xPos = $xMin + $fMaxWidth + $fIconSize + 4*$fPadding;
  881. $w = $xMax - $xPos - 2*$fPadding;
  882. $iNbLines = 1;
  883. $sText = '<p>'.str_replace("\n", '<br/>', htmlentities($sComments, ENT_QUOTES, 'UTF-8'), $iNbLines).'</p>';
  884. $fLineHeight = $oPdf->getStringHeight($w, $sText);
  885. $h = (1+$iNbLines) * $fLineHeight;
  886. $yPos = $yMax - 2*$fPadding - $h;
  887. $oPdf->writeHTMLCell($w, $h, $xPos + $fPadding, $yPos + $fPadding, $sText, 0 /* border */, 1 /* ln */);
  888. $oPdf->Rect($xPos, $yPos, $w + 2*$fPadding, $h + 2*$fPadding, 'D');
  889. $yMax = $yPos - $fPadding;
  890. }
  891. return array('xmin' => $fMaxWidth + $fIconSize + 4*$fPadding, 'xmax' => $xMax, 'ymin' => $yMin, 'ymax' => $yMax);
  892. }
  893. /**
  894. * Display the graph inside the given page, with the "filter" drawer above it
  895. * @param WebPage $oP
  896. * @param hash $aResults
  897. * @param string $sRelation
  898. * @param ApplicationContext $oAppContext
  899. * @param array $aExcludedObjects
  900. */
  901. function Display(WebPage $oP, $aResults, $sRelation, ApplicationContext $oAppContext, $aExcludedObjects = array(), $sObjClass = null, $iObjKey = null)
  902. {
  903. $aExcludedByClass = array();
  904. foreach($aExcludedObjects as $oObj)
  905. {
  906. if (!array_key_exists(get_class($oObj), $aExcludedByClass))
  907. {
  908. $aExcludedByClass[get_class($oObj)] = array();
  909. }
  910. $aExcludedByClass[get_class($oObj)][] = $oObj->GetKey();
  911. }
  912. $oP->add("<div id=\"ds_flash\" class=\"SearchDrawer\" style=\"display:none;\">\n");
  913. $oP->add_ready_script(
  914. <<<EOF
  915. $( "#tabbedContent_0" ).tabs({ heightStyle: "fill" });
  916. $("#dh_flash").click( function() {
  917. $("#ds_flash").slideToggle('normal', function() { $("#ds_flash").parent().resize(); } );
  918. $("#dh_flash").toggleClass('open');
  919. });
  920. $('#ReloadMovieBtn').button().button('disable');
  921. EOF
  922. );
  923. $aSortedElements = array();
  924. foreach($aResults as $sClassIdx => $aObjects)
  925. {
  926. foreach($aObjects as $oCurrObj)
  927. {
  928. $sSubClass = get_class($oCurrObj);
  929. $aSortedElements[$sSubClass] = MetaModel::GetName($sSubClass);
  930. }
  931. }
  932. asort($aSortedElements);
  933. $idx = 0;
  934. foreach($aSortedElements as $sSubClass => $sClassName)
  935. {
  936. $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> ");
  937. $idx++;
  938. }
  939. $oP->add("<p style=\"text-align:right\"><button type=\"button\" id=\"ReloadMovieBtn\" onClick=\"DoReload()\">".Dict::S('UI:Button:Refresh')."</button></p>");
  940. $oP->add("</div>\n");
  941. $oP->add("<div class=\"HRDrawer\"></div>\n");
  942. $oP->add("<div id=\"dh_flash\" class=\"DrawerHandle\">".Dict::S('UI:ElementsDisplayed')."</div>\n");
  943. $sDirection = utils::ReadParam('d', 'horizontal');
  944. $iGroupingThreshold = utils::ReadParam('g', 5);
  945. $oP->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/fraphael.js');
  946. $oP->add_linked_stylesheet(utils::GetAbsoluteUrlAppRoot().'css/jquery.contextMenu.css');
  947. $oP->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.contextMenu.js');
  948. $oP->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/simple_graph.js');
  949. try
  950. {
  951. $this->InitFromGraphviz();
  952. $sExportAsPdfURL = '';
  953. $sExportAsPdfURL = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php?operation=relation_pdf&relation='.$sRelation.'&direction='.($this->bDirectionDown ? 'down' : 'up');
  954. $oAppcontext = new ApplicationContext();
  955. $sContext = $oAppContext->GetForLink();
  956. $sDrillDownURL = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=details&class=%1$s&id=%2$s&'.$sContext;
  957. $sExportAsDocumentURL = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php?operation=relation_attachment&relation='.$sRelation.'&direction='.($this->bDirectionDown ? 'down' : 'up');
  958. $sLoadFromURL = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php?operation=relation_json&relation='.$sRelation.'&direction='.($this->bDirectionDown ? 'down' : 'up');
  959. $sAttachmentExportTitle = '';
  960. if (($sObjClass != null) && ($iObjKey != null))
  961. {
  962. $oTargetObj = MetaModel::GetObject($sObjClass, $iObjKey, false);
  963. if ($oTargetObj)
  964. {
  965. $sAttachmentExportTitle = Dict::Format('UI:Relation:AttachmentExportOptions_Name', $oTargetObj->GetName());
  966. }
  967. }
  968. $sId = 'graph';
  969. $oP->add('<div id="'.$sId.'" class="simple-graph"></div>');
  970. $aParams = array(
  971. 'source_url' => $sLoadFromURL,
  972. 'sources' => ($this->bDirectionDown ? $this->aSourceObjects : $this->aSinkObjects),
  973. 'excluded' => $aExcludedByClass,
  974. 'grouping_threshold' => $iGroupingThreshold,
  975. 'export_as_pdf' => array('url' => $sExportAsPdfURL, 'label' => Dict::S('UI:Relation:ExportAsPDF')),
  976. 'export_as_attachment' => array('url' => $sExportAsDocumentURL, 'label' => Dict::S('UI:Relation:ExportAsAttachment'), 'obj_class' => $sObjClass, 'obj_key' => $iObjKey),
  977. 'drill_down' => array('url' => $sDrillDownURL, 'label' => Dict::S('UI:Relation:DrillDown')),
  978. 'labels' => array(
  979. 'export_pdf_title' => Dict::S('UI:Relation:PDFExportOptions'),
  980. 'export_as_attachment_title' => $sAttachmentExportTitle,
  981. 'export' => Dict::S('UI:Button:Export'),
  982. 'cancel' => Dict::S('UI:Button:Cancel'),
  983. 'title' => Dict::S('UI:RelationOption:Title'),
  984. 'untitled' => Dict::S('UI:RelationOption:Untitled'),
  985. 'include_list' => Dict::S('UI:RelationOption:IncludeList'),
  986. 'comments' => Dict::S('UI:RelationOption:Comments'),
  987. 'grouping_threshold' => Dict::S('UI:RelationOption:GroupingThreshold'),
  988. 'refresh' => Dict::S('UI:Button:Refresh'),
  989. ),
  990. 'page_format' => array(
  991. 'label' => Dict::S('UI:Relation:PDFExportPageFormat'),
  992. 'values' => array(
  993. 'A3' => Dict::S('UI:PageFormat_A3'),
  994. 'A4' => Dict::S('UI:PageFormat_A4'),
  995. 'Letter' => Dict::S('UI:PageFormat_Letter'),
  996. ),
  997. ),
  998. 'page_orientation' => array(
  999. 'label' => Dict::S('UI:Relation:PDFExportPageOrientation'),
  1000. 'values' => array(
  1001. 'P' => Dict::S('UI:PageOrientation_Portrait'),
  1002. 'L' => Dict::S('UI:PageOrientation_Landscape'),
  1003. ),
  1004. ),
  1005. );
  1006. if (!extension_loaded('gd'))
  1007. {
  1008. // PDF export requires GD
  1009. unset($aParams['export_as_pdf']);
  1010. }
  1011. if (!extension_loaded('gd') || is_null($sObjClass) || is_null($iObjKey))
  1012. {
  1013. // PDF export requires GD AND a valid objclass/objkey couple
  1014. unset($aParams['export_as_pdf']);
  1015. unset($aParams['export_as_attachment']);
  1016. }
  1017. $oP->add_ready_script("$('#$sId').simple_graph(".json_encode($aParams).");");
  1018. }
  1019. catch(Exception $e)
  1020. {
  1021. $oP->add('<div>'.$e->getMessage().'</div>');
  1022. }
  1023. $oP->add_script(
  1024. <<<EOF
  1025. function DoReload()
  1026. {
  1027. $('#ReloadMovieBtn').button('disable');
  1028. try
  1029. {
  1030. var aExcluded = [];
  1031. $('input[name^=excluded]').each( function() {
  1032. if (!$(this).prop('checked'))
  1033. {
  1034. aExcluded.push($(this).val());
  1035. }
  1036. } );
  1037. $('#graph').simple_graph('option', {excluded_classes: aExcluded});
  1038. $('#graph').simple_graph('reload');
  1039. }
  1040. catch(err)
  1041. {
  1042. alert(err);
  1043. }
  1044. }
  1045. EOF
  1046. );
  1047. }
  1048. }