run_query.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. require_once('../application/application.inc.php');
  3. require_once('../application/itopwebpage.class.inc.php');
  4. require_once('../application/startup.inc.php');
  5. require_once('../application/loginwebpage.class.inc.php');
  6. login_web_page::DoLogin(); // Check user rights and prompt if needed
  7. $sOperation = utils::ReadParam('operation', 'menu');
  8. $oContext = new UserContext();
  9. $oAppContext = new ApplicationContext();
  10. $iActiveNodeId = utils::ReadParam('menu', -1);
  11. $currentOrganization = utils::ReadParam('org_id', '');
  12. $oP = new iTopWebPage("iTop - Expression Evaluation", $currentOrganization);
  13. // Main program
  14. $sExpression = utils::ReadParam('expression', '');
  15. $sEncoding = utils::ReadParam('encoding', 'oql');
  16. try
  17. {
  18. if ($sEncoding == 'crypted')
  19. {
  20. // Translate $sExpression into a oql expression
  21. $sClearText = base64_decode($sExpression);
  22. echo "<strong>FYI: '$sClearText'</strong><br/>\n";
  23. $oFilter = DBObjectSearch::unserialize($sExpression);
  24. $sExpression = $oFilter->ToOQL();
  25. exit;
  26. }
  27. else
  28. {
  29. // leave $sExpression as is
  30. }
  31. $aExamples = array(
  32. "Applications" => "SELECT bizApplication",
  33. "Changes planned on new year's day" => "SELECT bizChangeTicket AS ch WHERE ch.start_date >= '2009-12-31' AND ch.end_date <= '2010-01-01'",
  34. "Person having an 'A' in their name" => "SELECT bizPerson AS B WHERE B.name LIKE '%A%'",
  35. "NW interfaces of equipment in production for customer 'Demo'" => "SELECT bizInterface AS if JOIN bizDevice AS dev ON if.device_id = dev.id WHERE if.status = 'production' AND dev.status = 'production' AND dev.org_name = 'Demo' AND if.physical_type = 'ethernet'"
  36. );
  37. $oP->add("<form method=\"get\">\n");
  38. $oP->add("Expression to evaluate:<br/>\n");
  39. $oP->add("<textarea cols=\"50\" rows=\"20\" name=\"expression\">$sExpression</textarea>\n");
  40. $oP->add("<input type=\"submit\" value=\" Evaluate \">\n");
  41. $oP->add("</form>\n");
  42. $oP->add("<h3>Examples</h3>\n");
  43. $aDisplayData = array();
  44. foreach($aExamples as $sDescription => $sOql)
  45. {
  46. $sHighlight = '';
  47. $sDisable = '';
  48. if ($sOql == $sExpression)
  49. {
  50. // this one is currently being tested, highlight it
  51. $sHighlight = "background-color:yellow;";
  52. $sDisable = 'disabled';
  53. }
  54. $aDisplayData[] = array(
  55. 'desc' => "<div style=\"$sHighlight\">$sDescription</div>",
  56. 'oql' => "<div style=\"$sHighlight\">$sOql</div>",
  57. 'go' => "<form method=\"get\"><input type=\"hidden\" name=\"expression\" value=\"$sOql\"><input type=\"submit\" value=\"Test!\" $sDisable></form>\n",
  58. );
  59. }
  60. $aDisplayConfig = array();
  61. $aDisplayConfig['desc'] = array('label' => 'Target', 'description' => '');
  62. $aDisplayConfig['oql'] = array('label' => 'OQL Expression', 'description' => '');
  63. $aDisplayConfig['go'] = array('label' => '', 'description' => '');
  64. $oP->table($aDisplayConfig, $aDisplayData);
  65. if (!empty($sExpression))
  66. {
  67. $oFilter = DBObjectSearch::FromOQL($sExpression);
  68. if ($oFilter)
  69. {
  70. $oP->add("<h3>Query results</h3>\n");
  71. $oP->p('Query expression: '.$oFilter->ToOQL());
  72. $oP->p('Serialized filter: '.$oFilter->serialize());
  73. $oSet = new CMDBObjectSet($oFilter);
  74. $oP->p('The query returned '.$oSet->count().' results(s)');
  75. cmdbAbstractObject::DisplaySet($oP, $oSet);
  76. }
  77. }
  78. }
  79. catch(CoreException $e)
  80. {
  81. $oP->p('<b>An error occured while running the query:</b>');
  82. $oP->p($e->getHtmlDesc());
  83. }
  84. catch(Exception $e)
  85. {
  86. $oP->p('<b>An error occured while running the query:</b>');
  87. $oP->p($e->getMessage());
  88. }
  89. $oP->output();
  90. ?>