UniversalSearch.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. require_once('../application/application.inc.php');
  3. require_once('../application/itopwebpage.class.inc.php');
  4. require_once('../application/applicationcontext.class.inc.php');
  5. require_once('../application/startup.inc.php');
  6. require_once('../application/loginwebpage.class.inc.php');
  7. login_web_page::DoLogin(); // Check user rights and prompt if needed
  8. $oContext = new UserContext();
  9. $oAppContext = new ApplicationContext();
  10. $iActiveNodeId = utils::ReadParam('menu', -1);
  11. $currentOrganization = utils::ReadParam('org_id', 1);
  12. $oP = new iTopWebPage("iTop - Universal search", $currentOrganization);
  13. // From now on the context is limited to the the selected organization ??
  14. // Now render the content of the page
  15. $sClassName = utils::ReadParam('class', 'bizOrganization');
  16. $sFilter = utils::ReadParam('filter', '');
  17. $sOperation = utils::ReadParam('operation', '');
  18. // First part: select the class to search for
  19. $oP->add("<div id=\"TopPane\">");
  20. $oP->add("<form>");
  21. $oP->add("<input type=\"hidden\" name=\"org_id\" value=\"$currentOrganization\" />");
  22. $oP->add("Select the class to search: <select style=\"width: 150px;\" id=\"select_class\" name=\"class\" onChange=\"this.form.submit();\">");
  23. foreach(MetaModel::GetClasses('bizmodel') as $sClass)
  24. {
  25. $sDescription = MetaModel::GetClassDescription($sClass);
  26. $sSelected = ($sClass == $sClassName) ? " SELECTED" : "";
  27. $oP->add("<option value=\"$sClass\" title=\"$sDescription\"$sSelected>$sClass</option>");
  28. }
  29. $oP->add("</select></form>");
  30. // Second part: advanced search form:
  31. $oFilter = null;
  32. if (!empty($sFilter))
  33. {
  34. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  35. }
  36. else if (!empty($sClassName))
  37. {
  38. $oFilter = new CMDBSearchFilter($sClassName);
  39. }
  40. if ($oFilter != null)
  41. {
  42. $oSet =new CMDBObjectSet($oFilter);
  43. cmdbAbstractObject::DisplaySearchForm($oP, $oSet, array('org_id' => $currentOrganization, 'class' => $sClassName));
  44. $oP->add("</div>\n");
  45. // Search results
  46. $oP->add("<div id=\"BottomPane\">");
  47. $oResultBlock = new DisplayBlock($oFilter, 'list', false);
  48. $oResultBlock->RenderContent($oP);
  49. // Menu node
  50. $sFilter = $oFilter->ToOQL();
  51. $sMenuNodeContent = <<<EOF
  52. <div id="TopPane">
  53. <itopblock BlockClass="DisplayBlock" type="search" asynchronous="false" encoding="text/oql">$sFilter</itopblock>
  54. </div>
  55. <div id="BottomPane">
  56. <p></p>
  57. <itopblock BlockClass="DisplayBlock" type="list" asynchronous="false" encoding="text/oql">$sFilter</itopblock>
  58. </div>
  59. EOF;
  60. if ($sOperation == "add_menu")
  61. {
  62. $oMenuNode = MetaModel::NewObject('menuNode');
  63. $sClass = utils::ReadPostedParam('class', '');
  64. $sLabel = utils::ReadPostedParam('label', '');
  65. $sDescription = utils::ReadPostedParam('description', '');
  66. $iPreviousNodeId = utils::ReadPostedParam('previous_node_id', 1);
  67. $bChildItem = utils::ReadPostedParam('child_item', false);
  68. $oMenuNode->Set('label', $sDescription);
  69. $oMenuNode->Set('name', $sLabel);
  70. $oMenuNode->Set('icon_path', '/images/std_view.gif');
  71. $oMenuNode->Set('template', $sMenuNodeContent);
  72. $oMenuNode->Set('hyperlink', 'UI.php');
  73. $oMenuNode->Set('type', 'user');
  74. $oMenuNode->Set('user_id', UserRights::GetUserId());
  75. $oPreviousNode = MetaModel::GetObject('menuNode', $iPreviousNodeId);
  76. if ($bChildItem)
  77. {
  78. // Insert the new item as a child of the previous one
  79. $oMenuNode->Set('parent_id', $iPreviousNodeId);
  80. $oMenuNode->Set('rank', 1); // A new child item is the first one, so let's start the numbering at 1
  81. // If there are already child nodes, shift their rank by one
  82. // to make room for the newly inserted child node
  83. $oNextNodeSet = $oPreviousNode->GetChildNodesSet(null); // null => don't limit ourselves to the user context
  84. // since we need to update all children in order to keep
  85. // the database consistent
  86. while($oNextNode = $oNextNodeSet->Fetch())
  87. {
  88. $oNextNode->Set('rank', 1 + $oNextNode->Get('rank'));
  89. $oNextNode->DBUpdate();
  90. }
  91. }
  92. else
  93. {
  94. // Insert the new item as the next sibling of the previous one
  95. $oMenuNode->Set('parent_id', $oPreviousNode->Get('parent_id'));
  96. $oMenuNode->Set('rank', 1 + $oPreviousNode->Get('rank')); // the new item comes immediatly after the selected one
  97. // Add 1 to the rank of all the nodes currently following the 'selected' one
  98. // to make room for the newly inserted node
  99. $oNextNodeSet = $oPreviousNode->GetNextNodesSet(null); // null => don't limit ourselves to the user context
  100. // since we need to update all children in order to keep
  101. // the database consistent
  102. while($oNextNode = $oNextNodeSet->Fetch())
  103. {
  104. $oNextNode->Set('rank', 1 + $oNextNode->Get('rank'));
  105. $oNextNode->DBUpdate();
  106. }
  107. }
  108. if ($oMenuNode->CheckToInsert())
  109. {
  110. $oMenuNode->DBInsert();
  111. $oP->add("<form method=\"get\">");
  112. $oP->add("<p>Menu item created !</p>");
  113. $oP->add("<input type=\"hidden\" name=\"filter\" value=\"$sFilter\">");
  114. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClassName\">");
  115. $oP->add("<input type=\"submit\" name=\"\" value=\"Reload Page\">");
  116. $oP->add("<form>");
  117. }
  118. }
  119. $oP->add("</div>\n");
  120. }
  121. else
  122. {
  123. $oP->add("</div>\n");
  124. }
  125. $oP->output();
  126. ?>