UniversalSearch.php 5.8 KB

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