xml.navigator.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. require_once('../application/application.inc.php');
  3. require_once('../application/webpage.class.inc.php');
  4. require_once('../application/ajaxwebpage.class.inc.php');
  5. require_once('../application/wizardhelper.class.inc.php');
  6. require_once('../application/ui.linkswidget.class.inc.php');
  7. /**
  8. * Determines the most appropriate icon (among the ones supported by the navigator)
  9. * for the given object
  10. * @param $oObj DBObject
  11. * @return string The name of the icon
  12. */
  13. function GetIcon(DBObject $oObj)
  14. {
  15. switch(get_class($oObj))
  16. {
  17. case 'bizSoftware':
  18. $sIcon = 'application';
  19. break;
  20. case 'bizDatabase':
  21. $sIcon = 'database';
  22. break;
  23. case 'bizBusinessProcess':
  24. $sIcon = 'business_process';
  25. break;
  26. case 'bizContract':
  27. $sIcon = 'contract';
  28. break;
  29. case 'bizChangeTicket':
  30. $sIcon = 'change';
  31. break;
  32. case 'bizServiceCall':
  33. case 'bizIncidentTicket':
  34. $sIcon = 'incident';
  35. break;
  36. case 'bizServer':
  37. $sIcon = 'server';
  38. break;
  39. case 'bizPC':
  40. if ($oObj->Get('type') == 'desktop')
  41. {
  42. $sIcon = 'desktop PC';
  43. }
  44. else
  45. {
  46. $sIcon = 'laptop';
  47. }
  48. break;
  49. case 'bizNetworkDevice':
  50. $sIcon = 'network_device';
  51. break;
  52. case 'bizInterface':
  53. $sIcon = 'interface';
  54. break;
  55. case 'bizPerson':
  56. case 'bizTeam':
  57. $sIcon = 'contact';
  58. break;
  59. default:
  60. $sIcon = 'application';
  61. }
  62. return $sIcon;
  63. }
  64. /**
  65. * Fills the given XML node with te details of the specified object
  66. */
  67. function AddNodeDetails(&$oNode, $oObj)
  68. {
  69. $aZlist = MetaModel::GetZListItems(get_class($oObj), 'details');
  70. $aLabels = array();
  71. $index = 0;
  72. foreach($aZlist as $sAttCode)
  73. {
  74. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  75. $aLabels[] = $oAttDef->GetLabel();
  76. $oNode->SetAttribute('att_'.$index, $oObj->Get($sAttCode));
  77. $index++;
  78. }
  79. $oNode->SetAttribute('zlist', implode(',', $aLabels));
  80. }
  81. /**
  82. * Get the neighbours i.e. objects linked to the current object
  83. * @param DBObject $oObj The current object
  84. */
  85. function GetNeighbours(DBObject $oObj, &$oLinks, &$oXmlDoc, &$oXmlNode)
  86. {
  87. $oContext = new UserContext();
  88. $aRefs = MetaModel::EnumReferencingClasses(get_class($oObj));
  89. $aExtKeys = MetaModel::EnumLinkingClasses(get_class($oObj));
  90. if ((count($aRefs) != 0) || (count($aExtKeys) != 0))
  91. {
  92. foreach ($aRefs as $sRemoteClass => $aRemoteKeys)
  93. {
  94. foreach ($aRemoteKeys as $sExtKeyAttCode => $oExtKeyAttDef)
  95. {
  96. $oFilter = $oContext->NewFilter($sRemoteClass);
  97. $oFilter->AddCondition($sExtKeyAttCode, $oObj->GetKey());
  98. $oSet = new DBObjectSet($oFilter);
  99. if ($oSet->Count() > 0)
  100. {
  101. if (substr($sRemoteClass,0,3) == 'lnk')
  102. {
  103. // Special processing for "links":
  104. // Find out the first field which is an external key to another object,
  105. // and display this object as the one linked to the root object
  106. $sTargetClass = '';
  107. $sTargetAttCode = '';
  108. foreach(MetaModel::ListAttributeDefs($sRemoteClass) as $sAttCode=>$oAttDef)
  109. {
  110. if (($sAttCode != $sExtKeyAttCode) && ($oAttDef->IsExternalKey()) )
  111. {
  112. $sTargetClass = $oAttDef->GetTargetClass();
  113. $sTargetAttCode = $sAttCode;
  114. break;
  115. }
  116. }
  117. if ($sTargetClass != '')
  118. {
  119. while( $oRelatedObj = $oSet->Fetch())
  120. {
  121. $oTargetObj = $oContext->GetObject($sTargetClass, $oRelatedObj->Get($sTargetAttCode));
  122. if (is_object($oTargetObj))
  123. {
  124. $oLinkingNode = $oXmlDoc->CreateElement('link');
  125. $oLinkedNode = $oXmlDoc->CreateElement('node');
  126. $oLinkedNode->SetAttribute('id', $oTargetObj->GetKey());
  127. $oLinkedNode->SetAttribute('obj_class', get_class($oTargetObj));
  128. $oLinkedNode->SetAttribute('name', $oTargetObj->GetName());
  129. $oLinkedNode->SetAttribute('icon', BuildIconPath($oTargetObj->GetIcon()) );
  130. AddNodeDetails($oLinkedNode, $oTargetObj);
  131. $oLinkingNode->AppendChild($oLinkedNode);
  132. $oLinks->AppendChild($oLinkingNode);
  133. }
  134. }
  135. }
  136. }
  137. else
  138. {
  139. while( $oRelatedObj = $oSet->Fetch())
  140. {
  141. $oLinkingNode = $oXmlDoc->CreateElement('link');
  142. $oLinkedNode = $oXmlDoc->CreateElement('node');
  143. $oLinkedNode->SetAttribute('id', $oRelatedObj->GetKey());
  144. $oLinkedNode->SetAttribute('obj_class', get_class($oRelatedObj));
  145. $oLinkedNode->SetAttribute('name', $oRelatedObj->GetName());
  146. $oLinkedNode->SetAttribute('icon', BuildIconPath($oRelatedObj->GetIcon()));
  147. AddNodeDetails($oLinkedNode, $oRelatedObj);
  148. $oLinkingNode->AppendChild($oLinkedNode);
  149. $oLinks->AppendChild($oLinkingNode);
  150. }
  151. }
  152. }
  153. }
  154. foreach ($aExtKeys as $sLinkClass => $aRemoteClasses)
  155. {
  156. foreach($aRemoteClasses as $sExtKeyAttCode => $sRemoteClass)
  157. {
  158. // Special case to exclude such "silos" classes that will be linked
  159. // to almost all the objects of a chart and thus would make the chart
  160. // un-readable if all the links are displayed...
  161. if (($sLinkClass == get_class($oObj)) &&
  162. ($sRemoteClass != 'Location') &&
  163. ($sRemoteClass != 'Organization') )
  164. {
  165. $oRelatedObj = $oContext->GetObject($sRemoteClass, $oObj->Get($sExtKeyAttCode));
  166. $oLinkingNode = $oXmlDoc->CreateElement('link');
  167. $oLinkedNode = $oXmlDoc->CreateElement('node');
  168. $oLinkedNode->SetAttribute('id', $oRelatedObj->GetKey());
  169. $oLinkedNode->SetAttribute('obj_class', get_class($oRelatedObj));
  170. $oLinkedNode->SetAttribute('name', $oRelatedObj->GetName());
  171. $oLinkedNode->SetAttribute('icon', BuildIconPath($oRelatedObj->GetIcon()));
  172. AddNodeDetails($oLinkedNode, $oRelatedObj);
  173. $oLinkingNode->AppendChild($oLinkedNode);
  174. $oLinks->AppendChild($oLinkingNode);
  175. }
  176. }
  177. }
  178. }
  179. $oXmlNode->AppendChild($oLinks);
  180. }
  181. }
  182. /**
  183. * Get the related objects through the given relation
  184. * @param DBObject $oObj The current object
  185. * @param string $sRelation The name of the relation to search with
  186. */
  187. function GetRelatedObjects(DBObject $oObj, $sRelationName, &$oLinks, &$oXmlDoc, &$oXmlNode)
  188. {
  189. $oContext = new UserContext();
  190. $aResults = array();
  191. $oObj->GetRelatedObjects($sRelationName, 1 /* iMaxDepth */, $aResults);
  192. foreach($aResults as $sRelatedClass => $aObjects)
  193. {
  194. foreach($aObjects as $id => $oTargetObj)
  195. {
  196. if (is_object($oTargetObj))
  197. {
  198. $oLinkingNode = $oXmlDoc->CreateElement('link');
  199. $oLinkingNode->SetAttribute('relation', $sRelationName);
  200. $oLinkingNode->SetAttribute('arrow', 1); // Such relations have a direction, display an arrow
  201. $oLinkingNode->SetAttribute('debug', 142); // Such relations have a direction, display an arrow
  202. $oLinkedNode = $oXmlDoc->CreateElement('node');
  203. $oLinkedNode->SetAttribute('id', $oTargetObj->GetKey());
  204. $oLinkedNode->SetAttribute('obj_class', get_class($oTargetObj));
  205. $oLinkedNode->SetAttribute('name', $oTargetObj->GetName());
  206. $oLinkedNode->SetAttribute('icon', BuildIconPath($oTargetObj->GetIcon()));
  207. AddNodeDetails($oLinkedNode, $oTargetObj);
  208. $oSubLinks = $oXmlDoc->CreateElement('links');
  209. GetRelatedObjects($oTargetObj, $sRelationName, $oSubLinks, $oXmlDoc, $oLinkedNode);
  210. $oLinkingNode->AppendChild($oLinkedNode);
  211. $oLinks->AppendChild($oLinkingNode);
  212. }
  213. }
  214. }
  215. if (count($aResults) > 0)
  216. {
  217. $oXmlNode->AppendChild($oLinks);
  218. }
  219. }
  220. function BuildIconPath($sIconPath)
  221. {
  222. $sFullURL = utils::GetAbsoluteURL(false, false);
  223. $iLastSlashPos = strrpos($sFullURL, '/');
  224. $sFullURLPath = substr($sFullURL, 0, 1 + $iLastSlashPos);
  225. return $sFullURLPath.$sIconPath;
  226. }
  227. require_once('../application/startup.inc.php');
  228. require_once('../application/loginwebpage.class.inc.php');
  229. // For developping the Navigator
  230. session_start();
  231. $_SESSION['auth_user'] = 'admin';
  232. $_SESSION['auth_pwd'] = 'admin2';
  233. UserRights::Login($_SESSION['auth_user'], $_SESSION['auth_pwd']); // Set the user's language
  234. //LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  235. $oPage = new ajax_page("");
  236. $oPage->no_cache();
  237. $oContext = new UserContext();
  238. $sClass = utils::ReadParam('class', 'Contact');
  239. $id = utils::ReadParam('id', 1);
  240. $sRelation = utils::ReadParam('relation', 'impact');
  241. $aValidRelations = MetaModel::EnumRelations();
  242. if (!in_array($sRelation, $aValidRelations))
  243. {
  244. // Not a valid relation, use the default one instead
  245. $sRelation = 'neighbours';
  246. }
  247. if ($id != 0)
  248. {
  249. $oObj = $oContext->GetObject($sClass, $id);
  250. // Build the root XML part
  251. $oXmlDoc = new DOMDocument( '1.0', 'UTF-8' );
  252. $oXmlRoot = $oXmlDoc->CreateElement('root');
  253. $oXmlNode = $oXmlDoc->CreateElement('node');
  254. $oXmlNode->SetAttribute('id', $oObj->GetKey());
  255. $oXmlNode->SetAttribute('obj_class', get_class($oObj));
  256. $oXmlNode->SetAttribute('name', $oObj->GetName());
  257. $oXmlNode->SetAttribute('icon', BuildIconPath($oObj->GetIcon())); // Hard coded for the moment
  258. AddNodeDetails($oXmlNode, $oObj);
  259. $oLinks = $oXmlDoc->CreateElement("links");
  260. switch($sRelation)
  261. {
  262. case 'neighbours':
  263. // Now search for all the neighboor objects and append them
  264. $oXmlRoot->SetAttribute('title', 'Neighbours of '.$oObj->GetName());
  265. GetNeighbours($oObj, $oLinks, $oXmlDoc, $oXmlNode);
  266. $oXmlRoot->SetAttribute('position', 'center');
  267. break;
  268. default:
  269. $oXmlRoot->SetAttribute('position', 'left');
  270. $oXmlRoot->SetAttribute('title', MetaModel::GetRelationDescription($sRelation).' '.$oObj->GetName());
  271. GetRelatedObjects($oObj, $sRelation, $oLinks, $oXmlDoc, $oXmlNode);
  272. }
  273. $oXmlRoot->AppendChild($oXmlNode);
  274. $oXmlDoc->AppendChild($oXmlRoot);
  275. $oPage->add($oXmlDoc->SaveXML());
  276. }
  277. $oPage->output();
  278. ?>