UniversalSearch.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. $sOQLClass = utils::ReadParam('oql_class', 'bizOrganization');
  16. $sOQLClause = utils::ReadParam('oql_clause', '');
  17. $sClassName = utils::ReadParam('class', $sOQLClass);
  18. $sFilter = utils::ReadParam('filter', '');
  19. $sOperation = utils::ReadParam('operation', '');
  20. // First part: select the class to search for
  21. $oP->add("<form>");
  22. $oP->add("<input type=\"hidden\" name=\"org_id\" value=\"$currentOrganization\" />");
  23. $oP->add("Select the class to search: <select style=\"width: 150px;\" id=\"select_class\" name=\"class\" onChange=\"this.form.submit();\">");
  24. foreach(MetaModel::GetClasses('bizmodel') as $sClass)
  25. {
  26. $sDescription = MetaModel::GetClassDescription($sClass);
  27. $sSelected = ($sClass == $sClassName) ? " SELECTED" : "";
  28. $oP->add("<option value=\"$sClass\" title=\"$sDescription\"$sSelected>".MetaModel::GetName($sClass)."</option>");
  29. }
  30. $oP->add("</select></form>");
  31. try
  32. {
  33. if ($sOperation == 'search_form')
  34. {
  35. $sOQL = "SELECT $sOQLClass $sOQLClause";
  36. $oFilter = DBObjectSearch::FromOQL($sOQL);
  37. }
  38. else
  39. {
  40. // Second part: advanced search form:
  41. if (!empty($sFilter))
  42. {
  43. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  44. }
  45. else if (!empty($sClassName))
  46. {
  47. $oFilter = new CMDBSearchFilter($sClassName);
  48. }
  49. }
  50. }
  51. catch (CoreException $e)
  52. {
  53. $oFilter = new CMDBSearchFilter($sClassName);
  54. $oP->P("<b>Error:</b>");
  55. $oP->P($e->getHtmlDesc());
  56. }
  57. if ($oFilter != null)
  58. {
  59. $oSet = new CMDBObjectSet($oFilter);
  60. $oBlock = new DisplayBlock($oFilter, 'search', false);
  61. $oBlock->Display($oP, 0);
  62. // Search results
  63. $oResultBlock = new DisplayBlock($oFilter, 'list', false);
  64. $oResultBlock->RenderContent($oP);
  65. // Menu node
  66. $sFilter = $oFilter->ToOQL();
  67. $sMenuNodeContent = <<<EOF
  68. <div id="TopPane">
  69. <itopblock BlockClass="DisplayBlock" type="search" asynchronous="false" encoding="text/oql">$sFilter</itopblock>
  70. </div>
  71. <div id="BottomPane">
  72. <p></p>
  73. <itopblock BlockClass="DisplayBlock" type="list" asynchronous="false" encoding="text/oql">$sFilter</itopblock>
  74. </div>
  75. EOF;
  76. if ($sOperation == "add_menu")
  77. {
  78. $oMenuNode = MetaModel::NewObject('menuNode');
  79. $sClass = utils::ReadPostedParam('class', '');
  80. $sLabel = utils::ReadPostedParam('label', '');
  81. $sDescription = utils::ReadPostedParam('description', '');
  82. $iPreviousNodeId = utils::ReadPostedParam('previous_node_id', 1);
  83. $bChildItem = utils::ReadPostedParam('child_item', false);
  84. $oMenuNode->Set('label', $sDescription);
  85. $oMenuNode->Set('name', $sLabel);
  86. $oMenuNode->Set('icon_path', '../images/std_view.gif');
  87. $oMenuNode->Set('template', $sMenuNodeContent);
  88. $oMenuNode->Set('hyperlink', 'UI.php');
  89. $oMenuNode->Set('type', 'user');
  90. $oMenuNode->Set('user_id', UserRights::GetUserId());
  91. $oPreviousNode = MetaModel::GetObject('menuNode', $iPreviousNodeId);
  92. if ($bChildItem)
  93. {
  94. // Insert the new item as a child of the previous one
  95. $oMenuNode->Set('parent_id', $iPreviousNodeId);
  96. $oMenuNode->Set('rank', 1); // A new child item is the first one, so let's start the numbering at 1
  97. // If there are already child nodes, shift their rank by one
  98. // to make room for the newly inserted child node
  99. $oNextNodeSet = $oPreviousNode->GetChildNodesSet(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. else
  109. {
  110. // Insert the new item as the next sibling of the previous one
  111. $oMenuNode->Set('parent_id', $oPreviousNode->Get('parent_id'));
  112. $oMenuNode->Set('rank', 1 + $oPreviousNode->Get('rank')); // the new item comes immediatly after the selected one
  113. // Add 1 to the rank of all the nodes currently following the 'selected' one
  114. // to make room for the newly inserted node
  115. $oNextNodeSet = $oPreviousNode->GetNextNodesSet(null); // null => don't limit ourselves to the user context
  116. // since we need to update all children in order to keep
  117. // the database consistent
  118. while($oNextNode = $oNextNodeSet->Fetch())
  119. {
  120. $oNextNode->Set('rank', 1 + $oNextNode->Get('rank'));
  121. $oNextNode->DBUpdate();
  122. }
  123. }
  124. if ($oMenuNode->CheckToInsert())
  125. {
  126. $oMenuNode->DBInsert();
  127. $oP->add("<form method=\"get\">");
  128. $oP->add("<p>Menu item created !</p>");
  129. $oP->add("<input type=\"hidden\" name=\"filter\" value=\"$sFilter\">");
  130. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClassName\">");
  131. $oP->add("<input type=\"submit\" name=\"\" value=\"Reload Page\">");
  132. $oP->add("<form>");
  133. }
  134. }
  135. $oP->add("</div>\n");
  136. }
  137. else
  138. {
  139. $oP->add("</div>\n");
  140. }
  141. $oP->output();
  142. ?>