xml.navigator.php 8.5 KB

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