run_query.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  7. function ShowExamples($oP, $sExpression)
  8. {
  9. $bUsingExample = false;
  10. $aExamples = array(
  11. 'Pedagogic examples' => array(
  12. "Applications" => "SELECT bizApplication",
  13. "Person having an 'A' in their name" => "SELECT bizPerson AS B WHERE B.name LIKE '%A%'",
  14. "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'",
  15. "IPs in a range" => "SELECT bizDevice AS dev WHERE INET_ATON(dev.mgmt_ip) > INET_ATON('10.22.32.33') AND INET_ATON(dev.mgmt_ip) < INET_ATON('10.22.32.40')"
  16. ),
  17. 'Usefull examples' => array(
  18. "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'",
  19. "My tickets" => "SELECT bizIncidentTicket AS i WHERE i.agent_id = :current_contact_id",
  20. "People being owner of an active ticket" => "SELECT bizPerson AS p JOIN bizIncidentTicket AS i ON i.agent_id = p.id WHERE i.ticket_status != 'Closed'",
  21. "Contracts terminating in the next thirty days" => "SELECT bizContract AS c WHERE c.end_prod > NOW() AND c.end_prod < DATE_ADD(NOW(), INTERVAL 30 DAY)",
  22. "Orphan tickets (opened one hour ago, still not assigned)" => "SELECT bizIncidentTicket AS i WHERE i.start_date < DATE_SUB(NOW(), INTERVAL 60 MINUTE) AND i.ticket_status = 'New'",
  23. "Long lasting incidents (duration > 8 hours)" => "SELECT bizIncidentTicket AS i WHERE i.end_date > DATE_ADD(i.start_date, INTERVAL 8 HOUR)",
  24. ),
  25. );
  26. $aDisplayData = array();
  27. foreach($aExamples as $sTopic => $aQueries)
  28. {
  29. foreach($aQueries as $sDescription => $sOql)
  30. {
  31. $sHighlight = '';
  32. $sDisable = '';
  33. if ($sOql == $sExpression)
  34. {
  35. // this one is currently being tested, highlight it
  36. $sHighlight = "background-color:yellow;";
  37. $sDisable = 'disabled';
  38. // and remember we are testing a query of the list
  39. $bUsingExample = true;
  40. }
  41. //$aDisplayData[$sTopic][] = array(
  42. $aDisplayData['Query examples'][] = array(
  43. 'desc' => "<div style=\"$sHighlight\">".htmlentities($sDescription)."</div>",
  44. 'oql' => "<div style=\"$sHighlight\">".htmlentities($sOql)."</div>",
  45. 'go' => "<form method=\"get\"><input type=\"hidden\" name=\"expression\" value=\"$sOql\"><input type=\"submit\" value=\"Test!\" $sDisable></form>\n",
  46. );
  47. }
  48. }
  49. $aDisplayConfig = array();
  50. $aDisplayConfig['desc'] = array('label' => 'Target', 'description' => '');
  51. $aDisplayConfig['oql'] = array('label' => 'OQL Expression', 'description' => '');
  52. $aDisplayConfig['go'] = array('label' => '', 'description' => '');
  53. foreach ($aDisplayData as $sTopic => $aQueriesDisplayData)
  54. {
  55. $bShowOpened = $bUsingExample;
  56. $oP->StartCollapsibleSection($sTopic, $bShowOpened);
  57. $oP->table($aDisplayConfig, $aQueriesDisplayData);
  58. $oP->EndCollapsibleSection();
  59. }
  60. }
  61. $sOperation = utils::ReadParam('operation', 'menu');
  62. $oContext = new UserContext();
  63. $oAppContext = new ApplicationContext();
  64. $iActiveNodeId = utils::ReadParam('menu', -1);
  65. $currentOrganization = utils::ReadParam('org_id', '');
  66. $oP = new iTopWebPage("iTop - Expression Evaluation", $currentOrganization);
  67. // Main program
  68. $sExpression = utils::ReadParam('expression', '');
  69. $sEncoding = utils::ReadParam('encoding', 'oql');
  70. ShowExamples($oP, $sExpression);
  71. try
  72. {
  73. if ($sEncoding == 'crypted')
  74. {
  75. // Translate $sExpression into a oql expression
  76. $sClearText = base64_decode($sExpression);
  77. echo "<strong>FYI: '$sClearText'</strong><br/>\n";
  78. $oFilter = DBObjectSearch::unserialize($sExpression);
  79. $sExpression = $oFilter->ToOQL();
  80. exit;
  81. }
  82. else
  83. {
  84. // leave $sExpression as is
  85. }
  86. $oP->add("<form method=\"get\">\n");
  87. $oP->add("Expression to evaluate:<br/>\n");
  88. $oP->add("<textarea cols=\"120\" rows=\"8\" name=\"expression\">$sExpression</textarea>\n");
  89. $oP->add("<input type=\"submit\" value=\" Evaluate \">\n");
  90. $oP->add("</form>\n");
  91. if (!empty($sExpression))
  92. {
  93. $oFilter = DBObjectSearch::FromOQL($sExpression);
  94. if ($oFilter)
  95. {
  96. $oP->add("<h3>Query results</h3>\n");
  97. $oResultBlock = new DisplayBlock($oFilter, 'list', false);
  98. $oResultBlock->Display($oP, 1);
  99. $oP->p('');
  100. $oP->StartCollapsibleSection('More info on the query', false);
  101. $oP->p('Query expression redevelopped: '.$oFilter->ToOQL());
  102. $oP->p('Serialized filter: '.$oFilter->serialize());
  103. $oP->EndCollapsibleSection();
  104. }
  105. }
  106. }
  107. catch(CoreException $e)
  108. {
  109. $oP->p('<b>An error occured while running the query:</b>');
  110. $oP->p($e->getHtmlDesc());
  111. }
  112. catch(Exception $e)
  113. {
  114. $oP->p('<b>An error occured while running the query:</b>');
  115. $oP->p($e->getMessage());
  116. }
  117. $oP->output();
  118. ?>