xml.navigator.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // Copyright (C) 2010-2012 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. * Specific page to build the XML data describing the "relation" around a given seed object
  20. * This XML is desgined to be consumed by the Flash Navigator object (see ../navigator folder)
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. require_once('../approot.inc.php');
  26. require_once(APPROOT.'/application/application.inc.php');
  27. require_once(APPROOT.'/application/webpage.class.inc.php');
  28. require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
  29. require_once(APPROOT.'/application/wizardhelper.class.inc.php');
  30. require_once(APPROOT.'/application/ui.linkswidget.class.inc.php');
  31. define('MAX_RECURSION_DEPTH', 20);
  32. /**
  33. * Fills the given XML node with te details of the specified object
  34. */
  35. function AddNodeDetails(&$oNode, $oObj)
  36. {
  37. $aZlist = MetaModel::GetZListItems(get_class($oObj), 'list');
  38. $aLabels = array();
  39. $index = 0;
  40. foreach($aZlist as $sAttCode)
  41. {
  42. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  43. $aLabels[] = $oAttDef->GetLabel();
  44. if (!$oAttDef->IsLinkSet())
  45. {
  46. $oNode->SetAttribute('att_'.$index, $oObj->GetAsHTML($sAttCode));
  47. }
  48. $index++;
  49. }
  50. $oNode->SetAttribute('zlist', implode(',', $aLabels));
  51. }
  52. /**
  53. * Get the related objects through the given relation, output in XML
  54. * @param DBObject $oObj The current object
  55. * @param string $sRelation The name of the relation to search with
  56. */
  57. function GetRelatedObjectsAsXml(DBObject $oObj, $sRelationName, &$oLinks, &$oXmlDoc, &$oXmlNode, $iDepth = 0, $aExcludedClasses)
  58. {
  59. $aResults = array();
  60. $bAddLinks = false;
  61. $oObj->GetRelatedObjects($sRelationName, 1 /* iMaxDepth */, $aResults);
  62. if ($iDepth > MAX_RECURSION_DEPTH) return;
  63. foreach($aResults as $sRelatedClass => $aObjects)
  64. {
  65. foreach($aObjects as $id => $oTargetObj)
  66. {
  67. if (is_object($oTargetObj))
  68. {
  69. if (in_array(get_class($oTargetObj), $aExcludedClasses))
  70. {
  71. GetRelatedObjectsAsXml($oTargetObj, $sRelationName, $oLinks, $oXmlDoc, $oXmlNode, $iDepth++, $aExcludedClasses);
  72. }
  73. else
  74. {
  75. $oLinkingNode = $oXmlDoc->CreateElement('link');
  76. $oLinkingNode->SetAttribute('relation', $sRelationName);
  77. $oLinkingNode->SetAttribute('arrow', 1); // Such relations have a direction, display an arrow
  78. $oLinkedNode = $oXmlDoc->CreateElement('node');
  79. $oLinkedNode->SetAttribute('id', $oTargetObj->GetKey());
  80. $oLinkedNode->SetAttribute('obj_class', get_class($oTargetObj));
  81. $oLinkedNode->SetAttribute('obj_class_name', htmlspecialchars(MetaModel::GetName(get_class($oTargetObj))));
  82. $oLinkedNode->SetAttribute('name', htmlspecialchars($oTargetObj->GetRawName())); // htmlentities is too much for XML
  83. $oLinkedNode->SetAttribute('icon', BuildIconPath($oTargetObj->GetIcon(false /* No IMG tag */)));
  84. AddNodeDetails($oLinkedNode, $oTargetObj);
  85. $oSubLinks = $oXmlDoc->CreateElement('links');
  86. // Recurse
  87. GetRelatedObjectsAsXml($oTargetObj, $sRelationName, $oSubLinks, $oXmlDoc, $oLinkedNode, $iDepth++, $aExcludedClasses);
  88. $oLinkingNode->AppendChild($oLinkedNode);
  89. $oLinks->AppendChild($oLinkingNode);
  90. $bAddLinks = true;
  91. }
  92. }
  93. }
  94. }
  95. if ($bAddLinks)
  96. {
  97. $oXmlNode->AppendChild($oLinks);
  98. }
  99. }
  100. function BuildIconPath($sIconPath)
  101. {
  102. return $sIconPath;
  103. }
  104. require_once(APPROOT.'/application/startup.inc.php');
  105. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  106. // For developping the Navigator from within Flash
  107. //session_start();
  108. //$_SESSION['auth_user'] = 'admin';
  109. //UserRights::Login($_SESSION['auth_user']); // Set the user's language
  110. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  111. $oPage = new ajax_page("");
  112. $oPage->no_cache();
  113. $sClass = utils::ReadParam('class', 'Contact', false, 'class');
  114. $id = utils::ReadParam('id', 1);
  115. $sRelation = utils::ReadParam('relation', 'impacts');
  116. $aValidRelations = MetaModel::EnumRelations();
  117. $sFormat = utils::ReadParam('format', 'xml');
  118. $sExcludedClasses = utils::ReadParam('exclude', '', false, 'raw_data');
  119. $aExcludedClasses = explode(',', $sExcludedClasses);
  120. if (!in_array($sRelation, $aValidRelations))
  121. {
  122. // Not a valid relation, use the default one instead
  123. $sRelation = 'impacts';
  124. }
  125. try
  126. {
  127. if ($id != 0)
  128. {
  129. switch($sFormat)
  130. {
  131. case 'html':
  132. $oPage->SetContentType('text/html');
  133. $oObj = MetaModel::GetObject($sClass, $id, true /* object must exist */);
  134. $aResults = array();
  135. $oObj->GetRelatedObjects($sRelation, MAX_RECURSION_DEPTH /* iMaxDepth */, $aResults);
  136. $iBlock = 1; // Zero is not a valid blockid
  137. foreach($aResults as $sClass => $aObjects)
  138. {
  139. $oSet = CMDBObjectSet::FromArray($sClass, $aObjects);
  140. $oPage->add("<h1>".MetaModel::GetRelationDescription($sRelation).' '.$oObj->GetName()."</h1>\n");
  141. $oPage->add("<div class=\"page_header\">\n");
  142. $oPage->add("<h2>".MetaModel::GetClassIcon($sClass)."&nbsp;<span class=\"hilite\">".Dict::Format('UI:Search:Count_ObjectsOf_Class_Found', count($aObjects), Metamodel::GetName($sClass))."</h2>\n");
  143. $oPage->add("</div>\n");
  144. $oBlock = DisplayBlock::FromObjectSet($oSet, 'list');
  145. $oBlock->Display($oPage, $iBlock++);
  146. $oPage->P('&nbsp;'); // Some space ?
  147. }
  148. break;
  149. case 'xml':
  150. default:
  151. $oPage->SetContentType('text/xml');
  152. $oObj = MetaModel::GetObject($sClass, $id, true /* object must exist */);
  153. // Build the root XML part
  154. $oXmlDoc = new DOMDocument( '1.0', 'UTF-8' );
  155. $oXmlRoot = $oXmlDoc->CreateElement('root');
  156. $oXmlNode = $oXmlDoc->CreateElement('node');
  157. $oXmlNode->SetAttribute('id', $oObj->GetKey());
  158. $oXmlNode->SetAttribute('obj_class', get_class($oObj));
  159. $oXmlNode->SetAttribute('obj_class_name', htmlspecialchars(MetaModel::GetName(get_class($oObj))));
  160. $oXmlNode->SetAttribute('name', htmlspecialchars($oObj->GetRawName()));
  161. $oXmlNode->SetAttribute('icon', BuildIconPath($oObj->GetIcon(false /* No IMG tag */))); // Hard coded for the moment
  162. AddNodeDetails($oXmlNode, $oObj);
  163. $oLinks = $oXmlDoc->CreateElement("links");
  164. $oXmlRoot->SetAttribute('position', 'left');
  165. $oXmlRoot->SetAttribute('title', MetaModel::GetRelationDescription($sRelation).' '. htmlspecialchars($oObj->GetRawName()));
  166. GetRelatedObjectsAsXml($oObj, $sRelation, $oLinks, $oXmlDoc, $oXmlNode, 0, $aExcludedClasses);
  167. $oXmlRoot->AppendChild($oXmlNode);
  168. $oXmlDoc->AppendChild($oXmlRoot);
  169. $oPage->add($oXmlDoc->SaveXML());
  170. break;
  171. }
  172. }
  173. $oPage->output();
  174. }
  175. catch(Exception $e)
  176. {
  177. echo "Error: ".$e->getMessage();
  178. }
  179. ?>