xml.navigator.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Specific page to build the XML data describing the "relation" around a given seed object
  18. * This XML is desgined to be consumed by the Flash Navigator object (see ../navigator folder)
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  24. */
  25. require_once('../application/application.inc.php');
  26. require_once('../application/webpage.class.inc.php');
  27. require_once('../application/ajaxwebpage.class.inc.php');
  28. require_once('../application/wizardhelper.class.inc.php');
  29. require_once('../application/ui.linkswidget.class.inc.php');
  30. define('MAX_RECURSION_DEPTH', 20);
  31. /**
  32. * Fills the given XML node with te details of the specified object
  33. */
  34. function AddNodeDetails(&$oNode, $oObj)
  35. {
  36. $aZlist = MetaModel::GetZListItems(get_class($oObj), 'list');
  37. $aLabels = array();
  38. $index = 0;
  39. foreach($aZlist as $sAttCode)
  40. {
  41. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  42. $aLabels[] = $oAttDef->GetLabel();
  43. if (!$oAttDef->IsLinkSet())
  44. {
  45. $oNode->SetAttribute('att_'.$index, $oObj->GetAsHTML($sAttCode));
  46. }
  47. $index++;
  48. }
  49. $oNode->SetAttribute('zlist', implode(',', $aLabels));
  50. }
  51. /**
  52. * Get the related objects through the given relation, output in XML
  53. * @param DBObject $oObj The current object
  54. * @param string $sRelation The name of the relation to search with
  55. */
  56. function GetRelatedObjectsAsXml(DBObject $oObj, $sRelationName, &$oLinks, &$oXmlDoc, &$oXmlNode)
  57. {
  58. $aResults = array();
  59. $oObj->GetRelatedObjects($sRelationName, 1 /* iMaxDepth */, $aResults);
  60. static $iDepth = 0;
  61. $iDepth++;
  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. $oLinkingNode = $oXmlDoc->CreateElement('link');
  70. $oLinkingNode->SetAttribute('relation', $sRelationName);
  71. $oLinkingNode->SetAttribute('arrow', 1); // Such relations have a direction, display an arrow
  72. $oLinkedNode = $oXmlDoc->CreateElement('node');
  73. $oLinkedNode->SetAttribute('id', $oTargetObj->GetKey());
  74. $oLinkedNode->SetAttribute('obj_class', get_class($oTargetObj));
  75. $oLinkedNode->SetAttribute('obj_class_name', MetaModel::GetName(get_class($oTargetObj)));
  76. $oLinkedNode->SetAttribute('name', $oTargetObj->GetName());
  77. $oLinkedNode->SetAttribute('icon', BuildIconPath($oTargetObj->GetIcon(false /* No IMG tag */)));
  78. AddNodeDetails($oLinkedNode, $oTargetObj);
  79. $oSubLinks = $oXmlDoc->CreateElement('links');
  80. // Recurse
  81. GetRelatedObjectsAsXml($oTargetObj, $sRelationName, $oSubLinks, $oXmlDoc, $oLinkedNode);
  82. $oLinkingNode->AppendChild($oLinkedNode);
  83. $oLinks->AppendChild($oLinkingNode);
  84. }
  85. }
  86. }
  87. if (count($aResults) > 0)
  88. {
  89. $oXmlNode->AppendChild($oLinks);
  90. }
  91. }
  92. function BuildIconPath($sIconPath)
  93. {
  94. $sFullURL = utils::GetAbsoluteURL(false, false);
  95. $iLastSlashPos = strrpos($sFullURL, '/');
  96. $sFullURLPath = substr($sFullURL, 0, 1 + $iLastSlashPos);
  97. return $sFullURLPath.$sIconPath;
  98. }
  99. require_once('../application/startup.inc.php');
  100. require_once('../application/loginwebpage.class.inc.php');
  101. // For developping the Navigator from within Flash
  102. //session_start();
  103. //$_SESSION['auth_user'] = 'admin';
  104. //UserRights::Login($_SESSION['auth_user']); // Set the user's language
  105. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  106. $oPage = new ajax_page("");
  107. $oPage->no_cache();
  108. $sClass = utils::ReadParam('class', 'Contact');
  109. $id = utils::ReadParam('id', 1);
  110. $sRelation = utils::ReadParam('relation', 'impacts');
  111. $aValidRelations = MetaModel::EnumRelations();
  112. $sFormat = utils::ReadParam('format', 'xml');
  113. if (!in_array($sRelation, $aValidRelations))
  114. {
  115. // Not a valid relation, use the default one instead
  116. $sRelation = 'impacts';
  117. }
  118. try
  119. {
  120. if ($id != 0)
  121. {
  122. switch($sFormat)
  123. {
  124. case 'html':
  125. $oObj = MetaModel::GetObject($sClass, $id, true /* object must exist */);
  126. $aResults = array();
  127. $oObj->GetRelatedObjects($sRelation, MAX_RECURSION_DEPTH /* iMaxDepth */, $aResults);
  128. $iBlock = 0;
  129. foreach($aResults as $sClass => $aObjects)
  130. {
  131. $oSet = CMDBObjectSet::FromArray($sClass, $aObjects);
  132. $oPage->add("<h1>".MetaModel::GetRelationDescription($sRelation).' '.$oObj->GetName()."</h1>\n");
  133. $oPage->add("<div class=\"page_header\">\n");
  134. $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");
  135. $oPage->add("</div>\n");
  136. $oBlock = DisplayBlock::FromObjectSet($oSet, 'list');
  137. $oBlock->Display($oPage, $iBlock++);
  138. $oPage->P('&nbsp;'); // Some space ?
  139. }
  140. break;
  141. case 'xml':
  142. default:
  143. $oObj = MetaModel::GetObject($sClass, $id, true /* object must exist */);
  144. // Build the root XML part
  145. $oXmlDoc = new DOMDocument( '1.0', 'UTF-8' );
  146. $oXmlRoot = $oXmlDoc->CreateElement('root');
  147. $oXmlNode = $oXmlDoc->CreateElement('node');
  148. $oXmlNode->SetAttribute('id', $oObj->GetKey());
  149. $oXmlNode->SetAttribute('obj_class', get_class($oObj));
  150. $oXmlNode->SetAttribute('obj_class_name', MetaModel::GetName(get_class($oObj)));
  151. $oXmlNode->SetAttribute('name', $oObj->GetName());
  152. $oXmlNode->SetAttribute('icon', BuildIconPath($oObj->GetIcon(false /* No IMG tag */))); // Hard coded for the moment
  153. AddNodeDetails($oXmlNode, $oObj);
  154. $oLinks = $oXmlDoc->CreateElement("links");
  155. $oXmlRoot->SetAttribute('position', 'left');
  156. $oXmlRoot->SetAttribute('title', MetaModel::GetRelationDescription($sRelation).' '.$oObj->GetName());
  157. GetRelatedObjectsAsXml($oObj, $sRelation, $oLinks, $oXmlDoc, $oXmlNode);
  158. $oXmlRoot->AppendChild($oXmlNode);
  159. $oXmlDoc->AppendChild($oXmlRoot);
  160. $oPage->add($oXmlDoc->SaveXML());
  161. break;
  162. }
  163. }
  164. $oPage->output();
  165. }
  166. catch(Exception $e)
  167. {
  168. echo "Error: ".$e->getMessage();
  169. }
  170. ?>